4 * Project: MagpieRSS: a simple RSS integration tool
5 * File: rss_parse.inc - parse an RSS or Atom feed
6 * return as a simple object.
8 * Handles RSS 0.9x, RSS 2.0, RSS 1.0, Atom 0.3, and Atom 1.0
10 * The lastest version of MagpieRSS can be obtained from:
11 * http://magpierss.sourceforge.net
13 * For questions, help, comments, discussion, etc., please join the
14 * Magpie mailing list:
15 * magpierss-general@lists.sourceforge.net
17 * @author Kellan Elliott-McCrea <kellan@protest.net>
24 define('ATOM', 'Atom');
26 function _convert_entities ($string) {
27 # Source: http://www.w3.org/TR/REC-html40/sgml/entities.html
28 $html_entities = array(
29 " ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©",
30 "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³",
31 "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½",
32 "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç",
33 "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ",
34 "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û",
35 "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å",
36 "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
37 "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù",
38 "ú", "û", "ü", "ý", "þ", "ÿ",);
39 $numeric_entities = array(
40 " ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©",
41 "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³",
42 "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½",
43 "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç",
44 "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ",
45 "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û",
46 "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å",
47 "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
48 "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù",
49 "ú", "û", "ü", "ý", "þ", "ÿ");
50 return str_replace($html_entities, $numeric_entities, $string);
55 require_once (MAGPIE_DIR . 'rss_utils.inc');
58 * Hybrid parser, and object, takes RSS as a string and returns a simple object.
60 * see: rss_fetch.inc for a simpler interface with integrated caching support
66 var $current_item = array(); // item currently being parsed
67 var $items = array(); // collection of parsed items
68 var $channel = array(); // hash of channel fields
69 var $textinput = array();
73 var $encoding = ''; // output encoding of parsed rss
75 var $_source_encoding = ''; // only set if we have to parse xml prolog
80 // define some constants
82 var $_ATOM_CONTENT_CONSTRUCTS = array(
83 'content', 'summary', 'title', /* common */
84 'info', 'tagline', 'copyright', /* Atom 0.3 */
85 'rights', 'subtitle', /* Atom 1.0 */
87 var $_XHTML_CONTENT_CONSTRUCTS = array('body', 'div');
88 var $_KNOWN_ENCODINGS = array('UTF-8', 'US-ASCII', 'ISO-8859-1');
90 // parser variables, useless if you're not a parser, treat as private
91 var $stack = array(); // parser stack
92 var $inchannel = false;
95 var $incontent = array(); // non-empty if in namespaced XML content field
96 var $exclude_top = false; // true when Atom 1.0 type="xhtml"
98 var $intextinput = false;
100 var $current_namespace = false;
103 * Set up XML parser, parse source, and return populated RSS object..
105 * @param string $source string containing the RSS to be parsed
107 * NOTE: Probably a good idea to leave the encoding options alone unless
108 * you know what you're doing as PHP's character set support is
111 * NOTE: A lot of this is unnecessary but harmless with PHP5
114 * @param string $output_encoding output the parsed RSS in this character
115 * set defaults to ISO-8859-1 as this is PHP's
118 * NOTE: might be changed to UTF-8 in future
121 * @param string $input_encoding the character set of the incoming RSS source.
122 * Leave blank and Magpie will try to figure it
126 * @param bool $detect_encoding if false Magpie won't attempt to detect
127 * source encoding. (caveat emptor)
130 function MagpieRSS ($source, $output_encoding='ISO-8859-1',
131 $input_encoding=null, $detect_encoding=true)
133 # if PHP xml isn't compiled in, die
135 if (!function_exists('xml_parser_create')) {
136 $this->error( "Failed to load PHP's XML Extension. " .
137 "http://www.php.net/manual/en/ref.xml.php",
141 list($parser, $source) = $this->create_parser($source,
142 $output_encoding, $input_encoding, $detect_encoding);
145 if (!is_resource($parser)) {
146 $this->error( "Failed to create an instance of PHP's XML parser. " .
147 "http://www.php.net/manual/en/ref.xml.php",
152 $this->parser = $parser;
154 # pass in parser, and a reference to this object
157 xml_set_object( $this->parser, $this );
158 xml_set_element_handler($this->parser,
159 'feed_start_element', 'feed_end_element' );
161 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
163 $source=str_replace("<","<",$source);
164 $source=str_replace(">",">",$source);
165 $source=str_replace("&","&",$source);
167 $status = xml_parse( $this->parser, $source );
169 # try to force convert everything to UTF-8 and parse again
170 # to salvage at least some data from the feed
172 $errorcode = xml_get_error_code( $this->parser );
173 if ( $errorcode != XML_ERROR_NONE ) {
175 xml_parser_free( $this->parser );
177 if (preg_match('/<\?xml.*?encoding="([^ ]+)".*?\?>/',
178 $source, $matches)) {
182 $enc = mb_detect_encoding($string);
185 # try fix XML, pass 1
187 $source = mb_convert_encoding($source, "UTF-8", $enc);
189 list($parser, $source) = $this->create_parser($source,
190 $output_encoding, $input_encoding, $detect_encoding);
192 $this->parser = $parser;
194 xml_set_object( $this->parser, $this );
195 xml_set_element_handler($this->parser,
196 'feed_start_element', 'feed_end_element' );
198 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
200 $status = xml_parse( $this->parser, $source);
202 # try to fix XML, pass 2
205 $errorcode = xml_get_error_code( $this->parser );
206 if ( $errorcode != XML_ERROR_NONE ) {
208 $source = _convert_entities($source);
210 list($parser, $source) = $this->create_parser($source,
211 $output_encoding, $input_encoding, $detect_encoding);
213 $this->parser = $parser;
215 xml_set_object( $this->parser, $this );
216 xml_set_element_handler($this->parser,
217 'feed_start_element', 'feed_end_element' );
219 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
221 $status = xml_parse( $this->parser, $source);
229 $errorcode = xml_get_error_code( $this->parser );
230 if ( $errorcode != XML_ERROR_NONE ) {
231 $xml_error = xml_error_string( $errorcode );
232 $error_line = xml_get_current_line_number($this->parser);
233 $error_col = xml_get_current_column_number($this->parser);
234 $errormsg = "$xml_error at line $error_line, column $error_col";
236 $this->error( $errormsg );
240 xml_parser_free( $this->parser );
245 function feed_start_element($p, $element, &$attrs) {
246 $el = $element = strtolower($element);
247 $attrs = array_change_key_case($attrs, CASE_LOWER);
249 // check for a namespace, and split if found
250 // Don't munge content tags
251 if ( empty($this->incontent) ) {
253 if ( strpos( $element, ':' ) ) {
254 list($ns, $el) = split( ':', $element, 2);
256 if ( $ns and $ns != 'rdf' ) {
257 $this->current_namespace = $ns;
261 # if feed type isn't set, then this is first element of feed
262 # identify feed from root element
264 if (!isset($this->feed_type) ) {
265 if ( $el == 'rdf' ) {
266 $this->feed_type = RSS;
267 $this->feed_version = '1.0';
269 elseif ( $el == 'rss' ) {
270 $this->feed_type = RSS;
271 $this->feed_version = $attrs['version'];
273 elseif ( $el == 'feed' ) {
274 $this->feed_type = ATOM;
275 if ($attrs['xmlns'] == 'http://www.w3.org/2005/Atom') { // Atom 1.0
276 $this->feed_version = '1.0';
278 else { // Atom 0.3, probably.
279 $this->feed_version = $attrs['version'];
281 $this->inchannel = true;
286 // if we're inside a namespaced content construct, treat tags as text
287 if ( !empty($this->incontent) )
289 if ((count($this->incontent) > 1) or !$this->exclude_top) {
290 // if tags are inlined, then flatten
291 $attrs_str = join(' ',
292 array_map('map_attrs',
294 array_values($attrs) )
297 if (strlen($attrs_str) > 0) { $attrs_str = ' '.$attrs_str; }
299 $this->append_content( "<{$element}{$attrs_str}>" );
301 array_push($this->incontent, $el); // stack for parsing content XML
304 elseif ( $el == 'channel' ) {
305 $this->inchannel = true;
308 elseif ($el == 'item' or $el == 'entry' )
310 $this->initem = true;
311 if ( isset($attrs['rdf:about']) ) {
312 $this->current_item['about'] = $attrs['rdf:about'];
316 // if we're in the default namespace of an RSS feed,
317 // record textinput or image fields
319 $this->feed_type == RSS and
320 $this->current_namespace == '' and
323 $this->intextinput = true;
327 $this->feed_type == RSS and
328 $this->current_namespace == '' and
331 $this->inimage = true;
334 // set stack[0] to current element
336 // Atom support many links per containing element.
337 // Magpie treats link elements of type rel='alternate'
338 // as being equivalent to RSS's simple link element.
341 if ($this->feed_type == ATOM and $el == 'link') {
343 if (isset($attrs['rel']) and $attrs['rel'] != 'alternate') {
344 $el = $el . "_" . $attrs['rel']; // pseudo-element names for Atom link elements
347 # handle atom content constructs
348 elseif ( $this->feed_type == ATOM and in_array($el, $this->_ATOM_CONTENT_CONSTRUCTS) )
350 // avoid clashing w/ RSS mod_content
351 if ($el == 'content' ) {
352 $el = 'atom_content';
355 // assume that everything accepts namespaced XML
356 // (that will pass through some non-validating feeds;
357 // but so what? this isn't a validating parser)
358 $this->incontent = array();
359 array_push($this->incontent, $el); // start a stack
361 if ( isset($attrs['type']) and trim(strtolower($attrs['type']))=='xhtml') {
362 $this->exclude_top = true;
364 $this->exclude_top = false;
367 # Handle inline XHTML body elements --CWJ
368 elseif (($this->current_namespace=='xhtml' or
369 (isset($attrs['xmlns']) and $attrs['xmlns'] == 'http://www.w3.org/1999/xhtml'))
370 and in_array($el, $this->_XHTML_CONTENT_CONSTRUCTS) )
372 $this->current_namespace = 'xhtml';
373 $this->incontent = array();
374 array_push($this->incontent, $el); // start a stack
375 $this->exclude_top = false;
378 array_unshift($this->stack, $el);
379 $elpath = join('_', array_reverse($this->stack));
381 $n = $this->element_count($elpath);
382 $this->element_count($elpath, $n+1);
385 array_shift($this->stack);
386 array_unshift($this->stack, $el.'#'.($n+1));
387 $elpath = join('_', array_reverse($this->stack));
390 // this makes the baby Jesus cry, but we can't do it in normalize()
391 // because we've made the element name for Atom links unpredictable
392 // by tacking on the relation to the end. -CWJ
393 if ($atom_link and isset($attrs['href'])) {
394 $this->append($elpath, $attrs['href']);
398 if (count($attrs) > 0) {
399 $this->append($elpath.'@', join(',', array_keys($attrs)));
400 foreach ($attrs as $attr => $value) {
401 $this->append($elpath.'@'.$attr, $value);
409 function feed_cdata ($p, $text) {
411 if ($this->incontent) {
412 $this->append_content( $text );
415 $current_el = join('_', array_reverse($this->stack));
416 $this->append($current_el, $text);
420 function feed_end_element ($p, $el) {
421 $el = strtolower($el);
423 if ( $this->incontent ) {
424 $opener = array_pop($this->incontent);
426 // Don't get bamboozled by namespace voodoo
427 if (strpos($el, ':')) { list($ns, $closer) = split(':', $el); }
428 else { $ns = false; $closer = $el; }
430 // Don't get bamboozled by our munging of <atom:content>, either
431 if ($this->feed_type == ATOM and $closer == 'content') {
432 $closer = 'atom_content';
435 // balance tags properly
436 // note: i don't think this is actually neccessary
437 if ($opener != $closer) {
438 array_push($this->incontent, $opener);
439 $this->append_content("<$el />");
440 } elseif ($this->incontent) { // are we in the content construct still?
441 if ((count($this->incontent) > 1) or !$this->exclude_top) {
442 $this->append_content("</$el>");
444 } else { // shift the opening of the content construct off the normal stack
445 array_shift( $this->stack );
448 elseif ( $el == 'item' or $el == 'entry' )
450 $this->items[] = $this->current_item;
451 $this->current_item = array();
452 $this->initem = false;
454 $this->current_category = 0;
456 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' )
458 $this->intextinput = false;
460 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' )
462 $this->inimage = false;
464 elseif ($el == 'channel' or $el == 'feed' )
466 $this->inchannel = false;
469 array_shift( $this->stack );
472 if ( !$this->incontent ) { // Don't munge the namespace after finishing with elements in namespaced content constructs -CWJ
473 $this->current_namespace = false;
477 function concat (&$str1, $str2="") {
478 if (!isset($str1) ) {
484 function append_content($text) {
485 if ( $this->initem ) {
486 if ($this->current_namespace) {
487 $this->concat( $this->current_item[$this->current_namespace][ reset($this->incontent) ], $text );
489 $this->concat( $this->current_item[ reset($this->incontent) ], $text );
492 elseif ( $this->inchannel ) {
493 if ($this->current_namespace) {
494 $this->concat( $this->channel[$this->current_namespace][ reset($this->incontent) ], $text );
496 $this->concat( $this->channel[ reset($this->incontent) ], $text );
501 // smart append - field and namespace aware
502 function append($el, $text) {
506 if ( $this->current_namespace )
508 if ( $this->initem ) {
510 $this->current_item[ $this->current_namespace ][ $el ], $text);
512 elseif ($this->inchannel) {
514 $this->channel[ $this->current_namespace][ $el ], $text );
516 elseif ($this->intextinput) {
518 $this->textinput[ $this->current_namespace][ $el ], $text );
520 elseif ($this->inimage) {
522 $this->image[ $this->current_namespace ][ $el ], $text );
526 if ( $this->initem ) {
528 $this->current_item[ $el ], $text);
530 elseif ($this->intextinput) {
532 $this->textinput[ $el ], $text );
534 elseif ($this->inimage) {
536 $this->image[ $el ], $text );
538 elseif ($this->inchannel) {
540 $this->channel[ $el ], $text );
546 // smart count - field and namespace aware
547 function element_count ($el, $set = NULL) {
551 if ( $this->current_namespace )
553 if ( $this->initem ) {
554 if (!is_null($set)) { $this->current_item[ $this->current_namespace ][ $el.'#' ] = $set; }
555 $ret = (isset($this->current_item[ $this->current_namespace ][ $el.'#' ]) ?
556 $this->current_item[ $this->current_namespace ][ $el.'#' ] : 0);
558 elseif ($this->inchannel) {
559 if (!is_null($set)) { $this->channel[ $this->current_namespace ][ $el.'#' ] = $set; }
560 $ret = (isset($this->channel[ $this->current_namespace][ $el.'#' ]) ?
561 $this->channel[ $this->current_namespace][ $el.'#' ] : 0);
565 if ( $this->initem ) {
566 if (!is_null($set)) { $this->current_item[ $el.'#' ] = $set; }
567 $ret = (isset($this->current_item[ $el.'#' ]) ?
568 $this->current_item[ $el.'#' ] : 0);
570 elseif ($this->inchannel) {
571 if (!is_null($set)) {$this->channel[ $el.'#' ] = $set; }
572 $ret = (isset($this->channel[ $el.'#' ]) ?
573 $this->channel[ $el.'#' ] : 0);
579 function normalize_enclosure (&$source, $from, &$dest, $to, $i) {
580 $id_from = $this->element_id($from, $i);
581 $id_to = $this->element_id($to, $i);
582 if (isset($source["{$id_from}@"])) {
583 foreach (explode(',', $source["{$id_from}@"]) as $attr) {
584 if ($from=='link_enclosure' and $attr=='href') { // from Atom
585 $dest["{$id_to}@url"] = $source["{$id_from}@{$attr}"];
586 $dest["{$id_to}"] = $source["{$id_from}@{$attr}"];
588 elseif ($from=='enclosure' and $attr=='url') { // from RSS
589 $dest["{$id_to}@href"] = $source["{$id_from}@{$attr}"];
590 $dest["{$id_to}"] = $source["{$id_from}@{$attr}"];
593 $dest["{$id_to}@{$attr}"] = $source["{$id_from}@{$attr}"];
599 function normalize_atom_person (&$source, $person, &$dest, $to, $i) {
600 $id = $this->element_id($person, $i);
601 $id_to = $this->element_id($to, $i);
603 // Atom 0.3 <=> Atom 1.0
604 if ($this->feed_version >= 1.0) { $used = 'uri'; $norm = 'url'; }
605 else { $used = 'url'; $norm = 'uri'; }
607 if (isset($source["{$id}_{$used}"])) {
608 $dest["{$id_to}_{$norm}"] = $source["{$id}_{$used}"];
611 // Atom to RSS 2.0 and Dublin Core
612 // RSS 2.0 person strings should be valid e-mail addresses if possible.
613 if (isset($source["{$id}_email"])) {
614 $rss_author = $source["{$id}_email"];
616 if (isset($source["{$id}_name"])) {
617 $rss_author = $source["{$id}_name"]
618 . (isset($rss_author) ? " <$rss_author>" : '');
620 if (isset($rss_author)) {
621 $source[$id] = $rss_author; // goes to top-level author or contributor
622 $dest[$id_to] = $rss_author; // goes to dc:creator or dc:contributor
626 // Normalize Atom 1.0 and RSS 2.0 categories to Dublin Core...
627 function normalize_category (&$source, $from, &$dest, $to, $i) {
628 $cat_id = $this->element_id($from, $i);
629 $dc_id = $this->element_id($to, $i);
631 // first normalize category elements: Atom 1.0 <=> RSS 2.0
632 if ( isset($source["{$cat_id}@term"]) ) { // category identifier
633 $source[$cat_id] = $source["{$cat_id}@term"];
634 } elseif ( $this->feed_type == RSS ) {
635 $source["{$cat_id}@term"] = $source[$cat_id];
638 if ( isset($source["{$cat_id}@scheme"]) ) { // URI to taxonomy
639 $source["{$cat_id}@domain"] = $source["{$cat_id}@scheme"];
640 } elseif ( isset($source["{$cat_id}@domain"]) ) {
641 $source["{$cat_id}@scheme"] = $source["{$cat_id}@domain"];
644 // Now put the identifier into dc:subject
645 $dest[$dc_id] = $source[$cat_id];
649 function normalize_dc_subject (&$source, $from, &$dest, $to, $i) {
650 $dc_id = $this->element_id($from, $i);
651 $cat_id = $this->element_id($to, $i);
653 $dest[$cat_id] = $source[$dc_id]; // RSS 2.0
654 $dest["{$cat_id}@term"] = $source[$dc_id]; // Atom 1.0
657 // simplify the logic for normalize(). Makes sure that count of elements and
658 // each of multiple elements is normalized properly. If you need to mess
659 // with things like attributes or change formats or the like, pass it a
660 // callback to handle each element.
661 function normalize_element (&$source, $from, &$dest, $to, $via = NULL) {
662 if (isset($source[$from]) or isset($source["{$from}#"])) {
663 if (isset($source["{$from}#"])) {
664 $n = $source["{$from}#"];
665 $dest["{$to}#"] = $source["{$from}#"];
669 for ($i = 1; $i <= $n; $i++) {
670 if (isset($via)) { // custom callback for ninja attacks
671 $this->{$via}($source, $from, $dest, $to, $i);
673 else { // just make it the same
674 $from_id = $this->element_id($from, $i);
675 $to_id = $this->element_id($to, $i);
676 $dest[$to_id] = $source[$from_id];
682 function normalize () {
683 // if atom populate rss fields and normalize 0.3 and 1.0 feeds
684 if ( $this->is_atom() ) {
685 // Atom 1.0 elements <=> Atom 0.3 elements (Thanks, o brilliant wordsmiths of the Atom 1.0 standard!)
686 if ($this->feed_version < 1.0) {
687 $this->normalize_element($this->channel, 'tagline', $this->channel, 'subtitle');
688 $this->normalize_element($this->channel, 'copyright', $this->channel, 'rights');
689 $this->normalize_element($this->channel, 'modified', $this->channel, 'updated');
691 $this->normalize_element($this->channel, 'subtitle', $this->channel, 'tagline');
692 $this->normalize_element($this->channel, 'rights', $this->channel, 'copyright');
693 $this->normalize_element($this->channel, 'updated', $this->channel, 'modified');
695 $this->normalize_element($this->channel, 'author', $this->channel['dc'], 'creator', 'normalize_atom_person');
696 $this->normalize_element($this->channel, 'contributor', $this->channel['dc'], 'contributor', 'normalize_atom_person');
698 // Atom elements to RSS elements
699 $this->normalize_element($this->channel, 'subtitle', $this->channel, 'description');
701 if ( isset($this->channel['logo']) ) {
702 $this->normalize_element($this->channel, 'logo', $this->image, 'url');
703 $this->normalize_element($this->channel, 'link', $this->image, 'link');
704 $this->normalize_element($this->channel, 'title', $this->image, 'title');
707 for ( $i = 0; $i < count($this->items); $i++) {
708 $item = $this->items[$i];
710 // Atom 1.0 elements <=> Atom 0.3 elements
711 if ($this->feed_version < 1.0) {
712 $this->normalize_element($item, 'modified', $item, 'updated');
713 $this->normalize_element($item, 'issued', $item, 'published');
715 $this->normalize_element($item, 'updated', $item, 'modified');
716 $this->normalize_element($item, 'published', $item, 'issued');
719 // "If an atom:entry element does not contain
720 // atom:author elements, then the atom:author elements
721 // of the contained atom:source element are considered
722 // to apply. In an Atom Feed Document, the atom:author
723 // elements of the containing atom:feed element are
724 // considered to apply to the entry if there are no
725 // atom:author elements in the locations described
726 // above." <http://atompub.org/2005/08/17/draft-ietf-atompub-format-11.html#rfc.section.4.2.1>
727 if (!isset($item["author#"])) {
728 if (isset($item["source_author#"])) { // from aggregation source
730 $author = "source_author";
731 } elseif (isset($this->channel["author#"])) { // from containing feed
732 $source = $this->channel;
736 $item["author#"] = $source["{$author}#"];
737 for ($au = 1; $au <= $item["author#"]; $au++) {
738 $id_to = $this->element_id('author', $au);
739 $id_from = $this->element_id($author, $au);
741 $item[$id_to] = $source[$id_from];
742 foreach (array('name', 'email', 'uri', 'url') as $what) {
743 if (isset($source["{$id_from}_{$what}"])) {
744 $item["{$id_to}_{$what}"] = $source["{$id_from}_{$what}"];
750 // Atom elements to RSS elements
751 $this->normalize_element($item, 'author', $item['dc'], 'creator', 'normalize_atom_person');
752 $this->normalize_element($item, 'contributor', $item['dc'], 'contributor', 'normalize_atom_person');
753 $this->normalize_element($item, 'summary', $item, 'description');
754 $this->normalize_element($item, 'atom_content', $item['content'], 'encoded');
755 $this->normalize_element($item, 'link_enclosure', $item, 'enclosure', 'normalize_enclosure');
758 if ( isset($item['category#']) ) { // Atom 1.0 categories to dc:subject and RSS 2.0 categories
759 $this->normalize_element($item, 'category', $item['dc'], 'subject', 'normalize_category');
761 elseif ( isset($item['dc']['subject#']) ) { // dc:subject to Atom 1.0 and RSS 2.0 categories
762 $this->normalize_element($item['dc'], 'subject', $item, 'category', 'normalize_dc_subject');
765 // Normalized item timestamp
766 $atom_date = (isset($item['published']) ) ? $item['published'] : $item['updated'];
768 $epoch = @parse_w3cdtf($atom_date);
769 if ($epoch and $epoch > 0) {
770 $item['date_timestamp'] = $epoch;
774 $this->items[$i] = $item;
777 elseif ( $this->is_rss() ) {
778 // RSS elements to Atom elements
779 $this->normalize_element($this->channel, 'description', $this->channel, 'tagline'); // Atom 0.3
780 $this->normalize_element($this->channel, 'description', $this->channel, 'subtitle'); // Atom 1.0 (yay wordsmithing!)
781 $this->normalize_element($this->image, 'url', $this->channel, 'logo');
783 for ( $i = 0; $i < count($this->items); $i++) {
784 $item = $this->items[$i];
786 // RSS elements to Atom elements
787 $this->normalize_element($item, 'description', $item, 'summary');
788 $this->normalize_element($item['content'], 'encoded', $item, 'atom_content');
789 $this->normalize_element($item, 'enclosure', $item, 'link_enclosure', 'normalize_enclosure');
792 if ( isset($item['category#']) ) { // RSS 2.0 categories to dc:subject and Atom 1.0 categories
793 $this->normalize_element($item, 'category', $item['dc'], 'subject', 'normalize_category');
795 elseif ( isset($item['dc']['subject#']) ) { // dc:subject to Atom 1.0 and RSS 2.0 categories
796 $this->normalize_element($item['dc'], 'subject', $item, 'category', 'normalize_dc_subject');
799 // Normalized item timestamp
800 if ( $this->is_rss() == '1.0' and isset($item['dc']['date']) ) {
801 $epoch = @parse_w3cdtf($item['dc']['date']);
802 if ($epoch and $epoch > 0) {
803 $item['date_timestamp'] = $epoch;
806 elseif ( isset($item['pubdate']) ) {
807 $epoch = @strtotime($item['pubdate']);
809 $item['date_timestamp'] = $epoch;
813 $this->items[$i] = $item;
820 if ( $this->feed_type == RSS ) {
821 return $this->feed_version;
829 if ( $this->feed_type == ATOM ) {
830 return $this->feed_version;
838 * return XML parser, and possibly re-encoded source
841 function create_parser($source, $out_enc, $in_enc, $detect) {
842 if ( substr(phpversion(),0,1) == 5) {
843 $parser = $this->php5_create_parser($in_enc, $detect);
846 list($parser, $source) = $this->php4_create_parser($source, $in_enc, $detect);
849 $this->encoding = $out_enc;
850 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $out_enc);
853 return array($parser, $source);
857 * Instantiate an XML parser under PHP5
859 * PHP5 will do a fine job of detecting input encoding
860 * if passed an empty string as the encoding.
865 function php5_create_parser($in_enc, $detect) {
866 // by default php5 does a fine job of detecting input encodings
867 if(!$detect && $in_enc) {
868 return xml_parser_create($in_enc);
871 return xml_parser_create('');
876 * Instaniate an XML parser under PHP4
878 * Unfortunately PHP4's support for character encodings
879 * and especially XML and character encodings sucks. As
880 * long as the documents you parse only contain characters
881 * from the ISO-8859-1 character set (a superset of ASCII,
882 * and a subset of UTF-8) you're fine. However once you
883 * step out of that comfy little world things get mad, bad,
884 * and dangerous to know.
886 * The following code is based on SJM's work with FoF
887 * @see http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
890 function php4_create_parser($source, $in_enc, $detect) {
892 return array(xml_parser_create($in_enc), $source);
896 if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $source, $m)) {
897 $in_enc = strtoupper($m[1]);
898 $this->source_encoding = $in_enc;
905 if ($this->known_encoding($in_enc)) {
906 return array(xml_parser_create($in_enc), $source);
909 // the dectected encoding is not one of the simple encodings PHP knows
911 // attempt to use the iconv extension to
912 // cast the XML to a known encoding
913 // @see http://php.net/iconv
915 if (function_exists('iconv')) {
916 $encoded_source = iconv($in_enc,'UTF-8', $source);
917 if ($encoded_source) {
918 return array(xml_parser_create('UTF-8'), $encoded_source);
922 // iconv didn't work, try mb_convert_encoding
923 // @see http://php.net/mbstring
924 if(function_exists('mb_convert_encoding')) {
925 $encoded_source = mb_convert_encoding($source, 'UTF-8', $in_enc );
926 if ($encoded_source) {
927 return array(xml_parser_create('UTF-8'), $encoded_source);
932 $this->error("Feed is in an unsupported character encoding. ($in_enc) " .
933 "You may see strange artifacts, and mangled characters.",
936 return array(xml_parser_create(), $source);
939 function known_encoding($enc) {
940 $enc = strtoupper($enc);
941 if ( in_array($enc, $this->_KNOWN_ENCODINGS) ) {
949 function error ($errormsg, $lvl=E_USER_WARNING) {
950 // append PHP's error message if track_errors enabled
951 if ( isset($php_errormsg) ) {
952 $errormsg .= " ($php_errormsg)";
954 if ( MAGPIE_DEBUG ) {
955 trigger_error( $errormsg, $lvl);
958 error_log( $errormsg, 0);
961 $notices = E_USER_NOTICE|E_NOTICE;
962 if ( $lvl&$notices ) {
963 $this->WARNING = $errormsg;
965 $this->ERROR = $errormsg;
969 // magic ID function for multiple elemenets.
970 // can be called as static MagpieRSS::element_id()
971 function element_id ($el, $counter) {
972 return $el . (($counter > 1) ? '#'.$counter : '');
976 function map_attrs($k, $v) {
980 // patch to support medieval versions of PHP4.1.x,
981 // courtesy, Ryan Currie, ryan@digibliss.com
983 if (!function_exists('array_change_key_case')) {
984 define("CASE_UPPER",1);
985 define("CASE_LOWER",0);
988 function array_change_key_case($array,$case=CASE_LOWER) {
989 if ($case==CASE_LOWER) $cmd='strtolower';
990 elseif ($case==CASE_UPPER) $cmd='strtoupper';
991 foreach($array as $key=>$value) {
992 $output[$cmd($key)]=$value;