[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/cache/storage/ -> file.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: file.php 14401 2010-01-26 14:10:00Z louis $
   4   * @package        Joomla.Framework
   5   * @subpackage    Cache
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant
   9   * to the GNU General Public License, and as distributed it includes or
  10   * is derivative of works licensed under the GNU General Public License or
  11   * other free or open source software licenses.
  12   * See COPYRIGHT.php for copyright notices and details.
  13   */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * File cache storage handler
  20   *
  21   * @package        Joomla.Framework
  22   * @subpackage    Cache
  23   * @since        1.5
  24   */
  25  class JCacheStorageFile extends JCacheStorage
  26  {
  27      /**
  28      * Constructor
  29      *
  30      * @access protected
  31      * @param array $options optional parameters
  32      */
  33  	function __construct( $options = array() )
  34      {
  35          parent::__construct($options);
  36  
  37          $config            =& JFactory::getConfig();
  38          $this->_root    = $options['cachebase'];
  39          $this->_hash    = $config->getValue('config.secret');
  40      }
  41  
  42      /**
  43       * Get cached data from a file by id and group
  44       *
  45       * @access    public
  46       * @param    string    $id            The cache data id
  47       * @param    string    $group        The cache data group
  48       * @param    boolean    $checkTime    True to verify cache time expiration threshold
  49       * @return    mixed    Boolean false on failure or a cached data string
  50       * @since    1.5
  51       */
  52  	function get($id, $group, $checkTime)
  53      {
  54          $data = false;
  55  
  56          $path = $this->_getFilePath($id, $group);
  57          $this->_setExpire($id, $group);
  58          if (file_exists($path)) {
  59              $data = file_get_contents($path);
  60              if($data) {
  61                  // Remove the initial die() statement
  62                  $data    = preg_replace('/^.*\n/', '', $data);
  63              }
  64          }
  65  
  66          return $data;
  67      }
  68  
  69      /**
  70       * Store the data to a file by id and group
  71       *
  72       * @access    public
  73       * @param    string    $id        The cache data id
  74       * @param    string    $group    The cache data group
  75       * @param    string    $data    The data to store in cache
  76       * @return    boolean    True on success, false otherwise
  77       * @since    1.5
  78       */
  79  	function store($id, $group, $data)
  80      {
  81          $written    = false;
  82          $path        = $this->_getFilePath($id, $group);
  83          $expirePath    = $path . '_expire';
  84          $die        = '<?php die("Access Denied"); ?>'."\n";
  85  
  86          // Prepend a die string
  87  
  88          $data        = $die.$data;
  89  
  90          $fp = @fopen($path, "wb");
  91          if ($fp) {
  92              if ($this->_locking) {
  93                  @flock($fp, LOCK_EX);
  94              }
  95              $len = strlen($data);
  96              @fwrite($fp, $data, $len);
  97              if ($this->_locking) {
  98                  @flock($fp, LOCK_UN);
  99              }
 100              @fclose($fp);
 101              $written = true;
 102          }
 103          // Data integrity check
 104          if ($written && ($data == file_get_contents($path))) {
 105              @file_put_contents($expirePath, ($this->_now + $this->_lifetime));
 106              return true;
 107          } else {
 108              return false;
 109          }
 110      }
 111  
 112      /**
 113       * Remove a cached data file by id and group
 114       *
 115       * @access    public
 116       * @param    string    $id        The cache data id
 117       * @param    string    $group    The cache data group
 118       * @return    boolean    True on success, false otherwise
 119       * @since    1.5
 120       */
 121  	function remove($id, $group)
 122      {
 123          $path = $this->_getFilePath($id, $group);
 124          @unlink($path.'_expire');
 125          if (!@unlink($path)) {
 126              return false;
 127          }
 128          return true;
 129      }
 130  
 131      /**
 132       * Clean cache for a group given a mode.
 133       *
 134       * group mode        : cleans all cache in the group
 135       * notgroup mode    : cleans all cache not in the group
 136       *
 137       * @access    public
 138       * @param    string    $group    The cache data group
 139       * @param    string    $mode    The mode for cleaning cache [group|notgroup]
 140       * @return    boolean    True on success, false otherwise
 141       * @since    1.5
 142       */
 143  	function clean($group, $mode)
 144      {
 145          jimport('joomla.filesystem.folder');
 146  
 147          $return = true;
 148          $folder    = $group;
 149  
 150          if(trim($folder) == '') {
 151              $mode = 'notgroup';
 152          }
 153  
 154          switch ($mode)
 155          {
 156              case 'notgroup':
 157                  $folders = JFolder::folders($this->_root);
 158                  for ($i=0,$n=count($folders);$i<$n;$i++)
 159                  {
 160                      if ($folders[$i] != $folder) {
 161                          $return |= JFolder::delete($this->_root.DS.$folders[$i]);
 162                      }
 163                  }
 164                  break;
 165              case 'group':
 166              default:
 167                  if (is_dir($this->_root.DS.$folder)) {
 168                      $return = JFolder::delete($this->_root.DS.$folder);
 169                  }
 170                  break;
 171          }
 172          return $return;
 173      }
 174  
 175      /**
 176       * Garbage collect expired cache data
 177       *
 178       * @access public
 179       * @return boolean  True on success, false otherwise.
 180       */
 181      function gc()
 182      {    
 183          jimport('joomla.filesystem.file');
 184          $result = true;
 185          // files older than lifeTime get deleted from cache
 186          $files = JFolder::files($this->_root, '_expire', true, true);
 187          foreach($files As $file) {
 188              $time = @file_get_contents($file);
 189              if ($time < $this->_now) {
 190                  $result |= JFile::delete($file);
 191                  $result |= JFile::delete(str_replace('_expire', '', $file));
 192              }
 193          }
 194          return $result;
 195      }
 196  
 197      /**
 198       * Test to see if the cache storage is available.
 199       *
 200       * @static
 201       * @access public
 202       * @return boolean  True on success, false otherwise.
 203       */
 204  	function test()
 205      {
 206          $config    =& JFactory::getConfig();
 207          $root    = $config->getValue('config.cache_path', JPATH_ROOT.DS.'cache');
 208          return is_writable($root);
 209      }
 210  
 211      /**
 212       * Check to make sure cache is still valid, if not, delete it.
 213       *
 214       * @access private
 215       *
 216       * @param string  $id   Cache key to expire.
 217       * @param string  $group The cache data group.
 218       */
 219  	function _setExpire($id, $group)
 220      {
 221          $path = $this->_getFilePath($id, $group);
 222  
 223          // set prune period
 224          if(file_exists($path.'_expire')) {
 225              $time = @file_get_contents($path.'_expire');
 226              if ($time < $this->_now || empty($time)) {
 227                  $this->remove($id, $group);
 228              }
 229          } elseif(file_exists($path)) {
 230              //This means that for some reason there's no expire file, remove it
 231              $this->remove($id, $group);
 232          }
 233      }
 234  
 235      /**
 236       * Get a cache file path from an id/group pair
 237       *
 238       * @access    private
 239       * @param    string    $id        The cache data id
 240       * @param    string    $group    The cache data group
 241       * @return    string    The cache file path
 242       * @since    1.5
 243       */
 244  	function _getFilePath($id, $group)
 245      {
 246          $folder    = $group;
 247          $name    = md5($this->_application.'-'.$id.'-'.$this->_hash.'-'.$this->_language).'.php';
 248          $dir    = $this->_root.DS.$folder;
 249  
 250          // If the folder doesn't exist try to create it
 251          if (!is_dir($dir)) {
 252  
 253              // Make sure the index file is there
 254              $indexFile      = $dir . DS . 'index.html';
 255              @ mkdir($dir) && file_put_contents($indexFile, '<html><body bgcolor="#FFFFFF"></body></html>');
 256          }
 257  
 258          // Make sure the folder exists
 259          if (!is_dir($dir)) {
 260              return false;
 261          }
 262          return $dir.DS.$name;
 263      }
 264  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1