App.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 API模式 应用程序类
  14. */
  15. class App {
  16. /**
  17. * 应用程序初始化
  18. * @access public
  19. * @return void
  20. */
  21. static public function init() {
  22. // 定义当前请求的系统常量
  23. define('NOW_TIME', $_SERVER['REQUEST_TIME']);
  24. define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']);
  25. define('IS_GET', REQUEST_METHOD =='GET' ? true : false);
  26. define('IS_POST', REQUEST_METHOD =='POST' ? true : false);
  27. define('IS_PUT', REQUEST_METHOD =='PUT' ? true : false);
  28. define('IS_DELETE', REQUEST_METHOD =='DELETE' ? true : false);
  29. 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);
  30. // URL调度
  31. Dispatcher::dispatch();
  32. if(C('REQUEST_VARS_FILTER')){
  33. // 全局安全过滤
  34. array_walk_recursive($_GET, 'think_filter');
  35. array_walk_recursive($_POST, 'think_filter');
  36. array_walk_recursive($_REQUEST, 'think_filter');
  37. }
  38. // 日志目录转换为绝对路径
  39. C('LOG_PATH',realpath(LOG_PATH).'/');
  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 = A(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;
  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(C('URL_PARAMS_BIND') && $method->getNumberOfParameters()>0){
  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. array_walk_recursive($args,'think_filter');
  101. $method->invokeArgs($module,$args);
  102. }else{
  103. $method->invoke($module);
  104. }
  105. }else{
  106. // 操作方法不是Public 抛出异常
  107. throw new \ReflectionException();
  108. }
  109. } catch (\ReflectionException $e) {
  110. // 方法调用发生异常后 引导到__call方法处理
  111. $method = new \ReflectionMethod($module,'__call');
  112. $method->invokeArgs($module,array($action,''));
  113. }
  114. return ;
  115. }
  116. /**
  117. * 运行应用实例 入口文件使用的快捷方法
  118. * @access public
  119. * @return void
  120. */
  121. static public function run() {
  122. App::init();
  123. // Session初始化
  124. if(!IS_CLI){
  125. session(C('SESSION_OPTIONS'));
  126. }
  127. // 记录应用初始化时间
  128. G('initTime');
  129. App::exec();
  130. return ;
  131. }
  132. }