You are here

filecache.module in File Cache 7

Same filename and directory in other branches
  1. 5.5 filecache.module

File

filecache.module
View source
<?php

// -*- indent-tabs-mode:nil -*-
module_load_include('inc', 'filecache', 'filecache');

// load filecache.inc

/**
 * Implements hook_cron().
 *
 * @see hook_cron()
 */
function filecache_cron() {

  // Check if filecache is enabled in settings.php
  $filecache_enabled = FALSE;
  global $conf;
  foreach ($conf as $setting => $value) {
    if ($setting == 'cache_default_class' || substr($setting, 0, strlen('cache_class_')) === 'cache_class_') {
      if ($value === 'DrupalFileCache') {
        $filecache_enabled = TRUE;
        break;
      }
    }
  }

  // If filecache is disabled, be sure to remove filecache bin registry
  $filecache_registry_pathname = filecache_registry_pathname();
  if (!$filecache_enabled) {
    @unlink($filecache_registry_pathname);
    return;
  }

  // Load cache bin registry
  $registry = @unserialize(@file_get_contents($filecache_registry_pathname));
  if (!is_array($registry)) {
    @unlink($filecache_registry_pathname);
    return;
  }
  $cache_size = 0;
  foreach ($registry as $bin => $value) {
    $cache = new DrupalFileCache($bin);
    $cache_size += $cache
      ->delete_expired();
  }
  cache_set('filecache_space', array(
    'cache_size' => $cache_size,
  ));
}

/**
 * Implements hook_requirements().
 *
 * @param $phase
 *   Phase.
 * @see hook_requirements()
 */
function filecache_requirements($phase) {
  $t = get_t();
  $requirements = array(
    'title' => $t('File Cache'),
  );
  $requirements['severity'] = REQUIREMENT_ERROR;

  // Assume error first
  $readme_hint = $t('Please follow the instructions in <a href="!readmetxt">README.txt</a>.', array(
    '!readmetxt' => base_path() . drupal_get_path('module', 'filecache') . '/README.txt',
  ));
  $filecache_directory = filecache_directory();
  $registry_pathname = filecache_registry_pathname();
  $registry = @unserialize(@file_get_contents($registry_pathname));
  if (!isset($registry) || !is_array($registry)) {
    $registry = array();
  }
  $filecache_bins = array_keys($registry);
  sort($filecache_bins);
  if (empty($filecache_bins)) {
    $requirements['severity'] = REQUIREMENT_WARNING;
    $requirements['value'] = $t('No cache bins are served by File Cache.');
    $requirements['description'] = $readme_hint;
    return array(
      'filecache' => $requirements,
    );
  }
  $not_ok_bins = array();
  foreach ($filecache_bins as $bin) {
    $cache = new DrupalFileCache($bin);
    if (!$cache->ok) {
      array_push($not_ok_bins, $bin);
    }
  }
  if (!empty($not_ok_bins)) {
    $requirements['severity'] = REQUIREMENT_WARNING;
    $requirements['value'] = $t('The following File Cache cache bins has problems: %bins. Look at <em>Recent log messages</em> for details.', array(
      '%bins' => implode(', ', $not_ok_bins),
    ));
    $requirements['description'] = $readme_hint;
    return array(
      'filecache' => $requirements,
    );
  }

  // Show what cache bins we serve (in $requirements['value'])
  $requirements['severity'] = REQUIREMENT_OK;
  $requirements['value'] = $t('File Cache serves %bins cache bins.', array(
    '%bins' => count($filecache_bins),
  ));

  // Show filecache_directory and its size (in $requirements['description'])
  $space = cache_get('filecache_space');
  $cron_last = variable_get('cron_last');
  if ($space) {
    $requirements['description'] = $t('Cache files use !size (calculated !time ago)', array(
      '!size' => format_size($space->data['cache_size']),
      '!time' => format_interval(REQUEST_TIME - $cron_last),
    ));
  }
  else {
    $requirements['description'] = $t('Cache files use unknown disk space. Run cron to update.');
  }
  if ($filecache_directory == filecache_default_directory()) {
    $url = substr($filecache_directory, strlen(DRUPAL_ROOT)) . '/' . FILECACHE_CACHE_BIN_REGISTRY;
    $requirements['severity'] = REQUIREMENT_WARNING;
    $requirements['description'] .= $t('<br>You use default directory location %default_dir. ' . 'Make sure your webserver denies access to files and directories starting with ".ht" like in the following link: ' . '<a href="!link">link</a>. If you deny access to directories starting with ".ht" and you want to remove this message, ' . 'add the following line to your <code>settings.php</code> and then remove the <code>!quickstart</code> directory:<br>' . '<code>$conf[\'filecache_directory\'] = \'!new_dir\';</code>', array(
      '%default_dir' => $filecache_directory,
      '!link' => $url,
      '!quickstart' => FILECACHE_QUICKSTART_DIRECTORY,
      '!new_dir' => filecache_default_directory(FALSE),
    ));
  }
  return array(
    'filecache' => $requirements,
  );
}

Functions

Namesort descending Description
filecache_cron Implements hook_cron().
filecache_requirements Implements hook_requirements().