Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. class Url
  13. {
  14. // 生成URL地址的root
  15. protected static $root;
  16. protected static $bindCheck;
  17. /**
  18. * URL生成 支持路由反射
  19. * @param string $url 路由地址
  20. * @param string|array $vars 参数(支持数组和字符串)a=val&b=val2... ['a'=>'val1', 'b'=>'val2']
  21. * @param string|bool $suffix 伪静态后缀,默认为true表示获取配置值
  22. * @param boolean|string $domain 是否显示域名 或者直接传入域名
  23. * @param string $seo_pseudo URL模式
  24. * @param string $seo_pseudo_format URL格式
  25. * @return string
  26. */
  27. public static function build($url = '', $vars = '', $suffix = true, $domain = false, $seo_pseudo = null, $seo_pseudo_format = null, $seo_inlet = null)
  28. {
  29. static $request = null;
  30. if (null == $request) {
  31. $request = Request::instance();
  32. }
  33. $module = $request->module();
  34. $mca = !empty($url) ? explode('/', $url) : []; // by 小虎哥
  35. $main_lang = get_main_lang(); // 主语言 by 小虎哥
  36. /*自动识别系统环境隐藏入口文件 by 小虎哥*/
  37. self::root($request->baseFile().'/');
  38. null === $seo_inlet && $seo_inlet = config('ey_config.seo_inlet');
  39. if (1 == $seo_inlet) {
  40. if ('admin' != $module) { // 排除后台分组模块
  41. self::root(ROOT_DIR.'/');
  42. } else if (3 == count($mca) && 'admin' != $mca[0]) { // 排除url中带有admin分组模块
  43. self::root(ROOT_DIR.'/');
  44. }
  45. }
  46. /*--end*/
  47. // 解析参数 by 小虎哥
  48. if (is_string($vars)) {
  49. parse_str($vars, $vars);
  50. }
  51. $is_language = false;
  52. static $web_citysite_open = null;
  53. null === $web_citysite_open && $web_citysite_open = config('city_switch_on');
  54. if (!empty($web_citysite_open)) { // 多城市站点
  55. if ('admin' == $module) {
  56. if ((isset($vars['site']) && true === $vars['site'])) {
  57. $vars['site'] = '';
  58. } else if (empty($vars['site'])) {
  59. $vars['site'] = get_home_site();
  60. }
  61. } else {
  62. if ((isset($vars['site']) && true === $vars['site']) || get_default_site() == get_home_site() || $request->subDomain() == get_home_site()) {
  63. $vars['site'] = '';
  64. } else if (empty($vars['site'])) {
  65. $vars['site'] = get_home_site();
  66. }
  67. // tag标签链接不需要支持分站
  68. if (isset($mca[1]) && $mca[1] == 'Tags') {
  69. $vars['site'] = '';
  70. }
  71. }
  72. }
  73. else { // 多语言
  74. $is_language = is_language();
  75. if ($is_language) {
  76. if (empty($vars['lang'])) {
  77. $lang = $request->param('lang/s', '');
  78. if ('admin' == $module) {
  79. $system_home_default_lang = config('ey_config.system_home_default_lang');
  80. if (!empty($lang) && $lang != $system_home_default_lang) {
  81. $vars['lang'] = $lang;
  82. }
  83. } else {
  84. $lang = get_current_lang();
  85. $default_lang = get_default_lang();
  86. if ($default_lang != $lang) {
  87. $vars['lang'] = $lang;
  88. }
  89. }
  90. } else {
  91. if (!empty($vars['lang']) && $vars['lang'] == get_default_lang()) {
  92. unset($vars['lang']);
  93. }
  94. }
  95. } else { // 单语言
  96. if ('admin' == $module) { // 排除后台分组模块
  97. if (empty($vars['lang'])) {
  98. $vars['lang'] = $main_lang;
  99. }
  100. }
  101. }
  102. }
  103. if (false === $domain && Route::rules('domain')) {
  104. $domain = true;
  105. }
  106. // 解析URL
  107. if (0 === strpos($url, '[') && $pos = strpos($url, ']')) {
  108. // [name] 表示使用路由命名标识生成URL
  109. $name = substr($url, 1, $pos - 1);
  110. $url = 'name' . substr($url, $pos + 1);
  111. }
  112. if (false === strpos($url, '://') && 0 !== strpos($url, '/')) {
  113. $info = parse_url($url);
  114. $url = !empty($info['path']) ? $info['path'] : '';
  115. if (isset($info['fragment'])) {
  116. // 解析锚点
  117. $anchor = $info['fragment'];
  118. if (false !== strpos($anchor, '?')) {
  119. // 解析参数
  120. list($anchor, $info['query']) = explode('?', $anchor, 2);
  121. }
  122. if (false !== strpos($anchor, '@')) {
  123. // 解析域名
  124. list($anchor, $domain) = explode('@', $anchor, 2);
  125. }
  126. } elseif (strpos($url, '@') && false === strpos($url, '\\')) {
  127. // 解析域名
  128. list($url, $domain) = explode('@', $url, 2);
  129. }
  130. }
  131. // 解析参数
  132. if (is_string($vars)) {
  133. // aaa=1&bbb=2 转换成数组
  134. parse_str($vars, $vars);
  135. }
  136. if ($url) {
  137. $route_name = isset($name) ? $name : $url . (isset($info['query']) ? '?' . $info['query'] : '');
  138. $rule = Route::name($route_name);
  139. if (is_null($rule) && isset($info['query'])) {
  140. $rule = Route::name($url);
  141. // 解析地址里面参数 合并到vars
  142. parse_str($info['query'], $params);
  143. $vars = array_merge($params, $vars);
  144. unset($info['query']);
  145. }
  146. }
  147. if (!empty($rule) && $match = self::getRuleUrl($rule, $vars)) {
  148. // 匹配路由命名标识
  149. $url = $match[0];
  150. // 替换可选分隔符
  151. $url = preg_replace(['/(\W)\?$/', '/(\W)\?/'], ['', '\1'], $url);
  152. if (!empty($match[1])) {
  153. $domain = $match[1];
  154. }
  155. if (!is_null($match[2])) {
  156. $suffix = $match[2];
  157. }
  158. } elseif (!empty($rule) && isset($name)) {
  159. throw new \InvalidArgumentException('route name not exists:' . $name);
  160. } else {
  161. // 检查别名路由
  162. $alias = Route::rules('alias');
  163. $matchAlias = false;
  164. if ($alias) {
  165. // 别名路由解析
  166. foreach ($alias as $key => $val) {
  167. if (is_array($val)) {
  168. $val = $val[0];
  169. }
  170. if (0 === strpos($url, $val)) {
  171. $url = $key . substr($url, strlen($val));
  172. $matchAlias = true;
  173. break;
  174. }
  175. }
  176. }
  177. if (!$matchAlias) {
  178. // 路由标识不存在 直接解析
  179. $url = self::parseUrl($url, $domain);
  180. }
  181. if (isset($info['query'])) {
  182. // 解析地址里面参数 合并到vars
  183. parse_str($info['query'], $params);
  184. $vars = array_merge($params, $vars);
  185. }
  186. }
  187. // 检测URL绑定
  188. if (!self::$bindCheck) {
  189. $type = Route::getBind('type');
  190. if ($type) {
  191. $bind = Route::getBind($type);
  192. if ($bind && 0 === strpos($url, $bind)) {
  193. $url = substr($url, strlen($bind) + 1);
  194. }
  195. }
  196. }
  197. // 还原URL分隔符
  198. $depr = Config::get('pathinfo_depr');
  199. $url = str_replace('/', $depr, $url);
  200. // URL后缀
  201. $suffix = in_array($url, ['/', '']) ? '' : self::parseSuffix($suffix);
  202. // 锚点
  203. $anchor = !empty($anchor) ? '#' . $anchor : '';
  204. $ey_config = config('ey_config'); // URL模式 by 小虎哥
  205. $seo_pseudo = !empty($seo_pseudo) ? $seo_pseudo : $ey_config['seo_pseudo'];
  206. if (empty($seo_pseudo_format)) {
  207. if (1 == $seo_pseudo) {
  208. $seo_pseudo_format = $ey_config['seo_dynamic_format'];
  209. } else if (3 == $seo_pseudo) {
  210. $seo_pseudo_format = $ey_config['seo_rewrite_format'];
  211. }
  212. }
  213. if ((1 == $seo_pseudo && 1 == $seo_pseudo_format) || 2 == $seo_pseudo) {
  214. /*默认兼容模式,支持不开启pathinfo模式*/
  215. $urlinfo = $mca;
  216. // $urlinfo = explode('/', $url);
  217. $len = count($urlinfo);
  218. $m = !empty($urlinfo[$len - 3]) ? $urlinfo[$len - 3] : $request->module();
  219. $c = !empty($urlinfo[$len - 2]) ? $urlinfo[$len - 2] : $request->controller();
  220. $a = !empty($urlinfo[$len - 1]) ? $urlinfo[$len - 1] : $request->action();
  221. // 检测域名
  222. $domain = self::parseDomain($url, $domain);
  223. // URL组装
  224. $url = $domain . rtrim(self::$root ?: $request->root(), '/');
  225. if (1 == $seo_inlet && 'admin' != $m) {
  226. $url .= "/";
  227. if (2 == $seo_pseudo || stristr($request->url(), '/index.php')) {
  228. $url .= "index.php";
  229. }
  230. }
  231. $url .= "?m={$m}&c={$c}&a={$a}";
  232. /*URL全局参数(比如:可视化uiset、多模板v、多语言lang、多城市site)*/
  233. $urlParam = $request->param();
  234. !empty($vars['lang']) && !empty($urlParam['lang']) && $urlParam['lang'] = $vars['lang'];
  235. if (isset($vars['site']) && empty($vars['site'])) {
  236. unset($urlParam['site']);
  237. } else {
  238. !empty($vars['site']) && !empty($urlParam['site']) && $urlParam['site'] = $vars['site'];
  239. }
  240. foreach ($urlParam as $key => $val) {
  241. if (in_array($key, Config::get('global.parse_url_param'))) {
  242. $urlParam[$key] = trim($val, '/');
  243. } else {
  244. unset($urlParam[$key]);
  245. }
  246. }
  247. is_array($vars) && $vars = array_merge($vars, $urlParam);
  248. /*--end*/
  249. /*当前默认语言下,在后台的非后台模块链接将去掉lang参数,比如:地图sitemap.xml以及栏目、内容浏览*/
  250. if ('admin' == $module) { // 后台分组模块
  251. if (!empty($vars['lang']) && $vars['lang'] == $main_lang) {
  252. if (3 == count($mca) && 'admin' != $mca[0]) { // 排除带有admin分组模块
  253. unset($vars['lang']);
  254. }
  255. } else {
  256. if (2 == count($mca) || 'admin' == $mca[0]) { // 排除带有admin分组模块
  257. !isset($vars['lang']) && $vars['lang'] = get_current_lang();
  258. }
  259. }
  260. } else {
  261. /*URL全局参数(单语言不携带lang参数)*/
  262. if (!$is_language) {
  263. unset($vars['lang']);
  264. }
  265. if (empty($web_citysite_open)) { // 不开启多城市
  266. unset($vars['site']);
  267. }
  268. }
  269. /*--end*/
  270. // 参数组装
  271. if (!empty($vars)) {
  272. /*过滤分组模块、控制器、操作方法,以及没有值的参数*/
  273. foreach ($vars as $key => $val) {
  274. if (in_array($key, ['m','c','a']) || empty($val)) {
  275. unset($vars[$key]);
  276. }
  277. }
  278. /*--end*/
  279. /*添加参数*/
  280. $vars = http_build_query($vars);
  281. if (!empty($vars)) {
  282. $url .= "&".$vars.$anchor;
  283. }
  284. /*--end*/
  285. } else {
  286. $url .= $anchor;
  287. }
  288. /*--end*/
  289. } else {
  290. /*当前默认语言下,在后台的非后台模块链接将去掉lang参数,比如:地图sitemap.xml以及栏目、内容浏览*/
  291. if ('admin' == $module) { // 后台分组模块
  292. if (!empty($vars['lang']) && $vars['lang'] == $main_lang) {
  293. if (3 == count($mca) && 'admin' != $mca[0]) { // 排除带有admin分组模块
  294. unset($vars['lang']);
  295. }
  296. }
  297. }
  298. /*--end*/
  299. // 参数组装
  300. if (!empty($vars)) {
  301. // 添加参数
  302. if (Config::get('url_common_param')) {
  303. $vars = http_build_query($vars);
  304. $url .= $suffix . '?' . $vars . $anchor;
  305. } else {
  306. $paramType = Config::get('url_param_type');
  307. foreach ($vars as $var => $val) {
  308. if ('' !== trim($val)) {
  309. if ($paramType) {
  310. $url .= $depr . urlencode($val);
  311. } else {
  312. $url .= $depr . $var . $depr . urlencode($val);
  313. }
  314. }
  315. }
  316. $url .= $suffix . $anchor;
  317. }
  318. } else {
  319. $url .= $suffix . $anchor;
  320. }
  321. // 检测域名
  322. $domain = self::parseDomain($url, $domain);
  323. // URL组装
  324. $url = $domain . rtrim(self::$root ?: $request->root(), '/') . '/' . ltrim($url, '/');
  325. }
  326. self::$bindCheck = false;
  327. return $url;
  328. }
  329. // 直接解析URL地址
  330. protected static function parseUrl($url, &$domain)
  331. {
  332. $request = Request::instance();
  333. if (0 === strpos($url, '/')) {
  334. // 直接作为路由地址解析
  335. $url = substr($url, 1);
  336. } elseif (false !== strpos($url, '\\')) {
  337. // 解析到类
  338. $url = ltrim(str_replace('\\', '/', $url), '/');
  339. } elseif (0 === strpos($url, '@')) {
  340. // 解析到控制器
  341. $url = substr($url, 1);
  342. } else {
  343. // 解析到 模块/控制器/操作
  344. $module = $request->module();
  345. $domains = Route::rules('domain');
  346. if (true === $domain && 2 == substr_count($url, '/')) {
  347. $current = $request->host();
  348. $match = [];
  349. $pos = [];
  350. foreach ($domains as $key => $item) {
  351. if (isset($item['[bind]']) && 0 === strpos($url, $item['[bind]'][0])) {
  352. $pos[$key] = strlen($item['[bind]'][0]) + 1;
  353. $match[] = $key;
  354. $module = '';
  355. }
  356. }
  357. if ($match) {
  358. $domain = current($match);
  359. foreach ($match as $item) {
  360. if (0 === strpos($current, $item)) {
  361. $domain = $item;
  362. }
  363. }
  364. self::$bindCheck = true;
  365. $url = substr($url, $pos[$domain]);
  366. }
  367. } elseif ($domain) {
  368. if (isset($domains[$domain]['[bind]'][0])) {
  369. $bindModule = $domains[$domain]['[bind]'][0];
  370. if ($bindModule && !in_array($bindModule[0], ['\\', '@'])) {
  371. $module = '';
  372. }
  373. }
  374. }
  375. $module = $module ? $module . '/' : '';
  376. $controller = $request->controller();
  377. if ('' == $url) {
  378. // 空字符串输出当前的 模块/控制器/操作
  379. $action = $request->action();
  380. } else {
  381. $path = explode('/', $url);
  382. $action = array_pop($path);
  383. $controller = empty($path) ? $controller : array_pop($path);
  384. $module = empty($path) ? $module : array_pop($path) . '/';
  385. }
  386. if (Config::get('url_convert')) {
  387. $action = strtolower($action);
  388. $controller = Loader::parseName($controller);
  389. }
  390. $url = $module . $controller . '/' . $action;
  391. }
  392. return $url;
  393. }
  394. // 检测域名
  395. protected static function parseDomain(&$url, $domain)
  396. {
  397. if (!$domain) {
  398. return '';
  399. }
  400. $request = Request::instance();
  401. $rootDomain = Config::get('url_domain_root');
  402. if (true === $domain) {
  403. // 自动判断域名
  404. $domain = Config::get('app_host') ?: $request->host();
  405. $domains = Route::rules('domain');
  406. if ($domains) {
  407. $route_domain = array_keys($domains);
  408. foreach ($route_domain as $domain_prefix) {
  409. if (0 === strpos($domain_prefix, '*.') && strpos($domain, ltrim($domain_prefix, '*.')) !== false) {
  410. foreach ($domains as $key => $rule) {
  411. $rule = is_array($rule) ? $rule[0] : $rule;
  412. if (is_string($rule) && false === strpos($key, '*') && 0 === strpos($url, $rule)) {
  413. $url = ltrim($url, $rule);
  414. $domain = $key;
  415. // 生成对应子域名
  416. if (!empty($rootDomain)) {
  417. $domain .= $rootDomain;
  418. }
  419. break;
  420. } elseif (false !== strpos($key, '*')) {
  421. if (!empty($rootDomain)) {
  422. $domain .= $rootDomain;
  423. }
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. } else {
  431. $web_citysite_open = Config::get('city_switch_on');
  432. if (empty($rootDomain)) {
  433. $host = Config::get('app_host') ?: $request->host();
  434. if (!empty($web_citysite_open) && stristr($host, '.')) { // 多城市站点
  435. $rootDomain = $request->rootDomain($host);
  436. } else {
  437. $rootDomain = substr_count($host, '.') > 1 ? substr(strstr($host, '.'), 1) : $host;
  438. }
  439. }
  440. if (!empty($web_citysite_open) && $domain == $rootDomain) { // 多城市站点
  441. $domain = $rootDomain;
  442. } else {
  443. if ($domain != $rootDomain) {
  444. if (substr_count($domain, '.') < 2 && !strpos($domain, $rootDomain)) {
  445. $domain .= '.' . $rootDomain;
  446. }
  447. }
  448. }
  449. }
  450. if (false !== strpos($domain, '://')) {
  451. $scheme = '';
  452. } else {
  453. $scheme = $request->isSsl() || Config::get('is_https') ? 'https://' : 'http://';
  454. }
  455. return $scheme . $domain;
  456. }
  457. // 解析URL后缀
  458. protected static function parseSuffix($suffix)
  459. {
  460. if ($suffix) {
  461. $suffix = true === $suffix ? Config::get('url_html_suffix') : $suffix;
  462. if ($pos = strpos($suffix, '|')) {
  463. $suffix = substr($suffix, 0, $pos);
  464. }
  465. }
  466. return (empty($suffix) || 0 === strpos($suffix, '.') || '/' == $suffix) ? $suffix : '.' . $suffix;
  467. }
  468. // 匹配路由地址
  469. public static function getRuleUrl($rule, &$vars = [])
  470. {
  471. foreach ($rule as $item) {
  472. list($url, $pattern, $domain, $suffix) = $item;
  473. if (empty($pattern)) {
  474. return [rtrim($url, '$'), $domain, $suffix];
  475. }
  476. /*同个模块、控制器、操作名对应多个路由规则,进行优先级别匹配 by 许宇资*/
  477. $unequal = 0;
  478. foreach ($pattern as $key => $val){
  479. if (!isset($vars[$key])){
  480. $unequal = 1;
  481. break;
  482. }
  483. }
  484. if ($unequal){
  485. continue;
  486. }
  487. /*end*/
  488. $type = Config::get('url_common_param');
  489. foreach ($pattern as $key => $val) {
  490. if (isset($vars[$key])) {
  491. $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $type ? $vars[$key] : urlencode($vars[$key]), $url);
  492. unset($vars[$key]);
  493. $result = [$url, $domain, $suffix];
  494. } elseif (2 == $val) {
  495. $url = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url);
  496. $result = [$url, $domain, $suffix];
  497. } else {
  498. break;
  499. }
  500. }
  501. if (isset($result)) {
  502. return $result;
  503. }
  504. }
  505. return false;
  506. }
  507. // 指定当前生成URL地址的root
  508. public static function root($root)
  509. {
  510. self::$root = $root;
  511. Request::instance()->root($root);
  512. }
  513. }