Completed
Pull Request — 1.10.x (#1154)
by
unknown
45:16
created

RSSCache::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Project:     MagpieRSS: a simple RSS integration tool
4
 * File:        rss_cache.inc, a simple, rolling(no GC), cache 
5
 *              for RSS objects, keyed on URL.
6
 * Author:      Kellan Elliott-McCrea <[email protected]>
7
 * Version:     0.51
8
 * License:     GPL
9
 *
10
 * The lastest version of MagpieRSS can be obtained from:
11
 * http://magpierss.sourceforge.net
12
 *
13
 * For questions, help, comments, discussion, etc., please join the
14
 * Magpie mailing list:
15
 * http://lists.sourceforge.net/lists/listinfo/magpierss-general
16
 * @package chamilo.include.rss
17
 */
18
/**
19
 * @package chamilo.include.rss
20
 */
21
class RSSCache {
22
    public $BASE_CACHE = './cache';    // where the cache files are stored
23
    public $MAX_AGE    = 3600;         // when are files stale, default one hour
24
    public $ERROR      = "";           // accumulate error messages
25
    
26
    public function RSSCache ($base='', $age='') {
27
        if ( $base ) {
28
            $this->BASE_CACHE = $base;
29
        }
30
        if ( $age ) {
31
            $this->MAX_AGE = $age;
0 ignored issues
show
Documentation Bug introduced by
The property $MAX_AGE was declared of type integer, but $age is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
32
        }
33
        
34
        // attempt to make the cache directory
35
        if ( ! file_exists( $this->BASE_CACHE ) ) {
36
            $status = @mkdir( $this->BASE_CACHE, 0755 );
37
            
38
            // if make failed 
39
            if ( ! $status ) {
40
                $this->error(
41
                    "Cache couldn't make dir '" . $this->BASE_CACHE . "'."
42
                );
43
            }
44
        }
45
    }
46
    
47
/*=======================================================================*\
48
    Function:   set
49
    Purpose:    add an item to the cache, keyed on url
50
    Input:      url from wich the rss file was fetched
51
    Output:     true on sucess  
52
\*=======================================================================*/
53
    public function set ($url, $rss) {
54
        $this->ERROR = "";
55
        $cache_file = $this->file_name( $url );
56
        $fp = @fopen( $cache_file, 'w' );
57
        
58
        if ( ! $fp ) {
59
            $this->error(
60
                "Cache unable to open file for writing: $cache_file"
61
            );
62
            return 0;
63
        }
64
        
65
        
66
        $data = $this->serialize( $rss );
67
        fwrite( $fp, $data );
68
        fclose( $fp );
69
        
70
        return $cache_file;
71
    }
72
    
73
/*=======================================================================*\
74
    Function:   get
75
    Purpose:    fetch an item from the cache
76
    Input:      url from wich the rss file was fetched
77
    Output:     cached object on HIT, false on MISS 
78
\*=======================================================================*/ 
79
    public function get ($url) {
80
        $this->ERROR = "";
81
        $cache_file = $this->file_name( $url );
82
        
83
        if ( ! file_exists( $cache_file ) ) {
84
            $this->debug( 
85
                "Cache doesn't contain: $url (cache file: $cache_file)"
86
            );
87
            return 0;
88
        }
89
        
90
        $fp = @fopen($cache_file, 'r');
91
        if ( ! $fp ) {
92
            $this->error(
93
                "Failed to open cache file for reading: $cache_file"
94
            );
95
            return 0;
96
        }
97
        
98
        if ($filesize = filesize($cache_file) ) {
99
        	$data = fread( $fp, filesize($cache_file) );
100
        	$rss = $this->unserialize( $data );
101
        
102
        	return $rss;
103
    	}
104
    	
105
    	return 0;
106
    }
107
108
/*=======================================================================*\
109
    Function:   check_cache
110
    Purpose:    check a url for membership in the cache
111
                and whether the object is older then MAX_AGE (ie. STALE)
112
    Input:      url from wich the rss file was fetched
113
    Output:     cached object on HIT, false on MISS 
114
\*=======================================================================*/     
115
    public function check_cache ( $url ) {
116
        $this->ERROR = "";
117
        $filename = $this->file_name( $url );
118
        
119
        if ( file_exists( $filename ) ) {
120
            // find how long ago the file was added to the cache
121
            // and whether that is longer then MAX_AGE
122
            $mtime = filemtime( $filename );
123
            $age = time() - $mtime;
124
            if ( $this->MAX_AGE > $age ) {
125
                // object exists and is current
126
                return 'HIT';
127
            }
128
            else {
129
                // object exists but is old
130
                return 'STALE';
131
            }
132
        }
133
        else {
134
            // object does not exist
135
            return 'MISS';
136
        }
137
    }
138
139
	public function cache_age( $url ) {
140
		$filename = $this->file_name( $url);
141
		if ( file_exists( $filename ) ) {
142
			$mtime = filemtime( $filename );
143
            $age = time() - $mtime;
144
			return $age;
145
		}
146
		else {
147
			return -1;	
148
		}
149
	}
150
	
151
/*=======================================================================*\
152
    Function:   serialize
153
\*=======================================================================*/     
154
    public function serialize ( $rss ) {
155
        return serialize( $rss );
156
    }
157
158
/*=======================================================================*\
159
    Function:   unserialize
160
\*=======================================================================*/     
161
    public function unserialize ( $data ) {
162
        return unserialize( $data );
163
    }
164
    
165
/*=======================================================================*\
166
    Function:   file_name
167
    Purpose:    map url to location in cache
168
    Input:      url from wich the rss file was fetched
169
    Output:     a file name
170
\*=======================================================================*/     
171
    public function file_name ($url) {
172
        $filename = md5( $url );
173
        return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
174
    }
175
176
/*=======================================================================*\
177
    Function:   error
178
    Purpose:    register error
179
\*=======================================================================*/         
180
    public function error ($errormsg, $lvl=E_USER_WARNING) {
181
        // append PHP's error message if track_errors enabled
182
        if ( isset($php_errormsg) ) { 
183
            $errormsg .= " ($php_errormsg)";
184
        }
185
        $this->ERROR = $errormsg;
186
        if ( MAGPIE_DEBUG ) {
187
            trigger_error( $errormsg, $lvl);
188
        }
189
        else {
190
            error_log( $errormsg, 0);
191
        }
192
    }
193
    
194
    public function debug ($debugmsg, $lvl=E_USER_NOTICE) {
195
        if ( MAGPIE_DEBUG ) {
196
            $this->error("MagpieRSS [debug] $debugmsg", $lvl);
197
        }
198
    }
199
200
}
201