3 * Project: MagpieRSS: a simple RSS integration tool
4 * File: A compiled file for RSS syndication
5 * Author: Kellan Elliott-McCrea <kellan@protest.net>
11 define('ATOM', 'Atom');
12 define('MAGPIE_USER_AGENT', 'WordPress/' . $wp_version);
16 var $current_item = array(); // item currently being parsed
17 var $items = array(); // collection of parsed items
18 var $channel = array(); // hash of channel fields
19 var $textinput = array();
25 var $stack = array(); // parser stack
26 var $inchannel = false;
28 var $incontent = false; // if in Atom <content mode="xml"> field
29 var $intextinput = false;
31 var $current_field = '';
32 var $current_namespace = false;
36 var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
38 function MagpieRSS ($source) {
40 # if PHP xml isn't compiled in, die
42 if ( !function_exists('xml_parser_create') )
43 trigger_error( "Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php" );
45 $parser = @xml_parser_create();
47 if ( !is_resource($parser) )
48 trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");
51 $this->parser = $parser;
53 # pass in parser, and a reference to this object
56 xml_set_object( $this->parser, $this );
57 xml_set_element_handler($this->parser,
58 'feed_start_element', 'feed_end_element' );
60 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
62 $status = xml_parse( $this->parser, $source );
65 $errorcode = xml_get_error_code( $this->parser );
66 if ( $errorcode != XML_ERROR_NONE ) {
67 $xml_error = xml_error_string( $errorcode );
68 $error_line = xml_get_current_line_number($this->parser);
69 $error_col = xml_get_current_column_number($this->parser);
70 $errormsg = "$xml_error at line $error_line, column $error_col";
72 $this->error( $errormsg );
76 xml_parser_free( $this->parser );
81 function feed_start_element($p, $element, &$attrs) {
82 $el = $element = strtolower($element);
83 $attrs = array_change_key_case($attrs, CASE_LOWER);
85 // check for a namespace, and split if found
87 if ( strpos( $element, ':' ) ) {
88 list($ns, $el) = split( ':', $element, 2);
90 if ( $ns and $ns != 'rdf' ) {
91 $this->current_namespace = $ns;
94 # if feed type isn't set, then this is first element of feed
95 # identify feed from root element
97 if (!isset($this->feed_type) ) {
99 $this->feed_type = RSS;
100 $this->feed_version = '1.0';
102 elseif ( $el == 'rss' ) {
103 $this->feed_type = RSS;
104 $this->feed_version = $attrs['version'];
106 elseif ( $el == 'feed' ) {
107 $this->feed_type = ATOM;
108 $this->feed_version = $attrs['version'];
109 $this->inchannel = true;
114 if ( $el == 'channel' )
116 $this->inchannel = true;
118 elseif ($el == 'item' or $el == 'entry' )
120 $this->initem = true;
121 if ( isset($attrs['rdf:about']) ) {
122 $this->current_item['about'] = $attrs['rdf:about'];
126 // if we're in the default namespace of an RSS feed,
127 // record textinput or image fields
129 $this->feed_type == RSS and
130 $this->current_namespace == '' and
133 $this->intextinput = true;
137 $this->feed_type == RSS and
138 $this->current_namespace == '' and
141 $this->inimage = true;
144 # handle atom content constructs
145 elseif ( $this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
147 // avoid clashing w/ RSS mod_content
148 if ($el == 'content' ) {
149 $el = 'atom_content';
152 $this->incontent = $el;
157 // if inside an Atom content construct (e.g. content or summary) field treat tags as text
158 elseif ($this->feed_type == ATOM and $this->incontent )
160 // if tags are inlined, then flatten
161 $attrs_str = join(' ',
162 array_map('map_attrs',
164 array_values($attrs) ) );
166 $this->append_content( "<$element $attrs_str>" );
168 array_unshift( $this->stack, $el );
171 // Atom support many links per containging element.
172 // Magpie treats link elements of type rel='alternate'
173 // as being equivalent to RSS's simple link element.
175 elseif ($this->feed_type == ATOM and $el == 'link' )
177 if ( isset($attrs['rel']) and $attrs['rel'] == 'alternate' )
182 $link_el = 'link_' . $attrs['rel'];
185 $this->append($link_el, $attrs['href']);
187 // set stack[0] to current element
189 array_unshift($this->stack, $el);
195 function feed_cdata ($p, $text) {
197 if ($this->feed_type == ATOM and $this->incontent)
199 $this->append_content( $text );
202 $current_el = join('_', array_reverse($this->stack));
203 $this->append($current_el, $text);
207 function feed_end_element ($p, $el) {
208 $el = strtolower($el);
210 if ( $el == 'item' or $el == 'entry' )
212 $this->items[] = $this->current_item;
213 $this->current_item = array();
214 $this->initem = false;
216 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' )
218 $this->intextinput = false;
220 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' )
222 $this->inimage = false;
224 elseif ($this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
226 $this->incontent = false;
228 elseif ($el == 'channel' or $el == 'feed' )
230 $this->inchannel = false;
232 elseif ($this->feed_type == ATOM and $this->incontent ) {
233 // balance tags properly
234 // note: i don't think this is actually neccessary
235 if ( $this->stack[0] == $el )
237 $this->append_content("</$el>");
240 $this->append_content("<$el />");
243 array_shift( $this->stack );
246 array_shift( $this->stack );
249 $this->current_namespace = false;
252 function concat (&$str1, $str2="") {
253 if (!isset($str1) ) {
259 function append_content($text) {
260 if ( $this->initem ) {
261 $this->concat( $this->current_item[ $this->incontent ], $text );
263 elseif ( $this->inchannel ) {
264 $this->concat( $this->channel[ $this->incontent ], $text );
268 // smart append - field and namespace aware
269 function append($el, $text) {
273 if ( $this->current_namespace )
275 if ( $this->initem ) {
277 $this->current_item[ $this->current_namespace ][ $el ], $text);
279 elseif ($this->inchannel) {
281 $this->channel[ $this->current_namespace][ $el ], $text );
283 elseif ($this->intextinput) {
285 $this->textinput[ $this->current_namespace][ $el ], $text );
287 elseif ($this->inimage) {
289 $this->image[ $this->current_namespace ][ $el ], $text );
293 if ( $this->initem ) {
295 $this->current_item[ $el ], $text);
297 elseif ($this->intextinput) {
299 $this->textinput[ $el ], $text );
301 elseif ($this->inimage) {
303 $this->image[ $el ], $text );
305 elseif ($this->inchannel) {
307 $this->channel[ $el ], $text );
313 function normalize () {
314 // if atom populate rss fields
315 if ( $this->is_atom() ) {
316 $this->channel['descripton'] = $this->channel['tagline'];
317 for ( $i = 0; $i < count($this->items); $i++) {
318 $item = $this->items[$i];
319 if ( isset($item['summary']) )
320 $item['description'] = $item['summary'];
321 if ( isset($item['atom_content']))
322 $item['content']['encoded'] = $item['atom_content'];
324 $this->items[$i] = $item;
327 elseif ( $this->is_rss() ) {
328 $this->channel['tagline'] = $this->channel['description'];
329 for ( $i = 0; $i < count($this->items); $i++) {
330 $item = $this->items[$i];
331 if ( isset($item['description']))
332 $item['summary'] = $item['description'];
333 if ( isset($item['content']['encoded'] ) )
334 $item['atom_content'] = $item['content']['encoded'];
336 $this->items[$i] = $item;
342 if ( $this->feed_type == RSS ) {
343 return $this->feed_version;
351 if ( $this->feed_type == ATOM ) {
352 return $this->feed_version;
359 function map_attrs($k, $v) {
363 function error( $errormsg, $lvl = E_USER_WARNING ) {
364 // append PHP's error message if track_errors enabled
365 if ( isset($php_errormsg) ) {
366 $errormsg .= " ($php_errormsg)";
368 if ( MAGPIE_DEBUG ) {
369 trigger_error( $errormsg, $lvl);
371 error_log( $errormsg, 0);
376 require_once( dirname(__FILE__) . '/class-snoopy.php');
378 function fetch_rss ($url) {
379 // initialize constants
382 if ( !isset($url) ) {
383 // error("fetch_rss called without a url");
387 // if cache is disabled
388 if ( !MAGPIE_CACHE_ON ) {
389 // fetch file, and parse it
390 $resp = _fetch_remote_file( $url );
391 if ( is_success( $resp->status ) ) {
392 return _response_to_rss( $resp );
395 // error("Failed to fetch $url and cache is off");
403 // 2. if there is a hit, make sure its fresh
404 // 3. if cached obj fails freshness check, fetch remote
405 // 4. if remote fails, return stale object, or error
407 $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
409 if (MAGPIE_DEBUG and $cache->ERROR) {
410 debug($cache->ERROR, E_USER_WARNING);
414 $cache_status = 0; // response of check_cache
415 $request_headers = array(); // HTTP headers to send with fetch
416 $rss = 0; // parsed RSS object
417 $errormsg = 0; // errors, if any
419 if (!$cache->ERROR) {
420 // return cache HIT, MISS, or STALE
421 $cache_status = $cache->check_cache( $url );
424 // if object cached, and cache is fresh, return cached obj
425 if ( $cache_status == 'HIT' ) {
426 $rss = $cache->get( $url );
427 if ( isset($rss) and $rss ) {
428 $rss->from_cache = 1;
429 if ( MAGPIE_DEBUG > 1) {
430 debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
436 // else attempt a conditional get
439 if ( $cache_status == 'STALE' ) {
440 $rss = $cache->get( $url );
441 if ( $rss->etag and $rss->last_modified ) {
442 $request_headers['If-None-Match'] = $rss->etag;
443 $request_headers['If-Last-Modified'] = $rss->last_modified;
447 $resp = _fetch_remote_file( $url, $request_headers );
449 if (isset($resp) and $resp) {
450 if ($resp->status == '304' ) {
451 // we have the most current copy
452 if ( MAGPIE_DEBUG > 1) {
453 debug("Got 304 for $url");
455 // reset cache on 304 (at minutillo insistent prodding)
456 $cache->set($url, $rss);
459 elseif ( is_success( $resp->status ) ) {
460 $rss = _response_to_rss( $resp );
462 if (MAGPIE_DEBUG > 1) {
463 debug("Fetch successful");
465 // add object to cache
466 $cache->set( $url, $rss );
471 $errormsg = "Failed to fetch $url. ";
472 if ( $resp->error ) {
473 # compensate for Snoopy's annoying habbit to tacking
475 $http_error = substr($resp->error, 0, -2);
476 $errormsg .= "(HTTP Error: $http_error)";
479 $errormsg .= "(HTTP Response: " . $resp->response_code .')';
484 $errormsg = "Unable to retrieve RSS file for unknown reasons.";
489 // attempt to return cached object
491 if ( MAGPIE_DEBUG ) {
492 debug("Returning STALE object for $url");
497 // else we totally failed
498 // error( $errormsg );
502 } // end if ( !MAGPIE_CACHE_ON ) {
505 function _fetch_remote_file ($url, $headers = "" ) {
506 // Snoopy is an HTTP client in PHP
507 $client = new Snoopy();
508 $client->agent = MAGPIE_USER_AGENT;
509 $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
510 $client->use_gzip = MAGPIE_USE_GZIP;
511 if (is_array($headers) ) {
512 $client->rawheaders = $headers;
515 @$client->fetch($url);
520 function _response_to_rss ($resp) {
521 $rss = new MagpieRSS( $resp->results );
523 // if RSS parsed successfully
524 if ( $rss and !$rss->ERROR) {
526 // find Etag, and Last-Modified
527 foreach($resp->headers as $h) {
528 // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
529 if (strpos($h, ": ")) {
530 list($field, $val) = explode(": ", $h, 2);
537 if ( $field == 'ETag' ) {
541 if ( $field == 'Last-Modified' ) {
542 $rss->last_modified = $val;
547 } // else construct error message
549 $errormsg = "Failed to parse RSS file.";
552 $errormsg .= " (" . $rss->ERROR . ")";
557 } // end if ($rss and !$rss->error)
560 /*=======================================================================*\
562 Purpose: setup constants with default values
563 check for user overrides
564 \*=======================================================================*/
566 if ( defined('MAGPIE_INITALIZED') ) {
570 define('MAGPIE_INITALIZED', 1);
573 if ( !defined('MAGPIE_CACHE_ON') ) {
574 define('MAGPIE_CACHE_ON', 1);
577 if ( !defined('MAGPIE_CACHE_DIR') ) {
578 define('MAGPIE_CACHE_DIR', './cache');
581 if ( !defined('MAGPIE_CACHE_AGE') ) {
582 define('MAGPIE_CACHE_AGE', 60*60); // one hour
585 if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {
586 define('MAGPIE_CACHE_FRESH_ONLY', 0);
589 if ( !defined('MAGPIE_DEBUG') ) {
590 define('MAGPIE_DEBUG', 0);
593 if ( !defined('MAGPIE_USER_AGENT') ) {
594 $ua = 'WordPress/' . $wp_version;
596 if ( MAGPIE_CACHE_ON ) {
600 $ua = $ua . '; No cache)';
603 define('MAGPIE_USER_AGENT', $ua);
606 if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
607 define('MAGPIE_FETCH_TIME_OUT', 2); // 2 second timeout
610 // use gzip encoding to fetch rss files if supported?
611 if ( !defined('MAGPIE_USE_GZIP') ) {
612 define('MAGPIE_USE_GZIP', true);
616 function is_info ($sc) {
617 return $sc >= 100 && $sc < 200;
620 function is_success ($sc) {
621 return $sc >= 200 && $sc < 300;
624 function is_redirect ($sc) {
625 return $sc >= 300 && $sc < 400;
628 function is_error ($sc) {
629 return $sc >= 400 && $sc < 600;
632 function is_client_error ($sc) {
633 return $sc >= 400 && $sc < 500;
636 function is_server_error ($sc) {
637 return $sc >= 500 && $sc < 600;
641 var $BASE_CACHE = 'wp-content/cache'; // where the cache files are stored
642 var $MAX_AGE = 43200; // when are files stale, default twelve hours
643 var $ERROR = ''; // accumulate error messages
645 function RSSCache ($base='', $age='') {
647 $this->BASE_CACHE = $base;
650 $this->MAX_AGE = $age;
655 /*=======================================================================*\
657 Purpose: add an item to the cache, keyed on url
658 Input: url from wich the rss file was fetched
659 Output: true on sucess
660 \*=======================================================================*/
661 function set ($url, $rss) {
663 $cache_option = 'rss_' . $this->file_name( $url );
664 $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
666 if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$cache_option'") )
667 add_option($cache_option, '', '', 'no');
668 if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$cache_timestamp'") )
669 add_option($cache_timestamp, '', '', 'no');
671 update_option($cache_option, $rss);
672 update_option($cache_timestamp, time() );
674 return $cache_option;
677 /*=======================================================================*\
679 Purpose: fetch an item from the cache
680 Input: url from wich the rss file was fetched
681 Output: cached object on HIT, false on MISS
682 \*=======================================================================*/
683 function get ($url) {
685 $cache_option = 'rss_' . $this->file_name( $url );
687 if ( ! get_option( $cache_option ) ) {
689 "Cache doesn't contain: $url (cache option: $cache_option)"
694 $rss = get_option( $cache_option );
699 /*=======================================================================*\
700 Function: check_cache
701 Purpose: check a url for membership in the cache
702 and whether the object is older then MAX_AGE (ie. STALE)
703 Input: url from wich the rss file was fetched
704 Output: cached object on HIT, false on MISS
705 \*=======================================================================*/
706 function check_cache ( $url ) {
708 $cache_option = $this->file_name( $url );
709 $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
711 if ( $mtime = get_option($cache_timestamp) ) {
712 // find how long ago the file was added to the cache
713 // and whether that is longer then MAX_AGE
714 $age = time() - $mtime;
715 if ( $this->MAX_AGE > $age ) {
716 // object exists and is current
720 // object exists but is old
725 // object does not exist
730 /*=======================================================================*\
732 \*=======================================================================*/
733 function serialize ( $rss ) {
734 return serialize( $rss );
737 /*=======================================================================*\
738 Function: unserialize
739 \*=======================================================================*/
740 function unserialize ( $data ) {
741 return unserialize( $data );
744 /*=======================================================================*\
746 Purpose: map url to location in cache
747 Input: url from wich the rss file was fetched
749 \*=======================================================================*/
750 function file_name ($url) {
754 /*=======================================================================*\
756 Purpose: register error
757 \*=======================================================================*/
758 function error ($errormsg, $lvl=E_USER_WARNING) {
759 // append PHP's error message if track_errors enabled
760 if ( isset($php_errormsg) ) {
761 $errormsg .= " ($php_errormsg)";
763 $this->ERROR = $errormsg;
764 if ( MAGPIE_DEBUG ) {
765 trigger_error( $errormsg, $lvl);
768 error_log( $errormsg, 0);
771 function debug ($debugmsg, $lvl=E_USER_NOTICE) {
772 if ( MAGPIE_DEBUG ) {
773 $this->error("MagpieRSS [debug] $debugmsg", $lvl);
778 function parse_w3cdtf ( $date_str ) {
780 # regex to match wc3dtf
781 $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
783 if ( preg_match( $pat, $date_str, $match ) ) {
784 list( $year, $month, $day, $hours, $minutes, $seconds) =
785 array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
787 # calc epoch for current date assuming GMT
788 $epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
791 if ( $match[10] == 'Z' ) {
795 list( $tz_mod, $tz_hour, $tz_min ) =
796 array( $match[8], $match[9], $match[10]);
798 # zero out the variables
799 if ( ! $tz_hour ) { $tz_hour = 0; }
800 if ( ! $tz_min ) { $tz_min = 0; }
802 $offset_secs = (($tz_hour*60)+$tz_min)*60;
804 # is timezone ahead of GMT? then subtract offset
806 if ( $tz_mod == '+' ) {
807 $offset_secs = $offset_secs * -1;
810 $offset = $offset_secs;
812 $epoch = $epoch + $offset;
819 function wp_rss ($url, $num_items) {
820 //ini_set("display_errors", false); uncomment to suppress php errors thrown if the feed is not returned.
821 $rss = fetch_rss($url);
824 $rss->items = array_slice($rss->items, 0, $num_items);
825 foreach ($rss->items as $item ) {
827 echo "<a href='$item[link]' title='$item[description]'>";
828 echo htmlentities($item['title']);
835 echo "an error has occured the feed is probably down, try again later.";
839 function get_rss ($url, $num_items = 5) { // Like get posts, but for RSS
840 $rss = fetch_rss($url);
842 $rss->items = array_slice($rss->items, 0, $num_items);
843 foreach ($rss->items as $item ) {
845 echo "<a href='$item[link]' title='$item[description]'>";
846 echo htmlentities($item['title']);