123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php namespace Maze\Http;
- use Maze\Http\Output;
- use Maze\Config\Load as Config;
- use Maze\Config\Project;
- use Maze\Routing\Uri;
- class Url
- {
- /**
- * config
- *
- * @var array
- */
- static public $config;
- /**
- * link
- * @param string $value
- *
- * @return array
- */
- static public function get($value = false, $project = false, $replace = false)
- {
- if($replace)
- {
- # 这里貌似暂时没啥用了。先保留吧。
- return str_replace('{'.$replace.'}', $value, Config::$global['host']['domain']);
- }
-
- if($value === false)
- {
- $value = Uri::$url ? Uri::$url : '';
- }
-
- if(strpos($value, 'http://') !== false)
- {
- return $value;
- }
- $host = '';
- if(strpos($value, '/') !== false && (strpos($value, '.') !== false || strpos($value, '--') !== false))
- {
- $temp = explode('/', $value);
- $config = Project::load($temp[0]);
- if($config)
- {
- $value = $temp[1];
- $project = $temp[0];
- $host = $config['url'];
- }
- }
- elseif($project)
- {
- $config = Project::load($project);
- if($config)
- {
- $host = $config['url'];
- }
- }
-
- $key = $value;
- if(isset(self::$config['url']) && isset(self::$config['url'][$key]))
- {
- return self::$config['url'][$key];
- }
- Config::get('route', $project);
- if(Config::$global['route'])
- {
- if(strpos($value, '?') !== false)
- {
- $arg = explode('?', $value);
- $value = $arg[0];
- }
- if($uri = array_search($value, Config::$global['route']))
- {
- $value = $uri;
- }
- if(isset($arg[1]) && $arg[1])
- {
- parse_str($arg[1], $out);
- $str = $pre = '';
- $i = 1;
- self::$config['link_key'] = self::$config['link_value'] = array();
- foreach($out as $k => $v)
- {
- if($i > 1)
- {
- $pre = '&';
- }
- $str .= $pre . $k . '=$' . $i;
- $i++;
-
- self::$config['link_key'][] = $k;
- self::$config['link_value'][] = $v;
- }
-
- self::$config['link_index'] = 0;
- $result = '';
- if($key = array_search($value . '?' . $str, Config::$global['route']))
- {
- $result = preg_replace_callback('/\(.*?\)/', 'self::link', $key);
- }
- if(!$result || $result == $value)
- {
- $value = $value . '?' . $arg[1];
- }
- else
- {
- $value = $result;
- }
- }
- else
- {
- $index = strpos($value, '?');
- if($index === false)
- {
- $value = preg_replace('/&/', '?', $value, 1);
- }
- }
- }
- if(!$host)
- {
- Config::get('host', $project);
- $host = Config::$global['host']['base'];
- }
- /*
- if(defined('MAZE_FRONT_HOST'))
- {
- $host = MAZE_FRONT_HOST;
- }
- elseif($project)
- {
- $host = $project;
- }
- else
- {
- $host = Config::$global['host']['base'];
- }
- */
- return self::$config['url'][$key] = $host . Uri::$type . $value;
- }
- static private function link($param)
- {
- if(isset($param[0]) && $param[0] && isset(self::$config['link_value']) && isset(self::$config['link_value'][self::$config['link_index']]))
- {
- /*
- link encode
- $config = Helper::config('link_encode');
- if($config && is_numeric(self::$config['link_value'][self::$config['link_index']]) && in_array(self::$config['link_key'][self::$config['link_index']], $config))
- {
- self::$config['link_value'][self::$config['link_index']] = link_encode(self::$config['link_value'][self::$config['link_index']]);
- }
- */
- $param[0] = self::$config['link_value'][self::$config['link_index']];
- }
- self::$config['link_index']++;
-
- return $param[0];
- }
- /**
- * location
- * @param string $value
- *
- * @return mixed
- */
- static public function location($value, $type = 1)
- {
- if(strpos($value, 'http://') === false && strpos($value, 'https://') === false)
- {
- $value = self::get($value);
- }
- switch($type)
- {
- case 2 :
- $html = '<script>location.href="' . $value . '"</script>';
- Output::abert($html);
- die;
- break;
- default :
- header('Location: ' . $value);
- die;
- break;
- }
- }
- }
|