App.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. C('LOG_PATH', realpath(LOG_PATH).'/Common/');
  24. // 定义当前请求的系统常量
  25. define('NOW_TIME', $_SERVER['REQUEST_TIME']);
  26. define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']);
  27. define('IS_GET', REQUEST_METHOD =='GET' ? true : false);
  28. define('IS_POST', REQUEST_METHOD =='POST' ? true : false);
  29. define('IS_PUT', REQUEST_METHOD =='PUT' ? true : false);
  30. define('IS_DELETE', REQUEST_METHOD =='DELETE' ? true : false);
  31. // URL调度
  32. Dispatcher::dispatch();
  33. if(C('REQUEST_VARS_FILTER')){
  34. // 全局安全过滤
  35. array_walk_recursive($_GET, 'think_filter');
  36. array_walk_recursive($_POST, 'think_filter');
  37. array_walk_recursive($_REQUEST, 'think_filter');
  38. }
  39. 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);
  40. // TMPL_EXCEPTION_FILE 改为绝对地址
  41. C('TMPL_EXCEPTION_FILE',realpath(C('TMPL_EXCEPTION_FILE')));
  42. return ;
  43. }
  44. /**
  45. * 执行应用程序
  46. * @access public
  47. * @return void
  48. */
  49. static public function exec() {
  50. if(!preg_match('/^[A-Za-z](\/|\w)*$/',CONTROLLER_NAME)){ // 安全检测
  51. $module = false;
  52. }else{
  53. //创建控制器实例
  54. $module = controller(CONTROLLER_NAME);
  55. }
  56. if(!$module) {
  57. // 是否定义Empty控制器
  58. $module = A('Empty');
  59. if(!$module){
  60. E(L('_CONTROLLER_NOT_EXIST_').':'.CONTROLLER_NAME);
  61. }
  62. }
  63. // 获取当前操作名 支持动态路由
  64. $action = ACTION_NAME.C('ACTION_SUFFIX');
  65. try{
  66. if(!preg_match('/^[A-Za-z](\w)*$/',$action)){
  67. // 非法操作
  68. throw new \ReflectionException();
  69. }
  70. //执行当前操作
  71. $method = new \ReflectionMethod($module, $action);
  72. if($method->isPublic() && !$method->isStatic()) {
  73. $class = new \ReflectionClass($module);
  74. // URL参数绑定检测
  75. if($method->getNumberOfParameters()>0 && C('URL_PARAMS_BIND')){
  76. switch($_SERVER['REQUEST_METHOD']) {
  77. case 'POST':
  78. $vars = array_merge($_GET,$_POST);
  79. break;
  80. case 'PUT':
  81. parse_str(file_get_contents('php://input'), $vars);
  82. break;
  83. default:
  84. $vars = $_GET;
  85. }
  86. $params = $method->getParameters();
  87. $paramsBindType = C('URL_PARAMS_BIND_TYPE');
  88. foreach ($params as $param){
  89. $name = $param->getName();
  90. if( 1 == $paramsBindType && !empty($vars) ){
  91. $args[] = array_shift($vars);
  92. }elseif( 0 == $paramsBindType && isset($vars[$name])){
  93. $args[] = $vars[$name];
  94. }elseif($param->isDefaultValueAvailable()){
  95. $args[] = $param->getDefaultValue();
  96. }else{
  97. E(L('_PARAM_ERROR_').':'.$name);
  98. }
  99. }
  100. // 开启绑定参数过滤机制
  101. if(C('URL_PARAMS_SAFE')){
  102. $filters = C('URL_PARAMS_FILTER')?:C('DEFAULT_FILTER');
  103. if($filters) {
  104. $filters = explode(',',$filters);
  105. foreach($filters as $filter){
  106. $args = array_map_recursive($filter,$args); // 参数过滤
  107. }
  108. }
  109. }
  110. array_walk_recursive($args,'think_filter');
  111. $method->invokeArgs($module,$args);
  112. }else{
  113. $method->invoke($module);
  114. }
  115. }else{
  116. // 操作方法不是Public 抛出异常
  117. throw new \ReflectionException();
  118. }
  119. } catch (\ReflectionException $e) {
  120. // 方法调用发生异常后 引导到__call方法处理
  121. $method = new \ReflectionMethod($module,'__call');
  122. $method->invokeArgs($module,array($action,''));
  123. }
  124. return ;
  125. }
  126. /**
  127. * 运行应用实例 入口文件使用的快捷方法
  128. * @access public
  129. * @return void
  130. */
  131. static public function run() {
  132. App::init();
  133. // Session初始化
  134. if(!IS_CLI){
  135. session(C('SESSION_OPTIONS'));
  136. }
  137. // 记录应用初始化时间
  138. G('initTime');
  139. App::exec();
  140. return ;
  141. }
  142. }