PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/core/model/modx/xmlrss/rsscache.class.php

https://github.com/JoeBlow/revolution
PHP | 203 lines | 135 code | 17 blank | 51 comment | 17 complexity | 32ad37c202794558f98280dcd286bbbe MD5 | raw file
  1. <?php
  2. /**
  3. * @package modx
  4. * @subpackage xmlrss
  5. */
  6. /*
  7. * Project: MagpieRSS: a simple RSS integration tool
  8. * File: rss_cache.inc, a simple, rolling(no GC), cache
  9. * for RSS objects, keyed on URL.
  10. * Author: Kellan Elliott-McCrea <kellan@protest.net>
  11. * Version: 0.51
  12. * License: GPL
  13. *
  14. * The lastest version of MagpieRSS can be obtained from:
  15. * http://magpierss.sourceforge.net
  16. *
  17. * For questions, help, comments, discussion, etc., please join the
  18. * Magpie mailing list:
  19. * http://lists.sourceforge.net/lists/listinfo/magpierss-general
  20. *
  21. */
  22. class RSSCache {
  23. var $BASE_CACHE = './cache'; // where the cache files are stored
  24. var $MAX_AGE = 3600; // when are files stale, default one hour
  25. var $ERROR = ""; // accumulate error messages
  26. function RSSCache ($base='', $age='') {
  27. if ( $base ) {
  28. $this->BASE_CACHE = $base;
  29. }
  30. if ( $age ) {
  31. $this->MAX_AGE = $age;
  32. }
  33. // attempt to make the cache directory
  34. if ( ! file_exists( $this->BASE_CACHE ) ) {
  35. $status = @mkdir( $this->BASE_CACHE, 0755 );
  36. // if make failed
  37. if ( ! $status ) {
  38. $this->error(
  39. "Cache couldn't make dir '" . $this->BASE_CACHE . "'."
  40. );
  41. }
  42. }
  43. }
  44. /*=======================================================================*\
  45. Function: set
  46. Purpose: add an item to the cache, keyed on url
  47. Input: url from wich the rss file was fetched
  48. Output: true on sucess
  49. \*=======================================================================*/
  50. function set ($url, $rss) {
  51. $this->ERROR = "";
  52. $cache_file = $this->file_name( $url );
  53. $fp = @fopen( $cache_file, 'w' );
  54. if ( ! $fp ) {
  55. $this->error(
  56. "Cache unable to open file for writing: $cache_file"
  57. );
  58. return 0;
  59. }
  60. $data = $this->serialize( $rss );
  61. fwrite( $fp, $data );
  62. fclose( $fp );
  63. return $cache_file;
  64. }
  65. /*=======================================================================*\
  66. Function: get
  67. Purpose: fetch an item from the cache
  68. Input: url from wich the rss file was fetched
  69. Output: cached object on HIT, false on MISS
  70. \*=======================================================================*/
  71. function get ($url) {
  72. $this->ERROR = "";
  73. $cache_file = $this->file_name( $url );
  74. if ( ! file_exists( $cache_file ) ) {
  75. $this->debug(
  76. "Cache doesn't contain: $url (cache file: $cache_file)"
  77. );
  78. return 0;
  79. }
  80. $fp = @fopen($cache_file, 'r');
  81. if ( ! $fp ) {
  82. $this->error(
  83. "Failed to open cache file for reading: $cache_file"
  84. );
  85. return 0;
  86. }
  87. if ($filesize = filesize($cache_file) ) {
  88. $data = fread( $fp, filesize($cache_file) );
  89. $rss = $this->unserialize( $data );
  90. return $rss;
  91. }
  92. return 0;
  93. }
  94. /*=======================================================================*\
  95. Function: check_cache
  96. Purpose: check a url for membership in the cache
  97. and whether the object is older then MAX_AGE (ie. STALE)
  98. Input: url from wich the rss file was fetched
  99. Output: cached object on HIT, false on MISS
  100. \*=======================================================================*/
  101. function check_cache ( $url ) {
  102. $this->ERROR = "";
  103. $filename = $this->file_name( $url );
  104. if ( file_exists( $filename ) ) {
  105. // find how long ago the file was added to the cache
  106. // and whether that is longer then MAX_AGE
  107. $mtime = filemtime( $filename );
  108. $age = time() - $mtime;
  109. if ( $this->MAX_AGE > $age ) {
  110. // object exists and is current
  111. return 'HIT';
  112. }
  113. else {
  114. // object exists but is old
  115. return 'STALE';
  116. }
  117. }
  118. else {
  119. // object does not exist
  120. return 'MISS';
  121. }
  122. }
  123. function cache_age( $cache_key ) {
  124. $url = '';
  125. $filename = $this->file_name( $url );
  126. if ( file_exists( $filename ) ) {
  127. $mtime = filemtime( $filename );
  128. $age = time() - $mtime;
  129. return $age;
  130. }
  131. else {
  132. return -1;
  133. }
  134. }
  135. /*=======================================================================*\
  136. Function: serialize
  137. \*=======================================================================*/
  138. function serialize ( $rss ) {
  139. return serialize( $rss );
  140. }
  141. /*=======================================================================*\
  142. Function: unserialize
  143. \*=======================================================================*/
  144. function unserialize ( $data ) {
  145. return unserialize( $data );
  146. }
  147. /*=======================================================================*\
  148. Function: file_name
  149. Purpose: map url to location in cache
  150. Input: url from wich the rss file was fetched
  151. Output: a file name
  152. \*=======================================================================*/
  153. function file_name ($url) {
  154. $filename = md5( $url );
  155. return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
  156. }
  157. /*=======================================================================*\
  158. Function: error
  159. Purpose: register error
  160. \*=======================================================================*/
  161. function error ($errormsg, $lvl=E_USER_WARNING) {
  162. // append PHP's error message if track_errors enabled
  163. if ( isset($php_errormsg) ) {
  164. $errormsg .= " ($php_errormsg)";
  165. }
  166. $this->ERROR = $errormsg;
  167. if ( MAGPIE_DEBUG ) {
  168. trigger_error( $errormsg, $lvl);
  169. }
  170. else {
  171. error_log( $errormsg, 0);
  172. }
  173. }
  174. function debug ($debugmsg, $lvl=E_USER_NOTICE) {
  175. if ( MAGPIE_DEBUG ) {
  176. $this->error("MagpieRSS [debug] $debugmsg", $lvl);
  177. }
  178. }
  179. }