PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/php4/cache_v1/class.Cache.inc.php

http://flaimo-php.googlecode.com/
PHP | 325 lines | 101 code | 20 blank | 204 comment | 31 complexity | dae6b67bb67ed505e6e2618b012f50b0 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //+----------------------------------------------------------------------+
  4. //| WAMP (XP-SP1/1.3.24/4.0.12/4.3.0) |
  5. //+----------------------------------------------------------------------+
  6. //| Copyright (c) 1992-2003 Michael Wimmer |
  7. //+----------------------------------------------------------------------+
  8. //| I don't have the time to read through all the licences to find out |
  9. //| what the exactly say. But it's simple. It's free for non commercial |
  10. //| projects, but as soon as you make money with it, i want my share :-) |
  11. //| (License : Free for non-commercial use) |
  12. //+----------------------------------------------------------------------+
  13. //| Authors: Michael Wimmer <flaimo@gmx.net> |
  14. //+----------------------------------------------------------------------+
  15. //
  16. // $Id$
  17. /**
  18. * @package Cache
  19. */
  20. /**
  21. * Caching of generated html/text or images...
  22. *
  23. * User it like this:
  24. * <code>
  25. * if ($cache->isCached($cache_filename, 14) == true) {
  26. * echo $cache->returnCache($cache_filename, false, false);
  27. * } else {
  28. * // create content here and fill $text with it
  29. * $cache->writeCache($cache_filename, $text, false);
  30. * }
  31. * </code>
  32. *
  33. * Tested with Apache 1.3.24 and PHP 4.2.3
  34. *
  35. * @desc Caching of generated html/text or images&#x2026;
  36. * @access public
  37. * @author Michael Wimmer <flaimo@gmx.net>
  38. * @copyright Michael Wimmer
  39. * @link http://www.flaimo.com/
  40. * @package Cache
  41. * @version 1.002
  42. */
  43. class Cache {
  44. /*-------------------*/
  45. /* V A R I A B L E S */
  46. /*-------------------*/
  47. /**#@+
  48. * @var string
  49. * @access private
  50. */
  51. /**
  52. * Directory where cached files are saved
  53. *
  54. * @desc Directory where cached files are saved
  55. */
  56. var $cache_dir;
  57. /**
  58. * Default extention for cache files
  59. *
  60. * @desc Default extention for cache files
  61. */
  62. var $ext = 'txt';
  63. /**
  64. * Default prefix for cache files
  65. *
  66. * @desc Default prefix for cache files
  67. */
  68. var $prefix = '';
  69. /**#@-*/
  70. /*-----------------------*/
  71. /* C O N S T R U C T O R */
  72. /*-----------------------*/
  73. /**#@+
  74. * @access private
  75. */
  76. /**
  77. * Constructor
  78. *
  79. * Only job is to set all the variablesnames
  80. *
  81. * @desc Constructor
  82. * @param (string) $cachedir Directory where cached files are saved. If string is empty &#x201C;cached&#x201D; will be used
  83. * @return (void)
  84. */
  85. function Cache($cachedir = '') {
  86. $this->cache_dir = (string) ((strlen(trim($cachedir)) > 0) ? $cachedir : 'cached');
  87. if ($this->checkCacheDir() == FALSE) {
  88. die('error creating cache directory');
  89. } // end if
  90. } // end constructor
  91. /*-------------------*/
  92. /* F U N C T I O N S */
  93. /*-------------------*/
  94. /**
  95. * Checks if the cache directory exists, else trys to create it
  96. *
  97. * @desc Checks if the cache directory exists, else trys to create it
  98. * @return boolean
  99. */
  100. function checkCacheDir() {
  101. if (!is_dir($this->cache_dir)) {
  102. return (boolean) ((!mkdir($this->cache_dir, 0700)) ? FALSE : TRUE);
  103. } else {
  104. return (boolean) TRUE;
  105. } // end if
  106. } // end function
  107. /**
  108. * Encodes the temporary filename
  109. *
  110. * @desc Encodes the temporary filename
  111. * @param string $filename
  112. * @return string $filename Encoded string
  113. * @see decodeFilename()
  114. */
  115. function encodeFilename($filename) {
  116. //return $filename;
  117. return (string) md5($filename);
  118. } // end function
  119. /**
  120. * Decodes the temporary filename
  121. *
  122. * @desc Decodes the temporary filename
  123. * @param string $filename
  124. * @return string $filename Decoded string
  125. * @see encodeFilename()
  126. */
  127. function decodeFilename($filename) {
  128. return (string) $filename;
  129. //return (string) str_rot13($filename);
  130. } // end function
  131. /**
  132. * Sticks together filename + path
  133. *
  134. * @desc Sticks together filename + path
  135. * @param string $filename
  136. * @return string
  137. * @see returnCachePicturename()
  138. * @uses encodeFilename()
  139. */
  140. function returnCacheFilename($filename) {
  141. return (string) $this->cache_dir . '/' . $this->prefix . $this->encodeFilename($filename) . '.' . $this->ext;
  142. } // end function
  143. /**
  144. * Sticks together filename + path
  145. *
  146. * @desc Sticks together filename + path
  147. * @param string $filename
  148. * @param string $filetype
  149. * @return string
  150. * @see returnCacheFilename()
  151. * @uses encodeFilename()
  152. */
  153. function returnCachePicturename($filename, $filetype) {
  154. return (string) $this->cache_dir . '/' . $this->prefix . $this->encodeFilename($filename) . '.' . $filetype;
  155. } // end function
  156. /**
  157. * Delete old files
  158. *
  159. * @desc Delete old files
  160. * @param int $lastaccess Minimum age of the files last accesstime (in seconds) before files get deleted
  161. * @param int $created Minimum age of the files (in seconds) before files get deleted
  162. * @return void
  163. * @see writeCardFile()
  164. */
  165. function deleteOldFiles($lastaccess = 300, $created = 300) {
  166. if (!is_int($lastaccess) || $lastaccess < 1) {
  167. $lastaccess = (int) 300;
  168. } // end if
  169. if (!is_int($created) || $created < 1) {
  170. $created = (int) 300;
  171. } // end if
  172. $handle = opendir($this->download_dir);
  173. while ($file = readdir($handle)) {
  174. if (!eregi("^\.{1,2}$",$file) && !is_dir($this->cache_dir . '/' . $file) && (((time() - filemtime($this->cache_dir . '/' . $file)) > $created) || (((time() - fileatime($this->cache_dir . '/' . $file)) > $lastaccess)))) {
  175. unlink($this->cache_dir . '/' . $file);
  176. } // end if
  177. } // end while
  178. closedir($handle);
  179. if (isset($handle)) {
  180. unset($handle);
  181. } // end if
  182. if (isset($file)) {
  183. unset($file);
  184. } // end if
  185. } // end function
  186. /**#@-*/
  187. /**#@+
  188. * @access public
  189. */
  190. /**
  191. * Checks if a cache file is available and up to date
  192. *
  193. * @desc Checks if a cache file is available and up to date
  194. * @param string $file Name of the cached file
  195. * @param int $time Minimum time until cache file has to be renewed
  196. * @return boolean $cached
  197. * @see isPictureCached()
  198. * @uses returnCacheFilename()
  199. */
  200. function isCached($file, $time = 30) {
  201. if (($filetime = @filemtime($this->returnCacheFilename($file))) == FALSE) {
  202. return (boolean) FALSE;
  203. } // end if
  204. if ((time() - $filetime) > $time) {
  205. return (boolean) FALSE;
  206. } // end if
  207. return (boolean) TRUE;
  208. } // end function
  209. /**
  210. * Checks if a picture is cached and up to date
  211. *
  212. * @desc Checks if a picture is cached and up to date
  213. * @param string $file Name of the cached file
  214. * @param int $time Minimum time until cache file has to be renewed
  215. * @param string $type Type of picture
  216. * @return boolean $cached
  217. * @see isCached()
  218. * @uses returnCachePicturename()
  219. */
  220. function isPictureCached($file, $time = 30, $type = 'png') {
  221. if (file_exists($this->returnCachePicturename($file, $type)) && (time() - filemtime($this->returnCachePicturename($file, $type)) < $time)) {
  222. return (boolean) TRUE;
  223. } // end if
  224. return (boolean) FALSE;
  225. } // end function
  226. /**
  227. * Returns a cached file
  228. *
  229. * @desc Returns a cached file
  230. * @param string $file Name of the cached file
  231. * @param boolean $passthrough Return cache into a variable or directly output it to the browser
  232. * @param boolean $binary Whether file should be returned in binary mode or not
  233. * @return mixed $cache
  234. * @see returnPictureCache()
  235. * @uses returnCacheFilename()
  236. */
  237. function returnCache($file, $passthrough = FALSE, $binary = FALSE) {
  238. $returntype = (($binary == FALSE) ? 'r' : 'rb');
  239. if ($passthrough == TRUE) {
  240. return readfile($this->returnCacheFilename($file), $returntype);
  241. } else {
  242. $handle = fopen($this->returnCacheFilename($file), $returntype);
  243. $cache = fgets($handle);
  244. fclose($handle);
  245. return $cache;
  246. } // end if
  247. } // end function
  248. /**
  249. * Returns a cached picture
  250. *
  251. * @desc Returns a cached picture
  252. * @param string $file Name of the cached file
  253. * @param string $filetype Type of picture (jpg|gif|png)
  254. * @param boolean $binary Whether file should be returned in binary mode or not
  255. * @return string
  256. * @see returnCache()
  257. * @uses returnCachePicturename()
  258. */
  259. function returnPictureCache($file, $filetype = 'png', $binary = FALSE) {
  260. $returntype = (($binary == FALSE) ? 'r' : 'rb');
  261. return readfile($this->returnCachePicturename($file, $filetype), $returntype);
  262. } // end function
  263. /**
  264. * Writes a cache file
  265. *
  266. * @desc Writes a cache file
  267. * @param string $file Name of the cached file
  268. * @param mixed $content Content that should be cached
  269. * @param boolean $binary Whether file should be returned in binary mode or not
  270. * @return void
  271. * @see writePictureCache()
  272. * @uses returnCacheFilename()
  273. */
  274. function writeCache($file, $content, $binary = FALSE) {
  275. $writetype = (($binary == FALSE) ? 'w' : 'wb');
  276. $handle = fopen($this->returnCacheFilename($file), $writetype);
  277. fputs($handle, $content);
  278. fclose($handle);
  279. } // end function
  280. /**
  281. * Caches a picture
  282. *
  283. * @desc Caches a picture
  284. * @param string $file Name of the cached file
  285. * @param binary $picture The created picture
  286. * @param string $filetype Type of picture (jpg|gif|png)
  287. * @return void
  288. * @see writeCache()
  289. * @uses returnCachePicturename()
  290. */
  291. function writePictureCache($file, $picture, $filetype = 'png') {
  292. if ($filetype == 'jpg') {
  293. imagejpeg($picture, $this->returnCachePicturename($file, $filetype));
  294. } elseif ($filetype == 'gif') {
  295. imagegif($picture, $this->returnCachePicturename($file, $filetype));
  296. } elseif ($filetype == 'png') {
  297. imagepng($picture, $this->returnCachePicturename($file, $filetype));
  298. } else {
  299. return (boolean) FALSE;
  300. } // end if
  301. return (boolean) TRUE;
  302. } // end function
  303. /**#@-*/
  304. } // end class Cache
  305. ?>