PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/html/inc/lib/vlib/vlibTemplate/cache.php

https://github.com/epsylon3/torrentflux
PHP | 202 lines | 102 code | 25 blank | 75 comment | 35 complexity | e07b21d26e86e9ae55429f3d338a3aba MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  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,v 1.4 2003/05/15 14:17:41 releasedj Exp $
  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)) {
  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 (empty($cache_file)) return false;
  125. if(!$this->_prepareDirs($cache_file)) return false; // prepare all of the directories
  126. $f = fopen($cache_file, "w");
  127. if (!$f) {
  128. if(!is_dir($this->OPTIONS['CACHE_DIRECTORY']) && !empty($this->OPTIONS['CACHE_DIRECTORY']) ) {
  129. @ system("mkdir -m 775 -p '".$this->OPTIONS['CACHE_DIRECTORY']."'");
  130. mkdir($this->OPTIONS['CACHE_DIRECTORY'], 0775);
  131. }
  132. $f = fopen($cache_file, "w");
  133. if (!$f) vlibTemplateError::raiseError('VT_ERROR_NO_CACHE_WRITE',KILL,$cache_file);
  134. }
  135. flock($f, 2); // set an EXclusive lock
  136. fputs($f, $data); // write the parsed string from vlibTemplate
  137. flock($f, 3); // UNlock file
  138. fclose($f);
  139. touch($cache_file);
  140. return true;
  141. }
  142. /**
  143. * FUNCTION: _prepareDirs
  144. * prepares the directory structure
  145. *
  146. */
  147. function _prepareDirs($file) {
  148. if (empty($file)) die('no filename'); //do error in future
  149. $filepath = dirname($file);
  150. if (is_dir($filepath)) return true;
  151. $dirs = preg_split("#[\\/\\\\]#", $filepath); // "/" ou "\"
  152. $currpath=$this->OPTIONS['CACHE_DIRECTORY'];
  153. foreach ($dirs as $dir) {
  154. if (empty($dir))
  155. continue;
  156. $currpath .= $dir .'/';
  157. $type = @filetype($currpath);
  158. ($type=='link') && $type = 'dir';
  159. if ($type != 'dir' and $type != false and !empty($type)) {
  160. vlibTemplateError::raiseError('VT_ERROR_WRONG_CACHE_TYPE',KILL,'directory: '.$currpath.', type: '.$type);
  161. }
  162. if ($type == 'dir') {
  163. continue;
  164. }
  165. else {
  166. $s = (@mkdir($currpath, 0775) || @mkdir($currpath) );
  167. if (!$s) {
  168. @ system("mkdir -m 0775 -p '".$this->OPTIONS['CACHE_DIRECTORY']."'");
  169. @ system("mkdir -m 0775 -p '$currpath'");
  170. vlibTemplateError::raiseError('VT_ERROR_CACHE_MKDIR_FAILURE',KILL,'directory: '.$currpath);
  171. }
  172. }
  173. }
  174. return true;
  175. }
  176. } // -- end vlibTemplateCache class
  177. ?>