3ab2e5becc4bbde23bc586169a47ae874365123f.svn-base 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php namespace Maze\Routing;
  2. use Maze\Config\Load as Config;
  3. use Maze\Http\Input;
  4. class Uri
  5. {
  6. /**
  7. * explode
  8. *
  9. * @var string
  10. */
  11. const explode = '/';
  12. /**
  13. * uri value
  14. *
  15. * @var string
  16. */
  17. static public $value;
  18. /**
  19. * method
  20. *
  21. * @var string
  22. */
  23. static public $method;
  24. /**
  25. * uri type
  26. *
  27. * @var string
  28. */
  29. static public $type = '';
  30. /**
  31. * url
  32. *
  33. * @var string
  34. */
  35. static public $url;
  36. /**
  37. * 获取uri
  38. *
  39. * @return string
  40. */
  41. static public function get()
  42. {
  43. if(self::$value) return self::$value;
  44. if(isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'])
  45. {
  46. self::$type = '';
  47. self::$value = trim($_SERVER['PATH_INFO'], self::explode);
  48. self::$url = preg_replace('/^\//i', '', $_SERVER['REQUEST_URI']);
  49. }
  50. elseif(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] && $_SERVER['REQUEST_URI'] != '/')
  51. {
  52. self::$type = '?';
  53. $script = substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], 'index.php'));
  54. if(strpos($_SERVER['REQUEST_URI'], 'index.php') !== false)
  55. {
  56. self::$value = str_replace($_SERVER['SCRIPT_NAME'] . '?', '', $_SERVER['REQUEST_URI']);
  57. }
  58. elseif($script != $_SERVER['REQUEST_URI'])
  59. {
  60. self::$value = str_replace($script . '?', '', $_SERVER['REQUEST_URI']);
  61. }
  62. self::$url = self::$value;
  63. }
  64. self::$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
  65. Input::init();
  66. if(!self::$url && Input::get('maze_shell') == 1 && Input::get('send'))
  67. {
  68. self::$url = self::$value = str_replace(array('__', '^'), array('?', '&'), Input::get('send'));
  69. }
  70. self::match();
  71. empty(self::$value) && self::$value = 'home';
  72. return self::$value;
  73. }
  74. /**
  75. * 通过正则解析uri
  76. *
  77. * @return string
  78. */
  79. static public function match()
  80. {
  81. Config::get('route');
  82. self::input();
  83. if(Config::$global['route'] && self::$value)
  84. {
  85. if(isset(Config::$global['route'][self::$value]))
  86. {
  87. self::$value = Config::$global['route'][self::$value];
  88. }
  89. else
  90. {
  91. foreach(Config::$global['route'] as $k => $v)
  92. {
  93. $k = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $k));
  94. if(preg_match('#^'.$k.'$#', self::$value))
  95. {
  96. if(strpos($v, '$') !== false AND strpos($k, '(') !== false)
  97. {
  98. $v = preg_replace('#^'.$k.'$#', $v, self::$value);
  99. }
  100. self::$value = $v;
  101. self::$url = self::$value;
  102. }
  103. }
  104. self::input();
  105. }
  106. }
  107. }
  108. /**
  109. * 将uri解析?
  110. *
  111. * @return string
  112. */
  113. static private function input()
  114. {
  115. if(strpos(self::$value, '?') !== false)
  116. {
  117. $temp = explode('?', self::$value);
  118. self::$value = $temp[0];
  119. parse_str($temp[1], $input);
  120. Input::set('all', $input);
  121. }
  122. }
  123. /**
  124. * 将uri解析成file
  125. *
  126. * @return string
  127. */
  128. static public function file()
  129. {
  130. if(self::$method == 'GET' && !empty($_SERVER['REQUEST_URI']))
  131. {
  132. $request_uri = strtoupper(urldecode($_SERVER['REQUEST_URI']));
  133. if(strpos($request_uri, '<') !== false || strpos($request_uri, '"') !== false || strpos($request_uri, 'CONTENT-TRANSFER-ENCODING') !== false)
  134. {
  135. error(\Lang::get('request_tainting'));
  136. }
  137. unset($request_uri);
  138. }
  139. if(strpos(self::$value, self::explode) !== false)
  140. {
  141. $array = explode(self::explode, self::$value);
  142. if(isset($array[2]) && empty($array[3]))
  143. {
  144. $file = $array[0] . self::explode . $array[1] . self::explode . $array[2];
  145. unset($array[0]);
  146. unset($array[1]);
  147. unset($array[2]);
  148. }
  149. elseif(isset($array[1]))
  150. {
  151. $file = $array[0] . self::explode . $array[1];
  152. unset($array[0]);
  153. unset($array[1]);
  154. }
  155. elseif(isset($array[0]))
  156. {
  157. $file = $array[0];
  158. unset($array[0]);
  159. }
  160. $param = array_values($array);
  161. }
  162. else
  163. {
  164. $file = self::$value;
  165. }
  166. return $file;
  167. }
  168. }