1
0

cache.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002 Active Fish Group |
  7. // +----------------------------------------------------------------------+
  8. // | Authors: Kelvin Jones <kelvin@kelvinjones.co.uk> |
  9. // +----------------------------------------------------------------------+
  10. //
  11. // $Id: cache.php 1434 2006-10-29 02:33:41Z b4rt $
  12. /**
  13. * Class uses all of vlibTemplate's functionality but caches the template files.
  14. * It creates an identical tree structure to your filesystem but with cached files.
  15. *
  16. * @author Kelvin Jones <kelvin@kelvinjones.co.uk>
  17. * @since 22/02/2002
  18. * @package vLIB
  19. * @access public
  20. */
  21. class vlibTemplateCache extends vlibTemplate {
  22. /*-----------------------------------------------------------------------------\
  23. | DO NOT TOUCH ANYTHING IN THIS CLASS, IT MAY NOT WORK OTHERWISE |
  24. \-----------------------------------------------------------------------------*/
  25. var $_cache = 1; // tells vlibTemplate that we're caching
  26. var $_cachefile; // full path to current cache file (even if it doesn't yet exist)
  27. var $_cacheexists; // has this file been cached before
  28. var $_cachefilelocked; // is this file currently locked whilst writing
  29. var $_cachefiledir; // dir of current cache file
  30. var $_clearcache = 0;
  31. /**
  32. * FUNCTION: clearCache
  33. * will unset a file, and set $this->_cacheexists to 0.
  34. *
  35. * @access public
  36. * @return boolean
  37. */
  38. function clearCache() {
  39. $this->_clearcache = 1;
  40. return true;
  41. }
  42. /**
  43. * FUNCTION: recache
  44. * alias for clearCache().
  45. *
  46. * @access public
  47. * @return boolean
  48. */
  49. function recache() {
  50. return $this->clearCache();
  51. }
  52. /**
  53. * FUNCTION: setCacheLifeTime
  54. * sets the lifetime of the cached file
  55. *
  56. * @param int $int number of seconds to set lifetime to
  57. * @access public
  58. * @return boolean
  59. */
  60. function setCacheLifeTime($int = null) {
  61. if ($int == null || !is_int($int)) return false;
  62. if ($int == 0) $int = 60;
  63. if ($int == -1) $int = 157680000; // set to 5 yrs time
  64. $this->OPTIONS['CACHE_LIFETIME'] = $int;
  65. return true;
  66. }
  67. /**
  68. * FUNCTION: setCacheExtension
  69. * sets the extention of the cache file
  70. *
  71. * @param str $str name of new cache extention
  72. * @access public
  73. * @return boolean
  74. */
  75. function setCacheExtension($str = null) {
  76. if ($str == null || !preg_match('/^[a-z0-9]+$/', strtolower($str))) return false;
  77. $this->OPTIONS['CACHE_EXTENSION'] = strtolower($str);
  78. return true;
  79. }
  80. /*----------------------------------------\
  81. Private Functions
  82. -----------------------------------------*/
  83. /**
  84. * FUNCTION: _checkCache
  85. * checks if there's a cache, if there is then it will read the cache file as the template.
  86. */
  87. function _checkCache ($tmplfile) {
  88. $this->_cachefile = $this->_getFilename($tmplfile);
  89. if ($this->_clearcache) {
  90. if (file_exists($this->_cachefile)) unlink($this->_cachefile);
  91. return false;
  92. }
  93. if (file_exists($this->_cachefile) && is_readable($this->_cachefile)) {
  94. $this->_cacheexists = 1;
  95. // if it's expired
  96. if ((filemtime($this->_cachefile) + $this->OPTIONS['CACHE_LIFETIME']) < date ('U')
  97. || filemtime($this->_cachefile) < filemtime($tmplfile)) {
  98. $this->_cacheexists = 0;
  99. return false; // so that we know to recache
  100. }
  101. else {
  102. return true;
  103. }
  104. } else {
  105. $this->_cacheexists = 0;
  106. return false;
  107. }
  108. }
  109. /**
  110. * FUNCTION: _getFilename
  111. * gets the full pathname for the cached file
  112. *
  113. */
  114. function _getFilename($tmplfile) {
  115. return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaR'.realpath($tmplfile)).'.'.$this->OPTIONS['CACHE_EXTENSION'];
  116. }
  117. /**
  118. * FUNCTION: _createCache
  119. * creates the cached file
  120. *
  121. */
  122. function _createCache($data) {
  123. $cache_file = $this->_cachefile;
  124. if(!$this->_prepareDirs($cache_file)) return false; // prepare all of the directories
  125. $f = fopen ($cache_file, "w");
  126. flock($f, 2); // set an EXclusive lock
  127. if (!$f) vlibTemplateError::raiseError('VT_ERROR_NO_CACHE_WRITE',KILL,$cache_file);
  128. fputs ($f, $data); // write the parsed string from vlibTemplate
  129. flock($f, 3); // UNlock file
  130. fclose ($f);
  131. touch ($cache_file);
  132. return true;
  133. }
  134. /**
  135. * FUNCTION: _prepareDirs
  136. * prepares the directory structure
  137. *
  138. */
  139. function _prepareDirs($file) {
  140. if (empty($file)) die('no filename'); //do error in future
  141. $filepath = dirname($file);
  142. if (is_dir($filepath)) return true;
  143. $dirs = preg_split('[\\/]', $filepath);
  144. $currpath = "";
  145. foreach ($dirs as $dir) {
  146. $currpath .= $dir .'/';
  147. $type = @filetype($currpath);
  148. ($type=='link') and $type = 'dir';
  149. if ($type != 'dir' && $type != false && !empty($type)) {
  150. vlibTemplateError::raiseError('VT_ERROR_WRONG_CACHE_TYPE',KILL,'directory: '.$currpath.', type: '.$type);
  151. }
  152. if ($type == 'dir') {
  153. continue;
  154. }
  155. else {
  156. $s = @mkdir($currpath, 0775);
  157. if (!$s) vlibTemplateError::raiseError('VT_ERROR_CACHE_MKDIR_FAILURE',KILL,'directory: '.$currpath);
  158. }
  159. }
  160. return true;
  161. }
  162. } // -- end vlibTemplateCache class
  163. ?>