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 $status = xml_parse( $this->parser, $source );
165 # try to force convert everything to UTF-8 and parse again
166 # to salvage at least some data from the feed
168 $errorcode = xml_get_error_code( $this->parser );
169 if ( $errorcode != XML_ERROR_NONE ) {
171 xml_parser_free( $this->parser );
173 if (preg_match('/<\?xml.*?encoding="([^ ]+)".*?\?>/',
174 $source, $matches)) {
178 $enc = mb_detect_encoding($string);
181 # try fix XML, pass 1
183 $source = mb_convert_encoding($source, "UTF-8", $enc);
185 list($parser, $source) = $this->create_parser($source,
186 $output_encoding, $input_encoding, $detect_encoding);
188 $this->parser = $parser;
190 xml_set_object( $this->parser, $this );
191 xml_set_element_handler($this->parser,
192 'feed_start_element', 'feed_end_element' );
194 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
196 $status = xml_parse( $this->parser, $source);
198 # try to fix XML, pass 2
201 $errorcode = xml_get_error_code( $this->parser );
202 if ( $errorcode != XML_ERROR_NONE ) {
204 $source = _convert_entities($source);
206 list($parser, $source) = $this->create_parser($source,
207 $output_encoding, $input_encoding, $detect_encoding);
209 $this->parser = $parser;
211 xml_set_object( $this->parser, $this );
212 xml_set_element_handler($this->parser,
213 'feed_start_element', 'feed_end_element' );
215 xml_set_character_data_handler( $this->parser, 'feed_cdata' );
217 $status = xml_parse( $this->parser, $source);
225 $errorcode = xml_get_error_code( $this->parser );
226 if ( $errorcode != XML_ERROR_NONE ) {
227 $xml_error = xml_error_string( $errorcode );
228 $error_line = xml_get_current_line_number($this->parser);
229 $error_col = xml_get_current_column_number($this->parser);
230 $errormsg = "$xml_error at line $error_line, column $error_col";
232 $this->error( $errormsg );
236 xml_parser_free( $this->parser );
241 function feed_start_element($p, $element, &$attrs) {
242 $el = $element = strtolower($element);
243 $attrs = array_change_key_case($attrs, CASE_LOWER);
245 // check for a namespace, and split if found
246 // Don't munge content tags
247 if ( empty($this->incontent) ) {
249 if ( strpos( $element, ':' ) ) {
250 list($ns, $el) = split( ':', $element, 2);
252 if ( $ns and $ns != 'rdf' ) {
253 $this->current_namespace = $ns;
257 # if feed type isn't set, then this is first element of feed
258 # identify feed from root element
260 if (!isset($this->feed_type) ) {
261 if ( $el == 'rdf' ) {
262 $this->feed_type = RSS;
263 $this->feed_version = '1.0';
265 elseif ( $el == 'rss' ) {
266 $this->feed_type = RSS;
267 $this->feed_version = $attrs['version'];
269 elseif ( $el == 'feed' ) {
270 $this->feed_type = ATOM;
271 if ($attrs['xmlns'] == 'http://www.w3.org/2005/Atom') { // Atom 1.0
272 $this->feed_version = '1.0';
274 else { // Atom 0.3, probably.
275 $this->feed_version = $attrs['version'];
277 $this->inchannel = true;
282 // if we're inside a namespaced content construct, treat tags as text
283 if ( !empty($this->incontent) )
285 if ((count($this->incontent) > 1) or !$this->exclude_top) {
286 // if tags are inlined, then flatten
287 $attrs_str = join(' ',
288 array_map('map_attrs',
290 array_values($attrs) )
293 if (strlen($attrs_str) > 0) { $attrs_str = ' '.$attrs_str; }
295 $this->append_content( "<{$element}{$attrs_str}>" );
297 array_push($this->incontent, $el); // stack for parsing content XML
300 elseif ( $el == 'channel' ) {
301 $this->inchannel = true;
304 elseif ($el == 'item' or $el == 'entry' )
306 $this->initem = true;
307 if ( isset($attrs['rdf:about']) ) {
308 $this->current_item['about'] = $attrs['rdf:about'];
312 // if we're in the default namespace of an RSS feed,
313 // record textinput or image fields
315 $this->feed_type == RSS and
316 $this->current_namespace == '' and
319 $this->intextinput = true;
323 $this->feed_type == RSS and
324 $this->current_namespace == '' and
327 $this->inimage = true;
330 // set stack[0] to current element
332 // Atom support many links per containing element.
333 // Magpie treats link elements of type rel='alternate'
334 // as being equivalent to RSS's simple link element.
337 if ($this->feed_type == ATOM and $el == 'link') {
339 if (isset($attrs['rel']) and $attrs['rel'] != 'alternate') {
340 $el = $el . "_" . $attrs['rel']; // pseudo-element names for Atom link elements
343 # handle atom content constructs
344 elseif ( $this->feed_type == ATOM and in_array($el, $this->_ATOM_CONTENT_CONSTRUCTS) )
346 // avoid clashing w/ RSS mod_content
347 if ($el == 'content' ) {
348 $el = 'atom_content';
351 // assume that everything accepts namespaced XML
352 // (that will pass through some non-validating feeds;
353 // but so what? this isn't a validating parser)
354 $this->incontent = array();
355 array_push($this->incontent, $el); // start a stack
357 if ( isset($attrs['type']) and trim(strtolower($attrs['type']))=='xhtml') {
358 $this->exclude_top = true;
360 $this->exclude_top = false;
363 # Handle inline XHTML body elements --CWJ
364 elseif (($this->current_namespace=='xhtml' or
365 (isset($attrs['xmlns']) and $attrs['xmlns'] == 'http://www.w3.org/1999/xhtml'))
366 and in_array($el, $this->_XHTML_CONTENT_CONSTRUCTS) )
368 $this->current_namespace = 'xhtml';
369 $this->incontent = array();
370 array_push($this->incontent, $el); // start a stack
371 $this->exclude_top = false;
374 array_unshift($this->stack, $el);
375 $elpath = join('_', array_reverse($this->stack));
377 $n = $this->element_count($elpath);
378 $this->element_count($elpath, $n+1);
381 array_shift($this->stack);
382 array_unshift($this->stack, $el.'#'.($n+1));
383 $elpath = join('_', array_reverse($this->stack));
386 // this makes the baby Jesus cry, but we can't do it in normalize()
387 // because we've made the element name for Atom links unpredictable
388 // by tacking on the relation to the end. -CWJ
389 if ($atom_link and isset($attrs['href'])) {
390 $this->append($elpath, $attrs['href']);
394 if (count($attrs) > 0) {
395 $this->append($elpath.'@', join(',', array_keys($attrs)));
396 foreach ($attrs as $attr => $value) {
397 $this->append($elpath.'@'.$attr, $value);
405 function feed_cdata ($p, $text) {
407 if ($this->incontent) {
408 $this->append_content( $text );
411 $current_el = join('_', array_reverse($this->stack));
412 $this->append($current_el, $text);
416 function feed_end_element ($p, $el) {
417 $el = strtolower($el);
419 if ( $this->incontent ) {
420 $opener = array_pop($this->incontent);
422 // Don't get bamboozled by namespace voodoo
423 if (strpos($el, ':')) { list($ns, $closer) = split(':', $el); }
424 else { $ns = false; $closer = $el; }
426 // Don't get bamboozled by our munging of <atom:content>, either
427 if ($this->feed_type == ATOM and $closer == 'content') {
428 $closer = 'atom_content';
431 // balance tags properly
432 // note: i don't think this is actually neccessary
433 if ($opener != $closer) {
434 array_push($this->incontent, $opener);
435 $this->append_content("<$el />");
436 } elseif ($this->incontent) { // are we in the content construct still?
437 if ((count($this->incontent) > 1) or !$this->exclude_top) {
438 $this->append_content("</$el>");
440 } else { // shift the opening of the content construct off the normal stack
441 array_shift( $this->stack );
444 elseif ( $el == 'item' or $el == 'entry' )
446 $this->items[] = $this->current_item;
447 $this->current_item = array();
448 $this->initem = false;
450 $this->current_category = 0;
452 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' )
454 $this->intextinput = false;
456 elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' )
458 $this->inimage = false;
460 elseif ($el == 'channel' or $el == 'feed' )
462 $this->inchannel = false;
465 array_shift( $this->stack );
468 if ( !$this->incontent ) { // Don't munge the namespace after finishing with elements in namespaced content constructs -CWJ
469 $this->current_namespace = false;
473 function concat (&$str1, $str2="") {
474 if (!isset($str1) ) {
480 function append_content($text) {
481 if ( $this->initem ) {
482 if ($this->current_namespace) {
483 $this->concat( $this->current_item[$this->current_namespace][ reset($this->incontent) ], $text );
485 $this->concat( $this->current_item[ reset($this->incontent) ], $text );
488 elseif ( $this->inchannel ) {
489 if ($this->current_namespace) {
490 $this->concat( $this->channel[$this->current_namespace][ reset($this->incontent) ], $text );
492 $this->concat( $this->channel[ reset($this->incontent) ], $text );
497 // smart append - field and namespace aware
498 function append($el, $text) {
502 if ( $this->current_namespace )
504 if ( $this->initem ) {
506 $this->current_item[ $this->current_namespace ][ $el ], $text);
508 elseif ($this->inchannel) {
510 $this->channel[ $this->current_namespace][ $el ], $text );
512 elseif ($this->intextinput) {
514 $this->textinput[ $this->current_namespace][ $el ], $text );
516 elseif ($this->inimage) {
518 $this->image[ $this->current_namespace ][ $el ], $text );
522 if ( $this->initem ) {
524 $this->current_item[ $el ], $text);
526 elseif ($this->intextinput) {
528 $this->textinput[ $el ], $text );
530 elseif ($this->inimage) {
532 $this->image[ $el ], $text );
534 elseif ($this->inchannel) {
536 $this->channel[ $el ], $text );
542 // smart count - field and namespace aware
543 function element_count ($el, $set = NULL) {
547 if ( $this->current_namespace )
549 if ( $this->initem ) {
550 if (!is_null($set)) { $this->current_item[ $this->current_namespace ][ $el.'#' ] = $set; }
551 $ret = (isset($this->current_item[ $this->current_namespace ][ $el.'#' ]) ?
552 $this->current_item[ $this->current_namespace ][ $el.'#' ] : 0);
554 elseif ($this->inchannel) {
555 if (!is_null($set)) { $this->channel[ $this->current_namespace ][ $el.'#' ] = $set; }
556 $ret = (isset($this->channel[ $this->current_namespace][ $el.'#' ]) ?
557 $this->channel[ $this->current_namespace][ $el.'#' ] : 0);
561 if ( $this->initem ) {
562 if (!is_null($set)) { $this->current_item[ $el.'#' ] = $set; }
563 $ret = (isset($this->current_item[ $el.'#' ]) ?
564 $this->current_item[ $el.'#' ] : 0);
566 elseif ($this->inchannel) {
567 if (!is_null($set)) {$this->channel[ $el.'#' ] = $set; }
568 $ret = (isset($this->channel[ $el.'#' ]) ?
569 $this->channel[ $el.'#' ] : 0);
575 function normalize_enclosure (&$source, $from, &$dest, $to, $i) {
576 $id_from = $this->element_id($from, $i);
577 $id_to = $this->element_id($to, $i);
578 if (isset($source["{$id_from}@"])) {
579 foreach (explode(',', $source["{$id_from}@"]) as $attr) {
580 if ($from=='link_enclosure' and $attr=='href') { // from Atom
581 $dest["{$id_to}@url"] = $source["{$id_from}@{$attr}"];
582 $dest["{$id_to}"] = $source["{$id_from}@{$attr}"];
584 elseif ($from=='enclosure' and $attr=='url') { // from RSS
585 $dest["{$id_to}@href"] = $source["{$id_from}@{$attr}"];
586 $dest["{$id_to}"] = $source["{$id_from}@{$attr}"];
589 $dest["{$id_to}@{$attr}"] = $source["{$id_from}@{$attr}"];
595 function normalize_atom_person (&$source, $person, &$dest, $to, $i) {
596 $id = $this->element_id($person, $i);
597 $id_to = $this->element_id($to, $i);
599 // Atom 0.3 <=> Atom 1.0
600 if ($this->feed_version >= 1.0) { $used = 'uri'; $norm = 'url'; }
601 else { $used = 'url'; $norm = 'uri'; }
603 if (isset($source["{$id}_{$used}"])) {
604 $dest["{$id_to}_{$norm}"] = $source["{$id}_{$used}"];
607 // Atom to RSS 2.0 and Dublin Core
608 // RSS 2.0 person strings should be valid e-mail addresses if possible.
609 if (isset($source["{$id}_email"])) {
610 $rss_author = $source["{$id}_email"];
612 if (isset($source["{$id}_name"])) {
613 $rss_author = $source["{$id}_name"]
614 . (isset($rss_author) ? " <$rss_author>" : '');
616 if (isset($rss_author)) {
617 $source[$id] = $rss_author; // goes to top-level author or contributor
618 $dest[$id_to] = $rss_author; // goes to dc:creator or dc:contributor
622 // Normalize Atom 1.0 and RSS 2.0 categories to Dublin Core...
623 function normalize_category (&$source, $from, &$dest, $to, $i) {
624 $cat_id = $this->element_id($from, $i);
625 $dc_id = $this->element_id($to, $i);
627 // first normalize category elements: Atom 1.0 <=> RSS 2.0
628 if ( isset($source["{$cat_id}@term"]) ) { // category identifier
629 $source[$cat_id] = $source["{$cat_id}@term"];
630 } elseif ( $this->feed_type == RSS ) {
631 $source["{$cat_id}@term"] = $source[$cat_id];
634 if ( isset($source["{$cat_id}@scheme"]) ) { // URI to taxonomy
635 $source["{$cat_id}@domain"] = $source["{$cat_id}@scheme"];
636 } elseif ( isset($source["{$cat_id}@domain"]) ) {
637 $source["{$cat_id}@scheme"] = $source["{$cat_id}@domain"];
640 // Now put the identifier into dc:subject
641 $dest[$dc_id] = $source[$cat_id];
645 function normalize_dc_subject (&$source, $from, &$dest, $to, $i) {
646 $dc_id = $this->element_id($from, $i);
647 $cat_id = $this->element_id($to, $i);
649 $dest[$cat_id] = $source[$dc_id]; // RSS 2.0
650 $dest["{$cat_id}@term"] = $source[$dc_id]; // Atom 1.0
653 // simplify the logic for normalize(). Makes sure that count of elements and
654 // each of multiple elements is normalized properly. If you need to mess
655 // with things like attributes or change formats or the like, pass it a
656 // callback to handle each element.
657 function normalize_element (&$source, $from, &$dest, $to, $via = NULL) {
658 if (isset($source[$from]) or isset($source["{$from}#"])) {
659 if (isset($source["{$from}#"])) {
660 $n = $source["{$from}#"];
661 $dest["{$to}#"] = $source["{$from}#"];
665 for ($i = 1; $i <= $n; $i++) {
666 if (isset($via)) { // custom callback for ninja attacks
667 $this->{$via}($source, $from, $dest, $to, $i);
669 else { // just make it the same
670 $from_id = $this->element_id($from, $i);
671 $to_id = $this->element_id($to, $i);
672 $dest[$to_id] = $source[$from_id];
678 function normalize () {
679 // if atom populate rss fields and normalize 0.3 and 1.0 feeds
680 if ( $this->is_atom() ) {
681 // Atom 1.0 elements <=> Atom 0.3 elements (Thanks, o brilliant wordsmiths of the Atom 1.0 standard!)
682 if ($this->feed_version < 1.0) {
683 $this->normalize_element($this->channel, 'tagline', $this->channel, 'subtitle');
684 $this->normalize_element($this->channel, 'copyright', $this->channel, 'rights');
685 $this->normalize_element($this->channel, 'modified', $this->channel, 'updated');
687 $this->normalize_element($this->channel, 'subtitle', $this->channel, 'tagline');
688 $this->normalize_element($this->channel, 'rights', $this->channel, 'copyright');
689 $this->normalize_element($this->channel, 'updated', $this->channel, 'modified');
691 $this->normalize_element($this->channel, 'author', $this->channel['dc'], 'creator', 'normalize_atom_person');
692 $this->normalize_element($this->channel, 'contributor', $this->channel['dc'], 'contributor', 'normalize_atom_person');
694 // Atom elements to RSS elements
695 $this->normalize_element($this->channel, 'subtitle', $this->channel, 'description');
697 if ( isset($this->channel['logo']) ) {
698 $this->normalize_element($this->channel, 'logo', $this->image, 'url');
699 $this->normalize_element($this->channel, 'link', $this->image, 'link');
700 $this->normalize_element($this->channel, 'title', $this->image, 'title');
703 for ( $i = 0; $i < count($this->items); $i++) {
704 $item = $this->items[$i];
706 // Atom 1.0 elements <=> Atom 0.3 elements
707 if ($this->feed_version < 1.0) {
708 $this->normalize_element($item, 'modified', $item, 'updated');
709 $this->normalize_element($item, 'issued', $item, 'published');
711 $this->normalize_element($item, 'updated', $item, 'modified');
712 $this->normalize_element($item, 'published', $item, 'issued');
715 // "If an atom:entry element does not contain
716 // atom:author elements, then the atom:author elements
717 // of the contained atom:source element are considered
718 // to apply. In an Atom Feed Document, the atom:author
719 // elements of the containing atom:feed element are
720 // considered to apply to the entry if there are no
721 // atom:author elements in the locations described
722 // above." <http://atompub.org/2005/08/17/draft-ietf-atompub-format-11.html#rfc.section.4.2.1>
723 if (!isset($item["author#"])) {
724 if (isset($item["source_author#"])) { // from aggregation source
726 $author = "source_author";
727 } elseif (isset($this->channel["author#"])) { // from containing feed
728 $source = $this->channel;
732 $item["author#"] = $source["{$author}#"];
733 for ($au = 1; $au <= $item["author#"]; $au++) {
734 $id_to = $this->element_id('author', $au);
735 $id_from = $this->element_id($author, $au);
737 $item[$id_to] = $source[$id_from];
738 foreach (array('name', 'email', 'uri', 'url') as $what) {
739 if (isset($source["{$id_from}_{$what}"])) {
740 $item["{$id_to}_{$what}"] = $source["{$id_from}_{$what}"];
746 // Atom elements to RSS elements
747 $this->normalize_element($item, 'author', $item['dc'], 'creator', 'normalize_atom_person');
748 $this->normalize_element($item, 'contributor', $item['dc'], 'contributor', 'normalize_atom_person');
749 $this->normalize_element($item, 'summary', $item, 'description');
750 $this->normalize_element($item, 'atom_content', $item['content'], 'encoded');
751 $this->normalize_element($item, 'link_enclosure', $item, 'enclosure', 'normalize_enclosure');
754 if ( isset($item['category#']) ) { // Atom 1.0 categories to dc:subject and RSS 2.0 categories
755 $this->normalize_element($item, 'category', $item['dc'], 'subject', 'normalize_category');
757 elseif ( isset($item['dc']['subject#']) ) { // dc:subject to Atom 1.0 and RSS 2.0 categories
758 $this->normalize_element($item['dc'], 'subject', $item, 'category', 'normalize_dc_subject');
761 // Normalized item timestamp
762 $atom_date = (isset($item['published']) ) ? $item['published'] : $item['updated'];
764 $epoch = @parse_w3cdtf($atom_date);
765 if ($epoch and $epoch > 0) {
766 $item['date_timestamp'] = $epoch;
770 $this->items[$i] = $item;
773 elseif ( $this->is_rss() ) {
774 // RSS elements to Atom elements
775 $this->normalize_element($this->channel, 'description', $this->channel, 'tagline'); // Atom 0.3
776 $this->normalize_element($this->channel, 'description', $this->channel, 'subtitle'); // Atom 1.0 (yay wordsmithing!)
777 $this->normalize_element($this->image, 'url', $this->channel, 'logo');
779 for ( $i = 0; $i < count($this->items); $i++) {
780 $item = $this->items[$i];
782 // RSS elements to Atom elements
783 $this->normalize_element($item, 'description', $item, 'summary');
784 $this->normalize_element($item['content'], 'encoded', $item, 'atom_content');
785 $this->normalize_element($item, 'enclosure', $item, 'link_enclosure', 'normalize_enclosure');
788 if ( isset($item['category#']) ) { // RSS 2.0 categories to dc:subject and Atom 1.0 categories
789 $this->normalize_element($item, 'category', $item['dc'], 'subject', 'normalize_category');
791 elseif ( isset($item['dc']['subject#']) ) { // dc:subject to Atom 1.0 and RSS 2.0 categories
792 $this->normalize_element($item['dc'], 'subject', $item, 'category', 'normalize_dc_subject');
795 // Normalized item timestamp
796 if ( $this->is_rss() == '1.0' and isset($item['dc']['date']) ) {
797 $epoch = @parse_w3cdtf($item['dc']['date']);
798 if ($epoch and $epoch > 0) {
799 $item['date_timestamp'] = $epoch;
802 elseif ( isset($item['pubdate']) ) {
803 $epoch = @strtotime($item['pubdate']);
805 $item['date_timestamp'] = $epoch;
809 $this->items[$i] = $item;
816 if ( $this->feed_type == RSS ) {
817 return $this->feed_version;
825 if ( $this->feed_type == ATOM ) {
826 return $this->feed_version;
834 * return XML parser, and possibly re-encoded source
837 function create_parser($source, $out_enc, $in_enc, $detect) {
838 if ( substr(phpversion(),0,1) == 5) {
839 $parser = $this->php5_create_parser($in_enc, $detect);
842 list($parser, $source) = $this->php4_create_parser($source, $in_enc, $detect);
845 $this->encoding = $out_enc;
846 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $out_enc);
849 return array($parser, $source);
853 * Instantiate an XML parser under PHP5
855 * PHP5 will do a fine job of detecting input encoding
856 * if passed an empty string as the encoding.
861 function php5_create_parser($in_enc, $detect) {
862 // by default php5 does a fine job of detecting input encodings
863 if(!$detect && $in_enc) {
864 return xml_parser_create($in_enc);
867 return xml_parser_create('');
872 * Instaniate an XML parser under PHP4
874 * Unfortunately PHP4's support for character encodings
875 * and especially XML and character encodings sucks. As
876 * long as the documents you parse only contain characters
877 * from the ISO-8859-1 character set (a superset of ASCII,
878 * and a subset of UTF-8) you're fine. However once you
879 * step out of that comfy little world things get mad, bad,
880 * and dangerous to know.
882 * The following code is based on SJM's work with FoF
883 * @see http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
886 function php4_create_parser($source, $in_enc, $detect) {
888 return array(xml_parser_create($in_enc), $source);
892 if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $source, $m)) {
893 $in_enc = strtoupper($m[1]);
894 $this->source_encoding = $in_enc;
901 if ($this->known_encoding($in_enc)) {
902 return array(xml_parser_create($in_enc), $source);
905 // the dectected encoding is not one of the simple encodings PHP knows
907 // attempt to use the iconv extension to
908 // cast the XML to a known encoding
909 // @see http://php.net/iconv
911 if (function_exists('iconv')) {
912 $encoded_source = iconv($in_enc,'UTF-8', $source);
913 if ($encoded_source) {
914 return array(xml_parser_create('UTF-8'), $encoded_source);
918 // iconv didn't work, try mb_convert_encoding
919 // @see http://php.net/mbstring
920 if(function_exists('mb_convert_encoding')) {
921 $encoded_source = mb_convert_encoding($source, 'UTF-8', $in_enc );
922 if ($encoded_source) {
923 return array(xml_parser_create('UTF-8'), $encoded_source);
928 $this->error("Feed is in an unsupported character encoding. ($in_enc) " .
929 "You may see strange artifacts, and mangled characters.",
932 return array(xml_parser_create(), $source);
935 function known_encoding($enc) {
936 $enc = strtoupper($enc);
937 if ( in_array($enc, $this->_KNOWN_ENCODINGS) ) {
945 function error ($errormsg, $lvl=E_USER_WARNING) {
946 // append PHP's error message if track_errors enabled
947 if ( isset($php_errormsg) ) {
948 $errormsg .= " ($php_errormsg)";
950 if ( MAGPIE_DEBUG ) {
951 trigger_error( $errormsg, $lvl);
954 error_log( $errormsg, 0);
957 $notices = E_USER_NOTICE|E_NOTICE;
958 if ( $lvl&$notices ) {
959 $this->WARNING = $errormsg;
961 $this->ERROR = $errormsg;
965 // magic ID function for multiple elemenets.
966 // can be called as static MagpieRSS::element_id()
967 function element_id ($el, $counter) {
968 return $el . (($counter > 1) ? '#'.$counter : '');
972 function map_attrs($k, $v) {
976 // patch to support medieval versions of PHP4.1.x,
977 // courtesy, Ryan Currie, ryan@digibliss.com
979 if (!function_exists('array_change_key_case')) {
980 define("CASE_UPPER",1);
981 define("CASE_LOWER",0);
984 function array_change_key_case($array,$case=CASE_LOWER) {
985 if ($case==CASE_LOWER) $cmd='strtolower';
986 elseif ($case==CASE_UPPER) $cmd='strtoupper';
987 foreach($array as $key=>$value) {
988 $output[$cmd($key)]=$value;