PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/uri.php

https://gitlab.com/Ltaimao/wecenter
PHP | 319 lines | 247 code | 57 blank | 15 comment | 44 complexity | e0736043e4467ff58a3fb8e6668757ae MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | WeCenter [#RELEASE_VERSION#]
  5. | ========================================
  6. | by WeCenter Software
  7. | © 2011 - 2014 WeCenter. All Rights Reserved
  8. | http://www.wecenter.com
  9. | ========================================
  10. | Support: WeCenter@qq.com
  11. |
  12. +---------------------------------------------------------------------------
  13. */
  14. class core_uri
  15. {
  16. var $params = array(
  17. 'sep_value' => '-', // 赋值分隔符
  18. 'sep_var' => '__', // 变量分割符
  19. 'sep_act' => '/' // 动作分割符
  20. );
  21. // 默认控制器
  22. var $default_vars = array(
  23. 'app_dir' => 'explore',
  24. 'controller' => 'main',
  25. 'action' => 'index'
  26. );
  27. var $app_dir = '';
  28. var $controller = '';
  29. var $action = '';
  30. var $request_main = '';
  31. var $index_script = '';
  32. var $args_var_str = '';
  33. public function __construct()
  34. {
  35. if (!defined('G_INDEX_SCRIPT'))
  36. {
  37. return false;
  38. }
  39. if (G_INDEX_SCRIPT == '')
  40. {
  41. $this->index_script = '?/';
  42. }
  43. else
  44. {
  45. $this->index_script = G_INDEX_SCRIPT;
  46. }
  47. if ($_SERVER['REQUEST_URI'])
  48. {
  49. if (isset($_SERVER['HTTP_X_REWRITE_URL']))
  50. {
  51. $request_main = $_SERVER['HTTP_X_REWRITE_URL'];
  52. }
  53. else
  54. {
  55. $request_main = $_SERVER['REQUEST_URI'];
  56. }
  57. $requests = explode($this->index_script, $request_main);
  58. if (count($requests) == 1 AND dirname($_SERVER['SCRIPT_NAME']) != '/')
  59. {
  60. $request_main = preg_replace('/^' . preg_quote(dirname($_SERVER['SCRIPT_NAME']), '/') . '/i', '', $request_main);
  61. }
  62. else if (count($requests) == 2)
  63. {
  64. if ($requests[0] != '/')
  65. {
  66. $request_main = str_replace($requests[0], '', $request_main);
  67. }
  68. $request_main = str_replace($this->index_script, '', $request_main);
  69. }
  70. }
  71. else if ($_SERVER['QUERY_STRING'])
  72. {
  73. $request_main = $_SERVER['QUERY_STRING'];
  74. }
  75. $request_main = ltrim($request_main, "/\\");
  76. $base_script = basename($_SERVER['SCRIPT_NAME']);
  77. if (!strstr($request_main, '=') AND !strstr($request_main, '/') AND !strstr($request_main, '-') AND !strstr($request_main, '.'))
  78. {
  79. $request_main .= '/';
  80. }
  81. if (strstr($base_script, '.php'))
  82. {
  83. $request_main = str_replace($base_script . '/', '', $request_main);
  84. }
  85. if (count($requests) == 1)
  86. {
  87. $request_main = $this->_parse_uri($request_main);
  88. }
  89. $this->request_main = $request_main;
  90. }
  91. public function set_rewrite()
  92. {
  93. if (!defined('G_INDEX_SCRIPT'))
  94. {
  95. return false;
  96. }
  97. if (!$this->request_main OR $this->index_script == $this->request_main)
  98. {
  99. $this->controller = 'main';
  100. $this->action = 'index';
  101. return $this;
  102. }
  103. $request = explode('?', $this->request_main, 2);
  104. if (count($request) == 1)
  105. {
  106. $request = explode('&', $this->request_main, 2);
  107. }
  108. $uri = array(
  109. 'first' => array_shift($request),
  110. 'last' => ltrim(implode($request), '?')
  111. );
  112. if ($uri['last'])
  113. {
  114. parse_str($uri['last'], $query_string);
  115. foreach ($query_string AS $key => $val)
  116. {
  117. if (!$_GET[$key])
  118. {
  119. if (! strstr($val, '%'))
  120. {
  121. $_GET[$key] = $val;
  122. }
  123. else
  124. {
  125. $_GET[$key] = urldecode($val);
  126. }
  127. }
  128. }
  129. }
  130. $request = explode($this->params['sep_act'], $uri['first']);
  131. $uri['first'] = array(
  132. 'pattern' => '',
  133. 'args' => $request
  134. );
  135. $__app_dir = $this->default_vars['app_dir']; // 应用目录
  136. $this->controller = $this->default_vars['controller']; // 控制器
  137. $this->action = $this->default_vars['action']; // 动作
  138. $this->args_var_str = '';
  139. // 删除空值
  140. foreach ($uri['first']['args'] AS $key => $val)
  141. {
  142. if (strstr($val, $this->params['sep_value']) AND !$start_key)
  143. {
  144. $start_key = $key;
  145. }
  146. else if ($start_key)
  147. {
  148. $uri['first']['args'][$start_key] .= $this->params['sep_act'] . $val;
  149. unset($uri['first']['args'][$key]);
  150. }
  151. }
  152. $args_count = count($uri['first']['args']);
  153. switch ($args_count)
  154. {
  155. default:
  156. return $this;
  157. break;
  158. case 1:
  159. $this->args_var_str = end($uri['first']['args']);
  160. break;
  161. case 2:
  162. $this->args_var_str = end($uri['first']['args']);
  163. $__app_dir = $uri['first']['args'][0] ? $uri['first']['args'][0] : $this->default_vars['app_dir']; // 应用目录
  164. break;
  165. case 3:
  166. $this->args_var_str = end($uri['first']['args']);
  167. $__app_dir = $uri['first']['args'][0] ? $uri['first']['args'][0] : $this->default_vars['app_dir']; // 应用目录
  168. if (file_exists(ROOT_PATH . 'app/' . $__app_dir . '/' . $uri['first']['args'][1] . '.php'))
  169. {
  170. $this->controller = $uri['first']['args'][1]; // 控制器
  171. }
  172. else
  173. {
  174. $this->controller = $this->default_vars['controller']; // 控制器
  175. $this->action = $uri['first']['args'][1]; // 动作
  176. }
  177. break;
  178. case 4:
  179. $this->args_var_str = end($uri['first']['args']);
  180. $__app_dir = $uri['first']['args'][0] ? $uri['first']['args'][0] : $this->default_vars['app_dir']; // 应用目录
  181. $this->controller = $uri['first']['args'][1] ? $uri['first']['args'][1] : $this->default_vars['controller']; // 控制器
  182. $this->action = $uri['first']['args'][2] ? $uri['first']['args'][2] : $this->default_vars['action']; // 动作
  183. break;
  184. case 5:
  185. $this->args_var_str = end($uri['first']['args']);
  186. $__app_dir = $uri['first']['args'][0] ? $uri['first']['args'][0] : $this->default_vars['app_dir']; // 应用目录
  187. $this->controller = $uri['first']['args'][2] ? $uri['first']['args'][1] . '/' . $uri['first']['args'][2] : $this->default_vars['controller']; // 控制器
  188. $this->action = $uri['first']['args'][3] ? $uri['first']['args'][3] : $this->default_vars['action']; // 动作
  189. break;
  190. }
  191. $this->app_dir = ROOT_PATH . 'app/' . $__app_dir . '/';
  192. $_GET['c'] = $this->controller;
  193. $_GET['act'] = $this->action;
  194. $_GET['app'] = $__app_dir;
  195. return $this;
  196. }
  197. public function parse_args()
  198. {
  199. if ($args_var_str = $this->args_var_str)
  200. {
  201. if (defined('WECENTER_URI_SET_LAST_STRING_TO_ID'))
  202. {
  203. $_GET['id'] = urldecode($args_var_str);
  204. }
  205. else
  206. {
  207. // 兼容 __param-test 写法
  208. if (substr($args_var_str, 0, strlen($this->params['sep_var'])) == $this->params['sep_var'])
  209. {
  210. $args_var_str = substr($args_var_str, strlen($this->params['sep_var']));
  211. }
  212. if (!strstr($args_var_str, '-'))
  213. {
  214. $_GET['id'] = urldecode($args_var_str);
  215. }
  216. $uri['last'] = explode($this->params['sep_var'], $args_var_str);
  217. foreach ($uri['last'] as $val)
  218. {
  219. @list($k, $v) = explode($this->params['sep_value'], $val, 2);
  220. if ($k)
  221. {
  222. if (! strstr($v, '%'))
  223. {
  224. $_GET[$k] = $v;
  225. }
  226. else
  227. {
  228. $_GET[$k] = urldecode($v);
  229. }
  230. }
  231. }
  232. }
  233. }
  234. foreach ($_GET AS $key => $val)
  235. {
  236. if (strstr($key, '/'))
  237. {
  238. unset($_GET[$key]);
  239. }
  240. }
  241. return $this;
  242. }
  243. private function _parse_uri($request_main)
  244. {
  245. if (get_setting('url_rewrite_enable') == 'Y' AND $request_routes = get_request_route(false))
  246. {
  247. if (!$request_main)
  248. {
  249. $request_main = '/';
  250. }
  251. foreach($request_routes as $key => $val)
  252. {
  253. if (preg_match('/^' . $val[0] . '/', $request_main))
  254. {
  255. $request_main = preg_replace('/^' . $val[0] . '/', $val[1], $request_main);
  256. return $request_main;
  257. }
  258. }
  259. }
  260. return $request_main;
  261. }
  262. }