3 * MagpieRSS: a simple RSS integration tool
5 * A compiled file for RSS syndication
7 * @author Kellan Elliott-McCrea <kellan@protest.net>
12 * @subpackage MagpieRSS
16 * Deprecated. Use SimplePie (class-simplepie.php) instead.
18 _deprecated_file( basename( __FILE__ ), '3.0', WPINC . '/class-simplepie.php' );
21 * Hook to use another RSS object instead of MagpieRSS
23 do_action('load_feed_engine');
25 /** RSS feed constant. */
27 define('ATOM', 'Atom');
28 define('MAGPIE_USER_AGENT', 'WordPress/' . $GLOBALS['wp_version']);
32 var $current_item = array(); // item currently being parsed
33 var $items = array(); // collection of parsed items
34 var $channel = array(); // hash of channel fields
35 var $textinput = array();
41 var $stack = array(); // parser stack
42 var $inchannel = false;
44 var $incontent = false; // if in Atom <content mode="xml"> field
45 var $intextinput = false;
47 var $current_field = '';
48 var $current_namespace = false;
52 var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
54 function MagpieRSS ($source) {
56 # if PHP xml isn't compiled in, die
58 if ( !function_exists('xml_parser_create') )
59 trigger_error( "Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php" );
61 $parser = @xml_parser_create();
63 if ( !is_resource($parser) )
64 trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");
66 $this->parser = $parser;
68 # pass in parser, and a reference to this object
71 xml_set_object( $this->parser, $this );
72 xml_set_element_handler($this->parser,
73 'feed_start_element', 'feed_end_element' );
75 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
77 $status = xml_parse( $this->parser, $source );
80 $errorcode = xml_get_error_code( $this->parser );
81 if ( $errorcode != XML_ERROR_NONE ) {
82 $xml_error = xml_error_string( $errorcode );
83 $error_line = xml_get_current_line_number($this->parser);
84 $error_col = xml_get_current_column_number($this->parser);
85 $errormsg = "$xml_error at line $error_line, column $error_col";
87 $this->error( $errormsg );
91 xml_parser_free( $this->parser );
96 function feed_start_element($p, $element, &$attrs) {
97 $el = $element = strtolower($element);
98 $attrs = array_change_key_case($attrs, CASE_LOWER);
100 // check for a namespace, and split if found
102 if ( strpos( $element, ':' ) ) {
103 list($ns, $el) = split( ':', $element, 2);
105 if ( $ns and $ns != 'rdf' ) {
106 $this->current_namespace = $ns;
109 # if feed type isn't set, then this is first element of feed
110 # identify feed from root element
112 if (!isset($this->feed_type) ) {
113 if ( $el == 'rdf' ) {
114 $this->feed_type = RSS;
115 $this->feed_version = '1.0';
117 elseif ( $el == 'rss' ) {
118 $this->feed_type = RSS;
119 $this->feed_version = $attrs['version'];
121 elseif ( $el == 'feed' ) {
122 $this->feed_type = ATOM;
123 $this->feed_version = $attrs['version'];
124 $this->inchannel = true;
129 if ( $el == 'channel' )
131 $this->inchannel = true;
133 elseif ($el == 'item' or $el == 'entry' )
135 $this->initem = true;
136 if ( isset($attrs['rdf:about']) ) {
137 $this->current_item['about'] = $attrs['rdf:about'];
141 // if we're in the default namespace of an RSS feed,
142 // record textinput or image fields
144 $this->feed_type == RSS and
145 $this->current_namespace == '' and
148 $this->intextinput = true;
152 $this->feed_type == RSS and
153 $this->current_namespace == '' and
156 $this->inimage = true;
159 # handle atom content constructs
160 elseif ( $this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
162 // avoid clashing w/ RSS mod_content
163 if ($el == 'content' ) {
164 $el = 'atom_content';
167 $this->incontent = $el;
171 // if inside an Atom content construct (e.g. content or summary) field treat tags as text
172 elseif ($this->feed_type == ATOM and $this->incontent )
174 // if tags are inlined, then flatten
175 $attrs_str = join(' ',
176 array_map(array('MagpieRSS', 'map_attrs'),
178 array_values($attrs) ) );
180 $this->append_content( "<$element $attrs_str>" );
182 array_unshift( $this->stack, $el );
185 // Atom support many links per containging element.
186 // Magpie treats link elements of type rel='alternate'
187 // as being equivalent to RSS's simple link element.
189 elseif ($this->feed_type == ATOM and $el == 'link' )
191 if ( isset($attrs['rel']) and $attrs['rel'] == 'alternate' )
196 $link_el = 'link_' . $attrs['rel'];
199 $this->append($link_el, $attrs['href']);
201 // set stack[0] to current element
203 array_unshift($this->stack, $el);
207 function feed_cdata ($p, $text) {
209 if ($this->feed_type == ATOM and $this->incontent)
211 $this->append_content( $text );
214 $current_el = join('_', array_reverse($this->stack));
215 $this->append($current_el, $text);
219 function feed_end_element ($p, $el) {
220 $el = strtolower($el);
222 if ( $el == 'item' or $el == 'entry' )
224 $this->items[] = $this->current_item;
225 $this->current_item = array();
226 $this->initem = false;
228 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' )
230 $this->intextinput = false;
232 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' )
234 $this->inimage = false;
236 elseif ($this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
238 $this->incontent = false;
240 elseif ($el == 'channel' or $el == 'feed' )
242 $this->inchannel = false;
244 elseif ($this->feed_type == ATOM and $this->incontent ) {
245 // balance tags properly
246 // note: i don't think this is actually neccessary
247 if ( $this->stack[0] == $el )
249 $this->append_content("</$el>");
252 $this->append_content("<$el />");
255 array_shift( $this->stack );
258 array_shift( $this->stack );
261 $this->current_namespace = false;
264 function concat (&$str1, $str2="") {
265 if (!isset($str1) ) {
271 function append_content($text) {
272 if ( $this->initem ) {
273 $this->concat( $this->current_item[ $this->incontent ], $text );
275 elseif ( $this->inchannel ) {
276 $this->concat( $this->channel[ $this->incontent ], $text );
280 // smart append - field and namespace aware
281 function append($el, $text) {
285 if ( $this->current_namespace )
287 if ( $this->initem ) {
289 $this->current_item[ $this->current_namespace ][ $el ], $text);
291 elseif ($this->inchannel) {
293 $this->channel[ $this->current_namespace][ $el ], $text );
295 elseif ($this->intextinput) {
297 $this->textinput[ $this->current_namespace][ $el ], $text );
299 elseif ($this->inimage) {
301 $this->image[ $this->current_namespace ][ $el ], $text );
305 if ( $this->initem ) {
307 $this->current_item[ $el ], $text);
309 elseif ($this->intextinput) {
311 $this->textinput[ $el ], $text );
313 elseif ($this->inimage) {
315 $this->image[ $el ], $text );
317 elseif ($this->inchannel) {
319 $this->channel[ $el ], $text );
325 function normalize () {
326 // if atom populate rss fields
327 if ( $this->is_atom() ) {
328 $this->channel['descripton'] = $this->channel['tagline'];
329 for ( $i = 0; $i < count($this->items); $i++) {
330 $item = $this->items[$i];
331 if ( isset($item['summary']) )
332 $item['description'] = $item['summary'];
333 if ( isset($item['atom_content']))
334 $item['content']['encoded'] = $item['atom_content'];
336 $this->items[$i] = $item;
339 elseif ( $this->is_rss() ) {
340 $this->channel['tagline'] = $this->channel['description'];
341 for ( $i = 0; $i < count($this->items); $i++) {
342 $item = $this->items[$i];
343 if ( isset($item['description']))
344 $item['summary'] = $item['description'];
345 if ( isset($item['content']['encoded'] ) )
346 $item['atom_content'] = $item['content']['encoded'];
348 $this->items[$i] = $item;
354 if ( $this->feed_type == RSS ) {
355 return $this->feed_version;
363 if ( $this->feed_type == ATOM ) {
364 return $this->feed_version;
371 function map_attrs($k, $v) {
375 function error( $errormsg, $lvl = E_USER_WARNING ) {
376 // append PHP's error message if track_errors enabled
377 if ( isset($php_errormsg) ) {
378 $errormsg .= " ($php_errormsg)";
380 if ( MAGPIE_DEBUG ) {
381 trigger_error( $errormsg, $lvl);
383 error_log( $errormsg, 0);
389 if ( !function_exists('fetch_rss') ) :
391 * Build Magpie object based on RSS from URL.
395 * @subpackage MagpieRSS
397 * @param string $url URL to retrieve feed
398 * @return bool|MagpieRSS false on failure or MagpieRSS object on success.
400 function fetch_rss ($url) {
401 // initialize constants
404 if ( !isset($url) ) {
405 // error("fetch_rss called without a url");
409 // if cache is disabled
410 if ( !MAGPIE_CACHE_ON ) {
411 // fetch file, and parse it
412 $resp = _fetch_remote_file( $url );
413 if ( is_success( $resp->status ) ) {
414 return _response_to_rss( $resp );
417 // error("Failed to fetch $url and cache is off");
425 // 2. if there is a hit, make sure its fresh
426 // 3. if cached obj fails freshness check, fetch remote
427 // 4. if remote fails, return stale object, or error
429 $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
431 if (MAGPIE_DEBUG and $cache->ERROR) {
432 debug($cache->ERROR, E_USER_WARNING);
435 $cache_status = 0; // response of check_cache
436 $request_headers = array(); // HTTP headers to send with fetch
437 $rss = 0; // parsed RSS object
438 $errormsg = 0; // errors, if any
440 if (!$cache->ERROR) {
441 // return cache HIT, MISS, or STALE
442 $cache_status = $cache->check_cache( $url );
445 // if object cached, and cache is fresh, return cached obj
446 if ( $cache_status == 'HIT' ) {
447 $rss = $cache->get( $url );
448 if ( isset($rss) and $rss ) {
449 $rss->from_cache = 1;
450 if ( MAGPIE_DEBUG > 1) {
451 debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
457 // else attempt a conditional get
460 if ( $cache_status == 'STALE' ) {
461 $rss = $cache->get( $url );
462 if ( isset($rss->etag) and $rss->last_modified ) {
463 $request_headers['If-None-Match'] = $rss->etag;
464 $request_headers['If-Last-Modified'] = $rss->last_modified;
468 $resp = _fetch_remote_file( $url, $request_headers );
470 if (isset($resp) and $resp) {
471 if ($resp->status == '304' ) {
472 // we have the most current copy
473 if ( MAGPIE_DEBUG > 1) {
474 debug("Got 304 for $url");
476 // reset cache on 304 (at minutillo insistent prodding)
477 $cache->set($url, $rss);
480 elseif ( is_success( $resp->status ) ) {
481 $rss = _response_to_rss( $resp );
483 if (MAGPIE_DEBUG > 1) {
484 debug("Fetch successful");
486 // add object to cache
487 $cache->set( $url, $rss );
492 $errormsg = "Failed to fetch $url. ";
493 if ( $resp->error ) {
494 # compensate for Snoopy's annoying habbit to tacking
496 $http_error = substr($resp->error, 0, -2);
497 $errormsg .= "(HTTP Error: $http_error)";
500 $errormsg .= "(HTTP Response: " . $resp->response_code .')';
505 $errormsg = "Unable to retrieve RSS file for unknown reasons.";
510 // attempt to return cached object
512 if ( MAGPIE_DEBUG ) {
513 debug("Returning STALE object for $url");
518 // else we totally failed
519 // error( $errormsg );
523 } // end if ( !MAGPIE_CACHE_ON ) {
528 * Retrieve URL headers and content using WP HTTP Request API.
532 * @subpackage MagpieRSS
534 * @param string $url URL to retrieve
535 * @param array $headers Optional. Headers to send to the URL.
536 * @return Snoopy style response
538 function _fetch_remote_file($url, $headers = "" ) {
539 $resp = wp_remote_request($url, array('headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT));
540 if ( is_wp_error($resp) ) {
541 $error = array_shift($resp->errors);
543 $resp = new stdClass;
545 $resp->response_code = 500;
546 $resp->error = $error[0] . "\n"; //\n = Snoopy compatibility
550 // Snoopy returns headers unprocessed.
551 // Also note, WP_HTTP lowercases all keys, Snoopy did not.
552 $return_headers = array();
553 foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
554 if ( !is_array($value) ) {
555 $return_headers[] = "$key: $value";
557 foreach ( $value as $v )
558 $return_headers[] = "$key: $v";
562 $response = new stdClass;
563 $response->status = wp_remote_retrieve_response_code( $resp );
564 $response->response_code = wp_remote_retrieve_response_code( $resp );
565 $response->headers = $return_headers;
566 $response->results = wp_remote_retrieve_body( $resp );
576 * @subpackage MagpieRSS
578 * @param unknown_type $resp
581 function _response_to_rss ($resp) {
582 $rss = new MagpieRSS( $resp->results );
584 // if RSS parsed successfully
585 if ( $rss && (!isset($rss->ERROR) || !$rss->ERROR) ) {
587 // find Etag, and Last-Modified
588 foreach( (array) $resp->headers as $h) {
589 // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
590 if (strpos($h, ": ")) {
591 list($field, $val) = explode(": ", $h, 2);
598 if ( $field == 'etag' ) {
602 if ( $field == 'last-modified' ) {
603 $rss->last_modified = $val;
608 } // else construct error message
610 $errormsg = "Failed to parse RSS file.";
613 $errormsg .= " (" . $rss->ERROR . ")";
618 } // end if ($rss and !$rss->error)
622 * Set up constants with default values, unless user overrides.
626 * @subpackage MagpieRSS
629 if ( defined('MAGPIE_INITALIZED') ) {
633 define('MAGPIE_INITALIZED', 1);
636 if ( !defined('MAGPIE_CACHE_ON') ) {
637 define('MAGPIE_CACHE_ON', 1);
640 if ( !defined('MAGPIE_CACHE_DIR') ) {
641 define('MAGPIE_CACHE_DIR', './cache');
644 if ( !defined('MAGPIE_CACHE_AGE') ) {
645 define('MAGPIE_CACHE_AGE', 60*60); // one hour
648 if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {
649 define('MAGPIE_CACHE_FRESH_ONLY', 0);
652 if ( !defined('MAGPIE_DEBUG') ) {
653 define('MAGPIE_DEBUG', 0);
656 if ( !defined('MAGPIE_USER_AGENT') ) {
657 $ua = 'WordPress/' . $GLOBALS['wp_version'];
659 if ( MAGPIE_CACHE_ON ) {
663 $ua = $ua . '; No cache)';
666 define('MAGPIE_USER_AGENT', $ua);
669 if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
670 define('MAGPIE_FETCH_TIME_OUT', 2); // 2 second timeout
673 // use gzip encoding to fetch rss files if supported?
674 if ( !defined('MAGPIE_USE_GZIP') ) {
675 define('MAGPIE_USE_GZIP', true);
679 function is_info ($sc) {
680 return $sc >= 100 && $sc < 200;
683 function is_success ($sc) {
684 return $sc >= 200 && $sc < 300;
687 function is_redirect ($sc) {
688 return $sc >= 300 && $sc < 400;
691 function is_error ($sc) {
692 return $sc >= 400 && $sc < 600;
695 function is_client_error ($sc) {
696 return $sc >= 400 && $sc < 500;
699 function is_server_error ($sc) {
700 return $sc >= 500 && $sc < 600;
704 var $BASE_CACHE; // where the cache files are stored
705 var $MAX_AGE = 43200; // when are files stale, default twelve hours
706 var $ERROR = ''; // accumulate error messages
708 function RSSCache ($base='', $age='') {
709 $this->BASE_CACHE = WP_CONTENT_DIR . '/cache';
711 $this->BASE_CACHE = $base;
714 $this->MAX_AGE = $age;
719 /*=======================================================================*\
721 Purpose: add an item to the cache, keyed on url
722 Input: url from wich the rss file was fetched
723 Output: true on sucess
724 \*=======================================================================*/
725 function set ($url, $rss) {
726 $cache_option = 'rss_' . $this->file_name( $url );
728 set_transient($cache_option, $rss, $this->MAX_AGE);
730 return $cache_option;
733 /*=======================================================================*\
735 Purpose: fetch an item from the cache
736 Input: url from wich the rss file was fetched
737 Output: cached object on HIT, false on MISS
738 \*=======================================================================*/
739 function get ($url) {
741 $cache_option = 'rss_' . $this->file_name( $url );
743 if ( ! $rss = get_transient( $cache_option ) ) {
745 "Cache doesn't contain: $url (cache option: $cache_option)"
753 /*=======================================================================*\
754 Function: check_cache
755 Purpose: check a url for membership in the cache
756 and whether the object is older then MAX_AGE (ie. STALE)
757 Input: url from wich the rss file was fetched
758 Output: cached object on HIT, false on MISS
759 \*=======================================================================*/
760 function check_cache ( $url ) {
762 $cache_option = 'rss_' . $this->file_name( $url );
764 if ( get_transient($cache_option) ) {
765 // object exists and is current
768 // object does not exist
773 /*=======================================================================*\
775 \*=======================================================================*/
776 function serialize ( $rss ) {
777 return serialize( $rss );
780 /*=======================================================================*\
781 Function: unserialize
782 \*=======================================================================*/
783 function unserialize ( $data ) {
784 return unserialize( $data );
787 /*=======================================================================*\
789 Purpose: map url to location in cache
790 Input: url from wich the rss file was fetched
792 \*=======================================================================*/
793 function file_name ($url) {
797 /*=======================================================================*\
799 Purpose: register error
800 \*=======================================================================*/
801 function error ($errormsg, $lvl=E_USER_WARNING) {
802 // append PHP's error message if track_errors enabled
803 if ( isset($php_errormsg) ) {
804 $errormsg .= " ($php_errormsg)";
806 $this->ERROR = $errormsg;
807 if ( MAGPIE_DEBUG ) {
808 trigger_error( $errormsg, $lvl);
811 error_log( $errormsg, 0);
814 function debug ($debugmsg, $lvl=E_USER_NOTICE) {
815 if ( MAGPIE_DEBUG ) {
816 $this->error("MagpieRSS [debug] $debugmsg", $lvl);
821 if ( !function_exists('parse_w3cdtf') ) :
822 function parse_w3cdtf ( $date_str ) {
824 # regex to match wc3dtf
825 $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
827 if ( preg_match( $pat, $date_str, $match ) ) {
828 list( $year, $month, $day, $hours, $minutes, $seconds) =
829 array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[7]);
831 # calc epoch for current date assuming GMT
832 $epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
835 if ( $match[11] == 'Z' ) {
839 list( $tz_mod, $tz_hour, $tz_min ) =
840 array( $match[8], $match[9], $match[10]);
842 # zero out the variables
843 if ( ! $tz_hour ) { $tz_hour = 0; }
844 if ( ! $tz_min ) { $tz_min = 0; }
846 $offset_secs = (($tz_hour*60)+$tz_min)*60;
848 # is timezone ahead of GMT? then subtract offset
850 if ( $tz_mod == '+' ) {
851 $offset_secs = $offset_secs * -1;
854 $offset = $offset_secs;
856 $epoch = $epoch + $offset;
865 if ( !function_exists('wp_rss') ) :
867 * Display all RSS items in a HTML ordered list.
871 * @subpackage MagpieRSS
873 * @param string $url URL of feed to display. Will not auto sense feed URL.
874 * @param int $num_items Optional. Number of items to display, default is all.
876 function wp_rss( $url, $num_items = -1 ) {
877 if ( $rss = fetch_rss( $url ) ) {
880 if ( $num_items !== -1 ) {
881 $rss->items = array_slice( $rss->items, 0, $num_items );
884 foreach ( (array) $rss->items as $item ) {
886 '<li><a href="%1$s" title="%2$s">%3$s</a></li>',
887 esc_url( $item['link'] ),
888 esc_attr( strip_tags( $item['description'] ) ),
889 esc_html( $item['title'] )
895 _e( 'An error has occurred, which probably means the feed is down. Try again later.' );
900 if ( !function_exists('get_rss') ) :
902 * Display RSS items in HTML list items.
904 * You have to specify which HTML list you want, either ordered or unordered
905 * before using the function. You also have to specify how many items you wish
906 * to display. You can't display all of them like you can with wp_rss()
911 * @subpackage MagpieRSS
913 * @param string $url URL of feed to display. Will not auto sense feed URL.
914 * @param int $num_items Optional. Number of items to display, default is all.
915 * @return bool False on failure.
917 function get_rss ($url, $num_items = 5) { // Like get posts, but for RSS
918 $rss = fetch_rss($url);
920 $rss->items = array_slice($rss->items, 0, $num_items);
921 foreach ( (array) $rss->items as $item ) {
923 echo "<a href='$item[link]' title='$item[description]'>";
924 echo esc_html($item['title']);