Сравнение двух url-ов

@param string $url1 первый url
@param string $url2 второй url
@param bool $strict сравнивать с учётом www
321367
public static function isSameUrl($url1, $url2, $strict = false) {
		// проверяем host
		if (!self::isSameHostIdna($url1, $url2, $strict)) {
			return false;
		}

		// приводим к каноническому виду
		// -http, -www, -/
		$url1 = parse_url(self::httpAdd(self::slashLess(self::wwwLess($url1))));
		$url2 = parse_url(self::httpAdd(self::slashLess(self::wwwLess($url2))));

		// разбиваем path и query по разделителю, сортируем, собираем назад и сравниваем
		foreach (['path' => '/', 'query' => '&'] as $index => $delimeter) {
			if (empty($url1[$index]) || empty($url2[$index])) {
				continue;
			}

			// пытаемся декодировать эти блоки для сравнения
			$url1[$index] = urldecode($url1[$index]);
			$url2[$index] = urldecode($url2[$index]);

			$path = [];

			foreach ([$url1, $url2] as $url) {
				$url[$index] = array_filter(explode($delimeter, $url[$index]));
				sort($url[$index]);
				$path[] = implode($delimeter, $url[$index]);

			}
			if (strcasecmp($path[0], $path[1]) != 0) {
				// пытаемся кодировать-декодировать
				return false;
			}
		}

		return true;
	}
1147968
public function setUrl($url)
    {
        $this->url = $url;

        // Normalize URL if its relative (strictly speaking), or has leading slashes
        if (Director::is_relative_url($url) || preg_match('/^\//', $url)) {
            $this->url = preg_replace(array('/\/+/','/^\//', '/\/$/'), array('/','',''), $this->url);
        }
        if (preg_match('/^(.*)\.([A-Za-z][A-Za-z0-9]*)$/', $this->url, $matches)) {
            $this->url = $matches[1];
            $this->extension = $matches[2];
        }
        if ($this->url) {
            $this->dirParts = preg_split('|/+|', $this->url);
        } else {
            $this->dirParts = array();
        }

        return $this;
    }
668407
public static function replaceUrlAbstractToFullUrl($url, $domain = null)
    {

        if ($domain):
            $domain = (preg_match('/\/$/i', $domain) ? substr($domain, 0, -1) : $domain);
            $domain = str_replace(array("https://", "http://"), "", $domain);
            $domains = explode("/", $domain);
            $domain = "http://" . $domains[0];
            unset($domains[count($domains) - 1], $domains[0]);
            $count = count($domains);
        endif;

        if (preg_match('/^\/\//', $url) || preg_match('/^www\./', $url)):
            $url = 'http:' . (preg_match('/^www\./', $url) ? "//" : "") . $url;

        elseif ((preg_match('/^\//', $url) || preg_match('/^(\w|-)/i', $url)) && !preg_match('/^http:/i', $url) && $domain):
            $url = $domain . (!preg_match('/^\//', $url) ? "/" : "") . $url;

        elseif (preg_match('/^\.\./', $url) && $domain):
            if (preg_match_all('/\.\.\//i', $url, $matches) > 0):
                foreach ($matches[0] as $i => $t):
                    unset($domains[$count - $i]);
                endforeach;
            endif;

            $url = $domain . (!empty($domains) ? '/' . implode("/", $domains) : "") . '/' . str_replace('../', '', $url);

        elseif (preg_match('/^\./', $url) && $domain):
            $url = $domain . (isset($domains[1]) ? "/" . $domains[1] : "") . (isset($domains[2]) ? "/" . $domains[2] : "") . (isset($domains[3]) ? "/" . $domains[3] : "") . str_replace('./', "/", $url);
        endif;

        $testeHttp = explode("http", $url);
        if (isset($testeHttp[2])):
            $url = "http" . $testeHttp[count($testeHttp) - 1];
        endif;
        return $url;
    }
836956
protected function matchUrlDirectly($url)
    {
        foreach ($this->routes as $route) {
            if ($route->matchDirectly($url)) {
                return $route;
            }
        }
        return null;
    }
387463
public static function appendParamsToUrl($url, array $newParams = [])
    {
        if (empty($newParams)) {
            return $url;
        }

        if (strpos($url, '?') === false) {
            return $url . '?' . http_build_query($newParams, null, '&');
        }

        list($path, $query) = explode('?', $url, 2);
        $existingParams = [];
        parse_str($query, $existingParams);

        // Использовать параметры из исходного URL поверх $newParams
        $newParams = array_merge($newParams, $existingParams);

        // Сортировка для предикативного заказа
        ksort($newParams);

        return $path . '?' . http_build_query($newParams, null, '&');
    }
546121
public static String escape(final String url, final boolean strict) {
        return (strict ? STRICT_ESCAPER : ESCAPER).escape(url);
    }
622780
public function match(Parser $parser, $uri, $params = array(), $strict = false)
    {
        $op = $this->operator;

        // check expression operator first
        if ($op->id && $uri[0] !== $op->id) {
          return array($uri, $params);
        }

        // remove operator from input
        if ($op->id) {
            $uri = substr($uri, 1);
        }

        foreach($this->sortVariables($this->variables) as $var) {
            /** @var \Rize\UriTemplate\Node\Variable $regex */
            $regex = '#'.$op->toRegex($parser, $var).'#';
            $val   = null;

            // do a forward lookup and get just the relevant part
            $remainingUri = '';
            $preparedUri = $uri;
            if ($this->forwardLookupSeparator) {
                $lastOccurrenceOfSeparator = stripos($uri, $this->forwardLookupSeparator);
                $preparedUri = substr($uri, 0, $lastOccurrenceOfSeparator);
                $remainingUri = substr($uri, $lastOccurrenceOfSeparator);
            }

            if (preg_match($regex, $preparedUri, $match)) {

                // remove matched part from input
                $preparedUri = preg_replace($regex, '', $preparedUri, $limit = 1);
                $val = $op->extract($parser, $var, $match[0]);
            }

            // if strict is given, we quit immediately when there's no match
            else if ($strict) {
                return null;
            }

            $uri = $preparedUri . $remainingUri;

            $params[$var->getToken()] = $val;
        }

        return array($uri, $params);
    }
608795
public function match(Parser $parser, $uri, $params = array(), $strict = false)
    {
        // match literal string from start to end
        $length = strlen($this->token);
        if (substr($uri, 0, $length) === $this->token) {
            $uri = substr($uri, $length);
        }

        // when there's no match, just return null if strict mode is given
        else if ($strict) {
            return null;
        }

        return array($uri, $params);
    }
1298867
public function url($url = null)
    {
        if (!is_null($url) && true !== $url) {
            $this->url = $url;
            return $this;
        } elseif (!$this->url) {
            if ($this->isCli()) {
                $this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
            } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
                $this->url = $_SERVER['HTTP_X_REWRITE_URL'];
            } elseif (isset($_SERVER['REQUEST_URI'])) {
                $this->url = $_SERVER['REQUEST_URI'];
            } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
                $this->url = $_SERVER['ORIG_PATH_INFO'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
            } else {
                $this->url = '';
            }
        }
        return true === $url ? $this->domain() . $this->url : $this->url;
    }
1373752
public static function strtolower($str, bool $strict=false) {
		ref\mb::strtolower($str, $strict);
		return $str;
	}
1285853
public function isSameUrlFull($url1, $url2) {
		$url1 = mb_strtolower($url1);
		$url2 = mb_strtolower($url2);

		if (self::isSameUrl($url1, $url2)) {
			return true;
		}

		// проверим пуникоду ссылки
		$pyni = self::convertToPunycode($url1);
		$urlDec = urldecode($url1);

		if (strcasecmp($url1, $url2) == 0) {
			return true;
		} elseif (strcasecmp($urlDec, $url2) == 0) {
			return true;
		} elseif (strcasecmp($pyni['linkPunyEnc'], $url2) == 0) {
			return true;
		} elseif (strcasecmp($pyni['linkPunyDec'], $url2) == 0) {
			return true;
		} else {
			return false;
		}
	}
1074623
public static function isSameHostIdna($url1, $url2, $strict = true) {
		$Idna = new IdnaConvert();
		$url1 = $Idna->encode($url1);
		$url2 = $Idna->encode($url2);

		return strcasecmp(self::getHost($url1, !$strict), self::getHost($url2, !$strict)) == 0;
	}
1245361
public function buildUrl($url)
    {
        if (strncmp($url, '/', 1) === 0) {
            return $url;
        }
        $baseUrl = isset($this->grid->controllerUrl) ? $this->grid->controllerUrl : '';

        return $baseUrl ? Url::to($baseUrl . '/' . $url) : Url::to($url);
    }
947315
public static function getStatusBetween($from = null, $to = null, $strict = false, $strictTo = null)
    {
        $statusList = static::getStatusList();

        $strictFrom = $strict;
        $strictTo = isset($strictTo) ? (bool) $strictTo : $strict;

        // Remove status before given $from
        if (isset($from)) {
            static::checkAllowedStatus($from);
            foreach ($statusList as $key => $status) {
                if ($from !== $status) {
                    unset($statusList[$key]);
                } else {
                    if ($strictFrom) {
                        unset($statusList[$key]);
                    }
                    break;
                }
            }
        }

        // Remove status after given $to
        if (isset($to)) {
            static::checkAllowedStatus($to);
            // On inverse l'ordre des statuts
            $statusList = array_reverse($statusList);
            foreach ($statusList as $key => $status) {
                if ($to !== $status) {
                    unset($statusList[$key]);
                } else {
                    if ($strictTo) {
                        unset($statusList[$key]);
                    }
                    break;
                }
            }
            // On inverse l'ordre des statuts
            $statusList = array_reverse($statusList);
        }

        return array_values($statusList);
    }
719224
public static function nav(string $url, bool $add_get = false, string $language_url = ''): string
    {
        if (is_bool($url) || empty($url)) {
            echo 'Deprecated';
            exit;
        }

        if ($add_get) {
            $tmp = explode('?', $_SERVER['REQUEST_URI'], 2);
            if (isset($tmp[1])) {
                $url .= '?'.$tmp[1];
            }
        }

        if (!empty($language_url) && file_exists($language_url)) {
            $text = include $language_url;
        } elseif (file_exists(__DIR__.'/language/'.Core::$LANGUAGE['lang'].'.php')) {
            $text = include __DIR__.'/language/'.Core::$LANGUAGE['lang'].'.php';
        } else {
            $text = include __DIR__.'/language/ru.php';
        }

        $nav = '<ul class="pagination">';

        if (self::$curpage-self::$options['move'] > 1 && self::$options['begin']) {
            if (self::$options['begin']) {
                $nav .= '<li class="page-item"><a class="page-link" href="'.str_replace('{page}', '1', $url).'">'.$text['begin'].'</a></li>';
            }

            if (self::$options['before']) {
                $nav .= '<li class="page-item"><a class="page-link" href="'.str_replace('{page}', (self::$curpage-self::$options['move'] - 1), $url).'">'.$text['before'].'</a></li>';
            }

            if (self::$options['trash']) {
                $nav .= '<li class="page-item"><span class="page-link">...</span></li>';
            }
        }

        for ($i = self::$curpage-self::$options['move']; $i <= self::$curpage+self::$options['move']; ++$i) {
            if ($i == self::$curpage) {
                $nav .= '<li class="page-item active"><span class="page-link">'.self::$curpage.'</span></li>';
            } elseif ($i > 0 && $i <= self::$pages) {
                $nav .= '<li class="page-item"><a class="page-link" href="'.str_replace('{page}', $i, $url).'">'.$i.'</a></li>';
            }
        }

        if (self::$curpage - 1 > 0 && empty(Core::$META['prev'])) {
            Core::$META['prev'] = str_replace('{page}', (self::$curpage - 1), $url);
        }
        if (self::$curpage + 1 <= self::$pages && empty(Core::$META['next'])) {
            Core::$META['next'] = str_replace('{page}', (self::$curpage + 1), $url);
        }

        if (self::$curpage+self::$options['move'] < self::$pages) {
            if (self::$options['trash']) {
                $nav .= '<li class="page-item"><span class="page-link">...</span></li>';
            }

            if (self::$options['next']) {
                $nav .= '<li class="page-item"><a class="page-link" href="'.str_replace('{page}', (self::$curpage+self::$options['move'] + 1), $url).'">'.$text['next'].'</a></li>';
            }

            if (self::$options['end']) {
                $nav .= '<li class="page-item"><a class="page-link" href="'.str_replace('{page}', self::$pages, $url).'">'.$text['end'].'</a></li>';
            }
        }

        $nav .= '</ul>';
        return $nav;
    }
1211565
public function gotoURL($url, $base = true)
    {
        if ($base) {
            $url = $this->app->baseURL . $url;
        }

        $this->js('location.href="'.$url.'";');
    }
178680
public function toUrl($url, $parameters)
	{
		foreach ($parameters as $key => $value)
		{
			if (\is_array($value))
			{
				foreach ($value as $k => $v)
				{
					if (strpos($url, '?') === false)
					{
						$url .= '?' . $key . '=' . $v;
					}
					else
					{
						$url .= '&' . $key . '=' . $v;
					}
				}
			}
			else
			{
				if (strpos($value, ' ') !== false)
				{
					$value = $this->safeEncode($value);
				}

				if (strpos($url, '?') === false)
				{
					$url .= '?' . $key . '=' . $value;
				}
				else
				{
					$url .= '&' . $key . '=' . $value;
				}
			}
		}

		return $url;
	}