Parser.php 31.9 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * SimplePie
 *
 * A PHP-Based RSS and Atom Feed Framework.
 * Takes the hard work out of managing a complete RSS/Atom solution.
 *
8
 * Copyright (c) 2004-2016, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 	* Redistributions of source code must retain the above copyright notice, this list of
 * 	  conditions and the following disclaimer.
 *
 * 	* Redistributions in binary form must reproduce the above copyright notice, this list
 * 	  of conditions and the following disclaimer in the documentation and/or other materials
 * 	  provided with the distribution.
 *
 * 	* Neither the name of the SimplePie Team nor the names of its contributors may be used
 * 	  to endorse or promote products derived from this software without specific prior
 * 	  written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @package SimplePie
36
 * @copyright 2004-2016 Ryan Parman, Geoffrey Sneddon, Ryan McCue
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 * @author Ryan Parman
 * @author Geoffrey Sneddon
 * @author Ryan McCue
 * @link http://simplepie.org/ SimplePie
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
 */

/**
 * Parses XML into something sane
 *
 *
 * This class can be overloaded with {@see SimplePie::set_parser_class()}
 *
 * @package SimplePie
 * @subpackage Parsing
 */
class SimplePie_Parser
{
	var $error_code;
	var $error_string;
	var $current_line;
	var $current_column;
	var $current_byte;
	var $separator = ' ';
	var $namespace = array('');
	var $element = array('');
	var $xml_base = array('');
	var $xml_base_explicit = array(false);
	var $xml_lang = array('');
	var $data = array();
	var $datas = array(array());
	var $current_xhtml_construct = -1;
	var $encoding;
	protected $registry;

	public function set_registry(SimplePie_Registry $registry)
	{
		$this->registry = $registry;
	}

77
	public function parse(&$data, $encoding, $url = '')
78
	{
79 80 81 82
		if (class_exists('DOMXpath') && function_exists('Mf2\parse')) {
			$doc = new DOMDocument();
			@$doc->loadHTML($data);
			$xpath = new DOMXpath($doc);
83 84
			// Check for h-feed, h-entry and h-cite, as both a feed with no entries
			// and a list of entries without an h-feed are valid.
85 86
			$query = '//*[contains(concat(" ", @class, " "), " h-feed ") or ' .
				'contains(concat(" ", @class, " "), " h-entry ") or ' .
87
				'contains(concat(" ", @class, " "), " h-cite ")]';
88 89
			$result = $xpath->query($query);
			if ($result->length !== 0) {
90
				return $this->parse_microformats($data, $url);
91 92 93
			}
		}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		// Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
		if (strtoupper($encoding) === 'US-ASCII')
		{
			$this->encoding = 'UTF-8';
		}
		else
		{
			$this->encoding = $encoding;
		}

		// Strip BOM:
		// UTF-32 Big Endian BOM
		if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
		{
			$data = substr($data, 4);
		}
		// UTF-32 Little Endian BOM
		elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
		{
			$data = substr($data, 4);
		}
		// UTF-16 Big Endian BOM
		elseif (substr($data, 0, 2) === "\xFE\xFF")
		{
			$data = substr($data, 2);
		}
		// UTF-16 Little Endian BOM
		elseif (substr($data, 0, 2) === "\xFF\xFE")
		{
			$data = substr($data, 2);
		}
		// UTF-8 BOM
		elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
		{
			$data = substr($data, 3);
		}

		if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
		{
			$declaration = $this->registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5)));
			if ($declaration->parse())
			{
				$data = substr($data, $pos + 2);
137
				$data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' ."\n". $this->declare_html_entities() . $data;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
			}
			else
			{
				$this->error_string = 'SimplePie bug! Please report this!';
				return false;
			}
		}

		$return = true;

		static $xml_is_sane = null;
		if ($xml_is_sane === null)
		{
			$parser_check = xml_parser_create();
			xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
			xml_parser_free($parser_check);
			$xml_is_sane = isset($values[0]['value']);
		}

		// Create the parser
		if ($xml_is_sane)
		{
			$xml = xml_parser_create_ns($this->encoding, $this->separator);
			xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
			xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
			xml_set_object($xml, $this);
			xml_set_character_data_handler($xml, 'cdata');
			xml_set_element_handler($xml, 'tag_open', 'tag_close');

			// Parse!
			if (!xml_parse($xml, $data, true))
			{
				$this->error_code = xml_get_error_code($xml);
				$this->error_string = xml_error_string($this->error_code);
				$return = false;
			}
			$this->current_line = xml_get_current_line_number($xml);
			$this->current_column = xml_get_current_column_number($xml);
			$this->current_byte = xml_get_current_byte_index($xml);
			xml_parser_free($xml);
			return $return;
		}
180 181 182 183 184

		libxml_clear_errors();
		$xml = new XMLReader();
		$xml->xml($data);
		while (@$xml->read())
185
		{
186
			switch ($xml->nodeType)
187 188
			{

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
				case constant('XMLReader::END_ELEMENT'):
					if ($xml->namespaceURI !== '')
					{
						$tagName = $xml->namespaceURI . $this->separator . $xml->localName;
					}
					else
					{
						$tagName = $xml->localName;
					}
					$this->tag_close(null, $tagName);
					break;
				case constant('XMLReader::ELEMENT'):
					$empty = $xml->isEmptyElement;
					if ($xml->namespaceURI !== '')
					{
						$tagName = $xml->namespaceURI . $this->separator . $xml->localName;
					}
					else
					{
						$tagName = $xml->localName;
					}
					$attributes = array();
					while ($xml->moveToNextAttribute())
					{
213 214
						if ($xml->namespaceURI !== '')
						{
215
							$attrName = $xml->namespaceURI . $this->separator . $xml->localName;
216 217 218
						}
						else
						{
219
							$attrName = $xml->localName;
220
						}
221 222 223 224 225
						$attributes[$attrName] = $xml->value;
					}
					$this->tag_open(null, $tagName, $attributes);
					if ($empty)
					{
226
						$this->tag_close(null, $tagName);
227 228 229
					}
					break;
				case constant('XMLReader::TEXT'):
230

231 232 233
				case constant('XMLReader::CDATA'):
					$this->cdata(null, $xml->value);
					break;
234 235
			}
		}
236 237 238 239 240 241 242 243 244 245
		if ($error = libxml_get_last_error())
		{
			$this->error_code = $error->code;
			$this->error_string = $error->message;
			$this->current_line = $error->line;
			$this->current_column = $error->column;
			return false;
		}

		return true;
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	}

	public function get_error_code()
	{
		return $this->error_code;
	}

	public function get_error_string()
	{
		return $this->error_string;
	}

	public function get_current_line()
	{
		return $this->current_line;
	}

	public function get_current_column()
	{
		return $this->current_column;
	}

	public function get_current_byte()
	{
		return $this->current_byte;
	}

	public function get_data()
	{
		return $this->data;
	}

	public function tag_open($parser, $tag, $attributes)
	{
		list($this->namespace[], $this->element[]) = $this->split_ns($tag);

		$attribs = array();
		foreach ($attributes as $name => $value)
		{
			list($attrib_namespace, $attribute) = $this->split_ns($name);
			$attribs[$attrib_namespace][$attribute] = $value;
		}

		if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['base']))
		{
291 292 293 294 295 296
			$base = $this->registry->call('Misc', 'absolutize_url', array($attribs[SIMPLEPIE_NAMESPACE_XML]['base'], end($this->xml_base)));
			if ($base !== false)
			{
				$this->xml_base[] = $base;
				$this->xml_base_explicit[] = true;
			}
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
		}
		else
		{
			$this->xml_base[] = end($this->xml_base);
			$this->xml_base_explicit[] = end($this->xml_base_explicit);
		}

		if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['lang']))
		{
			$this->xml_lang[] = $attribs[SIMPLEPIE_NAMESPACE_XML]['lang'];
		}
		else
		{
			$this->xml_lang[] = end($this->xml_lang);
		}

		if ($this->current_xhtml_construct >= 0)
		{
			$this->current_xhtml_construct++;
			if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML)
			{
				$this->data['data'] .= '<' . end($this->element);
				if (isset($attribs['']))
				{
					foreach ($attribs[''] as $name => $value)
					{
						$this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"';
					}
				}
				$this->data['data'] .= '>';
			}
		}
		else
		{
			$this->datas[] =& $this->data;
			$this->data =& $this->data['child'][end($this->namespace)][end($this->element)][];
			$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));
			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')
			|| (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')
			|| (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_20 && in_array(end($this->element), array('title')))
			|| (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_090 && in_array(end($this->element), array('title')))
			|| (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_10 && in_array(end($this->element), array('title'))))
			{
				$this->current_xhtml_construct = 0;
			}
		}
	}

	public function cdata($parser, $cdata)
	{
		if ($this->current_xhtml_construct >= 0)
		{
			$this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding);
		}
		else
		{
			$this->data['data'] .= $cdata;
		}
	}

	public function tag_close($parser, $tag)
	{
		if ($this->current_xhtml_construct >= 0)
		{
			$this->current_xhtml_construct--;
			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')))
			{
				$this->data['data'] .= '</' . end($this->element) . '>';
			}
		}
		if ($this->current_xhtml_construct === -1)
		{
			$this->data =& $this->datas[count($this->datas) - 1];
			array_pop($this->datas);
		}

		array_pop($this->element);
		array_pop($this->namespace);
		array_pop($this->xml_base);
		array_pop($this->xml_base_explicit);
		array_pop($this->xml_lang);
	}

	public function split_ns($string)
	{
		static $cache = array();
		if (!isset($cache[$string]))
		{
			if ($pos = strpos($string, $this->separator))
			{
				static $separator_length;
				if (!$separator_length)
				{
					$separator_length = strlen($this->separator);
				}
				$namespace = substr($string, 0, $pos);
				$local_name = substr($string, $pos + $separator_length);
				if (strtolower($namespace) === SIMPLEPIE_NAMESPACE_ITUNES)
				{
					$namespace = SIMPLEPIE_NAMESPACE_ITUNES;
				}

				// Normalize the Media RSS namespaces
				if ($namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG ||
					$namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2 ||
					$namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3 ||
					$namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4 ||
					$namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5 )
				{
					$namespace = SIMPLEPIE_NAMESPACE_MEDIARSS;
				}
				$cache[$string] = array($namespace, $local_name);
			}
			else
			{
				$cache[$string] = array('', $string);
			}
		}
		return $cache[$string];
	}
417

418
	private function parse_hcard($data, $category = false) {
419 420 421
		$name = '';
		$link = '';
		// Check if h-card is set and pass that information on in the link.
Malcolm Blaney's avatar
Malcolm Blaney committed
422
		if (isset($data['type']) && in_array('h-card', $data['type'])) {
423 424 425 426 427
			if (isset($data['properties']['name'][0])) {
				$name = $data['properties']['name'][0];
			}
			if (isset($data['properties']['url'][0])) {
				$link = $data['properties']['url'][0];
428 429 430 431 432 433 434
				if ($name === '') {
					$name = $link;
				}
				else {
					// can't have commas in categories.
					$name = str_replace(',', '', $name);
				}
435
				$person_tag = $category ? '<span class="person-tag"></span>' : '';
436 437
				return '<a class="h-card" href="' . $link . '">' .
					$person_tag . $name . '</a>';
438 439 440 441 442
			}
		}
		return isset($data['value']) ? $data['value'] : '';
	}

443
	private function parse_microformats(&$data, $url) {
444
		$feed_title = '';
445
		$feed_author = NULL;
446
		$author_cache = array();
447 448
		$items = array();
		$entries = array();
449
		$mf = Mf2\parse($data, $url);
450 451
		$h_feed = array();
		foreach ($mf['items'] as $mf_item) {
452
			if (!isset($mf_item['type'])) continue;
453

454 455 456 457
			if (in_array('h-feed', $mf_item['type'])) {
				$h_feed = $mf_item;
				break;
			}
458 459 460 461
			if (in_array('h-cite', $mf_item['type'])) {
				$entries = $mf['items'];
				break;
			}
462 463
			if (in_array('h-entry', $mf_item['type'])) {
				if (count($mf['items']) === 1) {
464 465 466 467 468 469 470 471 472 473 474 475 476 477
					// If a single h-entry is found, it could either be a comments feed
					// or a single entry in a feed. Need to assume it's a single entry
					// because it could be a WebSub payload, so comment feeds must always
					// have a comment property to differentiate the two.
					if (isset($mf_item['properties']['comment'])) {
						// Set feed title and author from the h-entry for a comments feed.
						if (isset($mf_item['properties']['name'][0])) {
							$feed_title = $mf_item['properties']['name'][0];
						}
						if (isset($mf_item['properties']['author'][0])) {
							$feed_author = $mf_item['properties']['author'][0];
						}
						$entries = $mf_item['properties']['comment'];
						break;
478
					}
479
				}
480 481
				$entries = $mf['items'];
				break;
482
			}
483 484
			// If not one of the simpler formats above, look for h-feed, h-entry or
			// h-cite in the children of each top level item.
485 486 487 488 489 490 491 492
			if (isset($mf_item['children'][0]['type'])) {
				if (in_array('h-feed', $mf_item['children'][0]['type'])) {
					$h_feed = $mf_item['children'][0];
					// The parent of this h-feed may be an h-card, so use it as the
					// feed author.
					if (in_array('h-card', $mf_item['type'])) $feed_author = $mf_item;
					break;
				}
493 494
				if (in_array('h-entry', $mf_item['children'][0]['type']) ||
				    in_array('h-cite', $mf_item['children'][0]['type'])) {
495 496 497 498
					$entries = $mf_item['children'];
					// The parent of this h-entry may also be an h-card.
					if (in_array('h-card', $mf_item['type'])) $feed_author = $mf_item;
					break;
499
				}
500
			}
501
		}
502 503 504 505 506 507 508
		// Also set the feed title and store author from the h-feed if available.
		if ($feed_title === '' && isset($h_feed['properties']['name'][0])) {
			$feed_title = $h_feed['properties']['name'][0];
		}
		if (!isset($feed_author) && isset($h_feed['properties']['author'][0])) {
			$feed_author = $h_feed['properties']['author'][0];
		}
509 510 511
		if (isset($h_feed['children'])) {
			$entries = $h_feed['children'];
		}
512
		foreach ($entries as $entry) {
513 514
			if (in_array('h-entry', $entry['type']) ||
			    in_array('h-cite', $entry['type'])) {
515
				$item = array();
516
				$media = array();
517
				$title = '';
518
				$description = '';
519 520
				if (isset($entry['properties']['url'][0])) {
					$link = $entry['properties']['url'][0];
521
					if (isset($link['value'])) $link = $link['value'];
522
					$item['link'] = array(array('data' => $link));
523
				}
524 525
				if (isset($entry['properties']['uid'][0])) {
					$guid = $entry['properties']['uid'][0];
526
					if (isset($guid['value'])) $guid = $guid['value'];
527 528
					$item['guid'] = array(array('data' => $guid));
				}
529 530
				if (isset($entry['properties']['name'][0])) {
					$title = $entry['properties']['name'][0];
531
					if (isset($title['value'])) $title = $title['value'];
532 533
					$item['title'] = array(array('data' => $title));
				}
534
				if (isset($entry['properties']['author'][0]) || isset($feed_author)) {
535
					// author is a special case, it can be plain text or an h-card array.
536 537
					// If it's plain text it can also be a url that should be followed to
					// get the actual h-card.
538 539
					$author = isset($entry['properties']['author'][0]) ?
						$entry['properties']['author'][0] : $feed_author;
540
					if (!is_string($author)) {
541 542
						$author = $this->parse_hcard($author);
					}
543
					else if (strpos($author, 'http') === 0) {
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
						if (isset($author_cache[$author])) {
							$author = $author_cache[$author];
						}
						else {
							$mf = Mf2\fetch($author);
							foreach ($mf['items'] as $hcard) {
								// Only interested in an h-card by itself in this case.
								if (!in_array('h-card', $hcard['type'])) {
									continue;
								}
								// It must have a url property matching what we fetched.
								if (!isset($hcard['properties']['url']) ||
										!(in_array($author, $hcard['properties']['url']))) {
									continue;
								}
								// Save parse_hcard the trouble of finding the correct url.
								$hcard['properties']['url'][0] = $author;
								// Cache this h-card for the next h-entry to check.
562
								$author_cache[$author] = $this->parse_hcard($hcard);
563 564
								$author = $author_cache[$author];
								break;
565 566 567
							}
						}
					}
568
					$item['author'] = array(array('data' => $author));
569
				}
570 571 572
				$use_content = true;
				if (isset($entry['properties']['summary'][0])) {
					// Use summary as content if there are any unrecognized properties.
573 574 575 576
					$known_properties =
						array('like-of', 'repost-of', 'in-reply-to', 'url', 'author',
						      'name', 'uid', 'category', 'published', 'summary', 'content',
						      'photo', 'comment', 'like', 'repost');
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
					foreach ($entry['properties'] as $name => $property) {
						if (!in_array($name, $known_properties)) {
							$use_content = false;
							break;
						}
					}
					if (!$use_content) {
						if (isset($entry['properties']['summary'][0]['html'])) {
							$description .= $entry['properties']['summary'][0]['html'];
						}
						else if (is_string($entry['properties']['summary'][0])) {
							$description .= $entry['properties']['summary'][0];
						}
					}
				}
				if ($use_content && isset($entry['properties']['content'][0]['html'])) {
					// e-content['value'] is the same as p-name when they are on the same
					// element. Use this to replace title with a strip_tags version so
					// that alt text from images is not included in the title.
					if ($entry['properties']['content'][0]['value'] === $title) {
						$title = strip_tags($entry['properties']['content'][0]['html']);
						$item['title'] = array(array('data' => $title));
599
					}
600 601
					$description .= $entry['properties']['content'][0]['html'];
				}
602
				// Images, audio and video are all stored using Media RSS.
603
				if (isset($entry['properties']['photo'][0])) {
604
					$photo_list = array();
605
					foreach ($entry['properties']['photo'] as $photo) {
606
						if (!empty($photo) && strpos($description, $photo) === false) {
607 608 609
							$photo_list[] = $photo;
						}
					}
610
					if (count($photo_list) === 0) {
611 612 613 614
						// All photos were found to be duplicated in description, so assume
						// this is a photo post and display the lightbox. Add a wrapper
						// around the existing description so those photos can be hidden.
						$description = '<div class="photo-hidden">' . $description.'</div>';
615 616
						$photo_list = $entry['properties']['photo'];
					}
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
					foreach ($photo_list as $photo) {
						// Find the type of image.
						if (preg_match('/\.([a-z]+)$/', strtolower($photo), $match)) {
							$type = '';
							if (in_array($match[1], array('gif', 'jpeg', 'png'))) {
								$type = 'image/' . $match[1];
							}
							else if ($match[1] === 'jpg') {
								$type = 'image/jpeg';
							}
							else if ($match[1] === 'svg') {
								$type = 'image/svg+xml';
							}
							else {
								continue;
							}
							// The format used here is for Media RSS below.
							$media[] = array('attribs' => array('' =>
								array('url' => $photo, 'type' => $type)));
						}
					}
				}
				if (isset($entry['properties']['audio'][0])) {
					foreach ($entry['properties']['audio'] as $audio) {
						if (preg_match('/\.([a-z]+)$/', strtolower($audio), $match)) {
							$type = '';
							$audio_types = array('aac', 'mpeg', 'ogg', 'wav', 'webm');
							if (in_array($match[1], $audio_types)) {
								$type = 'audio/' . $match[1];
							}
							else if ($match[1] === 'mp3') {
								$type = 'audio/mpeg';
							}
							else if ($match[1] === 'oga') {
								$type = 'audio/ogg';
							}
							else {
								continue;
							}
							$media[] = array('attribs' => array('' =>
								array('url' => $audio, 'type' => $type)));
658 659
						}
					}
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
				}
				if (isset($entry['properties']['video'][0])) {
					foreach ($entry['properties']['video'] as $video) {
						if (preg_match('/\.([a-z]+)$/', strtolower($video), $match)) {
							$type = '';
							if (in_array($match[1], array('mp4', 'webm'))) {
								$type = 'video/' . $match[1];
							}
							else if ($match[1] === 'ogv') {
								$type = 'video/ogg';
							}
							else {
								continue;
							}
							$media[] = array('attribs' => array('' =>
								array('url' => $video, 'type' => $type)));
						}
677 678
					}
				}
679 680 681 682 683 684 685 686 687
				if (isset($entry['properties']['like-of'][0])) {
					$like_of = '';
					if (is_string($entry['properties']['like-of'][0])) {
						$like_of = $entry['properties']['like-of'][0];
					}
					else if (isset($entry['properties']['like-of'][0]['value'])) {
						$like_of = $entry['properties']['like-of'][0]['value'];
					}
					if ($like_of !== '') {
688 689
						$description .= '<p><span class="like-of"></span> ' .
							'<a href="' . $like_of . '">' . $like_of . '</a><p>';
690
					}
691
				}
692 693 694 695 696 697 698
				if (isset($entry['properties']['repost-of'][0])) {
					$repost_of = '';
					if (is_string($entry['properties']['repost-of'][0])) {
						$repost_of = $entry['properties']['repost-of'][0];
					}
					else if (isset($entry['properties']['repost-of'][0]['value'])) {
						$repost_of = $entry['properties']['repost-of'][0]['value'];
699
					}
700
					if ($repost_of !== '') {
701 702
						$description .= '<p><span class="repost-of"></span> ' .
							'<a href="' . $repost_of . '">' . $repost_of . '</a><p>';
703
					}
704 705 706 707 708 709 710 711
				}
				if (isset($entry['properties']['in-reply-to'][0])) {
					$in_reply_to = '';
					if (is_string($entry['properties']['in-reply-to'][0])) {
						$in_reply_to = $entry['properties']['in-reply-to'][0];
					}
					else if (isset($entry['properties']['in-reply-to'][0]['value'])) {
						$in_reply_to = $entry['properties']['in-reply-to'][0]['value'];
712
					}
713
					if ($in_reply_to !== '') {
714 715
						$description .= '<p><span class="in-reply-to"></span> ' .
							'<a href="' . $in_reply_to . '">' . $in_reply_to . '</a><p>';
716 717 718
					}
				}
				if ($description !== '') {
719
					$item['description'] = array(array('data' => $description));
720
				}
721
				if (isset($entry['properties']['category'])) {
722
					$category_list = array();
723
					// Categories can also contain h-cards.
724
					foreach ($entry['properties']['category'] as $category) {
725
						if (is_string($category)) {
726
							$category_list[] = array('data' => $category);
727 728
						}
						else {
729 730
							$category_list[] =
								array('data' => $this->parse_hcard($category, true));
731
						}
732
					}
733
					$item['category'] = array($category_list);
734
				}
735 736
				if (isset($entry['properties']['published'][0])) {
					$timestamp = strtotime($entry['properties']['published'][0]);
737
					$pub_date = date('F j Y g:ia', $timestamp) . ' GMT';
738
					$item['pubDate'] = array(array('data' => $pub_date));
739
				}
740 741 742 743 744 745
				// The title and description are set to the empty string to represent
				// a deleted item (which also makes it an invalid rss item).
				if (isset($entry['properties']['deleted'][0])) {
					$item['title'] = array(array('data' => ''));
					$item['description'] = array(array('data' => ''));
				}
746 747
				$items[] = array('child' => array('' => $item,
					SIMPLEPIE_NAMESPACE_MEDIARSS => array('content' => array($media))));
748 749 750 751
			}
		}
		// Mimic RSS data format when storing microformats.
		$link = array(array('data' => $url));
752
		$image = '';
753 754
		if (!is_string($feed_author) &&
				isset($feed_author['properties']['photo'][0])) {
755 756
			$image = array(array('child' => array('' => array('url' =>
				array(array('data' => $feed_author['properties']['photo'][0]))))));
757
		}
758
		// Use the name given for the h-feed, or get the title from the html.
759
		if ($feed_title !== '') {
760
			$feed_title = array(array('data' => $feed_title));
761 762
		}
		else if ($position = strpos($data, '<title>')) {
763 764 765 766
			$start = $position < 200 ? 0 : $position - 200;
			$check = substr($data, $start, 400);
			$matches = array();
			if (preg_match('/<title>(.+)<\/title>/', $check, $matches)) {
767
				$feed_title = array(array('data' => $matches[1]));
768 769 770
			}
		}
		$channel = array('channel' => array(array('child' => array('' =>
Malcolm Blaney's avatar
Malcolm Blaney committed
771 772
			array('link' => $link, 'image' => $image, 'title' => $feed_title,
			      'item' => $items)))));
773 774 775 776 777
		$rss = array(array('attribs' => array('' => array('version' => '2.0')),
		                   'child' => array('' => $channel)));
		$this->data = array('child' => array('' => array('rss' => $rss)));
		return true;
	}
778 779 780 781 782 783 784

	private function declare_html_entities() {
		// This is required because the RSS specification says that entity-encoded
		// html is allowed, but the xml specification says they must be declared.
		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;"> ]>';
	}
}