You are here

protected static function CspSettingsForm::isValidHost in Content-Security-Policy 8

Verifies the syntax of the given URL.

Similar to UrlHelper::isValid(), except:

  • protocol is optional; can only be http/https, or ws/wss.
  • domains must have at least a top-level and secondary domain.
  • an initial subdomain wildcard is allowed
  • wildcard is allowed as port value
  • query is not allowed.

Parameters

string $url: The URL to verify.

Return value

bool TRUE if the URL is in a valid format, FALSE otherwise.

2 calls to CspSettingsForm::isValidHost()
CspSettingsForm::validateForm in src/Form/CspSettingsForm.php
Form validation handler.
HostValidator::isValidHost in tests/src/Unit/Form/CspSettingsFormTest.php
Verifies the syntax of the given URL.
1 method overrides CspSettingsForm::isValidHost()
HostValidator::isValidHost in tests/src/Unit/Form/CspSettingsFormTest.php
Verifies the syntax of the given URL.

File

src/Form/CspSettingsForm.php, line 618

Class

CspSettingsForm
Form for editing Content Security Policy module settings.

Namespace

Drupal\csp\Form

Code

protected static function isValidHost($url) {
  return (bool) preg_match("\n        /^                                                      # Start at the beginning of the text\n        (?:[a-z][a-z0-9\\-.+]+:\\/\\/)?                             # Scheme (optional)\n        (?:\n          (?:                                                   # A domain name or a IPv4 address\n            (?:\\*\\.)?                                           # Wildcard prefix (optional)\n            (?:(?:[a-z0-9\\-\\.]|%[0-9a-f]{2})+\\.)+\n            (?:[a-z0-9\\-\\.]|%[0-9a-f]{2})+\n          )\n          |(?:\\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\\])         # or a well formed IPv6 address\n          |localhost\n        )\n        (?::(?:[0-9]+|\\*))?                                     # Server port number or wildcard (optional)\n        (?:[\\/|\\?]\n          (?:[\\w#!:\\.\\+=&@\$'~*,;\\/\\(\\)\\[\\]\\-]|%[0-9a-f]{2})     # The path (optional)\n        *)?\n      \$/xi", $url);
}