CheckActionRouteBehavior.class.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 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 Behavior;
  12. /**
  13. * 系统行为扩展:操作路由检测
  14. */
  15. class CheckActionRouteBehavior
  16. {
  17. // 行为扩展的执行入口必须是run
  18. public function run(&$config)
  19. {
  20. // 优先检测是否存在PATH_INFO
  21. $regx = trim($_SERVER['PATH_INFO'], '/');
  22. if (empty($regx)) {
  23. return;
  24. }
  25. // 路由定义文件优先于config中的配置定义
  26. // 路由处理
  27. $routes = $config['routes'];
  28. if (!empty($routes)) {
  29. $depr = C('URL_PATHINFO_DEPR');
  30. // 分隔符替换 确保路由定义使用统一的分隔符
  31. $regx = str_replace($depr, '/', $regx);
  32. $regx = substr_replace($regx, '', 0, strlen(__URL__));
  33. foreach ($routes as $rule => $route) {
  34. if (0 === strpos($rule, '/') && preg_match($rule, $regx, $matches)) {
  35. // 正则路由
  36. return C('ACTION_NAME', $this->parseRegex($matches, $route, $regx));
  37. } else {
  38. // 规则路由
  39. $len1 = substr_count($regx, '/');
  40. $len2 = substr_count($rule, '/');
  41. if ($len1 >= $len2) {
  42. if ('$' == substr($rule, -1, 1)) {
  43. // 完整匹配
  44. if ($len1 != $len2) {
  45. continue;
  46. } else {
  47. $rule = substr($rule, 0, -1);
  48. }
  49. }
  50. $match = $this->checkUrlMatch($regx, $rule);
  51. if ($match) {
  52. return C('ACTION_NAME', $this->parseRule($rule, $route, $regx));
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. // 检测URL和规则路由是否匹配
  60. private function checkUrlMatch($regx, $rule)
  61. {
  62. $m1 = explode('/', $regx);
  63. $m2 = explode('/', $rule);
  64. $match = true; // 是否匹配
  65. foreach ($m2 as $key => $val) {
  66. if (':' == substr($val, 0, 1)) {
  67. // 动态变量
  68. if (strpos($val, '\\')) {
  69. $type = substr($val, -1);
  70. if ('d' == $type && !is_numeric($m1[$key])) {
  71. $match = false;
  72. break;
  73. }
  74. } elseif (strpos($val, '^')) {
  75. $array = explode('|', substr(strstr($val, '^'), 1));
  76. if (in_array($m1[$key], $array)) {
  77. $match = false;
  78. break;
  79. }
  80. }
  81. } elseif (0 !== strcasecmp($val, $m1[$key])) {
  82. $match = false;
  83. break;
  84. }
  85. }
  86. return $match;
  87. }
  88. // 解析规范的路由地址
  89. // 地址格式 操作?参数1=值1&参数2=值2...
  90. private function parseUrl($url)
  91. {
  92. $var = array();
  93. if (false !== strpos($url, '?')) {
  94. // 操作?参数1=值1&参数2=值2...
  95. $info = parse_url($url);
  96. $path = $info['path'];
  97. parse_str($info['query'], $var);
  98. } else {
  99. // 操作
  100. $path = $url;
  101. }
  102. $var[C('VAR_ACTION')] = $path;
  103. return $var;
  104. }
  105. // 解析规则路由
  106. // '路由规则'=>'操作?额外参数1=值1&额外参数2=值2...'
  107. // '路由规则'=>array('操作','额外参数1=值1&额外参数2=值2...')
  108. // '路由规则'=>'外部地址'
  109. // '路由规则'=>array('外部地址','重定向代码')
  110. // 路由规则中 :开头 表示动态变量
  111. // 外部地址中可以用动态变量 采用 :1 :2 的方式
  112. // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
  113. // 'new/:id'=>array('/new.php?id=:1',301), 重定向
  114. private function parseRule($rule, $route, $regx)
  115. {
  116. // 获取路由地址规则
  117. $url = is_array($route) ? $route[0] : $route;
  118. // 获取URL地址中的参数
  119. $paths = explode('/', $regx);
  120. // 解析路由规则
  121. $matches = array();
  122. $rule = explode('/', $rule);
  123. foreach ($rule as $item) {
  124. if (0 === strpos($item, ':')) {
  125. // 动态变量获取
  126. if ($pos = strpos($item, '^')) {
  127. $var = substr($item, 1, $pos - 1);
  128. } elseif (strpos($item, '\\')) {
  129. $var = substr($item, 1, -2);
  130. } else {
  131. $var = substr($item, 1);
  132. }
  133. $matches[$var] = array_shift($paths);
  134. } else {
  135. // 过滤URL中的静态变量
  136. array_shift($paths);
  137. }
  138. }
  139. if (0 === strpos($url, '/') || 0 === strpos($url, 'http')) {
  140. // 路由重定向跳转
  141. if (strpos($url, ':')) { // 传递动态参数
  142. $values = array_values($matches);
  143. $url = preg_replace('/:(\d+)/e', '$values[\\1-1]', $url);
  144. }
  145. header("Location: $url", true, (is_array($route) && isset($route[1])) ? $route[1] : 301);
  146. exit;
  147. } else {
  148. // 解析路由地址
  149. $var = $this->parseUrl($url);
  150. // 解析路由地址里面的动态参数
  151. $values = array_values($matches);
  152. foreach ($var as $key => $val) {
  153. if (0 === strpos($val, ':')) {
  154. $var[$key] = $values[substr($val, 1) - 1];
  155. }
  156. }
  157. $var = array_merge($matches, $var);
  158. // 解析剩余的URL参数
  159. if ($paths) {
  160. preg_replace('@(\w+)\/([^\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/', $paths));
  161. }
  162. // 解析路由自动传入参数
  163. if (is_array($route) && isset($route[1])) {
  164. parse_str($route[1], $params);
  165. $var = array_merge($var, $params);
  166. }
  167. $action = $var[C('VAR_ACTION')];
  168. unset($var[C('VAR_ACTION')]);
  169. $_GET = array_merge($var, $_GET);
  170. return $action;
  171. }
  172. }
  173. // 解析正则路由
  174. // '路由正则'=>'[分组/模块/操作]?参数1=值1&参数2=值2...'
  175. // '路由正则'=>array('[分组/模块/操作]?参数1=值1&参数2=值2...','额外参数1=值1&额外参数2=值2...')
  176. // '路由正则'=>'外部地址'
  177. // '路由正则'=>array('外部地址','重定向代码')
  178. // 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式
  179. // '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'),
  180. // '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向
  181. private function parseRegex($matches, $route, $regx)
  182. {
  183. // 获取路由地址规则
  184. $url = is_array($route) ? $route[0] : $route;
  185. $url = preg_replace('/:(\d+)/e', '$matches[\\1]', $url);
  186. if (0 === strpos($url, '/') || 0 === strpos($url, 'http')) {
  187. // 路由重定向跳转
  188. header("Location: $url", true, (is_array($route) && isset($route[1])) ? $route[1] : 301);
  189. exit;
  190. } else {
  191. // 解析路由地址
  192. $var = $this->parseUrl($url);
  193. // 解析剩余的URL参数
  194. $regx = substr_replace($regx, '', 0, strlen($matches[0]));
  195. if ($regx) {
  196. preg_replace('@(\w+)\/([^,\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', $regx);
  197. }
  198. // 解析路由自动传入参数
  199. if (is_array($route) && isset($route[1])) {
  200. parse_str($route[1], $params);
  201. $var = array_merge($var, $params);
  202. }
  203. $action = $var[C('VAR_ACTION')];
  204. unset($var[C('VAR_ACTION')]);
  205. $_GET = array_merge($var, $_GET);
  206. }
  207. return $action;
  208. }
  209. }