Retrieve and cache a certificates file.

@param $url string location
@throws Google_Auth_Exception
@return array certificates
543577
public function retrieveCertsFromLocation($url)
  {
    // If we're retrieving a local file, just grab it.
    if ("http" != substr($url, 0, 4)) {
      $file = file_get_contents($url);
      if ($file) {
        return json_decode($file, true);
      } else {
        throw new Google_Auth_Exception(
            "Failed to retrieve verification certificates: '" .
            $url . "'."
        );
      }
    }

    // This relies on makeRequest caching certificate responses.
    $request = $this->client->getIo()->makeRequest(
        new Google_Http_Request(
            $url
        )
    );
    if ($request->getResponseHttpCode() == 200) {
      $certs = json_decode($request->getResponseBody(), true);
      if ($certs) {
        return $certs;
      }
    }
    throw new Google_Auth_Exception(
        "Failed to retrieve verification certificates: '" .
        $request->getResponseBody() . "'.",
        $request->getResponseHttpCode()
    );
  }
1312569
public function download_file($url, $path, $timeout = 0) {
        $result = $this->googleoauth->download_one($url, null, array('filepath' => $path, 'timeout' => $timeout));
        if ($result === true) {
            $info = $this->googleoauth->get_info();
            if (isset($info['http_code']) && $info['http_code'] == 200) {
                return array('path'=>$path, 'url'=>$url);
            } else {
                throw new moodle_exception('cannotdownload', 'repository');
            }
        } else {
            throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
        }
    }
201048
private static function GetTokenFromUrl($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    $headers = curl_getinfo($ch);
    curl_close($ch);

    if ($headers['http_code'] != 200) {
      throw new OAuthException($response);
    }

    return self::GetTokenFromQueryString($response);
  }
1327366
public function getGitkitCerts() {
    $certUrl = $this->gitkitApisUrl . 'publicKeys';
    if ($this->apiKey) {
      // try server-key first
      return $this->oauth2Client->retrieveCertsFromLocation(
          $certUrl . '?key=' . $this->apiKey);
    } else {
      // fallback to service account
      $httpRequest = new Google_Http_Request($certUrl);
      $response = $this->oauth2Client->authenticatedRequest($httpRequest)
          ->getResponseBody();
      return json_decode($response);
    }
  }
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);
  }
1349784
public function getFilePathFromUrl($url)
    {
        $url = parse_url($url)['path']; //remove query part from url.
        $rel = substr($url, strpos($url, self::ENTRY_POINT) + strlen(self::ENTRY_POINT));
        $parts = explode('/', $rel, 4);
        list ($webSourceId, $timestamp, $token, $subPath) = $parts;

        $parts = explode('*/', $subPath, 2);
        if (count($parts) < 2) {
            throw new \tao_models_classes_FileNotFoundException('`'.$subPath.'` - (wrong number of path parts)');
        }
        list ($subPath, $file) = $parts;

        $secret = $this->getOption('secret');
        $ttl = $this->getOption('ttl');

        $correctToken = md5($timestamp . $subPath . $secret);

        if (time() - $timestamp > $ttl) {
            throw new \tao_models_classes_FileNotFoundException('`' . $subPath . $file . '` - (file link expired)');
        }
        if ($token != $correctToken) {
            throw new \tao_models_classes_FileNotFoundException('`' . $subPath . $file . '` - (wrong token)');
        }


        $path = array();
        foreach (explode('/', $subPath . $file) as $ele) {
            $path[] = rawurldecode($ele);
        }
        $filename = implode('/', $path);

        return $filename;
    }
842860
protected function getURLData(string $driveUrl): array
    {
        /*
         * Known URL types:
         * https://drive.google.com/file/d/0B5q9i2h-vGaCc1ZBVnFhRzN2a3c/view?ths=true A Word file from Google Drive
         * https://docs.google.com/document/d/1-phrJPVDf1SpQYU5SzJY00Ke0c6QJmILHrtm_pcQk4w/edit A Google Docs file
         * https://drive.google.com/drive/u/0/folders/0B5q9i2h-vGaCQXhLZFNLT2JyV0U A Google Drive folder
         * https://drive.google.com/open?id=0B5q9i2h-vGaCYUE0ZTM4MDhseTQ A Google Drive shared file
         * https://docs.google.com/spreadsheets/d/13QGip-d_Z88Xru64pEFRyFVDKujDOUjciy35Qytw-Qc/edit A Google Sheets file
         * https://docs.google.com/presentation/d/1R1oF7zmnSDX3w3FBpyCTJnjvOu68C7Q2MH4kI4xMFWc/edit A Google Slides file
         */

        // Is it a Google Drive|Docs URL?
        if (preg_match(
            '~^(https?://)?(drive|docs)\.google\.[a-z]+/.*(/d/|/folders/|[\?&]id=)([a-z0-9\-_]{20,})([/?&#]|$)~i',
            $driveUrl,
            $matches
        )) {
            // Any Google Drive item can be obtained using a single API method: https://developers.google.com/drive/api/v3/reference/files/get
            return [
                'type' => 'google-drive',
                'id' => $matches[4]
            ];
        }

        // Is it a URL?
        if (preg_match('~^(https?://)?[a-z0-9.@\-_\x{0080}-\x{FFFF}]+(:[0-9]+)?(/|$)~iu', $driveUrl, $matches)) {
            return [
                'type' => 'http',
                'driveUrl' => ($matches[1] ? '' : 'http://').$driveUrl
            ];
        }

        return ['type' => 'unknown'];
    }
541711
protected function getDomainRoots( $url ) {
		$roots = [];
		$pieces = parse_url( $url );
		if ( !isset( $pieces['host'], $pieces['host'] ) ) {
			return [];
		}
		$roots[] = $pieces['host'];
		$roots[] = $pieces['host'] . '/';
		$domain = isset( $pieces['host'] ) ? $pieces['host'] : '';
		if ( preg_match( '/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs ) ) {
			$roots[] = $regs['domain'];
			$roots[] = $regs['domain'] . '/';
		}
		$parts = explode( '.', $pieces['host'] );
		if ( count( $parts ) >= 3 ) {
			$roots[] = implode( '.', array_slice( $parts, -2 ) );
			$roots[] = implode( '.', array_slice( $parts, -2 ) ) . '/';
		}

		return $roots;
	}
542817
public function loadDocument($url)
    {
        // Try to return a cached document
        $document = $this->vocabularyCache->getDocument($url);
        if ($document instanceof RemoteDocument) {
            return $document;
        }

        return $this->vocabularyCache->setDocument($url, parent::loadDocument($url));
    }
852524
public function retrieveData($url)
    {
        if (!is_null($this->getAuthorizationToken())) {
            $this->curlWrapper->setHeader('Authorization', $this->getAuthorizationToken());
        }

        $this->retrieve($url);

        return $this->curlWrapper->response;
    }
488319
protected function _getRemoteData($url)
    {
        ini_set('user_agent', $this->_getUserAgent());

        switch ($this->_getUpdateMethod()) {
            case self::UPDATE_LOCAL:
                $file = file_get_contents($url);

                if ($file !== false) {
                    return $file;
                } else {
                    throw new Exception('Cannot open the local file');
                }
            case self::UPDATE_FOPEN:
                // include proxy settings in the file_get_contents() call
                $context = $this->_getStreamContext();
                $file = file_get_contents($url, false, $context);

                if ($file !== false) {
                    return $file;
                } // else try with the next possibility (break omitted)
            case self::UPDATE_FSOCKOPEN:
                $remote_url = parse_url($url);
                $remote_handler = fsockopen($remote_url['host'], 80, $c, $e, $this->timeout);

                if ($remote_handler) {
                    stream_set_timeout($remote_handler, $this->timeout);

                    if (isset($remote_url['query'])) {
                        $remote_url['path'] .= '?' . $remote_url['query'];
                    }

                    $out = sprintf(
                        self::REQUEST_HEADERS,
                        $remote_url['path'],
                        $remote_url['host'],
                        $this->_getUserAgent()
                    );

                    fwrite($remote_handler, $out);

                    $response = fgets($remote_handler);
                    if (strpos($response, '200 OK') !== false) {
                        $file = '';
                        while (!feof($remote_handler)) {
                            $file .= fgets($remote_handler);
                        }

                        $file = str_replace("\r\n", "\n", $file);
                        $file = explode("\n\n", $file);
                        array_shift($file);

                        $file = implode("\n\n", $file);

                        fclose($remote_handler);

                        return $file;
                    }
                } // else try with the next possibility
            case self::UPDATE_CURL:
                $ch = curl_init($url);

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
                curl_setopt($ch, CURLOPT_USERAGENT, $this->_getUserAgent());

                $file = curl_exec($ch);

                curl_close($ch);

                if ($file !== false) {
                    return $file;
                } // else try with the next possibility
            case false:
                throw new Exception('Your server can\'t connect to external resources. Please update the file manually.');
        }
        
        return '';
    }
545860
public static function getParamsAsArray($url)
    {
        $query = parse_url($url, PHP_URL_QUERY);
        if (!$query) {
            return [];
        }
        $params = [];
        parse_str($query, $params);

        return $params;
    }
1226215
public function getUriArguments($url): array
    {
        $uriParam = str_replace($url, '', $this->router->getCurrentUri());

        return array_filter(explode('/', $uriParam));
    }
1157509
public function initFromUrl($url)
    {
        
        $options = [
            'http' => [
                'method'=>"GET",
                'header'=>"Accept-language: en\r\n".
                "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2\r\n"
          ]
        ];
        
        $context  = stream_context_create($options);
        

        if ($data = @file_get_contents($url, false, $context)) {
            return $this->initFromBinary($data);
        }

        throw new \Intervention\Image\Exception\NotReadableException(
            "Unable to init from given url (".$url.")."
        );
    }
1012090
private function getFile($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$result = curl_exec($ch);
		curl_close($ch);

		$response = json_decode($result, TRUE);
		if (is_array($response) && count($response) > 0 && (!isset($response['status']) || $response['status'] != '404')) {
			return $response;
		}
		
		return false;
	}
145569
public function downloadFileFromURL($url)
    {
        $request     = new \Google_Http_Request($url, 'GET', null, null);
        $httpRequest = $this->service->getClient()
           ->getAuth()
           ->authenticatedRequest($request);
        if ($httpRequest->getResponseHttpCode() == 200) {
            return $httpRequest->getResponseBody();
        } else {
            // An error occurred.
            return null;
        }
    }
894718
public function fetchUrl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // HTTPS
        if (strpos($url, 'https://') !== false) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        $res = curl_exec($ch);
        curl_close($ch);

        return $res;
    }