123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- class RSSCache {
- public $BASE_CACHE = './cache';
- public $MAX_AGE = 3600;
- public $ERROR = "";
-
- public function RSSCache ($base='', $age='') {
- if ( $base ) {
- $this->BASE_CACHE = $base;
- }
- if ( $age ) {
- $this->MAX_AGE = $age;
- }
-
-
- if ( ! file_exists( $this->BASE_CACHE ) ) {
- $status = @mkdir( $this->BASE_CACHE, 0755 );
-
-
- if ( ! $status ) {
- $this->error(
- "Cache couldn't make dir '" . $this->BASE_CACHE . "'."
- );
- }
- }
- }
-
- public function set ($url, $rss) {
- $this->ERROR = "";
- $cache_file = $this->file_name( $url );
- $fp = @fopen( $cache_file, 'w' );
-
- if ( ! $fp ) {
- $this->error(
- "Cache unable to open file for writing: $cache_file"
- );
- return 0;
- }
-
-
- $data = $this->serialize( $rss );
- fwrite( $fp, $data );
- fclose( $fp );
-
- return $cache_file;
- }
-
-
- public function get ($url) {
- $this->ERROR = "";
- $cache_file = $this->file_name( $url );
-
- if ( ! file_exists( $cache_file ) ) {
- $this->debug(
- "Cache doesn't contain: $url (cache file: $cache_file)"
- );
- return 0;
- }
-
- $fp = @fopen($cache_file, 'r');
- if ( ! $fp ) {
- $this->error(
- "Failed to open cache file for reading: $cache_file"
- );
- return 0;
- }
-
- if ($filesize = filesize($cache_file) ) {
- $data = fread( $fp, filesize($cache_file) );
- $rss = $this->unserialize( $data );
-
- return $rss;
- }
-
- return 0;
- }
-
- public function check_cache ( $url ) {
- $this->ERROR = "";
- $filename = $this->file_name( $url );
-
- if ( file_exists( $filename ) ) {
-
-
- $mtime = filemtime( $filename );
- $age = time() - $mtime;
- if ( $this->MAX_AGE > $age ) {
-
- return 'HIT';
- }
- else {
-
- return 'STALE';
- }
- }
- else {
-
- return 'MISS';
- }
- }
- public function cache_age( $url ) {
- $filename = $this->file_name( $url);
- if ( file_exists( $filename ) ) {
- $mtime = filemtime( $filename );
- $age = time() - $mtime;
- return $age;
- }
- else {
- return -1;
- }
- }
-
-
- public function serialize ( $rss ) {
- return serialize( $rss );
- }
-
- public function unserialize ( $data ) {
- return unserialize( $data );
- }
-
-
- public function file_name ($url) {
- $filename = md5( $url );
- return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
- }
-
- public function error ($errormsg, $lvl=E_USER_WARNING) {
-
- if ( isset($php_errormsg) ) {
- $errormsg .= " ($php_errormsg)";
- }
- $this->ERROR = $errormsg;
- if ( MAGPIE_DEBUG ) {
- trigger_error( $errormsg, $lvl);
- }
- else {
- error_log( $errormsg, 0);
- }
- }
-
- public function debug ($debugmsg, $lvl=E_USER_NOTICE) {
- if ( MAGPIE_DEBUG ) {
- $this->error("MagpieRSS [debug] $debugmsg", $lvl);
- }
- }
- }
|