View source
<?php
function imagecache_external_menu() {
$items['admin/config/media/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/config/media/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/config/media/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_permission() {
return array(
'View external images' => array(
'title' => t('View external images'),
'description' => t('TODO Add a description for \'View external images\''),
),
'Fetch external images' => array(
'title' => t('Fetch external images'),
'description' => t('TODO Add a description for \'Fetch external images\''),
),
'Administer imagecache external' => array(
'title' => t('Administer imagecache external'),
'description' => t('TODO Add a description for \'Administer imagecache external\''),
),
'Bypass black/white list' => array(
'title' => t('Bypass black/white list'),
'description' => t('TODO Add a description for \'Bypass black/white list\''),
),
);
}
function imagecache_external_theme() {
return array(
'imagecache_external' => array(
'variables' => array(
'style_name' => NULL,
'path' => NULL,
'alt' => '',
'title' => NULL,
'attributes' => array(),
),
),
);
}
function theme_imagecache_external($variables) {
if ($variables['path'] = imagecache_external_generate_path($variables['path'], $variables['style_name'])) {
return theme('image_style', $variables);
}
}
function imagecache_external_generate_path($url, $preset) {
$hash = md5($url);
$scheme = file_default_scheme();
$directory = $scheme . '://imagecache/' . $preset . '/externals/' . $hash;
if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
$file = imagecache_external_fetch($url, $directory);
if ($file) {
return $file->uri;
}
}
return FALSE;
}
function imagecache_external_fetch($url, $cachepath) {
if (!$url) {
return FALSE;
}
$parsed_url = parse_url($url);
$host = $parsed_url['host'];
$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 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->headers['content-type'], $types)) {
return file_save_data($result->data, $cachepath, FILE_EXISTS_REPLACE);
}
else {
watchdog('imagecache_external', 'The image ' . $url . ' could not be retrieved');
return FALSE;
}
}