Parser.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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, Sam 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, Sam Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Sam Sneddon
  39. * @author Ryan McCue
  40. * @link http://simplepie.org/ SimplePie
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. */
  43. /**
  44. * Parses XML into something sane
  45. *
  46. *
  47. * This class can be overloaded with {@see SimplePie::set_parser_class()}
  48. *
  49. * @package SimplePie
  50. * @subpackage Parsing
  51. */
  52. class SimplePie_Parser
  53. {
  54. var $error_code;
  55. var $error_string;
  56. var $current_line;
  57. var $current_column;
  58. var $current_byte;
  59. var $separator = ' ';
  60. var $namespace = array('');
  61. var $element = array('');
  62. var $xml_base = array('');
  63. var $xml_base_explicit = array(false);
  64. var $xml_lang = array('');
  65. var $data = array();
  66. var $datas = array(array());
  67. var $current_xhtml_construct = -1;
  68. var $encoding;
  69. protected $registry;
  70. public function set_registry(SimplePie_Registry $registry)
  71. {
  72. $this->registry = $registry;
  73. }
  74. public function parse(&$data, $encoding, $url = '')
  75. {
  76. $xmlEncoding = '';
  77. if (!empty($encoding))
  78. {
  79. // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
  80. if (strtoupper($encoding) === 'US-ASCII')
  81. {
  82. $this->encoding = 'UTF-8';
  83. }
  84. else
  85. {
  86. $this->encoding = $encoding;
  87. }
  88. // Strip BOM:
  89. // UTF-32 Big Endian BOM
  90. if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
  91. {
  92. $data = substr($data, 4);
  93. }
  94. // UTF-32 Little Endian BOM
  95. elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
  96. {
  97. $data = substr($data, 4);
  98. }
  99. // UTF-16 Big Endian BOM
  100. elseif (substr($data, 0, 2) === "\xFE\xFF")
  101. {
  102. $data = substr($data, 2);
  103. }
  104. // UTF-16 Little Endian BOM
  105. elseif (substr($data, 0, 2) === "\xFF\xFE")
  106. {
  107. $data = substr($data, 2);
  108. }
  109. // UTF-8 BOM
  110. elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
  111. {
  112. $data = substr($data, 3);
  113. }
  114. if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
  115. {
  116. $declaration = $this->registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5)));
  117. if ($declaration->parse())
  118. {
  119. $xmlEncoding = strtoupper($declaration->encoding); //FreshRSS
  120. $data = substr($data, $pos + 2);
  121. $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . $data;
  122. }
  123. else
  124. {
  125. $this->error_string = 'SimplePie bug! Please report this!';
  126. return false;
  127. }
  128. }
  129. }
  130. if ($xmlEncoding === '' || $xmlEncoding === 'UTF-8') //FreshRSS: case of no explicit HTTP encoding, and lax UTF-8
  131. {
  132. try
  133. {
  134. $dom = new DOMDocument();
  135. $dom->recover = true;
  136. $dom->strictErrorChecking = false;
  137. @$dom->loadXML($data, LIBXML_NOERROR | LIBXML_NOWARNING);
  138. $this->encoding = $encoding = $dom->encoding = 'UTF-8';
  139. $data2 = $dom->saveXML();
  140. if (function_exists('mb_convert_encoding'))
  141. {
  142. $data2 = mb_convert_encoding($data2, 'UTF-8', 'UTF-8');
  143. }
  144. if (strlen($data2) > (strlen($data) / 2.0))
  145. {
  146. $data = $data2;
  147. }
  148. unset($data2);
  149. }
  150. catch (Exception $e)
  151. {
  152. }
  153. }
  154. $return = true;
  155. static $xml_is_sane = null;
  156. if ($xml_is_sane === null)
  157. {
  158. $parser_check = xml_parser_create();
  159. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  160. xml_parser_free($parser_check);
  161. $xml_is_sane = isset($values[0]['value']);
  162. }
  163. // Create the parser
  164. if ($xml_is_sane)
  165. {
  166. $xml = xml_parser_create_ns($this->encoding, $this->separator);
  167. xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
  168. xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
  169. xml_set_object($xml, $this);
  170. xml_set_character_data_handler($xml, 'cdata');
  171. xml_set_element_handler($xml, 'tag_open', 'tag_close');
  172. // Parse!
  173. $wrapper = @is_writable(sys_get_temp_dir()) ? 'php://temp' : 'php://memory';
  174. if (($stream = fopen($wrapper, 'r+')) &&
  175. fwrite($stream, $data) &&
  176. rewind($stream))
  177. {
  178. //Parse by chunks not to use too much memory
  179. do
  180. {
  181. $stream_data = fread($stream, 1048576);
  182. if (!xml_parse($xml, $stream_data === false ? '' : $stream_data, feof($stream)))
  183. {
  184. $this->error_code = xml_get_error_code($xml);
  185. $this->error_string = xml_error_string($this->error_code);
  186. $return = false;
  187. break;
  188. }
  189. } while (!feof($stream));
  190. fclose($stream);
  191. }
  192. else
  193. {
  194. $return = false;
  195. }
  196. $this->current_line = xml_get_current_line_number($xml);
  197. $this->current_column = xml_get_current_column_number($xml);
  198. $this->current_byte = xml_get_current_byte_index($xml);
  199. xml_parser_free($xml);
  200. return $return;
  201. }
  202. libxml_clear_errors();
  203. $xml = new XMLReader();
  204. $xml->xml($data);
  205. while (@$xml->read())
  206. {
  207. switch ($xml->nodeType)
  208. {
  209. case constant('XMLReader::END_ELEMENT'):
  210. if ($xml->namespaceURI !== '')
  211. {
  212. $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
  213. }
  214. else
  215. {
  216. $tagName = $xml->localName;
  217. }
  218. $this->tag_close(null, $tagName);
  219. break;
  220. case constant('XMLReader::ELEMENT'):
  221. $empty = $xml->isEmptyElement;
  222. if ($xml->namespaceURI !== '')
  223. {
  224. $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
  225. }
  226. else
  227. {
  228. $tagName = $xml->localName;
  229. }
  230. $attributes = array();
  231. while ($xml->moveToNextAttribute())
  232. {
  233. if ($xml->namespaceURI !== '')
  234. {
  235. $attrName = $xml->namespaceURI . $this->separator . $xml->localName;
  236. }
  237. else
  238. {
  239. $attrName = $xml->localName;
  240. }
  241. $attributes[$attrName] = $xml->value;
  242. }
  243. $this->tag_open(null, $tagName, $attributes);
  244. if ($empty)
  245. {
  246. $this->tag_close(null, $tagName);
  247. }
  248. break;
  249. case constant('XMLReader::TEXT'):
  250. case constant('XMLReader::CDATA'):
  251. $this->cdata(null, $xml->value);
  252. break;
  253. }
  254. }
  255. if ($error = libxml_get_last_error())
  256. {
  257. $this->error_code = $error->code;
  258. $this->error_string = $error->message;
  259. $this->current_line = $error->line;
  260. $this->current_column = $error->column;
  261. return false;
  262. }
  263. return true;
  264. }
  265. public function get_error_code()
  266. {
  267. return $this->error_code;
  268. }
  269. public function get_error_string()
  270. {
  271. return $this->error_string;
  272. }
  273. public function get_current_line()
  274. {
  275. return $this->current_line;
  276. }
  277. public function get_current_column()
  278. {
  279. return $this->current_column;
  280. }
  281. public function get_current_byte()
  282. {
  283. return $this->current_byte;
  284. }
  285. public function get_data()
  286. {
  287. return $this->data;
  288. }
  289. public function tag_open($parser, $tag, $attributes)
  290. {
  291. list($this->namespace[], $this->element[]) = $this->split_ns($tag);
  292. $attribs = array();
  293. foreach ($attributes as $name => $value)
  294. {
  295. list($attrib_namespace, $attribute) = $this->split_ns($name);
  296. $attribs[$attrib_namespace][$attribute] = $value;
  297. }
  298. if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['base']))
  299. {
  300. $base = $this->registry->call('Misc', 'absolutize_url', array($attribs[SIMPLEPIE_NAMESPACE_XML]['base'], end($this->xml_base)));
  301. if ($base !== false)
  302. {
  303. $this->xml_base[] = $base;
  304. $this->xml_base_explicit[] = true;
  305. }
  306. }
  307. else
  308. {
  309. $this->xml_base[] = end($this->xml_base);
  310. $this->xml_base_explicit[] = end($this->xml_base_explicit);
  311. }
  312. if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['lang']))
  313. {
  314. $this->xml_lang[] = $attribs[SIMPLEPIE_NAMESPACE_XML]['lang'];
  315. }
  316. else
  317. {
  318. $this->xml_lang[] = end($this->xml_lang);
  319. }
  320. if ($this->current_xhtml_construct >= 0)
  321. {
  322. $this->current_xhtml_construct++;
  323. if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML)
  324. {
  325. $this->data['data'] .= '<' . end($this->element);
  326. if (isset($attribs['']))
  327. {
  328. foreach ($attribs[''] as $name => $value)
  329. {
  330. $this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"';
  331. }
  332. }
  333. $this->data['data'] .= '>';
  334. }
  335. }
  336. else
  337. {
  338. $this->datas[] =& $this->data;
  339. $this->data =& $this->data['child'][end($this->namespace)][end($this->element)][];
  340. $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));
  341. 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')
  342. || (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')
  343. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_20 && in_array(end($this->element), array('title')))
  344. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_090 && in_array(end($this->element), array('title')))
  345. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_10 && in_array(end($this->element), array('title'))))
  346. {
  347. $this->current_xhtml_construct = 0;
  348. }
  349. }
  350. }
  351. public function cdata($parser, $cdata)
  352. {
  353. if ($this->current_xhtml_construct >= 0)
  354. {
  355. $this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding);
  356. }
  357. else
  358. {
  359. $this->data['data'] .= $cdata;
  360. }
  361. }
  362. public function tag_close($parser, $tag)
  363. {
  364. if ($this->current_xhtml_construct >= 0)
  365. {
  366. $this->current_xhtml_construct--;
  367. 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')))
  368. {
  369. $this->data['data'] .= '</' . end($this->element) . '>';
  370. }
  371. }
  372. if ($this->current_xhtml_construct === -1)
  373. {
  374. $this->data =& $this->datas[count($this->datas) - 1];
  375. array_pop($this->datas);
  376. }
  377. array_pop($this->element);
  378. array_pop($this->namespace);
  379. array_pop($this->xml_base);
  380. array_pop($this->xml_base_explicit);
  381. array_pop($this->xml_lang);
  382. }
  383. public function split_ns($string)
  384. {
  385. static $cache = array();
  386. if (!isset($cache[$string]))
  387. {
  388. if ($pos = strpos($string, $this->separator))
  389. {
  390. static $separator_length;
  391. if (!$separator_length)
  392. {
  393. $separator_length = strlen($this->separator);
  394. }
  395. $namespace = substr($string, 0, $pos);
  396. $local_name = substr($string, $pos + $separator_length);
  397. if (strtolower($namespace) === SIMPLEPIE_NAMESPACE_ITUNES)
  398. {
  399. $namespace = SIMPLEPIE_NAMESPACE_ITUNES;
  400. }
  401. // Normalize the Media RSS namespaces
  402. if ($namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG ||
  403. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2 ||
  404. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3 ||
  405. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4 ||
  406. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5 )
  407. {
  408. $namespace = SIMPLEPIE_NAMESPACE_MEDIARSS;
  409. }
  410. $cache[$string] = array($namespace, $local_name);
  411. }
  412. else
  413. {
  414. $cache[$string] = array('', $string);
  415. }
  416. }
  417. return $cache[$string];
  418. }
  419. private function parse_hcard($data, $category = false) {
  420. $name = '';
  421. $link = '';
  422. // Check if h-card is set and pass that information on in the link.
  423. if (isset($data['type']) && in_array('h-card', $data['type'])) {
  424. if (isset($data['properties']['name'][0])) {
  425. $name = $data['properties']['name'][0];
  426. }
  427. if (isset($data['properties']['url'][0])) {
  428. $link = $data['properties']['url'][0];
  429. if ($name === '') {
  430. $name = $link;
  431. }
  432. else {
  433. // can't have commas in categories.
  434. $name = str_replace(',', '', $name);
  435. }
  436. $person_tag = $category ? '<span class="person-tag"></span>' : '';
  437. return '<a class="h-card" href="'.$link.'">'.$person_tag.$name.'</a>';
  438. }
  439. }
  440. return isset($data['value']) ? $data['value'] : '';
  441. }
  442. private function parse_microformats(&$data, $url) {
  443. $feed_title = '';
  444. $feed_author = NULL;
  445. $author_cache = array();
  446. $items = array();
  447. $entries = array();
  448. $mf = Mf2\parse($data, $url);
  449. // First look for an h-feed.
  450. $h_feed = array();
  451. foreach ($mf['items'] as $mf_item) {
  452. if (in_array('h-feed', $mf_item['type'])) {
  453. $h_feed = $mf_item;
  454. break;
  455. }
  456. // Also look for h-feed or h-entry in the children of each top level item.
  457. if (!isset($mf_item['children'][0]['type'])) continue;
  458. if (in_array('h-feed', $mf_item['children'][0]['type'])) {
  459. $h_feed = $mf_item['children'][0];
  460. // In this case the parent of the h-feed may be an h-card, so use it as
  461. // the feed_author.
  462. if (in_array('h-card', $mf_item['type'])) $feed_author = $mf_item;
  463. break;
  464. }
  465. else if (in_array('h-entry', $mf_item['children'][0]['type'])) {
  466. $entries = $mf_item['children'];
  467. // In this case the parent of the h-entry list may be an h-card, so use
  468. // it as the feed_author.
  469. if (in_array('h-card', $mf_item['type'])) $feed_author = $mf_item;
  470. break;
  471. }
  472. }
  473. if (isset($h_feed['children'])) {
  474. $entries = $h_feed['children'];
  475. // Also set the feed title and store author from the h-feed if available.
  476. if (isset($mf['items'][0]['properties']['name'][0])) {
  477. $feed_title = $mf['items'][0]['properties']['name'][0];
  478. }
  479. if (isset($mf['items'][0]['properties']['author'][0])) {
  480. $feed_author = $mf['items'][0]['properties']['author'][0];
  481. }
  482. }
  483. else if (count($entries) === 0) {
  484. $entries = $mf['items'];
  485. }
  486. for ($i = 0; $i < count($entries); $i++) {
  487. $entry = $entries[$i];
  488. if (in_array('h-entry', $entry['type'])) {
  489. $item = array();
  490. $title = '';
  491. $description = '';
  492. if (isset($entry['properties']['url'][0])) {
  493. $link = $entry['properties']['url'][0];
  494. if (isset($link['value'])) $link = $link['value'];
  495. $item['link'] = array(array('data' => $link));
  496. }
  497. if (isset($entry['properties']['uid'][0])) {
  498. $guid = $entry['properties']['uid'][0];
  499. if (isset($guid['value'])) $guid = $guid['value'];
  500. $item['guid'] = array(array('data' => $guid));
  501. }
  502. if (isset($entry['properties']['name'][0])) {
  503. $title = $entry['properties']['name'][0];
  504. if (isset($title['value'])) $title = $title['value'];
  505. $item['title'] = array(array('data' => $title));
  506. }
  507. if (isset($entry['properties']['author'][0]) || isset($feed_author)) {
  508. // author is a special case, it can be plain text or an h-card array.
  509. // If it's plain text it can also be a url that should be followed to
  510. // get the actual h-card.
  511. $author = isset($entry['properties']['author'][0]) ?
  512. $entry['properties']['author'][0] : $feed_author;
  513. if (!is_string($author)) {
  514. $author = $this->parse_hcard($author);
  515. }
  516. else if (strpos($author, 'http') === 0) {
  517. if (isset($author_cache[$author])) {
  518. $author = $author_cache[$author];
  519. }
  520. else {
  521. $mf = Mf2\fetch($author);
  522. foreach ($mf['items'] as $hcard) {
  523. // Only interested in an h-card by itself in this case.
  524. if (!in_array('h-card', $hcard['type'])) {
  525. continue;
  526. }
  527. // It must have a url property matching what we fetched.
  528. if (!isset($hcard['properties']['url']) ||
  529. !(in_array($author, $hcard['properties']['url']))) {
  530. continue;
  531. }
  532. // Save parse_hcard the trouble of finding the correct url.
  533. $hcard['properties']['url'][0] = $author;
  534. // Cache this h-card for the next h-entry to check.
  535. $author_cache[$author] = $this->parse_hcard($hcard);
  536. $author = $author_cache[$author];
  537. break;
  538. }
  539. }
  540. }
  541. $item['author'] = array(array('data' => $author));
  542. }
  543. if (isset($entry['properties']['photo'][0])) {
  544. // If a photo is also in content, don't need to add it again here.
  545. $content = '';
  546. if (isset($entry['properties']['content'][0]['html'])) {
  547. $content = $entry['properties']['content'][0]['html'];
  548. }
  549. $photo_list = array();
  550. for ($j = 0; $j < count($entry['properties']['photo']); $j++) {
  551. $photo = $entry['properties']['photo'][$j];
  552. if (!empty($photo) && strpos($content, $photo) === false) {
  553. $photo_list[] = $photo;
  554. }
  555. }
  556. // When there's more than one photo show the first and use a lightbox.
  557. // Need a permanent, unique name for the image set, but don't have
  558. // anything unique except for the content itself, so use that.
  559. $count = count($photo_list);
  560. if ($count > 1) {
  561. $image_set_id = preg_replace('/[[:^alnum:]]/', '', $photo_list[0]);
  562. $description = '<p>';
  563. for ($j = 0; $j < $count; $j++) {
  564. $hidden = $j === 0 ? '' : 'class="hidden" ';
  565. $description .= '<a href="'.$photo_list[$j].'" '.$hidden.
  566. 'data-lightbox="image-set-'.$image_set_id.'">'.
  567. '<img src="'.$photo_list[$j].'"></a>';
  568. }
  569. $description .= '<br><b>'.$count.' photos</b></p>';
  570. }
  571. else if ($count == 1) {
  572. $description = '<p><img src="'.$photo_list[0].'"></p>';
  573. }
  574. }
  575. if (isset($entry['properties']['content'][0]['html'])) {
  576. // e-content['value'] is the same as p-name when they are on the same
  577. // element. Use this to replace title with a strip_tags version so
  578. // that alt text from images is not included in the title.
  579. if ($entry['properties']['content'][0]['value'] === $title) {
  580. $title = strip_tags($entry['properties']['content'][0]['html']);
  581. $item['title'] = array(array('data' => $title));
  582. }
  583. $description .= $entry['properties']['content'][0]['html'];
  584. if (isset($entry['properties']['in-reply-to'][0])) {
  585. $in_reply_to = '';
  586. if (is_string($entry['properties']['in-reply-to'][0])) {
  587. $in_reply_to = $entry['properties']['in-reply-to'][0];
  588. }
  589. else if (isset($entry['properties']['in-reply-to'][0]['value'])) {
  590. $in_reply_to = $entry['properties']['in-reply-to'][0]['value'];
  591. }
  592. if ($in_reply_to !== '') {
  593. $description .= '<p><span class="in-reply-to"></span> '.
  594. '<a href="'.$in_reply_to.'">'.$in_reply_to.'</a><p>';
  595. }
  596. }
  597. $item['description'] = array(array('data' => $description));
  598. }
  599. if (isset($entry['properties']['category'])) {
  600. $category_csv = '';
  601. // Categories can also contain h-cards.
  602. foreach ($entry['properties']['category'] as $category) {
  603. if ($category_csv !== '') $category_csv .= ', ';
  604. if (is_string($category)) {
  605. // Can't have commas in categories.
  606. $category_csv .= str_replace(',', '', $category);
  607. }
  608. else {
  609. $category_csv .= $this->parse_hcard($category, true);
  610. }
  611. }
  612. $item['category'] = array(array('data' => $category_csv));
  613. }
  614. if (isset($entry['properties']['published'][0])) {
  615. $timestamp = strtotime($entry['properties']['published'][0]);
  616. $pub_date = date('F j Y g:ia', $timestamp).' GMT';
  617. $item['pubDate'] = array(array('data' => $pub_date));
  618. }
  619. // The title and description are set to the empty string to represent
  620. // a deleted item (which also makes it an invalid rss item).
  621. if (isset($entry['properties']['deleted'][0])) {
  622. $item['title'] = array(array('data' => ''));
  623. $item['description'] = array(array('data' => ''));
  624. }
  625. $items[] = array('child' => array('' => $item));
  626. }
  627. }
  628. // Mimic RSS data format when storing microformats.
  629. $link = array(array('data' => $url));
  630. $image = '';
  631. if (!is_string($feed_author) &&
  632. isset($feed_author['properties']['photo'][0])) {
  633. $image = array(array('child' => array('' => array('url' =>
  634. array(array('data' => $feed_author['properties']['photo'][0]))))));
  635. }
  636. // Use the name given for the h-feed, or get the title from the html.
  637. if ($feed_title !== '') {
  638. $feed_title = array(array('data' => htmlspecialchars($feed_title)));
  639. }
  640. else if ($position = strpos($data, '<title>')) {
  641. $start = $position < 200 ? 0 : $position - 200;
  642. $check = substr($data, $start, 400);
  643. $matches = array();
  644. if (preg_match('/<title>(.+)<\/title>/', $check, $matches)) {
  645. $feed_title = array(array('data' => htmlspecialchars($matches[1])));
  646. }
  647. }
  648. $channel = array('channel' => array(array('child' => array('' =>
  649. array('link' => $link, 'image' => $image, 'title' => $feed_title,
  650. 'item' => $items)))));
  651. $rss = array(array('attribs' => array('' => array('version' => '2.0')),
  652. 'child' => array('' => $channel)));
  653. $this->data = array('child' => array('' => array('rss' => $rss)));
  654. return true;
  655. }
  656. private function declare_html_entities() {
  657. // This is required because the RSS specification says that entity-encoded
  658. // html is allowed, but the xml specification says they must be declared.
  659. 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;"> ]>';
  660. }
  661. }