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 * Hook to use another RSS object instead of MagpieRSS
18 do_action('load_feed_engine');
20 /** RSS feed constant. */
22 define('ATOM', 'Atom');
23 define('MAGPIE_USER_AGENT', 'WordPress/' . $GLOBALS['wp_version']);
27 var $current_item = array(); // item currently being parsed
28 var $items = array(); // collection of parsed items
29 var $channel = array(); // hash of channel fields
30 var $textinput = array();
36 var $stack = array(); // parser stack
37 var $inchannel = false;
39 var $incontent = false; // if in Atom <content mode="xml"> field
40 var $intextinput = false;
42 var $current_field = '';
43 var $current_namespace = false;
47 var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
49 function MagpieRSS ($source) {
51 # if PHP xml isn't compiled in, die
53 if ( !function_exists('xml_parser_create') )
54 trigger_error( "Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php" );
56 $parser = @xml_parser_create();
58 if ( !is_resource($parser) )
59 trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");
62 $this->parser = $parser;
64 # pass in parser, and a reference to this object
67 xml_set_object( $this->parser, $this );
68 xml_set_element_handler($this->parser,
69 'feed_start_element', 'feed_end_element' );
71 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
73 $status = xml_parse( $this->parser, $source );
76 $errorcode = xml_get_error_code( $this->parser );
77 if ( $errorcode != XML_ERROR_NONE ) {
78 $xml_error = xml_error_string( $errorcode );
79 $error_line = xml_get_current_line_number($this->parser);
80 $error_col = xml_get_current_column_number($this->parser);
81 $errormsg = "$xml_error at line $error_line, column $error_col";
83 $this->error( $errormsg );
87 xml_parser_free( $this->parser );
92 function feed_start_element($p, $element, &$attrs) {
93 $el = $element = strtolower($element);
94 $attrs = array_change_key_case($attrs, CASE_LOWER);
96 // check for a namespace, and split if found
98 if ( strpos( $element, ':' ) ) {
99 list($ns, $el) = split( ':', $element, 2);
101 if ( $ns and $ns != 'rdf' ) {
102 $this->current_namespace = $ns;
105 # if feed type isn't set, then this is first element of feed
106 # identify feed from root element
108 if (!isset($this->feed_type) ) {
109 if ( $el == 'rdf' ) {
110 $this->feed_type = RSS;
111 $this->feed_version = '1.0';
113 elseif ( $el == 'rss' ) {
114 $this->feed_type = RSS;
115 $this->feed_version = $attrs['version'];
117 elseif ( $el == 'feed' ) {
118 $this->feed_type = ATOM;
119 $this->feed_version = $attrs['version'];
120 $this->inchannel = true;
125 if ( $el == 'channel' )
127 $this->inchannel = true;
129 elseif ($el == 'item' or $el == 'entry' )
131 $this->initem = true;
132 if ( isset($attrs['rdf:about']) ) {
133 $this->current_item['about'] = $attrs['rdf:about'];
137 // if we're in the default namespace of an RSS feed,
138 // record textinput or image fields
140 $this->feed_type == RSS and
141 $this->current_namespace == '' and
144 $this->intextinput = true;
148 $this->feed_type == RSS and
149 $this->current_namespace == '' and
152 $this->inimage = true;
155 # handle atom content constructs
156 elseif ( $this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
158 // avoid clashing w/ RSS mod_content
159 if ($el == 'content' ) {
160 $el = 'atom_content';
163 $this->incontent = $el;
168 // if inside an Atom content construct (e.g. content or summary) field treat tags as text
169 elseif ($this->feed_type == ATOM and $this->incontent )
171 // if tags are inlined, then flatten
172 $attrs_str = join(' ',
173 array_map(array('MagpieRSS', 'map_attrs'),
175 array_values($attrs) ) );
177 $this->append_content( "<$element $attrs_str>" );
179 array_unshift( $this->stack, $el );
182 // Atom support many links per containging element.
183 // Magpie treats link elements of type rel='alternate'
184 // as being equivalent to RSS's simple link element.
186 elseif ($this->feed_type == ATOM and $el == 'link' )
188 if ( isset($attrs['rel']) and $attrs['rel'] == 'alternate' )
193 $link_el = 'link_' . $attrs['rel'];
196 $this->append($link_el, $attrs['href']);
198 // set stack[0] to current element
200 array_unshift($this->stack, $el);
206 function feed_cdata ($p, $text) {
208 if ($this->feed_type == ATOM and $this->incontent)
210 $this->append_content( $text );
213 $current_el = join('_', array_reverse($this->stack));
214 $this->append($current_el, $text);
218 function feed_end_element ($p, $el) {
219 $el = strtolower($el);
221 if ( $el == 'item' or $el == 'entry' )
223 $this->items[] = $this->current_item;
224 $this->current_item = array();
225 $this->initem = false;
227 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' )
229 $this->intextinput = false;
231 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' )
233 $this->inimage = false;
235 elseif ($this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
237 $this->incontent = false;
239 elseif ($el == 'channel' or $el == 'feed' )
241 $this->inchannel = false;
243 elseif ($this->feed_type == ATOM and $this->incontent ) {
244 // balance tags properly
245 // note: i don't think this is actually neccessary
246 if ( $this->stack[0] == $el )
248 $this->append_content("</$el>");
251 $this->append_content("<$el />");
254 array_shift( $this->stack );
257 array_shift( $this->stack );
260 $this->current_namespace = false;
263 function concat (&$str1, $str2="") {
264 if (!isset($str1) ) {
270 function append_content($text) {
271 if ( $this->initem ) {
272 $this->concat( $this->current_item[ $this->incontent ], $text );
274 elseif ( $this->inchannel ) {
275 $this->concat( $this->channel[ $this->incontent ], $text );
279 // smart append - field and namespace aware
280 function append($el, $text) {
284 if ( $this->current_namespace )
286 if ( $this->initem ) {
288 $this->current_item[ $this->current_namespace ][ $el ], $text);
290 elseif ($this->inchannel) {
292 $this->channel[ $this->current_namespace][ $el ], $text );
294 elseif ($this->intextinput) {
296 $this->textinput[ $this->current_namespace][ $el ], $text );
298 elseif ($this->inimage) {
300 $this->image[ $this->current_namespace ][ $el ], $text );
304 if ( $this->initem ) {
306 $this->current_item[ $el ], $text);
308 elseif ($this->intextinput) {
310 $this->textinput[ $el ], $text );
312 elseif ($this->inimage) {
314 $this->image[ $el ], $text );
316 elseif ($this->inchannel) {
318 $this->channel[ $el ], $text );
324 function normalize () {
325 // if atom populate rss fields
326 if ( $this->is_atom() ) {
327 $this->channel['descripton'] = $this->channel['tagline'];
328 for ( $i = 0; $i < count($this->items); $i++) {
329 $item = $this->items[$i];
330 if ( isset($item['summary']) )
331 $item['description'] = $item['summary'];
332 if ( isset($item['atom_content']))
333 $item['content']['encoded'] = $item['atom_content'];
335 $this->items[$i] = $item;
338 elseif ( $this->is_rss() ) {
339 $this->channel['tagline'] = $this->channel['description'];
340 for ( $i = 0; $i < count($this->items); $i++) {
341 $item = $this->items[$i];
342 if ( isset($item['description']))
343 $item['summary'] = $item['description'];
344 if ( isset($item['content']['encoded'] ) )
345 $item['atom_content'] = $item['content']['encoded'];
347 $this->items[$i] = $item;
353 if ( $this->feed_type == RSS ) {
354 return $this->feed_version;
362 if ( $this->feed_type == ATOM ) {
363 return $this->feed_version;
370 function map_attrs($k, $v) {
374 function error( $errormsg, $lvl = E_USER_WARNING ) {
375 // append PHP's error message if track_errors enabled
376 if ( isset($php_errormsg) ) {
377 $errormsg .= " ($php_errormsg)";
379 if ( MAGPIE_DEBUG ) {
380 trigger_error( $errormsg, $lvl);
382 error_log( $errormsg, 0);
388 if ( !function_exists('fetch_rss') ) :
390 * Build Magpie object based on RSS from URL.
394 * @subpackage MagpieRSS
396 * @param string $url URL to retrieve feed
397 * @return bool|MagpieRSS false on failure or MagpieRSS object on success.
399 function fetch_rss ($url) {
400 // initialize constants
403 if ( !isset($url) ) {
404 // error("fetch_rss called without a url");
408 // if cache is disabled
409 if ( !MAGPIE_CACHE_ON ) {
410 // fetch file, and parse it
411 $resp = _fetch_remote_file( $url );
412 if ( is_success( $resp->status ) ) {
413 return _response_to_rss( $resp );
416 // error("Failed to fetch $url and cache is off");
424 // 2. if there is a hit, make sure its fresh
425 // 3. if cached obj fails freshness check, fetch remote
426 // 4. if remote fails, return stale object, or error
428 $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
430 if (MAGPIE_DEBUG and $cache->ERROR) {
431 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
549 $response = new stdClass;
550 $response->status = $resp['response']['code'];
551 $response->response_code = $resp['response']['code'];
552 $response->headers = $resp['headers'];
553 $response->results = $resp['body'];
563 * @subpackage MagpieRSS
565 * @param unknown_type $resp
568 function _response_to_rss ($resp) {
569 $rss = new MagpieRSS( $resp->results );
571 // if RSS parsed successfully
572 if ( $rss && (!isset($rss->ERROR) || !$rss->ERROR) ) {
574 // find Etag, and Last-Modified
575 foreach( (array) $resp->headers as $h) {
576 // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
577 if (strpos($h, ": ")) {
578 list($field, $val) = explode(": ", $h, 2);
585 if ( $field == 'ETag' ) {
589 if ( $field == 'Last-Modified' ) {
590 $rss->last_modified = $val;
595 } // else construct error message
597 $errormsg = "Failed to parse RSS file.";
600 $errormsg .= " (" . $rss->ERROR . ")";
605 } // end if ($rss and !$rss->error)
609 * Setup constants with default values, unless user overrides.
613 * @subpackage MagpieRSS
616 if ( defined('MAGPIE_INITALIZED') ) {
620 define('MAGPIE_INITALIZED', 1);
623 if ( !defined('MAGPIE_CACHE_ON') ) {
624 define('MAGPIE_CACHE_ON', 1);
627 if ( !defined('MAGPIE_CACHE_DIR') ) {
628 define('MAGPIE_CACHE_DIR', './cache');
631 if ( !defined('MAGPIE_CACHE_AGE') ) {
632 define('MAGPIE_CACHE_AGE', 60*60); // one hour
635 if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {
636 define('MAGPIE_CACHE_FRESH_ONLY', 0);
639 if ( !defined('MAGPIE_DEBUG') ) {
640 define('MAGPIE_DEBUG', 0);
643 if ( !defined('MAGPIE_USER_AGENT') ) {
644 $ua = 'WordPress/' . $GLOBALS['wp_version'];
646 if ( MAGPIE_CACHE_ON ) {
650 $ua = $ua . '; No cache)';
653 define('MAGPIE_USER_AGENT', $ua);
656 if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
657 define('MAGPIE_FETCH_TIME_OUT', 2); // 2 second timeout
660 // use gzip encoding to fetch rss files if supported?
661 if ( !defined('MAGPIE_USE_GZIP') ) {
662 define('MAGPIE_USE_GZIP', true);
666 function is_info ($sc) {
667 return $sc >= 100 && $sc < 200;
670 function is_success ($sc) {
671 return $sc >= 200 && $sc < 300;
674 function is_redirect ($sc) {
675 return $sc >= 300 && $sc < 400;
678 function is_error ($sc) {
679 return $sc >= 400 && $sc < 600;
682 function is_client_error ($sc) {
683 return $sc >= 400 && $sc < 500;
686 function is_server_error ($sc) {
687 return $sc >= 500 && $sc < 600;
691 var $BASE_CACHE; // where the cache files are stored
692 var $MAX_AGE = 43200; // when are files stale, default twelve hours
693 var $ERROR = ''; // accumulate error messages
695 function RSSCache ($base='', $age='') {
696 $this->BASE_CACHE = WP_CONTENT_DIR . '/cache';
698 $this->BASE_CACHE = $base;
701 $this->MAX_AGE = $age;
706 /*=======================================================================*\
708 Purpose: add an item to the cache, keyed on url
709 Input: url from wich the rss file was fetched
710 Output: true on sucess
711 \*=======================================================================*/
712 function set ($url, $rss) {
714 $cache_option = 'rss_' . $this->file_name( $url );
716 set_transient($cache_option, $rss, $this->MAX_AGE);
718 return $cache_option;
721 /*=======================================================================*\
723 Purpose: fetch an item from the cache
724 Input: url from wich the rss file was fetched
725 Output: cached object on HIT, false on MISS
726 \*=======================================================================*/
727 function get ($url) {
729 $cache_option = 'rss_' . $this->file_name( $url );
731 if ( ! $rss = get_transient( $cache_option ) ) {
733 "Cache doesn't contain: $url (cache option: $cache_option)"
741 /*=======================================================================*\
742 Function: check_cache
743 Purpose: check a url for membership in the cache
744 and whether the object is older then MAX_AGE (ie. STALE)
745 Input: url from wich the rss file was fetched
746 Output: cached object on HIT, false on MISS
747 \*=======================================================================*/
748 function check_cache ( $url ) {
750 $cache_option = 'rss_' . $this->file_name( $url );
752 if ( get_transient($cache_option) ) {
753 // object exists and is current
756 // object does not exist
761 /*=======================================================================*\
763 \*=======================================================================*/
764 function serialize ( $rss ) {
765 return serialize( $rss );
768 /*=======================================================================*\
769 Function: unserialize
770 \*=======================================================================*/
771 function unserialize ( $data ) {
772 return unserialize( $data );
775 /*=======================================================================*\
777 Purpose: map url to location in cache
778 Input: url from wich the rss file was fetched
780 \*=======================================================================*/
781 function file_name ($url) {
785 /*=======================================================================*\
787 Purpose: register error
788 \*=======================================================================*/
789 function error ($errormsg, $lvl=E_USER_WARNING) {
790 // append PHP's error message if track_errors enabled
791 if ( isset($php_errormsg) ) {
792 $errormsg .= " ($php_errormsg)";
794 $this->ERROR = $errormsg;
795 if ( MAGPIE_DEBUG ) {
796 trigger_error( $errormsg, $lvl);
799 error_log( $errormsg, 0);
802 function debug ($debugmsg, $lvl=E_USER_NOTICE) {
803 if ( MAGPIE_DEBUG ) {
804 $this->error("MagpieRSS [debug] $debugmsg", $lvl);
809 if ( !function_exists('parse_w3cdtf') ) :
810 function parse_w3cdtf ( $date_str ) {
812 # regex to match wc3dtf
813 $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
815 if ( preg_match( $pat, $date_str, $match ) ) {
816 list( $year, $month, $day, $hours, $minutes, $seconds) =
817 array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[7]);
819 # calc epoch for current date assuming GMT
820 $epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
823 if ( $match[11] == 'Z' ) {
827 list( $tz_mod, $tz_hour, $tz_min ) =
828 array( $match[8], $match[9], $match[10]);
830 # zero out the variables
831 if ( ! $tz_hour ) { $tz_hour = 0; }
832 if ( ! $tz_min ) { $tz_min = 0; }
834 $offset_secs = (($tz_hour*60)+$tz_min)*60;
836 # is timezone ahead of GMT? then subtract offset
838 if ( $tz_mod == '+' ) {
839 $offset_secs = $offset_secs * -1;
842 $offset = $offset_secs;
844 $epoch = $epoch + $offset;
853 if ( !function_exists('wp_rss') ) :
855 * Display all RSS items in a HTML ordered list.
859 * @subpackage MagpieRSS
861 * @param string $url URL of feed to display. Will not auto sense feed URL.
862 * @param int $num_items Optional. Number of items to display, default is all.
864 function wp_rss( $url, $num_items = -1 ) {
865 if ( $rss = fetch_rss( $url ) ) {
868 if ( $num_items !== -1 ) {
869 $rss->items = array_slice( $rss->items, 0, $num_items );
872 foreach ( (array) $rss->items as $item ) {
874 '<li><a href="%1$s" title="%2$s">%3$s</a></li>',
875 esc_url( $item['link'] ),
876 esc_attr( strip_tags( $item['description'] ) ),
877 htmlentities( $item['title'] )
883 _e( 'An error has occurred, which probably means the feed is down. Try again later.' );
888 if ( !function_exists('get_rss') ) :
890 * Display RSS items in HTML list items.
892 * You have to specify which HTML list you want, either ordered or unordered
893 * before using the function. You also have to specify how many items you wish
894 * to display. You can't display all of them like you can with wp_rss()
899 * @subpackage MagpieRSS
901 * @param string $url URL of feed to display. Will not auto sense feed URL.
902 * @param int $num_items Optional. Number of items to display, default is all.
903 * @return bool False on failure.
905 function get_rss ($url, $num_items = 5) { // Like get posts, but for RSS
906 $rss = fetch_rss($url);
908 $rss->items = array_slice($rss->items, 0, $num_items);
909 foreach ( (array) $rss->items as $item ) {
911 echo "<a href='$item[link]' title='$item[description]'>";
912 echo htmlentities($item['title']);