View source
<?php
function imagecache_external_menu() {
$items['external'] = array(
'title' => 'External image',
'page callback' => 'imagecache_external_image',
'access arguments' => array(
'View external images',
),
'type' => MENU_CALLBACK,
);
$items['admin/settings/imagecache_external'] = array(
'title' => 'Imagecache External',
'description' => 'Configure imagecache external',
'file' => 'imagecache_external.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'imagecache_external_admin_form',
),
'access arguments' => array(
'Administer imagecache external',
),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/imagecache_external/settings'] = array(
'title' => 'Settings',
'description' => 'Configure imagecache external',
'file' => 'imagecache_external.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'imagecache_external_admin_form',
),
'access arguments' => array(
'Administer imagecache external',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/imagecache_external/flush'] = array(
'title' => 'Flush external images',
'description' => 'Flush external images',
'file' => 'imagecache_external.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'imagecache_external_flush_form',
),
'access arguments' => array(
'Administer imagecache external',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function imagecache_external_perm() {
return array(
'View external images',
'Fetch external images',
'Administer imagecache external',
'Bypass black/white list',
);
}
function imagecache_external_image() {
$args = func_get_args();
$preset = array_shift($args);
if ($args[0] == 'http:' || $args[0] == 'https:') {
array_shift($args);
}
$url = 'http://' . implode('/', $args);
$host = $args[0];
if (!$url) {
return drupal_not_found();
}
$list = preg_split('/\\s+/', variable_get('imagecache_external_hosts', ''));
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')) {
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 {
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);
}
}