]> git.wh0rd.org Git - tt-rss.git/blob - lib/magpierss/rss_parse.inc
update translations
[tt-rss.git] / lib / magpierss / rss_parse.inc
1 <?php
2
3 /**
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.
7 *
8 * Handles RSS 0.9x, RSS 2.0, RSS 1.0, Atom 0.3, and Atom 1.0
9 *
10 * The lastest version of MagpieRSS can be obtained from:
11 * http://magpierss.sourceforge.net
12 *
13 * For questions, help, comments, discussion, etc., please join the
14 * Magpie mailing list:
15 * magpierss-general@lists.sourceforge.net
16 *
17 * @author           Kellan Elliott-McCrea <kellan@protest.net>
18 * @version          0.8
19 * @license          GPL
20 *
21 */
22
23 define('RSS', 'RSS');
24 define('ATOM', 'Atom');
25
26 function _convert_entities ($string) {
27         # Source: http://www.w3.org/TR/REC-html40/sgml/entities.html
28         $html_entities = array(
29                 "&nbsp",   "&iexcl",  "&cent",   "&pound",  "&curren", "&yen",    "&brvbar", "&sect",   "&uml",    "&copy",
30                 "&ordf",   "&laquo",  "&not",    "&shy",    "&reg",    "&macr",   "&deg",    "&plusmn", "&sup2",   "&sup3",
31                 "&acute",  "&micro",  "&para",   "&middot", "&cedil",  "&sup1",   "&ordm",   "&raquo",  "&frac14", "&frac12",
32                 "&frac34", "&iquest", "&Agrave", "&Aacute", "&Acirc",  "&Atilde", "&Auml",   "&Aring",  "&AElig",  "&Ccedil",
33                 "&Egrave", "&Eacute", "&Ecirc",  "&Euml",   "&Igrave", "&Iacute", "&Icirc",  "&Iuml",   "&ETH",    "&Ntilde",
34                 "&Ograve", "&Oacute", "&Ocirc",  "&Otilde", "&Ouml",   "&times",  "&Oslash", "&Ugrave", "&Uacute", "&Ucirc",
35                 "&Uuml",   "&Yacute", "&THORN",  "&szlig",  "&agrave", "&aacute", "&acirc",  "&atilde", "&auml",   "&aring",
36                 "&aelig",  "&ccedil", "&egrave", "&eacute", "&ecirc",  "&euml",   "&igrave", "&iacute", "&icirc",  "&iuml",
37                 "&eth",    "&ntilde", "&ograve", "&oacute", "&ocirc",  "&otilde", "&ouml",   "&divide", "&oslash", "&ugrave",
38                 "&uacute", "&ucirc",  "&uuml",   "&yacute", "&thorn",  "&yuml",);
39         $numeric_entities = array(
40                 "&#160;", "&#161;", "&#162;", "&#163;", "&#164;", "&#165;", "&#166;", "&#167;", "&#168;", "&#169;",
41                 "&#170;", "&#171;", "&#172;", "&#173;", "&#174;", "&#175;", "&#176;", "&#177;", "&#178;", "&#179;",
42                 "&#180;", "&#181;", "&#182;", "&#183;", "&#184;", "&#185;", "&#186;", "&#187;", "&#188;", "&#189;",
43                 "&#190;", "&#191;", "&#192;", "&#193;", "&#194;", "&#195;", "&#196;", "&#197;", "&#198;", "&#199;",
44                 "&#200;", "&#201;", "&#202;", "&#203;", "&#204;", "&#205;", "&#206;", "&#207;", "&#208;", "&#209;",
45                 "&#210;", "&#211;", "&#212;", "&#213;", "&#214;", "&#215;", "&#216;", "&#217;", "&#218;", "&#219;",
46                 "&#220;", "&#221;", "&#222;", "&#223;", "&#224;", "&#225;", "&#226;", "&#227;", "&#228;", "&#229;",
47                 "&#230;", "&#231;", "&#232;", "&#233;", "&#234;", "&#235;", "&#236;", "&#237;", "&#238;", "&#239;",
48                 "&#240;", "&#241;", "&#242;", "&#243;", "&#244;", "&#245;", "&#246;", "&#247;", "&#248;", "&#249;",
49                 "&#250;", "&#251;", "&#252;", "&#253;", "&#254;", "&#255;");
50         return str_replace($html_entities, $numeric_entities, $string);
51 }
52
53
54
55 require_once (MAGPIE_DIR . 'rss_utils.inc');
56
57 /**
58 * Hybrid parser, and object, takes RSS as a string and returns a simple object.
59 *
60 * see: rss_fetch.inc for a simpler interface with integrated caching support
61 *
62 */
63 class MagpieRSS {
64     var $parser;
65     
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();
70     var $image          = array();
71     var $feed_type;
72     var $feed_version;
73     var $encoding       = '';       // output encoding of parsed rss
74     
75     var $_source_encoding = '';     // only set if we have to parse xml prolog
76     
77     var $ERROR = "";
78     var $WARNING = "";
79     
80     // define some constants
81     
82     var $_ATOM_CONTENT_CONSTRUCTS = array(
83         'content', 'summary', 'title', /* common */
84     'info', 'tagline', 'copyright', /* Atom 0.3 */
85         'rights', 'subtitle', /* Atom 1.0 */
86     );
87     var $_XHTML_CONTENT_CONSTRUCTS = array('body', 'div');
88     var $_KNOWN_ENCODINGS    = array('UTF-8', 'US-ASCII', 'ISO-8859-1');
89
90     // parser variables, useless if you're not a parser, treat as private
91     var $stack              = array(); // parser stack
92     var $inchannel          = false;
93     var $initem             = false;
94     
95     var $incontent          = array(); // non-empty if in namespaced XML content field
96     var $exclude_top        = false; // true when Atom 1.0 type="xhtml"
97
98     var $intextinput        = false;
99     var $inimage            = false;
100     var $current_namespace  = false;
101     
102     /**
103      *  Set up XML parser, parse source, and return populated RSS object..
104      *   
105      *  @param string $source           string containing the RSS to be parsed
106      *
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
109      *         a little weird.
110      *
111      *  NOTE:  A lot of this is unnecessary but harmless with PHP5 
112      *
113      *
114      *  @param string $output_encoding  output the parsed RSS in this character 
115      *                                  set defaults to ISO-8859-1 as this is PHP's
116      *                                  default.
117      *
118      *                                  NOTE: might be changed to UTF-8 in future
119      *                                  versions.
120      *                               
121      *  @param string $input_encoding   the character set of the incoming RSS source. 
122      *                                  Leave blank and Magpie will try to figure it
123      *                                  out.
124      *                                  
125      *                                   
126      *  @param bool   $detect_encoding  if false Magpie won't attempt to detect
127      *                                  source encoding. (caveat emptor)
128      *
129      */
130     function MagpieRSS ($source, $output_encoding='ISO-8859-1', 
131                         $input_encoding=null, $detect_encoding=true) 
132     {   
133         # if PHP xml isn't compiled in, die
134         #
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",
138                            E_USER_ERROR );
139         }
140         
141         list($parser, $source) = $this->create_parser($source, 
142                 $output_encoding, $input_encoding, $detect_encoding);
143         
144         
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",
148                           E_USER_ERROR );
149         }
150
151         
152         $this->parser = $parser;
153         
154         # pass in parser, and a reference to this object
155         # setup handlers
156         #
157         xml_set_object( $this->parser, $this );
158         xml_set_element_handler($this->parser, 
159                 'feed_start_element', 'feed_end_element' );
160                         
161         xml_set_character_data_handler( $this->parser, 'feed_cdata' ); 
162
163         $source=str_replace("&lt;","&#60;",$source);
164         $source=str_replace("&gt;","&#62;",$source);
165         $source=str_replace("&amp;","&#38;",$source);
166     
167         $status = xml_parse( $this->parser, $source );
168
169                   # try to force convert everything to UTF-8 and parse again
170                   # to salvage at least some data from the feed 
171                   if (! $status) {
172             $errorcode = xml_get_error_code( $this->parser );
173                                 if ( $errorcode != XML_ERROR_NONE ) {
174
175                                         xml_parser_free( $this->parser );
176
177                                         if (preg_match('/<\?xml.*?encoding="([^ ]+)".*?\?>/',
178                                                                 $source, $matches)) {
179
180                                                 $enc = $matches[1];
181                                         } else {
182                                                 $enc = mb_detect_encoding($string);
183                                         }
184
185                                         # try fix XML, pass 1
186
187                                         $source = mb_convert_encoding($source, "UTF-8", $enc);
188
189                                         list($parser, $source) = $this->create_parser($source, 
190                         $output_encoding, $input_encoding, $detect_encoding);
191
192               $this->parser = $parser;
193         
194                         xml_set_object( $this->parser, $this );
195                         xml_set_element_handler($this->parser, 
196                         'feed_start_element', 'feed_end_element' );
197                         
198                         xml_set_character_data_handler( $this->parser, 'feed_cdata' ); 
199
200                                   $status = xml_parse( $this->parser, $source);
201
202                                   # try to fix XML, pass 2
203
204                                   if (! $status) {
205                             $errorcode = xml_get_error_code( $this->parser );
206                                                 if ( $errorcode != XML_ERROR_NONE ) {
207
208                                                 $source = _convert_entities($source);
209
210                                                 list($parser, $source) = $this->create_parser($source, 
211                                 $output_encoding, $input_encoding, $detect_encoding);
212         
213                       $this->parser = $parser;
214                 
215                                 xml_set_object( $this->parser, $this );
216                                 xml_set_element_handler($this->parser, 
217                                 'feed_start_element', 'feed_end_element' );
218                                 
219                                 xml_set_character_data_handler( $this->parser, 'feed_cdata' ); 
220         
221                                           $status = xml_parse( $this->parser, $source);
222
223                                                 }
224                                         }
225                                 }
226                         }
227
228         if (! $status ) {
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";
235
236                 $this->error( $errormsg );
237             }
238         }
239         
240         xml_parser_free( $this->parser );
241
242         $this->normalize();
243     }
244     
245     function feed_start_element($p, $element, &$attrs) {
246         $el = $element = strtolower($element);
247         $attrs = array_change_key_case($attrs, CASE_LOWER);
248         
249         // check for a namespace, and split if found
250         // Don't munge content tags
251         if ( empty($this->incontent) ) {   
252                 $ns = false;
253                 if ( strpos( $element, ':' ) ) {
254                    list($ns, $el) = split( ':', $element, 2); 
255                 }
256                 if ( $ns and $ns != 'rdf' ) {
257                    $this->current_namespace = $ns;
258                }
259           }
260
261         # if feed type isn't set, then this is first element of feed
262         # identify feed from root element
263         #
264         if (!isset($this->feed_type) ) {
265             if ( $el == 'rdf' ) {
266                 $this->feed_type = RSS;
267                 $this->feed_version = '1.0';
268             }
269             elseif ( $el == 'rss' ) {
270                 $this->feed_type = RSS;
271                 $this->feed_version = $attrs['version'];
272             }
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';
277                     }
278                     else { // Atom 0.3, probably.
279                         $this->feed_version = $attrs['version'];
280                     }
281                 $this->inchannel = true;
282             }
283             return;
284         }
285     
286         // if we're inside a namespaced content construct, treat tags as text
287         if ( !empty($this->incontent) ) 
288         {
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', 
293                           array_keys($attrs), 
294                           array_values($attrs) ) 
295                         );
296                     
297                         if (strlen($attrs_str) > 0) { $attrs_str = ' '.$attrs_str; }
298         
299                         $this->append_content( "<{$element}{$attrs_str}>"  );
300                 }
301                 array_push($this->incontent, $el); // stack for parsing content XML
302         } 
303         
304         elseif ( $el == 'channel' )  {
305             $this->inchannel = true;
306         }
307     
308         elseif ($el == 'item' or $el == 'entry' ) 
309         {
310             $this->initem = true;
311             if ( isset($attrs['rdf:about']) ) {
312                 $this->current_item['about'] = $attrs['rdf:about']; 
313             }
314         }
315
316         // if we're in the default namespace of an RSS feed,
317         //  record textinput or image fields
318         elseif ( 
319             $this->feed_type == RSS and 
320             $this->current_namespace == '' and 
321             $el == 'textinput' ) 
322         {
323             $this->intextinput = true;
324         }
325         
326         elseif (
327             $this->feed_type == RSS and 
328             $this->current_namespace == '' and 
329             $el == 'image' ) 
330         {
331             $this->inimage = true;
332         }
333         
334         // set stack[0] to current element
335         else {
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.
339
340               $atom_link = false;
341               if ($this->feed_type == ATOM and $el == 'link') {
342                     $atom_link = true;
343                     if (isset($attrs['rel']) and $attrs['rel'] != 'alternate') {
344                           $el = $el . "_" . $attrs['rel'];  // pseudo-element names for Atom link elements
345                     }
346               }
347               # handle atom content constructs
348               elseif ( $this->feed_type == ATOM and in_array($el, $this->_ATOM_CONTENT_CONSTRUCTS) )
349               {
350                     // avoid clashing w/ RSS mod_content
351                     if ($el == 'content' ) {
352                           $el = 'atom_content';
353                     }
354
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
360
361                     if ( isset($attrs['type']) and trim(strtolower($attrs['type']))=='xhtml') {
362                         $this->exclude_top = true;
363                     } else {
364                         $this->exclude_top = false;
365                     }
366               }
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) )
371               {
372                     $this->current_namespace = 'xhtml';
373                     $this->incontent = array();
374                     array_push($this->incontent, $el); // start a stack
375                     $this->exclude_top = false;
376               }
377
378               array_unshift($this->stack, $el);
379               $elpath = join('_', array_reverse($this->stack));
380         
381               $n = $this->element_count($elpath);
382               $this->element_count($elpath, $n+1);
383         
384               if ($n > 0) {
385                   array_shift($this->stack);
386                   array_unshift($this->stack, $el.'#'.($n+1));
387                   $elpath = join('_', array_reverse($this->stack));
388               }
389
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']);
395               }
396         
397               // add attributes
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);
402                     }
403               }
404        }
405     }
406     
407
408     
409     function feed_cdata ($p, $text) {
410         
411         if ($this->incontent) {
412             $this->append_content( $text );
413         }
414         else {
415             $current_el = join('_', array_reverse($this->stack));
416             $this->append($current_el, $text);
417         }
418     }
419     
420     function feed_end_element ($p, $el) {
421         $el = strtolower($el);
422
423         if ( $this->incontent ) {
424         $opener = array_pop($this->incontent);
425
426         // Don't get bamboozled by namespace voodoo
427         if (strpos($el, ':')) { list($ns, $closer) = split(':', $el); }
428         else { $ns = false; $closer = $el; }
429
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';
433         }
434
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>");
443             }
444         } else { // shift the opening of the content construct off the normal stack
445             array_shift( $this->stack );
446         }
447         }
448         elseif ( $el == 'item' or $el == 'entry' ) 
449         {
450             $this->items[] = $this->current_item;
451             $this->current_item = array();
452             $this->initem = false;
453
454         $this->current_category = 0;
455         }
456        elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' ) 
457         {
458             $this->intextinput = false;
459         }
460         elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' ) 
461         {
462             $this->inimage = false;
463         }
464         elseif ($el == 'channel' or $el == 'feed' ) 
465         {
466             $this->inchannel = false;
467         }
468         else {
469         array_shift( $this->stack );
470         }
471         
472     if ( !$this->incontent ) { // Don't munge the namespace after finishing with elements in namespaced content constructs -CWJ
473         $this->current_namespace = false;
474     }
475     }
476     
477     function concat (&$str1, $str2="") {
478         if (!isset($str1) ) {
479             $str1="";
480         }
481         $str1 .= $str2;
482     }
483     
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 );
488         } else {
489             $this->concat( $this->current_item[ reset($this->incontent) ], $text );
490         }
491     }
492     elseif ( $this->inchannel ) {
493         if ($this->current_namespace) {
494             $this->concat( $this->channel[$this->current_namespace][ reset($this->incontent) ], $text );
495         } else {
496             $this->concat( $this->channel[ reset($this->incontent) ], $text );
497         }
498     }
499     }
500     
501     // smart append - field and namespace aware
502     function append($el, $text) {
503         if (!$el) {
504             return;
505         }
506         if ( $this->current_namespace ) 
507         {
508             if ( $this->initem ) {
509             $this->concat(
510                 $this->current_item[ $this->current_namespace ][ $el ], $text);
511             }
512             elseif ($this->inchannel) {
513         $this->concat(
514             $this->channel[ $this->current_namespace][ $el ], $text );
515         }
516             elseif ($this->intextinput) {
517                 $this->concat(
518                     $this->textinput[ $this->current_namespace][ $el ], $text );
519             }
520             elseif ($this->inimage) {
521                 $this->concat(
522                     $this->image[ $this->current_namespace ][ $el ], $text );
523             }
524         }
525         else {
526             if ( $this->initem ) {
527         $this->concat(
528             $this->current_item[ $el ], $text);
529             }
530             elseif ($this->intextinput) {
531                 $this->concat(
532                     $this->textinput[ $el ], $text );
533             }
534             elseif ($this->inimage) {
535                 $this->concat(
536                     $this->image[ $el ], $text );
537             }
538             elseif ($this->inchannel) {
539         $this->concat(
540             $this->channel[ $el ], $text );
541             }
542             
543         }
544     }
545
546     // smart count - field and namespace aware
547     function element_count ($el, $set = NULL) {
548         if (!$el) {
549             return;
550         }
551         if ( $this->current_namespace ) 
552         {
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);
557             }
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);
562         }
563         }
564         else {
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);
569             }
570             elseif ($this->inchannel) {
571             if (!is_null($set)) {$this->channel[ $el.'#' ] = $set; }
572             $ret = (isset($this->channel[ $el.'#' ]) ?
573             $this->channel[ $el.'#' ] : 0);
574         }
575         }
576     return $ret;
577     }
578
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}"];
587                 }
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}"];
591                 }
592                 else {
593                     $dest["{$id_to}@{$attr}"] = $source["{$id_from}@{$attr}"];
594                 }
595             }
596         }
597     }
598
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);
602
603             // Atom 0.3 <=> Atom 1.0
604         if ($this->feed_version >= 1.0) { $used = 'uri'; $norm = 'url'; }
605         else { $used = 'url'; $norm = 'uri'; }
606
607         if (isset($source["{$id}_{$used}"])) {
608             $dest["{$id_to}_{$norm}"] = $source["{$id}_{$used}"];
609         }
610
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"];
615         }
616         if (isset($source["{$id}_name"])) {
617             $rss_author = $source["{$id}_name"]
618                 . (isset($rss_author) ? " <$rss_author>" : '');
619         }
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
623         }
624     }
625
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);
630
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];
636         }
637         
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"];
642         }
643
644         // Now put the identifier into dc:subject
645         $dest[$dc_id] = $source[$cat_id];
646     }
647     
648     // ... or vice versa
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);
652
653         $dest[$cat_id] = $source[$dc_id];       // RSS 2.0
654         $dest["{$cat_id}@term"] = $source[$dc_id];  // Atom 1.0
655     }
656
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}#"];
666             }
667             else { $n = 1; }
668
669             for ($i = 1; $i <= $n; $i++) {
670                 if (isset($via)) { // custom callback for ninja attacks
671                     $this->{$via}($source, $from, $dest, $to, $i);
672                 }
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];
677                 }
678             }
679         }
680     }
681
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');
690         } else {
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');
694         }
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');
697
698         // Atom elements to RSS elements
699         $this->normalize_element($this->channel, 'subtitle', $this->channel, 'description');
700         
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');
705         }
706
707         for ( $i = 0; $i < count($this->items); $i++) {
708             $item = $this->items[$i];
709
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');
714             } else {
715                 $this->normalize_element($item, 'updated', $item, 'modified');
716                 $this->normalize_element($item, 'published', $item, 'issued');
717             }
718
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
729                     $source = $item;
730                     $author = "source_author";
731                 } elseif (isset($this->channel["author#"])) { // from containing feed
732                     $source = $this->channel;
733                     $author = "author";
734                 }
735
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);
740                     
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}"];
745                         }
746                     }
747                 }
748             }
749
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');
756
757             // Categories
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');
760             }
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');
763             }
764
765             // Normalized item timestamp
766             $atom_date = (isset($item['published']) ) ? $item['published'] : $item['updated'];
767             if ( $atom_date ) {
768                 $epoch = @parse_w3cdtf($atom_date);
769                 if ($epoch and $epoch > 0) {
770                     $item['date_timestamp'] = $epoch;
771                 }
772             }
773
774             $this->items[$i] = $item;
775         }
776         }
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');
782
783             for ( $i = 0; $i < count($this->items); $i++) {
784                 $item = $this->items[$i];
785         
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');
790
791         // Categories
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');
794         }
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');
797         }
798
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;
804                     }
805                 }
806                 elseif ( isset($item['pubdate']) ) {
807                     $epoch = @strtotime($item['pubdate']);
808                     if ($epoch > 0) {
809                         $item['date_timestamp'] = $epoch;
810                     }
811                 }
812
813                 $this->items[$i] = $item;
814             }
815         }
816     }
817     
818     
819     function is_rss () {
820         if ( $this->feed_type == RSS ) {
821             return $this->feed_version; 
822         }
823         else {
824             return false;
825         }
826     }
827     
828     function is_atom() {
829         if ( $this->feed_type == ATOM ) {
830             return $this->feed_version;
831         }
832         else {
833             return false;
834         }
835     }
836
837     /**
838     * return XML parser, and possibly re-encoded source
839     *
840     */
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);
844         }
845         else {
846             list($parser, $source) = $this->php4_create_parser($source, $in_enc, $detect);
847         }
848         if ($out_enc) {
849             $this->encoding = $out_enc;
850             xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $out_enc);
851         }
852         
853         return array($parser, $source);
854     }
855     
856     /**
857     * Instantiate an XML parser under PHP5
858     *
859     * PHP5 will do a fine job of detecting input encoding
860     * if passed an empty string as the encoding. 
861     *
862     * All hail libxml2!
863     *
864     */
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);
869         }
870         else {
871             return xml_parser_create('');
872         }
873     }
874     
875     /**
876     * Instaniate an XML parser under PHP4
877     *
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.
885     *
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
888     *
889     */
890     function php4_create_parser($source, $in_enc, $detect) {
891         if ( !$detect ) {
892             return array(xml_parser_create($in_enc), $source);
893         }
894         
895         if (!$in_enc) {
896             if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $source, $m)) {
897                 $in_enc = strtoupper($m[1]);
898                 $this->source_encoding = $in_enc;
899             }
900             else {
901                 $in_enc = 'UTF-8';
902             }
903         }
904         
905         if ($this->known_encoding($in_enc)) {
906             return array(xml_parser_create($in_enc), $source);
907         }
908         
909         // the dectected encoding is not one of the simple encodings PHP knows
910         
911         // attempt to use the iconv extension to
912         // cast the XML to a known encoding
913         // @see http://php.net/iconv
914        
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);
919             }
920         }
921         
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);
928             }
929         }
930         
931         // else 
932         $this->error("Feed is in an unsupported character encoding. ($in_enc) " .
933                      "You may see strange artifacts, and mangled characters.",
934                      E_USER_NOTICE);
935             
936         return array(xml_parser_create(), $source);
937     }
938     
939     function known_encoding($enc) {
940         $enc = strtoupper($enc);
941         if ( in_array($enc, $this->_KNOWN_ENCODINGS) ) {
942             return $enc;
943         }
944         else {
945             return false;
946         }
947     }
948
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)";
953         }
954         if ( MAGPIE_DEBUG ) {
955             trigger_error( $errormsg, $lvl);        
956         }
957         else {
958             error_log( $errormsg, 0);
959         }
960         
961         $notices = E_USER_NOTICE|E_NOTICE;
962         if ( $lvl&$notices ) {
963             $this->WARNING = $errormsg;
964         } else {
965             $this->ERROR = $errormsg;
966         }
967     }
968
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 : '');
973     }
974 } // end class RSS
975
976 function map_attrs($k, $v) {
977     return "$k=\"$v\"";
978 }
979
980 // patch to support medieval versions of PHP4.1.x, 
981 // courtesy, Ryan Currie, ryan@digibliss.com
982
983 if (!function_exists('array_change_key_case')) {
984     define("CASE_UPPER",1);
985     define("CASE_LOWER",0);
986
987
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;
993        }
994        return $output;
995     }
996
997 }
998
999 ?>