[ Index ]
 

Code source de e107 0.7.8

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/e107_handlers/ -> cache_handler.php (source)

   1  <?php
   2  
   3  /*
   4  + ----------------------------------------------------------------------------+
   5  |     e107 website system
   6  |
   7  |     ©Steve Dunstan 2001-2002
   8  |     http://e107.org
   9  |     jalist@e107.org
  10  |
  11  |     Released under the terms and conditions of the
  12  |     GNU General Public License (http://gnu.org).
  13  |
  14  |     $Source: /cvsroot/e107/e107_0.7/e107_handlers/cache_handler.php,v $
  15  |     $Revision: 1.35 $
  16  |     $Date: 2006/11/17 13:26:38 $
  17  |     $Author: mrpete $
  18  +----------------------------------------------------------------------------+
  19  */
  20  
  21  if (!defined('e107_INIT')) { exit; }
  22  
  23  /**
  24  * Class to cache data as files, improving site speed and throughput.
  25  *
  26  * @package     e107
  27  * @version     $Revision: 1.35 $
  28  * @author      $Author: mrpete $
  29  */
  30  class ecache {
  31  
  32      var $CachePageMD5;
  33      var $CachenqMD5;
  34  
  35      /**
  36      * @return string
  37      * @param string $query
  38      * @desc Internal class function that returns the filename of a cache file based on the query.
  39      * @scope private
  40      * If the tag begins 'menu_', e_QUERY is not included in the hash which creates the file name
  41      */
  42  	function cache_fname($CacheTag) {
  43          global $FILES_DIRECTORY;
  44          if(strpos($CacheTag, "nomd5_") === 0) {
  45              // Add 'nomd5' to indicate we are not calculating an md5
  46              $CheckTag = '_nomd5';
  47          }
  48          elseif (isset($this)) {
  49              if (defined("THEME")) {
  50                  if (strpos($CacheTag, "nq_") === 0)    {
  51                      // We do not care about e_QUERY, so don't use it in the md5 calculation
  52                      if (!$this->CachenqMD5) {
  53                          $this->CachenqMD5 = md5(e_BASE.(defined("ADMIN") && ADMIN == true ? "admin" : "").e_LANGUAGE.THEME.USERCLASS_LIST.filemtime(THEME.'theme.php'));
  54                      }
  55                      // Add 'nq' to indicate we are not using e_QUERY
  56                      $CheckTag = '_nq_'.$this->CachenqMD5;
  57                      
  58                  } else {
  59                      // It's a page - need the query in the hash
  60                      if (!$this->CachePageMD5) {
  61                          $this->CachePageMD5 = md5(e_BASE.e_LANGUAGE.THEME.USERCLASS_LIST.e_QUERY.filemtime(THEME.'theme.php'));
  62                      }
  63                      $CheckTag = '_'.$this->CachePageMD5;
  64                  }
  65              } else {
  66                  $CheckTag = '';
  67              }
  68          } else {
  69              $CheckTag = '';
  70          }
  71          $q = preg_replace("#\W#", "_", $CacheTag);
  72          $fname = './'.e_BASE.$FILES_DIRECTORY.'cache/'.$q.$CheckTag.'.cache.php';
  73          return $fname;
  74      }
  75  
  76      /**
  77      * @return string
  78      * @param string $query
  79      * @param int $MaximumAge the time in minutes before the cache file 'expires'
  80      * @desc Returns the data from the cache file associated with $query, else it returns false if there is no cache for $query.
  81      * @scope public
  82      */
  83  	function retrieve($CacheTag, $MaximumAge = false, $ForcedCheck = false) {
  84          global $pref, $FILES_DIRECTORY, $tp;
  85          if (($pref['cachestatus'] || $ForcedCheck == true) && !$tp->checkHighlighting()) {
  86              $cache_file = (isset($this) ? $this->cache_fname($CacheTag) : ecache::cache_fname($CacheTag));
  87              if (file_exists($cache_file)) {
  88                  if ($MaximumAge != false && (filemtime($cache_file) + ($MaximumAge * 60)) < time()) {
  89                      unlink($cache_file);
  90                      return false;
  91                  } else {
  92                      $ret = file_get_contents($cache_file);
  93                      $ret = substr($ret, 5);
  94                      return $ret;
  95                  }
  96              } else {
  97                  return false;
  98              }
  99          }
 100          return false;
 101      }
 102  
 103      /**
 104      * @return void
 105      * @param string $CacheTag - name of tag for future retrieval
 106      * @param string $Data - data to be cached
 107      * @param bool   $ForceCache (optional, default false) - if TRUE, writes cache even when disabled
 108      * @param bool   $bRaw (optional, default false) - if TRUE, writes data exactly as provided instead of prefacing with php leadin
 109      * @desc Creates / overwrites the cache file for $query, $text is the data to store for $query.
 110      * @scope public
 111      */
 112  	function set($CacheTag, $Data, $ForceCache = false, $bRaw=0) {
 113          global $pref, $FILES_DIRECTORY, $tp;
 114          if (($pref['cachestatus'] || $ForceCache == true) && !$tp->checkHighlighting()) {
 115              $cache_file = (isset($this) ? $this->cache_fname($CacheTag) : ecache::cache_fname($CacheTag));
 116              file_put_contents($cache_file, ($bRaw? $Data : '<?php'.$Data) );
 117              @chmod($cache_file, 0777);
 118              @touch($cache_file);
 119          }
 120      }
 121  
 122      /**
 123      * @return bool
 124      * @param string $CacheTag
 125      * @desc Deletes cache files. If $query is set, deletes files named {$CacheTag}*.cache.php, if not it deletes all cache files - (*.cache.php)
 126      */
 127  	function clear($CacheTag = '') {
 128          global $pref, $FILES_DIRECTORY;
 129          $file = ($CacheTag) ? preg_replace("#\W#", "_", $CacheTag)."*.cache.php" : "*.cache.php";
 130          $dir = "./".e_BASE.$FILES_DIRECTORY."cache/";
 131          $ret = ecache::delete($dir, $file);
 132          return $ret;
 133      }
 134  
 135      /**
 136      * @return bool
 137      * @param string $dir
 138      * @param string $pattern
 139      * @desc Internal class function to allow deletion of cache files using a pattern, default '*.*'
 140      * @scope private
 141      */
 142  	function delete($dir, $pattern = "*.*") {
 143          $deleted = false;
 144          $pattern = str_replace(array("\*", "\?"), array(".*", "."), preg_quote($pattern));
 145          if (substr($dir, -1) != "/") {
 146              $dir .= "/";
 147          }
 148          if (is_dir($dir)) {
 149              $d = opendir($dir);
 150              while ($file = readdir($d)) {
 151                  if (is_file($dir.$file) && preg_match("/^{$pattern}$/", $file)) {
 152                      if (unlink($dir.$file)) {
 153                          $deleted[] = $file;
 154                      }
 155                  }
 156              }
 157              closedir($d);
 158              return true;
 159          } else {
 160              return false;
 161          }
 162      }
 163  }
 164  
 165  ?>


Généré le : Sun Apr 1 01:23:32 2007 par Balluche grâce à PHPXref 0.7