View source
<?php
define('IMAGEINFO_CACHE_LIFETIME', 60 * 60 * 72);
define('IMAGEINFO_CACHE_THEME_IMAGECACHE_CALLBACK', 'theme_imagecache');
define('IMAGEINFO_CACHE_THEME_IMAGEFIELD_IMAGE_CALLBACK', 'theme_imagefield_image');
define('IMAGEINFO_CACHE_THEME_IMAGECACHE', TRUE);
define('IMAGEINFO_CACHE_THEME_IMAGEFIELD_IMAGE', TRUE);
define('IMAGEINFO_CACHE_IMAGECACHE_PREGENERATE', TRUE);
define('IMAGEINFO_CACHE_ASYNC_MAX', 5);
define('IMAGEINFO_CACHE_HTTPRL_MODE', 0);
define('IMAGEINFO_CACHE_CCK_WIDGET', TRUE);
function imageinfo_cache_form_alter(&$form, $form_state, $form_id) {
if (empty($form['basic']['type']['#value']) || $form['basic']['type']['#value'] != 'filefield' || empty($form['widget_module']['#value']) || $form['widget_module']['#value'] != 'imagefield') {
return;
}
module_load_include('inc', 'imageinfo_cache', 'imageinfo_cache.admin');
return imageinfo_cache_cck_widget_form($form, $form_state, $form_id);
}
function imageinfo_cache_menu() {
$items = array();
$items['imageinfo_cache_generate'] = array(
'page callback' => 'imageinfo_cache_primer',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
$items['admin/settings/imageinfo-cache'] = array(
'title' => 'Imageinfo Cache',
'description' => 'Configuration page for Imageinfo Cache module.',
'page callback' => 'imageinfo_cache_admin_page',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'imageinfo_cache.admin.inc',
);
return $items;
}
function imageinfo_cache_file_insert($file) {
$op = 'insert';
if (imageinfo_cache_check_file($file, $op)) {
imageinfo_cache_shutdown_async($file, $op, 'widget');
}
}
function imageinfo_cache_file_delete($file) {
$op = 'delete';
if (imageinfo_cache_check_file($file, $op)) {
imageinfo_cache_shutdown_async($file, $op, 'widget');
}
}
function imageinfo_cache_nodeapi(&$node, $op) {
if ($op == 'insert' || $op == 'update') {
$op = 'insert';
$files = imageinfo_cache_get_images_in_node($node);
foreach ($files as $file) {
$file = (object) $file;
if (imageinfo_cache_check_file($file, $op)) {
imageinfo_cache_shutdown_async($file, $op, 'nodeapi');
}
}
}
}
function imageinfo_cache_shutdown_async($file = NULL, $op = NULL, $state = NULL) {
global $base_path, $base_root;
static $files = array();
static $registered = FALSE;
static $array_counter = 0;
static $file_counter = 0;
if (!empty($file) && !empty($op)) {
if (imageinfo_cache_check_file($file, $op) == FALSE) {
return;
}
$missing = FALSE;
$cid = 'theme_imagefield_' . md5($file->filepath);
$cache = cache_get($cid, 'cache_imageinfo');
if (empty($cache)) {
$missing = TRUE;
}
$file_info = array(
'fid' => $file->fid,
'filepath' => $file->filepath,
'timestamp' => $file->timestamp,
'op' => $op,
'state' => $state,
);
if (!empty($file->field['type_name'])) {
$file_info['field']['type_name'] = $file->field['type_name'];
}
if (!empty($file->field['field_name'])) {
$file_info['field']['field_name'] = $file->field['field_name'];
}
if (!$missing && module_exists('imagecache') && function_exists('imagecache_generate_image') && variable_get('imageinfo_cache_imagecache_pregenerate', IMAGEINFO_CACHE_IMAGECACHE_PREGENERATE)) {
foreach (imageinfo_cache_presets_to_generate($file_info) as $preset) {
$cid = 'imagecache_' . $preset . '_' . md5($file->filepath);
$cache = cache_get($cid, 'cache_imageinfo');
if (empty($cache)) {
$missing = TRUE;
break;
}
}
}
if (!$missing && $op == 'insert') {
return;
}
$file_counter++;
if ($file_counter > (int) variable_get('imageinfo_cache_async_max', IMAGEINFO_CACHE_ASYNC_MAX)) {
$array_counter++;
$file_counter = 0;
}
$files[$array_counter][$file->fid] = $file_info;
if (!$registered) {
register_shutdown_function(__FUNCTION__);
$registered = TRUE;
}
return;
}
if (empty($files)) {
return;
}
$key = variable_get('imageinfo_cache_url_key', md5(drupal_get_private_key()));
foreach ($files as $values) {
$query['files'] = $values;
$query['key'] = $key;
$query_string = http_build_query($query, '', '&');
$ip = variable_get('imageinfo_cache_server_addr', FALSE);
if (!empty($ip)) {
$url = 'http://' . $ip . $base_path . 'imageinfo_cache_generate';
}
else {
$url = httprl_build_url_self('imageinfo_cache_generate');
}
$headers = array(
'Host' => $_SERVER['HTTP_HOST'],
'Content-Type' => 'application/x-www-form-urlencoded',
);
$options = array(
'headers' => $headers,
'method' => 'POST',
'data' => $query_string,
'blocking' => variable_get('imageinfo_cache_httprl_mode', IMAGEINFO_CACHE_HTTPRL_MODE),
);
httprl_request($url, $options);
}
$resposes = httprl_send_request();
if (variable_get('imageinfo_cache_httprl_mode', IMAGEINFO_CACHE_HTTPRL_MODE)) {
foreach ($resposes as $url => $info) {
if (strcmp(trim($info->data), $key) !== 0) {
watchdog('imageinfo_cache', 'Asynchronous imageinfo cache primer failed %url. Using Synchronous mode. %key <br /> !data', array(
'%url' => $url,
'%key' => $key . ' ' . $info->data . ' ' . strcmp(trim($info->data), $key),
'!data' => print_r($info, TRUE),
));
imageinfo_cache_primer($query);
}
}
}
}
function imageinfo_cache_theme_registry_alter(&$theme_registry) {
if (variable_get('imageinfo_cache_theme_imagecache', IMAGEINFO_CACHE_THEME_IMAGECACHE) && isset($theme_registry['imagecache'])) {
variable_set('imageinfo_cache_theme_imagecache_callback', $theme_registry['imagecache']['function']);
$theme_registry['imagecache']['function'] = 'imageinfo_cache_theme_imagecache';
}
if (variable_get('imageinfo_cache_theme_imagefield_image', IMAGEINFO_CACHE_THEME_IMAGEFIELD_IMAGE) && isset($theme_registry['imagefield_image'])) {
variable_set('imageinfo_cache_theme_imagefield_image_callback', $theme_registry['imagefield_image']['function']);
$theme_registry['imagefield_image']['function'] = 'imageinfo_cache_theme_imagefield_image';
}
}
function imageinfo_cache_theme_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE, $absolute = TRUE) {
if ($getsize) {
$imagecache_path = imagecache_create_path($presetname, $path);
if ($imagecache_path) {
$cid = 'imagecache_' . $presetname . '_' . md5($path);
$image = cache_get($cid, 'cache_imageinfo');
if (!empty($image)) {
$image = $image->data;
if (empty($image) || empty($image['width']) || empty($image['height'])) {
unset($image);
}
if ($image['width'] == 1 || $image['height'] == 1) {
unset($image);
}
}
if (empty($image)) {
$image = image_get_info($imagecache_path);
if (!empty($image) && !empty($image['width']) && !empty($image['height'])) {
$lifetime = variable_get('imageinfo_cache_lifetime', IMAGEINFO_CACHE_LIFETIME);
cache_set($cid, $image, 'cache_imageinfo', time() + $lifetime);
}
}
if (!empty($image) && !empty($image['width']) && !empty($image['height'])) {
if (is_null($attributes)) {
$attributes = array(
'class' => 'imagecache imagecache-' . $presetname,
);
}
$attributes['width'] = $image['width'];
$attributes['height'] = $image['height'];
$getsize = FALSE;
}
}
}
$function = variable_get('imageinfo_cache_theme_imagecache_callback', IMAGEINFO_CACHE_THEME_IMAGECACHE_CALLBACK);
return $function($presetname, $path, $alt, $title, $attributes, $getsize, $absolute);
}
function imageinfo_cache_theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
$file = (array) $file;
if ($getsize) {
if (!empty($file['data']['width']) && !empty($file['data']['height'])) {
$attributes['width'] = $file['data']['width'];
$attributes['height'] = $file['data']['height'];
$getsize = FALSE;
}
else {
$cid = 'theme_imagefield_' . md5($file['filepath']);
$image = cache_get($cid, 'cache_imageinfo');
if (!empty($image)) {
$image = $image->data;
if (empty($image) || empty($image['width']) || empty($image['height'])) {
unset($image);
}
if ($image['width'] == 1 || $image['height'] == 1) {
unset($image);
}
}
if (empty($image)) {
$image = @getimagesize($file['filepath']);
if (!empty($image)) {
$image['width'] = $image[0];
$image['height'] = $image[1];
$lifetime = variable_get('imageinfo_cache_lifetime', IMAGEINFO_CACHE_LIFETIME);
cache_set($cid, $image, 'cache_imageinfo', time() + $lifetime);
}
}
if (!empty($image) && !empty($image['width']) && !empty($image['height'])) {
$attributes['width'] = $image['width'];
$attributes['height'] = $image['height'];
$getsize = FALSE;
}
}
}
$function = variable_get('imageinfo_cache_theme_imagefield_image_callback', IMAGEINFO_CACHE_THEME_IMAGEFIELD_IMAGE_CALLBACK);
return $function($file, $alt, $title, $attributes, $getsize);
}
function imageinfo_cache_primer($values = NULL) {
$async = FALSE;
if (is_null($values)) {
if (empty($_POST['key']) || empty($_POST['files'])) {
return;
}
$key = variable_get('imageinfo_cache_url_key', md5(drupal_get_private_key()));
if ($key != $_POST['key']) {
return;
}
else {
httprl_background_processing(check_plain($_POST['key']));
$async = TRUE;
}
$values = array();
$values['files'] = $_POST['files'];
}
foreach ($values['files'] as $file) {
$file = (object) $file;
if (empty($file->fid) || !is_numeric($file->fid) || empty($file->filepath)) {
continue;
}
if ($file->op == 'insert') {
$node_types = node_get_types();
if (!empty($file->field['type_name']) && empty($node_types[$file->field['type_name']])) {
continue;
}
if (module_exists('content')) {
$content_types = content_types();
if (!empty($file->field['field_name']) && empty($content_types[$file->field['type_name']]['fields'][$file->field['field_name']])) {
continue;
}
}
if (module_exists('filefield_paths') && !empty($file->field['field_name']) && !empty($file->field['field_name']) && $file->state == 'widget' && db_result(db_query("SELECT TRUE FROM {filefield_paths} WHERE type = '%s' AND field = '%s'", $file->field['type_name'], $file->field['field_name']))) {
continue;
}
theme('imagefield_image', (array) $file);
if (module_exists('imagecache') && function_exists('imagecache_generate_image') && variable_get('imageinfo_cache_imagecache_pregenerate', IMAGEINFO_CACHE_IMAGECACHE_PREGENERATE)) {
foreach (imageinfo_cache_presets_to_generate($file) as $preset) {
imagecache_generate_image($preset, $file->filepath);
theme('imagecache', $preset, $file->filepath);
}
}
}
elseif ($file->op == 'delete') {
$cid = 'theme_imagefield_' . md5($file->filepath);
cache_clear_all($cid, 'cache_imageinfo');
if (module_exists('imagecache') && function_exists('imagecache_generate_image')) {
imagecache_image_flush($file->filepath);
foreach (imageinfo_cache_presets_to_generate() as $preset) {
$cid = 'imagecache_' . $preset . '_' . md5($file->filepath);
cache_clear_all($cid, 'cache_imageinfo');
}
}
}
else {
watchdog('imageinfo_cache', 'No operation was matched. ' . $file->op . print_r($file, TRUE));
}
}
if ($async) {
exit;
}
else {
return;
}
}
function imageinfo_cache_presets_to_generate($file = NULL) {
$presets = array();
$all = TRUE;
if (!is_null($file) && is_array($file)) {
$file = (object) $file;
}
if (!is_null($file) && !empty($file->field['type_name']) && !empty($file->field['field_name'])) {
$field_settings = variable_get('imageinfo_cache_cck_widget_' . $file->field['type_name'] . '_' . $file->field['field_name'], IMAGEINFO_CACHE_CCK_WIDGET);
if (empty($field_settings)) {
return array();
}
$all = FALSE;
if (!is_array($field_settings) && $field_settings == TRUE) {
$all = TRUE;
}
if (!$all) {
foreach (imagecache_presets() as $preset) {
if (!empty($field_settings[$preset['presetid']])) {
$presets[] = $preset['presetname'];
}
}
}
}
if ($all) {
foreach (imagecache_presets() as $preset) {
$presets[] = $preset['presetname'];
}
}
return $presets;
}
function imageinfo_cache_imagecache_preset_flush($presetdir, $preset) {
$cid = 'imagecache_' . $preset['presetname'];
cache_clear_all($cid, 'cache_imageinfo', TRUE);
}
function imageinfo_cache_imagecache_image_flush($filepath, $preset, $path) {
$cid = 'imagecache_' . $preset['presetname'] . md5($path);
cache_clear_all($cid, 'cache_imageinfo');
}
function imageinfo_cache_check_file($file, $op) {
if ($op == 'insert') {
if (is_object($file) && !empty($file->fid) && !empty($file->field['type_name']) && !empty($file->field['field_name']) && !empty($file->filepath) && strpos($file->filemime, 'image') !== FALSE) {
return TRUE;
}
else {
return FALSE;
}
}
elseif ($op == 'delete') {
if (is_object($file) && !empty($file->fid) && !empty($file->filepath) && strpos($file->filemime, 'image') !== FALSE) {
return TRUE;
}
else {
return FALSE;
}
}
}
function imageinfo_cache_get_node_files($node) {
$fields = filefield_get_field_list($node->type);
$files = array();
foreach ($fields as $field) {
$db_info = content_database_info($field);
$fields = 'f.*';
$fields .= ', c.' . $db_info['columns']['list']['column'] . ' AS list';
$fields .= ', c.' . $db_info['columns']['data']['column'] . ' AS data';
$sql = 'SELECT ' . $fields . ' FROM {files} f INNER JOIN {' . $db_info['table'] . '} c ON f.fid = c.' . $db_info['columns']['fid']['column'] . ' AND c.vid = %d';
$result = db_query($sql, $node->vid);
while ($file = db_fetch_array($result)) {
$file['data'] = unserialize($file['data']);
$file['field']['field_name'] = $field['field_name'];
$file['field']['type_name'] = $field['type_name'];
$files[$file['fid']] = $file;
}
}
return $files;
}
function imageinfo_cache_get_images_in_node(&$node) {
$files = array();
if (module_exists('filefield')) {
$data = imageinfo_cache_get_node_files($node);
foreach ($data as $key => $value) {
if (strpos($value['filemime'], 'image') !== FALSE) {
$files[$key] = $value;
}
}
}
return $files;
}