MDL-56001 lib: Upgrade simplepie to 1.4.2
[moodle.git] / lib / simplepie / library / SimplePie / Parser.php
1 <?php
2 /**
3  * SimplePie
4  *
5  * A PHP-Based RSS and Atom Feed Framework.
6  * Takes the hard work out of managing a complete RSS/Atom solution.
7  *
8  * Copyright (c) 2004-2016, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification, are
12  * permitted provided that the following conditions are met:
13  *
14  *      * Redistributions of source code must retain the above copyright notice, this list of
15  *        conditions and the following disclaimer.
16  *
17  *      * Redistributions in binary form must reproduce the above copyright notice, this list
18  *        of conditions and the following disclaimer in the documentation and/or other materials
19  *        provided with the distribution.
20  *
21  *      * Neither the name of the SimplePie Team nor the names of its contributors may be used
22  *        to endorse or promote products derived from this software without specific prior
23  *        written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
26  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
28  * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * @package SimplePie
36  * @copyright 2004-2016 Ryan Parman, Geoffrey Sneddon, Ryan McCue
37  * @author Ryan Parman
38  * @author Geoffrey Sneddon
39  * @author Ryan McCue
40  * @link http://simplepie.org/ SimplePie
41  * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42  */
44 /**
45  * Parses XML into something sane
46  *
47  *
48  * This class can be overloaded with {@see SimplePie::set_parser_class()}
49  *
50  * @package SimplePie
51  * @subpackage Parsing
52  */
53 class SimplePie_Parser
54 {
55         var $error_code;
56         var $error_string;
57         var $current_line;
58         var $current_column;
59         var $current_byte;
60         var $separator = ' ';
61         var $namespace = array('');
62         var $element = array('');
63         var $xml_base = array('');
64         var $xml_base_explicit = array(false);
65         var $xml_lang = array('');
66         var $data = array();
67         var $datas = array(array());
68         var $current_xhtml_construct = -1;
69         var $encoding;
70         protected $registry;
72         public function set_registry(SimplePie_Registry $registry)
73         {
74                 $this->registry = $registry;
75         }
77         public function parse(&$data, $encoding, $url = '')
78         {
79                 if (function_exists('Mf2\parse')) {
80                         // Check for both h-feed and h-entry, as both a feed with no entries
81                         // and a list of entries without an h-feed wrapper are both valid.
82                         $position = 0;
83                         while ($position = strpos($data, 'h-feed', $position)) {
84                                 $start = $position < 200 ? 0 : $position - 200;
85                                 $check = substr($data, $start, 400);
86                                 if (preg_match('/class="[^"]*h-feed/', $check)) {
87                                         return $this->parse_microformats($data, $url);
88                                 }
89                                 $position += 7;
90                         }
91                         $position = 0;
92                         while ($position = strpos($data, 'h-entry', $position)) {
93                                 $start = $position < 200 ? 0 : $position - 200;
94                                 $check = substr($data, $start, 400);
95                                 if (preg_match('/class="[^"]*h-entry/', $check)) {
96                                         return $this->parse_microformats($data, $url);
97                                 }
98                                 $position += 7;
99                         }
100                 }
102                 // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
103                 if (strtoupper($encoding) === 'US-ASCII')
104                 {
105                         $this->encoding = 'UTF-8';
106                 }
107                 else
108                 {
109                         $this->encoding = $encoding;
110                 }
112                 // Strip BOM:
113                 // UTF-32 Big Endian BOM
114                 if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
115                 {
116                         $data = substr($data, 4);
117                 }
118                 // UTF-32 Little Endian BOM
119                 elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
120                 {
121                         $data = substr($data, 4);
122                 }
123                 // UTF-16 Big Endian BOM
124                 elseif (substr($data, 0, 2) === "\xFE\xFF")
125                 {
126                         $data = substr($data, 2);
127                 }
128                 // UTF-16 Little Endian BOM
129                 elseif (substr($data, 0, 2) === "\xFF\xFE")
130                 {
131                         $data = substr($data, 2);
132                 }
133                 // UTF-8 BOM
134                 elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
135                 {
136                         $data = substr($data, 3);
137                 }
139                 if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
140                 {
141                         $declaration = $this->registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5)));
142                         if ($declaration->parse())
143                         {
144                                 $data = substr($data, $pos + 2);
145                                 $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' ."\n". $this->declare_html_entities() . $data;
146                         }
147                         else
148                         {
149                                 $this->error_string = 'SimplePie bug! Please report this!';
150                                 return false;
151                         }
152                 }
154                 $return = true;
156                 static $xml_is_sane = null;
157                 if ($xml_is_sane === null)
158                 {
159                         $parser_check = xml_parser_create();
160                         xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
161                         xml_parser_free($parser_check);
162                         $xml_is_sane = isset($values[0]['value']);
163                 }
165                 // Create the parser
166                 if ($xml_is_sane)
167                 {
168                         $xml = xml_parser_create_ns($this->encoding, $this->separator);
169                         xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
170                         xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
171                         xml_set_object($xml, $this);
172                         xml_set_character_data_handler($xml, 'cdata');
173                         xml_set_element_handler($xml, 'tag_open', 'tag_close');
175                         // Parse!
176                         if (!xml_parse($xml, $data, true))
177                         {
178                                 $this->error_code = xml_get_error_code($xml);
179                                 $this->error_string = xml_error_string($this->error_code);
180                                 $return = false;
181                         }
182                         $this->current_line = xml_get_current_line_number($xml);
183                         $this->current_column = xml_get_current_column_number($xml);
184                         $this->current_byte = xml_get_current_byte_index($xml);
185                         xml_parser_free($xml);
186                         return $return;
187                 }
188                 else
189                 {
190                         libxml_clear_errors();
191                         $xml = new XMLReader();
192                         $xml->xml($data);
193                         while (@$xml->read())
194                         {
195                                 switch ($xml->nodeType)
196                                 {
198                                         case constant('XMLReader::END_ELEMENT'):
199                                                 if ($xml->namespaceURI !== '')
200                                                 {
201                                                         $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
202                                                 }
203                                                 else
204                                                 {
205                                                         $tagName = $xml->localName;
206                                                 }
207                                                 $this->tag_close(null, $tagName);
208                                                 break;
209                                         case constant('XMLReader::ELEMENT'):
210                                                 $empty = $xml->isEmptyElement;
211                                                 if ($xml->namespaceURI !== '')
212                                                 {
213                                                         $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
214                                                 }
215                                                 else
216                                                 {
217                                                         $tagName = $xml->localName;
218                                                 }
219                                                 $attributes = array();
220                                                 while ($xml->moveToNextAttribute())
221                                                 {
222                                                         if ($xml->namespaceURI !== '')
223                                                         {
224                                                                 $attrName = $xml->namespaceURI . $this->separator . $xml->localName;
225                                                         }
226                                                         else
227                                                         {
228                                                                 $attrName = $xml->localName;
229                                                         }
230                                                         $attributes[$attrName] = $xml->value;
231                                                 }
232                                                 $this->tag_open(null, $tagName, $attributes);
233                                                 if ($empty)
234                                                 {
235                                                         $this->tag_close(null, $tagName);
236                                                 }
237                                                 break;
238                                         case constant('XMLReader::TEXT'):
240                                         case constant('XMLReader::CDATA'):
241                                                 $this->cdata(null, $xml->value);
242                                                 break;
243                                 }
244                         }
245                         if ($error = libxml_get_last_error())
246                         {
247                                 $this->error_code = $error->code;
248                                 $this->error_string = $error->message;
249                                 $this->current_line = $error->line;
250                                 $this->current_column = $error->column;
251                                 return false;
252                         }
253                         else
254                         {
255                                 return true;
256                         }
257                 }
258         }
260         public function get_error_code()
261         {
262                 return $this->error_code;
263         }
265         public function get_error_string()
266         {
267                 return $this->error_string;
268         }
270         public function get_current_line()
271         {
272                 return $this->current_line;
273         }
275         public function get_current_column()
276         {
277                 return $this->current_column;
278         }
280         public function get_current_byte()
281         {
282                 return $this->current_byte;
283         }
285         public function get_data()
286         {
287                 return $this->data;
288         }
290         public function tag_open($parser, $tag, $attributes)
291         {
292                 list($this->namespace[], $this->element[]) = $this->split_ns($tag);
294                 $attribs = array();
295                 foreach ($attributes as $name => $value)
296                 {
297                         list($attrib_namespace, $attribute) = $this->split_ns($name);
298                         $attribs[$attrib_namespace][$attribute] = $value;
299                 }
301                 if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['base']))
302                 {
303                         $base = $this->registry->call('Misc', 'absolutize_url', array($attribs[SIMPLEPIE_NAMESPACE_XML]['base'], end($this->xml_base)));
304                         if ($base !== false)
305                         {
306                                 $this->xml_base[] = $base;
307                                 $this->xml_base_explicit[] = true;
308                         }
309                 }
310                 else
311                 {
312                         $this->xml_base[] = end($this->xml_base);
313                         $this->xml_base_explicit[] = end($this->xml_base_explicit);
314                 }
316                 if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['lang']))
317                 {
318                         $this->xml_lang[] = $attribs[SIMPLEPIE_NAMESPACE_XML]['lang'];
319                 }
320                 else
321                 {
322                         $this->xml_lang[] = end($this->xml_lang);
323                 }
325                 if ($this->current_xhtml_construct >= 0)
326                 {
327                         $this->current_xhtml_construct++;
328                         if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML)
329                         {
330                                 $this->data['data'] .= '<' . end($this->element);
331                                 if (isset($attribs['']))
332                                 {
333                                         foreach ($attribs[''] as $name => $value)
334                                         {
335                                                 $this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"';
336                                         }
337                                 }
338                                 $this->data['data'] .= '>';
339                         }
340                 }
341                 else
342                 {
343                         $this->datas[] =& $this->data;
344                         $this->data =& $this->data['child'][end($this->namespace)][end($this->element)][];
345                         $this->data = array('data' => '', 'attribs' => $attribs, 'xml_base' => end($this->xml_base), 'xml_base_explicit' => end($this->xml_base_explicit), 'xml_lang' => end($this->xml_lang));
346                         if ((end($this->namespace) === SIMPLEPIE_NAMESPACE_ATOM_03 && in_array(end($this->element), array('title', 'tagline', 'copyright', 'info', 'summary', 'content')) && isset($attribs['']['mode']) && $attribs['']['mode'] === 'xml')
347                         || (end($this->namespace) === SIMPLEPIE_NAMESPACE_ATOM_10 && in_array(end($this->element), array('rights', 'subtitle', 'summary', 'info', 'title', 'content')) && isset($attribs['']['type']) && $attribs['']['type'] === 'xhtml')
348                         || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_20 && in_array(end($this->element), array('title')))
349                         || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_090 && in_array(end($this->element), array('title')))
350                         || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_10 && in_array(end($this->element), array('title'))))
351                         {
352                                 $this->current_xhtml_construct = 0;
353                         }
354                 }
355         }
357         public function cdata($parser, $cdata)
358         {
359                 if ($this->current_xhtml_construct >= 0)
360                 {
361                         $this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding);
362                 }
363                 else
364                 {
365                         $this->data['data'] .= $cdata;
366                 }
367         }
369         public function tag_close($parser, $tag)
370         {
371                 if ($this->current_xhtml_construct >= 0)
372                 {
373                         $this->current_xhtml_construct--;
374                         if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML && !in_array(end($this->element), array('area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param')))
375                         {
376                                 $this->data['data'] .= '</' . end($this->element) . '>';
377                         }
378                 }
379                 if ($this->current_xhtml_construct === -1)
380                 {
381                         $this->data =& $this->datas[count($this->datas) - 1];
382                         array_pop($this->datas);
383                 }
385                 array_pop($this->element);
386                 array_pop($this->namespace);
387                 array_pop($this->xml_base);
388                 array_pop($this->xml_base_explicit);
389                 array_pop($this->xml_lang);
390         }
392         public function split_ns($string)
393         {
394                 static $cache = array();
395                 if (!isset($cache[$string]))
396                 {
397                         if ($pos = strpos($string, $this->separator))
398                         {
399                                 static $separator_length;
400                                 if (!$separator_length)
401                                 {
402                                         $separator_length = strlen($this->separator);
403                                 }
404                                 $namespace = substr($string, 0, $pos);
405                                 $local_name = substr($string, $pos + $separator_length);
406                                 if (strtolower($namespace) === SIMPLEPIE_NAMESPACE_ITUNES)
407                                 {
408                                         $namespace = SIMPLEPIE_NAMESPACE_ITUNES;
409                                 }
411                                 // Normalize the Media RSS namespaces
412                                 if ($namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG ||
413                                         $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2 ||
414                                         $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3 ||
415                                         $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4 ||
416                                         $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5 )
417                                 {
418                                         $namespace = SIMPLEPIE_NAMESPACE_MEDIARSS;
419                                 }
420                                 $cache[$string] = array($namespace, $local_name);
421                         }
422                         else
423                         {
424                                 $cache[$string] = array('', $string);
425                         }
426                 }
427                 return $cache[$string];
428         }
430         private function parse_hcard($data, $category = false) {
431                 $name = '';
432                 $link = '';
433                 // Check if h-card is set and pass that information on in the link.
434                 if (isset($data['type']) && in_array('h-card', $data['type'])) {
435                         if (isset($data['properties']['name'][0])) {
436                                 $name = $data['properties']['name'][0];
437                         }
438                         if (isset($data['properties']['url'][0])) {
439                                 $link = $data['properties']['url'][0];
440                                 if ($name === '') {
441                                         $name = $link;
442                                 }
443                                 else {
444                                         // can't have commas in categories.
445                                         $name = str_replace(',', '', $name);
446                                 }
447                                 $person_tag = $category ? '<span class="person-tag"></span>' : '';
448                                 return '<a class="h-card" href="'.$link.'">'.$person_tag.$name.'</a>';
449                         }
450                 }
451                 return isset($data['value']) ? $data['value'] : '';
452         }
454         private function parse_microformats(&$data, $url) {
455                 $feed_title = '';
456                 $feed_author = NULL;
457                 $author_cache = array();
458                 $items = array();
459                 $entries = array();
460                 $mf = Mf2\parse($data, $url);
461                 // First look for an h-feed.
462                 $h_feed = array();
463                 foreach ($mf['items'] as $mf_item) {
464                         if (in_array('h-feed', $mf_item['type'])) {
465                                 $h_feed = $mf_item;
466                                 break;
467                         }
468                         // Also look for an h-feed in the children of each top level item.
469                         if (!isset($mf_item['children'][0]['type'])) continue;
470                         if (in_array('h-feed', $mf_item['children'][0]['type'])) {
471                                 $h_feed = $mf_item['children'][0];
472                                 // In this case the parent of the h-feed may be an h-card, so use it as
473                                 // the feed_author.
474                                 if (in_array('h-card', $mf_item['type'])) $feed_author = $mf_item;
475                                 break;
476                         }
477                 }
478                 if (isset($h_feed['children'])) {
479                         $entries = $h_feed['children'];
480                         // Also set the feed title and store author from the h-feed if available.
481                         if (isset($mf['items'][0]['properties']['name'][0])) {
482                                 $feed_title = $mf['items'][0]['properties']['name'][0];
483                         }
484                         if (isset($mf['items'][0]['properties']['author'][0])) {
485                                 $feed_author = $mf['items'][0]['properties']['author'][0];
486                         }
487                 }
488                 else {
489                         $entries = $mf['items'];
490                 }
491                 for ($i = 0; $i < count($entries); $i++) {
492                         $entry = $entries[$i];
493                         if (in_array('h-entry', $entry['type'])) {
494                                 $item = array();
495                                 $title = '';
496                                 $description = '';
497                                 if (isset($entry['properties']['url'][0])) {
498                                         $link = $entry['properties']['url'][0];
499                                         if (isset($link['value'])) $link = $link['value'];
500                                         $item['link'] = array(array('data' => $link));
501                                 }
502                                 if (isset($entry['properties']['uid'][0])) {
503                                         $guid = $entry['properties']['uid'][0];
504                                         if (isset($guid['value'])) $guid = $guid['value'];
505                                         $item['guid'] = array(array('data' => $guid));
506                                 }
507                                 if (isset($entry['properties']['name'][0])) {
508                                         $title = $entry['properties']['name'][0];
509                                         if (isset($title['value'])) $title = $title['value'];
510                                         $item['title'] = array(array('data' => $title));
511                                 }
512                                 if (isset($entry['properties']['author'][0]) || isset($feed_author)) {
513                                         // author is a special case, it can be plain text or an h-card array.
514                                         // If it's plain text it can also be a url that should be followed to
515                                         // get the actual h-card.
516                                         $author = isset($entry['properties']['author'][0]) ?
517                                                 $entry['properties']['author'][0] : $feed_author;
518                                         if (!is_string($author)) {
519                                                 $author = $this->parse_hcard($author);
520                                         }
521                                         else if (strpos($author, 'http') === 0) {
522                                                 if (isset($author_cache[$author])) {
523                                                         $author = $author_cache[$author];
524                                                 }
525                                                 else {
526                                                         $mf = Mf2\fetch($author);
527                                                         foreach ($mf['items'] as $hcard) {
528                                                                 // Only interested in an h-card by itself in this case.
529                                                                 if (!in_array('h-card', $hcard['type'])) {
530                                                                         continue;
531                                                                 }
532                                                                 // It must have a url property matching what we fetched.
533                                                                 if (!isset($hcard['properties']['url']) ||
534                                                                                 !(in_array($author, $hcard['properties']['url']))) {
535                                                                         continue;
536                                                                 }
537                                                                 // Save parse_hcard the trouble of finding the correct url.
538                                                                 $hcard['properties']['url'][0] = $author;
539                                                                 // Cache this h-card for the next h-entry to check.
540                                                                 $author_cache[$author] = $this->parse_hcard($hcard);
541                                                                 $author = $author_cache[$author];
542                                                                 break;
543                                                         }
544                                                 }
545                                         }
546                                         $item['author'] = array(array('data' => $author));
547                                 }
548                                 if (isset($entry['properties']['photo'][0])) {
549                                         // If a photo is also in content, don't need to add it again here.
550                                         $content = '';
551                                         if (isset($entry['properties']['content'][0]['html'])) {
552                                                 $content = $entry['properties']['content'][0]['html'];
553                                         }
554                                         $photo_list = array();
555                                         for ($j = 0; $j < count($entry['properties']['photo']); $j++) {
556                                                 $photo = $entry['properties']['photo'][$j];
557                                                 if (strpos($content, $photo) === false) {
558                                                         $photo_list[] = $photo;
559                                                 }
560                                         }
561                                         // When there's more than one photo show the first and use a lightbox.
562                                         $count = count($photo_list);
563                                         if ($count > 1) {
564                                                 $description = '<p>';
565                                                 for ($j = 0; $j < $count; $j++) {
566                                                         $hidden = $j === 0 ? '' : 'class="hidden" ';
567                                                         $description .= '<a href="'.$photo_list[$j].'" '.$hidden.
568                                                                 'data-lightbox="image-set-'.$i.'">'.
569                                                                 '<img src="'.$photo_list[$j].'"></a>';
570                                                 }
571                                                 $description .= '<br><b>'.$count.' photos</b></p>';
572                                         }
573                                         else if ($count == 1) {
574                                                 $description = '<p><img src="'.$photo_list[0].'"></p>';
575                                         }
576                                 }
577                                 if (isset($entry['properties']['content'][0]['html'])) {
578                                         // e-content['value'] is the same as p-name when they are on the same
579                                         // element. Use this to replace title with a strip_tags version so
580                                         // that alt text from images is not included in the title.
581                                         if ($entry['properties']['content'][0]['value'] === $title) {
582                                                 $title = strip_tags($entry['properties']['content'][0]['html']);
583                                                 $item['title'] = array(array('data' => $title));
584                                         }
585                                         $description .= $entry['properties']['content'][0]['html'];
586                                         if (isset($entry['properties']['in-reply-to'][0]['value'])) {
587                                                 $in_reply_to = $entry['properties']['in-reply-to'][0]['value'];
588                                                 $description .= '<p><span class="in-reply-to"></span> '.
589                                                         '<a href="'.$in_reply_to.'">'.$in_reply_to.'</a><p>';
590                                         }
591                                         $item['description'] = array(array('data' => $description));
592                                 }
593                                 if (isset($entry['properties']['category'])) {
594                                         $category_csv = '';
595                                         // Categories can also contain h-cards.
596                                         foreach ($entry['properties']['category'] as $category) {
597                                                 if ($category_csv !== '') $category_csv .= ', ';
598                                                 if (is_string($category)) {
599                                                         // Can't have commas in categories.
600                                                         $category_csv .= str_replace(',', '', $category);
601                                                 }
602                                                 else {
603                                                         $category_csv .= $this->parse_hcard($category, true);
604                                                 }
605                                         }
606                                         $item['category'] = array(array('data' => $category_csv));
607                                 }
608                                 if (isset($entry['properties']['published'][0])) {
609                                         $timestamp = strtotime($entry['properties']['published'][0]);
610                                         $pub_date = date('F j Y g:ia', $timestamp).' GMT';
611                                         $item['pubDate'] = array(array('data' => $pub_date));
612                                 }
613                                 // The title and description are set to the empty string to represent
614                                 // a deleted item (which also makes it an invalid rss item).
615                                 if (isset($entry['properties']['deleted'][0])) {
616                                         $item['title'] = array(array('data' => ''));
617                                         $item['description'] = array(array('data' => ''));
618                                 }
619                                 $items[] = array('child' => array('' => $item));
620                         }
621                 }
622                 // Mimic RSS data format when storing microformats.
623                 $link = array(array('data' => $url));
624                 $image = '';
625                 if (!is_string($feed_author) &&
626                                 isset($feed_author['properties']['photo'][0])) {
627                         $image = array(array('child' => array('' => array('url' =>
628                                 array(array('data' => $feed_author['properties']['photo'][0]))))));
629                 }
630                 // Use the a name given for the h-feed, or get the title from the html.
631                 if ($feed_title !== '') {
632                         $feed_title = array(array('data' => htmlspecialchars($feed_title)));
633                 }
634                 else if ($position = strpos($data, '<title>')) {
635                         $start = $position < 200 ? 0 : $position - 200;
636                         $check = substr($data, $start, 400);
637                         $matches = array();
638                         if (preg_match('/<title>(.+)<\/title>/', $check, $matches)) {
639                                 $feed_title = array(array('data' => htmlspecialchars($matches[1])));
640                         }
641                 }
642                 $channel = array('channel' => array(array('child' => array('' =>
643                         array('link' => $link, 'image' => $image, 'title' => $feed_title,
644                               'item' => $items)))));
645                 $rss = array(array('attribs' => array('' => array('version' => '2.0')),
646                                    'child' => array('' => $channel)));
647                 $this->data = array('child' => array('' => array('rss' => $rss)));
648                 return true;
649         }
651         private function declare_html_entities() {
652                 // This is required because the RSS specification says that entity-encoded
653                 // html is allowed, but the xml specification says they must be declared.
654                 return '<!DOCTYPE html [ <!ENTITY nbsp "&#x00A0;"> <!ENTITY iexcl "&#x00A1;"> <!ENTITY cent "&#x00A2;"> <!ENTITY pound "&#x00A3;"> <!ENTITY curren "&#x00A4;"> <!ENTITY yen "&#x00A5;"> <!ENTITY brvbar "&#x00A6;"> <!ENTITY sect "&#x00A7;"> <!ENTITY uml "&#x00A8;"> <!ENTITY copy "&#x00A9;"> <!ENTITY ordf "&#x00AA;"> <!ENTITY laquo "&#x00AB;"> <!ENTITY not "&#x00AC;"> <!ENTITY shy "&#x00AD;"> <!ENTITY reg "&#x00AE;"> <!ENTITY macr "&#x00AF;"> <!ENTITY deg "&#x00B0;"> <!ENTITY plusmn "&#x00B1;"> <!ENTITY sup2 "&#x00B2;"> <!ENTITY sup3 "&#x00B3;"> <!ENTITY acute "&#x00B4;"> <!ENTITY micro "&#x00B5;"> <!ENTITY para "&#x00B6;"> <!ENTITY middot "&#x00B7;"> <!ENTITY cedil "&#x00B8;"> <!ENTITY sup1 "&#x00B9;"> <!ENTITY ordm "&#x00BA;"> <!ENTITY raquo "&#x00BB;"> <!ENTITY frac14 "&#x00BC;"> <!ENTITY frac12 "&#x00BD;"> <!ENTITY frac34 "&#x00BE;"> <!ENTITY iquest "&#x00BF;"> <!ENTITY Agrave "&#x00C0;"> <!ENTITY Aacute "&#x00C1;"> <!ENTITY Acirc "&#x00C2;"> <!ENTITY Atilde "&#x00C3;"> <!ENTITY Auml "&#x00C4;"> <!ENTITY Aring "&#x00C5;"> <!ENTITY AElig "&#x00C6;"> <!ENTITY Ccedil "&#x00C7;"> <!ENTITY Egrave "&#x00C8;"> <!ENTITY Eacute "&#x00C9;"> <!ENTITY Ecirc "&#x00CA;"> <!ENTITY Euml "&#x00CB;"> <!ENTITY Igrave "&#x00CC;"> <!ENTITY Iacute "&#x00CD;"> <!ENTITY Icirc "&#x00CE;"> <!ENTITY Iuml "&#x00CF;"> <!ENTITY ETH "&#x00D0;"> <!ENTITY Ntilde "&#x00D1;"> <!ENTITY Ograve "&#x00D2;"> <!ENTITY Oacute "&#x00D3;"> <!ENTITY Ocirc "&#x00D4;"> <!ENTITY Otilde "&#x00D5;"> <!ENTITY Ouml "&#x00D6;"> <!ENTITY times "&#x00D7;"> <!ENTITY Oslash "&#x00D8;"> <!ENTITY Ugrave "&#x00D9;"> <!ENTITY Uacute "&#x00DA;"> <!ENTITY Ucirc "&#x00DB;"> <!ENTITY Uuml "&#x00DC;"> <!ENTITY Yacute "&#x00DD;"> <!ENTITY THORN "&#x00DE;"> <!ENTITY szlig "&#x00DF;"> <!ENTITY agrave "&#x00E0;"> <!ENTITY aacute "&#x00E1;"> <!ENTITY acirc "&#x00E2;"> <!ENTITY atilde "&#x00E3;"> <!ENTITY auml "&#x00E4;"> <!ENTITY aring "&#x00E5;"> <!ENTITY aelig "&#x00E6;"> <!ENTITY ccedil "&#x00E7;"> <!ENTITY egrave "&#x00E8;"> <!ENTITY eacute "&#x00E9;"> <!ENTITY ecirc "&#x00EA;"> <!ENTITY euml "&#x00EB;"> <!ENTITY igrave "&#x00EC;"> <!ENTITY iacute "&#x00ED;"> <!ENTITY icirc "&#x00EE;"> <!ENTITY iuml "&#x00EF;"> <!ENTITY eth "&#x00F0;"> <!ENTITY ntilde "&#x00F1;"> <!ENTITY ograve "&#x00F2;"> <!ENTITY oacute "&#x00F3;"> <!ENTITY ocirc "&#x00F4;"> <!ENTITY otilde "&#x00F5;"> <!ENTITY ouml "&#x00F6;"> <!ENTITY divide "&#x00F7;"> <!ENTITY oslash "&#x00F8;"> <!ENTITY ugrave "&#x00F9;"> <!ENTITY uacute "&#x00FA;"> <!ENTITY ucirc "&#x00FB;"> <!ENTITY uuml "&#x00FC;"> <!ENTITY yacute "&#x00FD;"> <!ENTITY thorn "&#x00FE;"> <!ENTITY yuml "&#x00FF;"> <!ENTITY OElig "&#x0152;"> <!ENTITY oelig "&#x0153;"> <!ENTITY Scaron "&#x0160;"> <!ENTITY scaron "&#x0161;"> <!ENTITY Yuml "&#x0178;"> <!ENTITY fnof "&#x0192;"> <!ENTITY circ "&#x02C6;"> <!ENTITY tilde "&#x02DC;"> <!ENTITY Alpha "&#x0391;"> <!ENTITY Beta "&#x0392;"> <!ENTITY Gamma "&#x0393;"> <!ENTITY Epsilon "&#x0395;"> <!ENTITY Zeta "&#x0396;"> <!ENTITY Eta "&#x0397;"> <!ENTITY Theta "&#x0398;"> <!ENTITY Iota "&#x0399;"> <!ENTITY Kappa "&#x039A;"> <!ENTITY Lambda "&#x039B;"> <!ENTITY Mu "&#x039C;"> <!ENTITY Nu "&#x039D;"> <!ENTITY Xi "&#x039E;"> <!ENTITY Omicron "&#x039F;"> <!ENTITY Pi "&#x03A0;"> <!ENTITY Rho "&#x03A1;"> <!ENTITY Sigma "&#x03A3;"> <!ENTITY Tau "&#x03A4;"> <!ENTITY Upsilon "&#x03A5;"> <!ENTITY Phi "&#x03A6;"> <!ENTITY Chi "&#x03A7;"> <!ENTITY Psi "&#x03A8;"> <!ENTITY Omega "&#x03A9;"> <!ENTITY alpha "&#x03B1;"> <!ENTITY beta "&#x03B2;"> <!ENTITY gamma "&#x03B3;"> <!ENTITY delta "&#x03B4;"> <!ENTITY epsilon "&#x03B5;"> <!ENTITY zeta "&#x03B6;"> <!ENTITY eta "&#x03B7;"> <!ENTITY theta "&#x03B8;"> <!ENTITY iota "&#x03B9;"> <!ENTITY kappa "&#x03BA;"> <!ENTITY lambda "&#x03BB;"> <!ENTITY mu "&#x03BC;"> <!ENTITY nu "&#x03BD;"> <!ENTITY xi "&#x03BE;"> <!ENTITY omicron "&#x03BF;"> <!ENTITY pi "&#x03C0;"> <!ENTITY rho "&#x03C1;"> <!ENTITY sigmaf "&#x03C2;"> <!ENTITY sigma "&#x03C3;"> <!ENTITY tau "&#x03C4;"> <!ENTITY upsilon "&#x03C5;"> <!ENTITY phi "&#x03C6;"> <!ENTITY chi "&#x03C7;"> <!ENTITY psi "&#x03C8;"> <!ENTITY omega "&#x03C9;"> <!ENTITY thetasym "&#x03D1;"> <!ENTITY upsih "&#x03D2;"> <!ENTITY piv "&#x03D6;"> <!ENTITY ensp "&#x2002;"> <!ENTITY emsp "&#x2003;"> <!ENTITY thinsp "&#x2009;"> <!ENTITY zwnj "&#x200C;"> <!ENTITY zwj "&#x200D;"> <!ENTITY lrm "&#x200E;"> <!ENTITY rlm "&#x200F;"> <!ENTITY ndash "&#x2013;"> <!ENTITY mdash "&#x2014;"> <!ENTITY lsquo "&#x2018;"> <!ENTITY rsquo "&#x2019;"> <!ENTITY sbquo "&#x201A;"> <!ENTITY ldquo "&#x201C;"> <!ENTITY rdquo "&#x201D;"> <!ENTITY bdquo "&#x201E;"> <!ENTITY dagger "&#x2020;"> <!ENTITY Dagger "&#x2021;"> <!ENTITY bull "&#x2022;"> <!ENTITY hellip "&#x2026;"> <!ENTITY permil "&#x2030;"> <!ENTITY prime "&#x2032;"> <!ENTITY Prime "&#x2033;"> <!ENTITY lsaquo "&#x2039;"> <!ENTITY rsaquo "&#x203A;"> <!ENTITY oline "&#x203E;"> <!ENTITY frasl "&#x2044;"> <!ENTITY euro "&#x20AC;"> <!ENTITY image "&#x2111;"> <!ENTITY weierp "&#x2118;"> <!ENTITY real "&#x211C;"> <!ENTITY trade "&#x2122;"> <!ENTITY alefsym "&#x2135;"> <!ENTITY larr "&#x2190;"> <!ENTITY uarr "&#x2191;"> <!ENTITY rarr "&#x2192;"> <!ENTITY darr "&#x2193;"> <!ENTITY harr "&#x2194;"> <!ENTITY crarr "&#x21B5;"> <!ENTITY lArr "&#x21D0;"> <!ENTITY uArr "&#x21D1;"> <!ENTITY rArr "&#x21D2;"> <!ENTITY dArr "&#x21D3;"> <!ENTITY hArr "&#x21D4;"> <!ENTITY forall "&#x2200;"> <!ENTITY part "&#x2202;"> <!ENTITY exist "&#x2203;"> <!ENTITY empty "&#x2205;"> <!ENTITY nabla "&#x2207;"> <!ENTITY isin "&#x2208;"> <!ENTITY notin "&#x2209;"> <!ENTITY ni "&#x220B;"> <!ENTITY prod "&#x220F;"> <!ENTITY sum "&#x2211;"> <!ENTITY minus "&#x2212;"> <!ENTITY lowast "&#x2217;"> <!ENTITY radic "&#x221A;"> <!ENTITY prop "&#x221D;"> <!ENTITY infin "&#x221E;"> <!ENTITY ang "&#x2220;"> <!ENTITY and "&#x2227;"> <!ENTITY or "&#x2228;"> <!ENTITY cap "&#x2229;"> <!ENTITY cup "&#x222A;"> <!ENTITY int "&#x222B;"> <!ENTITY there4 "&#x2234;"> <!ENTITY sim "&#x223C;"> <!ENTITY cong "&#x2245;"> <!ENTITY asymp "&#x2248;"> <!ENTITY ne "&#x2260;"> <!ENTITY equiv "&#x2261;"> <!ENTITY le "&#x2264;"> <!ENTITY ge "&#x2265;"> <!ENTITY sub "&#x2282;"> <!ENTITY sup "&#x2283;"> <!ENTITY nsub "&#x2284;"> <!ENTITY sube "&#x2286;"> <!ENTITY supe "&#x2287;"> <!ENTITY oplus "&#x2295;"> <!ENTITY otimes "&#x2297;"> <!ENTITY perp "&#x22A5;"> <!ENTITY sdot "&#x22C5;"> <!ENTITY lceil "&#x2308;"> <!ENTITY rceil "&#x2309;"> <!ENTITY lfloor "&#x230A;"> <!ENTITY rfloor "&#x230B;"> <!ENTITY lang "&#x2329;"> <!ENTITY rang "&#x232A;"> <!ENTITY loz "&#x25CA;"> <!ENTITY spades "&#x2660;"> <!ENTITY clubs "&#x2663;"> <!ENTITY hearts "&#x2665;"> <!ENTITY diams "&#x2666;"> ]>';
655         }