You are here

public function OembedProviderForm::urlIsValid in oEmbed Providers 1.1.x

Same name and namespace in other branches
  1. 2.x src/OembedProviderForm.php \Drupal\oembed_providers\OembedProviderForm::urlIsValid()

Verifies the syntax of the given absolute URL.

This method is adapted from Drupal\Component\Utility\UrlHelper::isValid().

Parameters

string $url: The URL to verify.

Return value

bool Whether or not the URL is valid.

See also

\Drupal\Component\Utility\UrlHelper::isValid()

1 call to OembedProviderForm::urlIsValid()
OembedProviderForm::validateForm in src/OembedProviderForm.php
Form validation handler.

File

src/OembedProviderForm.php, line 436

Class

OembedProviderForm
Form controller for the oEmbed provider edit/add forms.

Namespace

Drupal\oembed_providers

Code

public function urlIsValid($url) {

  // If parse_url() can't parse it, then reject.
  if (!($parsed_url = parse_url($url))) {
    return FALSE;
  }

  // Only HTTP and HTTPS are valid schemes.
  if (!in_array($parsed_url['scheme'], [
    'http',
    'https',
  ])) {
    return FALSE;
  }

  // User, password, and fragments are not valid.
  if (isset($parsed_url['user']) || isset($parsed_url['pass']) || isset($parsed_url['fragment'])) {
    return FALSE;
  }

  // Valid the host, which may contain an optional wildcard subdomain.
  // *.example.com is valid.
  // *.*.example.com is invalid.
  // *.com is invalid.
  $host = (bool) preg_match("\n      /^\n      (?:\n        (?:([*\\.]?)([a-z0-9\\-\\.]{2,})(\\.)([a-z0-9\\-\\.]+)|%[0-9a-f]{2})+\n        |(?:\\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\\])\n      )\n    \$/xi", $parsed_url['host']);
  if (!$host) {
    return FALSE;
  }
  return TRUE;
}