You are here

function imagecache_external_validate_host in Imagecache External 7.2

Same name and namespace in other branches
  1. 8 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

boolean Can the image be fetched or not?

2 calls to imagecache_external_validate_host()
ImagecacheExternalTestCase::testCachingExternalWhitelistValidation in ./imagecache_external.test
Test the behaviour of imagecache_external_validate_host().
imagecache_external_fetch in ./imagecache_external.module
Api function to fetch a url.

File

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

Code

function imagecache_external_validate_host($url) {

  // 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 (variable_get('imagecache_external_use_whitelist', TRUE)) {
    $list = preg_split('/\\s+/', variable_get('imagecache_external_hosts', ''));
    $validhost = FALSE;
    foreach ($list as $ext_host) {
      if (preg_match('/\\.?' . preg_quote($ext_host, '/') . '/', $host) === 1) {
        $validhost = TRUE;
        break;
      }
    }
    if (!$validhost) {

      // If we are unsuccessful then log a message in watchdog.
      watchdog('imagecache_external', 'The image %url could not be retrieved, it did not meet the whitelist requirements.', array(
        '%url' => $url,
      ));
      return FALSE;
    }
  }
  return TRUE;
}