mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-02 21:52:34 +08:00
更新核心类库
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
use Think\Exception;
|
||||
|
||||
/**
|
||||
* App 应用管理
|
||||
@@ -22,52 +23,76 @@ class App {
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
static public function run() {
|
||||
static public function run($config) {
|
||||
// 监听app_init
|
||||
Tag::listen('app_init');
|
||||
if(Config::get('common_module')){
|
||||
define('COMMON_PATH', APP_PATH . Config::get('common_module').'/');
|
||||
// 加载全局初始化文件
|
||||
if(is_file( COMMON_PATH . 'init' . EXT )) {
|
||||
include COMMON_PATH . 'init' . EXT;
|
||||
}else{
|
||||
// 检测全局配置文件
|
||||
if(is_file(COMMON_PATH . 'config' . EXT)) {
|
||||
Config::set(include COMMON_PATH . 'config' . EXT);
|
||||
}
|
||||
// 加载全局别名文件
|
||||
if(is_file(COMMON_PATH . 'alias' . EXT)) {
|
||||
Loader::addMap(include COMMON_PATH . 'alias' . EXT);
|
||||
}
|
||||
// 加载全局公共文件
|
||||
if(is_file( COMMON_PATH . 'common' . EXT)) {
|
||||
include COMMON_PATH . 'common' . EXT;
|
||||
}
|
||||
if(is_file(COMMON_PATH . 'tags' . EXT)) {
|
||||
// 全局行为扩展文件
|
||||
Tag::import(include COMMON_PATH . 'tags' . EXT);
|
||||
}
|
||||
}
|
||||
}
|
||||
Hook::listen('app_init');
|
||||
|
||||
define('COMMON_PATH', APP_PATH . $config['common_module'].'/');
|
||||
// 加载全局初始化文件
|
||||
if(is_file( COMMON_PATH . 'init' . EXT )) {
|
||||
include COMMON_PATH . 'init' . EXT;
|
||||
}else{
|
||||
// 检测全局配置文件
|
||||
if(is_file(COMMON_PATH . 'config' . EXT)) {
|
||||
$config = Config::set(include COMMON_PATH . 'config' . EXT);
|
||||
}
|
||||
// 加载全局别名文件
|
||||
if(is_file(COMMON_PATH . 'alias' . EXT)) {
|
||||
Loader::addMap(include COMMON_PATH . 'alias' . EXT);
|
||||
}
|
||||
// 加载全局公共文件
|
||||
if(is_file( COMMON_PATH . 'common' . EXT)) {
|
||||
include COMMON_PATH . 'common' . EXT;
|
||||
}
|
||||
if(is_file(COMMON_PATH . 'tags' . EXT)) {
|
||||
// 全局行为扩展文件
|
||||
Hook::import(include COMMON_PATH . 'tags' . EXT);
|
||||
}
|
||||
}
|
||||
|
||||
// 应用URL调度
|
||||
self::dispatch($config);
|
||||
|
||||
// 监听app_run
|
||||
Tag::listen('app_run');
|
||||
Hook::listen('app_run');
|
||||
|
||||
// 执行操作
|
||||
$instance = Loader::controller(CONTROLLER_NAME);
|
||||
if(!preg_match('/^[A-Za-z](\/|\w)*$/',CONTROLLER_NAME)){ // 安全检测
|
||||
$instance = false;
|
||||
}elseif($config['action_bind_class']){
|
||||
// 操作绑定到类:模块\Controller\控制器\操作
|
||||
$layer = $config['controller_layer'];
|
||||
if(is_dir(MODULE_PATH.$layer.'/'.CONTROLLER_NAME)){
|
||||
$namespace = MODULE_NAME.'\\'.$layer.'\\'.CONTROLLER_NAME.'\\';
|
||||
}else{
|
||||
// 空控制器
|
||||
$namespace = MODULE_NAME.'\\'.$layer.'\\_empty\\';
|
||||
}
|
||||
$actionName = strtolower(ACTION_NAME);
|
||||
if(class_exists($namespace.$actionName)){
|
||||
$class = $namespace.$actionName;
|
||||
}elseif(class_exists($namespace.'_empty')){
|
||||
// 空操作
|
||||
$class = $namespace.'_empty';
|
||||
}else{
|
||||
throw new Exception('_ERROR_ACTION_:'.ACTION_NAME);
|
||||
}
|
||||
$instance = new $class;
|
||||
// 操作绑定到类后 固定执行run入口
|
||||
$action = 'run';
|
||||
}else{
|
||||
$instance = Loader::controller(CONTROLLER_NAME);
|
||||
// 获取当前操作名
|
||||
$action = ACTION_NAME . $config['action_suffix'];
|
||||
}
|
||||
if(!$instance) {
|
||||
E('[ ' . MODULE_NAME . '\\Controller\\' . parse_name(CONTROLLER_NAME, 1) . 'Controller ] not exists');
|
||||
throw new Exception('[ ' . MODULE_NAME . '\\Controller\\' . parse_name(CONTROLLER_NAME, 1) . 'Controller ] not exists');
|
||||
}
|
||||
|
||||
// 获取当前操作名
|
||||
$action = ACTION_NAME . $config['action_suffix'];
|
||||
try{
|
||||
// 操作方法开始监听
|
||||
$call = [$instance, $action];
|
||||
Tag::listen('action_begin', $call);
|
||||
Hook::listen('action_begin', $call);
|
||||
if(!preg_match('/^[A-Za-z](\w)*$/', $action)){
|
||||
// 非法操作
|
||||
throw new \ReflectionException();
|
||||
@@ -88,9 +113,12 @@ class App {
|
||||
$vars = $_GET;
|
||||
}
|
||||
$params = $method->getParameters();
|
||||
$paramsBindType = $config['url_parmas_bind_type'];
|
||||
foreach ($params as $param){
|
||||
$name = $param->getName();
|
||||
if(isset($vars[$name])) {
|
||||
if( 1 == $paramsBindType && !empty($vars) ){
|
||||
$args[] = array_shift($vars);
|
||||
}if(0 == $paramsBindType && isset($vars[$name])) {
|
||||
$args[] = $vars[$name];
|
||||
}elseif($param->isDefaultValueAvailable()){
|
||||
$args[] = $param->getDefaultValue();
|
||||
@@ -98,12 +126,13 @@ class App {
|
||||
E('_PARAM_ERROR_:' . $name);
|
||||
}
|
||||
}
|
||||
array_walk_recursive($args,'Input::filterExp');
|
||||
$method->invokeArgs($instance, $args);
|
||||
}else{
|
||||
$method->invoke($instance);
|
||||
}
|
||||
// 操作方法执行完成监听
|
||||
Tag::listen('action_end', $call);
|
||||
Hook::listen('action_end', $call);
|
||||
}else{
|
||||
// 操作方法不是Public 抛出异常
|
||||
throw new \ReflectionException();
|
||||
@@ -114,11 +143,11 @@ class App {
|
||||
$method = new \ReflectionMethod($instance, '_empty');
|
||||
$method->invokeArgs($instance, [$action, '']);
|
||||
}else{
|
||||
E('[ ' . (new \ReflectionClass($instance))->getName() . ':' . $action . ' ] not exists ', 404);
|
||||
throw new Exception('[ ' . (new \ReflectionClass($instance))->getName() . ':' . $action . ' ] not exists ', 404);
|
||||
}
|
||||
}
|
||||
// 监听app_end
|
||||
Tag::listen('app_end');
|
||||
Hook::listen('app_end');
|
||||
return ;
|
||||
}
|
||||
|
||||
@@ -128,25 +157,20 @@ class App {
|
||||
* @return void
|
||||
*/
|
||||
static public function dispatch($config) {
|
||||
$var_m = $config['var_module'];
|
||||
$var_g = $config['var_group'];
|
||||
$var_c = $config['var_controller'];
|
||||
$var_a = $config['var_action'];
|
||||
$var_p = $config['var_pathinfo'];
|
||||
if(isset($_GET[$var_p])) { // 判断URL里面是否有兼容模式参数
|
||||
$_SERVER['PATH_INFO'] = $_GET[$var_p];
|
||||
unset($_GET[$var_p]);
|
||||
if(isset($_GET[$config['var_pathinfo']])) { // 判断URL里面是否有兼容模式参数
|
||||
$_SERVER['PATH_INFO'] = $_GET[$config['var_pathinfo']];
|
||||
unset($_GET[$config['var_pathinfo']]);
|
||||
}elseif(IS_CLI){ // CLI模式下 index.php module/controller/action/params/...
|
||||
$_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
|
||||
}
|
||||
|
||||
// 检测域名部署
|
||||
if(!IS_CLI) {
|
||||
Route::checkDomain();
|
||||
if(!IS_CLI && $config['sub_domain_deploy']) {
|
||||
Route::checkDomain($config);
|
||||
}
|
||||
|
||||
// 监听path_info
|
||||
Tag::listen('path_info');
|
||||
Hook::listen('path_info');
|
||||
// 分析PATHINFO信息
|
||||
if(!isset($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) {
|
||||
$types = explode(',', $config['pathinfo_fetch']);
|
||||
@@ -162,43 +186,34 @@ class App {
|
||||
}
|
||||
}
|
||||
|
||||
// 定位模块
|
||||
if(empty($_SERVER['PATH_INFO'])) {
|
||||
$_SERVER['PATH_INFO'] = '';
|
||||
}
|
||||
|
||||
// URL后缀
|
||||
define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'],PATHINFO_EXTENSION)));
|
||||
$_SERVER['PATH_INFO'] = trim(preg_replace('/\.(' . trim($config['url_html_suffix'], '.') . ')$/i', '', $_SERVER['PATH_INFO']), '/');
|
||||
if($_SERVER['PATH_INFO']) {
|
||||
if($config['url_deny_suffix'] && preg_match('/\.('.$config['url_deny_suffix'].')$/i', $_SERVER['PATH_INFO'])){
|
||||
exit;
|
||||
}
|
||||
$paths = explode($config['pathinfo_depr'], $_SERVER['PATH_INFO']);
|
||||
// 获取URL中的模块名
|
||||
if($config['require_module'] && !isset($_GET[$var_m])) {
|
||||
$_GET[$var_m] = array_shift($paths);
|
||||
$_SERVER['PATH_INFO'] = implode('/', $paths);
|
||||
}
|
||||
define('__INFO__','');
|
||||
define('__EXT__','');
|
||||
}else{
|
||||
define('__INFO__',trim($_SERVER['PATH_INFO'],'/'));
|
||||
// URL后缀
|
||||
define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'],PATHINFO_EXTENSION)));
|
||||
$_SERVER['PATH_INFO'] = __INFO__;
|
||||
if(!defined('BIND_MODULE')){
|
||||
if($config['url_deny_suffix'] && preg_match('/\.('.$config['url_deny_suffix'].')$/i', $_SERVER['PATH_INFO'])){
|
||||
exit;
|
||||
}
|
||||
$paths = explode($config['pathinfo_depr'], $_SERVER['PATH_INFO']);
|
||||
// 获取URL中的模块名
|
||||
if($config['require_module'] && !isset($_GET[$config['var_module']])) {
|
||||
$_GET[$config['var_module']] = array_shift($paths);
|
||||
$_SERVER['PATH_INFO'] = implode('/', $paths);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取模块名称
|
||||
$module = strtolower(isset($_GET[$var_m]) ? $_GET[$var_m] : $config['default_module']);
|
||||
if($maps = Config::get('url_module_map')) {
|
||||
if(isset($maps[$module])) {
|
||||
// 记录当前别名
|
||||
define('MODULE_ALIAS',$module);
|
||||
// 获取实际的项目名
|
||||
$module = $maps[MODULE_ALIAS];
|
||||
}elseif(array_search($module,$maps)){
|
||||
// 禁止访问原始项目
|
||||
$module = '';
|
||||
}
|
||||
}
|
||||
define('MODULE_NAME', ucwords($module));
|
||||
define('MODULE_NAME', defined('BIND_MODULE')? BIND_MODULE : self::getModule($config));
|
||||
|
||||
// 模块初始化
|
||||
if(MODULE_NAME && Config::get('common_module') != MODULE_NAME && is_dir( APP_PATH . MODULE_NAME )) {
|
||||
Tag::listen('app_begin');
|
||||
if(MODULE_NAME && $config['common_module'] != MODULE_NAME && is_dir( APP_PATH . MODULE_NAME )) {
|
||||
Hook::listen('app_begin');
|
||||
define('MODULE_PATH', APP_PATH . MODULE_NAME . '/');
|
||||
// 加载模块初始化文件
|
||||
if(is_file( MODULE_PATH . 'init' . EXT )) {
|
||||
@@ -223,33 +238,39 @@ class App {
|
||||
}
|
||||
if(is_file(MODULE_PATH . 'tags' . EXT)) {
|
||||
// 行为扩展文件
|
||||
Tag::import(include MODULE_PATH . 'tags' . EXT);
|
||||
Hook::import(include MODULE_PATH . 'tags' . EXT);
|
||||
}
|
||||
}
|
||||
$var_g = $config['var_group'];
|
||||
$var_c = $config['var_controller'];
|
||||
$var_a = $config['var_action'];
|
||||
}else{
|
||||
E('module not exists :' . MODULE_NAME);
|
||||
throw new Exception('module not exists :' . MODULE_NAME);
|
||||
}
|
||||
// 路由检测和控制器、操作解析
|
||||
Route::check($_SERVER['PATH_INFO']);
|
||||
|
||||
// 获取分组名
|
||||
if(Config::get('require_group')){
|
||||
define('GROUP_NAME', strtolower(isset($_GET[$var_g]) ? $_GET[$var_g] : $config['default_group']));
|
||||
}else{
|
||||
define('GROUP_NAME', '');
|
||||
}
|
||||
Route::check($_SERVER['PATH_INFO'],$config);
|
||||
|
||||
// 获取控制器名
|
||||
define('CONTROLLER_NAME', strtolower(isset($_GET[$var_c]) ? $_GET[$var_c] : $config['default_controller']));
|
||||
define('CONTROLLER_NAME', strip_tags(strtolower(isset($_GET[$config['var_controller']]) ? $_GET[$config['var_controller']] : $config['default_controller'])));
|
||||
|
||||
// 获取操作名
|
||||
define('ACTION_NAME', strtolower(isset($_GET[$var_a]) ? $_GET[$var_a] : $config['default_action']));
|
||||
define('ACTION_NAME', strip_tags(strtolower(isset($_GET[$config['var_action']]) ? $_GET[$config['var_action']] : $config['default_action'])));
|
||||
|
||||
unset($_GET[$var_a], $_GET[$var_c], $_GET[$var_m]);
|
||||
unset($_GET[$config['var_action']], $_GET[$config['var_controller']], $_GET[$config['var_module']]);
|
||||
//保证$_REQUEST正常取值
|
||||
$_REQUEST = array_merge($_POST, $_GET);
|
||||
}
|
||||
|
||||
static private function getModule($config){
|
||||
$module = strtolower(isset($_GET[$config['var_module']]) ? $_GET[$config['var_module']] : $config['default_module']);
|
||||
if($maps = $config['url_module_map']) {
|
||||
if(isset($maps[$module])) {
|
||||
// 记录当前别名
|
||||
define('MODULE_ALIAS',$module);
|
||||
// 获取实际的项目名
|
||||
$module = $maps[MODULE_ALIAS];
|
||||
}elseif(array_search($module,$maps)){
|
||||
// 禁止访问原始项目
|
||||
$module = '';
|
||||
}
|
||||
}
|
||||
return strip_tags(ucwords($module));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class TokenBuildBehavior extends Behavior {
|
||||
if(isset($_SESSION[$tokenName][$tokenKey])) {// 相同页面不重复生成session
|
||||
$tokenValue = $_SESSION[$tokenName][$tokenKey];
|
||||
}else{
|
||||
$tokenValue = $tokenType(microtime(TRUE));
|
||||
$tokenValue = $tokenType(microtime(true));
|
||||
$_SESSION[$tokenName][$tokenKey] = $tokenValue;
|
||||
}
|
||||
$token = '<input type="hidden" name="'.$tokenName.'" value="'.$tokenKey.'_'.$tokenValue.'" />';
|
||||
|
||||
@@ -27,12 +27,12 @@ class Cache {
|
||||
*/
|
||||
static public function connect($options=[]) {
|
||||
$type = !empty($options['type'])?$options['type']:'File';
|
||||
$class = 'Think\\Cache\\Driver\\'.ucwords($type);
|
||||
$class = 'Think\\Cache\\Driver\\'.ucwords($type);
|
||||
self::$handler = new $class($options);
|
||||
return self::$handler;
|
||||
}
|
||||
|
||||
static public function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$handler, $method), $params);
|
||||
}
|
||||
static public function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$handler, $method), $params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,15 +36,16 @@ class Config {
|
||||
|
||||
// 检测配置是否存在
|
||||
static public function has($name,$range=''){
|
||||
$range = $range?$range:self::$_range;
|
||||
$name = strtolower($name);
|
||||
// 优先执行设置获取或赋值
|
||||
$range = $range ? $range : self::$_range;
|
||||
$name = strtolower($name);
|
||||
|
||||
if (!strpos($name, '.')) {
|
||||
return isset(self::$_config[$range][$name]);
|
||||
}else{
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
return isset(self::$_config[$range][$name[0]][$name[1]]);
|
||||
}
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
return isset(self::$_config[$range][$name[0]][$name[1]]);
|
||||
}
|
||||
|
||||
// 获取配置参数 为空则获取所有配置
|
||||
@@ -55,13 +56,13 @@ class Config {
|
||||
return self::$_config[$range];
|
||||
}
|
||||
$name = strtolower($name);
|
||||
// 优先执行设置获取或赋值
|
||||
if (!strpos($name, '.')) {
|
||||
return isset(self::$_config[$range][$name]) ? self::$_config[$range][$name] : null;
|
||||
}else{
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
return isset(self::$_config[$range][$name[0]][$name[1]]) ? self::$_config[$range][$name[0]][$name[1]] : null;
|
||||
}
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
return isset(self::$_config[$range][$name[0]][$name[1]]) ? self::$_config[$range][$name[0]][$name[1]] : null;
|
||||
}
|
||||
|
||||
// 设置配置参数 name为数组则为批量设置
|
||||
@@ -74,18 +75,18 @@ class Config {
|
||||
$name = strtolower($name);
|
||||
if (!strpos($name, '.')) {
|
||||
self::$_config[$range][$name] = $value;
|
||||
return;
|
||||
}else{
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
self::$_config[$range][$name[0]][$name[1]] = $value;
|
||||
}
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
self::$_config[$range][$name[0]][$name[1]] = $value;
|
||||
return;
|
||||
}
|
||||
// 批量设置
|
||||
if (is_array($name)){
|
||||
}elseif (is_array($name)){
|
||||
// 批量设置
|
||||
self::$_config[$range] = array_merge(self::$_config[$range], array_change_key_case($name));
|
||||
return self::$_config[$range];
|
||||
}else{
|
||||
return null; // 避免非法参数
|
||||
}
|
||||
return null; // 避免非法参数
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Think;
|
||||
use Think\View;
|
||||
use Think\Transform;
|
||||
|
||||
class Controller {
|
||||
// 视图类实例
|
||||
@@ -27,7 +28,7 @@ class Controller {
|
||||
'cache_path' => RUNTIME_PATH . 'Cache/',
|
||||
];
|
||||
$this->view = new View();
|
||||
$this->view->engine('think', $config);
|
||||
$this->view->engine(Config::get('template_engine'), $config);
|
||||
|
||||
//控制器初始化
|
||||
if(method_exists($this, '_initialize'))
|
||||
@@ -80,39 +81,41 @@ class Controller {
|
||||
* @return void
|
||||
*/
|
||||
protected function ajaxReturn($data, $type='') {
|
||||
if(empty($type)) $type = C('default_ajax_return');
|
||||
if(empty($type)) $type = Config::get('default_ajax_return');
|
||||
switch (strtoupper($type)){
|
||||
case 'JSON':
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
header('Content-Type:application/json; charset=utf-8');
|
||||
exit(Think\Transform::jsonEncode($data));
|
||||
$data = Transform::jsonEncode($data);
|
||||
break;
|
||||
case 'XML':
|
||||
// 返回xml格式数据
|
||||
header('Content-Type:text/xml; charset=utf-8');
|
||||
exit(Think\Transform::xmlEncode($data));
|
||||
$data = Transform::xmlEncode($data);
|
||||
break;
|
||||
case 'JSONP':
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
header('Content-Type:application/javascript; charset=utf-8');
|
||||
$handler = isset($_GET[C('var_jsonp_handler')]) ? $_GET[C('var_jsonp_handler')] : C('default_jsonp_handler');
|
||||
exit($handler . '(' . Think\Transform::jsonEncode($data) . ');');
|
||||
$data = $handler . '(' . Transform::jsonEncode($data) . ');';
|
||||
break;
|
||||
case 'SCRIPT':
|
||||
// 返回可执行的js脚本
|
||||
header('Content-Type:application/javascript; charset=utf-8');
|
||||
exit($data);
|
||||
break;
|
||||
case 'HTML':
|
||||
// 返回html片段
|
||||
header('Content-Type:text/html; charset=utf-8');
|
||||
echo $data;
|
||||
exit;
|
||||
break;
|
||||
case 'TEXT':
|
||||
// 返回一段纯文本
|
||||
header('Content-Type:text/plain; charset=utf-8');
|
||||
echo $data;
|
||||
exit;
|
||||
break;
|
||||
default:
|
||||
// 用于扩展其他返回格式数据
|
||||
Tag::listen('ajax_return', $data);
|
||||
$data = Hook::listen('ajax_return', $data);
|
||||
}
|
||||
exit($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,27 +161,35 @@ class Controller {
|
||||
$data['url'] = $jumpUrl;
|
||||
$this->ajaxReturn($data);
|
||||
}
|
||||
if(is_int($ajax)) $this->view->assign('waitSecond', $ajax);
|
||||
if(!empty($jumpUrl)) $this->view->assign('jumpUrl', $jumpUrl);
|
||||
// 模板变量
|
||||
$data = [];
|
||||
if(is_int($ajax))
|
||||
$data['waitSecond'] = $ajax;
|
||||
if(!empty($jumpUrl))
|
||||
$data['jumpUrl'] = $jumpUrl;
|
||||
|
||||
// 提示标题
|
||||
$this->view->assign('msgTitle', $status ? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
|
||||
$this->view->assign('status', $status); // 状态
|
||||
$data['msgTitle'] = $status ? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_');
|
||||
$data['status'] = $status; // 状态
|
||||
|
||||
//保证输出不受静态缓存影响
|
||||
C('HTML_CACHE_ON',false);
|
||||
Config::set('html_cache_on',false);
|
||||
if($status) { //发送成功信息
|
||||
$this->view->assign('message', $message);// 提示信息
|
||||
$data['message'] = $message;// 提示信息
|
||||
// 成功操作后默认停留1秒
|
||||
$this->view->assign('waitSecond', '1');
|
||||
$data['waitSecond'] = '1';
|
||||
// 默认操作成功自动返回操作前页面
|
||||
if(!$jumpUrl) $this->view->assign("jumpUrl", $_SERVER["HTTP_REFERER"]);
|
||||
$this->display(C('success_tmpl'));
|
||||
if(!$jumpUrl)
|
||||
$data["jumpUrl"] = $_SERVER["HTTP_REFERER"];
|
||||
$this->display(Config::get('success_tmpl'),$data);
|
||||
}else{
|
||||
$this->view->assign('error', $message);// 提示信息
|
||||
$data['error'] = $message;// 提示信息
|
||||
//发生错误时候默认停留3秒
|
||||
$this->view->assign('waitSecond', '3');
|
||||
$data['waitSecond'] = '3';
|
||||
// 默认发生错误的话自动返回上页
|
||||
if(!$jumpUrl) $this->view->assign('jumpUrl', 'javascript:history.back(-1);');
|
||||
$this->display(C('error_tmpl'));
|
||||
if(!$jumpUrl)
|
||||
$data['jumpUrl'] = 'javascript:history.back(-1);';
|
||||
$this->display(Config::get('error_tmpl'),$data);
|
||||
// 中止执行 避免出错后继续执行
|
||||
exit ;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ class Cookie {
|
||||
'expire' => 0, // cookie 保存时间
|
||||
'path' => '/', // cookie 保存路径
|
||||
'domain' => '', // cookie 有效域名
|
||||
'secure' => false, // cookie 启用安全传输
|
||||
'httponly' => '', // httponly设置
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -27,6 +29,9 @@ class Cookie {
|
||||
*/
|
||||
static public function init($config=[]){
|
||||
self::$config = array_merge(self::$config, array_change_key_case($config));
|
||||
if(!empty(self::$config['httponly'])){
|
||||
ini_set('session.cookie_httponly', 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +71,7 @@ class Cookie {
|
||||
$value = 'think:'.json_encode(array_map('urlencode',$value));
|
||||
}
|
||||
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
|
||||
setcookie($name, $value, $expire, $config['path'], $config['domain']);
|
||||
setcookie($name, $value, $expire, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
|
||||
$_COOKIE[$name] = $value;
|
||||
}
|
||||
|
||||
@@ -77,11 +82,11 @@ class Cookie {
|
||||
* @return mixed
|
||||
*/
|
||||
static public function get($name, $prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$config['prefix'];
|
||||
$prefix = $prefix ? $prefix : self::$config['prefix'];
|
||||
$name = $prefix . $name;
|
||||
if(isset($_COOKIE[$name])){
|
||||
$value = $_COOKIE[$name];
|
||||
if(0===strpos($value,'think:')){
|
||||
if(0 === strpos($value,'think:')){
|
||||
$value = substr($value,6);
|
||||
return array_map('urldecode',json_decode($value,true));
|
||||
}else{
|
||||
@@ -99,9 +104,10 @@ class Cookie {
|
||||
* @return mixed
|
||||
*/
|
||||
static public function delete($name, $prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$config['prefix'];
|
||||
$name = $prefix . $name;
|
||||
setcookie($name, '', time() - 3600, self::$config['path'], self::$config['domain']);
|
||||
$config = self::$config;
|
||||
$prefix = $prefix ? $prefix : $config['prefix'];
|
||||
$name = $prefix . $name;
|
||||
setcookie($name, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
|
||||
unset($_COOKIE[$name]); // 删除指定cookie
|
||||
}
|
||||
|
||||
@@ -115,11 +121,12 @@ class Cookie {
|
||||
if (empty($_COOKIE))
|
||||
return;
|
||||
// 要删除的cookie前缀,不指定则删除config设置的指定前缀
|
||||
$prefix = $prefix ? $prefix: self::$config['prefix'];
|
||||
$config = self::$config;
|
||||
$prefix = $prefix ? $prefix: $config['prefix'];
|
||||
if ($prefix) {// 如果前缀为空字符串将不作处理直接返回
|
||||
foreach ($_COOKIE as $key => $val) {
|
||||
if (0 === strpos($key, $prefix)) {
|
||||
setcookie($key, '', time() - 3600, self::$config['path'], self::$config['domain']);
|
||||
setcookie($key, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
|
||||
unset($_COOKIE[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ class Db {
|
||||
}
|
||||
|
||||
// 调用驱动类的方法
|
||||
static public function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$_instance, $method), $params);
|
||||
}
|
||||
static public function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$_instance, $method), $params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,10 @@ class Debug {
|
||||
*/
|
||||
static public function remark($name,$value='') {
|
||||
// 记录时间和内存使用
|
||||
self::$_info[$name] = is_float($value)?$value:microtime(TRUE);
|
||||
if('time' != $value && function_exists('memory_get_usage')) {
|
||||
self::$_mem['mem'][$name] = is_float($value)?$value:memory_get_usage();
|
||||
self::$_mem['peak'][$name] = function_exists('memory_get_peak_usage')?memory_get_peak_usage(): self::$_mem['mem'][$name];
|
||||
self::$_info[$name] = is_float($value) ? $value : microtime(true);
|
||||
if('time' != $value ) {
|
||||
self::$_mem['mem'][$name] = is_float($value) ? $value : memory_get_usage();
|
||||
self::$_mem['peak'][$name] = function_exists('memory_get_peak_usage') ? memory_get_peak_usage() : self::$_mem['mem'][$name];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,11 +35,12 @@ class Debug {
|
||||
* 统计某个区间的时间(微秒)使用情况
|
||||
* @param string $start 开始标签
|
||||
* @param string $end 结束标签
|
||||
* @param integer|string $dec 小数位或者m
|
||||
* @param integer|string $dec 小数位
|
||||
* @return mixed
|
||||
*/
|
||||
static public function getUseTime($start,$end,$dec=6) {
|
||||
if(!isset(self::$_info[$end])) self::$_info[$end] = microtime(TRUE);
|
||||
if(!isset(self::$_info[$end]))
|
||||
self::$_info[$end] = microtime(true);
|
||||
return number_format((self::$_info[$end]-self::$_info[$start]),$dec);
|
||||
}
|
||||
|
||||
@@ -47,7 +48,7 @@ class Debug {
|
||||
* 记录内存使用情况
|
||||
* @param string $start 开始标签
|
||||
* @param string $end 结束标签
|
||||
* @param integer|string $dec 小数位或者m
|
||||
* @param integer|string $dec 小数位
|
||||
* @return mixed
|
||||
*/
|
||||
static public function getUseMem($start,$end,$dec=2) {
|
||||
@@ -67,11 +68,12 @@ class Debug {
|
||||
* 统计内存峰值情况
|
||||
* @param string $start 开始标签
|
||||
* @param string $end 结束标签
|
||||
* @param integer|string $dec 小数位或者m
|
||||
* @param integer|string $dec 小数位
|
||||
* @return mixed
|
||||
*/
|
||||
static public function getMemPeak($start,$end,$dec=2) {
|
||||
if(!isset(self::$_mem['peak'][$end])) self::$_mem['peak'][$end] = function_exists('memory_get_peak_usage')?memory_get_peak_usage():memory_get_usage();
|
||||
if(!isset(self::$_mem['peak'][$end]))
|
||||
self::$_mem['peak'][$end] = function_exists('memory_get_peak_usage') ? memory_get_peak_usage() : memory_get_usage();
|
||||
$size = self::$_mem['peak'][$end]-self::$_mem['peak'][$start];
|
||||
$a = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$pos = 0;
|
||||
@@ -85,7 +87,7 @@ class Debug {
|
||||
/**
|
||||
* 浏览器友好的变量输出
|
||||
* @param mixed $var 变量
|
||||
* @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
|
||||
* @param boolean $echo 是否输出 默认为true 如果为false 则返回输出字符串
|
||||
* @param string $label 标签 默认为空
|
||||
* @return void|string
|
||||
*/
|
||||
|
||||
@@ -12,5 +12,5 @@
|
||||
namespace Think;
|
||||
|
||||
class Exception extends \Exception {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace Think;
|
||||
|
||||
class Tag {
|
||||
class Hook {
|
||||
|
||||
static private $tags = [];
|
||||
|
||||
@@ -23,7 +23,7 @@ class Tag {
|
||||
*/
|
||||
static public function add($tag,$behavior) {
|
||||
if(is_array($behavior)) {
|
||||
self::$tags[$tag] = array_merge(self::$tags[$tag],$behavior);
|
||||
self::$tags[$tag] = array_merge(self::$tags[$tag],$behavior);
|
||||
}else{
|
||||
self::$tags[$tag][] = $behavior;
|
||||
}
|
||||
@@ -44,13 +44,18 @@ class Tag {
|
||||
* @param mixed $params 传入参数
|
||||
* @return void
|
||||
*/
|
||||
static public function listen($tag, &$params=NULL) {
|
||||
static public function listen($tag, &$params=null) {
|
||||
if(isset(self::$tags[$tag])) {
|
||||
foreach (self::$tags[$tag] as $val) {
|
||||
Debug::remark('behavior_start','time');
|
||||
$result = self::exec($val, $params);
|
||||
Debug::remark('behavior_end','time');
|
||||
Log::record('Run '.$val.' Behavior [ RunTime:'.Debug::getUseTime('behavior_start','behavior_end').'s ]','INFO');
|
||||
foreach (self::$tags[$tag] as $name) {
|
||||
|
||||
Config::get('app_debug') && Debug::remark('behavior_start','time');
|
||||
|
||||
$result = self::exec($name, $tag,$params);
|
||||
|
||||
if(Config::get('app_debug')){
|
||||
Debug::remark('behavior_end','time');
|
||||
Log::record('Run '.$name.' [ RunTime:'.Debug::getUseTime('behavior_start','behavior_end').'s ]','INFO');
|
||||
}
|
||||
if(false === $result) {
|
||||
// 如果返回false 则中断行为执行
|
||||
return ;
|
||||
@@ -63,22 +68,19 @@ class Tag {
|
||||
/**
|
||||
* 执行某个行为
|
||||
* @param string $name 行为名称
|
||||
* @param string $tag 方法名(标签名)
|
||||
* @param Mixed $params 传人的参数
|
||||
* @return void
|
||||
*/
|
||||
static public function exec($name, &$params=NULL) {
|
||||
static public function exec($name, $tag,&$params=null) {
|
||||
if($name instanceof \Closure) {
|
||||
return $name($params);
|
||||
}
|
||||
if(false === strpos($name,'\\')) {
|
||||
$class = '\\'.ucwords(MODULE_NAME).'\\Behavior\\'.$name;
|
||||
}else{
|
||||
$class = $name;
|
||||
if('Behavior' == substr($name,-8) ){
|
||||
// 行为扩展必须用run入口方法
|
||||
$tag = 'run';
|
||||
}
|
||||
if(class_exists($class)) {
|
||||
$behavior = new $class();
|
||||
return $behavior->run($params);
|
||||
}
|
||||
return ;
|
||||
$addon = new $name();
|
||||
return $addon->$tag($params);
|
||||
}
|
||||
}
|
||||
@@ -156,7 +156,7 @@ Class GIFEncoder {
|
||||
printf ( "%s: %d %s", $this->VER, $i, $this->ERR [ 'ERR01' ] );
|
||||
exit ( 0 );
|
||||
}
|
||||
for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = TRUE; $k; $j++ ) {
|
||||
for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = true; $k; $j++ ) {
|
||||
switch ( $this->BUF [ $i ] { $j } ) {
|
||||
case "!":
|
||||
if ( ( substr ( $this->BUF [ $i ], ( $j + 3 ), 8 ) ) == "NETSCAPE" ) {
|
||||
|
||||
@@ -13,78 +13,160 @@ namespace Think;
|
||||
|
||||
class Input {
|
||||
// 全局过滤规则
|
||||
static $filter = NULL;
|
||||
static $filter = null;
|
||||
|
||||
/**
|
||||
* 获取系统变量 支持过滤和默认值
|
||||
* @access public
|
||||
* @param string $type 输入数据类型
|
||||
* @param string $method 输入数据类型
|
||||
* @param array $args 参数 array(key,filter,default)
|
||||
* @return mixed
|
||||
*/
|
||||
static public function __callStatic($type,$args=[]) {
|
||||
switch(strtolower($type)) {
|
||||
case 'get': $input =& $_GET;break;
|
||||
case 'post': $input =& $_POST;break;
|
||||
case 'put' : parse_str(file_get_contents('php://input'), $input);break;
|
||||
case 'param' :
|
||||
static public function __callStatic($method,$args=[]) {
|
||||
static $_PUT = null;
|
||||
$name = $args[0];
|
||||
$default = isset($args[2]) ? $args[2] : null;
|
||||
if(strpos($name,'/')){ // 指定修饰符
|
||||
list($name,$type) = explode('/',$name,2);
|
||||
}else{ // 默认强制转换为字符串
|
||||
$type = 's';
|
||||
}
|
||||
switch(strtolower($method)) {
|
||||
case 'get' :
|
||||
$input =& $_GET;
|
||||
break;
|
||||
case 'post' :
|
||||
$input =& $_POST;
|
||||
break;
|
||||
case 'put' :
|
||||
if(is_null($_PUT)){
|
||||
parse_str(file_get_contents('php://input'), $_PUT);
|
||||
}
|
||||
$input = $_PUT;
|
||||
break;
|
||||
case 'param' :
|
||||
switch($_SERVER['REQUEST_METHOD']) {
|
||||
case 'POST':
|
||||
$input = $_POST;
|
||||
break;
|
||||
case 'PUT':
|
||||
parse_str(file_get_contents('php://input'), $input);
|
||||
if(is_null($_PUT)){
|
||||
parse_str(file_get_contents('php://input'), $_PUT);
|
||||
}
|
||||
$input = $_PUT;
|
||||
break;
|
||||
default:
|
||||
$input = $_GET;
|
||||
}
|
||||
break;
|
||||
case 'request': $input =& $_REQUEST;break;
|
||||
case 'server': $input =& $_SERVER;break;
|
||||
case 'cookie': $input =& $_COOKIE;break;
|
||||
case 'session': $input =& $_SESSION;break;
|
||||
case 'globals': $input =& $GLOBALS;break;
|
||||
default:return NULL;
|
||||
case 'path' :
|
||||
$input = [];
|
||||
if(!empty($_SERVER['PATH_INFO'])){
|
||||
$depr = Config::get('url_pathinfo_depr');
|
||||
$input = explode($depr,trim($_SERVER['PATH_INFO'],$depr));
|
||||
}
|
||||
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:
|
||||
return null;
|
||||
}
|
||||
// 变量全局过滤
|
||||
array_walk_recursive($input,'self::filter_exp');
|
||||
if(self::$filter) {
|
||||
$_filters = explode(',',self::$filter);
|
||||
foreach($_filters as $_filter){
|
||||
// 全局参数过滤
|
||||
array_walk_recursive($input,$_filter);
|
||||
|
||||
if(''==$name) { // 获取全部变量
|
||||
$data = $input;
|
||||
if(isset(self::$filter)) {
|
||||
$filter = self::$filter;
|
||||
if(is_string($filters)){
|
||||
$filters = explode(',',$filters);
|
||||
}
|
||||
foreach($filters as $filter){
|
||||
$data = self::filter($filter,$data); // 参数过滤
|
||||
}
|
||||
}
|
||||
}
|
||||
if(''== $args[0]) {
|
||||
// 返回全部数据
|
||||
return $input;
|
||||
}elseif(isset($input[$args[0]])) {
|
||||
$data = $input[$args[0]];
|
||||
}elseif(isset($input[$name])) { // 取值操作
|
||||
$data = $input[$name];
|
||||
if(!empty($args[1])) {
|
||||
$filters = explode(',',$args[1]);
|
||||
foreach($filters as $filter){
|
||||
if(is_callable($filter)) {
|
||||
$data = is_array($data)?array_map($filter,$data):$filter($data); // 参数过滤
|
||||
if(is_string($filters)){
|
||||
if(0 === strpos($filters,'/') && 1 !== preg_match($filters,(string)$data)){
|
||||
// 支持正则验证
|
||||
return $default;
|
||||
}else{
|
||||
$data = filter_var($data,is_int($filter)?$filter:filter_id($filter));
|
||||
if(false === $data) {
|
||||
return isset($args[2])?$args[2]:NULL;
|
||||
$filters = explode(',',$filters);
|
||||
}
|
||||
}elseif(is_int($filters)){
|
||||
$filters = array($filters);
|
||||
}
|
||||
|
||||
if(is_array($filters)){
|
||||
foreach($filters as $filter){
|
||||
if(function_exists($filter)) {
|
||||
$data = is_array($data) ? self::filter($filter,$data) : $filter($data); // 参数过滤
|
||||
}else{
|
||||
$data = filter_var($data,is_int($filter) ? $filter : filter_id($filter));
|
||||
if(false === $data) {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!empty($type)){
|
||||
switch(strtolower($type)){
|
||||
case 'a': // 数组
|
||||
$data = (array)$data;
|
||||
break;
|
||||
case 'd': // 数字
|
||||
$data = (int)$data;
|
||||
break;
|
||||
case 'f': // 浮点
|
||||
$data = (float)$data;
|
||||
break;
|
||||
case 'b': // 布尔
|
||||
$data = (boolean)$data;
|
||||
break;
|
||||
case 's': // 字符串
|
||||
default:
|
||||
$data = (string)$data;
|
||||
}
|
||||
}
|
||||
|
||||
}else{
|
||||
// 不存在指定输入
|
||||
$data = isset($args[2])?$args[2]:NULL;
|
||||
}else{ // 变量默认值
|
||||
$data = $default;
|
||||
}
|
||||
is_array($data) && array_walk_recursive($data,'self::filterExp');
|
||||
return $data;
|
||||
}
|
||||
|
||||
// 过滤表单中的表达式
|
||||
static private function filter_exp(&$value){
|
||||
if (in_array(strtolower($value),['exp','or'])){
|
||||
static public function filterExp(&$value){
|
||||
// TODO 其他安全过滤
|
||||
|
||||
// 过滤查询特殊字符
|
||||
if(preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i',$value)){
|
||||
$value .= ' ';
|
||||
}
|
||||
}
|
||||
|
||||
static public function filter($filter, $data) {
|
||||
$result = array();
|
||||
foreach ($data as $key => $val) {
|
||||
$result[$key] = is_array($val)
|
||||
? self::filter($filter, $val)
|
||||
: call_user_func($filter, $val);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,9 +130,13 @@ class Loader {
|
||||
* @return Object
|
||||
*/
|
||||
static public function model($name = '', $layer = 'Model') {
|
||||
if(empty($name)) return new Model;
|
||||
if(empty($name)) {
|
||||
return new Model;
|
||||
}
|
||||
static $_model = [];
|
||||
if(isset($_model[$name . $layer])) return $_model[$name . $layer];
|
||||
if(isset($_model[$name . $layer])) {
|
||||
return $_model[$name . $layer];
|
||||
}
|
||||
if(strpos($name, '/')) {
|
||||
list($module, $name) = explode('/', $name);
|
||||
}else{
|
||||
@@ -157,7 +161,9 @@ class Loader {
|
||||
*/
|
||||
static public function controller($name, $layer = 'Controller') {
|
||||
static $_instance = [];
|
||||
if(isset($_instance[$name.$layer])) return $_instance[$name . $layer];
|
||||
if(isset($_instance[$name.$layer])) {
|
||||
return $_instance[$name . $layer];
|
||||
}
|
||||
if(strpos($name, '/')) {
|
||||
list($module,$name) = explode('/', $name);
|
||||
}else{
|
||||
@@ -226,7 +232,7 @@ class Loader {
|
||||
$_instance[$identify] = $o;
|
||||
}
|
||||
else
|
||||
E('_CLASS_NOT_EXIST_:' . $class);
|
||||
throw new Exception('_CLASS_NOT_EXIST_:' . $class);
|
||||
}
|
||||
return $_instance[$identify];
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ class Log {
|
||||
* @return array
|
||||
*/
|
||||
static public function getLog($level=''){
|
||||
return $level?self::$log[$level]:self::$log;
|
||||
return $level ? self::$log[$level] : self::$log;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,7 +55,7 @@ class Log {
|
||||
* @return void
|
||||
*/
|
||||
static public function save($destination='',$level='') {
|
||||
$log = $level?self::$log[$level]:self::$log;
|
||||
$log = self::getLog($level);
|
||||
if(empty($log)) return ;
|
||||
$message = '';
|
||||
if($level) {
|
||||
|
||||
@@ -83,11 +83,6 @@ class Model {
|
||||
$this->dbName = $config['db_name'];
|
||||
}
|
||||
|
||||
// 设置表前缀
|
||||
if(empty($this->tablePrefix)) {
|
||||
$this->tablePrefix = is_null($this->tablePrefix)?'':C('database.prefix');
|
||||
}
|
||||
|
||||
// 数据库初始化操作
|
||||
// 获取数据库操作对象
|
||||
// 当前模型有独立的数据库连接信息
|
||||
@@ -566,7 +561,7 @@ class Model {
|
||||
$config = C($config);
|
||||
}
|
||||
$_db[$linkNum] = Db::instance($config);
|
||||
}elseif(NULL === $config){
|
||||
}elseif(null === $config){
|
||||
$_db[$linkNum]->close(); // 关闭数据库连接
|
||||
unset($_db[$linkNum]);
|
||||
return ;
|
||||
@@ -825,7 +820,7 @@ class Model {
|
||||
* @param array $args 参数
|
||||
* @return Model
|
||||
*/
|
||||
public function scope($scope='',$args=NULL){
|
||||
public function scope($scope='',$args=null){
|
||||
if('' === $scope) {
|
||||
if(isset($this->scope['default'])) {
|
||||
// 默认的命名范围
|
||||
|
||||
@@ -69,7 +69,7 @@ class Baidu extends Driver{
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
$data = $this->call('passport/users/getLoggedInUser');
|
||||
return !empty($data['uid'])?$data['uid']:NULL;
|
||||
return !empty($data['uid'])?$data['uid']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class Diandian extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -67,7 +67,7 @@ class Douban extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class Github extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('user');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -75,7 +75,7 @@ class Google extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('userinfo');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -70,7 +70,7 @@ class Kaixin extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('users/me');
|
||||
return !empty($data['uid'])?$data['uid']:NULL;
|
||||
return !empty($data['uid'])?$data['uid']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -76,7 +76,7 @@ class Msn extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('me');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -83,7 +83,7 @@ class Qq extends Driver{
|
||||
if(isset($data['openid']))
|
||||
return $data['openid'];
|
||||
}
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getOauthInfo(){
|
||||
|
||||
@@ -93,7 +93,7 @@ class Renren extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class Sina extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class Sohu extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -71,7 +71,7 @@ class T163 extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('users/show');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -71,7 +71,7 @@ class Taobao extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,7 +74,7 @@ class Tencent extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class X360 extends Driver{
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
$data = $this->call('user/me');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,7 +26,7 @@ class Parser {
|
||||
}
|
||||
|
||||
// 调用驱动类的方法
|
||||
static public function __callStatic($method, $params){
|
||||
static public function __callStatic($method, $params){
|
||||
return self::parse($params[0],$method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ class Route {
|
||||
'DELETE' => [],
|
||||
'*' => [],
|
||||
];
|
||||
|
||||
// URL映射规则
|
||||
static private $map = [];
|
||||
// 子域名部署规则
|
||||
@@ -86,7 +87,7 @@ class Route {
|
||||
}
|
||||
|
||||
// 检测子域名部署
|
||||
static public function checkDomain(){
|
||||
static public function checkDomain($config=[]){
|
||||
// 开启子域名部署 支持二级和三级域名
|
||||
if(!empty(self::$domain)) {
|
||||
$rules = self::$domain;
|
||||
@@ -123,7 +124,7 @@ class Route {
|
||||
exit;
|
||||
}
|
||||
if(is_array($rule)) {
|
||||
$_GET[Config::get('var_module')] = $rule[0];
|
||||
$_GET[$config['var_module']] = $rule[0];
|
||||
if(isset($rule[1])) { // 传入参数
|
||||
parse_str($rule[1], $parms);
|
||||
if(isset($panDomain)) {
|
||||
@@ -136,18 +137,20 @@ class Route {
|
||||
$_GET = array_merge($_GET,$parms);
|
||||
}
|
||||
}else{
|
||||
$_GET[Config::get('var_module')] = $rule;
|
||||
$_GET[$config['var_module']] = $rule;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检测URL路由
|
||||
static public function check($regx) {
|
||||
static public function check($regx,$config) {
|
||||
// 优先检测是否存在PATH_INFO
|
||||
if(empty($regx)) $regx = '/' ;
|
||||
// 分隔符替换 确保路由定义使用统一的分隔符
|
||||
$regx = str_replace(Config::get('pathinfo_depr'), '/', $regx);
|
||||
if('/' != $config['pathinfo_depr']){
|
||||
$regx = str_replace($config['pathinfo_depr'], '/', $regx);
|
||||
}
|
||||
if(isset(self::$map[$regx])) { // URL映射
|
||||
return self::parseUrl(self::$map[$regx]);
|
||||
}
|
||||
@@ -187,7 +190,7 @@ class Route {
|
||||
self::invokeRegx($route, $matches);
|
||||
exit;
|
||||
}
|
||||
return self::parseRegex($matches, $route, $regx);
|
||||
return self::parseRegex($matches, $route, $regx,$config);
|
||||
}else{ // 规则路由
|
||||
$len1 = substr_count($regx, '/');
|
||||
$len2 = substr_count($rule, '/');
|
||||
@@ -210,13 +213,13 @@ class Route {
|
||||
self::invokeRule($route, $var);
|
||||
exit;
|
||||
}
|
||||
return self::parseRule($rule, $route, $regx);
|
||||
return self::parseRule($rule, $route, $regx,$config);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return self::parseUrl($regx);
|
||||
return self::parseUrl($regx,$config);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,33 +268,34 @@ class Route {
|
||||
$reflect->invokeArgs($args);
|
||||
}
|
||||
|
||||
// 解析模块的URL地址
|
||||
static private function parseUrl($url) {
|
||||
// 解析模块的URL地址 [模块/]控制器/操作
|
||||
static private function parseUrl($url,$config=[]) {
|
||||
if('/' == $url) {
|
||||
return ;
|
||||
}
|
||||
$paths = explode('/', $url);
|
||||
$var_g = Config::get('var_group');
|
||||
$var_c = Config::get('var_controller');
|
||||
$var_a = Config::get('var_action');
|
||||
if(Config::get('require_group') && !isset($_GET[$var_g])) {
|
||||
$_GET[$var_g] = array_shift($paths);
|
||||
}
|
||||
if(Config::get('require_controller') && !isset($_GET[$var_c])) {
|
||||
$_GET[$var_c] = array_shift($paths);
|
||||
|
||||
$_GET[$config['var_action']] = array_pop($paths);
|
||||
|
||||
if(!defined('BIND_CONTROLLER') && !isset($_GET[$config['var_controller']])) {
|
||||
$_GET[$config['var_controller']] = array_pop($paths);
|
||||
}
|
||||
if(!isset($_GET[$var_a])) {
|
||||
$_GET[$var_a] = array_shift($paths);
|
||||
|
||||
if(!defined('BIND_MODULE') && !isset($_GET[$config['var_module']])) {
|
||||
$_GET[$config['var_module']] = array_pop($paths);
|
||||
}
|
||||
|
||||
// 解析剩余的URL参数
|
||||
$var = [];
|
||||
preg_replace('@(\w+)\/([^\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode('/', $paths));
|
||||
$_GET = array_merge($var, $_GET);
|
||||
if(!empty($paths)) {
|
||||
preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){ $var[strtolower($match[1])]=strip_tags($match[2]);}, implode('/',$paths));
|
||||
}
|
||||
$_GET = array_merge($var, $_GET);
|
||||
}
|
||||
|
||||
// 解析规范的路由地址
|
||||
// 地址格式 [控制器/操作?]参数1=值1&参数2=值2...
|
||||
static private function parseRoute($url) {
|
||||
static private function parseRoute($url,$config=[]) {
|
||||
$var = [];
|
||||
if(false !== strpos($url, '?')) { // [控制器/操作?]参数1=值1&参数2=值2...
|
||||
$info = parse_url($url);
|
||||
@@ -304,9 +308,12 @@ class Route {
|
||||
}
|
||||
if(isset($path)) {
|
||||
$action = array_pop($path);
|
||||
$_GET[Config::get('var_action')] = '[rest]'==$action? REQUEST_METHOD : $action;
|
||||
$_GET[$config['var_action']] = '[rest]'==$action ? REQUEST_METHOD : $action;
|
||||
if(!empty($path)) {
|
||||
$_GET[Config::get('var_controller')] = array_pop($path);
|
||||
$_GET[$config['var_controller']] = array_pop($path);
|
||||
}
|
||||
if(!empty($path)) {
|
||||
$_GET[$config['var_module']] = array_pop($path);
|
||||
}
|
||||
}
|
||||
return $var;
|
||||
@@ -352,7 +359,7 @@ class Route {
|
||||
// 外部地址中可以用动态变量 采用 :1 :2 的方式
|
||||
// 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
|
||||
// 'new/:id'=>array('/new.php?id=:1',301), 重定向
|
||||
static private function parseRule($rule, $route, $regx) {
|
||||
static private function parseRule($rule, $route, $regx,$config) {
|
||||
// 获取路由地址规则
|
||||
$url = is_array($route) ? $route[0] : $route;
|
||||
// 获取URL地址中的参数
|
||||
@@ -383,7 +390,7 @@ class Route {
|
||||
exit;
|
||||
}else{
|
||||
// 解析路由地址
|
||||
$var = self::parseRoute($url);
|
||||
$var = self::parseRoute($url,$config);
|
||||
// 解析路由地址里面的动态参数
|
||||
$values = array_values($matches);
|
||||
foreach ($var as $key => $val){
|
||||
@@ -393,8 +400,10 @@ class Route {
|
||||
}
|
||||
$var = array_merge($matches, $var);
|
||||
// 解析剩余的URL参数
|
||||
if($paths) {
|
||||
preg_replace('@(\w+)\/([^\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/', $paths));
|
||||
if(!empty($paths)) {
|
||||
preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){
|
||||
$var[strtolower($match[1])] = strip_tags($match[2]);
|
||||
}, implode('/',$paths));
|
||||
}
|
||||
// 解析路由自动传人参数
|
||||
if(is_array($route) && isset($route[1])) {
|
||||
@@ -413,7 +422,7 @@ class Route {
|
||||
// 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式
|
||||
// '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'),
|
||||
// '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向
|
||||
static private function parseRegex($matches, $route, $regx) {
|
||||
static private function parseRegex($matches, $route, $regx,$config) {
|
||||
// 获取路由地址规则
|
||||
$url = is_array($route) ? $route[0] : $route;
|
||||
$url = preg_replace('/:(\d+)/e', '$matches[\\1]', $url);
|
||||
@@ -422,11 +431,13 @@ class Route {
|
||||
exit;
|
||||
}else{
|
||||
// 解析路由地址
|
||||
$var = self::parseRoute($url);
|
||||
$var = self::parseRoute($url,$config);
|
||||
// 解析剩余的URL参数
|
||||
$regx = substr_replace($regx, '', 0, strlen($matches[0]));
|
||||
if($regx) {
|
||||
preg_replace('@(\w+)\/([^,\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', $regx);
|
||||
preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){
|
||||
$var[strtolower($match[1])] = strip_tags($match[2]);
|
||||
}, $regx);
|
||||
}
|
||||
// 解析路由自动传人参数
|
||||
if(is_array($route) && isset($route[1])) {
|
||||
|
||||
@@ -33,21 +33,32 @@ class Session {
|
||||
* @return void
|
||||
*/
|
||||
static public function init($config=[]) {
|
||||
if(isset($config['prefix'])) self::$prefix = $config['prefix'];
|
||||
if(isset($config['prefix']))
|
||||
self::$prefix = $config['prefix'];
|
||||
if(isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])){
|
||||
session_id($_REQUEST[$config['var_session_id']]);
|
||||
}elseif(isset($config['id'])) {
|
||||
session_id($config['id']);
|
||||
}
|
||||
ini_set('session.auto_start', 0);
|
||||
if(isset($config['name'])) session_name($config['name']);
|
||||
if(isset($config['path'])) session_save_path($config['path']);
|
||||
if(isset($config['domain'])) ini_set('session.cookie_domain', $config['domain']);
|
||||
if(isset($config['expire'])) ini_set('session.gc_maxlifetime', $config['expire']);
|
||||
if(isset($config['use_trans_sid'])) ini_set('session.use_trans_sid', $config['use_trans_sid']?1:0);
|
||||
if(isset($config['use_cookies'])) ini_set('session.use_cookies', $config['use_cookies']?1:0);
|
||||
if(isset($config['cache_limiter'])) session_cache_limiter($config['cache_limiter']);
|
||||
if(isset($config['cache_expire'])) session_cache_expire($config['cache_expire']);
|
||||
if(isset($config['name']))
|
||||
session_name($config['name']);
|
||||
if(isset($config['path']))
|
||||
session_save_path($config['path']);
|
||||
if(isset($config['domain']))
|
||||
ini_set('session.cookie_domain', $config['domain']);
|
||||
if(isset($name['expire'])) {
|
||||
ini_set('session.gc_maxlifetime', $name['expire']);
|
||||
ini_set('session.cookie_lifetime', $name['expire']);
|
||||
}
|
||||
if(isset($config['use_trans_sid']))
|
||||
ini_set('session.use_trans_sid', $config['use_trans_sid']?1:0);
|
||||
if(isset($config['use_cookies']))
|
||||
ini_set('session.use_cookies', $config['use_cookies']?1:0);
|
||||
if(isset($config['cache_limiter']))
|
||||
session_cache_limiter($config['cache_limiter']);
|
||||
if(isset($config['cache_expire']))
|
||||
session_cache_expire($config['cache_expire']);
|
||||
if(!empty($config['type'])) { // 读取session驱动
|
||||
$class = 'Think\\Session\\Driver\\'. ucwords(strtolower($config['type']));
|
||||
// 检查驱动类
|
||||
@@ -65,11 +76,16 @@ class Session {
|
||||
* @return void
|
||||
*/
|
||||
static public function set($name,$value='',$prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$prefix;
|
||||
if($prefix){
|
||||
if (!is_array($_SESSION[$prefix])) {
|
||||
$_SESSION[$prefix] = [];
|
||||
$prefix = $prefix ? $prefix : self::$prefix;
|
||||
if(strpos($name,'.')){
|
||||
// 二维数组赋值
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
if($prefix){
|
||||
$_SESSION[$prefix][$name1][$name2] = $value;
|
||||
}else{
|
||||
$_SESSION[$name1][$name2] = $value;
|
||||
}
|
||||
}elseif($prefix){
|
||||
$_SESSION[$prefix][$name] = $value;
|
||||
}else{
|
||||
$_SESSION[$name] = $value;
|
||||
@@ -82,13 +98,27 @@ class Session {
|
||||
* @param string $prefix 作用域(前缀)
|
||||
* @return mixed
|
||||
*/
|
||||
static public function get($name,$prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$prefix;
|
||||
if($prefix){ // 获取session
|
||||
return isset($_SESSION[$prefix][$name])?$_SESSION[$prefix][$name]:null;
|
||||
static public function get($name='',$prefix='') {
|
||||
$prefix = $prefix ? $prefix : self::$prefix;
|
||||
if(''==$name){
|
||||
// 获取全部的session
|
||||
$value = $prefix ? $_SESSION[$prefix] : $_SESSION;
|
||||
}elseif($prefix){ // 获取session
|
||||
if(strpos($name,'.')){
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
$value = isset($_SESSION[$prefix][$name1][$name2])?$_SESSION[$prefix][$name1][$name2]:null;
|
||||
}else{
|
||||
$value = isset($_SESSION[$prefix][$name]) ? $_SESSION[$prefix][$name] : null;
|
||||
}
|
||||
}else{
|
||||
return isset($_SESSION[$name])?$_SESSION[$name]:null;
|
||||
if(strpos($name,'.')){
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
$value = isset($_SESSION[$name1][$name2]) ? $_SESSION[$name1][$name2] : null;
|
||||
}else{
|
||||
$value = isset($_SESSION[$name]) ? $_SESSION[$name] : null;
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,10 +129,19 @@ class Session {
|
||||
*/
|
||||
static public function delete($name,$prefix='') {
|
||||
$prefix = $prefix?$prefix:$this->prefix;
|
||||
if($prefix){
|
||||
unset($_SESSION[$prefix][$name]);
|
||||
if(strpos($name,'.')){
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
if($prefix){
|
||||
unset($_SESSION[$prefix][$name1][$name2]);
|
||||
}else{
|
||||
unset($_SESSION[$name1][$name2]);
|
||||
}
|
||||
}else{
|
||||
unset($_SESSION[$name]);
|
||||
if($prefix){
|
||||
unset($_SESSION[$prefix][$name]);
|
||||
}else{
|
||||
unset($_SESSION[$name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,11 +166,12 @@ class Session {
|
||||
* @return boolean
|
||||
*/
|
||||
static public function has($name,$prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$prefix;
|
||||
if($prefix){
|
||||
return isset($_SESSION[$prefix][$name]);
|
||||
$prefix = $prefix ? $prefix : self::$prefix;
|
||||
if(strpos($name,'.')){ // 支持数组
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
return $prefix ? isset($_SESSION[$prefix][$name1][$name2]) : isset($_SESSION[$name1][$name2]);
|
||||
}else{
|
||||
return isset($_SESSION[$name]);
|
||||
return $prefix ? isset($_SESSION[$prefix][$name]) : isset($_SESSION[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +180,7 @@ class Session {
|
||||
* @param string $name session操作名称
|
||||
* @return void
|
||||
*/
|
||||
static public function operate($name) {
|
||||
static private function operate($name) {
|
||||
if('pause'==$name){ // 暂停session
|
||||
session_write_close();
|
||||
}elseif('start'==$name){ // 启动session
|
||||
|
||||
@@ -17,9 +17,9 @@ namespace Think;
|
||||
* 编译型模板引擎 支持动态缓存
|
||||
*/
|
||||
class Template {
|
||||
protected $tVar = []; // 模板变量
|
||||
protected $data = []; // 模板变量
|
||||
protected $config = [ // 引擎配置
|
||||
'tpl_path' => '',
|
||||
'tpl_path' => '', // 模板路径
|
||||
'tpl_suffix' => '.html', // 默认模板文件后缀
|
||||
'cache_suffix' => '.php', // 默认模板缓存后缀
|
||||
'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
|
||||
@@ -28,8 +28,8 @@ class Template {
|
||||
'tpl_end' => '}', // 模板引擎普通标签结束标记
|
||||
'strip_space' => false, // 是否去除模板文件里面的html空格与换行
|
||||
'tpl_cache' => true, // 是否开启模板编译缓存,设为false则每次都会重新编译
|
||||
'compile_type' => 'file',
|
||||
'cache_path' => '',
|
||||
'compile_type' => 'file', // 模板编译类型
|
||||
'cache_path' => '', // 模板缓存目录
|
||||
'cache_prefix' => '', // 模板缓存前缀标识,可以动态改变
|
||||
'cache_time' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒)
|
||||
'layout_item' => '{__CONTENT__}', // 布局模板的内容替换标识
|
||||
@@ -38,7 +38,7 @@ class Template {
|
||||
'taglib_load' => true, // 是否使用内置标签库之外的其它标签库,默认自动检测
|
||||
'taglib_build_in' => 'cx', // 内置标签库名称(标签使用不必指定标签库名称),以逗号分隔 注意解析顺序
|
||||
'taglib_pre_load' => '', // 需要额外加载的标签库(须指定标签库名称),多个以逗号分隔
|
||||
'display_cache' => false,
|
||||
'display_cache' => false, // 模板渲染缓存
|
||||
];
|
||||
|
||||
private $literal = [];
|
||||
@@ -84,9 +84,9 @@ class Template {
|
||||
*/
|
||||
public function assign($name,$value=''){
|
||||
if(is_array($name)) {
|
||||
$this->tVar = array_merge($this->tVar,$name);
|
||||
$this->data = array_merge($this->data,$name);
|
||||
}else {
|
||||
$this->tVar[$name] = $value;
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ class Template {
|
||||
}
|
||||
|
||||
public function get($name){
|
||||
return $this->tVar[$name];
|
||||
return $this->data[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,7 +118,7 @@ class Template {
|
||||
*/
|
||||
public function display($template,$vars=[],$config=[]) {
|
||||
if($vars){
|
||||
$this->tVar = $vars;
|
||||
$this->data = $vars;
|
||||
}
|
||||
if($config){
|
||||
$this->config($config);
|
||||
@@ -133,7 +133,7 @@ class Template {
|
||||
ob_start();
|
||||
ob_implicit_flush(0);
|
||||
// 读取编译存储
|
||||
$this->storage->read($cacheFile,$this->tVar);
|
||||
$this->storage->read($cacheFile,$this->data);
|
||||
// 获取并清空缓存
|
||||
$content = ob_get_clean();
|
||||
if($this->config['cache_id'] && $this->config['display_cache']) {
|
||||
@@ -152,7 +152,7 @@ class Template {
|
||||
*/
|
||||
public function fetch($content,$vars=[]) {
|
||||
if($vars){
|
||||
$this->tVar = $vars;
|
||||
$this->data = $vars;
|
||||
}
|
||||
$cacheFile = $this->config['cache_path'].$this->config['cache_prefix'].md5($content).$this->config['cache_suffix'];
|
||||
if(!$this->checkCache($content,$cacheFile)) { // 缓存无效
|
||||
@@ -160,7 +160,7 @@ class Template {
|
||||
$this->compiler($content,$cacheFile);
|
||||
}
|
||||
// 读取编译存储
|
||||
$this->storage->read($cacheFile,$this->tVar);
|
||||
$this->storage->read($cacheFile,$this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -591,22 +591,22 @@ class Template {
|
||||
//模板函数过滤
|
||||
$fun = strtolower(trim($args[0]));
|
||||
switch($fun) {
|
||||
case 'default': // 特殊模板函数
|
||||
$name = '('.$name.')?('.$name.'):'.$args[1];
|
||||
break;
|
||||
default: // 通用模板函数
|
||||
if(!in_array($fun,$template_deny_funs)){
|
||||
if(isset($args[1])){
|
||||
if(strstr($args[1],'###')){
|
||||
$args[1] = str_replace('###',$name,$args[1]);
|
||||
$name = "$fun($args[1])";
|
||||
}else{
|
||||
$name = "$fun($name,$args[1])";
|
||||
case 'default': // 特殊模板函数
|
||||
$name = '('.$name.')?('.$name.'):'.$args[1];
|
||||
break;
|
||||
default: // 通用模板函数
|
||||
if(!in_array($fun,$template_deny_funs)){
|
||||
if(isset($args[1])){
|
||||
if(strstr($args[1],'###')){
|
||||
$args[1] = str_replace('###',$name,$args[1]);
|
||||
$name = "$fun($args[1])";
|
||||
}else{
|
||||
$name = "$fun($name,$args[1])";
|
||||
}
|
||||
}else if(!empty($args[0])){
|
||||
$name = "$fun($name)";
|
||||
}
|
||||
}else if(!empty($args[0])){
|
||||
$name = "$fun($name)";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $name;
|
||||
@@ -627,11 +627,14 @@ class Template {
|
||||
$vars[2] = trim($vars[2]);
|
||||
switch($vars[1]){
|
||||
case 'SERVER':
|
||||
$parseStr = '$_SERVER[\''.strtoupper($vars[2]).'\']';break;
|
||||
$parseStr = '$_SERVER[\''.strtoupper($vars[2]).'\']';
|
||||
break;
|
||||
case 'GET':
|
||||
$parseStr = '$_GET[\''.$vars[2].'\']';break;
|
||||
$parseStr = '$_GET[\''.$vars[2].'\']';
|
||||
break;
|
||||
case 'POST':
|
||||
$parseStr = '$_POST[\''.$vars[2].'\']';break;
|
||||
$parseStr = '$_POST[\''.$vars[2].'\']';
|
||||
break;
|
||||
case 'COOKIE':
|
||||
if(isset($vars[3])) {
|
||||
$parseStr = '$_COOKIE[\''.$vars[2].'\'][\''.$vars[3].'\']';
|
||||
@@ -647,19 +650,25 @@ class Template {
|
||||
}
|
||||
break;
|
||||
case 'ENV':
|
||||
$parseStr = '$_ENV[\''.strtoupper($vars[2]).'\']';break;
|
||||
$parseStr = '$_ENV[\''.strtoupper($vars[2]).'\']';
|
||||
break;
|
||||
case 'REQUEST':
|
||||
$parseStr = '$_REQUEST[\''.$vars[2].'\']';break;
|
||||
$parseStr = '$_REQUEST[\''.$vars[2].'\']';
|
||||
break;
|
||||
case 'CONST':
|
||||
$parseStr = strtoupper($vars[2]);break;
|
||||
$parseStr = strtoupper($vars[2]);
|
||||
break;
|
||||
case 'LANG':
|
||||
$parseStr = 'L("'.$vars[2].'")';break;
|
||||
$parseStr = 'L("'.$vars[2].'")';
|
||||
break;
|
||||
case 'CONFIG':
|
||||
if(isset($vars[3])) {
|
||||
$vars[2] .= '.'.$vars[3];
|
||||
}
|
||||
$parseStr = 'C("'.$vars[2].'")';break;
|
||||
default:break;
|
||||
$parseStr = 'C("'.$vars[2].'")';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}else if(count($vars)==2){
|
||||
switch($vars[1]){
|
||||
@@ -676,8 +685,9 @@ class Template {
|
||||
$parseStr = $this->config['tpl_end'];
|
||||
break;
|
||||
default:
|
||||
if(defined($vars[1]))
|
||||
if(defined($vars[1])){
|
||||
$parseStr = $vars[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $parseStr;
|
||||
|
||||
@@ -10,17 +10,20 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Template\Driver;
|
||||
use Think\Exception;
|
||||
|
||||
class File {
|
||||
// 写入编译缓存
|
||||
public function write($cacheFile,$content){
|
||||
// 检测模板目录
|
||||
$dir = dirname($cacheFile);
|
||||
if(!is_dir($dir))
|
||||
mkdir($dir,0755,true);
|
||||
if(!is_dir($dir)){
|
||||
mkdir($dir,0777,true);
|
||||
}
|
||||
// 生成模板缓存文件
|
||||
if( false === file_put_contents($cacheFile,$content))
|
||||
E('_CACHE_WRITE_ERROR_:'.$cacheFile);
|
||||
if( false === file_put_contents($cacheFile,$content)){
|
||||
throw new Exception('_CACHE_WRITE_ERROR_:'.$cacheFile);
|
||||
}
|
||||
}
|
||||
|
||||
// 读取编译编译
|
||||
|
||||
@@ -10,24 +10,27 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
use Think\Exception;
|
||||
|
||||
class View {
|
||||
protected $engine = null; // 模板引擎实例
|
||||
protected $theme = ''; // 模板主题名称
|
||||
protected $data = []; // 模板变量
|
||||
protected $config = [ // 视图参数
|
||||
'http_output_content' => true,
|
||||
'http_content_type' => 'text/html',
|
||||
'http_charset' => 'utf-8',
|
||||
'http_cache_control' => 'private',
|
||||
'http_render_content' => false,
|
||||
'theme_on' => false,
|
||||
'auto_detect_theme' => false,
|
||||
'var_theme' => 't',
|
||||
'default_theme' => 'default',
|
||||
'http_cache_id' => null,
|
||||
'view_path' => '',
|
||||
'view_suffix' => '.html',
|
||||
'http_output_content' => true,
|
||||
'http_content_type' => 'text/html',
|
||||
'http_charset' => 'utf-8',
|
||||
'http_cache_control' => 'private',
|
||||
'http_render_content' => false,
|
||||
'theme_on' => false,
|
||||
'auto_detect_theme' => false,
|
||||
'var_theme' => 't',
|
||||
'default_theme' => 'default',
|
||||
'http_cache_id' => null,
|
||||
'view_path' => '',
|
||||
'view_suffix' => '.html',
|
||||
'view_depr' => '/',
|
||||
'view_layer' => 'View',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -100,11 +103,11 @@ class View {
|
||||
* @return mixed
|
||||
*/
|
||||
public function display($template = '', $vars = [], $cache_id = '') {
|
||||
Tag::listen('view_begin', $template);
|
||||
Hook::listen('view_begin', $template);
|
||||
// 解析并获取模板内容
|
||||
$content = $this->fetch($template, $vars, $cache_id);
|
||||
// 输出内容过滤
|
||||
Tag::listen('view_filter', $content);
|
||||
Hook::listen('view_filter', $content);
|
||||
// 输出模板内容
|
||||
if($this->config['http_output_content']) {
|
||||
$this->render($content);
|
||||
@@ -123,10 +126,12 @@ class View {
|
||||
*/
|
||||
protected function fetch($template, $vars = [], $cache_id='') {
|
||||
if(!$this->config['http_render_content']) {
|
||||
// 获取模板文件名
|
||||
$template = $this->parseTemplate($template);
|
||||
// 模板不存在 抛出异常
|
||||
if(!is_file($template))
|
||||
E('template file not exists:' . $template);
|
||||
if(!is_file($template)) {
|
||||
throw new Exception('template file not exists:' . $template);
|
||||
}
|
||||
}
|
||||
$vars = $vars ? $vars : $this->data;
|
||||
// 页面缓存
|
||||
@@ -152,17 +157,25 @@ class View {
|
||||
if(is_file($template)) {
|
||||
return $template;
|
||||
}
|
||||
$template = str_replace(':', '/', $template);
|
||||
// 获取当前主题名称
|
||||
$theme = $this->getTemplateTheme();
|
||||
$depr = $this->config['view_depr'];
|
||||
$template = str_replace(':', $depr, $template);
|
||||
|
||||
// 获取当前模块
|
||||
$module = MODULE_NAME;
|
||||
if(strpos($template,'@')){ // 跨模块调用模版文件
|
||||
list($module,$template) = explode('@',$template);
|
||||
}
|
||||
// 获取当前主题的模版路径
|
||||
defined('THEME_PATH') or define('THEME_PATH', $this->getThemePath($module));
|
||||
|
||||
// 分析模板文件规则
|
||||
if('' == $template) {
|
||||
// 如果模板文件名为空 按照默认规则定位
|
||||
$template = CONTROLLER_NAME . '/' . ACTION_NAME;
|
||||
}elseif(false === strpos($template, '/')){
|
||||
$template = CONTROLLER_NAME . '/' . $template;
|
||||
$template = CONTROLLER_NAME . $depr . ACTION_NAME;
|
||||
}elseif(false === strpos($template, $depr)){
|
||||
$template = CONTROLLER_NAME . $depr . $template;
|
||||
}
|
||||
return ($this->config['view_path'] ? $this->config['view_path'] : MODULE_PATH . 'View/').$theme.$template.$this->config['view_suffix'];
|
||||
return THEME_PATH.$template.$this->config['view_suffix'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,7 +183,7 @@ class View {
|
||||
* @access private
|
||||
* @return string
|
||||
*/
|
||||
private function getTemplateTheme() {
|
||||
private function getTemplateTheme($module) {
|
||||
if($this->config['theme_on']) {
|
||||
if($this->theme) { // 指定模板主题
|
||||
$theme = $this->theme;
|
||||
@@ -182,7 +195,7 @@ class View {
|
||||
}elseif(Cookie::get('think_theme')){
|
||||
$theme = Cookie::get('think_theme');
|
||||
}
|
||||
if(!is_dir(MODULE_PATH . 'View/' . $theme)) {
|
||||
if(!is_dir(APP_PATH.$module . '/'. $this->config['view_layer'].'/' . $theme)) {
|
||||
$theme = $this->config['default_theme'];
|
||||
}
|
||||
Cookie::set('think_theme', $theme, 864000);
|
||||
@@ -194,6 +207,24 @@ class View {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的模板路径
|
||||
* @access protected
|
||||
* @param string $module 模块名
|
||||
* @return string
|
||||
*/
|
||||
protected function getThemePath($module=MODULE_NAME){
|
||||
// 获取当前主题名称
|
||||
$theme = $this->getTemplateTheme($module);
|
||||
// 获取当前主题的模版路径
|
||||
$tmplPath = $this->config['view_path']; // 模块设置独立的视图目录
|
||||
if(!$tmplPath){
|
||||
// 定义TMPL_PATH 则改变全局的视图目录到模块之外
|
||||
$tmplPath = defined('TMPL_PATH')? TMPL_PATH.$module.'/' : APP_PATH.$module.'/'.$this->config['view_layer'].'/';
|
||||
}
|
||||
return $tmplPath.$theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图输出参数设置
|
||||
* @access public
|
||||
|
||||
@@ -190,8 +190,8 @@ trait Extend {
|
||||
if(false !== ($value = F($guid))) { // 存在缓存写入数据
|
||||
if(time()>S($guid.'_time')+$lazyTime) {
|
||||
// 延时更新时间到了,删除缓存数据 并实际写入数据库
|
||||
S($guid,NULL);
|
||||
S($guid.'_time',NULL);
|
||||
S($guid,null);
|
||||
S($guid.'_time',null);
|
||||
return $value+$step;
|
||||
}else{
|
||||
// 追加数据到缓存
|
||||
|
||||
@@ -24,7 +24,7 @@ return [
|
||||
'Think\Error' => CORE_PATH . 'Error.php',
|
||||
'Think\Cache' => CORE_PATH . 'Cache.php',
|
||||
'Think\Cache\Driver\File' => CORE_PATH . 'Cache/Driver/File.php',
|
||||
'Think\Tag' => CORE_PATH . 'Tag.php',
|
||||
'Think\Hook' => CORE_PATH . 'Hook.php',
|
||||
'Think\Session' => CORE_PATH . 'Session.php',
|
||||
'Think\Cookie' => CORE_PATH . 'Cookie.php',
|
||||
'Think\Controller' => CORE_PATH . 'Controller.php',
|
||||
|
||||
15
base.php
15
base.php
@@ -29,6 +29,13 @@ defined('VENDOR_PATH') or define('VENDOR_PATH', THINK_PATH.'Vendor/');
|
||||
defined('EXT') or define('EXT', '.php');
|
||||
defined('APP_DEBUG') or define('APP_DEBUG', false); // 是否调试模式
|
||||
|
||||
defined('VAR_MODULE') or define('VAR_MODULE', 'm');
|
||||
defined('VAR_CONTROLLER') or define('VAR_CONTROLLER','c');
|
||||
defined('VAR_ACTION') or define('VAR_ACTION','a');
|
||||
defined('VAR_ADDON') or define('VAR_ADDON','addon');
|
||||
defined('VAR_PATHINFO') or define('VAR_PATHINFO','s');
|
||||
defined('VAR_TEMPLATE') or define('VAR_TEMPLATE','t');
|
||||
|
||||
// 环境常量
|
||||
define('MEMORY_LIMIT_ON', function_exists('memory_get_usage'));
|
||||
define('IS_CGI', strpos(PHP_SAPI, 'cgi') === 0 ? 1 : 0);
|
||||
@@ -167,7 +174,7 @@ function E($msg, $code=0) {
|
||||
/**
|
||||
* 浏览器友好的变量输出
|
||||
* @param mixed $var 变量
|
||||
* @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
|
||||
* @param boolean $echo 是否输出 默认为true 如果为false 则返回输出字符串
|
||||
* @param string $label 标签 默认为空
|
||||
* @return void|string
|
||||
*/
|
||||
@@ -182,7 +189,7 @@ function dump($var, $echo=true, $label=null) {
|
||||
* @return void
|
||||
*/
|
||||
function W($name, $data=[]) {
|
||||
echo Think\Loader::action($name,$data,'Widget');
|
||||
return Think\Loader::action($name,$data,'Widget');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,9 +215,9 @@ function S($name,$value='',$options=null) {
|
||||
return $cache->rm($name);
|
||||
}else { // 缓存数据
|
||||
if(is_array($options)) {
|
||||
$expire = isset($options['expire'])?$options['expire']:NULL; //修复查询缓存无法设置过期时间
|
||||
$expire = isset($options['expire'])?$options['expire']:null; //修复查询缓存无法设置过期时间
|
||||
}else{
|
||||
$expire = is_numeric($options)?$options:NULL; //默认快捷缓存设置过期时间
|
||||
$expire = is_numeric($options)?$options:null; //默认快捷缓存设置过期时间
|
||||
}
|
||||
return $cache->set($name, $value, $expire);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ return [
|
||||
'app_debug' => true, // 调试模式
|
||||
'app_status' => 'debug',// 调试模式状态
|
||||
'var_module' => 'm', // 模块变量名
|
||||
'var_group' => 'g', // 分组变量名
|
||||
'var_controller' => 'c', // 控制器变量名
|
||||
'var_action' => 'a', // 操作变量名
|
||||
'var_pathinfo' => 's', // PATHINFO变量名 用于兼容模式
|
||||
@@ -22,8 +21,6 @@ return [
|
||||
'pathinfo_depr' => '/', // pathinfo分隔符
|
||||
'require_module' => true, // 是否显示模块
|
||||
'default_module' => 'index', // 默认模块名
|
||||
'require_group' => false, // 控制器是否需要分组
|
||||
'default_group' => '', // 默认分组名
|
||||
'require_controller' => true, // 是否显示控制器
|
||||
'default_controller' => 'index', // 默认控制器名
|
||||
'default_action' => 'index', // 默认操作名
|
||||
@@ -38,6 +35,7 @@ return [
|
||||
'default_ajax_return' => 'JSON', // 默认AJAX 数据返回格式,可选JSON XML ...
|
||||
'default_jsonp_handler' => 'jsonpReturn', // 默认JSONP格式返回的处理方法
|
||||
'var_jsonp_handler' => 'callback',
|
||||
'template_engine' => 'think',
|
||||
|
||||
/* 错误设置 */
|
||||
'error_message' => '页面错误!请稍后再试~',//错误显示信息,非调试模式有效
|
||||
|
||||
@@ -34,7 +34,7 @@ set_error_handler(['Think\Error','appError']);
|
||||
set_exception_handler(['Think\Error','appException']);
|
||||
|
||||
// 导入系统惯例
|
||||
Config::load(THINK_PATH.'convention.php');
|
||||
$config = Config::load(THINK_PATH.'convention.php');
|
||||
|
||||
// 初始化操作可以在应用的公共文件中处理 下面只是示例
|
||||
//---------------------------------------------------
|
||||
@@ -53,4 +53,4 @@ if(is_file(APP_PATH.'build.php')) { // 自动化创建脚本
|
||||
Create::build(include APP_PATH.'build.php');
|
||||
}
|
||||
// 执行应用
|
||||
App::run();
|
||||
App::run($config);
|
||||
|
||||
Reference in New Issue
Block a user