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
13 * @deprecated 3.0.0 Use SimplePie instead.
17 * Deprecated. Use SimplePie (class-simplepie.php) instead.
19 _deprecated_file( basename( __FILE__ ), '3.0.0', WPINC . '/class-simplepie.php' );
22 * Fires before MagpieRSS is loaded, to optionally replace it.
27 do_action( 'load_feed_engine' );
29 /** RSS feed constant. */
31 define('ATOM', 'Atom');
32 define('MAGPIE_USER_AGENT', 'WordPress/' . $GLOBALS['wp_version']);
36 var $current_item = array(); // item currently being parsed
37 var $items = array(); // collection of parsed items
38 var $channel = array(); // hash of channel fields
39 var $textinput = array();
45 var $stack = array(); // parser stack
46 var $inchannel = false;
48 var $incontent = false; // if in Atom <content mode="xml"> field
49 var $intextinput = false;
51 var $current_field = '';
52 var $current_namespace = false;
56 var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
61 function __construct( $source ) {
63 # Check if PHP xml isn't compiled
65 if ( ! function_exists('xml_parser_create') ) {
66 return trigger_error( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." );
69 $parser = xml_parser_create();
71 $this->parser = $parser;
73 # pass in parser, and a reference to this object
76 xml_set_object( $this->parser, $this );
77 xml_set_element_handler($this->parser,
78 'feed_start_element', 'feed_end_element' );
80 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
82 $status = xml_parse( $this->parser, $source );
85 $errorcode = xml_get_error_code( $this->parser );
86 if ( $errorcode != XML_ERROR_NONE ) {
87 $xml_error = xml_error_string( $errorcode );
88 $error_line = xml_get_current_line_number($this->parser);
89 $error_col = xml_get_current_column_number($this->parser);
90 $errormsg = "$xml_error at line $error_line, column $error_col";
92 $this->error( $errormsg );
96 xml_parser_free( $this->parser );
104 public function MagpieRSS( $source ) {
105 self::__construct( $source );
108 function feed_start_element($p, $element, &$attrs) {
109 $el = $element = strtolower($element);
110 $attrs = array_change_key_case($attrs, CASE_LOWER);
112 // check for a namespace, and split if found
114 if ( strpos( $element, ':' ) ) {
115 list($ns, $el) = explode( ':', $element, 2);
117 if ( $ns and $ns != 'rdf' ) {
118 $this->current_namespace = $ns;
121 # if feed type isn't set, then this is first element of feed
122 # identify feed from root element
124 if (!isset($this->feed_type) ) {
125 if ( $el == 'rdf' ) {
126 $this->feed_type = RSS;
127 $this->feed_version = '1.0';
129 elseif ( $el == 'rss' ) {
130 $this->feed_type = RSS;
131 $this->feed_version = $attrs['version'];
133 elseif ( $el == 'feed' ) {
134 $this->feed_type = ATOM;
135 $this->feed_version = $attrs['version'];
136 $this->inchannel = true;
141 if ( $el == 'channel' )
143 $this->inchannel = true;
145 elseif ($el == 'item' or $el == 'entry' )
147 $this->initem = true;
148 if ( isset($attrs['rdf:about']) ) {
149 $this->current_item['about'] = $attrs['rdf:about'];
153 // if we're in the default namespace of an RSS feed,
154 // record textinput or image fields
156 $this->feed_type == RSS and
157 $this->current_namespace == '' and
160 $this->intextinput = true;
164 $this->feed_type == RSS and
165 $this->current_namespace == '' and
168 $this->inimage = true;
171 # handle atom content constructs
172 elseif ( $this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
174 // avoid clashing w/ RSS mod_content
175 if ($el == 'content' ) {
176 $el = 'atom_content';
179 $this->incontent = $el;
183 // if inside an Atom content construct (e.g. content or summary) field treat tags as text
184 elseif ($this->feed_type == ATOM and $this->incontent )
186 // if tags are inlined, then flatten
187 $attrs_str = join(' ',
188 array_map(array('MagpieRSS', 'map_attrs'),
190 array_values($attrs) ) );
192 $this->append_content( "<$element $attrs_str>" );
194 array_unshift( $this->stack, $el );
197 // Atom support many links per containging element.
198 // Magpie treats link elements of type rel='alternate'
199 // as being equivalent to RSS's simple link element.
201 elseif ($this->feed_type == ATOM and $el == 'link' )
203 if ( isset($attrs['rel']) and $attrs['rel'] == 'alternate' )
208 $link_el = 'link_' . $attrs['rel'];
211 $this->append($link_el, $attrs['href']);
213 // set stack[0] to current element
215 array_unshift($this->stack, $el);
219 function feed_cdata ($p, $text) {
221 if ($this->feed_type == ATOM and $this->incontent)
223 $this->append_content( $text );
226 $current_el = join('_', array_reverse($this->stack));
227 $this->append($current_el, $text);
231 function feed_end_element ($p, $el) {
232 $el = strtolower($el);
234 if ( $el == 'item' or $el == 'entry' )
236 $this->items[] = $this->current_item;
237 $this->current_item = array();
238 $this->initem = false;
240 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' )
242 $this->intextinput = false;
244 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' )
246 $this->inimage = false;
248 elseif ($this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
250 $this->incontent = false;
252 elseif ($el == 'channel' or $el == 'feed' )
254 $this->inchannel = false;
256 elseif ($this->feed_type == ATOM and $this->incontent ) {
257 // balance tags properly
258 // note: This may not actually be necessary
259 if ( $this->stack[0] == $el )
261 $this->append_content("</$el>");
264 $this->append_content("<$el />");
267 array_shift( $this->stack );
270 array_shift( $this->stack );
273 $this->current_namespace = false;
276 function concat (&$str1, $str2="") {
277 if (!isset($str1) ) {
283 function append_content($text) {
284 if ( $this->initem ) {
285 $this->concat( $this->current_item[ $this->incontent ], $text );
287 elseif ( $this->inchannel ) {
288 $this->concat( $this->channel[ $this->incontent ], $text );
292 // smart append - field and namespace aware
293 function append($el, $text) {
297 if ( $this->current_namespace )
299 if ( $this->initem ) {
301 $this->current_item[ $this->current_namespace ][ $el ], $text);
303 elseif ($this->inchannel) {
305 $this->channel[ $this->current_namespace][ $el ], $text );
307 elseif ($this->intextinput) {
309 $this->textinput[ $this->current_namespace][ $el ], $text );
311 elseif ($this->inimage) {
313 $this->image[ $this->current_namespace ][ $el ], $text );
317 if ( $this->initem ) {
319 $this->current_item[ $el ], $text);
321 elseif ($this->intextinput) {
323 $this->textinput[ $el ], $text );
325 elseif ($this->inimage) {
327 $this->image[ $el ], $text );
329 elseif ($this->inchannel) {
331 $this->channel[ $el ], $text );
337 function normalize () {
338 // if atom populate rss fields
339 if ( $this->is_atom() ) {
340 $this->channel['descripton'] = $this->channel['tagline'];
341 for ( $i = 0; $i < count($this->items); $i++) {
342 $item = $this->items[$i];
343 if ( isset($item['summary']) )
344 $item['description'] = $item['summary'];
345 if ( isset($item['atom_content']))
346 $item['content']['encoded'] = $item['atom_content'];
348 $this->items[$i] = $item;
351 elseif ( $this->is_rss() ) {
352 $this->channel['tagline'] = $this->channel['description'];
353 for ( $i = 0; $i < count($this->items); $i++) {
354 $item = $this->items[$i];
355 if ( isset($item['description']))
356 $item['summary'] = $item['description'];
357 if ( isset($item['content']['encoded'] ) )
358 $item['atom_content'] = $item['content']['encoded'];
360 $this->items[$i] = $item;
366 if ( $this->feed_type == RSS ) {
367 return $this->feed_version;
375 if ( $this->feed_type == ATOM ) {
376 return $this->feed_version;
383 function map_attrs($k, $v) {
387 function error( $errormsg, $lvl = E_USER_WARNING ) {
388 // append PHP's error message if track_errors enabled
389 if ( isset($php_errormsg) ) {
390 $errormsg .= " ($php_errormsg)";
392 if ( MAGPIE_DEBUG ) {
393 trigger_error( $errormsg, $lvl);
395 error_log( $errormsg, 0);
401 if ( !function_exists('fetch_rss') ) :
403 * Build Magpie object based on RSS from URL.
407 * @subpackage MagpieRSS
409 * @param string $url URL to retrieve feed
410 * @return bool|MagpieRSS false on failure or MagpieRSS object on success.
412 function fetch_rss ($url) {
413 // initialize constants
416 if ( !isset($url) ) {
417 // error("fetch_rss called without a url");
421 // if cache is disabled
422 if ( !MAGPIE_CACHE_ON ) {
423 // fetch file, and parse it
424 $resp = _fetch_remote_file( $url );
425 if ( is_success( $resp->status ) ) {
426 return _response_to_rss( $resp );
429 // error("Failed to fetch $url and cache is off");
437 // 2. if there is a hit, make sure it's fresh
438 // 3. if cached obj fails freshness check, fetch remote
439 // 4. if remote fails, return stale object, or error
441 $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
443 if (MAGPIE_DEBUG and $cache->ERROR) {
444 debug($cache->ERROR, E_USER_WARNING);
447 $cache_status = 0; // response of check_cache
448 $request_headers = array(); // HTTP headers to send with fetch
449 $rss = 0; // parsed RSS object
450 $errormsg = 0; // errors, if any
452 if (!$cache->ERROR) {
453 // return cache HIT, MISS, or STALE
454 $cache_status = $cache->check_cache( $url );
457 // if object cached, and cache is fresh, return cached obj
458 if ( $cache_status == 'HIT' ) {
459 $rss = $cache->get( $url );
460 if ( isset($rss) and $rss ) {
461 $rss->from_cache = 1;
462 if ( MAGPIE_DEBUG > 1) {
463 debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
469 // else attempt a conditional get
472 if ( $cache_status == 'STALE' ) {
473 $rss = $cache->get( $url );
474 if ( isset($rss->etag) and $rss->last_modified ) {
475 $request_headers['If-None-Match'] = $rss->etag;
476 $request_headers['If-Last-Modified'] = $rss->last_modified;
480 $resp = _fetch_remote_file( $url, $request_headers );
482 if (isset($resp) and $resp) {
483 if ($resp->status == '304' ) {
484 // we have the most current copy
485 if ( MAGPIE_DEBUG > 1) {
486 debug("Got 304 for $url");
488 // reset cache on 304 (at minutillo insistent prodding)
489 $cache->set($url, $rss);
492 elseif ( is_success( $resp->status ) ) {
493 $rss = _response_to_rss( $resp );
495 if (MAGPIE_DEBUG > 1) {
496 debug("Fetch successful");
498 // add object to cache
499 $cache->set( $url, $rss );
504 $errormsg = "Failed to fetch $url. ";
505 if ( $resp->error ) {
506 # compensate for Snoopy's annoying habbit to tacking
508 $http_error = substr($resp->error, 0, -2);
509 $errormsg .= "(HTTP Error: $http_error)";
512 $errormsg .= "(HTTP Response: " . $resp->response_code .')';
517 $errormsg = "Unable to retrieve RSS file for unknown reasons.";
522 // attempt to return cached object
524 if ( MAGPIE_DEBUG ) {
525 debug("Returning STALE object for $url");
530 // else we totally failed
531 // error( $errormsg );
535 } // end if ( !MAGPIE_CACHE_ON ) {
540 * Retrieve URL headers and content using WP HTTP Request API.
544 * @subpackage MagpieRSS
546 * @param string $url URL to retrieve
547 * @param array $headers Optional. Headers to send to the URL.
548 * @return Snoopy style response
550 function _fetch_remote_file($url, $headers = "" ) {
551 $resp = wp_safe_remote_request( $url, array( 'headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT ) );
552 if ( is_wp_error($resp) ) {
553 $error = array_shift($resp->errors);
555 $resp = new stdClass;
557 $resp->response_code = 500;
558 $resp->error = $error[0] . "\n"; //\n = Snoopy compatibility
562 // Snoopy returns headers unprocessed.
563 // Also note, WP_HTTP lowercases all keys, Snoopy did not.
564 $return_headers = array();
565 foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
566 if ( !is_array($value) ) {
567 $return_headers[] = "$key: $value";
569 foreach ( $value as $v )
570 $return_headers[] = "$key: $v";
574 $response = new stdClass;
575 $response->status = wp_remote_retrieve_response_code( $resp );
576 $response->response_code = wp_remote_retrieve_response_code( $resp );
577 $response->headers = $return_headers;
578 $response->results = wp_remote_retrieve_body( $resp );
588 * @subpackage MagpieRSS
591 * @return MagpieRSS|bool
593 function _response_to_rss ($resp) {
594 $rss = new MagpieRSS( $resp->results );
596 // if RSS parsed successfully
597 if ( $rss && (!isset($rss->ERROR) || !$rss->ERROR) ) {
599 // find Etag, and Last-Modified
600 foreach ( (array) $resp->headers as $h) {
601 // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
602 if (strpos($h, ": ")) {
603 list($field, $val) = explode(": ", $h, 2);
610 if ( $field == 'etag' ) {
614 if ( $field == 'last-modified' ) {
615 $rss->last_modified = $val;
620 } // else construct error message
622 $errormsg = "Failed to parse RSS file.";
625 $errormsg .= " (" . $rss->ERROR . ")";
630 } // end if ($rss and !$rss->error)
634 * Set up constants with default values, unless user overrides.
638 * @subpackage MagpieRSS
641 if ( defined('MAGPIE_INITALIZED') ) {
645 define('MAGPIE_INITALIZED', 1);
648 if ( !defined('MAGPIE_CACHE_ON') ) {
649 define('MAGPIE_CACHE_ON', 1);
652 if ( !defined('MAGPIE_CACHE_DIR') ) {
653 define('MAGPIE_CACHE_DIR', './cache');
656 if ( !defined('MAGPIE_CACHE_AGE') ) {
657 define('MAGPIE_CACHE_AGE', 60*60); // one hour
660 if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {
661 define('MAGPIE_CACHE_FRESH_ONLY', 0);
664 if ( !defined('MAGPIE_DEBUG') ) {
665 define('MAGPIE_DEBUG', 0);
668 if ( !defined('MAGPIE_USER_AGENT') ) {
669 $ua = 'WordPress/' . $GLOBALS['wp_version'];
671 if ( MAGPIE_CACHE_ON ) {
675 $ua = $ua . '; No cache)';
678 define('MAGPIE_USER_AGENT', $ua);
681 if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
682 define('MAGPIE_FETCH_TIME_OUT', 2); // 2 second timeout
685 // use gzip encoding to fetch rss files if supported?
686 if ( !defined('MAGPIE_USE_GZIP') ) {
687 define('MAGPIE_USE_GZIP', true);
691 function is_info ($sc) {
692 return $sc >= 100 && $sc < 200;
695 function is_success ($sc) {
696 return $sc >= 200 && $sc < 300;
699 function is_redirect ($sc) {
700 return $sc >= 300 && $sc < 400;
703 function is_error ($sc) {
704 return $sc >= 400 && $sc < 600;
707 function is_client_error ($sc) {
708 return $sc >= 400 && $sc < 500;
711 function is_server_error ($sc) {
712 return $sc >= 500 && $sc < 600;
716 var $BASE_CACHE; // where the cache files are stored
717 var $MAX_AGE = 43200; // when are files stale, default twelve hours
718 var $ERROR = ''; // accumulate error messages
723 function __construct( $base = '', $age = '' ) {
724 $this->BASE_CACHE = WP_CONTENT_DIR . '/cache';
726 $this->BASE_CACHE = $base;
729 $this->MAX_AGE = $age;
737 public function RSSCache( $base = '', $age = '' ) {
738 self::__construct( $base, $age );
741 /*=======================================================================*\
743 Purpose: add an item to the cache, keyed on url
744 Input: url from which the rss file was fetched
745 Output: true on success
746 \*=======================================================================*/
747 function set ($url, $rss) {
748 $cache_option = 'rss_' . $this->file_name( $url );
750 set_transient($cache_option, $rss, $this->MAX_AGE);
752 return $cache_option;
755 /*=======================================================================*\
757 Purpose: fetch an item from the cache
758 Input: url from which the rss file was fetched
759 Output: cached object on HIT, false on MISS
760 \*=======================================================================*/
761 function get ($url) {
763 $cache_option = 'rss_' . $this->file_name( $url );
765 if ( ! $rss = get_transient( $cache_option ) ) {
767 "Cache doesn't contain: $url (cache option: $cache_option)"
775 /*=======================================================================*\
776 Function: check_cache
777 Purpose: check a url for membership in the cache
778 and whether the object is older then MAX_AGE (ie. STALE)
779 Input: url from which the rss file was fetched
780 Output: cached object on HIT, false on MISS
781 \*=======================================================================*/
782 function check_cache ( $url ) {
784 $cache_option = 'rss_' . $this->file_name( $url );
786 if ( get_transient($cache_option) ) {
787 // object exists and is current
790 // object does not exist
795 /*=======================================================================*\
797 \*=======================================================================*/
798 function serialize ( $rss ) {
799 return serialize( $rss );
802 /*=======================================================================*\
803 Function: unserialize
804 \*=======================================================================*/
805 function unserialize ( $data ) {
806 return unserialize( $data );
809 /*=======================================================================*\
811 Purpose: map url to location in cache
812 Input: url from which the rss file was fetched
814 \*=======================================================================*/
815 function file_name ($url) {
819 /*=======================================================================*\
821 Purpose: register error
822 \*=======================================================================*/
823 function error ($errormsg, $lvl=E_USER_WARNING) {
824 // append PHP's error message if track_errors enabled
825 if ( isset($php_errormsg) ) {
826 $errormsg .= " ($php_errormsg)";
828 $this->ERROR = $errormsg;
829 if ( MAGPIE_DEBUG ) {
830 trigger_error( $errormsg, $lvl);
833 error_log( $errormsg, 0);
836 function debug ($debugmsg, $lvl=E_USER_NOTICE) {
837 if ( MAGPIE_DEBUG ) {
838 $this->error("MagpieRSS [debug] $debugmsg", $lvl);
843 if ( !function_exists('parse_w3cdtf') ) :
844 function parse_w3cdtf ( $date_str ) {
846 # regex to match wc3dtf
847 $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
849 if ( preg_match( $pat, $date_str, $match ) ) {
850 list( $year, $month, $day, $hours, $minutes, $seconds) =
851 array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[7]);
853 # calc epoch for current date assuming GMT
854 $epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
857 if ( $match[11] == 'Z' ) {
861 list( $tz_mod, $tz_hour, $tz_min ) =
862 array( $match[8], $match[9], $match[10]);
864 # zero out the variables
865 if ( ! $tz_hour ) { $tz_hour = 0; }
866 if ( ! $tz_min ) { $tz_min = 0; }
868 $offset_secs = (($tz_hour*60)+$tz_min)*60;
870 # is timezone ahead of GMT? then subtract offset
872 if ( $tz_mod == '+' ) {
873 $offset_secs = $offset_secs * -1;
876 $offset = $offset_secs;
878 $epoch = $epoch + $offset;
887 if ( !function_exists('wp_rss') ) :
889 * Display all RSS items in a HTML ordered list.
893 * @subpackage MagpieRSS
895 * @param string $url URL of feed to display. Will not auto sense feed URL.
896 * @param int $num_items Optional. Number of items to display, default is all.
898 function wp_rss( $url, $num_items = -1 ) {
899 if ( $rss = fetch_rss( $url ) ) {
902 if ( $num_items !== -1 ) {
903 $rss->items = array_slice( $rss->items, 0, $num_items );
906 foreach ( (array) $rss->items as $item ) {
908 '<li><a href="%1$s" title="%2$s">%3$s</a></li>',
909 esc_url( $item['link'] ),
910 esc_attr( strip_tags( $item['description'] ) ),
911 esc_html( $item['title'] )
917 _e( 'An error has occurred, which probably means the feed is down. Try again later.' );
922 if ( !function_exists('get_rss') ) :
924 * Display RSS items in HTML list items.
926 * You have to specify which HTML list you want, either ordered or unordered
927 * before using the function. You also have to specify how many items you wish
928 * to display. You can't display all of them like you can with wp_rss()
933 * @subpackage MagpieRSS
935 * @param string $url URL of feed to display. Will not auto sense feed URL.
936 * @param int $num_items Optional. Number of items to display, default is all.
937 * @return bool False on failure.
939 function get_rss ($url, $num_items = 5) { // Like get posts, but for RSS
940 $rss = fetch_rss($url);
942 $rss->items = array_slice($rss->items, 0, $num_items);
943 foreach ( (array) $rss->items as $item ) {
945 echo "<a href='$item[link]' title='$item[description]'>";
946 echo esc_html($item['title']);