You are here

function imagecache_external_image in Imagecache External 6

Menu callback does the heavy lifting

1 string reference to 'imagecache_external_image'
imagecache_external_menu in ./imagecache_external.module
Implementation of hook_menu().

File

./imagecache_external.module, line 65

Code

function imagecache_external_image() {
  $args = func_get_args();
  $preset = array_shift($args);

  // pop the http:
  if ($args[0] == 'http:' || $args[0] == 'https:') {
    array_shift($args);
  }
  $url = 'http://' . implode('/', $args);
  $host = $args[0];

  //the host comes first

  //now we test it against the whitelist/blacklist
  if (!$url) {
    return drupal_not_found();
  }
  $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 drupal_access_denied();
  }
  $dir = file_create_path('externals');
  if (!file_check_directory($dir)) {
    mkdir($dir, 0775, FALSE);
  }
  $hash = md5($url);
  $cachepath = file_create_path('externals/' . $hash);
  if (!is_file($cachepath)) {
    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 drupal_access_denied();
    }
    $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);
      $transfer = TRUE;
    }
    else {

      //if we are unsuccessful then log a message in watchdog
      watchdog('imagecache_external', 'The image ' . $url . ' could not be retrieved');
      return drupal_not_found();
    }
  }
  else {
    $transfer = TRUE;
  }
  if ($transfer) {
    $args = explode('/', file_directory_path());
    array_push($args, 'externals', $hash);
    array_unshift($args, $preset);
    return call_user_func_array('imagecache_cache', $args);
  }
}