Validate a parsed URL

@param  array      $urlConfig URL config
@param  array      $p         Parsed URL
@return string|null           Error message if invalid, or NULL
162035
protected static function validateUrl(array $urlConfig, array $p)
	{
		if ($p['scheme'] !== '' && !preg_match($urlConfig['allowedSchemes'], $p['scheme']))
		{
			return 'URL scheme is not allowed';
		}

		if ($p['host'] !== '')
		{
			/**
			* Test whether the host is valid
			* @link http://tools.ietf.org/html/rfc1035#section-2.3.1
			* @link http://tools.ietf.org/html/rfc1123#section-2
			*/
			$regexp = '/^(?!-)[-a-z0-9]{0,62}[a-z0-9](?:\\.(?!-)[-a-z0-9]{0,62}[a-z0-9])*$/i';
			if (!preg_match($regexp, $p['host']))
			{
				// If the host invalid, retest as an IPv4 and IPv6 address (IPv6 in brackets)
				if (!NetworkFilter::filterIpv4($p['host'])
				 && !NetworkFilter::filterIpv6(preg_replace('/^\\[(.*)\\]$/', '$1', $p['host'])))
				{
					return 'URL host is invalid';
				}
			}

			if ((isset($urlConfig['disallowedHosts']) && preg_match($urlConfig['disallowedHosts'], $p['host']))
			 || (isset($urlConfig['restrictedHosts']) && !preg_match($urlConfig['restrictedHosts'], $p['host'])))
			{
				return 'URL host is not allowed';
			}
		}
		elseif (preg_match('(^(?:(?:f|ht)tps?)$)', $p['scheme']))
		{
			return 'Missing host';
		}
	}
1200860
public function parseString($config)
    {
        // Handle PHP start tag
        $config = trim($config);
        if (substr($config, 0, 2) === '<?') {
            $config = '?>' . $config;
        }

        // Eval the string, if it throws an exception, rethrow it
        try {
            $data = $this->isolate($config);
        } catch (Exception $exception) {
            throw new ParseException(
                [
                    'message'   => 'PHP string threw an exception',
                    'exception' => $exception,
                ]
            );
        }

        // Complete parsing
        return (array)$this->parse($data);
    }
1283005
private function _p() {
		$c = $this->_c; /** @var C $c */
		$c->setHeaders($this->headers());
		$c->setUri($this->url());
		try {
			$res = $c->request(); /** @var \Zend_Http_Response $res */
			if (!($resBody = $res->getBody()) && $res->isError()) { /** @var string $resBody */
				throw new eHTTP($res);
			}
			else {
				/** @var mixed|null $r */
				// 2017-08-08
				// «No Content»
				// «The server has successfully fulfilled the request
				// and that there is no additional content to send in the response payload body»
				// https://httpstatuses.com/204
				if (!$resBody && 204 === $res->getStatus()) {
					$r = null;
				}
				else {
					$r = $this->_filtersResBV->filter($resBody);
					if ($validatorC = $this->responseValidatorC() /** @var string $validatorC */) {
						$validator = new $validatorC($r); /** @var Validator $validator */
						// 2019-01-12
						// I have added `$res->isError() ||` today
						// because a 4xx or a 5xx HTTP code clearly indicates an error.
						// I have use this feature in the Inkifi_Mediaclip module.
						if ($res->isError() || !$validator->valid()) {
							throw $validator;
						}
					}
					$r = $this->_filtersResAV->filter($r);
				}
			}
			if ($this->logging()) {
				$req = df_zf_http_last_req($c); /** @var string $req */
				/** @var string $m */ /** @var string $title */
				$title = df_api_name($m = df_module_name($this));
				$path = df_url_path($this->url()); /** @var string $path */
				df_log_l($m,
					(!$path ? 'A' : "A `{$path}`") . " {$title} API request has succeeded\n"
					. "Request:\n$req\nResponse:\n"
					. (!df_check_json($resBody) ? $resBody : df_json_prettify($resBody))
				);
			}
		}
		catch (\Exception $e) {
			/** @var string $long */ /** @var string $short */
			list($long, $short) = $e instanceof E ? [$e->long(), $e->short()] : [null, df_ets($e)];
			$req = df_zf_http_last_req($c); /** @var string $req */
			$title = df_api_name($m = df_module_name($this)); /** @var string $m */ /** @var string $title */
			$path = df_url_path($this->url()); /** @var string $path */
			$ex = df_error_create(
				(!$path ? 'A' : "A `{$path}`")
				. " {$title} API request has failed"
				. ($short ? ": «{$short}»" : ' without error messages') . ".\n"
				. ($long === $short ? "Request:\n$req" : df_cc_kv([
					'The full error description' => $long, 'Request' => $req
				]))
			); /** @var DFE $ex */
			if (!$this->_silent) {
				df_log_l($m, $ex);
				df_sentry($m, $short, ['extra' => ['Request' => $req, 'Response' => $long]]);
			}
			throw $ex;
		}
		return $r;
	}
310220
public function _validate_config($config)
	{
		$validated = array();

		foreach ($config as $name => $item)
		{
			if ($name == 'memcached' and is_array($item))
			{
				foreach ($item as $name => $value)
				{
					switch ($name)
					{
						case 'cookie_name':
							if ( empty($value) or ! is_string($value))
							{
								$value = 'fuelmid';
							}
						break;

						case 'servers':
							// do we have a servers config
							if ( empty($value) or ! is_array($value))
							{
								$value = array('default' => array('host' => '127.0.0.1', 'port' => '11211'));
							}

							// validate the servers
							foreach ($value as $key => $server)
							{
								// do we have a host?
								if ( ! isset($server['host']) or ! is_string($server['host']))
								{
									throw new \FuelException('Invalid Memcached server definition in the session configuration.');
								}
								// do we have a port number?
								if ( ! isset($server['port']) or ! is_numeric($server['port']) or $server['port'] < 1025 or $server['port'] > 65535)
								{
									throw new \FuelException('Invalid Memcached server definition in the session configuration.');
								}
								// do we have a relative server weight?
								if ( ! isset($server['weight']) or ! is_numeric($server['weight']) or $server['weight'] < 0)
								{
									// set a default
									$value[$key]['weight'] = 0;
								}
							}
						break;

						default:
							// unknown property
							continue;
					}

					$validated[$name] = $value;
				}
			}
			else
			{
				// skip all config array properties
				if (is_array($item))
				{
					continue;
				}

				// global config, was validated in the driver
				$validated[$name] = $item;
			}

		}

		// validate all global settings as well
		return parent::_validate_config($validated);
	}
672192
public static function parse(string $url, string $default_scheme = null)
    {
        if (substr($url, 0, 4) === "www." && !empty($default_scheme))
            $url = $default_scheme . '://' . $url;
        elseif (substr($url, 0, 2) === '//' && !empty($_SERVER['REQUEST_SCHEME']))
            $url = $_SERVER['REQUEST_SCHEME'] . '://' . substr($url, 2);

        $parts = parse_url($url);

        if ($parts === false)
            throw new URLException("Invalid URL: " . $url);

        $scheme   = $parts['scheme'] ?? null;
        $username = $parts['user'] ?? null;
        $password = $parts['pass'] ?? null;
        $host     = $parts['host'] ?? null;
        $port     = $parts['port'] ?? null;
        $path     = $parts['path'] ?? '/';
        $query    = $parts['query'] ?? null;
        $fragment = $parts['fragment'] ?? null;

        $query = self::parseQuery($query);

        // Scheme is case insensitive, to normalize to lowercase
        if ($scheme !== null)
            $scheme = strtolower($scheme);

        // Hostnames are case insensitive, to normalize to lowercase
        if ($host !== null)
            $host = strtolower($host);

        if (!empty($scheme) && !in_array($scheme, array('http', 'https', 'ftp')))
            throw new URLException("Unsupported scheme: '" . $scheme . "'");

        return array(
            'scheme' => $scheme,
            'username' => $username,
            'password' => $password,
            'host' => $host,
            'port' => $port,
            'path' => $path,
            'query' => $query,
            'fragment' => $fragment
        );
    }
574450
public function validate(\IPkgShopOrderPaymentConfig $config)
    {
        $required = array(
            'merchantId',
            'accessKey',
            'applicationName',
            'applicationVersion',
            'region',
            'payWithAmazonButtonURL',
        );
        foreach ($required as $field) {
            if (null === $config->getValue($field, null)) {
                throw new \InvalidArgumentException("required parameter {$field} missing");
            }
        }
        $region = $config->getValue('region');
        $allowedRegions = array(
            AmazonPaymentGroupConfig::REGION_DE,
            AmazonPaymentGroupConfig::REGION_NA,
            AmazonPaymentGroupConfig::REGION_UK,
            AmazonPaymentGroupConfig::REGION_US,
        );
        if (false === in_array($region, $allowedRegions)) {
            throw new \InvalidArgumentException("{$region} is not a supported region");
        }

        $urls = array('serviceURL', 'widgetURL', 'payWithAmazonButtonURL');
        foreach ($urls as $url) {
            if (false === \TCMSUserInput::FilterValue($url, \TCMSUserInput::FILTER_URL)) {
                throw new \InvalidArgumentException("{$url} is not a valid url");
            }
        }
    }
198734
protected function verifyUrl(array $urlParameters = array())
    {
        $expectedUrl = $this->getUrl($urlParameters);
        $currentUrl = $this->getDriver()->getCurrentUrl();

        if ($currentUrl !== $expectedUrl) {
            throw new UnexpectedPageException(sprintf('Expected to be on "%s" but found "%s" instead', $expectedUrl, $currentUrl));
        }
    }
37885
public static final String[] parseURLs(Tree config, String name, String[] defaultURLs) {
		Tree urlNode = config.get(name);
		List<String> urlList;
		if (urlNode == null) {
			return defaultURLs;
		} else if (urlNode.isPrimitive()) {
			urlList = new ArrayList<>();
			String[] urls = urlNode.asString().split(",");
			for (String url : urls) {
				url = url.trim();
				if (!url.isEmpty()) {
					urlList.add(url);
				}
			}
		} else if (urlNode.isEnumeration()) {
			urlList = urlNode.asList(String.class);
		} else {
			return defaultURLs;
		}
		if (urlList.isEmpty()) {
			return defaultURLs;
		}
		String[] urls = new String[urlList.size()];
		urlList.toArray(urls);
		return urls;
	}
1084128
function getURLParameter(url, name) {
        var URLParamRegEx = new RegExp('[?|&]?' + name + '=([^&;]+?)(&|#|;|$)');
        var value = URLParamRegEx.exec(url);
        value = (value) ? value[1] : "";
        return decodeURIComponent(value.replace(/\+/g, '%20')) || null;
    }
1247233
private function parseConfig(\DOMDocument $config) {

		try {

			// determine server context, missing SERVER_ADDR assumes localhost/CLI

			$this->isLocalhost = !isset($_SERVER['SERVER_ADDR']) || !!preg_match('/^(?:127|192|1|0)(?:\.\d{1,3}){3}$/', $_SERVER['SERVER_ADDR']);

			$rootNode = $config->firstChild;

			// allow parsing of specific sections

			foreach($rootNode->childNodes as $section) {

				if($section->nodeType !== XML_ELEMENT_NODE) {

					continue;

				}

				$sectionName = $section->nodeName;

				if(empty($this->sections) || in_array($sectionName, $this->sections)) {

					$methodName =
						'parse' .
						ucfirst(preg_replace_callback('/_([a-z])/', function($match) { return strtoupper($match[1]); }, $sectionName)) .
						'Settings'
					;

					if(method_exists($this, $methodName)) {

						call_user_func([$this, $methodName], $section);

					}
				}

			}
		}

		catch(ConfigException $e) {

			throw $e;

		}

	}
32876
private function validateUrl($url)
    {
        // Check for feed URL
        $url = trim($url);
        if ('feed://' === strtolower(substr($url, 0, 7))) {
            $url = 'http://' . substr($url, 7);
        }

        if (!preg_match('!^https?://.+!i', $url)) {
            $url = 'http://' . $url;
        }

        // explode url to convert accents
        $parsedUrl = parse_url($url);

        if (false === $parsedUrl) {
            throw new \Exception(sprintf('Url "%s" is not valid.', $url));
        }

        if (isset($parsedUrl['host']) && preg_match('/[\x80-\xff]/', $parsedUrl['host'])) {
            $parsedUrl['host'] = $this->punycode->encode($parsedUrl['host']);
        }

        if (isset($parsedUrl['path']) && preg_match('/[\x80-\xff]/', $parsedUrl['path'])) {
            $path = [];
            foreach (explode('/', $parsedUrl['path']) as $value) {
                $path[] = urlencode($value);
            }
            $parsedUrl['path'] = implode('/', $path);
        }

        // everything should be converted, rebuild the final url
        $url = $this->unparseUrl($parsedUrl);

        if (false === filter_var($url, FILTER_VALIDATE_URL)) {
            throw new \Exception(sprintf('Url "%s" is not valid.', $url));
        }

        $url = filter_var($url, FILTER_SANITIZE_URL);

        if (false === $this->isUrlAllowed($url)) {
            throw new \Exception(sprintf('Url "%s" is not allowed to be parsed.', $url));
        }

        return $url;
    }
211860
private function _validateUrl($url)
    {
        $parsed = parse_url($url);
        if (empty($parsed['scheme'])
            || empty($parsed['host'])
            || $parsed['scheme'] !== 'https'
            || substr($url, -4) !== '.pem'
            || !preg_match($this->defaultHostPattern, $parsed['host'])
        ) {
            throw new OffAmazonPaymentsNotifications_InvalidMessageException(
                'The certificate is located on an invalid domain.'
            );
        }
    }
348848
private static function httpParseQuery(string $queryString, string $argSeparator = '&', int $decType = PHP_QUERY_RFC1738): array
    {
        $result = [];
        $parts = explode($argSeparator, $queryString);

        foreach ($parts as $part) {
            list($paramName, $paramValue) = explode('=', $part, 2);

            switch ($decType) {
                case PHP_QUERY_RFC3986:
                    $paramName = rawurldecode($paramName);
                    $paramValue = rawurldecode($paramValue);
                    break;
                case PHP_QUERY_RFC1738:
                default:
                    $paramName = urldecode($paramName);
                    $paramValue = urldecode($paramValue);
                    break;
            }

            if (preg_match_all('/\[([^\]]*)\]/m', $paramName, $matches)) {
                $paramName = substr($paramName, 0, strpos($paramName, '['));
                $keys = array_merge([$paramName], $matches[1]);
            } else {
                $keys = [$paramName];
            }

            $target = &$result;

            foreach ($keys as $index) {
                if ('' === $index) {
                    if (is_array($target)) {
                        $intKeys = array_filter(array_keys($target), 'is_int');
                        $index = count($intKeys) ? max($intKeys) + 1 : 0;
                    } else {
                        $target = [$target];
                        $index = 1;
                    }
                } elseif (isset($target[$index]) && !is_array($target[$index])) {
                    $target[$index] = [$target[$index]];
                }

                $target = &$target[$index];
            }

            if (is_array($target)) {
                $target[] = $paramValue;
            } else {
                $target = $paramValue;
            }
        }

        return $result;
    }
1365500
public void validateConfiguration(String configuration) {

        try {
            JSONObject json = new JSONObject(configuration);
            String className = json.optString(CmsDataViewConstants.CONFIG_VIEW_CLASS);

            Class<?> cls = Class.forName(className, false, getClass().getClassLoader());
            if (!I_CmsDataView.class.isAssignableFrom(cls)) {
                throw new IllegalArgumentException(
                    "Class " + cls.getName() + " does not implement " + I_CmsDataView.class.getName());
            }
        } catch (Exception e) {
            throw new CmsWidgetConfigurationException(e);
        }

    }
1237437
public static function validateURL($string)
    {
        if (filter_var($string, FILTER_VALIDATE_URL)) {
            return (string)$string;
        }

        throw new InvalidArgumentException(
            sprintf(
                'Provided string "%1$s" was not a valid URL.',
                $string
            )
        );
    }
1116539
public static function filter($attrValue, array $urlConfig, Logger $logger = null)
	{
		/**
		* Trim the URL to conform with HTML5 then parse it
		* @link http://dev.w3.org/html5/spec/links.html#attr-hyperlink-href
		*/
		$p = self::parseUrl(trim($attrValue));

		$error = self::validateUrl($urlConfig, $p);
		if (!empty($error))
		{
			if (isset($logger))
			{
				$p['attrValue'] = $attrValue;
				$logger->err($error, $p);
			}

			return false;
		}

		return self::rebuildUrl($p);
	}
1244503
private function validateUrl( $urlParts ) {
        if ( !is_array( $urlParts ) ) {
            throw new \InvalidArgumentException( 'Invalid serverUrl has been provided' );
        }

        if ( !isset( $urlParts["scheme"] ) ) {
            throw new \InvalidArgumentException( 'serverUrl has been provided without a valid scheme (http:// or https://)' );
        }

        if ( !isset( $urlParts["host"] ) ) {
            throw new \InvalidArgumentException( 'Invalid serverUrl has been provided' );
        }
    }