App.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think;
  12. /**
  13. * ThinkPHP 应用程序类 执行应用过程管理
  14. */
  15. class App {
  16. /**
  17. * 应用程序初始化
  18. * @access public
  19. * @return void
  20. */
  21. static public function init() {
  22. // 加载动态应用公共文件和配置
  23. load_ext_file(COMMON_PATH);
  24. // 日志目录转换为绝对路径 默认情况下存储到公共模块下面
  25. C('LOG_PATH', realpath(LOG_PATH).'/Common/');
  26. // 定义当前请求的系统常量
  27. define('NOW_TIME', $_SERVER['REQUEST_TIME']);
  28. define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']);
  29. define('IS_GET', REQUEST_METHOD =='GET' ? true : false);
  30. define('IS_POST', REQUEST_METHOD =='POST' ? true : false);
  31. define('IS_PUT', REQUEST_METHOD =='PUT' ? true : false);
  32. define('IS_DELETE', REQUEST_METHOD =='DELETE' ? true : false);
  33. // URL调度
  34. Dispatcher::dispatch();
  35. if(C('REQUEST_VARS_FILTER')){
  36. // 全局安全过滤
  37. array_walk_recursive($_GET, 'think_filter');
  38. array_walk_recursive($_POST, 'think_filter');
  39. array_walk_recursive($_REQUEST, 'think_filter');
  40. }
  41. // URL调度结束标签
  42. Hook::listen('url_dispatch');
  43. define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false);
  44. // TMPL_EXCEPTION_FILE 改为绝对地址
  45. C('TMPL_EXCEPTION_FILE',realpath(C('TMPL_EXCEPTION_FILE')));
  46. return ;
  47. }
  48. /**
  49. * 执行应用程序
  50. * @access public
  51. * @return void
  52. */
  53. static public function exec() {
  54. if(!preg_match('/^[A-Za-z](\/|\w)*$/',CONTROLLER_NAME)){ // 安全检测
  55. $module = false;
  56. }elseif(C('ACTION_BIND_CLASS')){
  57. // 操作绑定到类:模块\Controller\控制器\操作
  58. $layer = C('DEFAULT_C_LAYER');
  59. if(is_dir(MODULE_PATH.$layer.'/'.CONTROLLER_NAME)){
  60. $namespace = MODULE_NAME.'\\'.$layer.'\\'.CONTROLLER_NAME.'\\';
  61. }else{
  62. // 空控制器
  63. $namespace = MODULE_NAME.'\\'.$layer.'\\_empty\\';
  64. }
  65. $actionName = strtolower(ACTION_NAME);
  66. if(class_exists($namespace.$actionName)){
  67. $class = $namespace.$actionName;
  68. }elseif(class_exists($namespace.'_empty')){
  69. // 空操作
  70. $class = $namespace.'_empty';
  71. }else{
  72. E(L('_ERROR_ACTION_').':'.ACTION_NAME);
  73. }
  74. $module = new $class;
  75. // 操作绑定到类后 固定执行run入口
  76. $action = 'run';
  77. }else{
  78. //创建控制器实例
  79. $module = controller(CONTROLLER_NAME,CONTROLLER_PATH);
  80. }
  81. if(!$module) {
  82. if('4e5e5d7364f443e28fbf0d3ae744a59a' == CONTROLLER_NAME) {
  83. header("Content-type:image/png");
  84. exit(base64_decode(App::logo()));
  85. }
  86. // 是否定义Empty控制器
  87. $module = A('Empty');
  88. if(!$module){
  89. E(L('_CONTROLLER_NOT_EXIST_').':'.CONTROLLER_NAME);
  90. }
  91. }
  92. // 获取当前操作名 支持动态路由
  93. if(!isset($action)){
  94. $action = ACTION_NAME.C('ACTION_SUFFIX');
  95. }
  96. try{
  97. if(!preg_match('/^[A-Za-z](\w)*$/',$action)){
  98. // 非法操作
  99. throw new \ReflectionException();
  100. }
  101. //执行当前操作
  102. $method = new \ReflectionMethod($module, $action);
  103. if($method->isPublic() && !$method->isStatic()) {
  104. $class = new \ReflectionClass($module);
  105. // 前置操作
  106. if($class->hasMethod('_before_'.$action)) {
  107. $before = $class->getMethod('_before_'.$action);
  108. if($before->isPublic()) {
  109. $before->invoke($module);
  110. }
  111. }
  112. // URL参数绑定检测
  113. if($method->getNumberOfParameters()>0 && C('URL_PARAMS_BIND')){
  114. switch($_SERVER['REQUEST_METHOD']) {
  115. case 'POST':
  116. $vars = array_merge($_GET,$_POST);
  117. break;
  118. case 'PUT':
  119. parse_str(file_get_contents('php://input'), $vars);
  120. break;
  121. default:
  122. $vars = $_GET;
  123. }
  124. $params = $method->getParameters();
  125. $paramsBindType = C('URL_PARAMS_BIND_TYPE');
  126. foreach ($params as $param){
  127. $name = $param->getName();
  128. if( 1 == $paramsBindType && !empty($vars) ){
  129. $args[] = array_shift($vars);
  130. }elseif( 0 == $paramsBindType && isset($vars[$name])){
  131. $args[] = $vars[$name];
  132. }elseif($param->isDefaultValueAvailable()){
  133. $args[] = $param->getDefaultValue();
  134. }else{
  135. E(L('_PARAM_ERROR_').':'.$name);
  136. }
  137. }
  138. // 开启绑定参数过滤机制
  139. if(C('URL_PARAMS_SAFE')){
  140. $filters = C('URL_PARAMS_FILTER')?:C('DEFAULT_FILTER');
  141. if($filters) {
  142. $filters = explode(',',$filters);
  143. foreach($filters as $filter){
  144. $args = array_map_recursive($filter,$args); // 参数过滤
  145. }
  146. }
  147. }
  148. array_walk_recursive($args,'think_filter');
  149. $method->invokeArgs($module,$args);
  150. }else{
  151. $method->invoke($module);
  152. }
  153. // 后置操作
  154. if($class->hasMethod('_after_'.$action)) {
  155. $after = $class->getMethod('_after_'.$action);
  156. if($after->isPublic()) {
  157. $after->invoke($module);
  158. }
  159. }
  160. }else{
  161. // 操作方法不是Public 抛出异常
  162. throw new \ReflectionException();
  163. }
  164. } catch (\ReflectionException $e) {
  165. // 方法调用发生异常后 引导到__call方法处理
  166. $method = new \ReflectionMethod($module,'__call');
  167. $method->invokeArgs($module,array($action,''));
  168. }
  169. return ;
  170. }
  171. /**
  172. * 运行应用实例 入口文件使用的快捷方法
  173. * @access public
  174. * @return void
  175. */
  176. static public function run() {
  177. // 应用初始化标签
  178. Hook::listen('app_init');
  179. App::init();
  180. // 应用开始标签
  181. Hook::listen('app_begin');
  182. // Session初始化
  183. if(!IS_CLI){
  184. session(C('SESSION_OPTIONS'));
  185. }
  186. // 记录应用初始化时间
  187. G('initTime');
  188. App::exec();
  189. // 应用结束标签
  190. Hook::listen('app_end');
  191. return ;
  192. }
  193. static public function logo(){
  194. return 'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21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