Dispatcher.class.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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模式的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. $varModule = C('VAR_MODULE');
  25. $varController = C('VAR_CONTROLLER');
  26. $varAction = C('VAR_ACTION');
  27. $urlCase = C('URL_CASE_INSENSITIVE');
  28. if(isset($_GET[$varPath])) { // 判断URL里面是否有兼容模式参数
  29. $_SERVER['PATH_INFO'] = $_GET[$varPath];
  30. unset($_GET[$varPath]);
  31. }elseif(IS_CLI){ // CLI模式下 index.php module/controller/action/params/...
  32. $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
  33. }
  34. // 开启子域名部署
  35. if(C('APP_SUB_DOMAIN_DEPLOY')) {
  36. $rules = C('APP_SUB_DOMAIN_RULES');
  37. if(isset($rules[$_SERVER['HTTP_HOST']])) { // 完整域名或者IP配置
  38. define('APP_DOMAIN',$_SERVER['HTTP_HOST']); // 当前完整域名
  39. $rule = $rules[APP_DOMAIN];
  40. }else{
  41. if(strpos(C('APP_DOMAIN_SUFFIX'),'.')){ // com.cn net.cn
  42. $domain = array_slice(explode('.', $_SERVER['HTTP_HOST']), 0, -3);
  43. }else{
  44. $domain = array_slice(explode('.', $_SERVER['HTTP_HOST']), 0, -2);
  45. }
  46. if(!empty($domain)) {
  47. $subDomain = implode('.', $domain);
  48. define('SUB_DOMAIN',$subDomain); // 当前完整子域名
  49. $domain2 = array_pop($domain); // 二级域名
  50. if($domain) { // 存在三级域名
  51. $domain3 = array_pop($domain);
  52. }
  53. if(isset($rules[$subDomain])) { // 子域名
  54. $rule = $rules[$subDomain];
  55. }elseif(isset($rules['*.' . $domain2]) && !empty($domain3)){ // 泛三级域名
  56. $rule = $rules['*.' . $domain2];
  57. $panDomain = $domain3;
  58. }elseif(isset($rules['*']) && !empty($domain2) && 'www' != $domain2 ){ // 泛二级域名
  59. $rule = $rules['*'];
  60. $panDomain = $domain2;
  61. }
  62. }
  63. }
  64. if(!empty($rule)) {
  65. // 子域名部署规则 '子域名'=>array('模块名[/控制器名]','var1=a&var2=b');
  66. if(is_array($rule)){
  67. list($rule,$vars) = $rule;
  68. }
  69. $array = explode('/',$rule);
  70. // 模块绑定
  71. define('BIND_MODULE',array_shift($array));
  72. // 控制器绑定
  73. if(!empty($array)) {
  74. $controller = array_shift($array);
  75. if($controller){
  76. define('BIND_CONTROLLER',$controller);
  77. }
  78. }
  79. if(isset($vars)) { // 传入参数
  80. parse_str($vars,$parms);
  81. if(isset($panDomain)){
  82. $pos = array_search('*', $parms);
  83. if(false !== $pos) {
  84. // 泛域名作为参数
  85. $parms[$pos] = $panDomain;
  86. }
  87. }
  88. $_GET = array_merge($_GET,$parms);
  89. }
  90. }
  91. }
  92. // 分析PATHINFO信息
  93. if(!isset($_SERVER['PATH_INFO'])) {
  94. $types = explode(',',C('URL_PATHINFO_FETCH'));
  95. foreach ($types as $type){
  96. if(!empty($_SERVER[$type])) {
  97. $_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type],$_SERVER['SCRIPT_NAME']))?
  98. substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
  99. break;
  100. }
  101. }
  102. }
  103. if(empty($_SERVER['PATH_INFO'])) {
  104. $_SERVER['PATH_INFO'] = '';
  105. }
  106. $depr = C('URL_PATHINFO_DEPR');
  107. define('MODULE_PATHINFO_DEPR', $depr);
  108. define('__INFO__',trim($_SERVER['PATH_INFO'],'/'));
  109. // URL后缀
  110. define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'],PATHINFO_EXTENSION)));
  111. $_SERVER['PATH_INFO'] = __INFO__;
  112. if (__INFO__ && C('MULTI_MODULE') && !defined('BIND_MODULE')){ // 获取模块名
  113. $paths = explode($depr,__INFO__,2);
  114. $allowList = C('MODULE_ALLOW_LIST'); // 允许的模块列表
  115. $module = preg_replace('/\.' . __EXT__ . '$/i', '',$paths[0]);
  116. if( empty($allowList) || (is_array($allowList) && in_array_case($module, $allowList))){
  117. $_GET[$varModule] = $module;
  118. $_SERVER['PATH_INFO'] = isset($paths[1])?$paths[1]:'';
  119. }
  120. }
  121. // 获取模块名称
  122. define('MODULE_NAME', defined('BIND_MODULE')? BIND_MODULE : self::getModule($varModule));
  123. // 检测模块是否存在
  124. if( MODULE_NAME && (defined('BIND_MODULE') || !in_array_case(MODULE_NAME,C('MODULE_DENY_LIST')) ) && is_dir(APP_PATH.MODULE_NAME)){
  125. // 定义当前模块路径
  126. define('MODULE_PATH', APP_PATH.MODULE_NAME.'/');
  127. // 定义当前模块的模版缓存路径
  128. C('CACHE_PATH',CACHE_PATH.MODULE_NAME.'/');
  129. // 加载模块配置文件
  130. if(is_file(MODULE_PATH.'Conf/config.php'))
  131. C(include MODULE_PATH.'Conf/config.php');
  132. // 加载模块别名定义
  133. if(is_file(MODULE_PATH.'Conf/alias.php'))
  134. Think::addMap(include MODULE_PATH.'Conf/alias.php');
  135. // 加载模块函数文件
  136. if(is_file(MODULE_PATH.'Common/function.php'))
  137. include MODULE_PATH.'Common/function.php';
  138. }else{
  139. E(L('_MODULE_NOT_EXIST_').':'.MODULE_NAME);
  140. }
  141. if('' != $_SERVER['PATH_INFO'] && (!C('URL_ROUTER_ON') || !Route::check()) ){ // 检测路由规则 如果没有则按默认规则调度URL
  142. // 检查禁止访问的URL后缀
  143. if(C('URL_DENY_SUFFIX') && preg_match('/\.('.trim(C('URL_DENY_SUFFIX'),'.').')$/i', $_SERVER['PATH_INFO'])){
  144. send_http_status(404);
  145. exit;
  146. }
  147. // 去除URL后缀
  148. $_SERVER['PATH_INFO'] = preg_replace(C('URL_HTML_SUFFIX')? '/\.('.trim(C('URL_HTML_SUFFIX'),'.').')$/i' : '/\.'.__EXT__.'$/i', '', $_SERVER['PATH_INFO']);
  149. $depr = C('URL_PATHINFO_DEPR');
  150. $paths = explode($depr,trim($_SERVER['PATH_INFO'],$depr));
  151. if(!defined('BIND_CONTROLLER')) {// 获取控制器
  152. $_GET[$varController] = array_shift($paths);
  153. }
  154. // 获取操作
  155. if(!defined('BIND_ACTION')){
  156. $_GET[$varAction] = array_shift($paths);
  157. }
  158. // 解析剩余的URL参数
  159. $var = array();
  160. if(C('URL_PARAMS_BIND') && 1 == C('URL_PARAMS_BIND_TYPE')){
  161. // URL参数按顺序绑定变量
  162. $var = $paths;
  163. }else{
  164. preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){$var[$match[1]]=strip_tags($match[2]);}, implode('/',$paths));
  165. }
  166. $_GET = array_merge($var,$_GET);
  167. }
  168. // 获取控制器和操作名
  169. define('CONTROLLER_NAME', defined('BIND_CONTROLLER')? BIND_CONTROLLER : self::getController($varController,$urlCase));
  170. define('ACTION_NAME', defined('BIND_ACTION')? BIND_ACTION : self::getAction($varAction,$urlCase));
  171. //保证$_REQUEST正常取值
  172. $_REQUEST = array_merge($_POST,$_GET);
  173. }
  174. /**
  175. * 获得实际的控制器名称
  176. */
  177. static private function getController($var,$urlCase) {
  178. $controller = (!empty($_GET[$var])? $_GET[$var]:C('DEFAULT_CONTROLLER'));
  179. unset($_GET[$var]);
  180. if($urlCase) {
  181. // URL地址不区分大小写
  182. // 智能识别方式 user_type 识别到 UserTypeController 控制器
  183. $controller = parse_name($controller,1);
  184. }
  185. return strip_tags(ucfirst($controller));
  186. }
  187. /**
  188. * 获得实际的操作名称
  189. */
  190. static private function getAction($var,$urlCase) {
  191. $action = !empty($_POST[$var]) ?
  192. $_POST[$var] :
  193. (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_ACTION'));
  194. unset($_POST[$var],$_GET[$var]);
  195. return strip_tags($urlCase?strtolower($action):$action);
  196. }
  197. /**
  198. * 获得实际的模块名称
  199. */
  200. static private function getModule($var) {
  201. $module = (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_MODULE'));
  202. unset($_GET[$var]);
  203. if($maps = C('URL_MODULE_MAP')) {
  204. if(isset($maps[strtolower($module)])) {
  205. // 记录当前别名
  206. define('MODULE_ALIAS',strtolower($module));
  207. // 获取实际的模块名
  208. return ucfirst($maps[MODULE_ALIAS]);
  209. }elseif(array_search(strtolower($module),$maps)){
  210. // 禁止访问原始模块
  211. return '';
  212. }
  213. }
  214. return strip_tags(ucfirst(strtolower($module)));
  215. }
  216. }