Dispatcher.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. /**
  10. * Senthot built Dispatcher class
  11. * Complete URL parsing , routing and scheduling
  12. * @category Sen
  13. * @package Sen
  14. * @subpackage Core
  15. * @author ms134n <[email protected]>
  16. */
  17. class Dispatcher {
  18. /**
  19. * URL mapping to the controller
  20. * @access public
  21. * @return void
  22. */
  23. static public function dispatch() {
  24. $urlMode = C('URL_MODEL');
  25. if(!empty($_GET[C('VAR_PATHINFO')])) { // Determine whether there is compatibility mode inside the URL parameters
  26. $_SERVER['PATH_INFO'] = $_GET[C('VAR_PATHINFO')];
  27. unset($_GET[C('VAR_PATHINFO')]);
  28. }
  29. if($urlMode == URL_COMPAT ){
  30. // Compatibility mode judgment
  31. define('PHP_FILE',_PHP_FILE_.'?'.C('VAR_PATHINFO').'=');
  32. }elseif($urlMode == URL_REWRITE ) {
  33. //Current projects address
  34. $url = dirname(_PHP_FILE_);
  35. if($url == '/' || $url == '\\')
  36. $url = '';
  37. define('PHP_FILE',$url);
  38. }else {
  39. //Current projects address
  40. define('PHP_FILE',_PHP_FILE_);
  41. }
  42. // On sub-domain deployment
  43. if(C('APP_SUB_DOMAIN_DEPLOY')) {
  44. $rules = C('APP_SUB_DOMAIN_RULES');
  45. $subDomain = strtolower(substr($_SERVER['HTTP_HOST'],0,strpos($_SERVER['HTTP_HOST'],'.')));
  46. define('SUB_DOMAIN',$subDomain); // Two domain defined
  47. if($subDomain && isset($rules[$subDomain])) {
  48. $rule = $rules[$subDomain];
  49. }elseif(isset($rules['*'])){ // Pan-domain name Support
  50. if('www' != $subDomain && !in_array($subDomain,C('APP_SUB_DOMAIN_DENY'))) {
  51. $rule = $rules['*'];
  52. }
  53. }
  54. if(!empty($rule)) {
  55. // Subdomain deployment rules 'Subdomain'=>array('Group name/[Module name]','var1=a&var2=b');
  56. $array = explode('/',$rule[0]);
  57. $module = array_pop($array);
  58. if(!empty($module)) {
  59. $_GET[C('VAR_MODULE')] = $module;
  60. $domainModule = true;
  61. }
  62. if(!empty($array)) {
  63. $_GET[C('VAR_GROUP')] = array_pop($array);
  64. $domainGroup = true;
  65. }
  66. if(isset($rule[1])) { // Incoming parameters
  67. parse_str($rule[1],$parms);
  68. $_GET = array_merge($_GET,$parms);
  69. }
  70. }
  71. }
  72. // Analysis PATHINFO information
  73. if(empty($_SERVER['PATH_INFO'])) {
  74. $types = explode(',',C('URL_PATHINFO_FETCH'));
  75. foreach ($types as $type){
  76. if(0===strpos($type,':')) {// Support function to determine
  77. $_SERVER['PATH_INFO'] = call_user_func(substr($type,1));
  78. break;
  79. }elseif(!empty($_SERVER[$type])) {
  80. $_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type],$_SERVER['SCRIPT_NAME']))?
  81. substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
  82. break;
  83. }
  84. }
  85. }
  86. $depr = C('URL_PATHINFO_DEPR');
  87. if(!empty($_SERVER['PATH_INFO'])) {
  88. tag('path_info');
  89. $part = pathinfo($_SERVER['PATH_INFO']);
  90. define('__EXT__', isset($part['extension'])?strtolower($part['extension']):'');
  91. if(C('URL_HTML_SUFFIX')) {
  92. $_SERVER['PATH_INFO'] = preg_replace('/\.('.trim(C('URL_HTML_SUFFIX'),'.').')$/i', '', $_SERVER['PATH_INFO']);
  93. }elseif(__EXT__) {
  94. $_SERVER['PATH_INFO'] = preg_replace('/.'.__EXT__.'$/i','',$_SERVER['PATH_INFO']);
  95. }
  96. if(!self::routerCheck()){ // Detect routing rules If you do not press the default rule scheduling URL
  97. $paths = explode($depr,trim($_SERVER['PATH_INFO'],'/'));
  98. if(C('VAR_URL_PARAMS')) {
  99. // Directly through $_GET['_URL_'][1] $_GET['_URL_'][2] Get URL parameters Convenient access without routing parameters
  100. $_GET[C('VAR_URL_PARAMS')] = $paths;
  101. }
  102. $var = array();
  103. if (C('APP_GROUP_LIST') && !isset($_GET[C('VAR_GROUP')])){
  104. $var[C('VAR_GROUP')] = in_array(strtolower($paths[0]),explode(',',strtolower(C('APP_GROUP_LIST'))))? array_shift($paths) : '';
  105. if(C('APP_GROUP_DENY') && in_array(strtolower($var[C('VAR_GROUP')]),explode(',',strtolower(C('APP_GROUP_DENY'))))) {
  106. // Prohibit direct access to packet
  107. exit;
  108. }
  109. }
  110. if(!isset($_GET[C('VAR_MODULE')])) {// Have not defined module name
  111. $var[C('VAR_MODULE')] = array_shift($paths);
  112. }
  113. $var[C('VAR_ACTION')] = array_shift($paths);
  114. // Parsing URL parameters remaining
  115. preg_replace('@(\w+)\/([^\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode('/',$paths));
  116. $_GET = array_merge($var,$_GET);
  117. }
  118. define('__INFO__',$_SERVER['PATH_INFO']);
  119. }
  120. // URL Constants
  121. define('__SELF__',strip_tags($_SERVER['REQUEST_URI']));
  122. // Current projects address
  123. define('__APP__',strip_tags(PHP_FILE));
  124. // Get Grouping Module and action names
  125. if (C('APP_GROUP_LIST')) {
  126. define('GROUP_NAME', self::getGroup(C('VAR_GROUP')));
  127. // URL address of the packet
  128. define('__GROUP__',(!empty($domainGroup) || strtolower(GROUP_NAME) == strtolower(C('DEFAULT_GROUP')) )?__APP__ : __APP__.'/'.GROUP_NAME);
  129. }
  130. // Define the project based on the load path
  131. define('BASE_LIB_PATH', (defined('GROUP_NAME') && C('APP_GROUP_MODE')==1) ? APP_PATH.C('APP_GROUP_PATH').'/'.GROUP_NAME.'/' : LIB_PATH);
  132. if(defined('GROUP_NAME')) {
  133. if(1 == C('APP_GROUP_MODE')){ // Independent Packet Mode
  134. $config_path = BASE_LIB_PATH.'Conf/';
  135. $common_path = BASE_LIB_PATH.'Common/';
  136. }else{ // General Packet Mode
  137. $config_path = CONF_PATH.GROUP_NAME.'/';
  138. $common_path = COMMON_PATH.GROUP_NAME.'/';
  139. }
  140. // Grouping configuration file to load
  141. if(is_file($config_path.'config.php'))
  142. C(include $config_path.'config.php');
  143. // Loading grouping alias definitions
  144. if(is_file($config_path.'alias.php'))
  145. alias_import(include $config_path.'alias.php');
  146. // Grouping function to load the file
  147. if(is_file($common_path.'function.php'))
  148. include $common_path.'function.php';
  149. }
  150. define('MODULE_NAME',self::getModule(C('VAR_MODULE')));
  151. define('ACTION_NAME',self::getAction(C('VAR_ACTION')));
  152. // Current module and packet address
  153. $moduleName = defined('MODULE_ALIAS')?MODULE_ALIAS:MODULE_NAME;
  154. if(defined('GROUP_NAME')) {
  155. define('__URL__',!empty($domainModule)?__GROUP__.$depr : __GROUP__.$depr.$moduleName);
  156. }else{
  157. define('__URL__',!empty($domainModule)?__APP__.'/' : __APP__.'/'.$moduleName);
  158. }
  159. // Address the current operation
  160. define('__ACTION__',__URL__.$depr.(defined('ACTION_ALIAS')?ACTION_ALIAS:ACTION_NAME));
  161. //Guarantee $_REQUEST Normal values
  162. $_REQUEST = array_merge($_POST,$_GET);
  163. }
  164. /**
  165. * Routing Detection
  166. * @access public
  167. * @return void
  168. */
  169. static public function routerCheck() {
  170. $return = false;
  171. // Routing detection tag
  172. tag('route_check',$return);
  173. return $return;
  174. }
  175. /**
  176. * Get the actual name of the module
  177. * @access private
  178. * @return string
  179. */
  180. static private function getModule($var) {
  181. $module = (!empty($_GET[$var])? $_GET[$var]:C('DEFAULT_MODULE'));
  182. unset($_GET[$var]);
  183. if($maps = C('URL_MODULE_MAP')) {
  184. if(isset($maps[strtolower($module)])) {
  185. // Record current alias
  186. define('MODULE_ALIAS',strtolower($module));
  187. // Get the actual name of the module
  188. return $maps[MODULE_ALIAS];
  189. }elseif(array_search(strtolower($module),$maps)){
  190. // Prohibit access to the original module
  191. return '';
  192. }
  193. }
  194. if(C('URL_CASE_INSENSITIVE')) {
  195. // URL address is not case sensitive
  196. // Intelligent recognition method index.php/user_type/index/ Identified UserTypeAction Module
  197. $module = ucfirst(parse_name($module,1));
  198. }
  199. return strip_tags($module);
  200. }
  201. /**
  202. * Get the actual name of the operation
  203. * @access private
  204. * @return string
  205. */
  206. static private function getAction($var) {
  207. $action = !empty($_POST[$var]) ?
  208. $_POST[$var] :
  209. (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_ACTION'));
  210. unset($_POST[$var],$_GET[$var]);
  211. if($maps = C('URL_ACTION_MAP')) {
  212. if(isset($maps[strtolower(MODULE_NAME)])) {
  213. $maps = $maps[strtolower(MODULE_NAME)];
  214. if(isset($maps[strtolower($action)])) {
  215. // Record current alias
  216. define('ACTION_ALIAS',strtolower($action));
  217. // Get the actual name of the operation
  218. return $maps[ACTION_ALIAS];
  219. }elseif(array_search(strtolower($action),$maps)){
  220. // Prohibit access to the original operating
  221. return '';
  222. }
  223. }
  224. }
  225. return strip_tags(C('URL_CASE_INSENSITIVE')?strtolower($action):$action);
  226. }
  227. /**
  228. * Get the actual group name
  229. * @access private
  230. * @return string
  231. */
  232. static private function getGroup($var) {
  233. $group = (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_GROUP'));
  234. unset($_GET[$var]);
  235. return strip_tags(C('URL_CASE_INSENSITIVE') ?ucfirst(strtolower($group)):$group);
  236. }
  237. }