Ejemplo n.º 1
0
 /**
  * 判断是否可以合表
  * @param $dao TXDAO
  * @return bool
  */
 protected function checkConfig($dao)
 {
     $tMaster = is_array($this->dbConfig) ? $this->dbConfig[0] : $this->dbConfig;
     $tSlave = is_array($this->dbConfig) ? $this->dbConfig[1] : $this->dbConfig;
     $dDbConfig = $dao->getDbConfig();
     $dMaster = is_array($dDbConfig) ? $dDbConfig[0] : $dDbConfig;
     $dSlave = is_array($dDbConfig) ? $dDbConfig[1] : $dDbConfig;
     if ($tMaster === $dMaster && $tSlave === $dSlave) {
         return true;
     }
     if ($tMaster !== $dMaster) {
         $tConfig = TXConfig::getAppConfig($tMaster, 'dns');
         $dConfig = TXConfig::getAppConfig($dMaster, 'dns');
         unset($tConfig['database']);
         unset($dConfig['database']);
         if (array_diff($tConfig, $dConfig)) {
             return false;
         }
     } else {
         if ($tSlave !== $dSlave) {
             $tConfig = TXConfig::getAppConfig($tSlave, 'dns');
             $dConfig = TXConfig::getAppConfig($dSlave, 'dns');
             unset($tConfig['database']);
             unset($dConfig['database']);
             if (array_diff($tConfig, $dConfig)) {
                 return false;
             }
         }
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * 路由重定向
  * @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;
 }
Ejemplo n.º 3
0
 public function __construct()
 {
     parent::__construct();
     if ($this->_pkCache) {
         $this->cacheKey = sprintf(TXConfig::getConfig('pkCache'), substr(get_called_class(), 0, -3));
     }
 }
Ejemplo n.º 4
0
 public static function instance()
 {
     if (null === self::$instance) {
         $memcacheCfg = TXConfig::getAppConfig('memcache', 'dns');
         self::$instance = new self($memcacheCfg);
     }
     return self::$instance;
 }
Ejemplo n.º 5
0
 public static function instance()
 {
     if (null === self::$instance) {
         $SocketConfig = TXConfig::getAppConfig('socket', 'dns');
         self::$instance = new self($SocketConfig);
     }
     return self::$instance;
 }
Ejemplo n.º 6
0
 public static function instance()
 {
     if (null === self::$_instance) {
         $config = TXConfig::getAppConfig('redis', 'dns');
         self::$_instance = new self($config);
     }
     return self::$_instance;
 }
Ejemplo n.º 7
0
 /**
  * 格式化代码为字符串
  * @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);
 }
Ejemplo n.º 8
0
 public function setDbTable($table)
 {
     if (null === $this->database) {
         if (is_string($this->dbConfig) && ($db = TXConfig::getAppConfig($this->dbConfig, 'dns')['database'])) {
             $this->database = $db;
         } else {
             if (is_array($this->dbConfig)) {
                 $master = TXConfig::getAppConfig($this->dbConfig[0], 'dns')['database'];
                 $slave = TXConfig::getAppConfig($this->dbConfig[1], 'dns')['database'];
                 if ($master === $slave) {
                     $this->database = $master;
                 } else {
                     throw new TXException(3008, array($slave, $master));
                 }
             }
         }
     }
     $this->dbTable = $this->database . ".`{$table}`";
 }
Ejemplo n.º 9
0
 /**
  * 构造函数
  */
 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();
 }
Ejemplo n.º 10
0
 /**
  * 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];
 }
Ejemplo n.º 11
0
 /**
  * 加载
  */
 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));
         }
     }
 }
Ejemplo n.º 12
0
 /**
  * @param $view
  * @param array $array
  * @param array $objects 直接使用参数
  * @return TXResponse
  */
 public function display($view, $array = array(), $objects = array())
 {
     $objects = array_merge(array('webRoot' => TXConfig::getAppConfig('webRoot'), 'CDN_ROOT' => TXConfig::getAppConfig('CDN_ROOT')), $objects);
     return parent::display($view, $array, $objects);
 }
Ejemplo n.º 13
0
 /**
  * 记录错误日志
  * @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);
 }
Ejemplo n.º 14
0
 /**
  * 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 [];
     }
 }
Ejemplo n.º 15
0
 /**
  * 验证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;
 }