コード例 #1
0
ファイル: Mysql.php プロジェクト: minowu/smartthink
 /**
  * 连接数据库方法
  * @access public
  * @throws ThinkExecption
  */
 public function connect($config = '', $linkNum = 0, $force = false)
 {
     if (!isset($this->linkID[$linkNum])) {
         if (empty($config)) {
             $config = $this->config;
         }
         // 处理不带端口号的socket连接情况
         $host = $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '');
         // 是否长连接
         $pconnect = !empty($config['params']['persist']) ? $config['params']['persist'] : $this->pconnect;
         if ($pconnect) {
             $this->linkID[$linkNum] = mysql_pconnect($host, $config['username'], $config['password'], 131072);
         } else {
             $this->linkID[$linkNum] = mysql_connect($host, $config['username'], $config['password'], true, 131072);
         }
         if (!$this->linkID[$linkNum] || !empty($config['database']) && !mysql_select_db($config['database'], $this->linkID[$linkNum])) {
             Debug::throw_exception(mysql_error());
         }
         $dbVersion = mysql_get_server_info($this->linkID[$linkNum]);
         //使用UTF8存取数据库
         mysql_query("SET NAMES '" . C('DB_CHARSET') . "'", $this->linkID[$linkNum]);
         //设置 sql_model
         if ($dbVersion > '5.0.1') {
             mysql_query("SET sql_mode=''", $this->linkID[$linkNum]);
         }
         // 标记连接成功
         $this->connected = true;
         // 注销数据库连接配置信息
         if (1 != C('DB_DEPLOY_TYPE')) {
             unset($this->config);
         }
     }
     return $this->linkID[$linkNum];
 }
コード例 #2
0
ファイル: WriteHtmlCache.php プロジェクト: minowu/smartthink
 public function run(&$content)
 {
     if (C('HTML_CACHE_ON') && defined('HTML_FILE_NAME')) {
         //静态文件写入
         // 如果开启HTML功能 检查并重写HTML文件
         // 没有模版的操作不生成静态文件
         if (!is_dir(dirname(HTML_FILE_NAME))) {
             mkdir(dirname(HTML_FILE_NAME), 0755, true);
         }
         if (false === file_put_contents(HTML_FILE_NAME, $content)) {
             Debug::throw_exception(L('_CACHE_WRITE_ERROR_') . ':' . HTML_FILE_NAME);
         }
     }
 }
コード例 #3
0
ファイル: functions.php プロジェクト: minowu/smartthink
/**
 * 重写C方法,改为引用Config方法
 *
 * @param string $name
 */
function C($name = null, $value = null)
{
    // 无参数时获取所有
    if (empty($name)) {
        return Config::getAll();
    }
    // 字符串,为空则获取
    if (is_string($name) && is_null($value)) {
        return Config::get($name);
    }
    // 字符串,不为空则单个设置
    if (is_string($name) && !is_null($value)) {
        return Config::set($name, $value);
    }
    // 数组设置
    if (is_array($name)) {
        return Config::setAll($name);
    }
    // 避免非法参数
    Debug::throw_exception('C funtion error!');
}
コード例 #4
0
 /**
  * 自动定位模板文件
  * @access private
  * @param string $templateFile 文件名
  * @return string
  */
 private function parseTemplateFile($templateFile)
 {
     if ('' == $templateFile) {
         // 如果模板文件名为空 按照默认规则定位
         $templateFile = C('TEMPLATE_NAME');
     } elseif (false === strpos($templateFile, C('TMPL_TEMPLATE_SUFFIX'))) {
         // 解析规则为 模板主题:模块:操作 不支持 跨项目和跨分组调用
         $path = explode(':', $templateFile);
         $action = array_pop($path);
         $module = !empty($path) ? array_pop($path) : CONTROLLER_NAME;
         if (!empty($path)) {
             // 设置模板主题
             $path = dirname(THEME_PATH) . '/' . array_pop($path) . '/';
         } else {
             $path = THEME_PATH;
         }
         $templateFile = $path . $module . C('TMPL_FILE_DEPR') . $action . C('TMPL_TEMPLATE_SUFFIX');
     }
     if (!File::exists_case($templateFile)) {
         Debug::throw_exception(L('_TEMPLATE_NOT_EXIST_') . '[' . $templateFile . ']');
     }
     return $templateFile;
 }
コード例 #5
0
ファイル: Controller.php プロジェクト: minowu/smartthink
 /**
  * 魔术方法 有不存在的操作的时候执行
  *
  * @param string $method
  * @param array $args
  * @return mixed
  */
 public function __call($method, $args)
 {
     if (0 === strcasecmp($method, ACTION_NAME . C('ACTION_SUFFIX'))) {
         if (method_exists($this, '_empty')) {
             // 如果定义了_empty操作 则调用
             $this->_empty($method, $args);
         } elseif (File::exists_case(C('TEMPLATE_NAME'))) {
             // 检查是否存在默认模版 如果有直接输出模版
             $this->display();
         } elseif (function_exists('__hack_action')) {
             // hack 方式定义扩展操作
             __hack_action();
         } else {
             Debug::output(new Exception("Controller method 无效,\"" . ACTION_NAME . "\""));
         }
     } else {
         switch (strtolower($method)) {
             // 判断提交方式
             case 'ispost':
             case 'isget':
             case 'ishead':
             case 'isdelete':
             case 'isput':
                 return strtolower($_SERVER['REQUEST_METHOD']) == strtolower(substr($method, 2));
                 // 获取变量 支持过滤和默认值 调用方式 $this->_post($key,$filter,$default);
             // 获取变量 支持过滤和默认值 调用方式 $this->_post($key,$filter,$default);
             case '_get':
                 $input =& $_GET;
                 break;
             case '_post':
                 $input =& $_POST;
                 break;
             case '_put':
                 parse_str(file_get_contents('php://input'), $input);
                 break;
             case '_param':
                 switch ($_SERVER['REQUEST_METHOD']) {
                     case 'POST':
                         $input = $_POST;
                         break;
                     case 'PUT':
                         parse_str(file_get_contents('php://input'), $input);
                         break;
                     default:
                         $input = $_GET;
                 }
                 if (C('VAR_URL_PARAMS')) {
                     $params = $_GET[C('VAR_URL_PARAMS')];
                     $input = array_merge($input, $params);
                 }
                 break;
             case '_request':
                 $input =& $_REQUEST;
                 break;
             case '_session':
                 $input =& $_SESSION;
                 break;
             case '_cookie':
                 $input =& $_COOKIE;
                 break;
             case '_server':
                 $input =& $_SERVER;
                 break;
             case '_globals':
                 $input =& $GLOBALS;
                 break;
             default:
                 Debug::throw_exception(__CLASS__ . ':' . $method . L('_METHOD_NOT_EXIST_'));
         }
         if (!isset($args[0])) {
             // 获取全局变量
             $data = $input;
             // 由VAR_FILTERS配置进行过滤
         } elseif (isset($input[$args[0]])) {
             // 取值操作
             $data = $input[$args[0]];
             $filters = isset($args[1]) ? $args[1] : C('DEFAULT_FILTER');
             if ($filters) {
                 // 2012/3/23 增加多方法过滤支持
                 $filters = explode(',', $filters);
                 foreach ($filters as $filter) {
                     if (function_exists($filter)) {
                         $data = is_array($data) ? array_map($filter, $data) : $filter($data);
                         // 参数过滤
                     }
                 }
             }
         } else {
             // 变量默认值
             $data = isset($args[2]) ? $args[2] : NULL;
         }
         return $data;
     }
 }
コード例 #6
0
ファイル: Db.php プロジェクト: minowu/smartthink
 protected function parseWhereItem($key, $val)
 {
     $whereStr = '';
     if (is_array($val)) {
         if (is_string($val[0])) {
             if (preg_match('/^(EQ|NEQ|GT|EGT|LT|ELT)$/i', $val[0])) {
                 // 比较运算
                 $whereStr .= $key . ' ' . $this->comparison[strtolower($val[0])] . ' ' . $this->parseValue($val[1]);
             } elseif (preg_match('/^(NOTLIKE|LIKE)$/i', $val[0])) {
                 // 模糊查找
                 if (is_array($val[1])) {
                     $likeLogic = isset($val[2]) ? strtoupper($val[2]) : 'OR';
                     if (in_array($likeLogic, array('AND', 'OR', 'XOR'))) {
                         $likeStr = $this->comparison[strtolower($val[0])];
                         $like = array();
                         foreach ($val[1] as $item) {
                             $like[] = $key . ' ' . $likeStr . ' ' . $this->parseValue($item);
                         }
                         $whereStr .= '(' . implode(' ' . $likeLogic . ' ', $like) . ')';
                     }
                 } else {
                     $whereStr .= $key . ' ' . $this->comparison[strtolower($val[0])] . ' ' . $this->parseValue($val[1]);
                 }
             } elseif ('exp' == strtolower($val[0])) {
                 // 使用表达式
                 $whereStr .= ' (' . $key . ' ' . $val[1] . ') ';
             } elseif (preg_match('/IN/i', $val[0])) {
                 // IN 运算
                 if (isset($val[2]) && 'exp' == $val[2]) {
                     $whereStr .= $key . ' ' . strtoupper($val[0]) . ' ' . $val[1];
                 } else {
                     if (is_string($val[1])) {
                         $val[1] = explode(',', $val[1]);
                     }
                     $zone = implode(',', $this->parseValue($val[1]));
                     $whereStr .= $key . ' ' . strtoupper($val[0]) . ' (' . $zone . ')';
                 }
             } elseif (preg_match('/BETWEEN/i', $val[0])) {
                 // BETWEEN运算
                 $data = is_string($val[1]) ? explode(',', $val[1]) : $val[1];
                 $whereStr .= ' (' . $key . ' ' . strtoupper($val[0]) . ' ' . $this->parseValue($data[0]) . ' AND ' . $this->parseValue($data[1]) . ' )';
             } else {
                 Debug::throw_exception(L('_EXPRESS_ERROR_') . ':' . $val[0]);
             }
         } else {
             $count = count($val);
             $rule = isset($val[$count - 1]) ? strtoupper($val[$count - 1]) : '';
             if (in_array($rule, array('AND', 'OR', 'XOR'))) {
                 $count = $count - 1;
             } else {
                 $rule = 'AND';
             }
             for ($i = 0; $i < $count; $i++) {
                 $data = is_array($val[$i]) ? $val[$i][1] : $val[$i];
                 if ('exp' == strtolower($val[$i][0])) {
                     $whereStr .= '(' . $key . ' ' . $data . ') ' . $rule . ' ';
                 } else {
                     $op = is_array($val[$i]) ? $this->comparison[strtolower($val[$i][0])] : '=';
                     $whereStr .= '(' . $key . ' ' . $op . ' ' . $this->parseValue($data) . ') ' . $rule . ' ';
                 }
             }
             $whereStr = substr($whereStr, 0, -4);
         }
     } else {
         //对字符串类型字段采用模糊匹配
         if (C('DB_LIKE_FIELDS') && preg_match('/(' . C('DB_LIKE_FIELDS') . ')/i', $key)) {
             $val = '%' . $val . '%';
             $whereStr .= $key . ' LIKE ' . $this->parseValue($val);
         } else {
             $whereStr .= $key . ' = ' . $this->parseValue($val);
         }
     }
     return $whereStr;
 }
コード例 #7
0
ファイル: App.php プロジェクト: minowu/smartthink
 /**
  * 执行controller->action
  *
  * @return void
  */
 public static function exec()
 {
     // Controller name 安全过滤
     if (!preg_match('/^[A-Za-z](\\w)*$/', CONTROLLER_NAME)) {
         $module = false;
     } else {
         $module = Import::controller(GROUP_NAME, CONTROLLER_NAME);
     }
     // 执行空控制器
     try {
         if (!$module) {
             $module = Import::controller(GROUP_NAME, 'Empty');
             if (!$module) {
                 throw new Exception("Controller不存在,\"" . CONTROLLER_NAME . "\"");
             }
         }
     } catch (Exception $error) {
         Debug::output($error);
     }
     // 获取控制器操作名
     $action = ACTION_NAME;
     // 定义模板名称
     Config::set('TEMPLATE_NAME', THEME_PATH . CONTROLLER_NAME . '/' . $action . '.html');
     try {
         // Action name 安全过滤
         if (!preg_match('/^[A-Za-z](\\w)*$/', $action)) {
             throw new ReflectionException();
         }
         // 对当前控制器的方法执行操作映射
         $method = new ReflectionMethod($module, $action);
         // public方法
         if ($method->isPublic()) {
             // 映射执行
             $class = new ReflectionClass($module);
             // 前置操作
             if ($class->hasMethod('_before_' . $action)) {
                 $before = $class->getMethod('_before_' . $action);
                 // public并执行
                 if ($before->isPublic()) {
                     $before->invoke($module);
                 }
             }
             // URL参数绑定检测
             if (Config::get('URL_PARAMS_BIND') && $method->getNumberOfParameters() > 0) {
                 switch ($_SERVER['REQUEST_METHOD']) {
                     case 'POST':
                         $vars = $_POST;
                         break;
                     case 'PUT':
                         parse_str(file_get_contents('php://input'), $vars);
                         break;
                     default:
                         $vars = $_GET;
                 }
                 $params = $method->getParameters();
                 foreach ($params as $param) {
                     $name = $param->getName();
                     if (isset($vars[$name])) {
                         $args[] = $vars[$name];
                     } elseif ($param->isDefaultValueAvailable()) {
                         $args[] = $param->getDefaultValue();
                     } else {
                         Debug::throw_exception(L('_PARAM_ERROR_') . ':' . $name);
                     }
                 }
                 $method->invokeArgs($module, $args);
             } else {
                 $method->invoke($module);
             }
             // 后置操作
             if ($class->hasMethod('_after_' . $action)) {
                 $after = $class->getMethod('_after_' . $action);
                 // public并执行
                 if ($after->isPublic()) {
                     $after->invoke($module);
                 }
             }
         } else {
             throw new ReflectionException();
         }
     } catch (ReflectionException $e) {
         // 方法调用发生异常后 引导到__call方法处理
         $method = new ReflectionMethod($module, '__call');
         $method->invokeArgs($module, array($action, ''));
     }
 }