You are here

imagecache_external.module in Imagecache External 6.2

File

imagecache_external.module
View source
<?php

/*
 * @file imagecache_external.module
 * Allows imagecache to operate on external images
 * @copyright Copyright(c) 2010 Lee Rowlands
 * @license GPL v2 http://www.fsf.org/licensing/licenses/gpl.html 
 * @author Lee Rowlands contact at rowlandsgroup dot com
 * 
 */

/**
 * Implementation of hook_menu().
 */
function imagecache_external_menu() {

  // more complex menu item
  $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;
}

/**
 * Implementation of hook_perm().
 */
function imagecache_external_perm() {
  return array(
    'View external images',
    'Fetch external images',
    'Administer imagecache external',
    'Bypass black/white list',
  );
}

/**
 * Util to generate a path to an image
 * @param $url string the url to the image
 * @param $preset imagecache preset
 * @return string the url to the image
*/
function imagecache_external_generate_path($url, $preset) {
  $hash = md5($url);
  $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)) {
    $result = imagecache_external_fetch($url, $cachepath);
    if (!$result) {

      //we couldn't get the file
      return FALSE;
    }
  }

  //we now have the file, return url relative to the imagecache preset
  return url(file_directory_path() . '/imagecache/' . $preset . '/externals/' . $hash);
}

/**
 * Api function to fetch a url
 * @param $url string url to fetch
*/
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;
}

Functions

Namesort descending Description
imagecache_external_fetch Api function to fetch a url
imagecache_external_generate_path Util to generate a path to an image
imagecache_external_menu Implementation of hook_menu().
imagecache_external_perm Implementation of hook_perm().