You are here

function imagecache_external_validate_host in Imagecache External 8

Same name and namespace in other branches
  1. 7.2 imagecache_external.module \imagecache_external_validate_host()

Helper function to validate the image host against the whitelist.

Parameters

string $url: The URL of the image.

Return value

bool Can the image be fetched or not?

1 call to imagecache_external_validate_host()
imagecache_external_fetch in ./imagecache_external.module
Api function to fetch a url.

File

./imagecache_external.module, line 246
Allows the usage of Image Styles on external images.

Code

function imagecache_external_validate_host($url) {
  $config = imagecache_external_config();

  // Extract the hostname from the url.
  if (!($host = parse_url($url, PHP_URL_HOST))) {
    return FALSE;
  }

  // Check if a whitelist is used and if the host is in the list.
  if ($config
    ->get('imagecache_external_use_whitelist')) {
    $list = preg_split('/\\s+/', $config
      ->get('imagecache_external_hosts'));
    $validhost = FALSE;
    foreach ($list as $ext_host) {
      if (preg_match('/\\.?' . $ext_host . '/', $host) == 1) {
        $validhost = TRUE;
        break;
      }
    }
    if (!$validhost) {

      // If we are unsuccessful then log a message in watchdog.
      \Drupal::logger('imagecache_external')
        ->notice(t('The image %url could not be retrieved, it did not meet the whitelist requirements.', [
        '%url' => $url,
      ]));
      return FALSE;
    }
  }
  return TRUE;
}