Parse the project ID and possibly other details from a provided URL.

@param string $url
A web UI, API, or public URL of the project.

@return array
11281
private function parseProjectId($url)
    {
        $result = [];

        // If it's a plain alphanumeric string, then it's an ID already.
        if (!preg_match('/\W/', $url)) {
            $result['projectId'] = $url;

            return $result;
        }

        $urlParts = parse_url($url);
        if ($urlParts === false || empty($urlParts['host'])) {
            return $result;
        }

        $this->debug('Parsing URL to determine project ID: ' . $url);

        $host = $urlParts['host'];
        $path = isset($urlParts['path']) ? $urlParts['path'] : '';
        $fragment = isset($urlParts['fragment']) ? $urlParts['fragment'] : '';

        $site_domains_pattern = '(' . implode('|', array_map('preg_quote', $this->config->get('detection.site_domains'))) . ')';
        $site_pattern = '/\-\w+\.[a-z]{2}(\-[0-9])?\.' . $site_domains_pattern . '$/';

        if (preg_match($site_pattern, $host)) {
            list($env_project_app,) = explode('.', $host, 2);
            if (($tripleDashPos = strrpos($env_project_app, '---')) !== false) {
                $env_project_app = substr($env_project_app, $tripleDashPos + 3);
            }
            if (($doubleDashPos = strrpos($env_project_app, '--')) !== false) {
                $env_project = substr($env_project_app, 0, $doubleDashPos);
                $result['appId'] = substr($env_project_app, $doubleDashPos + 2);
            } else {
                $env_project = $env_project_app;
            }
            if (($dashPos = strrpos($env_project, '-')) !== false) {
                $result['projectId'] = substr($env_project, $dashPos + 1);
                $result['environmentId'] = substr($env_project, 0, $dashPos);
            }

            return $result;
        }

        if (strpos($path, '/projects/') !== false || strpos($fragment, '/projects/') !== false) {
            $result['host'] = $host;
            $result['projectId'] = basename(preg_replace('#/projects(/\w+)/?.*$#', '$1', $url));
            if (preg_match('#/environments(/[^/]+)/?.*$#', $url, $matches)) {
                $result['environmentId'] = rawurldecode(basename($matches[1]));
            }

            return $result;
        }

        if ($this->config->has('detection.console_domain')
            && $host === $this->config->get('detection.console_domain')
            && preg_match('#^/[a-z0-9-]+/([a-z0-9-]+)(/([^/]+))?#', $path, $matches)) {
            $result['projectId'] = $matches[1];
            if (isset($matches[3])) {
                $result['environmentId'] = rawurldecode($matches[3]);
            }

            return $result;
        }

        return $result;
    }
960775
protected function processRoutes($url)
    {
        $routes = $this->config->get('routes', []);

        foreach ($routes as $from => $to) {
            $result = UrlHelper::routeParse($from, $to, $url);
            if (false !== $result && (empty($result->methods) || in_array($this->method, $result->methods, true))) {
                return $result->url;
            }
        }

        return $url;
    }
989552
protected function parseURL($url)
    {
        $parsed = parse_url($url);
        if ($parsed === false) {
            return false;
        } elseif (!isset($parsed['scheme']) || !$this->isValidScheme($parsed['scheme'])) {
            return false;
        } else {
            if (!isset($parsed['host']) || !$this->isValidHostName($parsed['host'])) {
                return false;
            } else {
                if (!isset($parsed['port'])) {
                    $parsed['port'] = getservbyname($parsed['scheme'], 'tcp');
                    if (!is_int($parsed['port'])) {
                        return false;
                    }
                }
            }
        }
        $parsed['custom'] = (isset($parsed['path']) ? $parsed['path'] : '/') . (isset($parsed['query']) ? '?' . $parsed['query'] : '');
        return $parsed;
    }
817192
public function getVars($url)
    {
        $pattern = explode('/', $this->pattern);
        $url = explode('/', $url);
        $vars = [];
        foreach ($pattern as $key => $value) {
            if (substr($value, 0, 1) === '{' || substr($value, 0, 2) === '{?') {
                $vars []= isset($url[$key]) ? $url[$key] : null;
            }
        }
        return $vars;
    }
985665
private function getQueryParameterFromUrl($url)
    {
        $parameters = array();
        foreach (\preg_split('/&(amp;)?/i', \preg_split('/[?]/ui', $url)[1]) as $value) {
            $chunks                 = \explode('=', $value);
            $parameters[$chunks[0]] = $chunks[1];
        }

        return $parameters;
    }
100533
protected function _parseExtension($url)
    {
        if (count($this->_extensions) && strpos($url, '.') !== false) {
            foreach ($this->_extensions as $ext) {
                $len = strlen($ext) + 1;
                if (substr($url, -$len) === '.' . $ext) {
                    return [substr($url, 0, $len * -1), $ext];
                }
            }
        }

        return [$url, null];
    }
1374581
public function parseUrl($url) {
        $region = $this->getConfig('region');
        $bucket = $this->getConfig('bucket');
        $key = $url;

        if (strpos($url, 'amazonaws.com') !== false) {

            // s3<region>.amazonaws.com/<bucket>
            if (preg_match('/^https?:\/\/s3(.+?)?\.amazonaws\.com\/(.+?)\/(.+?)$/i', $url, $matches)) {
                $region = $matches[1] ?: $region;
                $bucket = $matches[2];
                $key = $matches[3];

            // <bucket>.s3<region>.amazonaws.com
            } else if (preg_match('/^https?:\/\/(.+?)\.s3(.+?)?\.amazonaws\.com\/(.+?)$/i', $url, $matches)) {
                $bucket = $matches[1];
                $region = $matches[2] ?: $region;
                $key = $matches[3];
            }
        }

        return array(
            'bucket' => $bucket,
            'key' => trim($key, '/'),
            'region' => trim($region, '-')
        );
    }
1042878
public function crawl( $url ) {

		$this->parse($url);

		while( $url = array_shift($this->sitemaps) ) {

			$this->parse($url);
		}

		return $this->urls;
	}
1261690
public function parse($url)
    {
        if (!$url) {
            $url = '/';
        }

        $Routes = TableRegistry::get('Wasabi/Core.Routes');

        $route = $Routes->find()
            ->orWhere([$Routes->aliasField('page_type') => 'collection'])
            ->orWhere([$Routes->aliasField('page_type') => 'simple'])
            ->andWhere([$Routes->aliasField('url') => $url])
            ->first();

        $pageNumber = false;

        $urlParts = explode('/', $url);

        // no direct route found -> try paged collection routes
        if (!$route) {
            $part = array_pop($urlParts);

            if (preg_match('/^[0-9]+$/', $part)) {
                $pageNumber = (int)$part;
                $part = array_pop($urlParts);
            }

            // paged url found so lets try to find a proper collection route
            if ($pageNumber !== false && $part === self::PAGE_PART) {
                $route = $Routes->find()->where([
                    $Routes->aliasField('page_type') => 'collection',
                    $Routes->aliasField('url') => join('/', $urlParts)
                ])->first();
            }
        }

        if (!$route) {
            return false;
        }

        if ($route->redirect_to !== null) {
            /** @var \Wasabi\Core\Model\Entity\Route $redirectRoute */
            $redirectRoute = $Routes->get($route->redirect_to);

            $redirectUrl = $redirectRoute->url;
            $statusCode = 301;
            if ($pageNumber !== false && $pageNumber > 1) {
                $redirectUrl .= '/' . self::PAGE_PART . '/' . $pageNumber;
            }
            if ($route->status_code !== null) {
                $statusCode = (int)$route->status_code;
            }

            $this->_redirect($redirectUrl, $statusCode);
        }

        // Dispatch Wasabi.Routes.parse event to all plugin listeners to get params.
        $routesParseEvent = new Event('Wasabi.Routes.parse', $url, [
            'route' => $route,
            'pageNumber' => $pageNumber
        ]);

        EventManager::instance()->dispatch($routesParseEvent);

        if (!$routesParseEvent->isStopped() || empty($routesParseEvent->result['params'])) {
            return false;
        }

        $params = $routesParseEvent->result['params'];

        return $params;
    }
781315
protected function parse($url) {
        // set the download file url
        $this->url = $url;

        // parse the url, to remove possible query parameters, otherwise it is not possible to fetch the extension
        $urlParsed = parse_url($url);

        // get the path info of the given name
        $pathinfo = pathinfo($urlParsed['path']);

        // get the base file name
        $this->nameOriginal = $pathinfo['filename'];

        // set the file name
        $this->extension = $pathinfo['extension'];

        // done, return the instance of this, to make chaining possible
        return $this;
    }
132893
public static function getParameters($url)
    {
        if (!$url) {
            return false;
        }
        $urlQuery = [];
        if ($query = parse_url($url)) {
            $query = explode('&', $query);
            foreach ($query as $item) {
                list($key, $value) = explode('=', $item);
                $urlQuery[$key] = $value;
            }
        }
        return $urlQuery;
    }
1229616
public static function parseUrl($url)
	{
		$result = false;

		// Build arrays of values we need to decode before parsing
		$entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%24', '%2C', '%2F', '%3F', '%23', '%5B', '%5D');
		$replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "$", ",", "/", "?", "#", "[", "]");

		// Create encoded URL with special URL characters decoded so it can be parsed
		// All other characters will be encoded
		$encodedURL = str_replace($entities, $replacements, urlencode($url));

		// Parse the encoded URL
		$encodedParts = parse_url($encodedURL);

		// Now, decode each value of the resulting array
		if ($encodedParts)
		{
			foreach ($encodedParts as $key => $value)
			{
				$result[$key] = urldecode(str_replace($replacements, $entities, $value));
			}
		}

		return $result;
	}
457127
public function parseFromString($url) {
		$urlData = parse_url($url);
		
		if (isset($urlData['scheme'])) {
			$this->scheme = $urlData['scheme'];
		}
		
		if (isset($urlData['host'])) {
			$this->host = $urlData['host'];
		}
		
		if (isset($urlData['port'])) {
			$this->port = $urlData['port'];
		}
		
		if (isset($urlData['user'])) {
			$this->username = $urlData['user'];
		}
		
		if (isset($urlData['pass'])) {
			$this->password = $urlData['pass'];
		}
		
		if (isset($urlData['path'])) {
			$this->path = $urlData['path'];
		}
		
		if (isset($urlData['fragment'])) {
			$this->fragment = $urlData['fragment'];
		}
		
		if (isset($urlData['query'])) {
			$this->queryRaw = $urlData['query'];
			
			parse_str($urlData['query'], $this->query);
		}
		
		return $this;
	}
624209
static function GatherParams($url)
    {
        $reader = new StringReader($url);
        $param = '';
        $params = array();
        $start = false;
        while (false !== ($ch = $reader->ReadChar())) {
            if ($ch == '{') {
                $start = true;
            }
            else if ($ch == '}' && $start) {
                $params[] = $param;
                $param = '';
                $start = false;
            }
            else if ($start) {
                $param .= $ch;
            }
        }
        return $params;
    }
461096
protected function parseUrl($url)
    {
        if (strpos($url, '//') === 0) {
            $url = "http:$url";
        }

        $this->info = parse_url($url);

        if (isset($this->info['query'])) {
            parse_str($this->info['query'], $this->info['query']);

            array_walk_recursive($this->info['query'], function (&$value) {
                $value = urldecode($value);
            });
        }

        if (isset($this->info['path'])) {
            $this->setPath($this->info['path']);
        }
    }
1312558
public function parseUrl($url) {
		$url = parse_url($url);
		$url['vars'] = array();
		if (!isset($url['fragment'])) {
			$url['fragment'] = null;
		}
		if (isset($url['query'])) {
			parse_str($url['query'], $url['vars']);
		}

		return $url;
	}
924050
protected static function parseHost($url)
    {
        // Get base hostname
        $host = parse_url($url, PHP_URL_HOST);
        if (!$host) {
            return null;
        }

        // Include port
        $port = parse_url($url, PHP_URL_PORT);
        if ($port) {
            $host .= ':' . $port;
        }

        return $host;
    }