2110b5f93fb490cbf8f18fe387c49802fc0e3088.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php namespace Maze\Http;
  2. use Maze\Http\Output;
  3. use Maze\Config\Load as Config;
  4. use Maze\Config\Project;
  5. use Maze\Routing\Uri;
  6. class Url
  7. {
  8. /**
  9. * config
  10. *
  11. * @var array
  12. */
  13. static public $config;
  14. /**
  15. * link
  16. * @param string $value
  17. *
  18. * @return array
  19. */
  20. static public function get($value = false, $project = false, $replace = false)
  21. {
  22. if($replace)
  23. {
  24. # 这里貌似暂时没啥用了。先保留吧。
  25. return str_replace('{'.$replace.'}', $value, Config::$global['host']['domain']);
  26. }
  27. if($value === false)
  28. {
  29. $value = Uri::$url ? Uri::$url : '';
  30. }
  31. if(strpos($value, 'http://') !== false)
  32. {
  33. return $value;
  34. }
  35. $host = '';
  36. if(strpos($value, '/') !== false && (strpos($value, '.') !== false || strpos($value, '--') !== false))
  37. {
  38. $temp = explode('/', $value);
  39. $config = Project::load($temp[0]);
  40. if($config)
  41. {
  42. $value = $temp[1];
  43. $project = $temp[0];
  44. $host = $config['url'];
  45. }
  46. }
  47. elseif($project)
  48. {
  49. $config = Project::load($project);
  50. if($config)
  51. {
  52. $host = $config['url'];
  53. }
  54. }
  55. $key = $value;
  56. if(isset(self::$config['url']) && isset(self::$config['url'][$key]))
  57. {
  58. return self::$config['url'][$key];
  59. }
  60. Config::get('route', $project);
  61. if(Config::$global['route'])
  62. {
  63. if(strpos($value, '?') !== false)
  64. {
  65. $arg = explode('?', $value);
  66. $value = $arg[0];
  67. }
  68. if($uri = array_search($value, Config::$global['route']))
  69. {
  70. $value = $uri;
  71. }
  72. if(isset($arg[1]) && $arg[1])
  73. {
  74. parse_str($arg[1], $out);
  75. $str = $pre = '';
  76. $i = 1;
  77. self::$config['link_key'] = self::$config['link_value'] = array();
  78. foreach($out as $k => $v)
  79. {
  80. if($i > 1)
  81. {
  82. $pre = '&';
  83. }
  84. $str .= $pre . $k . '=$' . $i;
  85. $i++;
  86. self::$config['link_key'][] = $k;
  87. self::$config['link_value'][] = $v;
  88. }
  89. self::$config['link_index'] = 0;
  90. $result = '';
  91. if($key = array_search($value . '?' . $str, Config::$global['route']))
  92. {
  93. $result = preg_replace_callback('/\(.*?\)/', 'self::link', $key);
  94. }
  95. if(!$result || $result == $value)
  96. {
  97. $value = $value . '?' . $arg[1];
  98. }
  99. else
  100. {
  101. $value = $result;
  102. }
  103. }
  104. else
  105. {
  106. $index = strpos($value, '?');
  107. if($index === false)
  108. {
  109. $value = preg_replace('/&/', '?', $value, 1);
  110. }
  111. }
  112. }
  113. if(!$host)
  114. {
  115. Config::get('host', $project);
  116. $host = Config::$global['host']['base'];
  117. }
  118. /*
  119. if(defined('MAZE_FRONT_HOST'))
  120. {
  121. $host = MAZE_FRONT_HOST;
  122. }
  123. elseif($project)
  124. {
  125. $host = $project;
  126. }
  127. else
  128. {
  129. $host = Config::$global['host']['base'];
  130. }
  131. */
  132. return self::$config['url'][$key] = $host . Uri::$type . $value;
  133. }
  134. static private function link($param)
  135. {
  136. if(isset($param[0]) && $param[0] && isset(self::$config['link_value']) && isset(self::$config['link_value'][self::$config['link_index']]))
  137. {
  138. /*
  139. link encode
  140. $config = Helper::config('link_encode');
  141. if($config && is_numeric(self::$config['link_value'][self::$config['link_index']]) && in_array(self::$config['link_key'][self::$config['link_index']], $config))
  142. {
  143. self::$config['link_value'][self::$config['link_index']] = link_encode(self::$config['link_value'][self::$config['link_index']]);
  144. }
  145. */
  146. $param[0] = self::$config['link_value'][self::$config['link_index']];
  147. }
  148. self::$config['link_index']++;
  149. return $param[0];
  150. }
  151. /**
  152. * location
  153. * @param string $value
  154. *
  155. * @return mixed
  156. */
  157. static public function location($value, $type = 1)
  158. {
  159. if(strpos($value, 'http://') === false && strpos($value, 'https://') === false)
  160. {
  161. $value = self::get($value);
  162. }
  163. switch($type)
  164. {
  165. case 2 :
  166. $html = '<script>location.href="' . $value . '"</script>';
  167. Output::abert($html);
  168. die;
  169. break;
  170. default :
  171. header('Location: ' . $value);
  172. die;
  173. break;
  174. }
  175. }
  176. }