Cleans and fixes vTiger URL
@access private
@static
@param  string  Base URL of vTiger CRM
@param string $baseUrl
@return string Returns cleaned and fixed vTiger URL
546036
private static function fixVtigerBaseUrl($baseUrl)
    {
        if (!preg_match('/^https?:\/\//i', $baseUrl)) {
            $baseUrl = sprintf('http://%s', $baseUrl);
        }
        if (strripos($baseUrl, '/') !== strlen($baseUrl) - 1) {
            $baseUrl .= '/';
        }
        return $baseUrl;
    }
990418
public function getCorsOrigin($url = null) {
        if (is_null($url)) {
            $baseUrl = Mage::getBaseUrl();
        } else {
            $baseUrl = $url;
        }
        $path = parse_url($baseUrl, PHP_URL_PATH);
        $domain = parse_url($baseUrl, PHP_URL_HOST);
        // there has to be a better way to just strip the path off
        return substr($baseUrl, 0,
            strpos($baseUrl, $path,
                strpos($baseUrl, $domain)));
    }
963790
public static function get_base_url($url) {
        $pieces = parse_url($url);
        $retval = $pieces['scheme'].'://'.$pieces['host'];
        $port = self::get($pieces,'port');
        if ( $port && $port != 80 && $port != 443 ) $retval .= ':' . $port;
        return $retval;
    }
504481
public static function getBaseUrl() : string
    {
        $https = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') ? 'https://' : 'http://';

        return $https . rtrim(rtrim($_SERVER['HTTP_HOST'], '\\/') . dirname($_SERVER['PHP_SELF']), '\\/');
    }
623194
private function buildUrl($url)
    {
        if (!is_array($url)) {
            if (strpos($url, '://')) {
                return (string) $url;
            }
            return (string) $this->baseUrl->combine($url);
        } elseif (strpos($url[0], '://')) {
            return \GuzzleHttp\uri_template($url[0], $url[1]);
        }

        return (string) $this->baseUrl->combine(
            \GuzzleHttp\uri_template($url[0], $url[1])
        );
    }
693001
private static function mergeUrl($uri, $baseUrl)
    {
        $href = parse_url($uri);
        $base = parse_url($baseUrl);
        $full = $href + $base + parse_url('http://localhost/');
        return $full['scheme'].'://'.$full['host'].$full['path'];
    }
1165222
protected function transformUri($url, $baseUrl)
    {
        // Only relative URIs must be transformed while
        // taking xml:base into account.
        if (Url::isRelative($url) === false) {
            return $url;
        }

        $xmlBasePolicy = $this->getRenderingEngine()->getXmlBasePolicy();

        switch ($xmlBasePolicy) {

            case AbstractMarkupRenderingEngine::XMLBASE_PROCESS:
                if (empty($baseUrl) === false) {
                    return Url::rtrim($baseUrl) . '/' . Url::ltrim($url);
                } else {
                    return $url;
                }
            break;

            default:
                return $url;
            break;
        }
    }
687763
public function setBaseUrl($baseUrl)
    {
        $this->baseUrl = '/'.trim(trim($baseUrl), '/');
        $this->router->getContext()->setBaseUrl($this->baseUrl);

        return $this;
    }
337042
public function fillFromBaseUrl($baseUrl)
    {
        $reg = "/^(https?):\/\/((www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6})(:([0-9]+))?/";
        $matches = [];

        $scheme = "http";
        $host   = "hugealice.nem.ninja";
        $port   = 7890;
        if ((bool) preg_match_all($reg, $baseUrl, $matches)) {
            $scheme = $matches[1][0];
            $host   = $matches[2][0];
            $port   = $matches[5][0];
        }

        $this->setUseSsl($scheme === "https");
        $this->setProtocol($scheme);
        $this->setHost($host);
        $this->setPort((int) $port);
        return $this;
    }
348568
public function setBaseUrl($baseUrl)
    {
        if (substr($baseUrl, 0, 2) === '//') {
            $baseUrl = 'http:'.$baseUrl;
            $this->isRelativeDomain = true;
        }

        $this->baseUrl = rtrim($baseUrl, '/');
    }
609806
public function addBaseurlToRelativeLinks($text, $baseurl, $callback)
    {
        $pattern = "#<a(.+?)href=\"([^\"]*)\"([.^>]*)>#";

        return preg_replace_callback(
            $pattern,
            function ($matches) use ($baseurl, $callback) {
                $url = $callback($matches[2], $baseurl);
                return "<a${matches[1]}href=\"$url\"${matches[3]}>";
            },
            $text
        );
    }
679241
private function cleanUrl($url)
    {
        if (strlen($url) > 1 && strrpos($url, '/') === strlen($url) - 1) {
            $url = substr($url, 0, -1);
        }
        if (strrpos($url, '.php') === strlen($url) - 4) {
            $url = substr($url, 0, -4);
        }
        return $url;
    }
1154912
public static function createFromURL($url, Dir $base = NULL) {
    if (!isset($base)) $base = new Dir(getcwd().DIRECTORY_SEPARATOR);
    
    return $base->sub($url);
  }
865368
static public function getRootUrl($url=null)
	{
	    if (!$url || strpos($url, '://') === false) {
            $schema = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
            $domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : getenv('HTTP_HOST');
            $domain = static::GetRootDomain($domain);
            $baseUrl = $domain ? $schema . $domain : '';
            $port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null;
	    } else {
	        $parts = parse_url($url);
	        $domain = static::GetRootDomain($parts['host']);
	        $baseUrl = $parts['scheme'] . '://' . $domain;
	        $port = isset($parts['port']) ? $parts['port'] : '';
	    }
	    if ($port && $port != 80 && $port != 443) {
	        $baseUrl .= ':'.$port;
	    }
	    
	    return $baseUrl;
	}
1327647
public function getBaseURL(): string
	{
		if($this->baseURL === null)
		{
			// Get the protocol

			$protocol = $this->isSecure() ? 'https://' : 'http://';

			// Get the server name and port

			if(($host = $this->server->get('HTTP_HOST')) === null)
			{
				$host = $this->server->get('SERVER_NAME');

				$port = $this->server->get('SERVER_PORT');

				if($port !== null && $port != 80)
				{
					$host = $host . ':' . $port;
				}
			}

			// Put them all together along with the base path

			$this->baseURL = $protocol . $host . $this->getBasePath();
		}

		return $this->baseURL;
	}
1313471
function resolve_url($base, $url) {
        if (!strlen($base)) return $url;
        // Step 2
        if (!strlen($url)) return $base;
        // Step 3
        if (preg_match('!^[a-z]+:!i', $url)) return $url;
        $base = parse_url($base);
        if ($url{0} == "#") {
            // Step 2 (fragment)
            $base['fragment'] = substr($url, 1);
            return Page::unparse_url($base);
        }
        unset($base['fragment']);
        unset($base['query']);
        if (substr($url, 0, 2) == "//") {
            // Step 4
            return Page::unparse_url(array('scheme' => $base['scheme'], 'path' => $url,));
        } else if ($url{0} == "/") {
            // Step 5
            $base['path'] = $url;
        } else {
            // Step 6
            $path = explode('/', $base['path']);
            $url_path = explode('/', $url);
            // Step 6a: drop file from base
            array_pop($path);
            // Step 6b, 6c, 6e: append url while removing "." and ".." from
            // the directory portion
            $end = array_pop($url_path);
            foreach ($url_path as $segment) {
                if ($segment == '.') {
                    // skip
                    
                } else if ($segment == '..' && $path && $path[sizeof($path) - 1] != '..') {
                    array_pop($path);
                } else {
                    $path[] = $segment;
                }
            }
            // Step 6d, 6f: remove "." and ".." from file portion
            if ($end == '.') {
                $path[] = '';
            } else if ($end == '..' && $path && $path[sizeof($path) - 1] != '..') {
                $path[sizeof($path) - 1] = '';
            } else {
                $path[] = $end;
            }
            // Step 6h
            $base['path'] = join('/', $path);
        }
        // Step 7
        return Page::unparse_url($base);
    }
1328911
public function resolve($url)
    {
        $baseUrl = Utils::sanitizeUri($this->getBasePath());
        $baseDir = pathinfo($baseUrl, PATHINFO_DIRNAME);

        if (empty($baseDir)) {
            $msg = "The base directory of the document ('${baseDir}') could not be resolved.";
            throw new ResolutionException($msg);
        }

        $href = $baseDir . '/' . ltrim($url, '/');

        return $href;
    }