You are here

function imagecache_external_fetch in Imagecache External 6.2

Same name and namespace in other branches
  1. 8 imagecache_external.module \imagecache_external_fetch()
  2. 7.2 imagecache_external.module \imagecache_external_fetch()
  3. 7 imagecache_external.module \imagecache_external_fetch()

Api function to fetch a url

Parameters

$url string url to fetch:

1 call to imagecache_external_fetch()
imagecache_external_generate_path in ./imagecache_external.module
Util to generate a path to an image

File

./imagecache_external.module, line 83

Code

function imagecache_external_fetch($url, $cachepath) {

  //now we test it against the whitelist/blacklist
  if (!$url) {
    return FALSE;
  }

  //extract hostname from url
  $parsed_url = parse_url($url);
  $host = $parsed_url['host'];
  $list = preg_split('/\\s+/', variable_get('imagecache_external_hosts', ''));

  // Check if the list is set as a blacklist and the host is in the list or if
  // the list is set as a whitelist and the host is not found in the list.
  // Note that this is retrospective, ie a previously downloaded image can be blocked
  if ((variable_get('imagecache_external_option', 'white') == 'black' && in_array($host, $list) || variable_get('imagecache_external_option', 'white') == 'white' && !in_array($host, $list)) && !user_access('Bypass black/white list')) {

    //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 black/white list requirements.');
    return FALSE;
  }
  if (!user_access('Fetch external images')) {
    watchdog('imagecache_external', 'The image ' . $url . ' could not be retrieved, the user does not have permission to fetch external images.');
    return FALSE;
  }
  $result = drupal_http_request($url);
  $code = floor($result->code / 100) * 100;
  $types = array(
    'image/jpeg',
    'image/png',
    'image/gif',
  );
  if ($result->data && $code != 400 && $code != 500 && in_array($result->Content - Type, $types)) {
    $src = file_save_data($result->data, $cachepath);
  }
  else {

    //if we are unsuccessful then log a message in watchdog
    watchdog('imagecache_external', 'The image ' . $url . ' could not be retrieved');
    return FALSE;
  }
  return TRUE;
}