topical media & game development
mashup-delicious-13-rally2007-lib-rss-fetch.inc / inc
<?php
/*
* Project: MagpieRSS: a simple RSS integration tool
* File: rss_fetch.inc, a simple functional interface
to fetching and parsing RSS files, via the
function fetch_rss()
* Author: Kellan Elliott-McCrea <kellan@protest.net>
* License: GPL
*
* The lastest version of MagpieRSS can be obtained from:
* http://magpierss.sourceforge.net
*
* For questions, help, comments, discussion, etc., please join the
* Magpie mailing list:
* magpierss-general@lists.sourceforge.net
*
*/
// Setup MAGPIE_DIR for use on hosts that don't include
// the current path in include_path.
// with thanks to rajiv and smarty
if (!defined('DIR_SEP')) {
define('DIR_SEP', DIRECTORY_SEPARATOR);
}
if (!defined('MAGPIE_DIR')) {
define('MAGPIE_DIR', dirname(__FILE__) . DIR_SEP);
}
require_once( MAGPIE_DIR . 'rss_parse.inc' );
require_once( MAGPIE_DIR . 'rss_cache.inc' );
// for including 3rd party libraries
define('MAGPIE_EXTLIB', MAGPIE_DIR . 'extlib' . DIR_SEP);
require_once( MAGPIE_EXTLIB . 'Snoopy.class.inc');
/*
* CONSTANTS - redefine these in your script to change the
* behaviour of fetch_rss() currently, most options effect the cache
*
* MAGPIE_CACHE_ON - Should Magpie cache parsed RSS objects?
* For me a built in cache was essential to creating a "PHP-like"
* feel to Magpie, see rss_cache.inc for rationale
*
*
* MAGPIE_CACHE_DIR - Where should Magpie cache parsed RSS objects?
* This should be a location that the webserver can write to. If this
* directory does not already exist Mapie will try to be smart and create
* it. This will often fail for permissions reasons.
*
*
* MAGPIE_CACHE_AGE - How long to store cached RSS objects? In seconds.
*
*
* MAGPIE_CACHE_FRESH_ONLY - If remote fetch fails, throw error
* instead of returning stale object?
*
* MAGPIE_DEBUG - Display debugging notices?
*
*/
/*=======================================================================*\
Function: fetch_rss:
Purpose: return RSS object for the give url
maintain the cache
Input: url of RSS file
Output: parsed RSS object (see rss_parse.inc)
NOTES ON CACHEING:
If caching is on (MAGPIE_CACHE_ON) fetch_rss will first check the cache.
NOTES ON RETRIEVING REMOTE FILES:
If conditional gets are on (MAGPIE_CONDITIONAL_GET_ON) fetch_rss will
return a cached object, and touch the cache object upon recieving a
304.
NOTES ON FAILED REQUESTS:
If there is an HTTP error while fetching an RSS object, the cached
version will be return, if it exists (and if MAGPIE_CACHE_FRESH_ONLY is off)
\*=======================================================================*/
define('MAGPIE_VERSION', '0.72');
url) {
// initialize constants
init();
if ( !isset(resp = _fetch_remote_file( resp->status ) ) {
return _response_to_rss( url and cache is off");
return false;
}
}
// else cache is ON
else {
// Flow
// 1. check cache
// 2. if there is a hit, make sure its fresh
// 3. if cached obj fails freshness check, fetch remote
// 4. if remote fails, return stale object, or error
cache->ERROR) {
debug(cache_status = 0; // response of check_cache
rss = 0; // parsed RSS object
cache_key = cache->ERROR) {
// return cache HIT, MISS, or STALE
cache->check_cache( cache_status == 'HIT' ) {
cache->get( rss) and rss->from_cache = 1;
if ( MAGPIE_DEBUG > 1) {
debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
}
return cache_status == 'STALE' ) {
cache->get( rss and rss->last_modified ) {
rss->etag;
rss->last_modified;
}
}
url, resp) and resp->status == '304' ) {
// we have the most current copy
if ( MAGPIE_DEBUG > 1) {
debug("Got 304 for cache->set(rss);
return resp->status ) ) {
resp );
if ( cache->set( rss );
return errormsg = "Failed to fetch resp->status == '-100' ) {
resp->error ) {
# compensate for Snoopy's annoying habbit to tacking
# on '\n'
resp->error, 0, -2);
http_error)";
}
else {
resp->response_code .')';
}
}
}
else {
rss) {
if ( MAGPIE_DEBUG ) {
debug("Returning STALE object for rss;
}
// else we totally failed
error( errormsg, MAGPIE_ERROR;
// append PHP's error message if track_errors enabled
if ( isset(errormsg .= " (errormsg ) {
errormsg";
errormsg;
trigger_error( lvl);
}
}
function debug (lvl=E_USER_NOTICE) {
trigger_error("MagpieRSS [debug] lvl);
}
/*=======================================================================*\
Function: magpie_error
Purpose: accessor for the magpie error variable
\*=======================================================================*/
function magpie_error (MAGPIE_ERROR;
if ( isset(errormsg ) {
errormsg;
}
return url, client = new Snoopy();
client->read_timeout = MAGPIE_FETCH_TIME_OUT;
headers) ) {
headers;
}
@url);
return resp) {
resp->results, MAGPIE_OUTPUT_ENCODING, MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );
// if RSS parsed successfully
if ( rss->ERROR) {
// find Etag, and Last-Modified
foreach(h) {
// 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
if (strpos(field, h, 2);
}
else {
h;
field == 'ETag' ) {
val;
}
if ( rss->last_modified = rss;
} // else construct error message
else {
rss) {
rss->ERROR . ")";
}
error(rss and !ua = 'MagpieRSS/'. MAGPIE_VERSION . ' (+http://magpierss.sf.net';
if ( MAGPIE_CACHE_ON ) {
ua . ')';
}
else {
ua . '; No cache)';
}
define('MAGPIE_USER_AGENT', sc) {
return sc < 200;
}
/*=======================================================================*\
Function: is_success
Purpose: return true if Successful status code
\*=======================================================================*/
function is_success (sc >= 200 && sc) {
return sc < 400;
}
/*=======================================================================*\
Function: is_error
Purpose: return true if Error status code
\*=======================================================================*/
function is_error (sc >= 400 && sc) {
return sc < 500;
}
/*=======================================================================*\
Function: is_client_error
Purpose: return true if Error status code, and its a server error
\*=======================================================================*/
function is_server_error (sc >= 500 &&
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.