You are here

function css_emimage_advagg_scan_for_changes in CSS Embedded Images 7

Implements hook_advagg_scan_for_changes().

2 calls to css_emimage_advagg_scan_for_changes()
css_emimage_advagg_build_aggregate_plans_alter in ./css_emimage.advagg.inc
Implements hook_advagg_build_aggregate_plans_alter().
css_emimage_advagg_changed_files in ./css_emimage.advagg.inc
Implements hook_advagg_changed_files().

File

./css_emimage.advagg.inc, line 132
CSS Embedded Images module.

Code

function css_emimage_advagg_scan_for_changes($filename, $save_changes = FALSE) {
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  if ($ext != 'css') {
    return FALSE;
  }

  // Get the info on this file.
  module_load_include('inc', 'advagg', 'advagg');
  $info = advagg_get_info_on_file($filename);

  // Cache info.
  $filename_hashes =& drupal_static('advagg_get_info_on_file');
  $cache_id = 'advagg:file:' . $info['filename_hash'];
  $hash_id = 'css_emimage:' . $info['filename_hash'];
  $cache_is_empty = FALSE;
  if (!isset($info['css_emimage'])) {
    $info['css_emimage'] = advagg_get_hash_settings($hash_id);
    $cache_is_empty = TRUE;
  }

  // Load file.
  $optimize = TRUE;
  $contents = advagg_load_css_stylesheet($filename, $optimize);

  // Check if this file links to any images.
  $images = css_emimage_string_contains_images($contents);

  // If no images bail out here.
  if (empty($images)) {
    if (empty($info['css_emimage'])) {

      // File never had any images.
      if (!isset($info['css_emimage'])) {

        // Save results.
        if ($save_changes) {
          $info['css_emimage'] = array();
          $filename_hashes[$cache_id] = $info;
          cache_set($cache_id, $info, 'cache_advagg_info', CACHE_PERMANENT);
          advagg_set_hash_settings($hash_id, $info['css_emimage']);
        }
      }
      return FALSE;
    }
    else {

      // File used to have images, but no longer does.
      // Save results.
      if ($save_changes) {
        $info['css_emimage'] = array();
        $filename_hashes[$cache_id] = $info;
        cache_set($cache_id, $info, 'cache_advagg_info', CACHE_PERMANENT);
        advagg_set_hash_settings($hash_id, $info['css_emimage']);
      }
      return TRUE;
    }
  }

  // See if any of the images has changed.
  $changed = FALSE;
  foreach ($images as $image_filename) {
    if (empty($image_filename)) {
      continue;
    }

    // Strip base path from filename.
    $image_filename = preg_replace('/^' . preg_quote($GLOBALS['base_path'], '/') . '/i', '', $image_filename);
    if (file_exists($image_filename) == FALSE) {
      continue;
    }
    $image_hash = drupal_hash_base64(file_get_contents($image_filename));
    if (empty($info['css_emimage'][$image_filename]) || $info['css_emimage'][$image_filename]['hash'] != $image_hash) {

      // Save results.
      if ($save_changes) {
        $info['css_emimage'][$image_filename] = array(
          'hash' => $image_hash,
          'filesize' => filesize($image_filename),
          'base64_size' => strlen(base64_encode(file_get_contents($image_filename))),
        );
        $filename_hashes[$cache_id] = $info;
        cache_set($cache_id, $info, 'cache_advagg_info', CACHE_PERMANENT);
        advagg_set_hash_settings($hash_id, $info['css_emimage']);
      }
      $changed = TRUE;
    }
    elseif ($save_changes) {

      // Still put info into static cache.
      $filename_hashes[$cache_id] = $info;
      if ($cache_is_empty) {
        cache_set($cache_id, $info, 'cache_advagg_info', CACHE_PERMANENT);
      }
    }
  }
  return $changed;
}