00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 class RSSCache {
00020 var $BASE_CACHE = './cache';
00021 var $MAX_AGE = 3600;
00022 var $ERROR = "";
00023
00024 function RSSCache ($base='', $age='') {
00025 if ( $base ) {
00026 $this->BASE_CACHE = $base;
00027 }
00028 if ( $age ) {
00029 $this->MAX_AGE = $age;
00030 }
00031
00032
00033 if ( ! file_exists( $this->BASE_CACHE ) ) {
00034 $status = @mkdir( $this->BASE_CACHE, 0755 );
00035
00036
00037 if ( ! $status ) {
00038 $this->error(
00039 "Cache couldn't make dir '" . $this->BASE_CACHE . "'."
00040 );
00041 }
00042 }
00043 }
00044
00045
00046
00047
00048
00049
00050
00051 function set ($url, $rss) {
00052 $this->ERROR = "";
00053 $cache_file = $this->file_name( $url );
00054 $fp = @fopen( $cache_file, 'w' );
00055
00056 if ( ! $fp ) {
00057 $this->error(
00058 "Cache unable to open file for writing: $cache_file"
00059 );
00060 return 0;
00061 }
00062
00063
00064 $data = $this->serialize( $rss );
00065 fwrite( $fp, $data );
00066 fclose( $fp );
00067
00068 return $cache_file;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 function get ($url) {
00078 $this->ERROR = "";
00079 $cache_file = $this->file_name( $url );
00080
00081 if ( ! file_exists( $cache_file ) ) {
00082 $this->debug(
00083 "Cache doesn't contain: $url (cache file: $cache_file)"
00084 );
00085 return 0;
00086 }
00087
00088 $fp = @fopen($cache_file, 'r');
00089 if ( ! $fp ) {
00090 $this->error(
00091 "Failed to open cache file for reading: $cache_file"
00092 );
00093 return 0;
00094 }
00095
00096 if ($filesize = filesize($cache_file) ) {
00097 $data = fread( $fp, filesize($cache_file) );
00098 $rss = $this->unserialize( $data );
00099
00100 return $rss;
00101 }
00102
00103 return 0;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113 function check_cache ( $url ) {
00114 $this->ERROR = "";
00115 $filename = $this->file_name( $url );
00116
00117 if ( file_exists( $filename ) ) {
00118
00119
00120 $mtime = filemtime( $filename );
00121 $age = time() - $mtime;
00122 if ( $this->MAX_AGE > $age ) {
00123
00124 return 'HIT';
00125 }
00126 else {
00127
00128 return 'STALE';
00129 }
00130 }
00131 else {
00132
00133 return 'MISS';
00134 }
00135 }
00136
00137 function cache_age( $cache_key ) {
00138 $filename = $this->file_name( $url );
00139 if ( file_exists( $filename ) ) {
00140 $mtime = filemtime( $filename );
00141 $age = time() - $mtime;
00142 return $age;
00143 }
00144 else {
00145 return -1;
00146 }
00147 }
00148
00149
00150
00151
00152 function serialize ( $rss ) {
00153 return serialize( $rss );
00154 }
00155
00156
00157
00158
00159 function unserialize ( $data ) {
00160 return unserialize( $data );
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 function file_name ($url) {
00170 $filename = md5( $url );
00171 return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
00172 }
00173
00174
00175
00176
00177
00178 function error ($errormsg, $lvl=E_USER_WARNING) {
00179
00180 if ( isset($php_errormsg) ) {
00181 $errormsg .= " ($php_errormsg)";
00182 }
00183 $this->ERROR = $errormsg;
00184 if ( MAGPIE_DEBUG ) {
00185 trigger_error( $errormsg, $lvl);
00186 }
00187 else {
00188 error_log( $errormsg, 0);
00189 }
00190 }
00191
00192 function debug ($debugmsg, $lvl=E_USER_NOTICE) {
00193 if ( MAGPIE_DEBUG ) {
00194 $this->error("MagpieRSS [debug] $debugmsg", $lvl);
00195 }
00196 }
00197
00198 }
00199
00200 ?>