public static function runlog($message, $level, $repeatcheck = false) { $request = \yk::app()->getDispatcher()->getRequest(); $env = array('clientip' => g('_clientip'), 'uid' => g('uid'), 'siteurl' => APP_NAME . '/' . $request->getModuleName() . '/' . $request->getControllerName() . '/' . $request->getActionName()); $log = $env['clientip'] . "\t{$env['uid']}\t{$env['siteurl']}\t" . "\n" . $message; self::write($log, $level, $repeatcheck); }
public static function get() { $uid = g('uid'); $str1 = pack('VVV', $uid, date('w'), date('d')); $request = \yk::app()->getDispatcher()->getRequest(); $str2 = $request->getModuleName(); $str3 = $request->getControllerName(); return substr(md5($str1 . $str2 . $str3), 0, -8); }
public function __construct($environ = 'product') { define('APP_PATH', MAIN_PATH . '/application/' . $environ); define('APP_NAME', $environ); if (!self::$_yaf_app) { self::$_yaf_app = new Yaf\Application(MAIN_PATH . '/conf/app.ini', $environ); } if (!self::$_yaf_app) { die('yaf init error'); } spl_autoload_register([__CLASS__, 'autoload'], true, true); (new yk\core())->init(self::$_yaf_app); }
/** * @param string $name 键值 * @param string $func filter的类型 * @param string $extra filter的额外参数 * @param string $msg 不正确时报错信息,以null|开头时表示非必需 * @param string $type 获取方法 */ public static function get($name, $func, $msg, $extra, $type = self::REQUEST) { $value = null; while ($type && $value === null) { if ($type & self::GET) { $value = isset($_GET[$name]) ? $_GET[$name] : $value; $type ^= self::GET; continue; } if ($type & self::POST) { $value = isset($_POST[$name]) ? $_POST[$name] : $value; $type ^= self::POST; continue; } if ($type & self::PARAMS) { $value = \yk::app()->getDispatcher()->getRequest()->getParam($name); $type ^= self::PARAMS; continue; } if ($type & self::COOKIE) { $tvalue = cookie($name); $value = $tvalue !== null ? $tvalue : $value; $type ^= self::COOKIE; continue; } break; } $ignore = $msg === false; $need = $ignore || $msg == null || strpos($msg, 'null|') === 0 ? false : true; $msg = !$need && is_string($msg) ? substr($msg, 5) : $msg; if (!$need && $value === null || $value !== null && filter::verify($value, $func, $extra)) { return $value; } else { if (!$ignore) { throw new error(!is_null($msg) ? $msg : "you post {$name} variable's value is wrong"); } else { return null; } } }
public function disableView() { \yk::app()->getDispatcher()->disableView(); }
public function parse($tplfile, $tpl, $path) { $fp = fopen($tplfile, 'rb'); if ($fp) { $content = fread($fp, filesize($tplfile)); fclose($fp); } else { throw new error("{$tpl} template file don't read!"); } //变量 $var_regexp = self::VAR_REGEXP; //常量 $const_regexp = self::CONST_REGEXP; //解析动态模版, 最高允许8层 $i = 0; while ($i < 8 && strpos($content, '{subtemplate') !== false) { $content = preg_replace_callback('/[\\n\\r\\t]*(?:\\<\\!\\-\\-)?\\{subtemplate\\s+(.+?)\\}(\\-\\-\\>)?[\\r\\r\\t]*/is', function ($matches) use($path) { $content = implode('', file(view_tplfile($matches[1]))); if ($content) { $this->_subtemplate_list[] = $matches[1]; return $content; } else { return "<!--{$matches[1]} no find-->"; } }, $content); $i++; } unset($i); //去掉tab $content = preg_replace('/([\\n\\r])+\\t+/s', "\$1", $content); //统一{}和<!--{}--> $content = preg_replace('/\\<\\!\\-\\-(\\{.+?\\})\\-\\-\\>/s', "\$1", $content); //保存不解析的内容 $content = preg_replace_callback('/[\\n\\r\\t]*\\{html\\}(.+?)\\{\\/html\\}/is', function ($matches) { $this->_func_store['search'][$this->_func_index] = $search = '<!--FUNC-HTML_' . $this->_func_index . '-->'; $this->_func_store['replace'][$this->_func_index] = $matches[1]; $this->_func_index++; return $search; }, $content); //解析date $content = preg_replace_callback('/[\\n\\r\\t]*\\{date\\s+(.+?)(?:\\s+(.+?))?\\s*\\}/is', function ($matches) { if (isset($matches[2])) { return $this->_ay_func("{$matches['1']}, {$matches['2']}", 'ygmdate'); } else { return $this->_ay_func("{$matches['1']}", 'ygmdate'); } }, $content); //解析echo,php原形echo $content = preg_replace_callback('/[\\n\\r\\t]*\\{echo\\s+(.+?)(?:;)?[\\n\\r\\t]*\\}/is', function ($matches) { $echo = $this->_addquote($matches[1]); $this->_func_store['search'][$this->_func_index] = $search = '<!--FUNC-ECHO_' . $this->_func_index . '-->'; $this->_func_store['replace'][$this->_func_index] = "<?php echo {$echo};?>"; $this->_func_index++; return $search; }, $content); //解析eval,将内容当作php代码 $content = preg_replace_callback('/[\\n\\r\\t]*\\{eval\\s+(.+?)\\s*\\}/is', function ($matches) { $eval = $this->_addquote($matches[1]); $this->_func_store['search'][$this->_func_index] = $search = '<!--FUNC-EVAL_' . $this->_func_index . '-->'; $this->_func_store['replace'][$this->_func_index] = "<?php {$eval};?>"; //后面加上;总是没错的 $this->_func_index++; return $search; }, $content); //解析yaf\registry里的值 $content = preg_replace_callback('/\\{G\\s+([\\w\\/]+)\\s*\\}/s', function ($matches) { $keys = trim($matches[1], '/'); return $this->_ay_func("'{$keys}'", 'g'); }, $content); //解析CSS $content = preg_replace_callback('/\\{\\-css\\-\\}/s', function ($matches) { $request = \yk::app()->getDispatcher()->getRequest(); $curmca = strtolower($request->getModuleName() . '/' . $request->getControllerName() . '/' . $request->getActionName()); $cssdata = file_get_contents(view_cachefile('module', '.css')); $common_cache = view_cachefile('common', '.css'); $module_css[] = file_exists($common_cache) ? file_get_contents($common_cache) : ''; $cssdata = preg_replace_callback('/\\[(.+?)\\](.+?)\\[end\\]/s', function ($m) use($curmca, &$module_css) { foreach (explode(',', $m[1]) as $name) { if (strpos($curmca, trim($name)) === 0) { $module_css[] = $m[2]; } } return ''; }, $cssdata); $cssname = g('_config/view/name') . '_' . md5(APP_NAME . '/' . $curmca) . '.css'; if (!empty($module_css)) { $fp = fopen(MAIN_PATH . '/public/css/' . $cssname, 'wb'); if ($fp) { fwrite($fp, implode('', $module_css)); fclose($fp); } else { throw new error("css module can't write to ./css path"); } return '<link rel="stylesheet" type="text/css" href="/css/' . $cssname . '" />'; } else { return ''; } }, $content); //变量替换 $content = preg_replace('/\\{(\\$[a-zA-Z0-9_\\-\\>\\[\\]\'\\"\\$\\.\\x7f-\\xff]+)\\}/s', "<?=\$1?>", $content); $content = preg_replace_callback("/{$var_regexp}/s", [$this, '_ay_var_revise'], $content); $content = preg_replace_callback("/\\<\\?\\=\\<\\?\\={$var_regexp}\\?\\>\\?\\>/s", function ($matches) { return $this->_addquote('<?=' . $matches[1] . '?>'); }, $content); //----------------------------解析end----------------------------------------- //动态模版是包含的,所以需要额外检查是否更新了 $header = ''; if (!empty($this->_subtemplate_list)) { $header .= "\n0 "; foreach ($this->_subtemplate_list as $file) { $header .= "|| view_ckreftpl('{$tpl}', '{$file}', '" . time() . "')\n"; } $header .= ';'; } //加上头信息 $content = "<?php {$header}?>\n{$content}"; //解析嵌套模版 $content = preg_replace_callback('/[\\n\\r\\t]*\\{template\\s+(.+?)\\}[\\n\\r\\t]*/is', function ($matches) { return '<?php include view_template(\'' . $matches[1] . '\');?>'; }, $content); //解析if elseif else /if $content = preg_replace_callback('/([\\n\\r\\t]*)\\{if\\s*(.+?)\\s*\\}([\\n\\r\\t]*)/is', function ($matches) { return $this->_stripvtags($matches[1] . '<?php if(' . $matches[2] . ') { ?>' . $matches[3]); }, $content); $content = preg_replace_callback('/([\\n\\r\\t]*)\\{elseif\\s*(.+?)\\s*\\}([\\n\\r\\t]*)/is', function ($matches) { return $this->_stripvtags($matches[1] . '<?php }elseif(' . $matches[2] . ') { ?>' . $matches[3]); }, $content); $content = preg_replace('/([\\n\\r\\t]*)\\{else\\s*\\}([\\n\\r\\t]*)/is', "\$1<?php }else { ?>\$2", $content); $content = preg_replace('/([\\n\\r\\t]*)\\{\\/if\\s*\\}([\\n\\r\\t]*)/is', "\$1<?php } ?>\$2", $content); //解析foreach // foreach[(]$array [as] [$k] [=>] $v[)] 其中[]内可省略 //loop 是 foreach的别名 $content = preg_replace('/\\{foreach(.+?)\\}/is', "{loop\$1}", $content); //抽出()符号 $content = preg_replace('/[\\n\\r\\t]*\\{loop[\\s\\(]*(.+?)[\\s\\)]*\\}/is', "{loop \$1}", $content); //抽出as $content = preg_replace('/\\{loop[\\s\\(]*(.+?)\\s+as\\s+(.+?)[\\s\\)]*\\}/is', "{loop \$1 \$2}", $content); //抽出=> $content = preg_replace('/\\{loop[\\s\\(]*(.+?)\\s*\\=\\>\\s*(.+?)[\\s\\)]*\\}/is', "{loop \$1 \$2}", $content); //正式解析loop $content = preg_replace_callback('/\\{loop\\s+(\\S+)\\s+(\\S+)\\}/is', function ($matches) { $str = "<?php if(is_array({$matches[1]}) && !empty({$matches[1]})) foreach({$matches[1]} as {$matches[2]} ){ ?>"; return $this->_stripvtags($str); }, $content); $content = preg_replace_callback('/\\{loop\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\}/is', function ($matches) { $str = "<?php if(is_array({$matches[1]}) && !empty({$matches[1]})) foreach({$matches[1]} as {$matches['2']} => {$matches['3']} ){ ?>"; return $this->_stripvtags($str); }, $content); //解析loop foreach 的空数据 //解析empty $content = preg_replace('/\\{empty\\}/', "<?php }else{ ?>", $content); $content = preg_replace('/([\\n\\r\\t]*)\\{\\/(?:foreach|loop)\\s*\\}([\\n\\r\\t]*)/is', "\$1<?php } ?>\$2", $content); //常量替换, 避免污染表达式,以及特定的符号 $content = preg_replace("/\\{{$const_regexp}\\}/s", "<?=\$1?>", $content); //恢复方法 if ($this->_func_index > 0) { $content = str_replace($this->_func_store['search'], $this->_func_store['replace'], $content); } /*恢复<?=$var?> */ $content = preg_replace('/\\<\\?\\=(.+?)\\?\\>/is', "<?php echo \$1; ?>", $content); /*去掉相邻的?><?php */ $content = preg_replace('/\\?\\>[\\n\\r]*\\<\\?php/is', " ", $content); return $content; }
public function render($view_path, $tpl_vars = NULL) { if (is_array($tpl_vars)) { foreach ($tpl_vars as $k => $v) { $this->_tpl_val[$k] = $v; } } if (g('_inajax')) { return !empty($this->_tpl_val) ? json_encode($this->_tpl_val, JSON_UNESCAPED_UNICODE) : null; } else { g('_formhash', \common\formhash::get()); if (strpos($view_path, '/') === 0) { $content = $this->checkRender($view_path); } else { $module = strtolower(\yk::app()->getDispatcher()->getRequest()->getModuleName()); $content = $this->checkRender($module . '/' . $view_path); } try { ob_start(); extract($this->_tpl_val); /*eval('?>'. $content);*/ include $content; } catch (\Exception $e) { ob_get_clean(); throw $e; } $output = ob_get_clean(); return $output; } }