Ejemplo n.º 1
0
 /**
  * Filter a URL
  *
  * Prefix the URL with "http://" if it has no scheme, then check the URL
  * for validity. You can specify what parts of the URL are allowed.
  *
  * @param string       $uriString
  * @param string[]     $allowedParts Array of allowed URL parts (optional)
  *
  * @throws InvalidUrlException If URL is invalid, the scheme is not http or
  *                             contains parts that are not expected.
  *
  * @return UriInterface Filtered URI (with default scheme if there was no scheme)
  */
 private function filterUri($uriString, array $allowedParts = [])
 {
     // Creating a PSR-7 URI without scheme (with parse_url) results in the
     // original hostname to be seen as path. So first add a scheme if none
     // is given.
     if (false === strpos($uriString, '://')) {
         $uriString = sprintf('%s://%s', 'http', $uriString);
     }
     try {
         $uri = UriFactoryDiscovery::find()->createUri($uriString);
     } catch (\InvalidArgumentException $e) {
         throw InvalidUrlException::invalidUrl($uriString);
     }
     if (!$uri->getScheme()) {
         throw InvalidUrlException::invalidUrl($uriString, 'empty scheme');
     }
     if (count($allowedParts) > 0) {
         $parts = parse_url((string) $uri);
         $diff = array_diff(array_keys($parts), $allowedParts);
         if (count($diff) > 0) {
             throw InvalidUrlException::invalidUrlParts($uriString, $allowedParts);
         }
     }
     return $uri;
 }
Ejemplo n.º 2
0
    /**
     * Filter a URL
     *
     * Prefix the URL with "http://" if it has no scheme, then check the URL
     * for validity. You can specify what parts of the URL are allowed.
     *
     * @param string   $url
     * @param string[] $allowedParts Array of allowed URL parts (optional)
     *
     * @throws InvalidUrlException If URL is invalid, the scheme is not http or
     *                             contains parts that are not expected.
     *
     * @return string The URL (with default scheme if there was no scheme)
     */
    protected function filterUrl($url, array $allowedParts = array())
    {
        // parse_url doesn’t work properly when no scheme is supplied, so
        // prefix URL with HTTP scheme if necessary.
        if (false === strpos($url, '://')) {
            $url = sprintf('%s://%s', $this->getDefaultScheme(), $url);
        }

        if (!$parts = parse_url($url)) {
            throw InvalidUrlException::invalidUrl($url);
        }
        if (empty($parts['scheme'])) {
            throw InvalidUrlException::invalidUrl($url, 'empty scheme');
        }

        if (!in_array(strtolower($parts['scheme']), $this->getAllowedSchemes())) {
            throw InvalidUrlException::invalidUrlScheme($url, $parts['scheme'], $this->getAllowedSchemes());
        }

        if (count($allowedParts) > 0) {
            $diff = array_diff(array_keys($parts), $allowedParts);
            if (count($diff) > 0) {
                throw InvalidUrlException::invalidUrlParts($url, $allowedParts);
            }
        }

        return $url;
    }