123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php namespace Maze\Routing;
- use Maze\Config\Load as Config;
- use Maze\Http\Input;
- class Uri
- {
- /**
- * explode
- *
- * @var string
- */
- const explode = '/';
-
- /**
- * uri value
- *
- * @var string
- */
- static public $value;
- /**
- * method
- *
- * @var string
- */
- static public $method;
-
- /**
- * uri type
- *
- * @var string
- */
- static public $type = '';
-
- /**
- * url
- *
- * @var string
- */
- static public $url;
- /**
- * 获取uri
- *
- * @return string
- */
- static public function get()
- {
- if(self::$value) return self::$value;
-
- if(isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'])
- {
- self::$type = '';
- self::$value = trim($_SERVER['PATH_INFO'], self::explode);
- self::$url = preg_replace('/^\//i', '', $_SERVER['REQUEST_URI']);
- }
- elseif(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] && $_SERVER['REQUEST_URI'] != '/')
- {
- self::$type = '?';
- $script = substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], 'index.php'));
- if(strpos($_SERVER['REQUEST_URI'], 'index.php') !== false)
- {
- self::$value = str_replace($_SERVER['SCRIPT_NAME'] . '?', '', $_SERVER['REQUEST_URI']);
- }
- elseif($script != $_SERVER['REQUEST_URI'])
- {
- self::$value = str_replace($script . '?', '', $_SERVER['REQUEST_URI']);
- }
- self::$url = self::$value;
- }
- self::$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
- Input::init();
- if(!self::$url && Input::get('maze_shell') == 1 && Input::get('send'))
- {
- self::$url = self::$value = str_replace(array('__', '^'), array('?', '&'), Input::get('send'));
- }
- self::match();
- empty(self::$value) && self::$value = 'home';
-
- return self::$value;
- }
-
- /**
- * 通过正则解析uri
- *
- * @return string
- */
- static public function match()
- {
- Config::get('route');
- self::input();
- if(Config::$global['route'] && self::$value)
- {
- if(isset(Config::$global['route'][self::$value]))
- {
- self::$value = Config::$global['route'][self::$value];
- }
- else
- {
- foreach(Config::$global['route'] as $k => $v)
- {
- $k = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $k));
- if(preg_match('#^'.$k.'$#', self::$value))
- {
- if(strpos($v, '$') !== false && strpos($k, '(') !== false)
- {
- $v = preg_replace('#^'.$k.'$#', $v, self::$value);
- }
- self::$value = $v;
- self::$url = self::$value;
- }
- }
- self::input();
- }
- }
- }
- /**
- * 将uri解析?
- *
- * @return string
- */
- static private function input()
- {
- if(strpos(self::$value, '?') !== false)
- {
- $temp = explode('?', self::$value);
- self::$value = $temp[0];
- parse_str($temp[1], $input);
- Input::set('all', $input);
- }
- }
-
- /**
- * 将uri解析成file
- *
- * @return string
- */
- static public function file()
- {
- if(self::$method == 'GET' && !empty($_SERVER['REQUEST_URI']))
- {
- $request_uri = strtoupper(urldecode($_SERVER['REQUEST_URI']));
- if(strpos($request_uri, '<') !== false || strpos($request_uri, '"') !== false || strpos($request_uri, 'CONTENT-TRANSFER-ENCODING') !== false)
- {
- error(\Lang::get('request_tainting'));
- }
- unset($request_uri);
- }
- if(strpos(self::$value, self::explode) !== false)
- {
- $array = explode(self::explode, self::$value);
-
- if(isset($array[2]) && empty($array[3]))
- {
- $file = $array[0] . self::explode . $array[1] . self::explode . $array[2];
- unset($array[0]);
- unset($array[1]);
- unset($array[2]);
- }
- elseif(isset($array[1]))
- {
- $file = $array[0] . self::explode . $array[1];
- unset($array[0]);
- unset($array[1]);
- }
- elseif(isset($array[0]))
- {
- $file = $array[0];
- unset($array[0]);
- }
-
- $param = array_values($array);
- }
- else
- {
- $file = self::$value;
- }
-
- return $file;
- }
- }
|