Dispatcher.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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内置的Dispatcher类
  14. * 完成URL解析、路由和调度
  15. */
  16. class Dispatcher {
  17. /**
  18. * URL映射到控制器
  19. * @access public
  20. * @return void
  21. */
  22. static public function dispatch() {
  23. $varPath = C('VAR_PATHINFO');
  24. $varAddon = C('VAR_ADDON');
  25. $varModule = C('VAR_MODULE');
  26. $varController = C('VAR_CONTROLLER');
  27. $varAction = C('VAR_ACTION');
  28. $urlCase = C('URL_CASE_INSENSITIVE');
  29. if(isset($_GET[$varPath])) { // 判断URL里面是否有兼容模式参数
  30. $_SERVER['PATH_INFO'] = $_GET[$varPath];
  31. unset($_GET[$varPath]);
  32. }elseif(IS_CLI){ // CLI模式下 index.php module/controller/action/params/...
  33. $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  34. }
  35. // 开启子域名部署
  36. if(C('APP_SUB_DOMAIN_DEPLOY')) {
  37. $rules = C('APP_SUB_DOMAIN_RULES');
  38. if(isset($rules[$_SERVER['HTTP_HOST']])) { // 完整域名或者IP配置
  39. define('APP_DOMAIN',$_SERVER['HTTP_HOST']); // 当前完整域名
  40. $rule = $rules[APP_DOMAIN];
  41. }else{
  42. if(strpos(C('APP_DOMAIN_SUFFIX'),'.')){ // com.cn net.cn
  43. $domain = array_slice(explode('.', $_SERVER['HTTP_HOST']), 0, -3);
  44. }else{
  45. $domain = array_slice(explode('.', $_SERVER['HTTP_HOST']), 0, -2);
  46. }
  47. if(!empty($domain)) {
  48. $subDomain = implode('.', $domain);
  49. define('SUB_DOMAIN',$subDomain); // 当前完整子域名
  50. $domain2 = array_pop($domain); // 二级域名
  51. if($domain) { // 存在三级域名
  52. $domain3 = array_pop($domain);
  53. }
  54. if(isset($rules[$subDomain])) { // 子域名
  55. $rule = $rules[$subDomain];
  56. }elseif(isset($rules['*.' . $domain2]) && !empty($domain3)){ // 泛三级域名
  57. $rule = $rules['*.' . $domain2];
  58. $panDomain = $domain3;
  59. }elseif(isset($rules['*']) && !empty($domain2) && 'www' != $domain2 ){ // 泛二级域名
  60. $rule = $rules['*'];
  61. $panDomain = $domain2;
  62. }
  63. }
  64. }
  65. if(!empty($rule)) {
  66. // 子域名部署规则 '子域名'=>array('模块名[/控制器名]','var1=a&var2=b');
  67. if(is_array($rule)){
  68. list($rule,$vars) = $rule;
  69. }
  70. $array = explode('/',$rule);
  71. // 模块绑定
  72. define('BIND_MODULE',array_shift($array));
  73. // 控制器绑定
  74. if(!empty($array)) {
  75. $controller = array_shift($array);
  76. if($controller){
  77. define('BIND_CONTROLLER',$controller);
  78. }
  79. }
  80. if(isset($vars)) { // 传入参数
  81. parse_str($vars,$parms);
  82. if(isset($panDomain)){
  83. $pos = array_search('*', $parms);
  84. if(false !== $pos) {
  85. // 泛域名作为参数
  86. $parms[$pos] = $panDomain;
  87. }
  88. }
  89. $_GET = array_merge($_GET,$parms);
  90. }
  91. }
  92. }
  93. // 分析PATHINFO信息
  94. if(!isset($_SERVER['PATH_INFO'])) {
  95. $types = explode(',',C('URL_PATHINFO_FETCH'));
  96. foreach ($types as $type){
  97. if(0===strpos($type,':')) {// 支持函数判断
  98. $_SERVER['PATH_INFO'] = call_user_func(substr($type,1));
  99. break;
  100. }elseif(!empty($_SERVER[$type])) {
  101. $_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type],$_SERVER['SCRIPT_NAME']))?
  102. substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
  103. break;
  104. }
  105. }
  106. }
  107. $depr = C('URL_PATHINFO_DEPR');
  108. define('MODULE_PATHINFO_DEPR', $depr);
  109. if(empty($_SERVER['PATH_INFO'])) {
  110. $_SERVER['PATH_INFO'] = '';
  111. define('__INFO__','');
  112. define('__EXT__','');
  113. }else{
  114. define('__INFO__',trim($_SERVER['PATH_INFO'],'/'));
  115. // URL后缀
  116. define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'],PATHINFO_EXTENSION)));
  117. $_SERVER['PATH_INFO'] = __INFO__;
  118. if(!defined('BIND_MODULE') && (!C('URL_ROUTER_ON') || !Route::check())){
  119. if (__INFO__ && C('MULTI_MODULE')){ // 获取模块名
  120. $paths = explode($depr,__INFO__,2);
  121. $allowList = C('MODULE_ALLOW_LIST'); // 允许的模块列表
  122. $module = preg_replace('/\.' . __EXT__ . '$/i', '',$paths[0]);
  123. if( empty($allowList) || (is_array($allowList) && in_array_case($module, $allowList))){
  124. $_GET[$varModule] = $module;
  125. $_SERVER['PATH_INFO'] = isset($paths[1])?$paths[1]:'';
  126. }
  127. }
  128. }
  129. }
  130. // URL常量
  131. define('__SELF__',strip_tags($_SERVER[C('URL_REQUEST_URI')]));
  132. // 获取模块名称
  133. define('MODULE_NAME', defined('BIND_MODULE')? BIND_MODULE : self::getModule($varModule));
  134. // 检测模块是否存在
  135. if( MODULE_NAME && (defined('BIND_MODULE') || !in_array_case(MODULE_NAME,C('MODULE_DENY_LIST')) ) && is_dir(APP_PATH.MODULE_NAME)){
  136. // 定义当前模块路径
  137. define('MODULE_PATH', APP_PATH.MODULE_NAME.'/');
  138. // 定义当前模块的模版缓存路径
  139. C('CACHE_PATH',CACHE_PATH.MODULE_NAME.'/');
  140. // 定义当前模块的日志目录
  141. C('LOG_PATH', realpath(LOG_PATH).'/'.MODULE_NAME.'/');
  142. // 模块检测
  143. Hook::listen('module_check');
  144. // 加载模块配置文件
  145. if(is_file(MODULE_PATH.'Conf/config'.CONF_EXT))
  146. C(load_config(MODULE_PATH.'Conf/config'.CONF_EXT));
  147. // 加载应用模式对应的配置文件
  148. if('common' != APP_MODE && is_file(MODULE_PATH.'Conf/config_'.APP_MODE.CONF_EXT))
  149. C(load_config(MODULE_PATH.'Conf/config_'.APP_MODE.CONF_EXT));
  150. // 当前应用状态对应的配置文件
  151. if(APP_STATUS && is_file(MODULE_PATH.'Conf/'.APP_STATUS.CONF_EXT))
  152. C(load_config(MODULE_PATH.'Conf/'.APP_STATUS.CONF_EXT));
  153. // 加载模块别名定义
  154. if(is_file(MODULE_PATH.'Conf/alias.php'))
  155. Think::addMap(include MODULE_PATH.'Conf/alias.php');
  156. // 加载模块tags文件定义
  157. if(is_file(MODULE_PATH.'Conf/tags.php'))
  158. Hook::import(include MODULE_PATH.'Conf/tags.php');
  159. // 加载模块函数文件
  160. if(is_file(MODULE_PATH.'Common/function.php'))
  161. include MODULE_PATH.'Common/function.php';
  162. // 加载模块的扩展配置文件
  163. load_ext_file(MODULE_PATH);
  164. }else{
  165. E(L('_MODULE_NOT_EXIST_').':'.MODULE_NAME);
  166. }
  167. if(!defined('__APP__')){
  168. $urlMode = C('URL_MODEL');
  169. if($urlMode == URL_COMPAT ){// 兼容模式判断
  170. define('PHP_FILE',_PHP_FILE_.'?'.$varPath.'=');
  171. }elseif($urlMode == URL_REWRITE ) {
  172. $url = dirname(_PHP_FILE_);
  173. if($url == '/' || $url == '\\')
  174. $url = '';
  175. define('PHP_FILE',$url);
  176. }else {
  177. define('PHP_FILE',_PHP_FILE_);
  178. }
  179. // 当前应用地址
  180. define('__APP__',strip_tags(PHP_FILE));
  181. }
  182. // 模块URL地址
  183. $moduleName = defined('MODULE_ALIAS')? MODULE_ALIAS : MODULE_NAME;
  184. define('__MODULE__',(defined('BIND_MODULE') || !C('MULTI_MODULE'))? __APP__ : __APP__.'/'.($urlCase ? strtolower($moduleName) : $moduleName));
  185. if('' != $_SERVER['PATH_INFO'] && (!C('URL_ROUTER_ON') || !Route::check()) ){ // 检测路由规则 如果没有则按默认规则调度URL
  186. Hook::listen('path_info');
  187. // 检查禁止访问的URL后缀
  188. if(C('URL_DENY_SUFFIX') && preg_match('/\.('.trim(C('URL_DENY_SUFFIX'),'.').')$/i', $_SERVER['PATH_INFO'])){
  189. send_http_status(404);
  190. exit;
  191. }
  192. // 去除URL后缀
  193. $_SERVER['PATH_INFO'] = preg_replace(C('URL_HTML_SUFFIX')? '/\.('.trim(C('URL_HTML_SUFFIX'),'.').')$/i' : '/\.'.__EXT__.'$/i', '', $_SERVER['PATH_INFO']);
  194. $depr = C('URL_PATHINFO_DEPR');
  195. $paths = explode($depr,trim($_SERVER['PATH_INFO'],$depr));
  196. if(!defined('BIND_CONTROLLER')) {// 获取控制器
  197. if(C('CONTROLLER_LEVEL')>1){// 控制器层次
  198. $_GET[$varController] = implode('/',array_slice($paths,0,C('CONTROLLER_LEVEL')));
  199. $paths = array_slice($paths, C('CONTROLLER_LEVEL'));
  200. }else{
  201. $_GET[$varController] = array_shift($paths);
  202. }
  203. }
  204. // 获取操作
  205. if(!defined('BIND_ACTION')){
  206. $_GET[$varAction] = array_shift($paths);
  207. }
  208. // 解析剩余的URL参数
  209. $var = array();
  210. if(C('URL_PARAMS_BIND') && 1 == C('URL_PARAMS_BIND_TYPE')){
  211. // URL参数按顺序绑定变量
  212. $var = $paths;
  213. }else{
  214. preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){$var[$match[1]]=strip_tags($match[2]);}, implode('/',$paths));
  215. }
  216. $_GET = array_merge($var,$_GET);
  217. }
  218. // 获取控制器的命名空间(路径)
  219. define('CONTROLLER_PATH', self::getSpace($varAddon,$urlCase));
  220. // 获取控制器和操作名
  221. define('CONTROLLER_NAME', defined('BIND_CONTROLLER')? BIND_CONTROLLER : self::getController($varController,$urlCase));
  222. define('ACTION_NAME', defined('BIND_ACTION')? BIND_ACTION : self::getAction($varAction,$urlCase));
  223. // 当前控制器的UR地址
  224. $controllerName = defined('CONTROLLER_ALIAS')? CONTROLLER_ALIAS : CONTROLLER_NAME;
  225. define('__CONTROLLER__',__MODULE__.$depr.(defined('BIND_CONTROLLER')? '': ( $urlCase ? parse_name($controllerName) : $controllerName )) );
  226. // 当前操作的URL地址
  227. define('__ACTION__',__CONTROLLER__.$depr.(defined('ACTION_ALIAS')?ACTION_ALIAS:ACTION_NAME));
  228. //保证$_REQUEST正常取值
  229. $_REQUEST = array_merge($_POST,$_GET);
  230. }
  231. /**
  232. * 获得控制器的命名空间路径 便于插件机制访问
  233. */
  234. static private function getSpace($var,$urlCase) {
  235. $space = !empty($_GET[$var])?strip_tags($_GET[$var]):'';
  236. unset($_GET[$var]);
  237. return $space;
  238. }
  239. /**
  240. * 获得实际的控制器名称
  241. */
  242. static private function getController($var,$urlCase) {
  243. $controller = (!empty($_GET[$var])? $_GET[$var]:C('DEFAULT_CONTROLLER'));
  244. unset($_GET[$var]);
  245. if($maps = C('URL_CONTROLLER_MAP')) {
  246. if(isset($maps[strtolower($controller)])) {
  247. // 记录当前别名
  248. define('CONTROLLER_ALIAS',strtolower($controller));
  249. // 获取实际的控制器名
  250. return ucfirst($maps[CONTROLLER_ALIAS]);
  251. }elseif(array_search(strtolower($controller),$maps)){
  252. // 禁止访问原始控制器
  253. return '';
  254. }
  255. }
  256. if($urlCase) {
  257. // URL地址不区分大小写
  258. // 智能识别方式 user_type 识别到 UserTypeController 控制器
  259. $controller = parse_name($controller,1);
  260. }
  261. return strip_tags(ucfirst($controller));
  262. }
  263. /**
  264. * 获得实际的操作名称
  265. */
  266. static private function getAction($var,$urlCase) {
  267. $action = !empty($_POST[$var]) ?
  268. $_POST[$var] :
  269. (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_ACTION'));
  270. unset($_POST[$var],$_GET[$var]);
  271. if($maps = C('URL_ACTION_MAP')) {
  272. if(isset($maps[strtolower(CONTROLLER_NAME)])) {
  273. $maps = $maps[strtolower(CONTROLLER_NAME)];
  274. if(isset($maps[strtolower($action)])) {
  275. // 记录当前别名
  276. define('ACTION_ALIAS',strtolower($action));
  277. // 获取实际的操作名
  278. if(is_array($maps[ACTION_ALIAS])){
  279. parse_str($maps[ACTION_ALIAS][1],$vars);
  280. $_GET = array_merge($_GET,$vars);
  281. return $maps[ACTION_ALIAS][0];
  282. }else{
  283. return $maps[ACTION_ALIAS];
  284. }
  285. }elseif(array_search(strtolower($action),$maps)){
  286. // 禁止访问原始操作
  287. return '';
  288. }
  289. }
  290. }
  291. return strip_tags( $urlCase? strtolower($action) : $action );
  292. }
  293. /**
  294. * 获得实际的模块名称
  295. */
  296. static private function getModule($var) {
  297. $module = (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_MODULE'));
  298. unset($_GET[$var]);
  299. if($maps = C('URL_MODULE_MAP')) {
  300. if(isset($maps[strtolower($module)])) {
  301. // 记录当前别名
  302. define('MODULE_ALIAS',strtolower($module));
  303. // 获取实际的模块名
  304. return ucfirst($maps[MODULE_ALIAS]);
  305. }elseif(array_search(strtolower($module),$maps)){
  306. // 禁止访问原始模块
  307. return '';
  308. }
  309. }
  310. return strip_tags(ucfirst($module));
  311. }
  312. }