CheckRouteBehavior.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. defined('SEN_PATH') or exit();
  10. /**
  11. * System behavior extension : Routing Detection
  12. * @category Sen
  13. * @package Sen
  14. * @subpackage Behavior
  15. * @author ms134n <[email protected]>
  16. */
  17. class CheckRouteBehavior extends Behavior {
  18. // Behavioral parameters defined ( the default value ) Covered in the project configuration
  19. protected $options = array(
  20. 'URL_ROUTER_ON' => false, // Whether to open the URL routing
  21. 'URL_ROUTE_RULES' => array(), // The default routing rules, Note : Packet configuration can not be replaced
  22. );
  23. // Behavior extension execution entry must be run
  24. public function run(&$return){
  25. // Priority detect the presence of PATH_INFO
  26. $regx = trim($_SERVER['PATH_INFO'],'/');
  27. if(empty($regx)) return $return = true;
  28. // Whether to open the route to use
  29. if(!C('URL_ROUTER_ON')) return $return = false;
  30. // Routing config definition files take precedence over the configuration definition
  31. $routes = C('URL_ROUTE_RULES');
  32. // Route processing
  33. if(!empty($routes)) {
  34. $depr = C('URL_PATHINFO_DEPR');
  35. // Replace separator Ensure that routing defined delimiters using uniform
  36. $regx = str_replace($depr,'/',$regx);
  37. foreach ($routes as $rule=>$route){
  38. if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // Regular routes
  39. return $return = $this->parseRegex($matches,$route,$regx);
  40. }else{ // Routing rules
  41. $len1 = substr_count($regx,'/');
  42. $len2 = substr_count($rule,'/');
  43. if($len1>=$len2) {
  44. if('$' == substr($rule,-1,1)) {// Exact hits
  45. if($len1 != $len2) {
  46. continue;
  47. }else{
  48. $rule = substr($rule,0,-1);
  49. }
  50. }
  51. $match = $this->checkUrlMatch($regx,$rule);
  52. if($match) return $return = $this->parseRule($rule,$route,$regx);
  53. }
  54. }
  55. }
  56. }
  57. $return = false;
  58. }
  59. // Detecting URL and rules route matches
  60. private function checkUrlMatch($regx,$rule) {
  61. $m1 = explode('/',$regx);
  62. $m2 = explode('/',$rule);
  63. $match = true; // Match
  64. foreach ($m2 as $key=>$val){
  65. if(':' == substr($val,0,1)) {// Dynamic variables
  66. if(strpos($val,'\\')) {
  67. $type = substr($val,-1);
  68. if('d'==$type && !is_numeric($m1[$key])) {
  69. $match = false;
  70. break;
  71. }
  72. }elseif(strpos($val,'^')){
  73. $array = explode('|',substr(strstr($val,'^'),1));
  74. if(in_array($m1[$key],$array)) {
  75. $match = false;
  76. break;
  77. }
  78. }
  79. }elseif(0 !== strcasecmp($val,$m1[$key])){
  80. $match = false;
  81. break;
  82. }
  83. }
  84. return $match;
  85. }
  86. // Analytical standard routing address
  87. // Address Format [Grouping/Module/Operating?]Parameter1=Value1&Parameter2=Value2...
  88. private function parseUrl($url) {
  89. $var = array();
  90. if(false !== strpos($url,'?')) { // [Grouping/Module/Operating?]Parameter1=Value1&Parameter2=Value2...
  91. $info = parse_url($url);
  92. $path = explode('/',$info['path']);
  93. parse_str($info['query'],$var);
  94. }elseif(strpos($url,'/')){ // [Grouping/Module/Operating]
  95. $path = explode('/',$url);
  96. }else{ // Parameter1=Value1&Parameter2=Value2...
  97. parse_str($url,$var);
  98. }
  99. if(isset($path)) {
  100. $var[C('VAR_ACTION')] = array_pop($path);
  101. if(!empty($path)) {
  102. $var[C('VAR_MODULE')] = array_pop($path);
  103. }
  104. if(!empty($path)) {
  105. $var[C('VAR_GROUP')] = array_pop($path);
  106. }
  107. }
  108. return $var;
  109. }
  110. // Routing parsing rules
  111. // 'Routing rules'=>'[Grouping/Module/Operating]?Additional parameters 1=Value1&Additional parameter 2=Value2...'
  112. // 'Routing rules'=>array('[Grouping/Module/Operating]','Additional parameters 1=Value1&Additional parameter 2=Value2...')
  113. // 'Routing rules'=>'External Address'
  114. // 'Routing rules'=>array('External Address','Redirect code')
  115. // Routing rule :Beginning Represents a dynamic variable
  116. // External address can be used in a dynamic variable Adoption :1 :2 Way
  117. // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
  118. // 'new/:id'=>array('/new.php?id=:1',301), Redirect
  119. private function parseRule($rule,$route,$regx) {
  120. // Rules for routing address
  121. $url = is_array($route)?$route[0]:$route;
  122. // Get the URL address of the parameter
  123. $paths = explode('/',$regx);
  124. // Resolution routing rules
  125. $matches = array();
  126. $rule = explode('/',$rule);
  127. foreach ($rule as $item){
  128. if(0===strpos($item,':')) { // Dynamic variables for
  129. if($pos = strpos($item,'^') ) {
  130. $var = substr($item,1,$pos-1);
  131. }elseif(strpos($item,'\\')){
  132. $var = substr($item,1,-2);
  133. }else{
  134. $var = substr($item,1);
  135. }
  136. $matches[$var] = array_shift($paths);
  137. }else{ // Static variables in the URL filtering
  138. array_shift($paths);
  139. }
  140. }
  141. if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // Jump Route Redirection
  142. if(strpos($url,':')) { // Passing dynamic parameters
  143. $values = array_values($matches);
  144. $url = preg_replace('/:(\d+)/e','$values[\\1-1]',$url);
  145. }
  146. header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
  147. exit;
  148. }else{
  149. // Resolve routing address
  150. $var = $this->parseUrl($url);
  151. // Resolve routing address inside the dynamic parameters
  152. $values = array_values($matches);
  153. foreach ($var as $key=>$val){
  154. if(0===strpos($val,':')) {
  155. $var[$key] = $values[substr($val,1)-1];
  156. }
  157. }
  158. $var = array_merge($matches,$var);
  159. // Parsing URL parameters remaining
  160. if($paths) {
  161. preg_replace('@(\w+)\/([^\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/',$paths));
  162. }
  163. // Automatic transmission parameter parsing routing
  164. if(is_array($route) && isset($route[1])) {
  165. parse_str($route[1],$params);
  166. $var = array_merge($var,$params);
  167. }
  168. $_GET = array_merge($var,$_GET);
  169. }
  170. return true;
  171. }
  172. // Parsing regular route
  173. // 'Regular routes'=>'[Grouping/Module/Operating]?Parameter1=Value1&Parameter2=Value2...'
  174. // 'Regular routes'=>array('[Grouping/Module/Operating]?Parameter1=Value1&Parameter2=Value2...','Additional parameters 1=Value1&Additional parameter 2=Value2...')
  175. // 'Regular routes'=>'External Address'
  176. // 'Regular routes'=>array('External Address','Redirect code')
  177. // Parameter value and the external address can be used in a dynamic variable Adoption :1 :2 Way
  178. // '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'),
  179. // '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), Redirect
  180. private function parseRegex($matches,$route,$regx) {
  181. // Rules for routing address
  182. $url = is_array($route)?$route[0]:$route;
  183. $url = preg_replace('/:(\d+)/e','$matches[\\1]',$url);
  184. if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // Jump Route Redirection
  185. header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
  186. exit;
  187. }else{
  188. // Resolve routing address
  189. $var = $this->parseUrl($url);
  190. // Parsing URL parameters remaining
  191. $regx = substr_replace($regx,'',0,strlen($matches[0]));
  192. if($regx) {
  193. preg_replace('@(\w+)\/([^,\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', $regx);
  194. }
  195. // Automatic transmission parameter parsing routing
  196. if(is_array($route) && isset($route[1])) {
  197. parse_str($route[1],$params);
  198. $var = array_merge($var,$params);
  199. }
  200. $_GET = array_merge($var,$_GET);
  201. }
  202. return true;
  203. }
  204. }