Subversion Repositories Applications.papyrus

Rev

Rev 968 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 968 Rev 1285
1
<?php
1
<?php
2
/*
2
/*
3
 * Project:     MagpieRSS: a simple RSS integration tool
3
 * Project:     MagpieRSS: a simple RSS integration tool
4
 * File:        rss_fetch.inc, a simple functional interface
4
 * File:        rss_fetch.inc, a simple functional interface
5
                to fetching and parsing RSS files, via the
5
                to fetching and parsing RSS files, via the
6
                function fetch_rss()
6
                function fetch_rss()
7
 * Author:      Kellan Elliott-McCrea <kellan@protest.net>
7
 * Author:      Kellan Elliott-McCrea <kellan@protest.net>
8
 * License:     GPL
8
 * License:     GPL
9
 *
9
 *
10
 * The lastest version of MagpieRSS can be obtained from:
10
 * The lastest version of MagpieRSS can be obtained from:
11
 * http://magpierss.sourceforge.net
11
 * http://magpierss.sourceforge.net
12
 *
12
 *
13
 * For questions, help, comments, discussion, etc., please join the
13
 * For questions, help, comments, discussion, etc., please join the
14
 * Magpie mailing list:
14
 * Magpie mailing list:
15
 * magpierss-general@lists.sourceforge.net
15
 * magpierss-general@lists.sourceforge.net
16
 *
16
 *
17
 */
17
 */
18
 
18
 
19
// Setup MAGPIE_DIR for use on hosts that don't include
19
// Setup MAGPIE_DIR for use on hosts that don't include
20
// the current path in include_path.
20
// the current path in include_path.
21
// with thanks to rajiv and smarty
21
// with thanks to rajiv and smarty
22
if (!defined('DIR_SEP')) {
22
if (!defined('DIR_SEP')) {
23
    define('DIR_SEP', DIRECTORY_SEPARATOR);
23
    define('DIR_SEP', DIRECTORY_SEPARATOR);
24
}
24
}
25
 
25
 
26
if (!defined('MAGPIE_DIR')) {
26
if (!defined('MAGPIE_DIR')) {
27
    define('MAGPIE_DIR', dirname(__FILE__) . DIR_SEP);
27
    define('MAGPIE_DIR', dirname(__FILE__) . DIR_SEP);
28
}
28
}
-
 
29
 
-
 
30
define('MAGPIE_CACHE_ON', 0);
29
 
31
 
30
require_once( MAGPIE_DIR . 'rss_parse.inc' );
32
require_once( MAGPIE_DIR . 'rss_parse.inc' );
31
require_once( MAGPIE_DIR . 'rss_cache.inc' );
33
require_once( MAGPIE_DIR . 'rss_cache.inc' );
32
 
34
 
33
// for including 3rd party libraries
35
// for including 3rd party libraries
34
define('MAGPIE_EXTLIB', MAGPIE_DIR . 'extlib' . DIR_SEP);
36
define('MAGPIE_EXTLIB', MAGPIE_DIR . 'extlib' . DIR_SEP);
35
require_once( MAGPIE_EXTLIB . 'Snoopy.class.inc');
37
require_once( MAGPIE_EXTLIB . 'Snoopy.class.inc');
36
 
38
 
37
 
39
 
38
/* 
40
/* 
39
 * CONSTANTS - redefine these in your script to change the
41
 * CONSTANTS - redefine these in your script to change the
40
 * behaviour of fetch_rss() currently, most options effect the cache
42
 * behaviour of fetch_rss() currently, most options effect the cache
41
 *
43
 *
42
 * MAGPIE_CACHE_ON - Should Magpie cache parsed RSS objects? 
44
 * MAGPIE_CACHE_ON - Should Magpie cache parsed RSS objects? 
43
 * For me a built in cache was essential to creating a "PHP-like" 
45
 * For me a built in cache was essential to creating a "PHP-like" 
44
 * feel to Magpie, see rss_cache.inc for rationale
46
 * feel to Magpie, see rss_cache.inc for rationale
45
 *
47
 *
46
 *
48
 *
47
 * MAGPIE_CACHE_DIR - Where should Magpie cache parsed RSS objects?
49
 * MAGPIE_CACHE_DIR - Where should Magpie cache parsed RSS objects?
48
 * This should be a location that the webserver can write to.   If this 
50
 * This should be a location that the webserver can write to.   If this 
49
 * directory does not already exist Mapie will try to be smart and create 
51
 * directory does not already exist Mapie will try to be smart and create 
50
 * it.  This will often fail for permissions reasons.
52
 * it.  This will often fail for permissions reasons.
51
 *
53
 *
52
 *
54
 *
53
 * MAGPIE_CACHE_AGE - How long to store cached RSS objects? In seconds.
55
 * MAGPIE_CACHE_AGE - How long to store cached RSS objects? In seconds.
54
 *
56
 *
55
 *
57
 *
56
 * MAGPIE_CACHE_FRESH_ONLY - If remote fetch fails, throw error
58
 * MAGPIE_CACHE_FRESH_ONLY - If remote fetch fails, throw error
57
 * instead of returning stale object?
59
 * instead of returning stale object?
58
 *
60
 *
59
 * MAGPIE_DEBUG - Display debugging notices?
61
 * MAGPIE_DEBUG - Display debugging notices?
60
 *
62
 *
61
*/
63
*/
62
 
64
 
63
 
65
 
64
/*=======================================================================*\
66
/*=======================================================================*\
65
    Function: fetch_rss: 
67
    Function: fetch_rss: 
66
    Purpose:  return RSS object for the give url
68
    Purpose:  return RSS object for the give url
67
              maintain the cache
69
              maintain the cache
68
    Input:    url of RSS file
70
    Input:    url of RSS file
69
    Output:   parsed RSS object (see rss_parse.inc)
71
    Output:   parsed RSS object (see rss_parse.inc)
70
 
72
 
71
    NOTES ON CACHEING:  
73
    NOTES ON CACHEING:  
72
    If caching is on (MAGPIE_CACHE_ON) fetch_rss will first check the cache.
74
    If caching is on (MAGPIE_CACHE_ON) fetch_rss will first check the cache.
73
    
75
    
74
    NOTES ON RETRIEVING REMOTE FILES:
76
    NOTES ON RETRIEVING REMOTE FILES:
75
    If conditional gets are on (MAGPIE_CONDITIONAL_GET_ON) fetch_rss will
77
    If conditional gets are on (MAGPIE_CONDITIONAL_GET_ON) fetch_rss will
76
    return a cached object, and touch the cache object upon recieving a
78
    return a cached object, and touch the cache object upon recieving a
77
    304.
79
    304.
78
    
80
    
79
    NOTES ON FAILED REQUESTS:
81
    NOTES ON FAILED REQUESTS:
80
    If there is an HTTP error while fetching an RSS object, the cached
82
    If there is an HTTP error while fetching an RSS object, the cached
81
    version will be return, if it exists (and if MAGPIE_CACHE_FRESH_ONLY is off)
83
    version will be return, if it exists (and if MAGPIE_CACHE_FRESH_ONLY is off)
82
\*=======================================================================*/
84
\*=======================================================================*/
83
 
85
 
84
define('MAGPIE_VERSION', '0.72');
86
define('MAGPIE_VERSION', '0.72');
85
 
87
 
86
$MAGPIE_ERROR = "";
88
$MAGPIE_ERROR = "";
87
 
89
 
88
function fetch_rss ($url) {
90
function fetch_rss ($url) {
89
    // initialize constants
91
    // initialize constants
90
    init();
92
    init();
91
    
93
    
92
    if ( !isset($url) ) {
94
    if ( !isset($url) ) {
93
        error("fetch_rss called without a url");
95
        error("fetch_rss called without a url");
94
        return false;
96
        return false;
95
    }
97
    }
96
    
98
    
97
    // if cache is disabled
99
    // if cache is disabled
98
    if ( !MAGPIE_CACHE_ON ) {
100
    if ( !MAGPIE_CACHE_ON ) {
99
        // fetch file, and parse it
101
        // fetch file, and parse it
100
        $resp = _fetch_remote_file( $url );
102
        $resp = _fetch_remote_file( $url );
101
        if ( is_success( $resp->status ) ) {
103
        if ( is_success( $resp->status ) ) {
102
            return _response_to_rss( $resp );
104
            return _response_to_rss( $resp );
103
        }
105
        }
104
        else {
106
        else {
105
            error("Failed to fetch $url and cache is off");
107
            error("Failed to fetch $url and cache is off");
106
            return false;
108
            return false;
107
        }
109
        }
108
    } 
110
    } 
109
    // else cache is ON
111
    // else cache is ON
110
    else {
112
    else {
111
        // Flow
113
        // Flow
112
        // 1. check cache
114
        // 1. check cache
113
        // 2. if there is a hit, make sure its fresh
115
        // 2. if there is a hit, make sure its fresh
114
        // 3. if cached obj fails freshness check, fetch remote
116
        // 3. if cached obj fails freshness check, fetch remote
115
        // 4. if remote fails, return stale object, or error
117
        // 4. if remote fails, return stale object, or error
116
        
118
        
117
        $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
119
        $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
118
        
120
        
119
        if (MAGPIE_DEBUG and $cache->ERROR) {
121
        if (MAGPIE_DEBUG and $cache->ERROR) {
120
            debug($cache->ERROR, E_USER_WARNING);
122
            debug($cache->ERROR, E_USER_WARNING);
121
        }
123
        }
122
        
124
        
123
        
125
        
124
        $cache_status    = 0;       // response of check_cache
126
        $cache_status    = 0;       // response of check_cache
125
        $request_headers = array(); // HTTP headers to send with fetch
127
        $request_headers = array(); // HTTP headers to send with fetch
126
        $rss             = 0;       // parsed RSS object
128
        $rss             = 0;       // parsed RSS object
127
        $errormsg        = 0;       // errors, if any
129
        $errormsg        = 0;       // errors, if any
128
        
130
        
129
        // store parsed XML by desired output encoding
131
        // store parsed XML by desired output encoding
130
        // as character munging happens at parse time
132
        // as character munging happens at parse time
131
        $cache_key       = $url . MAGPIE_OUTPUT_ENCODING;
133
        $cache_key       = $url . MAGPIE_OUTPUT_ENCODING;
132
        
134
        
133
        if (!$cache->ERROR) {
135
        if (!$cache->ERROR) {
134
            // return cache HIT, MISS, or STALE
136
            // return cache HIT, MISS, or STALE
135
            $cache_status = $cache->check_cache( $cache_key);
137
            $cache_status = $cache->check_cache( $cache_key);
136
        }
138
        }
137
                
139
                
138
        // if object cached, and cache is fresh, return cached obj
140
        // if object cached, and cache is fresh, return cached obj
139
        if ( $cache_status == 'HIT' ) {
141
        if ( $cache_status == 'HIT' ) {
140
            $rss = $cache->get( $cache_key );
142
            $rss = $cache->get( $cache_key );
141
            if ( isset($rss) and $rss ) {
143
            if ( isset($rss) and $rss ) {
142
                // should be cache age
144
                // should be cache age
143
                $rss->from_cache = 1;
145
                $rss->from_cache = 1;
144
                if ( MAGPIE_DEBUG > 1) {
146
                if ( MAGPIE_DEBUG > 1) {
145
                    debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
147
                    debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
146
                }
148
                }
147
                return $rss;
149
                return $rss;
148
            }
150
            }
149
        }
151
        }
150
        
152
        
151
        // else attempt a conditional get
153
        // else attempt a conditional get
152
        
154
        
153
        // setup headers
155
        // setup headers
154
        if ( $cache_status == 'STALE' ) {
156
        if ( $cache_status == 'STALE' ) {
155
            $rss = $cache->get( $cache_key );
157
            $rss = $cache->get( $cache_key );
156
            if ( $rss and $rss->etag and $rss->last_modified ) {
158
            if ( $rss and $rss->etag and $rss->last_modified ) {
157
                $request_headers['If-None-Match'] = $rss->etag;
159
                $request_headers['If-None-Match'] = $rss->etag;
158
                $request_headers['If-Last-Modified'] = $rss->last_modified;
160
                $request_headers['If-Last-Modified'] = $rss->last_modified;
159
            }
161
            }
160
        }
162
        }
161
        
163
        
162
        $resp = _fetch_remote_file( $url, $request_headers );
164
        $resp = _fetch_remote_file( $url, $request_headers );
163
        
165
        
164
        if (isset($resp) and $resp) {
166
        if (isset($resp) and $resp) {
165
          if ($resp->status == '304' ) {
167
          if ($resp->status == '304' ) {
166
                // we have the most current copy
168
                // we have the most current copy
167
                if ( MAGPIE_DEBUG > 1) {
169
                if ( MAGPIE_DEBUG > 1) {
168
                    debug("Got 304 for $url");
170
                    debug("Got 304 for $url");
169
                }
171
                }
170
                // reset cache on 304 (at minutillo insistent prodding)
172
                // reset cache on 304 (at minutillo insistent prodding)
171
                $cache->set($cache_key, $rss);
173
                $cache->set($cache_key, $rss);
172
                return $rss;
174
                return $rss;
173
            }
175
            }
174
            elseif ( is_success( $resp->status ) ) {
176
            elseif ( is_success( $resp->status ) ) {
175
                $rss = _response_to_rss( $resp );
177
                $rss = _response_to_rss( $resp );
176
                if ( $rss ) {
178
                if ( $rss ) {
177
                    if (MAGPIE_DEBUG > 1) {
179
                    if (MAGPIE_DEBUG > 1) {
178
                        debug("Fetch successful");
180
                        debug("Fetch successful");
179
                    }
181
                    }
180
                    // add object to cache
182
                    // add object to cache
181
                    $cache->set( $cache_key, $rss );
183
                    $cache->set( $cache_key, $rss );
182
                    return $rss;
184
                    return $rss;
183
                }
185
                }
184
            }
186
            }
185
            else {
187
            else {
186
                $errormsg = "Failed to fetch $url ";
188
                $errormsg = "Failed to fetch $url ";
187
                if ( $resp->status == '-100' ) {
189
                if ( $resp->status == '-100' ) {
188
                    $errormsg .= "(Request timed out after " . MAGPIE_FETCH_TIME_OUT . " seconds)";
190
                    $errormsg .= "(Request timed out after " . MAGPIE_FETCH_TIME_OUT . " seconds)";
189
                }
191
                }
190
                elseif ( $resp->error ) {
192
                elseif ( $resp->error ) {
191
                    # compensate for Snoopy's annoying habbit to tacking
193
                    # compensate for Snoopy's annoying habbit to tacking
192
                    # on '\n'
194
                    # on '\n'
193
                    $http_error = substr($resp->error, 0, -2); 
195
                    $http_error = substr($resp->error, 0, -2); 
194
                    $errormsg .= "(HTTP Error: $http_error)";
196
                    $errormsg .= "(HTTP Error: $http_error)";
195
                }
197
                }
196
                else {
198
                else {
197
                    $errormsg .=  "(HTTP Response: " . $resp->response_code .')';
199
                    $errormsg .=  "(HTTP Response: " . $resp->response_code .')';
198
                }
200
                }
199
            }
201
            }
200
        }
202
        }
201
        else {
203
        else {
202
            $errormsg = "Unable to retrieve RSS file for unknown reasons.";
204
            $errormsg = "Unable to retrieve RSS file for unknown reasons.";
203
        }
205
        }
204
        
206
        
205
        // else fetch failed
207
        // else fetch failed
206
        
208
        
207
        // attempt to return cached object
209
        // attempt to return cached object
208
        if ($rss) {
210
        if ($rss) {
209
            if ( MAGPIE_DEBUG ) {
211
            if ( MAGPIE_DEBUG ) {
210
                debug("Returning STALE object for $url");
212
                debug("Returning STALE object for $url");
211
            }
213
            }
212
            return $rss;
214
            return $rss;
213
        }
215
        }
214
        
216
        
215
        // else we totally failed
217
        // else we totally failed
216
        error( $errormsg ); 
218
        error( $errormsg ); 
217
        
219
        
218
        return false;
220
        return false;
219
        
221
        
220
    } // end if ( !MAGPIE_CACHE_ON ) {
222
    } // end if ( !MAGPIE_CACHE_ON ) {
221
} // end fetch_rss()
223
} // end fetch_rss()
222
 
224
 
223
/*=======================================================================*\
225
/*=======================================================================*\
224
    Function:   error
226
    Function:   error
225
    Purpose:    set MAGPIE_ERROR, and trigger error
227
    Purpose:    set MAGPIE_ERROR, and trigger error
226
\*=======================================================================*/
228
\*=======================================================================*/
227
 
229
 
228
function error ($errormsg, $lvl=E_USER_WARNING) {
230
function error ($errormsg, $lvl=E_USER_WARNING) {
229
        global $MAGPIE_ERROR;
231
        global $MAGPIE_ERROR;
230
        
232
        
231
        // append PHP's error message if track_errors enabled
233
        // append PHP's error message if track_errors enabled
232
        if ( isset($php_errormsg) ) { 
234
        if ( isset($php_errormsg) ) { 
233
            $errormsg .= " ($php_errormsg)";
235
            $errormsg .= " ($php_errormsg)";
234
        }
236
        }
235
        if ( $errormsg ) {
237
        if ( $errormsg ) {
236
            $errormsg = "MagpieRSS: $errormsg";
238
            $errormsg = "MagpieRSS: $errormsg";
237
            $MAGPIE_ERROR = $errormsg;
239
            $MAGPIE_ERROR = $errormsg;
238
            trigger_error( $errormsg, $lvl);                
240
            trigger_error( $errormsg, $lvl);                
239
        }
241
        }
240
}
242
}
241
 
243
 
242
function debug ($debugmsg, $lvl=E_USER_NOTICE) {
244
function debug ($debugmsg, $lvl=E_USER_NOTICE) {
243
    trigger_error("MagpieRSS [debug] $debugmsg", $lvl);
245
    trigger_error("MagpieRSS [debug] $debugmsg", $lvl);
244
}
246
}
245
            
247
            
246
/*=======================================================================*\
248
/*=======================================================================*\
247
    Function:   magpie_error
249
    Function:   magpie_error
248
    Purpose:    accessor for the magpie error variable
250
    Purpose:    accessor for the magpie error variable
249
\*=======================================================================*/
251
\*=======================================================================*/
250
function magpie_error ($errormsg="") {
252
function magpie_error ($errormsg="") {
251
    global $MAGPIE_ERROR;
253
    global $MAGPIE_ERROR;
252
    
254
    
253
    if ( isset($errormsg) and $errormsg ) { 
255
    if ( isset($errormsg) and $errormsg ) { 
254
        $MAGPIE_ERROR = $errormsg;
256
        $MAGPIE_ERROR = $errormsg;
255
    }
257
    }
256
    
258
    
257
    return $MAGPIE_ERROR;   
259
    return $MAGPIE_ERROR;   
258
}
260
}
259
 
261
 
260
/*=======================================================================*\
262
/*=======================================================================*\
261
    Function:   _fetch_remote_file
263
    Function:   _fetch_remote_file
262
    Purpose:    retrieve an arbitrary remote file
264
    Purpose:    retrieve an arbitrary remote file
263
    Input:      url of the remote file
265
    Input:      url of the remote file
264
                headers to send along with the request (optional)
266
                headers to send along with the request (optional)
265
    Output:     an HTTP response object (see Snoopy.class.inc)  
267
    Output:     an HTTP response object (see Snoopy.class.inc)  
266
\*=======================================================================*/
268
\*=======================================================================*/
267
function _fetch_remote_file ($url, $headers = "" ) {
269
function _fetch_remote_file ($url, $headers = "" ) {
268
    // Snoopy is an HTTP client in PHP
270
    // Snoopy is an HTTP client in PHP
269
    $client = new Snoopy();
271
    $client = new Snoopy();
270
    $client->agent = MAGPIE_USER_AGENT;
272
    $client->agent = MAGPIE_USER_AGENT;
271
    $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
273
    $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
272
    $client->use_gzip = MAGPIE_USE_GZIP;
274
    $client->use_gzip = MAGPIE_USE_GZIP;
273
    if (is_array($headers) ) {
275
    if (is_array($headers) ) {
274
        $client->rawheaders = $headers;
276
        $client->rawheaders = $headers;
275
    }
277
    }
276
    
278
    
277
    @$client->fetch($url);
279
    @$client->fetch($url);
278
    return $client;
280
    return $client;
279
 
281
 
280
}
282
}
281
 
283
 
282
/*=======================================================================*\
284
/*=======================================================================*\
283
    Function:   _response_to_rss
285
    Function:   _response_to_rss
284
    Purpose:    parse an HTTP response object into an RSS object
286
    Purpose:    parse an HTTP response object into an RSS object
285
    Input:      an HTTP response object (see Snoopy)
287
    Input:      an HTTP response object (see Snoopy)
286
    Output:     parsed RSS object (see rss_parse)
288
    Output:     parsed RSS object (see rss_parse)
287
\*=======================================================================*/
289
\*=======================================================================*/
288
function _response_to_rss ($resp) {
290
function _response_to_rss ($resp) {
289
    $rss = new MagpieRSS( $resp->results, MAGPIE_OUTPUT_ENCODING, MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );
291
    $rss = new MagpieRSS( $resp->results, MAGPIE_OUTPUT_ENCODING, MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );
290
    
292
    
291
    // if RSS parsed successfully       
293
    // if RSS parsed successfully       
292
    if ( $rss and !$rss->ERROR) {
294
    if ( $rss and !$rss->ERROR) {
293
        
295
        
294
        // find Etag, and Last-Modified
296
        // find Etag, and Last-Modified
295
        foreach($resp->headers as $h) {
297
        foreach($resp->headers as $h) {
296
            // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
298
            // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
297
            if (strpos($h, ": ")) {
299
            if (strpos($h, ": ")) {
298
                list($field, $val) = explode(": ", $h, 2);
300
                list($field, $val) = explode(": ", $h, 2);
299
            }
301
            }
300
            else {
302
            else {
301
                $field = $h;
303
                $field = $h;
302
                $val = "";
304
                $val = "";
303
            }
305
            }
304
            
306
            
305
            if ( $field == 'ETag' ) {
307
            if ( $field == 'ETag' ) {
306
                $rss->etag = $val;
308
                $rss->etag = $val;
307
            }
309
            }
308
            
310
            
309
            if ( $field == 'Last-Modified' ) {
311
            if ( $field == 'Last-Modified' ) {
310
                $rss->last_modified = $val;
312
                $rss->last_modified = $val;
311
            }
313
            }
312
        }
314
        }
313
        
315
        
314
        return $rss;    
316
        return $rss;    
315
    } // else construct error message
317
    } // else construct error message
316
    else {
318
    else {
317
        $errormsg = "Failed to parse RSS file.";
319
        $errormsg = "Failed to parse RSS file.";
318
        
320
        
319
        if ($rss) {
321
        if ($rss) {
320
            $errormsg .= " (" . $rss->ERROR . ")";
322
            $errormsg .= " (" . $rss->ERROR . ")";
321
        }
323
        }
322
        error($errormsg);
324
        error($errormsg);
323
        
325
        
324
        return false;
326
        return false;
325
    } // end if ($rss and !$rss->error)
327
    } // end if ($rss and !$rss->error)
326
}
328
}
327
 
329
 
328
/*=======================================================================*\
330
/*=======================================================================*\
329
    Function:   init
331
    Function:   init
330
    Purpose:    setup constants with default values
332
    Purpose:    setup constants with default values
331
                check for user overrides
333
                check for user overrides
332
\*=======================================================================*/
334
\*=======================================================================*/
333
function init () {
335
function init () {
334
    if ( defined('MAGPIE_INITALIZED') ) {
336
    if ( defined('MAGPIE_INITALIZED') ) {
335
        return;
337
        return;
336
    }
338
    }
337
    else {
339
    else {
338
        define('MAGPIE_INITALIZED', true);
340
        define('MAGPIE_INITALIZED', true);
339
    }
341
    }
340
    
342
    
341
    if ( !defined('MAGPIE_CACHE_ON') ) {
343
    if ( !defined('MAGPIE_CACHE_ON') ) {
342
        define('MAGPIE_CACHE_ON', false);
344
        define('MAGPIE_CACHE_ON', true);
343
    }
345
    }
344
 
346
 
345
    if ( !defined('MAGPIE_CACHE_DIR') ) {
347
    if ( !defined('MAGPIE_CACHE_DIR') ) {
346
        define('MAGPIE_CACHE_DIR', './cache');
348
        define('MAGPIE_CACHE_DIR', './cache');
347
    }
349
    }
348
 
350
 
349
    if ( !defined('MAGPIE_CACHE_AGE') ) {
351
    if ( !defined('MAGPIE_CACHE_AGE') ) {
350
        define('MAGPIE_CACHE_AGE', 60*60); // one hour
352
        define('MAGPIE_CACHE_AGE', 60*60); // one hour
351
    }
353
    }
352
 
354
 
353
    if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {
355
    if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {
354
        define('MAGPIE_CACHE_FRESH_ONLY', false);
356
        define('MAGPIE_CACHE_FRESH_ONLY', false);
355
    }
357
    }
356
 
358
 
357
    if ( !defined('MAGPIE_OUTPUT_ENCODING') ) {
359
    if ( !defined('MAGPIE_OUTPUT_ENCODING') ) {
358
        define('MAGPIE_OUTPUT_ENCODING', 'ISO-8859-1');
360
        define('MAGPIE_OUTPUT_ENCODING', 'ISO-8859-1');
359
    }
361
    }
360
    
362
    
361
    if ( !defined('MAGPIE_INPUT_ENCODING') ) {
363
    if ( !defined('MAGPIE_INPUT_ENCODING') ) {
362
        define('MAGPIE_INPUT_ENCODING', null);
364
        define('MAGPIE_INPUT_ENCODING', null);
363
    }
365
    }
364
    
366
    
365
    if ( !defined('MAGPIE_DETECT_ENCODING') ) {
367
    if ( !defined('MAGPIE_DETECT_ENCODING') ) {
366
        define('MAGPIE_DETECT_ENCODING', true);
368
        define('MAGPIE_DETECT_ENCODING', true);
367
    }
369
    }
368
    
370
    
369
    if ( !defined('MAGPIE_DEBUG') ) {
371
    if ( !defined('MAGPIE_DEBUG') ) {
370
        define('MAGPIE_DEBUG', 0);
372
        define('MAGPIE_DEBUG', 0);
371
    }
373
    }
372
    
374
    
373
    if ( !defined('MAGPIE_USER_AGENT') ) {
375
    if ( !defined('MAGPIE_USER_AGENT') ) {
374
        $ua = 'MagpieRSS/'. MAGPIE_VERSION . ' (+http://magpierss.sf.net';
376
        $ua = 'MagpieRSS/'. MAGPIE_VERSION . ' (+http://magpierss.sf.net';
375
        
377
        
376
        if ( MAGPIE_CACHE_ON ) {
378
        if ( MAGPIE_CACHE_ON ) {
377
            $ua = $ua . ')';
379
            $ua = $ua . ')';
378
        }
380
        }
379
        else {
381
        else {
380
            $ua = $ua . '; No cache)';
382
            $ua = $ua . '; No cache)';
381
        }
383
        }
382
        
384
        
383
        define('MAGPIE_USER_AGENT', $ua);
385
        define('MAGPIE_USER_AGENT', $ua);
384
    }
386
    }
385
    
387
    
386
    if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
388
    if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
387
        define('MAGPIE_FETCH_TIME_OUT', 5); // 5 second timeout
389
        define('MAGPIE_FETCH_TIME_OUT', 5); // 5 second timeout
388
    }
390
    }
389
    
391
    
390
    // use gzip encoding to fetch rss files if supported?
392
    // use gzip encoding to fetch rss files if supported?
391
    if ( !defined('MAGPIE_USE_GZIP') ) {
393
    if ( !defined('MAGPIE_USE_GZIP') ) {
392
        define('MAGPIE_USE_GZIP', true);    
394
        define('MAGPIE_USE_GZIP', true);    
393
    }
395
    }
394
}
396
}
395
 
397
 
396
// NOTE: the following code should really be in Snoopy, or at least
398
// NOTE: the following code should really be in Snoopy, or at least
397
// somewhere other then rss_fetch!
399
// somewhere other then rss_fetch!
398
 
400
 
399
/*=======================================================================*\
401
/*=======================================================================*\
400
    HTTP STATUS CODE PREDICATES
402
    HTTP STATUS CODE PREDICATES
401
    These functions attempt to classify an HTTP status code
403
    These functions attempt to classify an HTTP status code
402
    based on RFC 2616 and RFC 2518.
404
    based on RFC 2616 and RFC 2518.
403
    
405
    
404
    All of them take an HTTP status code as input, and return true or false
406
    All of them take an HTTP status code as input, and return true or false
405
 
407
 
406
    All this code is adapted from LWP's HTTP::Status.
408
    All this code is adapted from LWP's HTTP::Status.
407
\*=======================================================================*/
409
\*=======================================================================*/
408
 
410
 
409
 
411
 
410
/*=======================================================================*\
412
/*=======================================================================*\
411
    Function:   is_info
413
    Function:   is_info
412
    Purpose:    return true if Informational status code
414
    Purpose:    return true if Informational status code
413
\*=======================================================================*/
415
\*=======================================================================*/
414
function is_info ($sc) { 
416
function is_info ($sc) { 
415
    return $sc >= 100 && $sc < 200; 
417
    return $sc >= 100 && $sc < 200; 
416
}
418
}
417
 
419
 
418
/*=======================================================================*\
420
/*=======================================================================*\
419
    Function:   is_success
421
    Function:   is_success
420
    Purpose:    return true if Successful status code
422
    Purpose:    return true if Successful status code
421
\*=======================================================================*/
423
\*=======================================================================*/
422
function is_success ($sc) { 
424
function is_success ($sc) { 
423
    return $sc >= 200 && $sc < 300; 
425
    return $sc >= 200 && $sc < 300; 
424
}
426
}
425
 
427
 
426
/*=======================================================================*\
428
/*=======================================================================*\
427
    Function:   is_redirect
429
    Function:   is_redirect
428
    Purpose:    return true if Redirection status code
430
    Purpose:    return true if Redirection status code
429
\*=======================================================================*/
431
\*=======================================================================*/
430
function is_redirect ($sc) { 
432
function is_redirect ($sc) { 
431
    return $sc >= 300 && $sc < 400; 
433
    return $sc >= 300 && $sc < 400; 
432
}
434
}
433
 
435
 
434
/*=======================================================================*\
436
/*=======================================================================*\
435
    Function:   is_error
437
    Function:   is_error
436
    Purpose:    return true if Error status code
438
    Purpose:    return true if Error status code
437
\*=======================================================================*/
439
\*=======================================================================*/
438
function is_error ($sc) { 
440
function is_error ($sc) { 
439
    return $sc >= 400 && $sc < 600; 
441
    return $sc >= 400 && $sc < 600; 
440
}
442
}
441
 
443
 
442
/*=======================================================================*\
444
/*=======================================================================*\
443
    Function:   is_client_error
445
    Function:   is_client_error
444
    Purpose:    return true if Error status code, and its a client error
446
    Purpose:    return true if Error status code, and its a client error
445
\*=======================================================================*/
447
\*=======================================================================*/
446
function is_client_error ($sc) { 
448
function is_client_error ($sc) { 
447
    return $sc >= 400 && $sc < 500; 
449
    return $sc >= 400 && $sc < 500; 
448
}
450
}
449
 
451
 
450
/*=======================================================================*\
452
/*=======================================================================*\
451
    Function:   is_client_error
453
    Function:   is_client_error
452
    Purpose:    return true if Error status code, and its a server error
454
    Purpose:    return true if Error status code, and its a server error
453
\*=======================================================================*/
455
\*=======================================================================*/
454
function is_server_error ($sc) { 
456
function is_server_error ($sc) { 
455
    return $sc >= 500 && $sc < 600; 
457
    return $sc >= 500 && $sc < 600; 
456
}
458
}
457
 
459
 
458
?>
460
?>