diff --git a/core.php b/core.php index d0aea5c..223b495 100644 --- a/core.php +++ b/core.php @@ -58,8 +58,11 @@ function AppRun(string $entry, array $nec = null): void if (!defined('ROUTE')) { $router = [ + 'mod'=>0, 'ctrl' => empty($_GET['c']) ? 'index' : $_GET['c'], 'act' => empty($_GET['a']) ? 'index' : $_GET['a'], + 'uri' => $_SERVER['REQUEST_URI'], + 'query'=> $_GET, ]; empty($GLOBALS['ZPHP_CONFIG']['ROUTER']['module']) || $router['module'] = empty($_GET['m']) ? 'index' : $_GET['m']; define('ROUTE', $router); @@ -145,40 +148,54 @@ function ArrayIsList (&$arr) { } return $isList; } -function ExportArray ($arr, $quoteValStr = true, $quoteKeyStr = true) { - if (!is_array($arr) && !is_object($arr)) { - return var_export($arr, true); - } - if (!$arr || (!is_array($arr) && !$arr = (array)$arr)) { +function ExportArray (array $arr, bool $escape = false, string $indent = '', $prefix = '', $indentNum = 0, $forceStringValue = true) { + if (!$arr) { return '[]'; } + if ($escape) { + $search = ["'", "\n","\t","\r","\f","\\","\v"]; + $replace = ["\'", '\n','\t','\r','\f','\\','\v']; + } else { + $search = "'"; + $replace = "\'"; + } + if (ArrayIsList($arr)) { foreach($arr as $v) { if (is_array($v)) { - $slice[] = ExportArray($v); + $slice[] = ExportArray($v, $escape, $indent, $prefix, 1 + $indentNum, $forceStringValue); } elseif (null === $v) { $slice[] = 'null'; } else { - $quoteValStr && is_string($v) && $v = "'" . str_replace("'", "\'", $v) . "'"; + ($forceStringValue || is_string($v)) && $v = "'" . str_replace($search, $replace, $v) . "'"; $slice[] = $v; } } } else { foreach($arr as $k=>$v) { - is_string($k) && $k = str_replace(["'", '"'], ["\'", '\"'], $k); - $key = $quoteKeyStr && is_string($k) ? "'{$k}'=>" : "{$k}=>"; + if (is_string($k)) { + $k = str_replace($search, $replace, $k); + $key = "'{$k}'=>"; + } else { + $key = "{$k}=>"; + } if (is_array($v)) { - $slice[] = $key . ExportArray($v); + $slice[] = $key . ExportArray($v, $escape, $indent, $prefix, 1 + $indentNum, $forceStringValue); } elseif (null === $v) { $slice[] = "{$key}null"; } else { - $quoteValStr && is_string($v) && $v = "'" . str_replace("'", "\'", $v) . "'"; + ($forceStringValue || is_string($v)) && $v = "'" . str_replace($search, $replace, $v) . "'"; $slice[] = $key . $v; } } } - return '[' . implode(',', $slice) . ']'; + if ($indent) { + $pre = $prefix . str_repeat($indent, $indentNum); + return "[\n{$pre}{$indent}" . implode(",\n{$pre}{$indent}", $slice) . "\n{$pre}]"; + } else { + return '[' . implode(',', $slice) . ']'; + } } function P($var, bool $echo = true): string { @@ -271,7 +288,7 @@ function Page($cfg, $return = false): array $var = $cfg['var'] ?? 'p'; $mod = $cfg['mod'] ?? null; $nourl = $cfg['nourl'] ?? 'javascript:;'; - $params = ROUTE['params'] ?? false; + $params = ROUTE['params'] ?? []; $query = $_GET; foreach ($return as $v) { switch ($v) { diff --git a/lib/z/dbc/cache.php b/lib/z/dbc/cache.php index 8591454..dc1249f 100644 --- a/lib/z/dbc/cache.php +++ b/lib/z/dbc/cache.php @@ -33,7 +33,7 @@ protected function cacheDir (string $dbname = '', string $table = '') $dbname || $dbname = $this->DB_CONFIG['dbname']; return P_CACHE . "{$this->DB_CONFIG['cache_dir']}/{$dbname}/{$table}"; } - function CleanCache (string $mod = '', string $dbname, string $table = '') + function CleanCache (string $mod = '', string $dbname, string $table = ''): int { $mod || $mod = $this->DB_CONFIG['cache_mod']; switch ($mod) { @@ -44,11 +44,11 @@ function CleanCache (string $mod = '', string $dbname, string $table = '') return $this->delMemcached($dbname, $table); break; default: - return $this->delFile($dbname, $table); + return (int)($this->delFile($dbname, $table)); break; } } - private function delFile(string $dbname = '', string $table = '') + private function delFile(string $dbname = '', string $table = ''): int | false { $dbname || $dbname = $this->DB_CONFIG['dbname']; $dir = $this->cacheDir($dbname, $table); @@ -72,7 +72,7 @@ protected function delRedis (string $dbname = '', string $table = ''): int } return $result; } - protected function delMemcached($dbname = '', $table) + protected function delMemcached($dbname = '', $table): int { $dbname || $dbname = $this->DB_CONFIG['dbname']; $path = "DB:{$dbname}:"; diff --git a/lib/z/dbc/db.php b/lib/z/dbc/db.php index 70b81fc..5c851b0 100644 --- a/lib/z/dbc/db.php +++ b/lib/z/dbc/db.php @@ -92,9 +92,14 @@ public function Wrap (string|array $key, string $alias = '') return is_array($key) ? $alias . implode(",{$alias}", $key) : $alias . $key; } if (is_array($key)) { - $keys = array_map(function ($k) { - return $this->WrapSql($k); - }, $key); + $keys = []; + foreach ($key as $k=>$v) { + if (is_int($k)) { + $keys[] = $this->WrapSql($v); + } else { + $keys[] = $this->WrapSql($k) . " AS {$this->DB_PREG_WRAP_L}{$v}{$this->DB_PREG_WRAP_R}"; + } + } return implode(',', $keys); } return $this->WrapSql($key); @@ -185,7 +190,7 @@ public function Commit(): bool } public function Tmp(string $sql, string $alias = 'z'): static { - $this->DB_TMP = "({$sql}) AS {$alias}"; + $this->DB_TMP = "({$sql}) AS {$this->DB_PREG_WRAP_L}{$alias}{$this->DB_PREG_WRAP_R}"; return $this; } public function Table(string $table): static @@ -423,10 +428,14 @@ public function Fetch(bool $lock = false): \PDOStatement $this->DB_done(); return $stmt; } - public function Find(string $field = '', bool $lock = false) + public function Find(string | bool $field = false, bool $lock = false) { - $fetch = \PDO::FETCH_ASSOC; - $field && ($this->DB_FIELD = $this->WrapSql($field)) && $fetch = \PDO::FETCH_COLUMN; + if ($field) { + $fetch = \PDO::FETCH_COLUMN; + is_bool($field) || $this->DB_FIELD = $this->WrapSql($field); + } else { + $fetch = \PDO::FETCH_ASSOC; + } $sql = $this->DB_sql(); $field = $this->DB_field(); $sql = "SELECT {$field} FROM {$sql}"; @@ -499,10 +508,14 @@ public function CacheSelect(int $fetch): array } return $result; } - public function Select(string $field = null, bool $lock = false): array + public function Select(string | bool $field = false, bool $lock = false): array { - $fetch = \PDO::FETCH_ASSOC; - $field && ($this->DB_FIELD = $this->WrapSql($field)) && $fetch = \PDO::FETCH_COLUMN; + if ($field) { + $fetch = \PDO::FETCH_COLUMN; + is_bool($field) || $this->DB_FIELD = $this->WrapSql($field); + } else { + $fetch = \PDO::FETCH_ASSOC; + } if ($lock || !$this->Z_CACHE) { $field = $this->DB_field(); $this->DB_PAGE && $this->DB_page(); diff --git a/lib/z/input.php b/lib/z/input.php index a9772b0..d95cd22 100644 --- a/lib/z/input.php +++ b/lib/z/input.php @@ -639,9 +639,9 @@ public static function Input (string $key, string $dim = '', callable $filter = if ($key) { $data = match (METHOD) { 'GET'=>$_GET[$key] ?? null, - 'POST'=>$_POST[$key] ?? null, - default=> INPUT[$key], - } ?? $_GET[$key] ?? $_POST[$key] ?? INPUT[$key] ?? null; + 'POST'=>$_POST[$key] ?? $_GET[$key] ?? null, + default=> INPUT[$key] ?? $_GET[$key] ?? null, + }; if (null === $data) { return null; } @@ -670,50 +670,45 @@ public static function InputInt (string $key, string $dim = '', callable $filter { $data = match (METHOD) { 'GET'=>$_GET[$key] ?? null, - 'POST'=>$_POST[$key] ?? null, - default=> INPUT, + 'POST'=>$_POST[$key] ?? $_GET[$key] ?? null, + default=> INPUT[$key] ?? $_GET[$key] ?? null, }; - null === $data && $data = $_GET[$key] ?? null; return null === $data ? null : self::ParseInt($data, $dim, $filter); } public static function InputFloat (string $key, string $dim = '', callable $filter = null): float|array|null { $data = match (METHOD) { 'GET'=>$_GET[$key] ?? null, - 'POST'=>$_POST[$key] ?? null, - default=> INPUT, + 'POST'=>$_POST[$key] ?? $_GET[$key] ?? null, + default=> INPUT[$key] ?? $_GET[$key] ?? null, }; - null === $data && $data = $_GET[$key] ?? null; return null === $data ? null : self::ParseFloat($data, $dim, $filter); } public static function InputFixed (string $key, string $dim = '', callable $filter = null): float|array|null { $data = match (METHOD) { 'GET'=>$_GET[$key] ?? null, - 'POST'=>$_POST[$key] ?? null, - default=> INPUT, + 'POST'=>$_POST[$key] ?? $_GET[$key] ?? null, + default=> INPUT[$key] ?? $_GET[$key] ?? null, }; - null === $data && $data = $_GET[$key] ?? null; return null === $data ? null : self::ParseFixed($data, $dim, $filter); } public static function InputMoney (string $key, string $dim = '', callable $filter = null) { $data = match (METHOD) { 'GET'=>$_GET[$key] ?? null, - 'POST'=>$_POST[$key] ?? null, - default=> INPUT, + 'POST'=>$_POST[$key] ?? $_GET[$key] ?? null, + default=> INPUT[$key] ?? $_GET[$key] ?? null, }; - null === $data && $data = $_GET[$key] ?? null; return null === $data ? null : self::ParseMoney($data, $dim, $filter); } public static function InputBool (string $key, bool $toInt = false): bool|int|null { $data = match (METHOD) { 'GET'=>$_GET[$key] ?? null, - 'POST'=>$_POST[$key] ?? null, - default=> INPUT, + 'POST'=>$_POST[$key] ?? $_GET[$key] ?? null, + default=> INPUT[$key] ?? $_GET[$key] ?? null, }; - null === $data && $data = $_GET[$key] ?? null; null === $data || $data = filter_var($data, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE); if (null === $data) { return null; diff --git a/nec/z/router.php b/nec/z/router.php index f901060..dd51cff 100644 --- a/nec/z/router.php +++ b/nec/z/router.php @@ -56,6 +56,7 @@ public static function setup(): void { $route['query'] = isset($route['params']) ? $route['params'] + $_GET : $_GET; $route['uri'] = $_SERVER['REQUEST_URI']; $route['app'] = APP_NAME; + $route['mod'] = self::$MOD; define('ROUTE', $route); }); } diff --git a/nec/z/view.php b/nec/z/view.php index 2bd4c54..cb709e2 100644 --- a/nec/z/view.php +++ b/nec/z/view.php @@ -211,6 +211,17 @@ private static function replaceTemplate(\DOMDocument $dom, string $file = ''): v } } + private static function checkTplParams (&$params) + { + foreach ($params as &$v) { + if (is_array($v)) { + self::checkTplParams($v); + } elseif (is_string($v)) { + $v = str_replace([''], ['<?', '?>'], $v); + } + } + } + public static function GetRun (string $tpl): array { $arr = explode('/', $tpl); @@ -218,9 +229,12 @@ public static function GetRun (string $tpl): array $name = $arr[$len - 1]; return [P_RUN . APP_NAME . '/' . THEME, $arr[$len - 2] . '/' . explode('.', $name)[0] . '.php']; } - public static function Fetch(string $filename = ''): string + public static function Fetch(string $filename = '', bool $isDisplay = false): string { - self::$PARAMS && extract(self::$PARAMS); + if (self::$PARAMS) { + $isDisplay || self::checkTplParams(self::$PARAMS); + extract(self::$PARAMS); + } ob_start() && require self::GetCompile($filename); return ob_get_clean(); } @@ -427,7 +441,7 @@ private static function replaceCustomTags(string $name, array $cfg): void $var = '$var'; } $code = str_contains($var, ',') ? "list({$var})=" : "{$var}="; - $code .= $call . '(' . ExportArray($args, false) . ');?'; + $code .= $call . '(' . ExportArray($args) . ');?'; } else { list($pre, $dd) = $call($attrs, $args); $code = $pre . '?'; @@ -480,7 +494,10 @@ public static function Display(string $name = ''): void { if (self::$CACHE) { $ret = cache::SetFileCache(self::$CACHE[1], function (): string { - self::$PARAMS && extract(self::$PARAMS); + if (self::$PARAMS) { + self::checkTplParams(self::$PARAMS); + extract(self::$PARAMS); + } ob_start() && require self::GetCompile(self::$CACHE[0]); return ob_get_clean(); });