Identify a project and environment from a URL's response headers.

@param string $url

@return array
554835
private function identifyFromHeaders($url)
    {
        if (!strpos($url, '.')) {
            throw new \InvalidArgumentException('Invalid URL: ' . $url);
        }
        if (strpos($url, '//') === false) {
            $url = 'https://' . $url;
        }
        $result = ['projectId' => null, 'environmentId' => null];
        $cluster = $this->getClusterHeader($url);
        if (!empty($cluster)) {
            $this->debug('Identified project cluster: ' . $cluster);
            list($result['projectId'], $result['environmentId']) = explode('-', $cluster, 2);
        }

        return $result;
    }
864484
public function fetch($url)
    {
        $SHDObject = $this->getSHDWrapper($url);
        $urls      = $this->getPageUrls($SHDObject);
        $titles    = $this->getPageTitles($SHDObject);
        $snippets  = $this->getPageSnippets($SHDObject);
        return array('urls'     => $urls,
                     'titles'   => $titles,
                     'snippets' => $snippets);
    }
878451
public function environments($site)
    {
        $variables = array('site' => $site);
        $request = $this->get(array('{+base_path}/sites/{site}/envs.json', $variables));
        return new Response\Environments($request);
    }
1226215
public function getUriArguments($url): array
    {
        $uriParam = str_replace($url, '', $this->router->getCurrentUri());

        return array_filter(explode('/', $uriParam));
    }
23727
public static function get_image_from_url( $url ) {
		$image_id = self::get_image_id( $url );
		$image    = wp_get_attachment_image_src( $image_id, 'full' );

		return [
			'url'       => $image[0],
			'width'     => $image[1],
			'height'    => $image[2],
			'thumbnail' => $image[3],
		];
	}
545860
public static function getParamsAsArray($url)
    {
        $query = parse_url($url, PHP_URL_QUERY);
        if (!$query) {
            return [];
        }
        $params = [];
        parse_str($query, $params);

        return $params;
    }
1317098
private function getTrackerHosts($urls)
    {
        $hosts = array();
        foreach ($urls as $url) {
            $url = parse_url($url);
            if (isset($url['host'])) {
                $hosts[] = $url['host'];
            }
        }
        return $hosts;
    }
1292627
public function getRemote($url){
		$ch = curl_init($url);
		curl_setopt_array($ch, [
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_TIMEOUT => 5,
			CURLOPT_SSL_VERIFYPEER => false,
			CURLOPT_FAILONERROR => true,
		]);
		$result = curl_exec($ch);
		if( false === $result ){
			return false;
		}
		$return = [
			'info' => curl_getinfo($ch),
			'body' => $result,
		];
		curl_close($ch);
		return $return;
	}
682461
protected function parseUrl($url)
    {
        $parsed = array(
            'pageName' => 'index',
            'revision' => null
        );

        $options = $this->getOptions();
        $path = substr($url, strlen($options['route']));

        $path = ltrim($path, '/');
        $splitPath = explode('/', $path);

        if (empty($splitPath[0])) {
            return $parsed;
        }

        $pageName = $splitPath[0];
        $revision = null;

        if (!empty($splitPath[1])) {
            $revision = $splitPath[1];
        }

        return array(
            'pageName' => $pageName,
            'revision' => $revision
        );
    }
775281
private function matchStripHostsPatterns($url)
    {
        if (Filesystem::isLocalPath($url)) {
            return true;
        }

        if (is_array($this->stripHosts)) {
            $url = trim(parse_url($url, PHP_URL_HOST), '[]');

            if (false !== filter_var($url, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
                $urltype = 'ipv4';
            } elseif (false !== filter_var($url, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
                $urltype = 'ipv6';
            } else {
                $urltype = 'name';
            }

            if ('ipv4' === $urltype || 'ipv6' === $urltype) {
                $urlunpack = unpack('N*', @inet_pton($url));
            }

            foreach ($this->stripHosts as $pattern) {
                @list($type, $host, $mask) = $pattern;

                if ('/local' === $type) {
                    if ('name' === $urltype && 'localhost' === strtolower($url)
                        || ('ipv4' === $urltype || 'ipv6' === $urltype)
                        && false === filter_var($url, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)
                    ) {
                        return true;
                    }
                } elseif ('/private' === $type) {
                    if (('ipv4' === $urltype || 'ipv6' === $urltype)
                        && false === filter_var($url, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)
                    ) {
                        return true;
                    }
                } elseif ('ipv4' === $type || 'ipv6' === $type) {
                    if ($urltype === $type && $this->matchAddr($urlunpack, $host, $mask)) {
                        return true;
                    }
                } elseif ('name' === $type) {
                    if ('name' === $urltype && preg_match($host, $url)) {
                        return true;
                    }
                }
            }
        }
    }
25025
public function load($url)
    {
        $masterRequest = $this->requestStack->getMasterRequest();
        $request = Request::create($url, 'GET', array(), $masterRequest->cookies->all(), array(), $masterRequest->server->all());
        if ($masterRequest->getSession()) {
            $request->setSession($masterRequest->getSession());
        }

        $response = $this->kernel->handle($request, HttpKernelInterface::SUB_REQUEST, false);

        return $response->getContent();
    }
1330620
protected function isUrl($url)
    {
        $urlArr = parse_url($url);

        return $urlArr['scheme']
        && in_array($urlArr['scheme'], array('http', 'https'))
        && $urlArr['host']
        && $urlArr['path'];
    }
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;
    }
950991
public function lookup($url)
  {
    $result = array();
    // get url's to check
    $canon_url = GoogleSafeBrowsing_Lookup_URL::googleCanonicalize($url);
    if(is_null($canon_url)){
      // unable to canonicalize, nothing we can do
      $this->debugLog('lookup('.$url.'): cannot canonicalize url, skipping');
      return $result;
    }
    $lookup_urls = GoogleSafeBrowsing_Lookup_URL::googleGetLookupExpressions($canon_url);
    
    $lookup_hashes = array();
    foreach($lookup_urls as $lookup_url){
      $lookup_hashes[]=GoogleSafeBrowsing_Lookup_URL::hash($lookup_url);
      //$this->debugLog(bin2hex($lookup_hashes[count($lookup_hashes)-1])." $lookup_url");
    }
    
    $found_hashes=$this->storage->hasPrefix($lookup_hashes);
    
    $matched_lists=array();
    // full hash lookups
    foreach($found_hashes as $found_hash){
      $this->debugLog('lookup('.$url.'): found hash prefix for '.bin2hex($found_hash));
      $lookup_result=$this->storage->isListedInCache($found_hash);
      if(is_null($lookup_result)){
        $lookup_result=array();
        // not cached, query google
        $collection=$this->api->getHash(substr($found_hash,0,4),$this->storage->getLists());
        if(count($collection->list_hashes)==0){
          // no hashes found, can happen, so insert dummy record for this prefix
          // so we can use the cached result (empty)
          $this->storage->addHashInCache($found_hash,$lookup_result,null,(int)$collection->cache);
        }else{
          // full length hashes found
          foreach($collection->list_hashes as $list_hash){
            $this->debugLog('lookup('.$url.'): remote lookup found full length hashes in list '.$list_hash->list_name);
            foreach($list_hash->hashes as $hash){
              $this->debugLog('lookup('.$url.'):   hash '.bin2hex($hash->hash));
              // cache in db
              $cache_seconds=(int)$collection->cache;
              if(isset($hash->cache) && $hash->cache){
                $cache_seconds=(int)$hash->cache;
              }
              $this->storage->addHashInCache($hash->hash,array($list_hash->list_name),$hash->raw_meta,$cache_seconds);
              if($hash->hash==$found_hash){
                // full length hash match
                if(!in_array($list_hash->list_name, $lookup_result)){
                  $lookup_result[]=$list_hash->list_name;
                }
              }
            }
          }
        }
      }// end cached if/else
      // $lookup_result is array of listnames (can be empty if not listed)
      foreach($lookup_result as $matched_list){
        $matched_lists[$matched_list]=true;
      }
    }
    if(count($matched_lists)>0){
      $this->debugLog('lookup('.$url.'): listed in '.implode(', ',array_keys($matched_lists)));
    }
    return array_keys($matched_lists);
  }
1338982
public function getRoute($url)
    {
        $routes = $this->_route->getRoutes();
        $return = '';
        foreach ($routes as $route) {
            $routeURI = $this->_config->from('app')->get('basepath').$route['uri'];

            // POST requests
            if (isset($_POST)) {
                foreach ($_POST as $key => $value) {
                    $route['query'][] = [$key => $value];
                }
            }

            // GET requests
            $getexp = explode('?', $_SERVER['REQUEST_URI']);
            if (isset($getexp[1])) {
                if (implode('?', $getexp) == $routeURI.'?'.$getexp[1]) {
                    $route['querystring'] = $getexp[1];
                    $qexp = explode('&', $route['querystring']);
                    foreach ($qexp as $q) {
                        $i = explode('=', $q);
                        $route['query'][] = [$i[0] => $i[1]];
                    }
                }
            }

            // For handling routes with variables
            $expressionExp = explode('/', $route['uri']);
            $serveruri = '/'.substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], $expressionExp[1]));
            $serveruriexp = explode('/', $serveruri);

            for ($i = 0; $i < count($expressionExp); $i++) {
                $expressionExp[$i] = preg_replace_callback('/\{(.+)\}/', function ($m) use ($serveruriexp, $i, &$route) {
                    $route['query'][] = [$m[1] => $serveruriexp[$i]];

                    return strtolower($serveruriexp[$i]); // url hack to accept upper cases too
                }, $expressionExp[$i]);
            }

            $expression = implode('/', $expressionExp);
            $route['uri'] = $expression;

            if (isset($route['querystring'])) {
                $checkurl = $route['uri'].'?'.$route['querystring'];
            } else {
                $checkurl = $route['uri'];
            }

            if ((strtolower($checkurl) == $url) && ($_SERVER['REQUEST_METHOD'] == $route['method'])) {
                return $route;
            }
        }
    }
971277
protected function extractUrlVariables($url){
        $variables = array();
        $pattern = "/(\\".static::$_URL_VAR_CHARACTER.".*?[^\\/]*)/";
        if (preg_match($pattern,$url,$matches)){
            array_shift($matches);
            foreach($matches as $match){
                $variables[] = $match[0];
            }
        }
        return $variables;
    }
152643
public function processUrl($url)
    {
        if (strpos($url, '::') !== false) {
            list($folder, $asset) = explode('::', $url, 2);
        } else {
            $folder = null;
            $asset = $url;
        }

        if (empty($asset)) {
            return false;
        }

        return array(
            'folder' => $folder,
            'asset' => $asset
        );
    }