public function __construct() { parent::__construct(); if ($this->_pkCache) { $this->cacheKey = sprintf(TXConfig::getConfig('pkCache'), substr(get_called_class(), 0, -3)); } }
/** * 路由重定向 * @param $url * @return array */ private function reRouter($url) { $path = NULL; $rules = TXConfig::getConfig('routeRule'); foreach ($rules as $key => $value) { $key = trim($key, '/'); if (preg_match_all("/<([\\w_]+):([^>]+)>/", $key, $matchs)) { foreach ($matchs[2] as &$val) { $val = '(' . $val . ')'; } unset($val); $matchs[0][] = '/'; $matchs[0][] = '.'; $matchs[2][] = '\\/'; $matchs[2][] = '\\.'; $key = str_replace($matchs[0], $matchs[2], $key); if (preg_match('/' . $key . '$/', $url, $args)) { foreach ($matchs[1] as $key => $val) { self::$ARGS[$val] = $args[$key + 1]; } if (preg_match_all("/<([\\w_]+)>/", $value, $matchs)) { $replaces = []; foreach ($matchs[1] as &$val) { $replaces[] = isset(self::$ARGS[$val]) ? self::$ARGS[$val] : $val; } $value = str_replace($matchs[0], $replaces, $value); } $path = str_replace($args[0], $value, $url); break; } } } return $path; }
/** * 格式化代码为字符串 * @param int $code * @param array $params * @return string */ private function fmt_code($code, $params) { try { $msgtpl = TXConfig::getConfig($code, 'exception'); } catch (TXException $ex) { //防止异常的死循环 $msgtpl = $ex->getMessage(); } return vsprintf($msgtpl, $params); }
/** * 构造函数 */ public function __construct() { $this->posts = $_POST; $this->params = $_REQUEST; $this->gets = $_GET; //判断是否维护中 if (isMaintenance) { return $this->display('Main/maintenance'); } if ($this->csrfValidate && !TXApp::$base->request->validateCsrfToken()) { header(TXConfig::getConfig(401, 'http')); echo $this->error("Unauthorized"); exit; } // 权限验证 $this->valid_privilege(); TXApp::$base->request->createCsrfToken(); TXApp::$base->request->setCharset(); TXApp::$base->request->setContentType(); }
/** * dynamic create object * @param string $class * @param string $alias * @return TXSingleDAO | mixed */ public static function create($class, $alias = null) { if (null === $alias) { $alias = $class; } if (!isset(self::$objects[$alias])) { //可以不写DAO文件自动建立对象 if (substr($class, -3) == 'DAO') { $key = substr($class, 0, -3); $dbConfig = TXConfig::getConfig('dbConfig', 'database'); if (isset($dbConfig[$key])) { $dao = new TXSingleDAO($dbConfig[$key], $class); self::$objects[$alias] = $dao; } else { self::$objects[$alias] = new $class(); } } else { self::$objects[$alias] = new $class(); } } return self::$objects[$alias]; }
/** * 加载 */ private static function loading() { $lastTime = filemtime(self::$autoPath); // 5秒缓存不更新 if (!$lastTime || time() - $lastTime > TXConfig::getConfig('autoSkipLoad')) { self::$loaders = array(); self::getLoads(__DIR__); self::getLoads(TXApp::$app_root . DS . "controller"); self::getLoads(TXApp::$app_root . DS . "shell"); self::getLoads(TXApp::$app_root . DS . "service"); self::getLoads(TXApp::$app_root . DS . "dao"); self::getLoads(TXApp::$app_root . DS . "form"); self::getLoads(TXApp::$app_root . DS . "event"); self::getLoads(TXApp::$app_root . DS . "model"); //写入文件 if (is_writeable(self::$autoPath)) { file_put_contents(self::$autoPath, "<?php\nreturn " . var_export(self::$loaders, true) . ';'); } else { throw new TXException(1005, array(self::$autoPath)); } } }
/** * 记录错误日志 * @param $message * @param $level */ public static function addError($message, $level = ERROR) { $errorLevel = TXConfig::getConfig('errorLevel'); if ($errorLevel < $level) { return; } if (is_array($message) || is_object($message)) { $message = var_export($message, true); } $header = sprintf("[%s]%s:%s[%s] %s\n", isset(self::$LEVELS[$level]) ? self::$LEVELS[$level] : 'ERROR', date('Y-m-d H:i:s'), substr(microtime(), 2, 3), RUN_SHELL ? 'localhost' : TXApp::$base->request->getUserIp(), TXApp::$base->request->getUrl()); $message = "{$header} {$message}\n"; $filename = sprintf("%s/error_%s.log", TXApp::$log_root, date('Y-m-d')); file_put_contents($filename, $message, FILE_APPEND | LOCK_EX); }
/** * sql query data * @param string $sql * @param $key * @param int $mode * @return array */ public function sql($sql, $key = null, $mode = self::FETCH_TYPE_ALL) { $start = microtime(true); $rs = mysqli_query($this->handler, $sql); $time = (microtime(true) - $start) * 1000; if ($time > (TXConfig::getConfig('slowQuery') ?: 1000)) { TXLogger::addError(sprintf('Slow Query: %s [%sms]', $sql, $time), WARNING); TXLogger::warn(sprintf('Slow Query: %s [%sms]', $sql, $time)); } if ($rs) { if ($mode == self::FETCH_TYPE_ALL) { $result = array(); while ($row = mysqli_fetch_assoc($rs)) { if ($key) { $result[$row[$key]] = $row; } else { $result[] = $row; } } return $result; } else { $result = mysqli_fetch_assoc($rs) ?: []; } return $result; } else { TXLogger::addError(sprintf("sql Error: %s [%s]", mysqli_error($this->handler), $sql)); TXLogger::error(sprintf("%s [%s]", mysqli_error($this->handler), $sql), 'sql Error:'); return []; } }
/** * 验证csrfToken */ public function validateCsrfToken() { if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { $method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); } else { $method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET'; } if (in_array($method, ['GET', 'HEAD', 'OPTIONS'], true)) { return true; } $ips = TXConfig::getConfig('csrfWhiteIps'); foreach ($ips as $ip) { if ($this->matchCIDR($this->getUserIp(), $ip)) { return true; } } $trueToken = TXConfig::getConfig('trueToken'); $csrfPost = TXConfig::getConfig('csrfPost'); $csrfHeader = 'HTTP_' . str_replace('-', '_', TXConfig::getConfig('csrfHeader')); $trueToken = $_COOKIE[$trueToken]; $token = isset($_POST[$csrfPost]) ? $_POST[$csrfPost] : (isset($_SERVER[$csrfHeader]) ? $_SERVER[$csrfHeader] : null); return md5($trueToken) === $token; }