PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/ThinkPHP/Lib/Behavior/CheckRouteBehavior.class.php

http://thinkphp.googlecode.com/
PHP | 211 lines | 168 code | 4 blank | 39 comment | 29 complexity | dddb0249d6d8bf89329c1104c3c7326b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  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. // $Id: CheckRouteBehavior.class.php 2929 2012-05-02 06:45:47Z liu21st@gmail.com $
  12. !defined('THINK_PATH') && exit();
  13. /**
  14. +------------------------------------------------------------------------------
  15. * ?????? ????
  16. +------------------------------------------------------------------------------
  17. */
  18. class CheckRouteBehavior extends Behavior {
  19. // ??????????? ?????????
  20. protected $options = array(
  21. 'URL_ROUTER_ON' => false, // ????URL??
  22. 'URL_ROUTE_RULES' => array(), // ?????????????????
  23. );
  24. // ????????????run
  25. public function run(&$return){
  26. // ????????PATH_INFO
  27. $regx = trim($_SERVER['PATH_INFO'],'/');
  28. if(empty($regx)) return $return = true;
  29. // ????????
  30. if(!C('URL_ROUTER_ON')) return $return = false;
  31. // ?????????config??????
  32. $routes = C('URL_ROUTE_RULES');
  33. // ????
  34. if(!empty($routes)) {
  35. $depr = C('URL_PATHINFO_DEPR');
  36. // ????? ??????????????
  37. $regx = str_replace($depr,'/',$regx);
  38. foreach ($routes as $rule=>$route){
  39. if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // ????
  40. return $return = $this->parseRegex($matches,$route,$regx);
  41. }else{ // ????
  42. $len1= substr_count($regx,'/');
  43. $len2 = substr_count($rule,'/');
  44. if($len1>=$len2) {
  45. if('$' == substr($rule,-1,1)) {// ????
  46. if($len1 != $len2) {
  47. continue;
  48. }else{
  49. $rule = substr($rule,0,-1);
  50. }
  51. }
  52. $match = $this->checkUrlMatch($regx,$rule);
  53. if($match) return $return = $this->parseRule($rule,$route,$regx);
  54. }
  55. }
  56. }
  57. }
  58. $return = false;
  59. }
  60. // ??URL?????????
  61. private function checkUrlMatch($regx,$rule) {
  62. $m1 = explode('/',$regx);
  63. $m2 = explode('/',$rule);
  64. $match = true; // ????
  65. foreach ($m2 as $key=>$val){
  66. if(':' == substr($val,0,1)) {// ????
  67. if(strpos($val,'\\')) {
  68. $type = substr($val,-1);
  69. if('d'==$type && !is_numeric($m1[$key])) {
  70. $match = false;
  71. break;
  72. }
  73. }elseif(strpos($val,'^')){
  74. $array = explode('|',substr(strstr($val,'^'),1));
  75. if(in_array($m1[$key],$array)) {
  76. $match = false;
  77. break;
  78. }
  79. }
  80. }elseif(0 !== strcasecmp($val,$m1[$key])){
  81. $match = false;
  82. break;
  83. }
  84. }
  85. return $match;
  86. }
  87. // ?????????
  88. // ???? [??/??/???]??1=?1&??2=?2...
  89. private function parseUrl($url) {
  90. $var = array();
  91. if(false !== strpos($url,'?')) { // [??/??/???]??1=?1&??2=?2...
  92. $info = parse_url($url);
  93. $path = explode('/',$info['path']);
  94. parse_str($info['query'],$var);
  95. }elseif(strpos($url,'/')){ // [??/??/??]
  96. $path = explode('/',$url);
  97. }else{ // ??1=?1&??2=?2...
  98. parse_str($url,$var);
  99. }
  100. if(isset($path)) {
  101. $var[C('VAR_ACTION')] = array_pop($path);
  102. if(!empty($path)) {
  103. $var[C('VAR_MODULE')] = array_pop($path);
  104. }
  105. if(!empty($path)) {
  106. $var[C('VAR_GROUP')] = array_pop($path);
  107. }
  108. }
  109. return $var;
  110. }
  111. // ??????
  112. // '????'=>'[??/??/??]?????1=?1&????2=?2...'
  113. // '????'=>array('[??/??/??]','????1=?1&????2=?2...')
  114. // '????'=>'????'
  115. // '????'=>array('????','?????')
  116. // ????? :?? ??????
  117. // ???????????? ?? :1 :2 ???
  118. // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
  119. // 'new/:id'=>array('/new.php?id=:1',301), ???
  120. private function parseRule($rule,$route,$regx) {
  121. // ????????
  122. $url = is_array($route)?$route[0]:$route;
  123. // ??URL??????
  124. $paths = explode('/',$regx);
  125. // ??????
  126. $matches = array();
  127. $rule = explode('/',$rule);
  128. foreach ($rule as $item){
  129. if(0===strpos($item,':')) { // ??????
  130. if($pos = strpos($item,'^') ) {
  131. $var = substr($item,1,$pos-1);
  132. }elseif(strpos($item,'\\')){
  133. $var = substr($item,1,-2);
  134. }else{
  135. $var = substr($item,1);
  136. }
  137. $matches[$var] = array_shift($paths);
  138. }else{ // ??URL??????
  139. array_shift($paths);
  140. }
  141. }
  142. if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // ???????
  143. if(strpos($url,':')) { // ??????
  144. $values = array_values($matches);
  145. $url = preg_replace('/:(\d+)/e','$values[\\1-1]',$url);
  146. }
  147. header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
  148. exit;
  149. }else{
  150. // ??????
  151. $var = $this->parseUrl($url);
  152. // ?????????????
  153. $values = array_values($matches);
  154. foreach ($var as $key=>$val){
  155. if(0===strpos($val,':')) {
  156. $var[$key] = $values[substr($val,1)-1];
  157. }
  158. }
  159. $var = array_merge($matches,$var);
  160. // ?????URL??
  161. if($paths) {
  162. preg_replace('@(\w+)\/([^,\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/',$paths));
  163. }
  164. // ??????????
  165. if(is_array($route) && isset($route[1])) {
  166. parse_str($route[1],$params);
  167. $var = array_merge($var,$params);
  168. }
  169. $_GET = array_merge($var,$_GET);
  170. }
  171. return true;
  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. $url = is_array($route)?$route[0]:$route;
  184. $url = preg_replace('/:(\d+)/e','$matches[\\1]',$url);
  185. if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // ???????
  186. header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
  187. exit;
  188. }else{
  189. // ??????
  190. $var = $this->parseUrl($url);
  191. // ?????URL??
  192. $regx = substr_replace($regx,'',0,strlen($matches[0]));
  193. if($regx) {
  194. preg_replace('@(\w+)\/([^,\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', $regx);
  195. }
  196. // ??????????
  197. if(is_array($route) && isset($route[1])) {
  198. parse_str($route[1],$params);
  199. $var = array_merge($var,$params);
  200. }
  201. $_GET = array_merge($var,$_GET);
  202. }
  203. return true;
  204. }
  205. }