PageRenderTime 134ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 2ms

/sites/all/libraries/simplepie/simplepie.compiled.php

https://bitbucket.org/micahw156/sites_blogrimage
PHP | 17768 lines | 13791 code | 806 blank | 3171 comment | 1093 complexity | b6088f03aac9d03ccc0cb912103496d8 MD5 | raw file
Possible License(s): GPL-2.0
  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. * Please note: This file is automatically generated by a build script. The
  9. * full original source is always available from http://simplepie.org/
  10. *
  11. * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification, are
  15. * permitted provided that the following conditions are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright notice, this list of
  18. * conditions and the following disclaimer.
  19. *
  20. * * Redistributions in binary form must reproduce the above copyright notice, this list
  21. * of conditions and the following disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  25. * to endorse or promote products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  30. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  31. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  35. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * @package SimplePie
  39. * @version 1.3.1
  40. * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  41. * @author Ryan Parman
  42. * @author Geoffrey Sneddon
  43. * @author Ryan McCue
  44. * @link http://simplepie.org/ SimplePie
  45. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  46. */
  47. /**
  48. * SimplePie Name
  49. */
  50. define('SIMPLEPIE_NAME', 'SimplePie');
  51. /**
  52. * SimplePie Version
  53. */
  54. define('SIMPLEPIE_VERSION', '1.3.1');
  55. /**
  56. * SimplePie Build
  57. * @todo Hardcode for release (there's no need to have to call SimplePie_Misc::get_build() only every load of simplepie.inc)
  58. */
  59. define('SIMPLEPIE_BUILD', '20121030175911');
  60. /**
  61. * SimplePie Website URL
  62. */
  63. define('SIMPLEPIE_URL', 'http://simplepie.org');
  64. /**
  65. * SimplePie Useragent
  66. * @see SimplePie::set_useragent()
  67. */
  68. define('SIMPLEPIE_USERAGENT', SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION . ' (Feed Parser; ' . SIMPLEPIE_URL . '; Allow like Gecko) Build/' . SIMPLEPIE_BUILD);
  69. /**
  70. * SimplePie Linkback
  71. */
  72. define('SIMPLEPIE_LINKBACK', '<a href="' . SIMPLEPIE_URL . '" title="' . SIMPLEPIE_NAME . ' ' . SIMPLEPIE_VERSION . '">' . SIMPLEPIE_NAME . '</a>');
  73. /**
  74. * No Autodiscovery
  75. * @see SimplePie::set_autodiscovery_level()
  76. */
  77. define('SIMPLEPIE_LOCATOR_NONE', 0);
  78. /**
  79. * Feed Link Element Autodiscovery
  80. * @see SimplePie::set_autodiscovery_level()
  81. */
  82. define('SIMPLEPIE_LOCATOR_AUTODISCOVERY', 1);
  83. /**
  84. * Local Feed Extension Autodiscovery
  85. * @see SimplePie::set_autodiscovery_level()
  86. */
  87. define('SIMPLEPIE_LOCATOR_LOCAL_EXTENSION', 2);
  88. /**
  89. * Local Feed Body Autodiscovery
  90. * @see SimplePie::set_autodiscovery_level()
  91. */
  92. define('SIMPLEPIE_LOCATOR_LOCAL_BODY', 4);
  93. /**
  94. * Remote Feed Extension Autodiscovery
  95. * @see SimplePie::set_autodiscovery_level()
  96. */
  97. define('SIMPLEPIE_LOCATOR_REMOTE_EXTENSION', 8);
  98. /**
  99. * Remote Feed Body Autodiscovery
  100. * @see SimplePie::set_autodiscovery_level()
  101. */
  102. define('SIMPLEPIE_LOCATOR_REMOTE_BODY', 16);
  103. /**
  104. * All Feed Autodiscovery
  105. * @see SimplePie::set_autodiscovery_level()
  106. */
  107. define('SIMPLEPIE_LOCATOR_ALL', 31);
  108. /**
  109. * No known feed type
  110. */
  111. define('SIMPLEPIE_TYPE_NONE', 0);
  112. /**
  113. * RSS 0.90
  114. */
  115. define('SIMPLEPIE_TYPE_RSS_090', 1);
  116. /**
  117. * RSS 0.91 (Netscape)
  118. */
  119. define('SIMPLEPIE_TYPE_RSS_091_NETSCAPE', 2);
  120. /**
  121. * RSS 0.91 (Userland)
  122. */
  123. define('SIMPLEPIE_TYPE_RSS_091_USERLAND', 4);
  124. /**
  125. * RSS 0.91 (both Netscape and Userland)
  126. */
  127. define('SIMPLEPIE_TYPE_RSS_091', 6);
  128. /**
  129. * RSS 0.92
  130. */
  131. define('SIMPLEPIE_TYPE_RSS_092', 8);
  132. /**
  133. * RSS 0.93
  134. */
  135. define('SIMPLEPIE_TYPE_RSS_093', 16);
  136. /**
  137. * RSS 0.94
  138. */
  139. define('SIMPLEPIE_TYPE_RSS_094', 32);
  140. /**
  141. * RSS 1.0
  142. */
  143. define('SIMPLEPIE_TYPE_RSS_10', 64);
  144. /**
  145. * RSS 2.0
  146. */
  147. define('SIMPLEPIE_TYPE_RSS_20', 128);
  148. /**
  149. * RDF-based RSS
  150. */
  151. define('SIMPLEPIE_TYPE_RSS_RDF', 65);
  152. /**
  153. * Non-RDF-based RSS (truly intended as syndication format)
  154. */
  155. define('SIMPLEPIE_TYPE_RSS_SYNDICATION', 190);
  156. /**
  157. * All RSS
  158. */
  159. define('SIMPLEPIE_TYPE_RSS_ALL', 255);
  160. /**
  161. * Atom 0.3
  162. */
  163. define('SIMPLEPIE_TYPE_ATOM_03', 256);
  164. /**
  165. * Atom 1.0
  166. */
  167. define('SIMPLEPIE_TYPE_ATOM_10', 512);
  168. /**
  169. * All Atom
  170. */
  171. define('SIMPLEPIE_TYPE_ATOM_ALL', 768);
  172. /**
  173. * All feed types
  174. */
  175. define('SIMPLEPIE_TYPE_ALL', 1023);
  176. /**
  177. * No construct
  178. */
  179. define('SIMPLEPIE_CONSTRUCT_NONE', 0);
  180. /**
  181. * Text construct
  182. */
  183. define('SIMPLEPIE_CONSTRUCT_TEXT', 1);
  184. /**
  185. * HTML construct
  186. */
  187. define('SIMPLEPIE_CONSTRUCT_HTML', 2);
  188. /**
  189. * XHTML construct
  190. */
  191. define('SIMPLEPIE_CONSTRUCT_XHTML', 4);
  192. /**
  193. * base64-encoded construct
  194. */
  195. define('SIMPLEPIE_CONSTRUCT_BASE64', 8);
  196. /**
  197. * IRI construct
  198. */
  199. define('SIMPLEPIE_CONSTRUCT_IRI', 16);
  200. /**
  201. * A construct that might be HTML
  202. */
  203. define('SIMPLEPIE_CONSTRUCT_MAYBE_HTML', 32);
  204. /**
  205. * All constructs
  206. */
  207. define('SIMPLEPIE_CONSTRUCT_ALL', 63);
  208. /**
  209. * Don't change case
  210. */
  211. define('SIMPLEPIE_SAME_CASE', 1);
  212. /**
  213. * Change to lowercase
  214. */
  215. define('SIMPLEPIE_LOWERCASE', 2);
  216. /**
  217. * Change to uppercase
  218. */
  219. define('SIMPLEPIE_UPPERCASE', 4);
  220. /**
  221. * PCRE for HTML attributes
  222. */
  223. define('SIMPLEPIE_PCRE_HTML_ATTRIBUTE', '((?:[\x09\x0A\x0B\x0C\x0D\x20]+[^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"(?:[^"]*)"|\'(?:[^\']*)\'|(?:[^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?)*)[\x09\x0A\x0B\x0C\x0D\x20]*');
  224. /**
  225. * PCRE for XML attributes
  226. */
  227. define('SIMPLEPIE_PCRE_XML_ATTRIBUTE', '((?:\s+(?:(?:[^\s:]+:)?[^\s:]+)\s*=\s*(?:"(?:[^"]*)"|\'(?:[^\']*)\'))*)\s*');
  228. /**
  229. * XML Namespace
  230. */
  231. define('SIMPLEPIE_NAMESPACE_XML', 'http://www.w3.org/XML/1998/namespace');
  232. /**
  233. * Atom 1.0 Namespace
  234. */
  235. define('SIMPLEPIE_NAMESPACE_ATOM_10', 'http://www.w3.org/2005/Atom');
  236. /**
  237. * Atom 0.3 Namespace
  238. */
  239. define('SIMPLEPIE_NAMESPACE_ATOM_03', 'http://purl.org/atom/ns#');
  240. /**
  241. * RDF Namespace
  242. */
  243. define('SIMPLEPIE_NAMESPACE_RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  244. /**
  245. * RSS 0.90 Namespace
  246. */
  247. define('SIMPLEPIE_NAMESPACE_RSS_090', 'http://my.netscape.com/rdf/simple/0.9/');
  248. /**
  249. * RSS 1.0 Namespace
  250. */
  251. define('SIMPLEPIE_NAMESPACE_RSS_10', 'http://purl.org/rss/1.0/');
  252. /**
  253. * RSS 1.0 Content Module Namespace
  254. */
  255. define('SIMPLEPIE_NAMESPACE_RSS_10_MODULES_CONTENT', 'http://purl.org/rss/1.0/modules/content/');
  256. /**
  257. * RSS 2.0 Namespace
  258. * (Stupid, I know, but I'm certain it will confuse people less with support.)
  259. */
  260. define('SIMPLEPIE_NAMESPACE_RSS_20', '');
  261. /**
  262. * DC 1.0 Namespace
  263. */
  264. define('SIMPLEPIE_NAMESPACE_DC_10', 'http://purl.org/dc/elements/1.0/');
  265. /**
  266. * DC 1.1 Namespace
  267. */
  268. define('SIMPLEPIE_NAMESPACE_DC_11', 'http://purl.org/dc/elements/1.1/');
  269. /**
  270. * W3C Basic Geo (WGS84 lat/long) Vocabulary Namespace
  271. */
  272. define('SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
  273. /**
  274. * GeoRSS Namespace
  275. */
  276. define('SIMPLEPIE_NAMESPACE_GEORSS', 'http://www.georss.org/georss');
  277. /**
  278. * Media RSS Namespace
  279. */
  280. define('SIMPLEPIE_NAMESPACE_MEDIARSS', 'http://search.yahoo.com/mrss/');
  281. /**
  282. * Wrong Media RSS Namespace. Caused by a long-standing typo in the spec.
  283. */
  284. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG', 'http://search.yahoo.com/mrss');
  285. /**
  286. * Wrong Media RSS Namespace #2. New namespace introduced in Media RSS 1.5.
  287. */
  288. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2', 'http://video.search.yahoo.com/mrss');
  289. /**
  290. * Wrong Media RSS Namespace #3. A possible typo of the Media RSS 1.5 namespace.
  291. */
  292. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3', 'http://video.search.yahoo.com/mrss/');
  293. /**
  294. * Wrong Media RSS Namespace #4. New spec location after the RSS Advisory Board takes it over, but not a valid namespace.
  295. */
  296. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4', 'http://www.rssboard.org/media-rss');
  297. /**
  298. * Wrong Media RSS Namespace #5. A possible typo of the RSS Advisory Board URL.
  299. */
  300. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5', 'http://www.rssboard.org/media-rss/');
  301. /**
  302. * iTunes RSS Namespace
  303. */
  304. define('SIMPLEPIE_NAMESPACE_ITUNES', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
  305. /**
  306. * XHTML Namespace
  307. */
  308. define('SIMPLEPIE_NAMESPACE_XHTML', 'http://www.w3.org/1999/xhtml');
  309. /**
  310. * IANA Link Relations Registry
  311. */
  312. define('SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY', 'http://www.iana.org/assignments/relation/');
  313. /**
  314. * No file source
  315. */
  316. define('SIMPLEPIE_FILE_SOURCE_NONE', 0);
  317. /**
  318. * Remote file source
  319. */
  320. define('SIMPLEPIE_FILE_SOURCE_REMOTE', 1);
  321. /**
  322. * Local file source
  323. */
  324. define('SIMPLEPIE_FILE_SOURCE_LOCAL', 2);
  325. /**
  326. * fsockopen() file source
  327. */
  328. define('SIMPLEPIE_FILE_SOURCE_FSOCKOPEN', 4);
  329. /**
  330. * cURL file source
  331. */
  332. define('SIMPLEPIE_FILE_SOURCE_CURL', 8);
  333. /**
  334. * file_get_contents() file source
  335. */
  336. define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);
  337. /**
  338. * SimplePie
  339. *
  340. * @package SimplePie
  341. * @subpackage API
  342. */
  343. class SimplePie
  344. {
  345. /**
  346. * @var array Raw data
  347. * @access private
  348. */
  349. public $data = array();
  350. /**
  351. * @var mixed Error string
  352. * @access private
  353. */
  354. public $error;
  355. /**
  356. * @var object Instance of SimplePie_Sanitize (or other class)
  357. * @see SimplePie::set_sanitize_class()
  358. * @access private
  359. */
  360. public $sanitize;
  361. /**
  362. * @var string SimplePie Useragent
  363. * @see SimplePie::set_useragent()
  364. * @access private
  365. */
  366. public $useragent = SIMPLEPIE_USERAGENT;
  367. /**
  368. * @var string Feed URL
  369. * @see SimplePie::set_feed_url()
  370. * @access private
  371. */
  372. public $feed_url;
  373. /**
  374. * @var object Instance of SimplePie_File to use as a feed
  375. * @see SimplePie::set_file()
  376. * @access private
  377. */
  378. public $file;
  379. /**
  380. * @var string Raw feed data
  381. * @see SimplePie::set_raw_data()
  382. * @access private
  383. */
  384. public $raw_data;
  385. /**
  386. * @var int Timeout for fetching remote files
  387. * @see SimplePie::set_timeout()
  388. * @access private
  389. */
  390. public $timeout = 10;
  391. /**
  392. * @var bool Forces fsockopen() to be used for remote files instead
  393. * of cURL, even if a new enough version is installed
  394. * @see SimplePie::force_fsockopen()
  395. * @access private
  396. */
  397. public $force_fsockopen = false;
  398. /**
  399. * @var bool Force the given data/URL to be treated as a feed no matter what
  400. * it appears like
  401. * @see SimplePie::force_feed()
  402. * @access private
  403. */
  404. public $force_feed = false;
  405. /**
  406. * @var bool Enable/Disable Caching
  407. * @see SimplePie::enable_cache()
  408. * @access private
  409. */
  410. public $cache = true;
  411. /**
  412. * @var int Cache duration (in seconds)
  413. * @see SimplePie::set_cache_duration()
  414. * @access private
  415. */
  416. public $cache_duration = 3600;
  417. /**
  418. * @var int Auto-discovery cache duration (in seconds)
  419. * @see SimplePie::set_autodiscovery_cache_duration()
  420. * @access private
  421. */
  422. public $autodiscovery_cache_duration = 604800; // 7 Days.
  423. /**
  424. * @var string Cache location (relative to executing script)
  425. * @see SimplePie::set_cache_location()
  426. * @access private
  427. */
  428. public $cache_location = './cache';
  429. /**
  430. * @var string Function that creates the cache filename
  431. * @see SimplePie::set_cache_name_function()
  432. * @access private
  433. */
  434. public $cache_name_function = 'md5';
  435. /**
  436. * @var bool Reorder feed by date descending
  437. * @see SimplePie::enable_order_by_date()
  438. * @access private
  439. */
  440. public $order_by_date = true;
  441. /**
  442. * @var mixed Force input encoding to be set to the follow value
  443. * (false, or anything type-cast to false, disables this feature)
  444. * @see SimplePie::set_input_encoding()
  445. * @access private
  446. */
  447. public $input_encoding = false;
  448. /**
  449. * @var int Feed Autodiscovery Level
  450. * @see SimplePie::set_autodiscovery_level()
  451. * @access private
  452. */
  453. public $autodiscovery = SIMPLEPIE_LOCATOR_ALL;
  454. /**
  455. * Class registry object
  456. *
  457. * @var SimplePie_Registry
  458. */
  459. public $registry;
  460. /**
  461. * @var int Maximum number of feeds to check with autodiscovery
  462. * @see SimplePie::set_max_checked_feeds()
  463. * @access private
  464. */
  465. public $max_checked_feeds = 10;
  466. /**
  467. * @var array All the feeds found during the autodiscovery process
  468. * @see SimplePie::get_all_discovered_feeds()
  469. * @access private
  470. */
  471. public $all_discovered_feeds = array();
  472. /**
  473. * @var string Web-accessible path to the handler_image.php file.
  474. * @see SimplePie::set_image_handler()
  475. * @access private
  476. */
  477. public $image_handler = '';
  478. /**
  479. * @var array Stores the URLs when multiple feeds are being initialized.
  480. * @see SimplePie::set_feed_url()
  481. * @access private
  482. */
  483. public $multifeed_url = array();
  484. /**
  485. * @var array Stores SimplePie objects when multiple feeds initialized.
  486. * @access private
  487. */
  488. public $multifeed_objects = array();
  489. /**
  490. * @var array Stores the get_object_vars() array for use with multifeeds.
  491. * @see SimplePie::set_feed_url()
  492. * @access private
  493. */
  494. public $config_settings = null;
  495. /**
  496. * @var integer Stores the number of items to return per-feed with multifeeds.
  497. * @see SimplePie::set_item_limit()
  498. * @access private
  499. */
  500. public $item_limit = 0;
  501. /**
  502. * @var array Stores the default attributes to be stripped by strip_attributes().
  503. * @see SimplePie::strip_attributes()
  504. * @access private
  505. */
  506. public $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  507. /**
  508. * @var array Stores the default tags to be stripped by strip_htmltags().
  509. * @see SimplePie::strip_htmltags()
  510. * @access private
  511. */
  512. public $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  513. /**
  514. * The SimplePie class contains feed level data and options
  515. *
  516. * To use SimplePie, create the SimplePie object with no parameters. You can
  517. * then set configuration options using the provided methods. After setting
  518. * them, you must initialise the feed using $feed->init(). At that point the
  519. * object's methods and properties will be available to you.
  520. *
  521. * Previously, it was possible to pass in the feed URL along with cache
  522. * options directly into the constructor. This has been removed as of 1.3 as
  523. * it caused a lot of confusion.
  524. *
  525. * @since 1.0 Preview Release
  526. */
  527. public function __construct()
  528. {
  529. if (version_compare(PHP_VERSION, '5.2', '<'))
  530. {
  531. trigger_error('PHP 4.x, 5.0 and 5.1 are no longer supported. Please upgrade to PHP 5.2 or newer.');
  532. die();
  533. }
  534. // Other objects, instances created here so we can set options on them
  535. $this->sanitize = new SimplePie_Sanitize();
  536. $this->registry = new SimplePie_Registry();
  537. if (func_num_args() > 0)
  538. {
  539. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  540. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_location() directly.', $level);
  541. $args = func_get_args();
  542. switch (count($args)) {
  543. case 3:
  544. $this->set_cache_duration($args[2]);
  545. case 2:
  546. $this->set_cache_location($args[1]);
  547. case 1:
  548. $this->set_feed_url($args[0]);
  549. $this->init();
  550. }
  551. }
  552. }
  553. /**
  554. * Used for converting object to a string
  555. */
  556. public function __toString()
  557. {
  558. return md5(serialize($this->data));
  559. }
  560. /**
  561. * Remove items that link back to this before destroying this object
  562. */
  563. public function __destruct()
  564. {
  565. if ((version_compare(PHP_VERSION, '5.3', '<') || !gc_enabled()) && !ini_get('zend.ze1_compatibility_mode'))
  566. {
  567. if (!empty($this->data['items']))
  568. {
  569. foreach ($this->data['items'] as $item)
  570. {
  571. $item->__destruct();
  572. }
  573. unset($item, $this->data['items']);
  574. }
  575. if (!empty($this->data['ordered_items']))
  576. {
  577. foreach ($this->data['ordered_items'] as $item)
  578. {
  579. $item->__destruct();
  580. }
  581. unset($item, $this->data['ordered_items']);
  582. }
  583. }
  584. }
  585. /**
  586. * Force the given data/URL to be treated as a feed
  587. *
  588. * This tells SimplePie to ignore the content-type provided by the server.
  589. * Be careful when using this option, as it will also disable autodiscovery.
  590. *
  591. * @since 1.1
  592. * @param bool $enable Force the given data/URL to be treated as a feed
  593. */
  594. public function force_feed($enable = false)
  595. {
  596. $this->force_feed = (bool) $enable;
  597. }
  598. /**
  599. * Set the URL of the feed you want to parse
  600. *
  601. * This allows you to enter the URL of the feed you want to parse, or the
  602. * website you want to try to use auto-discovery on. This takes priority
  603. * over any set raw data.
  604. *
  605. * You can set multiple feeds to mash together by passing an array instead
  606. * of a string for the $url. Remember that with each additional feed comes
  607. * additional processing and resources.
  608. *
  609. * @since 1.0 Preview Release
  610. * @see set_raw_data()
  611. * @param string|array $url This is the URL (or array of URLs) that you want to parse.
  612. */
  613. public function set_feed_url($url)
  614. {
  615. $this->multifeed_url = array();
  616. if (is_array($url))
  617. {
  618. foreach ($url as $value)
  619. {
  620. $this->multifeed_url[] = $this->registry->call('Misc', 'fix_protocol', array($value, 1));
  621. }
  622. }
  623. else
  624. {
  625. $this->feed_url = $this->registry->call('Misc', 'fix_protocol', array($url, 1));
  626. }
  627. }
  628. /**
  629. * Set an instance of {@see SimplePie_File} to use as a feed
  630. *
  631. * @param SimplePie_File &$file
  632. * @return bool True on success, false on failure
  633. */
  634. public function set_file(&$file)
  635. {
  636. if ($file instanceof SimplePie_File)
  637. {
  638. $this->feed_url = $file->url;
  639. $this->file =& $file;
  640. return true;
  641. }
  642. return false;
  643. }
  644. /**
  645. * Set the raw XML data to parse
  646. *
  647. * Allows you to use a string of RSS/Atom data instead of a remote feed.
  648. *
  649. * If you have a feed available as a string in PHP, you can tell SimplePie
  650. * to parse that data string instead of a remote feed. Any set feed URL
  651. * takes precedence.
  652. *
  653. * @since 1.0 Beta 3
  654. * @param string $data RSS or Atom data as a string.
  655. * @see set_feed_url()
  656. */
  657. public function set_raw_data($data)
  658. {
  659. $this->raw_data = $data;
  660. }
  661. /**
  662. * Set the the default timeout for fetching remote feeds
  663. *
  664. * This allows you to change the maximum time the feed's server to respond
  665. * and send the feed back.
  666. *
  667. * @since 1.0 Beta 3
  668. * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed.
  669. */
  670. public function set_timeout($timeout = 10)
  671. {
  672. $this->timeout = (int) $timeout;
  673. }
  674. /**
  675. * Force SimplePie to use fsockopen() instead of cURL
  676. *
  677. * @since 1.0 Beta 3
  678. * @param bool $enable Force fsockopen() to be used
  679. */
  680. public function force_fsockopen($enable = false)
  681. {
  682. $this->force_fsockopen = (bool) $enable;
  683. }
  684. /**
  685. * Enable/disable caching in SimplePie.
  686. *
  687. * This option allows you to disable caching all-together in SimplePie.
  688. * However, disabling the cache can lead to longer load times.
  689. *
  690. * @since 1.0 Preview Release
  691. * @param bool $enable Enable caching
  692. */
  693. public function enable_cache($enable = true)
  694. {
  695. $this->cache = (bool) $enable;
  696. }
  697. /**
  698. * Set the length of time (in seconds) that the contents of a feed will be
  699. * cached
  700. *
  701. * @param int $seconds The feed content cache duration
  702. */
  703. public function set_cache_duration($seconds = 3600)
  704. {
  705. $this->cache_duration = (int) $seconds;
  706. }
  707. /**
  708. * Set the length of time (in seconds) that the autodiscovered feed URL will
  709. * be cached
  710. *
  711. * @param int $seconds The autodiscovered feed URL cache duration.
  712. */
  713. public function set_autodiscovery_cache_duration($seconds = 604800)
  714. {
  715. $this->autodiscovery_cache_duration = (int) $seconds;
  716. }
  717. /**
  718. * Set the file system location where the cached files should be stored
  719. *
  720. * @param string $location The file system location.
  721. */
  722. public function set_cache_location($location = './cache')
  723. {
  724. $this->cache_location = (string) $location;
  725. }
  726. /**
  727. * Set whether feed items should be sorted into reverse chronological order
  728. *
  729. * @param bool $enable Sort as reverse chronological order.
  730. */
  731. public function enable_order_by_date($enable = true)
  732. {
  733. $this->order_by_date = (bool) $enable;
  734. }
  735. /**
  736. * Set the character encoding used to parse the feed
  737. *
  738. * This overrides the encoding reported by the feed, however it will fall
  739. * back to the normal encoding detection if the override fails
  740. *
  741. * @param string $encoding Character encoding
  742. */
  743. public function set_input_encoding($encoding = false)
  744. {
  745. if ($encoding)
  746. {
  747. $this->input_encoding = (string) $encoding;
  748. }
  749. else
  750. {
  751. $this->input_encoding = false;
  752. }
  753. }
  754. /**
  755. * Set how much feed autodiscovery to do
  756. *
  757. * @see SIMPLEPIE_LOCATOR_NONE
  758. * @see SIMPLEPIE_LOCATOR_AUTODISCOVERY
  759. * @see SIMPLEPIE_LOCATOR_LOCAL_EXTENSION
  760. * @see SIMPLEPIE_LOCATOR_LOCAL_BODY
  761. * @see SIMPLEPIE_LOCATOR_REMOTE_EXTENSION
  762. * @see SIMPLEPIE_LOCATOR_REMOTE_BODY
  763. * @see SIMPLEPIE_LOCATOR_ALL
  764. * @param int $level Feed Autodiscovery Level (level can be a combination of the above constants, see bitwise OR operator)
  765. */
  766. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  767. {
  768. $this->autodiscovery = (int) $level;
  769. }
  770. /**
  771. * Get the class registry
  772. *
  773. * Use this to override SimplePie's default classes
  774. * @see SimplePie_Registry
  775. * @return SimplePie_Registry
  776. */
  777. public function &get_registry()
  778. {
  779. return $this->registry;
  780. }
  781. /**#@+
  782. * Useful when you are overloading or extending SimplePie's default classes.
  783. *
  784. * @deprecated Use {@see get_registry()} instead
  785. * @link http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends PHP5 extends documentation
  786. * @param string $class Name of custom class
  787. * @return boolean True on success, false otherwise
  788. */
  789. /**
  790. * Set which class SimplePie uses for caching
  791. */
  792. public function set_cache_class($class = 'SimplePie_Cache')
  793. {
  794. return $this->registry->register('Cache', $class, true);
  795. }
  796. /**
  797. * Set which class SimplePie uses for auto-discovery
  798. */
  799. public function set_locator_class($class = 'SimplePie_Locator')
  800. {
  801. return $this->registry->register('Locator', $class, true);
  802. }
  803. /**
  804. * Set which class SimplePie uses for XML parsing
  805. */
  806. public function set_parser_class($class = 'SimplePie_Parser')
  807. {
  808. return $this->registry->register('Parser', $class, true);
  809. }
  810. /**
  811. * Set which class SimplePie uses for remote file fetching
  812. */
  813. public function set_file_class($class = 'SimplePie_File')
  814. {
  815. return $this->registry->register('File', $class, true);
  816. }
  817. /**
  818. * Set which class SimplePie uses for data sanitization
  819. */
  820. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  821. {
  822. return $this->registry->register('Sanitize', $class, true);
  823. }
  824. /**
  825. * Set which class SimplePie uses for handling feed items
  826. */
  827. public function set_item_class($class = 'SimplePie_Item')
  828. {
  829. return $this->registry->register('Item', $class, true);
  830. }
  831. /**
  832. * Set which class SimplePie uses for handling author data
  833. */
  834. public function set_author_class($class = 'SimplePie_Author')
  835. {
  836. return $this->registry->register('Author', $class, true);
  837. }
  838. /**
  839. * Set which class SimplePie uses for handling category data
  840. */
  841. public function set_category_class($class = 'SimplePie_Category')
  842. {
  843. return $this->registry->register('Category', $class, true);
  844. }
  845. /**
  846. * Set which class SimplePie uses for feed enclosures
  847. */
  848. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  849. {
  850. return $this->registry->register('Enclosure', $class, true);
  851. }
  852. /**
  853. * Set which class SimplePie uses for `<media:text>` captions
  854. */
  855. public function set_caption_class($class = 'SimplePie_Caption')
  856. {
  857. return $this->registry->register('Caption', $class, true);
  858. }
  859. /**
  860. * Set which class SimplePie uses for `<media:copyright>`
  861. */
  862. public function set_copyright_class($class = 'SimplePie_Copyright')
  863. {
  864. return $this->registry->register('Copyright', $class, true);
  865. }
  866. /**
  867. * Set which class SimplePie uses for `<media:credit>`
  868. */
  869. public function set_credit_class($class = 'SimplePie_Credit')
  870. {
  871. return $this->registry->register('Credit', $class, true);
  872. }
  873. /**
  874. * Set which class SimplePie uses for `<media:rating>`
  875. */
  876. public function set_rating_class($class = 'SimplePie_Rating')
  877. {
  878. return $this->registry->register('Rating', $class, true);
  879. }
  880. /**
  881. * Set which class SimplePie uses for `<media:restriction>`
  882. */
  883. public function set_restriction_class($class = 'SimplePie_Restriction')
  884. {
  885. return $this->registry->register('Restriction', $class, true);
  886. }
  887. /**
  888. * Set which class SimplePie uses for content-type sniffing
  889. */
  890. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  891. {
  892. return $this->registry->register('Content_Type_Sniffer', $class, true);
  893. }
  894. /**
  895. * Set which class SimplePie uses item sources
  896. */
  897. public function set_source_class($class = 'SimplePie_Source')
  898. {
  899. return $this->registry->register('Source', $class, true);
  900. }
  901. /**#@-*/
  902. /**
  903. * Set the user agent string
  904. *
  905. * @param string $ua New user agent string.
  906. */
  907. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  908. {
  909. $this->useragent = (string) $ua;
  910. }
  911. /**
  912. * Set callback function to create cache filename with
  913. *
  914. * @param mixed $function Callback function
  915. */
  916. public function set_cache_name_function($function = 'md5')
  917. {
  918. if (is_callable($function))
  919. {
  920. $this->cache_name_function = $function;
  921. }
  922. }
  923. /**
  924. * Set options to make SP as fast as possible
  925. *
  926. * Forgoes a substantial amount of data sanitization in favor of speed. This
  927. * turns SimplePie into a dumb parser of feeds.
  928. *
  929. * @param bool $set Whether to set them or not
  930. */
  931. public function set_stupidly_fast($set = false)
  932. {
  933. if ($set)
  934. {
  935. $this->enable_order_by_date(false);
  936. $this->remove_div(false);
  937. $this->strip_comments(false);
  938. $this->strip_htmltags(false);
  939. $this->strip_attributes(false);
  940. $this->set_image_handler(false);
  941. }
  942. }
  943. /**
  944. * Set maximum number of feeds to check with autodiscovery
  945. *
  946. * @param int $max Maximum number of feeds to check
  947. */
  948. public function set_max_checked_feeds($max = 10)
  949. {
  950. $this->max_checked_feeds = (int) $max;
  951. }
  952. public function remove_div($enable = true)
  953. {
  954. $this->sanitize->remove_div($enable);
  955. }
  956. public function strip_htmltags($tags = '', $encode = null)
  957. {
  958. if ($tags === '')
  959. {
  960. $tags = $this->strip_htmltags;
  961. }
  962. $this->sanitize->strip_htmltags($tags);
  963. if ($encode !== null)
  964. {
  965. $this->sanitize->encode_instead_of_strip($tags);
  966. }
  967. }
  968. public function encode_instead_of_strip($enable = true)
  969. {
  970. $this->sanitize->encode_instead_of_strip($enable);
  971. }
  972. public function strip_attributes($attribs = '')
  973. {
  974. if ($attribs === '')
  975. {
  976. $attribs = $this->strip_attributes;
  977. }
  978. $this->sanitize->strip_attributes($attribs);
  979. }
  980. /**
  981. * Set the output encoding
  982. *
  983. * Allows you to override SimplePie's output to match that of your webpage.
  984. * This is useful for times when your webpages are not being served as
  985. * UTF-8. This setting will be obeyed by {@see handle_content_type()}, and
  986. * is similar to {@see set_input_encoding()}.
  987. *
  988. * It should be noted, however, that not all character encodings can support
  989. * all characters. If your page is being served as ISO-8859-1 and you try
  990. * to display a Japanese feed, you'll likely see garbled characters.
  991. * Because of this, it is highly recommended to ensure that your webpages
  992. * are served as UTF-8.
  993. *
  994. * The number of supported character encodings depends on whether your web
  995. * host supports {@link http://php.net/mbstring mbstring},
  996. * {@link http://php.net/iconv iconv}, or both. See
  997. * {@link http://simplepie.org/wiki/faq/Supported_Character_Encodings} for
  998. * more information.
  999. *
  1000. * @param string $encoding
  1001. */
  1002. public function set_output_encoding($encoding = 'UTF-8')
  1003. {
  1004. $this->sanitize->set_output_encoding($encoding);
  1005. }
  1006. public function strip_comments($strip = false)
  1007. {
  1008. $this->sanitize->strip_comments($strip);
  1009. }
  1010. /**
  1011. * Set element/attribute key/value pairs of HTML attributes
  1012. * containing URLs that need to be resolved relative to the feed
  1013. *
  1014. * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite,
  1015. * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite,
  1016. * |q|@cite
  1017. *
  1018. * @since 1.0
  1019. * @param array|null $element_attribute Element/attribute key/value pairs, null for default
  1020. */
  1021. public function set_url_replacements($element_attribute = null)
  1022. {
  1023. $this->sanitize->set_url_replacements($element_attribute);
  1024. }
  1025. /**
  1026. * Set the handler to enable the display of cached images.
  1027. *
  1028. * @param str $page Web-accessible path to the handler_image.php file.
  1029. * @param str $qs The query string that the value should be passed to.
  1030. */
  1031. public function set_image_handler($page = false, $qs = 'i')
  1032. {
  1033. if ($page !== false)
  1034. {
  1035. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1036. }
  1037. else
  1038. {
  1039. $this->image_handler = '';
  1040. }
  1041. }
  1042. /**
  1043. * Set the limit for items returned per-feed with multifeeds
  1044. *
  1045. * @param integer $limit The maximum number of items to return.
  1046. */
  1047. public function set_item_limit($limit = 0)
  1048. {
  1049. $this->item_limit = (int) $limit;
  1050. }
  1051. /**
  1052. * Initialize the feed object
  1053. *
  1054. * This is what makes everything happen. Period. This is where all of the
  1055. * configuration options get processed, feeds are fetched, cached, and
  1056. * parsed, and all of that other good stuff.
  1057. *
  1058. * @return boolean True if successful, false otherwise
  1059. */
  1060. public function init()
  1061. {
  1062. // Check absolute bare minimum requirements.
  1063. if (!extension_loaded('xml') || !extension_loaded('pcre'))
  1064. {
  1065. return false;
  1066. }
  1067. // Then check the xml extension is sane (i.e., libxml 2.7.x issue on PHP < 5.2.9 and libxml 2.7.0 to 2.7.2 on any version) if we don't have xmlreader.
  1068. elseif (!extension_loaded('xmlreader'))
  1069. {
  1070. static $xml_is_sane = null;
  1071. if ($xml_is_sane === null)
  1072. {
  1073. $parser_check = xml_parser_create();
  1074. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1075. xml_parser_free($parser_check);
  1076. $xml_is_sane = isset($values[0]['value']);
  1077. }
  1078. if (!$xml_is_sane)
  1079. {
  1080. return false;
  1081. }
  1082. }
  1083. if (method_exists($this->sanitize, 'set_registry'))
  1084. {
  1085. $this->sanitize->set_registry($this->registry);
  1086. }
  1087. // Pass whatever was set with config options over to the sanitizer.
  1088. // Pass the classes in for legacy support; new classes should use the registry instead
  1089. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->registry->get_class('Cache'));
  1090. $this->sanitize->pass_file_data($this->registry->get_class('File'), $this->timeout, $this->useragent, $this->force_fsockopen);
  1091. if (!empty($this->multifeed_url))
  1092. {
  1093. $i = 0;
  1094. $success = 0;
  1095. $this->multifeed_objects = array();
  1096. $this->error = array();
  1097. foreach ($this->multifeed_url as $url)
  1098. {
  1099. $this->multifeed_objects[$i] = clone $this;
  1100. $this->multifeed_objects[$i]->set_feed_url($url);
  1101. $single_success = $this->multifeed_objects[$i]->init();
  1102. $success |= $single_success;
  1103. if (!$single_success)
  1104. {
  1105. $this->error[$i] = $this->multifeed_objects[$i]->error();
  1106. }
  1107. $i++;
  1108. }
  1109. return (bool) $success;
  1110. }
  1111. elseif ($this->feed_url === null && $this->raw_data === null)
  1112. {
  1113. return false;
  1114. }
  1115. $this->error = null;
  1116. $this->data = array();
  1117. $this->multifeed_objects = array();
  1118. $cache = false;
  1119. if ($this->feed_url !== null)
  1120. {
  1121. $parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1122. // Decide whether to enable caching
  1123. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1124. {
  1125. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc'));
  1126. }
  1127. // Fetch the data via SimplePie_File into $this->raw_data
  1128. if (($fetched = $this->fetch_data($cache)) === true)
  1129. {
  1130. return true;
  1131. }
  1132. elseif ($fetched === false) {
  1133. return false;
  1134. }
  1135. list($headers, $sniffed) = $fetched;
  1136. }
  1137. // Set up array of possible encodings
  1138. $encodings = array();
  1139. // First check to see if input has been overridden.
  1140. if ($this->input_encoding !== false)
  1141. {
  1142. $encodings[] = $this->input_encoding;
  1143. }
  1144. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1145. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1146. // RFC 3023 (only applies to sniffed content)
  1147. if (isset($sniffed))
  1148. {
  1149. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1150. {
  1151. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1152. {
  1153. $encodings[] = strtoupper($charset[1]);
  1154. }
  1155. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1156. $encodings[] = 'UTF-8';
  1157. }
  1158. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1159. {
  1160. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1161. {
  1162. $encodings[] = $charset[1];
  1163. }
  1164. $encodings[] = 'US-ASCII';
  1165. }
  1166. // Text MIME-type default
  1167. elseif (substr($sniffed, 0, 5) === 'text/')
  1168. {
  1169. $encodings[] = 'US-ASCII';
  1170. }
  1171. }
  1172. // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
  1173. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1174. $encodings[] = 'UTF-8';
  1175. $encodings[] = 'ISO-8859-1';
  1176. // There's no point in trying an encoding twice
  1177. $encodings = array_unique($encodings);
  1178. // Loop through each possible encoding, till we return something, or run out of possibilities
  1179. foreach ($encodings as $encoding)
  1180. {
  1181. // Change the encoding to UTF-8 (as we always use UTF-8 internally)
  1182. if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))
  1183. {
  1184. // Create new parser
  1185. $parser = $this->registry->create('Parser');
  1186. // If it's parsed fine
  1187. if ($parser->parse($utf8_data, 'UTF-8'))
  1188. {
  1189. $this->data = $parser->get_data();
  1190. if (!($this->get_type() & ~SIMPLEPIE_TYPE_NONE))
  1191. {
  1192. $this->error = "A feed could not be found at $this->feed_url. This does not appear to be a valid RSS or Atom feed.";
  1193. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1194. return false;
  1195. }
  1196. if (isset($headers))
  1197. {
  1198. $this->data['headers'] = $headers;
  1199. }
  1200. $this->data['build'] = SIMPLEPIE_BUILD;
  1201. // Cache the file if caching is enabled
  1202. if ($cache && !$cache->save($this))
  1203. {
  1204. trigger_error("$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1205. }
  1206. return true;
  1207. }
  1208. }
  1209. }
  1210. if (isset($parser))
  1211. {
  1212. // We have an error, just set SimplePie_Misc::error to it and quit
  1213. $this->error = sprintf('This XML document is invalid, likely due to invalid characters. XML error: %s at line %d, column %d', $parser->get_error_string(), $parser->get_current_line(), $parser->get_current_column());
  1214. }
  1215. else
  1216. {
  1217. $this->error = 'The data could not be converted to UTF-8. You MUST have either the iconv or mbstring extension installed. Upgrading to PHP 5.x (which includes iconv) is highly recommended.';
  1218. }
  1219. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1220. return false;
  1221. }
  1222. /**
  1223. * Fetch the data via SimplePie_File
  1224. *
  1225. * If the data is already cached, attempt to fetch it from there instead
  1226. * @param SimplePie_Cache|false $cache Cache handler, or false to not load from the cache
  1227. * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1228. */
  1229. protected function fetch_data(&$cache)
  1230. {
  1231. // If it's enabled, use the cache
  1232. if ($cache)
  1233. {
  1234. // Load the Cache
  1235. $this->data = $cache->load();
  1236. if (!empty($this->data))
  1237. {
  1238. // If the cache is for an outdated build of SimplePie
  1239. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1240. {
  1241. $cache->unlink();
  1242. $this->data = array();
  1243. }
  1244. // If we've hit a collision just rerun it with caching disabled
  1245. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1246. {
  1247. $cache = false;
  1248. $this->data = array();
  1249. }
  1250. // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL.
  1251. elseif (isset($this->data['feed_url']))
  1252. {
  1253. // If the autodiscovery cache is still valid use it.
  1254. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1255. {
  1256. // Do not need to do feed autodiscovery yet.
  1257. if ($this->data['feed_url'] !== $this->data['url'])
  1258. {
  1259. $this->set_feed_url($this->data['feed_url']);
  1260. return $this->init();
  1261. }
  1262. $cache->unlink();
  1263. $this->data = array();
  1264. }
  1265. }
  1266. // Check if the cache has been updated
  1267. elseif ($cache->mtime() + $this->cache_duration < time())
  1268. {
  1269. // If we have last-modified and/or etag set
  1270. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1271. {
  1272. $headers = array(
  1273. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1274. );
  1275. if (isset($this->data['headers']['last-modified']))
  1276. {
  1277. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1278. }
  1279. if (isset($this->data['headers']['etag']))
  1280. {
  1281. $headers['if-none-match'] = $this->data['headers']['etag'];
  1282. }
  1283. $file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen));
  1284. if ($file->success)
  1285. {
  1286. if ($file->status_code === 304)
  1287. {
  1288. $cache->touch();
  1289. return true;
  1290. }
  1291. }
  1292. else
  1293. {
  1294. unset($file);
  1295. }
  1296. }
  1297. }
  1298. // If the cache is still valid, just return true
  1299. else
  1300. {
  1301. $this->raw_data = false;
  1302. return true;
  1303. }
  1304. }
  1305. // If the cache is empty, delete it
  1306. else
  1307. {
  1308. $cache->unlink();
  1309. $this->data = array();
  1310. }
  1311. }
  1312. // If we don't already have the file (it'll only exist if we've opened it to check if the cache has been modified), open it.
  1313. if (!isset($file))
  1314. {
  1315. if ($this->file instanceof SimplePie_File && $this->file->url === $this->feed_url)
  1316. {
  1317. $file =& $this->file;
  1318. }
  1319. else
  1320. {
  1321. $headers = array(
  1322. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1323. );
  1324. $file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen));
  1325. }
  1326. }
  1327. // If the file connection has an error, set SimplePie::error to that and quit
  1328. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1329. {
  1330. $this->error = $file->error;
  1331. return !empty($this->data);
  1332. }
  1333. if (!$this->force_feed)
  1334. {
  1335. // Check if the supplied URL is a feed, if it isn't, look for it.
  1336. $locate = $this->registry->create('Locator', array(&$file, $this->timeout, $this->useragent, $this->max_checked_feeds));
  1337. if (!$locate->is_feed($file))
  1338. {
  1339. // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
  1340. unset($file);
  1341. try
  1342. {
  1343. if (!($file = $locate->find($this->autodiscovery, $this->all_discovered_feeds)))
  1344. {
  1345. $this->error = "A feed could not be found at $this->feed_url. A feed with an invalid mime type may fall victim to this error, or " . SIMPLEPIE_NAME . " was unable to auto-discover it.. Use force_feed() if you are certain this URL is a real feed.";
  1346. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1347. return false;
  1348. }
  1349. }
  1350. catch (SimplePie_Exception $e)
  1351. {
  1352. // This is usually because DOMDocument doesn't exist
  1353. $this->error = $e->getMessage();
  1354. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, $e->getFile(), $e->getLine()));
  1355. return false;
  1356. }
  1357. if ($cache)
  1358. {
  1359. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1360. if (!$cache->save($this))
  1361. {
  1362. trigger_error("$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1363. }
  1364. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc'));
  1365. }
  1366. $this->feed_url = $file->url;
  1367. }
  1368. $locate = null;
  1369. }
  1370. $this->raw_data = $file->body;
  1371. $headers = $file->headers;
  1372. $sniffer = $this->registry->create('Content_Type_Sniffer', array(&$file));
  1373. $sniffed = $sniffer->get_type();
  1374. return array($headers, $sniffed);
  1375. }
  1376. /**
  1377. * Get the error message for the occured error
  1378. *
  1379. * @return string|array Error message, or array of messages for multifeeds
  1380. */
  1381. public function error()
  1382. {
  1383. return $this->error;
  1384. }
  1385. /**
  1386. * Get the raw XML
  1387. *
  1388. * This is the same as the old `$feed->enable_xml_dump(true)`, but returns
  1389. * the data instead of printing it.
  1390. *
  1391. * @return string|boolean Raw XML data, false if the cache is used
  1392. */
  1393. public function get_raw_data()
  1394. {
  1395. return $this->raw_data;
  1396. }
  1397. /**
  1398. * Get the character encoding used for output
  1399. *
  1400. * @since Preview Release
  1401. * @return string
  1402. */
  1403. public function get_encoding()
  1404. {
  1405. return $this->sanitize->output_encoding;
  1406. }
  1407. /**
  1408. * Send the content-type header with correct encoding
  1409. *
  1410. * This method ensures that the SimplePie-enabled page is being served with
  1411. * the correct {@link http://www.iana.org/assignments/media-types/ mime-type}
  1412. * and character encoding HTTP headers (character encoding determined by the
  1413. * {@see set_output_encoding} config option).
  1414. *
  1415. * This won't work properly if any content or whitespace has already been
  1416. * sent to the browser, because it relies on PHP's
  1417. * {@link http://php.net/header header()} function, and these are the
  1418. * circumstances under which the function works.
  1419. *
  1420. * Because it's setting these settings for the entire page (as is the nature
  1421. * of HTTP headers), this should only be used once per page (again, at the
  1422. * top).
  1423. *
  1424. * @param string $mime MIME type to serve the page as
  1425. */
  1426. public function handle_content_type($mime = 'text/html')
  1427. {
  1428. if (!headers_sent())
  1429. {
  1430. $header = "Content-type: $mime;";
  1431. if ($this->get_encoding())
  1432. {
  1433. $header .= ' charset=' . $this->get_encoding();
  1434. }
  1435. else
  1436. {
  1437. $header .= ' charset=UTF-8';
  1438. }
  1439. header($header);
  1440. }
  1441. }
  1442. /**
  1443. * Get the type of the feed
  1444. *
  1445. * This returns a SIMPLEPIE_TYPE_* constant, which can be tested against
  1446. * using {@link http://php.net/language.operators.bitwise bitwise operators}
  1447. *
  1448. * @since 0.8 (usage changed to using constants in 1.0)
  1449. * @see SIMPLEPIE_TYPE_NONE Unknown.
  1450. * @see SIMPLEPIE_TYPE_RSS_090 RSS 0.90.
  1451. * @see SIMPLEPIE_TYPE_RSS_091_NETSCAPE RSS 0.91 (Netscape).
  1452. * @see SIMPLEPIE_TYPE_RSS_091_USERLAND RSS 0.91 (Userland).
  1453. * @see SIMPLEPIE_TYPE_RSS_091 RSS 0.91.
  1454. * @see SIMPLEPIE_TYPE_RSS_092 RSS 0.92.
  1455. * @see SIMPLEPIE_TYPE_RSS_093 RSS 0.93.
  1456. * @see SIMPLEPIE_TYPE_RSS_094 RSS 0.94.
  1457. * @see SIMPLEPIE_TYPE_RSS_10 RSS 1.0.
  1458. * @see SIMPLEPIE_TYPE_RSS_20 RSS 2.0.x.
  1459. * @see SIMPLEPIE_TYPE_RSS_RDF RDF-based RSS.
  1460. * @see SIMPLEPIE_TYPE_RSS_SYNDICATION Non-RDF-based RSS (truly intended as syndication format).
  1461. * @see SIMPLEPIE_TYPE_RSS_ALL Any version of RSS.
  1462. * @see SIMPLEPIE_TYPE_ATOM_03 Atom 0.3.
  1463. * @see SIMPLEPIE_TYPE_ATOM_10 Atom 1.0.
  1464. * @see SIMPLEPIE_TYPE_ATOM_ALL Any version of Atom.
  1465. * @see SIMPLEPIE_TYPE_ALL Any known/supported feed type.
  1466. * @return int SIMPLEPIE_TYPE_* constant
  1467. */
  1468. public function get_type()
  1469. {
  1470. if (!isset($this->data['type']))
  1471. {
  1472. $this->data['type'] = SIMPLEPIE_TYPE_ALL;
  1473. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed']))
  1474. {
  1475. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_10;
  1476. }
  1477. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed']))
  1478. {
  1479. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_03;
  1480. }
  1481. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF']))
  1482. {
  1483. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['channel'])
  1484. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['image'])
  1485. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item'])
  1486. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['textinput']))
  1487. {
  1488. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_10;
  1489. }
  1490. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['channel'])
  1491. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['image'])
  1492. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item'])
  1493. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['textinput']))
  1494. {
  1495. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_090;
  1496. }
  1497. }
  1498. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss']))
  1499. {
  1500. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_ALL;
  1501. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1502. {
  1503. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1504. {
  1505. case '0.91':
  1506. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091;
  1507. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1508. {
  1509. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1510. {
  1511. case '0':
  1512. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_NETSCAPE;
  1513. break;
  1514. case '24':
  1515. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_USERLAND;
  1516. break;
  1517. }
  1518. }
  1519. break;
  1520. case '0.92':
  1521. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_092;
  1522. break;
  1523. case '0.93':
  1524. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_093;
  1525. break;
  1526. case '0.94':
  1527. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_094;
  1528. break;
  1529. case '2.0':
  1530. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_20;
  1531. break;
  1532. }
  1533. }
  1534. }
  1535. else
  1536. {
  1537. $this->data['type'] = SIMPLEPIE_TYPE_NONE;
  1538. }
  1539. }
  1540. return $this->data['type'];
  1541. }
  1542. /**
  1543. * Get the URL for the feed
  1544. *
  1545. * May or may not be different from the URL passed to {@see set_feed_url()},
  1546. * depending on whether auto-discovery was used.
  1547. *
  1548. * @since Preview Release (previously called `get_feed_url()` since SimplePie 0.8.)
  1549. * @todo If we have a perm redirect we should return the new URL
  1550. * @todo When we make the above change, let's support <itunes:new-feed-url> as well
  1551. * @todo Also, |atom:link|@rel=self
  1552. * @return string|null
  1553. */
  1554. public function subscribe_url()
  1555. {
  1556. if ($this->feed_url !== null)
  1557. {
  1558. return $this->sanitize($this->feed_url, SIMPLEPIE_CONSTRUCT_IRI);
  1559. }
  1560. else
  1561. {
  1562. return null;
  1563. }
  1564. }
  1565. /**
  1566. * Get data for an feed-level element
  1567. *
  1568. * This method allows you to get access to ANY element/attribute that is a
  1569. * sub-element of the opening feed tag.
  1570. *
  1571. * The return value is an indexed array of elements matching the given
  1572. * namespace and tag name. Each element has `attribs`, `data` and `child`
  1573. * subkeys. For `attribs` and `child`, these contain namespace subkeys.
  1574. * `attribs` then has one level of associative name => value data (where
  1575. * `value` is a string) after the namespace. `child` has tag-indexed keys
  1576. * after the namespace, each member of which is an indexed array matching
  1577. * this same format.
  1578. *
  1579. * For example:
  1580. * <pre>
  1581. * // This is probably a bad example because we already support
  1582. * // <media:content> natively, but it shows you how to parse through
  1583. * // the nodes.
  1584. * $group = $item->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'group');
  1585. * $content = $group[0]['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'];
  1586. * $file = $content[0]['attribs']['']['url'];
  1587. * echo $file;
  1588. * </pre>
  1589. *
  1590. * @since 1.0
  1591. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1592. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1593. * @param string $tag Tag name
  1594. * @return array
  1595. */
  1596. public function get_feed_tags($namespace, $tag)
  1597. {
  1598. $type = $this->get_type();
  1599. if ($type & SIMPLEPIE_TYPE_ATOM_10)
  1600. {
  1601. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag]))
  1602. {
  1603. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag];
  1604. }
  1605. }
  1606. if ($type & SIMPLEPIE_TYPE_ATOM_03)
  1607. {
  1608. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag]))
  1609. {
  1610. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag];
  1611. }
  1612. }
  1613. if ($type & SIMPLEPIE_TYPE_RSS_RDF)
  1614. {
  1615. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag]))
  1616. {
  1617. return $this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag];
  1618. }
  1619. }
  1620. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1621. {
  1622. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag]))
  1623. {
  1624. return $this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag];
  1625. }
  1626. }
  1627. return null;
  1628. }
  1629. /**
  1630. * Get data for an channel-level element
  1631. *
  1632. * This method allows you to get access to ANY element/attribute in the
  1633. * channel/header section of the feed.
  1634. *
  1635. * See {@see SimplePie::get_feed_tags()} for a description of the return value
  1636. *
  1637. * @since 1.0
  1638. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1639. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1640. * @param string $tag Tag name
  1641. * @return array
  1642. */
  1643. public function get_channel_tags($namespace, $tag)
  1644. {
  1645. $type = $this->get_type();
  1646. if ($type & SIMPLEPIE_TYPE_ATOM_ALL)
  1647. {
  1648. if ($return = $this->get_feed_tags($namespace, $tag))
  1649. {
  1650. return $return;
  1651. }
  1652. }
  1653. if ($type & SIMPLEPIE_TYPE_RSS_10)
  1654. {
  1655. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'channel'))
  1656. {
  1657. if (isset($channel[0]['child'][$namespace][$tag]))
  1658. {
  1659. return $channel[0]['child'][$namespace][$tag];
  1660. }
  1661. }
  1662. }
  1663. if ($type & SIMPLEPIE_TYPE_RSS_090)
  1664. {
  1665. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'channel'))
  1666. {
  1667. if (isset($channel[0]['child'][$namespace][$tag]))
  1668. {
  1669. return $channel[0]['child'][$namespace][$tag];
  1670. }
  1671. }
  1672. }
  1673. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1674. {
  1675. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'channel'))
  1676. {
  1677. if (isset($channel[0]['child'][$namespace][$tag]))
  1678. {
  1679. return $channel[0]['child'][$namespace][$tag];
  1680. }
  1681. }
  1682. }
  1683. return null;
  1684. }
  1685. /**
  1686. * Get data for an channel-level element
  1687. *
  1688. * This method allows you to get access to ANY element/attribute in the
  1689. * image/logo section of the feed.
  1690. *
  1691. * See {@see SimplePie::get_feed_tags()} for a description of the return value
  1692. *
  1693. * @since 1.0
  1694. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1695. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1696. * @param string $tag Tag name
  1697. * @return array
  1698. */
  1699. public function get_image_tags($namespace, $tag)
  1700. {
  1701. $type = $this->get_type();
  1702. if ($type & SIMPLEPIE_TYPE_RSS_10)
  1703. {
  1704. if ($image = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'image'))
  1705. {
  1706. if (isset($image[0]['child'][$namespace][$tag]))
  1707. {
  1708. return $image[0]['child'][$namespace][$tag];
  1709. }
  1710. }
  1711. }
  1712. if ($type & SIMPLEPIE_TYPE_RSS_090)
  1713. {
  1714. if ($image = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'image'))
  1715. {
  1716. if (isset($image[0]['child'][$namespace][$tag]))
  1717. {
  1718. return $image[0]['child'][$namespace][$tag];
  1719. }
  1720. }
  1721. }
  1722. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1723. {
  1724. if ($image = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'image'))
  1725. {
  1726. if (isset($image[0]['child'][$namespace][$tag]))
  1727. {
  1728. return $image[0]['child'][$namespace][$tag];
  1729. }
  1730. }
  1731. }
  1732. return null;
  1733. }
  1734. /**
  1735. * Get the base URL value from the feed
  1736. *
  1737. * Uses `<xml:base>` if available, otherwise uses the first link in the
  1738. * feed, or failing that, the URL of the feed itself.
  1739. *
  1740. * @see get_link
  1741. * @see subscribe_url
  1742. *
  1743. * @param array $element
  1744. * @return string
  1745. */
  1746. public function get_base($element = array())
  1747. {
  1748. if (!($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION) && !empty($element['xml_base_explicit']) && isset($element['xml_base']))
  1749. {
  1750. return $element['xml_base'];
  1751. }
  1752. elseif ($this->get_link() !== null)
  1753. {
  1754. return $this->get_link();
  1755. }
  1756. else
  1757. {
  1758. return $this->subscribe_url();
  1759. }
  1760. }
  1761. /**
  1762. * Sanitize feed data
  1763. *
  1764. * @access private
  1765. * @see SimplePie_Sanitize::sanitize()
  1766. * @param string $data Data to sanitize
  1767. * @param int $type One of the SIMPLEPIE_CONSTRUCT_* constants
  1768. * @param string $base Base URL to resolve URLs against
  1769. * @return string Sanitized data
  1770. */
  1771. public function sanitize($data, $type, $base = '')
  1772. {
  1773. return $this->sanitize->sanitize($data, $type, $base);
  1774. }
  1775. /**
  1776. * Get the title of the feed
  1777. *
  1778. * Uses `<atom:title>`, `<title>` or `<dc:title>`
  1779. *
  1780. * @since 1.0 (previously called `get_feed_title` since 0.8)
  1781. * @return string|null
  1782. */
  1783. public function get_title()
  1784. {
  1785. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'title'))
  1786. {
  1787. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  1788. }
  1789. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'title'))
  1790. {
  1791. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  1792. }
  1793. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  1794. {
  1795. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  1796. }
  1797. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  1798. {
  1799. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  1800. }
  1801. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  1802. {
  1803. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  1804. }
  1805. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  1806. {
  1807. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1808. }
  1809. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  1810. {
  1811. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1812. }
  1813. else
  1814. {
  1815. return null;
  1816. }
  1817. }
  1818. /**
  1819. * Get a category for the feed
  1820. *
  1821. * @since Unknown
  1822. * @param int $key The category that you want to return. Remember that arrays begin with 0, not 1
  1823. * @return SimplePie_Category|null
  1824. */
  1825. public function get_category($key = 0)
  1826. {
  1827. $categories = $this->get_categories();
  1828. if (isset($categories[$key]))
  1829. {
  1830. return $categories[$key];
  1831. }
  1832. else
  1833. {
  1834. return null;
  1835. }
  1836. }
  1837. /**
  1838. * Get all categories for the feed
  1839. *
  1840. * Uses `<atom:category>`, `<category>` or `<dc:subject>`
  1841. *
  1842. * @since Unknown
  1843. * @return array|null List of {@see SimplePie_Category} objects
  1844. */
  1845. public function get_categories()
  1846. {
  1847. $categories = array();
  1848. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'category') as $category)
  1849. {
  1850. $term = null;
  1851. $scheme = null;
  1852. $label = null;
  1853. if (isset($category['attribs']['']['term']))
  1854. {
  1855. $term = $this->sanitize($category['attribs']['']['term'], SIMPLEPIE_CONSTRUCT_TEXT);
  1856. }
  1857. if (isset($category['attribs']['']['scheme']))
  1858. {
  1859. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  1860. }
  1861. if (isset($category['attribs']['']['label']))
  1862. {
  1863. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  1864. }
  1865. $categories[] = $this->registry->create('Category', array($term, $scheme, $label));
  1866. }
  1867. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'category') as $category)
  1868. {
  1869. // This is really the label, but keep this as the term also for BC.
  1870. // Label will also work on retrieving because that falls back to term.
  1871. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1872. if (isset($category['attribs']['']['domain']))
  1873. {
  1874. $scheme = $this->sanitize($category['attribs']['']['domain'], SIMPLEPIE_CONSTRUCT_TEXT);
  1875. }
  1876. else
  1877. {
  1878. $scheme = null;
  1879. }
  1880. $categories[] = $this->registry->create('Category', array($term, $scheme, null));
  1881. }
  1882. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'subject') as $category)
  1883. {
  1884. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  1885. }
  1886. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'subject') as $category)
  1887. {
  1888. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  1889. }
  1890. if (!empty($categories))
  1891. {
  1892. return array_unique($categories);
  1893. }
  1894. else
  1895. {
  1896. return null;
  1897. }
  1898. }
  1899. /**
  1900. * Get an author for the feed
  1901. *
  1902. * @since 1.1
  1903. * @param int $key The author that you want to return. Remember that arrays begin with 0, not 1
  1904. * @return SimplePie_Author|null
  1905. */
  1906. public function get_author($key = 0)
  1907. {
  1908. $authors = $this->get_authors();
  1909. if (isset($authors[$key]))
  1910. {
  1911. return $authors[$key];
  1912. }
  1913. else
  1914. {
  1915. return null;
  1916. }
  1917. }
  1918. /**
  1919. * Get all authors for the feed
  1920. *
  1921. * Uses `<atom:author>`, `<author>`, `<dc:creator>` or `<itunes:author>`
  1922. *
  1923. * @since 1.1
  1924. * @return array|null List of {@see SimplePie_Author} objects
  1925. */
  1926. public function get_authors()
  1927. {
  1928. $authors = array();
  1929. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author') as $author)
  1930. {
  1931. $name = null;
  1932. $uri = null;
  1933. $email = null;
  1934. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  1935. {
  1936. $name = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1937. }
  1938. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  1939. {
  1940. $uri = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  1941. }
  1942. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  1943. {
  1944. $email = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1945. }
  1946. if ($name !== null || $email !== null || $uri !== null)
  1947. {
  1948. $authors[] = $this->registry->create('Author', array($name, $uri, $email));
  1949. }
  1950. }
  1951. if ($author = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'author'))
  1952. {
  1953. $name = null;
  1954. $url = null;
  1955. $email = null;
  1956. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  1957. {
  1958. $name = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1959. }
  1960. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  1961. {
  1962. $url = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  1963. }
  1964. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  1965. {
  1966. $email = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1967. }
  1968. if ($name !== null || $email !== null || $url !== null)
  1969. {
  1970. $authors[] = $this->registry->create('Author', array($name, $url, $email));
  1971. }
  1972. }
  1973. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'creator') as $author)
  1974. {
  1975. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  1976. }
  1977. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'creator') as $author)
  1978. {
  1979. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  1980. }
  1981. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'author') as $author)
  1982. {
  1983. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  1984. }
  1985. if (!empty($authors))
  1986. {
  1987. return array_unique($authors);
  1988. }
  1989. else
  1990. {
  1991. return null;
  1992. }
  1993. }
  1994. /**
  1995. * Get a contributor for the feed
  1996. *
  1997. * @since 1.1
  1998. * @param int $key The contrbutor that you want to return. Remember that arrays begin with 0, not 1
  1999. * @return SimplePie_Author|null
  2000. */
  2001. public function get_contributor($key = 0)
  2002. {
  2003. $contributors = $this->get_contributors();
  2004. if (isset($contributors[$key]))
  2005. {
  2006. return $contributors[$key];
  2007. }
  2008. else
  2009. {
  2010. return null;
  2011. }
  2012. }
  2013. /**
  2014. * Get all contributors for the feed
  2015. *
  2016. * Uses `<atom:contributor>`
  2017. *
  2018. * @since 1.1
  2019. * @return array|null List of {@see SimplePie_Author} objects
  2020. */
  2021. public function get_contributors()
  2022. {
  2023. $contributors = array();
  2024. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'contributor') as $contributor)
  2025. {
  2026. $name = null;
  2027. $uri = null;
  2028. $email = null;
  2029. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  2030. {
  2031. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2032. }
  2033. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  2034. {
  2035. $uri = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  2036. }
  2037. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  2038. {
  2039. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2040. }
  2041. if ($name !== null || $email !== null || $uri !== null)
  2042. {
  2043. $contributors[] = $this->registry->create('Author', array($name, $uri, $email));
  2044. }
  2045. }
  2046. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'contributor') as $contributor)
  2047. {
  2048. $name = null;
  2049. $url = null;
  2050. $email = null;
  2051. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  2052. {
  2053. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2054. }
  2055. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  2056. {
  2057. $url = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  2058. }
  2059. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  2060. {
  2061. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2062. }
  2063. if ($name !== null || $email !== null || $url !== null)
  2064. {
  2065. $contributors[] = $this->registry->create('Author', array($name, $url, $email));
  2066. }
  2067. }
  2068. if (!empty($contributors))
  2069. {
  2070. return array_unique($contributors);
  2071. }
  2072. else
  2073. {
  2074. return null;
  2075. }
  2076. }
  2077. /**
  2078. * Get a single link for the feed
  2079. *
  2080. * @since 1.0 (previously called `get_feed_link` since Preview Release, `get_feed_permalink()` since 0.8)
  2081. * @param int $key The link that you want to return. Remember that arrays begin with 0, not 1
  2082. * @param string $rel The relationship of the link to return
  2083. * @return string|null Link URL
  2084. */
  2085. public function get_link($key = 0, $rel = 'alternate')
  2086. {
  2087. $links = $this->get_links($rel);
  2088. if (isset($links[$key]))
  2089. {
  2090. return $links[$key];
  2091. }
  2092. else
  2093. {
  2094. return null;
  2095. }
  2096. }
  2097. /**
  2098. * Get the permalink for the item
  2099. *
  2100. * Returns the first link available with a relationship of "alternate".
  2101. * Identical to {@see get_link()} with key 0
  2102. *
  2103. * @see get_link
  2104. * @since 1.0 (previously called `get_feed_link` since Preview Release, `get_feed_permalink()` since 0.8)
  2105. * @internal Added for parity between the parent-level and the item/entry-level.
  2106. * @return string|null Link URL
  2107. */
  2108. public function get_permalink()
  2109. {
  2110. return $this->get_link(0);
  2111. }
  2112. /**
  2113. * Get all links for the feed
  2114. *
  2115. * Uses `<atom:link>` or `<link>`
  2116. *
  2117. * @since Beta 2
  2118. * @param string $rel The relationship of links to return
  2119. * @return array|null Links found for the feed (strings)
  2120. */
  2121. public function get_links($rel = 'alternate')
  2122. {
  2123. if (!isset($this->data['links']))
  2124. {
  2125. $this->data['links'] = array();
  2126. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link'))
  2127. {
  2128. foreach ($links as $link)
  2129. {
  2130. if (isset($link['attribs']['']['href']))
  2131. {
  2132. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  2133. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  2134. }
  2135. }
  2136. }
  2137. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link'))
  2138. {
  2139. foreach ($links as $link)
  2140. {
  2141. if (isset($link['attribs']['']['href']))
  2142. {
  2143. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  2144. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  2145. }
  2146. }
  2147. }
  2148. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link'))
  2149. {
  2150. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2151. }
  2152. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link'))
  2153. {
  2154. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2155. }
  2156. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link'))
  2157. {
  2158. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2159. }
  2160. $keys = array_keys($this->data['links']);
  2161. foreach ($keys as $key)
  2162. {
  2163. if ($this->registry->call('Misc', 'is_isegment_nz_nc', array($key)))
  2164. {
  2165. if (isset($this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]))
  2166. {
  2167. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]);
  2168. $this->data['links'][$key] =& $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key];
  2169. }
  2170. else
  2171. {
  2172. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] =& $this->data['links'][$key];
  2173. }
  2174. }
  2175. elseif (substr($key, 0, 41) === SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY)
  2176. {
  2177. $this->data['links'][substr($key, 41)] =& $this->data['links'][$key];
  2178. }
  2179. $this->data['links'][$key] = array_unique($this->data['links'][$key]);
  2180. }
  2181. }
  2182. if (isset($this->data['links'][$rel]))
  2183. {
  2184. return $this->data['links'][$rel];
  2185. }
  2186. else
  2187. {
  2188. return null;
  2189. }
  2190. }
  2191. public function get_all_discovered_feeds()
  2192. {
  2193. return $this->all_discovered_feeds;
  2194. }
  2195. /**
  2196. * Get the content for the item
  2197. *
  2198. * Uses `<atom:subtitle>`, `<atom:tagline>`, `<description>`,
  2199. * `<dc:description>`, `<itunes:summary>` or `<itunes:subtitle>`
  2200. *
  2201. * @since 1.0 (previously called `get_feed_description()` since 0.8)
  2202. * @return string|null
  2203. */
  2204. public function get_description()
  2205. {
  2206. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'subtitle'))
  2207. {
  2208. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2209. }
  2210. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'tagline'))
  2211. {
  2212. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2213. }
  2214. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'description'))
  2215. {
  2216. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2217. }
  2218. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'description'))
  2219. {
  2220. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2221. }
  2222. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'description'))
  2223. {
  2224. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2225. }
  2226. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'description'))
  2227. {
  2228. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2229. }
  2230. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'description'))
  2231. {
  2232. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2233. }
  2234. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'summary'))
  2235. {
  2236. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2237. }
  2238. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'subtitle'))
  2239. {
  2240. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2241. }
  2242. else
  2243. {
  2244. return null;
  2245. }
  2246. }
  2247. /**
  2248. * Get the copyright info for the feed
  2249. *
  2250. * Uses `<atom:rights>`, `<atom:copyright>` or `<dc:rights>`
  2251. *
  2252. * @since 1.0 (previously called `get_feed_copyright()` since 0.8)
  2253. * @return string|null
  2254. */
  2255. public function get_copyright()
  2256. {
  2257. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'rights'))
  2258. {
  2259. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2260. }
  2261. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'copyright'))
  2262. {
  2263. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2264. }
  2265. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'copyright'))
  2266. {
  2267. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2268. }
  2269. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'rights'))
  2270. {
  2271. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2272. }
  2273. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'rights'))
  2274. {
  2275. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2276. }
  2277. else
  2278. {
  2279. return null;
  2280. }
  2281. }
  2282. /**
  2283. * Get the language for the feed
  2284. *
  2285. * Uses `<language>`, `<dc:language>`, or @xml_lang
  2286. *
  2287. * @since 1.0 (previously called `get_feed_language()` since 0.8)
  2288. * @return string|null
  2289. */
  2290. public function get_language()
  2291. {
  2292. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'language'))
  2293. {
  2294. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2295. }
  2296. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'language'))
  2297. {
  2298. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2299. }
  2300. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'language'))
  2301. {
  2302. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2303. }
  2304. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['xml_lang']))
  2305. {
  2306. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2307. }
  2308. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['xml_lang']))
  2309. {
  2310. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2311. }
  2312. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['xml_lang']))
  2313. {
  2314. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2315. }
  2316. elseif (isset($this->data['headers']['content-language']))
  2317. {
  2318. return $this->sanitize($this->data['headers']['content-language'], SIMPLEPIE_CONSTRUCT_TEXT);
  2319. }
  2320. else
  2321. {
  2322. return null;
  2323. }
  2324. }
  2325. /**
  2326. * Get the latitude coordinates for the item
  2327. *
  2328. * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications
  2329. *
  2330. * Uses `<geo:lat>` or `<georss:point>`
  2331. *
  2332. * @since 1.0
  2333. * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo
  2334. * @link http://www.georss.org/ GeoRSS
  2335. * @return string|null
  2336. */
  2337. public function get_latitude()
  2338. {
  2339. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lat'))
  2340. {
  2341. return (float) $return[0]['data'];
  2342. }
  2343. elseif (($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  2344. {
  2345. return (float) $match[1];
  2346. }
  2347. else
  2348. {
  2349. return null;
  2350. }
  2351. }
  2352. /**
  2353. * Get the longitude coordinates for the feed
  2354. *
  2355. * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications
  2356. *
  2357. * Uses `<geo:long>`, `<geo:lon>` or `<georss:point>`
  2358. *
  2359. * @since 1.0
  2360. * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo
  2361. * @link http://www.georss.org/ GeoRSS
  2362. * @return string|null
  2363. */
  2364. public function get_longitude()
  2365. {
  2366. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'long'))
  2367. {
  2368. return (float) $return[0]['data'];
  2369. }
  2370. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lon'))
  2371. {
  2372. return (float) $return[0]['data'];
  2373. }
  2374. elseif (($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  2375. {
  2376. return (float) $match[2];
  2377. }
  2378. else
  2379. {
  2380. return null;
  2381. }
  2382. }
  2383. /**
  2384. * Get the feed logo's title
  2385. *
  2386. * RSS 0.9.0, 1.0 and 2.0 feeds are allowed to have a "feed logo" title.
  2387. *
  2388. * Uses `<image><title>` or `<image><dc:title>`
  2389. *
  2390. * @return string|null
  2391. */
  2392. public function get_image_title()
  2393. {
  2394. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  2395. {
  2396. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2397. }
  2398. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  2399. {
  2400. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2401. }
  2402. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  2403. {
  2404. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2405. }
  2406. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  2407. {
  2408. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2409. }
  2410. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  2411. {
  2412. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2413. }
  2414. else
  2415. {
  2416. return null;
  2417. }
  2418. }
  2419. /**
  2420. * Get the feed logo's URL
  2421. *
  2422. * RSS 0.9.0, 2.0, Atom 1.0, and feeds with iTunes RSS tags are allowed to
  2423. * have a "feed logo" URL. This points directly to the image itself.
  2424. *
  2425. * Uses `<itunes:image>`, `<atom:logo>`, `<atom:icon>`,
  2426. * `<image><title>` or `<image><dc:title>`
  2427. *
  2428. * @return string|null
  2429. */
  2430. public function get_image_url()
  2431. {
  2432. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'image'))
  2433. {
  2434. return $this->sanitize($return[0]['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI);
  2435. }
  2436. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'logo'))
  2437. {
  2438. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2439. }
  2440. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'icon'))
  2441. {
  2442. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2443. }
  2444. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'url'))
  2445. {
  2446. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2447. }
  2448. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'url'))
  2449. {
  2450. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2451. }
  2452. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2453. {
  2454. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2455. }
  2456. else
  2457. {
  2458. return null;
  2459. }
  2460. }
  2461. /**
  2462. * Get the feed logo's link
  2463. *
  2464. * RSS 0.9.0, 1.0 and 2.0 feeds are allowed to have a "feed logo" link. This
  2465. * points to a human-readable page that the image should link to.
  2466. *
  2467. * Uses `<itunes:image>`, `<atom:logo>`, `<atom:icon>`,
  2468. * `<image><title>` or `<image><dc:title>`
  2469. *
  2470. * @return string|null
  2471. */
  2472. public function get_image_link()
  2473. {
  2474. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link'))
  2475. {
  2476. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2477. }
  2478. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link'))
  2479. {
  2480. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2481. }
  2482. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link'))
  2483. {
  2484. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2485. }
  2486. else
  2487. {
  2488. return null;
  2489. }
  2490. }
  2491. /**
  2492. * Get the feed logo's link
  2493. *
  2494. * RSS 2.0 feeds are allowed to have a "feed logo" width.
  2495. *
  2496. * Uses `<image><width>` or defaults to 88.0 if no width is specified and
  2497. * the feed is an RSS 2.0 feed.
  2498. *
  2499. * @return int|float|null
  2500. */
  2501. public function get_image_width()
  2502. {
  2503. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'width'))
  2504. {
  2505. return round($return[0]['data']);
  2506. }
  2507. elseif ($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION && $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2508. {
  2509. return 88.0;
  2510. }
  2511. else
  2512. {
  2513. return null;
  2514. }
  2515. }
  2516. /**
  2517. * Get the feed logo's height
  2518. *
  2519. * RSS 2.0 feeds are allowed to have a "feed logo" height.
  2520. *
  2521. * Uses `<image><height>` or defaults to 31.0 if no height is specified and
  2522. * the feed is an RSS 2.0 feed.
  2523. *
  2524. * @return int|float|null
  2525. */
  2526. public function get_image_height()
  2527. {
  2528. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'height'))
  2529. {
  2530. return round($return[0]['data']);
  2531. }
  2532. elseif ($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION && $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2533. {
  2534. return 31.0;
  2535. }
  2536. else
  2537. {
  2538. return null;
  2539. }
  2540. }
  2541. /**
  2542. * Get the number of items in the feed
  2543. *
  2544. * This is well-suited for {@link http://php.net/for for()} loops with
  2545. * {@see get_item()}
  2546. *
  2547. * @param int $max Maximum value to return. 0 for no limit
  2548. * @return int Number of items in the feed
  2549. */
  2550. public function get_item_quantity($max = 0)
  2551. {
  2552. $max = (int) $max;
  2553. $qty = count($this->get_items());
  2554. if ($max === 0)
  2555. {
  2556. return $qty;
  2557. }
  2558. else
  2559. {
  2560. return ($qty > $max) ? $max : $qty;
  2561. }
  2562. }
  2563. /**
  2564. * Get a single item from the feed
  2565. *
  2566. * This is better suited for {@link http://php.net/for for()} loops, whereas
  2567. * {@see get_items()} is better suited for
  2568. * {@link http://php.net/foreach foreach()} loops.
  2569. *
  2570. * @see get_item_quantity()
  2571. * @since Beta 2
  2572. * @param int $key The item that you want to return. Remember that arrays begin with 0, not 1
  2573. * @return SimplePie_Item|null
  2574. */
  2575. public function get_item($key = 0)
  2576. {
  2577. $items = $this->get_items();
  2578. if (isset($items[$key]))
  2579. {
  2580. return $items[$key];
  2581. }
  2582. else
  2583. {
  2584. return null;
  2585. }
  2586. }
  2587. /**
  2588. * Get all items from the feed
  2589. *
  2590. * This is better suited for {@link http://php.net/for for()} loops, whereas
  2591. * {@see get_items()} is better suited for
  2592. * {@link http://php.net/foreach foreach()} loops.
  2593. *
  2594. * @see get_item_quantity
  2595. * @since Beta 2
  2596. * @param int $start Index to start at
  2597. * @param int $end Number of items to return. 0 for all items after `$start`
  2598. * @return array|null List of {@see SimplePie_Item} objects
  2599. */
  2600. public function get_items($start = 0, $end = 0)
  2601. {
  2602. if (!isset($this->data['items']))
  2603. {
  2604. if (!empty($this->multifeed_objects))
  2605. {
  2606. $this->data['items'] = SimplePie::merge_items($this->multifeed_objects, $start, $end, $this->item_limit);
  2607. }
  2608. else
  2609. {
  2610. $this->data['items'] = array();
  2611. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'entry'))
  2612. {
  2613. $keys = array_keys($items);
  2614. foreach ($keys as $key)
  2615. {
  2616. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2617. }
  2618. }
  2619. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'entry'))
  2620. {
  2621. $keys = array_keys($items);
  2622. foreach ($keys as $key)
  2623. {
  2624. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2625. }
  2626. }
  2627. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'item'))
  2628. {
  2629. $keys = array_keys($items);
  2630. foreach ($keys as $key)
  2631. {
  2632. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2633. }
  2634. }
  2635. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'item'))
  2636. {
  2637. $keys = array_keys($items);
  2638. foreach ($keys as $key)
  2639. {
  2640. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2641. }
  2642. }
  2643. if ($items = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'item'))
  2644. {
  2645. $keys = array_keys($items);
  2646. foreach ($keys as $key)
  2647. {
  2648. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2649. }
  2650. }
  2651. }
  2652. }
  2653. if (!empty($this->data['items']))
  2654. {
  2655. // If we want to order it by date, check if all items have a date, and then sort it
  2656. if ($this->order_by_date && empty($this->multifeed_objects))
  2657. {
  2658. if (!isset($this->data['ordered_items']))
  2659. {
  2660. $do_sort = true;
  2661. foreach ($this->data['items'] as $item)
  2662. {
  2663. if (!$item->get_date('U'))
  2664. {
  2665. $do_sort = false;
  2666. break;
  2667. }
  2668. }
  2669. $item = null;
  2670. $this->data['ordered_items'] = $this->data['items'];
  2671. if ($do_sort)
  2672. {
  2673. usort($this->data['ordered_items'], array(get_class($this), 'sort_items'));
  2674. }
  2675. }
  2676. $items = $this->data['ordered_items'];
  2677. }
  2678. else
  2679. {
  2680. $items = $this->data['items'];
  2681. }
  2682. // Slice the data as desired
  2683. if ($end === 0)
  2684. {
  2685. return array_slice($items, $start);
  2686. }
  2687. else
  2688. {
  2689. return array_slice($items, $start, $end);
  2690. }
  2691. }
  2692. else
  2693. {
  2694. return array();
  2695. }
  2696. }
  2697. /**
  2698. * Set the favicon handler
  2699. *
  2700. * @deprecated Use your own favicon handling instead
  2701. */
  2702. public function set_favicon_handler($page = false, $qs = 'i')
  2703. {
  2704. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2705. trigger_error('Favicon handling has been removed, please use your own handling', $level);
  2706. return false;
  2707. }
  2708. /**
  2709. * Get the favicon for the current feed
  2710. *
  2711. * @deprecated Use your own favicon handling instead
  2712. */
  2713. public function get_favicon()
  2714. {
  2715. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2716. trigger_error('Favicon handling has been removed, please use your own handling', $level);
  2717. if (($url = $this->get_link()) !== null)
  2718. {
  2719. return 'http://g.etfv.co/' . urlencode($url);
  2720. }
  2721. return false;
  2722. }
  2723. /**
  2724. * Magic method handler
  2725. *
  2726. * @param string $method Method name
  2727. * @param array $args Arguments to the method
  2728. * @return mixed
  2729. */
  2730. public function __call($method, $args)
  2731. {
  2732. if (strpos($method, 'subscribe_') === 0)
  2733. {
  2734. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2735. trigger_error('subscribe_*() has been deprecated, implement the callback yourself', $level);
  2736. return '';
  2737. }
  2738. if ($method === 'enable_xml_dump')
  2739. {
  2740. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2741. trigger_error('enable_xml_dump() has been deprecated, use get_raw_data() instead', $level);
  2742. return false;
  2743. }
  2744. $class = get_class($this);
  2745. $trace = debug_backtrace();
  2746. $file = $trace[0]['file'];
  2747. $line = $trace[0]['line'];
  2748. trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR);
  2749. }
  2750. /**
  2751. * Sorting callback for items
  2752. *
  2753. * @access private
  2754. * @param SimplePie $a
  2755. * @param SimplePie $b
  2756. * @return boolean
  2757. */
  2758. public static function sort_items($a, $b)
  2759. {
  2760. return $a->get_date('U') <= $b->get_date('U');
  2761. }
  2762. /**
  2763. * Merge items from several feeds into one
  2764. *
  2765. * If you're merging multiple feeds together, they need to all have dates
  2766. * for the items or else SimplePie will refuse to sort them.
  2767. *
  2768. * @link http://simplepie.org/wiki/tutorial/sort_multiple_feeds_by_time_and_date#if_feeds_require_separate_per-feed_settings
  2769. * @param array $urls List of SimplePie feed objects to merge
  2770. * @param int $start Starting item
  2771. * @param int $end Number of items to return
  2772. * @param int $limit Maximum number of items per feed
  2773. * @return array
  2774. */
  2775. public static function merge_items($urls, $start = 0, $end = 0, $limit = 0)
  2776. {
  2777. if (is_array($urls) && sizeof($urls) > 0)
  2778. {
  2779. $items = array();
  2780. foreach ($urls as $arg)
  2781. {
  2782. if ($arg instanceof SimplePie)
  2783. {
  2784. $items = array_merge($items, $arg->get_items(0, $limit));
  2785. }
  2786. else
  2787. {
  2788. trigger_error('Arguments must be SimplePie objects', E_USER_WARNING);
  2789. }
  2790. }
  2791. $do_sort = true;
  2792. foreach ($items as $item)
  2793. {
  2794. if (!$item->get_date('U'))
  2795. {
  2796. $do_sort = false;
  2797. break;
  2798. }
  2799. }
  2800. $item = null;
  2801. if ($do_sort)
  2802. {
  2803. usort($items, array(get_class($urls[0]), 'sort_items'));
  2804. }
  2805. if ($end === 0)
  2806. {
  2807. return array_slice($items, $start);
  2808. }
  2809. else
  2810. {
  2811. return array_slice($items, $start, $end);
  2812. }
  2813. }
  2814. else
  2815. {
  2816. trigger_error('Cannot merge zero SimplePie objects', E_USER_WARNING);
  2817. return array();
  2818. }
  2819. }
  2820. }
  2821. /**
  2822. * Manages all author-related data
  2823. *
  2824. * Used by {@see SimplePie_Item::get_author()} and {@see SimplePie::get_authors()}
  2825. *
  2826. * This class can be overloaded with {@see SimplePie::set_author_class()}
  2827. *
  2828. * @package SimplePie
  2829. * @subpackage API
  2830. */
  2831. class SimplePie_Author
  2832. {
  2833. /**
  2834. * Author's name
  2835. *
  2836. * @var string
  2837. * @see get_name()
  2838. */
  2839. var $name;
  2840. /**
  2841. * Author's link
  2842. *
  2843. * @var string
  2844. * @see get_link()
  2845. */
  2846. var $link;
  2847. /**
  2848. * Author's email address
  2849. *
  2850. * @var string
  2851. * @see get_email()
  2852. */
  2853. var $email;
  2854. /**
  2855. * Constructor, used to input the data
  2856. *
  2857. * @param string $name
  2858. * @param string $link
  2859. * @param string $email
  2860. */
  2861. public function __construct($name = null, $link = null, $email = null)
  2862. {
  2863. $this->name = $name;
  2864. $this->link = $link;
  2865. $this->email = $email;
  2866. }
  2867. /**
  2868. * String-ified version
  2869. *
  2870. * @return string
  2871. */
  2872. public function __toString()
  2873. {
  2874. // There is no $this->data here
  2875. return md5(serialize($this));
  2876. }
  2877. /**
  2878. * Author's name
  2879. *
  2880. * @return string|null
  2881. */
  2882. public function get_name()
  2883. {
  2884. if ($this->name !== null)
  2885. {
  2886. return $this->name;
  2887. }
  2888. else
  2889. {
  2890. return null;
  2891. }
  2892. }
  2893. /**
  2894. * Author's link
  2895. *
  2896. * @return string|null
  2897. */
  2898. public function get_link()
  2899. {
  2900. if ($this->link !== null)
  2901. {
  2902. return $this->link;
  2903. }
  2904. else
  2905. {
  2906. return null;
  2907. }
  2908. }
  2909. /**
  2910. * Author's email address
  2911. *
  2912. * @return string|null
  2913. */
  2914. public function get_email()
  2915. {
  2916. if ($this->email !== null)
  2917. {
  2918. return $this->email;
  2919. }
  2920. else
  2921. {
  2922. return null;
  2923. }
  2924. }
  2925. }
  2926. /**
  2927. * Base for cache objects
  2928. *
  2929. * Classes to be used with {@see SimplePie_Cache::register()} are expected
  2930. * to implement this interface.
  2931. *
  2932. * @package SimplePie
  2933. * @subpackage Caching
  2934. */
  2935. interface SimplePie_Cache_Base
  2936. {
  2937. /**
  2938. * Feed cache type
  2939. *
  2940. * @var string
  2941. */
  2942. const TYPE_FEED = 'spc';
  2943. /**
  2944. * Image cache type
  2945. *
  2946. * @var string
  2947. */
  2948. const TYPE_IMAGE = 'spi';
  2949. /**
  2950. * Create a new cache object
  2951. *
  2952. * @param string $location Location string (from SimplePie::$cache_location)
  2953. * @param string $name Unique ID for the cache
  2954. * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  2955. */
  2956. public function __construct($location, $name, $type);
  2957. /**
  2958. * Save data to the cache
  2959. *
  2960. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  2961. * @return bool Successfulness
  2962. */
  2963. public function save($data);
  2964. /**
  2965. * Retrieve the data saved to the cache
  2966. *
  2967. * @return array Data for SimplePie::$data
  2968. */
  2969. public function load();
  2970. /**
  2971. * Retrieve the last modified time for the cache
  2972. *
  2973. * @return int Timestamp
  2974. */
  2975. public function mtime();
  2976. /**
  2977. * Set the last modified time to the current time
  2978. *
  2979. * @return bool Success status
  2980. */
  2981. public function touch();
  2982. /**
  2983. * Remove the cache
  2984. *
  2985. * @return bool Success status
  2986. */
  2987. public function unlink();
  2988. }
  2989. /**
  2990. * Base class for database-based caches
  2991. *
  2992. * @package SimplePie
  2993. * @subpackage Caching
  2994. */
  2995. abstract class SimplePie_Cache_DB implements SimplePie_Cache_Base
  2996. {
  2997. /**
  2998. * Helper for database conversion
  2999. *
  3000. * Converts a given {@see SimplePie} object into data to be stored
  3001. *
  3002. * @param SimplePie $data
  3003. * @return array First item is the serialized data for storage, second item is the unique ID for this item
  3004. */
  3005. protected static function prepare_simplepie_object_for_cache($data)
  3006. {
  3007. $items = $data->get_items();
  3008. $items_by_id = array();
  3009. if (!empty($items))
  3010. {
  3011. foreach ($items as $item)
  3012. {
  3013. $items_by_id[$item->get_id()] = $item;
  3014. }
  3015. if (count($items_by_id) !== count($items))
  3016. {
  3017. $items_by_id = array();
  3018. foreach ($items as $item)
  3019. {
  3020. $items_by_id[$item->get_id(true)] = $item;
  3021. }
  3022. }
  3023. if (isset($data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]))
  3024. {
  3025. $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0];
  3026. }
  3027. elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]))
  3028. {
  3029. $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0];
  3030. }
  3031. elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]))
  3032. {
  3033. $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0];
  3034. }
  3035. elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['channel'][0]))
  3036. {
  3037. $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['channel'][0];
  3038. }
  3039. else
  3040. {
  3041. $channel = null;
  3042. }
  3043. if ($channel !== null)
  3044. {
  3045. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry']))
  3046. {
  3047. unset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry']);
  3048. }
  3049. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['entry']))
  3050. {
  3051. unset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['entry']);
  3052. }
  3053. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item']))
  3054. {
  3055. unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item']);
  3056. }
  3057. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item']))
  3058. {
  3059. unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item']);
  3060. }
  3061. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_20]['item']))
  3062. {
  3063. unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_20]['item']);
  3064. }
  3065. }
  3066. if (isset($data->data['items']))
  3067. {
  3068. unset($data->data['items']);
  3069. }
  3070. if (isset($data->data['ordered_items']))
  3071. {
  3072. unset($data->data['ordered_items']);
  3073. }
  3074. }
  3075. return array(serialize($data->data), $items_by_id);
  3076. }
  3077. }
  3078. /**
  3079. * Caches data to the filesystem
  3080. *
  3081. * @package SimplePie
  3082. * @subpackage Caching
  3083. */
  3084. class SimplePie_Cache_File implements SimplePie_Cache_Base
  3085. {
  3086. /**
  3087. * Location string
  3088. *
  3089. * @see SimplePie::$cache_location
  3090. * @var string
  3091. */
  3092. protected $location;
  3093. /**
  3094. * Filename
  3095. *
  3096. * @var string
  3097. */
  3098. protected $filename;
  3099. /**
  3100. * File extension
  3101. *
  3102. * @var string
  3103. */
  3104. protected $extension;
  3105. /**
  3106. * File path
  3107. *
  3108. * @var string
  3109. */
  3110. protected $name;
  3111. /**
  3112. * Create a new cache object
  3113. *
  3114. * @param string $location Location string (from SimplePie::$cache_location)
  3115. * @param string $name Unique ID for the cache
  3116. * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  3117. */
  3118. public function __construct($location, $name, $type)
  3119. {
  3120. $this->location = $location;
  3121. $this->filename = $name;
  3122. $this->extension = $type;
  3123. $this->name = "$this->location/$this->filename.$this->extension";
  3124. }
  3125. /**
  3126. * Save data to the cache
  3127. *
  3128. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  3129. * @return bool Successfulness
  3130. */
  3131. public function save($data)
  3132. {
  3133. if (file_exists($this->name) && is_writeable($this->name) || file_exists($this->location) && is_writeable($this->location))
  3134. {
  3135. if ($data instanceof SimplePie)
  3136. {
  3137. $data = $data->data;
  3138. }
  3139. $data = serialize($data);
  3140. return (bool) file_put_contents($this->name, $data);
  3141. }
  3142. return false;
  3143. }
  3144. /**
  3145. * Retrieve the data saved to the cache
  3146. *
  3147. * @return array Data for SimplePie::$data
  3148. */
  3149. public function load()
  3150. {
  3151. if (file_exists($this->name) && is_readable($this->name))
  3152. {
  3153. return unserialize(file_get_contents($this->name));
  3154. }
  3155. return false;
  3156. }
  3157. /**
  3158. * Retrieve the last modified time for the cache
  3159. *
  3160. * @return int Timestamp
  3161. */
  3162. public function mtime()
  3163. {
  3164. if (file_exists($this->name))
  3165. {
  3166. return filemtime($this->name);
  3167. }
  3168. return false;
  3169. }
  3170. /**
  3171. * Set the last modified time to the current time
  3172. *
  3173. * @return bool Success status
  3174. */
  3175. public function touch()
  3176. {
  3177. if (file_exists($this->name))
  3178. {
  3179. return touch($this->name);
  3180. }
  3181. return false;
  3182. }
  3183. /**
  3184. * Remove the cache
  3185. *
  3186. * @return bool Success status
  3187. */
  3188. public function unlink()
  3189. {
  3190. if (file_exists($this->name))
  3191. {
  3192. return unlink($this->name);
  3193. }
  3194. return false;
  3195. }
  3196. }
  3197. /**
  3198. * Caches data to memcache
  3199. *
  3200. * Registered for URLs with the "memcache" protocol
  3201. *
  3202. * For example, `memcache://localhost:11211/?timeout=3600&prefix=sp_` will
  3203. * connect to memcache on `localhost` on port 11211. All tables will be
  3204. * prefixed with `sp_` and data will expire after 3600 seconds
  3205. *
  3206. * @package SimplePie
  3207. * @subpackage Caching
  3208. * @uses Memcache
  3209. */
  3210. class SimplePie_Cache_Memcache implements SimplePie_Cache_Base
  3211. {
  3212. /**
  3213. * Memcache instance
  3214. *
  3215. * @var Memcache
  3216. */
  3217. protected $cache;
  3218. /**
  3219. * Options
  3220. *
  3221. * @var array
  3222. */
  3223. protected $options;
  3224. /**
  3225. * Cache name
  3226. *
  3227. * @var string
  3228. */
  3229. protected $name;
  3230. /**
  3231. * Create a new cache object
  3232. *
  3233. * @param string $location Location string (from SimplePie::$cache_location)
  3234. * @param string $name Unique ID for the cache
  3235. * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  3236. */
  3237. public function __construct($location, $name, $type)
  3238. {
  3239. $this->options = array(
  3240. 'host' => '127.0.0.1',
  3241. 'port' => 11211,
  3242. 'extras' => array(
  3243. 'timeout' => 3600, // one hour
  3244. 'prefix' => 'simplepie_',
  3245. ),
  3246. );
  3247. $parsed = SimplePie_Cache::parse_URL($location);
  3248. $this->options['host'] = empty($parsed['host']) ? $this->options['host'] : $parsed['host'];
  3249. $this->options['port'] = empty($parsed['port']) ? $this->options['port'] : $parsed['port'];
  3250. $this->options['extras'] = array_merge($this->options['extras'], $parsed['extras']);
  3251. $this->name = $this->options['extras']['prefix'] . md5("$name:$type");
  3252. $this->cache = new Memcache();
  3253. $this->cache->addServer($this->options['host'], (int) $this->options['port']);
  3254. }
  3255. /**
  3256. * Save data to the cache
  3257. *
  3258. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  3259. * @return bool Successfulness
  3260. */
  3261. public function save($data)
  3262. {
  3263. if ($data instanceof SimplePie)
  3264. {
  3265. $data = $data->data;
  3266. }
  3267. return $this->cache->set($this->name, serialize($data), MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']);
  3268. }
  3269. /**
  3270. * Retrieve the data saved to the cache
  3271. *
  3272. * @return array Data for SimplePie::$data
  3273. */
  3274. public function load()
  3275. {
  3276. $data = $this->cache->get($this->name);
  3277. if ($data !== false)
  3278. {
  3279. return unserialize($data);
  3280. }
  3281. return false;
  3282. }
  3283. /**
  3284. * Retrieve the last modified time for the cache
  3285. *
  3286. * @return int Timestamp
  3287. */
  3288. public function mtime()
  3289. {
  3290. $data = $this->cache->get($this->name);
  3291. if ($data !== false)
  3292. {
  3293. // essentially ignore the mtime because Memcache expires on it's own
  3294. return time();
  3295. }
  3296. return false;
  3297. }
  3298. /**
  3299. * Set the last modified time to the current time
  3300. *
  3301. * @return bool Success status
  3302. */
  3303. public function touch()
  3304. {
  3305. $data = $this->cache->get($this->name);
  3306. if ($data !== false)
  3307. {
  3308. return $this->cache->set($this->name, $data, MEMCACHE_COMPRESSED, (int) $this->duration);
  3309. }
  3310. return false;
  3311. }
  3312. /**
  3313. * Remove the cache
  3314. *
  3315. * @return bool Success status
  3316. */
  3317. public function unlink()
  3318. {
  3319. return $this->cache->delete($this->name, 0);
  3320. }
  3321. }
  3322. /**
  3323. * Caches data to a MySQL database
  3324. *
  3325. * Registered for URLs with the "mysql" protocol
  3326. *
  3327. * For example, `mysql://root:password@localhost:3306/mydb?prefix=sp_` will
  3328. * connect to the `mydb` database on `localhost` on port 3306, with the user
  3329. * `root` and the password `password`. All tables will be prefixed with `sp_`
  3330. *
  3331. * @package SimplePie
  3332. * @subpackage Caching
  3333. */
  3334. class SimplePie_Cache_MySQL extends SimplePie_Cache_DB
  3335. {
  3336. /**
  3337. * PDO instance
  3338. *
  3339. * @var PDO
  3340. */
  3341. protected $mysql;
  3342. /**
  3343. * Options
  3344. *
  3345. * @var array
  3346. */
  3347. protected $options;
  3348. /**
  3349. * Cache ID
  3350. *
  3351. * @var string
  3352. */
  3353. protected $id;
  3354. /**
  3355. * Create a new cache object
  3356. *
  3357. * @param string $location Location string (from SimplePie::$cache_location)
  3358. * @param string $name Unique ID for the cache
  3359. * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  3360. */
  3361. public function __construct($location, $name, $type)
  3362. {
  3363. $this->options = array(
  3364. 'user' => null,
  3365. 'pass' => null,
  3366. 'host' => '127.0.0.1',
  3367. 'port' => '3306',
  3368. 'path' => '',
  3369. 'extras' => array(
  3370. 'prefix' => '',
  3371. ),
  3372. );
  3373. $this->options = array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));
  3374. // Path is prefixed with a "/"
  3375. $this->options['dbname'] = substr($this->options['path'], 1);
  3376. try
  3377. {
  3378. $this->mysql = new PDO("mysql:dbname={$this->options['dbname']};host={$this->options['host']};port={$this->options['port']}", $this->options['user'], $this->options['pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
  3379. }
  3380. catch (PDOException $e)
  3381. {
  3382. $this->mysql = null;
  3383. return;
  3384. }
  3385. $this->id = $name . $type;
  3386. if (!$query = $this->mysql->query('SHOW TABLES'))
  3387. {
  3388. $this->mysql = null;
  3389. return;
  3390. }
  3391. $db = array();
  3392. while ($row = $query->fetchColumn())
  3393. {
  3394. $db[] = $row;
  3395. }
  3396. if (!in_array($this->options['extras']['prefix'] . 'cache_data', $db))
  3397. {
  3398. $query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'cache_data` (`id` TEXT CHARACTER SET utf8 NOT NULL, `items` SMALLINT NOT NULL DEFAULT 0, `data` BLOB NOT NULL, `mtime` INT UNSIGNED NOT NULL, UNIQUE (`id`(125)))');
  3399. if ($query === false)
  3400. {
  3401. $this->mysql = null;
  3402. }
  3403. }
  3404. if (!in_array($this->options['extras']['prefix'] . 'items', $db))
  3405. {
  3406. $query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'items` (`feed_id` TEXT CHARACTER SET utf8 NOT NULL, `id` TEXT CHARACTER SET utf8 NOT NULL, `data` TEXT CHARACTER SET utf8 NOT NULL, `posted` INT UNSIGNED NOT NULL, INDEX `feed_id` (`feed_id`(125)))');
  3407. if ($query === false)
  3408. {
  3409. $this->mysql = null;
  3410. }
  3411. }
  3412. }
  3413. /**
  3414. * Save data to the cache
  3415. *
  3416. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  3417. * @return bool Successfulness
  3418. */
  3419. public function save($data)
  3420. {
  3421. if ($this->mysql === null)
  3422. {
  3423. return false;
  3424. }
  3425. if ($data instanceof SimplePie)
  3426. {
  3427. $data = clone $data;
  3428. $prepared = self::prepare_simplepie_object_for_cache($data);
  3429. $query = $this->mysql->prepare('SELECT COUNT(*) FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed');
  3430. $query->bindValue(':feed', $this->id);
  3431. if ($query->execute())
  3432. {
  3433. if ($query->fetchColumn() > 0)
  3434. {
  3435. $items = count($prepared[1]);
  3436. if ($items)
  3437. {
  3438. $sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `items` = :items, `data` = :data, `mtime` = :time WHERE `id` = :feed';
  3439. $query = $this->mysql->prepare($sql);
  3440. $query->bindValue(':items', $items);
  3441. }
  3442. else
  3443. {
  3444. $sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `data` = :data, `mtime` = :time WHERE `id` = :feed';
  3445. $query = $this->mysql->prepare($sql);
  3446. }
  3447. $query->bindValue(':data', $prepared[0]);
  3448. $query->bindValue(':time', time());
  3449. $query->bindValue(':feed', $this->id);
  3450. if (!$query->execute())
  3451. {
  3452. return false;
  3453. }
  3454. }
  3455. else
  3456. {
  3457. $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:feed, :count, :data, :time)');
  3458. $query->bindValue(':feed', $this->id);
  3459. $query->bindValue(':count', count($prepared[1]));
  3460. $query->bindValue(':data', $prepared[0]);
  3461. $query->bindValue(':time', time());
  3462. if (!$query->execute())
  3463. {
  3464. return false;
  3465. }
  3466. }
  3467. $ids = array_keys($prepared[1]);
  3468. if (!empty($ids))
  3469. {
  3470. foreach ($ids as $id)
  3471. {
  3472. $database_ids[] = $this->mysql->quote($id);
  3473. }
  3474. $query = $this->mysql->prepare('SELECT `id` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `id` = ' . implode(' OR `id` = ', $database_ids) . ' AND `feed_id` = :feed');
  3475. $query->bindValue(':feed', $this->id);
  3476. if ($query->execute())
  3477. {
  3478. $existing_ids = array();
  3479. while ($row = $query->fetchColumn())
  3480. {
  3481. $existing_ids[] = $row;
  3482. }
  3483. $new_ids = array_diff($ids, $existing_ids);
  3484. foreach ($new_ids as $new_id)
  3485. {
  3486. if (!($date = $prepared[1][$new_id]->get_date('U')))
  3487. {
  3488. $date = time();
  3489. }
  3490. $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'items` (`feed_id`, `id`, `data`, `posted`) VALUES(:feed, :id, :data, :date)');
  3491. $query->bindValue(':feed', $this->id);
  3492. $query->bindValue(':id', $new_id);
  3493. $query->bindValue(':data', serialize($prepared[1][$new_id]->data));
  3494. $query->bindValue(':date', $date);
  3495. if (!$query->execute())
  3496. {
  3497. return false;
  3498. }
  3499. }
  3500. return true;
  3501. }
  3502. }
  3503. else
  3504. {
  3505. return true;
  3506. }
  3507. }
  3508. }
  3509. else
  3510. {
  3511. $query = $this->mysql->prepare('SELECT `id` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed');
  3512. $query->bindValue(':feed', $this->id);
  3513. if ($query->execute())
  3514. {
  3515. if ($query->rowCount() > 0)
  3516. {
  3517. $query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `items` = 0, `data` = :data, `mtime` = :time WHERE `id` = :feed');
  3518. $query->bindValue(':data', serialize($data));
  3519. $query->bindValue(':time', time());
  3520. $query->bindValue(':feed', $this->id);
  3521. if ($this->execute())
  3522. {
  3523. return true;
  3524. }
  3525. }
  3526. else
  3527. {
  3528. $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:id, 0, :data, :time)');
  3529. $query->bindValue(':id', $this->id);
  3530. $query->bindValue(':data', serialize($data));
  3531. $query->bindValue(':time', time());
  3532. if ($query->execute())
  3533. {
  3534. return true;
  3535. }
  3536. }
  3537. }
  3538. }
  3539. return false;
  3540. }
  3541. /**
  3542. * Retrieve the data saved to the cache
  3543. *
  3544. * @return array Data for SimplePie::$data
  3545. */
  3546. public function load()
  3547. {
  3548. if ($this->mysql === null)
  3549. {
  3550. return false;
  3551. }
  3552. $query = $this->mysql->prepare('SELECT `items`, `data` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
  3553. $query->bindValue(':id', $this->id);
  3554. if ($query->execute() && ($row = $query->fetch()))
  3555. {
  3556. $data = unserialize($row[1]);
  3557. if (isset($this->options['items'][0]))
  3558. {
  3559. $items = (int) $this->options['items'][0];
  3560. }
  3561. else
  3562. {
  3563. $items = (int) $row[0];
  3564. }
  3565. if ($items !== 0)
  3566. {
  3567. if (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]))
  3568. {
  3569. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0];
  3570. }
  3571. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]))
  3572. {
  3573. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0];
  3574. }
  3575. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]))
  3576. {
  3577. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0];
  3578. }
  3579. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]))
  3580. {
  3581. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0];
  3582. }
  3583. else
  3584. {
  3585. $feed = null;
  3586. }
  3587. if ($feed !== null)
  3588. {
  3589. $sql = 'SELECT `data` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :feed ORDER BY `posted` DESC';
  3590. if ($items > 0)
  3591. {
  3592. $sql .= ' LIMIT ' . $items;
  3593. }
  3594. $query = $this->mysql->prepare($sql);
  3595. $query->bindValue(':feed', $this->id);
  3596. if ($query->execute())
  3597. {
  3598. while ($row = $query->fetchColumn())
  3599. {
  3600. $feed['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry'][] = unserialize($row);
  3601. }
  3602. }
  3603. else
  3604. {
  3605. return false;
  3606. }
  3607. }
  3608. }
  3609. return $data;
  3610. }
  3611. return false;
  3612. }
  3613. /**
  3614. * Retrieve the last modified time for the cache
  3615. *
  3616. * @return int Timestamp
  3617. */
  3618. public function mtime()
  3619. {
  3620. if ($this->mysql === null)
  3621. {
  3622. return false;
  3623. }
  3624. $query = $this->mysql->prepare('SELECT `mtime` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
  3625. $query->bindValue(':id', $this->id);
  3626. if ($query->execute() && ($time = $query->fetchColumn()))
  3627. {
  3628. return $time;
  3629. }
  3630. else
  3631. {
  3632. return false;
  3633. }
  3634. }
  3635. /**
  3636. * Set the last modified time to the current time
  3637. *
  3638. * @return bool Success status
  3639. */
  3640. public function touch()
  3641. {
  3642. if ($this->mysql === null)
  3643. {
  3644. return false;
  3645. }
  3646. $query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `mtime` = :time WHERE `id` = :id');
  3647. $query->bindValue(':time', time());
  3648. $query->bindValue(':id', $this->id);
  3649. if ($query->execute() && $query->rowCount() > 0)
  3650. {
  3651. return true;
  3652. }
  3653. else
  3654. {
  3655. return false;
  3656. }
  3657. }
  3658. /**
  3659. * Remove the cache
  3660. *
  3661. * @return bool Success status
  3662. */
  3663. public function unlink()
  3664. {
  3665. if ($this->mysql === null)
  3666. {
  3667. return false;
  3668. }
  3669. $query = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
  3670. $query->bindValue(':id', $this->id);
  3671. $query2 = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :id');
  3672. $query2->bindValue(':id', $this->id);
  3673. if ($query->execute() && $query2->execute())
  3674. {
  3675. return true;
  3676. }
  3677. else
  3678. {
  3679. return false;
  3680. }
  3681. }
  3682. }
  3683. /**
  3684. * Used to create cache objects
  3685. *
  3686. * This class can be overloaded with {@see SimplePie::set_cache_class()},
  3687. * although the preferred way is to create your own handler
  3688. * via {@see register()}
  3689. *
  3690. * @package SimplePie
  3691. * @subpackage Caching
  3692. */
  3693. class SimplePie_Cache
  3694. {
  3695. /**
  3696. * Cache handler classes
  3697. *
  3698. * These receive 3 parameters to their constructor, as documented in
  3699. * {@see register()}
  3700. * @var array
  3701. */
  3702. protected static $handlers = array(
  3703. 'mysql' => 'SimplePie_Cache_MySQL',
  3704. 'memcache' => 'SimplePie_Cache_Memcache',
  3705. );
  3706. /**
  3707. * Don't call the constructor. Please.
  3708. */
  3709. private function __construct() { }
  3710. /**
  3711. * Create a new SimplePie_Cache object
  3712. *
  3713. * @param string $location URL location (scheme is used to determine handler)
  3714. * @param string $filename Unique identifier for cache object
  3715. * @param string $extension 'spi' or 'spc'
  3716. * @return SimplePie_Cache_Base Type of object depends on scheme of `$location`
  3717. */
  3718. public static function get_handler($location, $filename, $extension)
  3719. {
  3720. $type = explode(':', $location, 2);
  3721. $type = $type[0];
  3722. if (!empty(self::$handlers[$type]))
  3723. {
  3724. $class = self::$handlers[$type];
  3725. return new $class($location, $filename, $extension);
  3726. }
  3727. return new SimplePie_Cache_File($location, $filename, $extension);
  3728. }
  3729. /**
  3730. * Create a new SimplePie_Cache object
  3731. *
  3732. * @deprecated Use {@see get_handler} instead
  3733. */
  3734. public function create($location, $filename, $extension)
  3735. {
  3736. trigger_error('Cache::create() has been replaced with Cache::get_handler(). Switch to the registry system to use this.', E_USER_DEPRECATED);
  3737. return self::get_handler($location, $filename, $extension);
  3738. }
  3739. /**
  3740. * Register a handler
  3741. *
  3742. * @param string $type DSN type to register for
  3743. * @param string $class Name of handler class. Must implement SimplePie_Cache_Base
  3744. */
  3745. public static function register($type, $class)
  3746. {
  3747. self::$handlers[$type] = $class;
  3748. }
  3749. /**
  3750. * Parse a URL into an array
  3751. *
  3752. * @param string $url
  3753. * @return array
  3754. */
  3755. public static function parse_URL($url)
  3756. {
  3757. $params = parse_url($url);
  3758. $params['extras'] = array();
  3759. if (isset($params['query']))
  3760. {
  3761. parse_str($params['query'], $params['extras']);
  3762. }
  3763. return $params;
  3764. }
  3765. }
  3766. /**
  3767. * Handles `<media:text>` captions as defined in Media RSS.
  3768. *
  3769. * Used by {@see SimplePie_Enclosure::get_caption()} and {@see SimplePie_Enclosure::get_captions()}
  3770. *
  3771. * This class can be overloaded with {@see SimplePie::set_caption_class()}
  3772. *
  3773. * @package SimplePie
  3774. * @subpackage API
  3775. */
  3776. class SimplePie_Caption
  3777. {
  3778. /**
  3779. * Content type
  3780. *
  3781. * @var string
  3782. * @see get_type()
  3783. */
  3784. var $type;
  3785. /**
  3786. * Language
  3787. *
  3788. * @var string
  3789. * @see get_language()
  3790. */
  3791. var $lang;
  3792. /**
  3793. * Start time
  3794. *
  3795. * @var string
  3796. * @see get_starttime()
  3797. */
  3798. var $startTime;
  3799. /**
  3800. * End time
  3801. *
  3802. * @var string
  3803. * @see get_endtime()
  3804. */
  3805. var $endTime;
  3806. /**
  3807. * Caption text
  3808. *
  3809. * @var string
  3810. * @see get_text()
  3811. */
  3812. var $text;
  3813. /**
  3814. * Constructor, used to input the data
  3815. *
  3816. * For documentation on all the parameters, see the corresponding
  3817. * properties and their accessors
  3818. */
  3819. public function __construct($type = null, $lang = null, $startTime = null, $endTime = null, $text = null)
  3820. {
  3821. $this->type = $type;
  3822. $this->lang = $lang;
  3823. $this->startTime = $startTime;
  3824. $this->endTime = $endTime;
  3825. $this->text = $text;
  3826. }
  3827. /**
  3828. * String-ified version
  3829. *
  3830. * @return string
  3831. */
  3832. public function __toString()
  3833. {
  3834. // There is no $this->data here
  3835. return md5(serialize($this));
  3836. }
  3837. /**
  3838. * Get the end time
  3839. *
  3840. * @return string|null Time in the format 'hh:mm:ss.SSS'
  3841. */
  3842. public function get_endtime()
  3843. {
  3844. if ($this->endTime !== null)
  3845. {
  3846. return $this->endTime;
  3847. }
  3848. else
  3849. {
  3850. return null;
  3851. }
  3852. }
  3853. /**
  3854. * Get the language
  3855. *
  3856. * @link http://tools.ietf.org/html/rfc3066
  3857. * @return string|null Language code as per RFC 3066
  3858. */
  3859. public function get_language()
  3860. {
  3861. if ($this->lang !== null)
  3862. {
  3863. return $this->lang;
  3864. }
  3865. else
  3866. {
  3867. return null;
  3868. }
  3869. }
  3870. /**
  3871. * Get the start time
  3872. *
  3873. * @return string|null Time in the format 'hh:mm:ss.SSS'
  3874. */
  3875. public function get_starttime()
  3876. {
  3877. if ($this->startTime !== null)
  3878. {
  3879. return $this->startTime;
  3880. }
  3881. else
  3882. {
  3883. return null;
  3884. }
  3885. }
  3886. /**
  3887. * Get the text of the caption
  3888. *
  3889. * @return string|null
  3890. */
  3891. public function get_text()
  3892. {
  3893. if ($this->text !== null)
  3894. {
  3895. return $this->text;
  3896. }
  3897. else
  3898. {
  3899. return null;
  3900. }
  3901. }
  3902. /**
  3903. * Get the content type (not MIME type)
  3904. *
  3905. * @return string|null Either 'text' or 'html'
  3906. */
  3907. public function get_type()
  3908. {
  3909. if ($this->type !== null)
  3910. {
  3911. return $this->type;
  3912. }
  3913. else
  3914. {
  3915. return null;
  3916. }
  3917. }
  3918. }
  3919. /**
  3920. * Manages all category-related data
  3921. *
  3922. * Used by {@see SimplePie_Item::get_category()} and {@see SimplePie_Item::get_categories()}
  3923. *
  3924. * This class can be overloaded with {@see SimplePie::set_category_class()}
  3925. *
  3926. * @package SimplePie
  3927. * @subpackage API
  3928. */
  3929. class SimplePie_Category
  3930. {
  3931. /**
  3932. * Category identifier
  3933. *
  3934. * @var string
  3935. * @see get_term
  3936. */
  3937. var $term;
  3938. /**
  3939. * Categorization scheme identifier
  3940. *
  3941. * @var string
  3942. * @see get_scheme()
  3943. */
  3944. var $scheme;
  3945. /**
  3946. * Human readable label
  3947. *
  3948. * @var string
  3949. * @see get_label()
  3950. */
  3951. var $label;
  3952. /**
  3953. * Constructor, used to input the data
  3954. *
  3955. * @param string $term
  3956. * @param string $scheme
  3957. * @param string $label
  3958. */
  3959. public function __construct($term = null, $scheme = null, $label = null)
  3960. {
  3961. $this->term = $term;
  3962. $this->scheme = $scheme;
  3963. $this->label = $label;
  3964. }
  3965. /**
  3966. * String-ified version
  3967. *
  3968. * @return string
  3969. */
  3970. public function __toString()
  3971. {
  3972. // There is no $this->data here
  3973. return md5(serialize($this));
  3974. }
  3975. /**
  3976. * Get the category identifier
  3977. *
  3978. * @return string|null
  3979. */
  3980. public function get_term()
  3981. {
  3982. if ($this->term !== null)
  3983. {
  3984. return $this->term;
  3985. }
  3986. else
  3987. {
  3988. return null;
  3989. }
  3990. }
  3991. /**
  3992. * Get the categorization scheme identifier
  3993. *
  3994. * @return string|null
  3995. */
  3996. public function get_scheme()
  3997. {
  3998. if ($this->scheme !== null)
  3999. {
  4000. return $this->scheme;
  4001. }
  4002. else
  4003. {
  4004. return null;
  4005. }
  4006. }
  4007. /**
  4008. * Get the human readable label
  4009. *
  4010. * @return string|null
  4011. */
  4012. public function get_label()
  4013. {
  4014. if ($this->label !== null)
  4015. {
  4016. return $this->label;
  4017. }
  4018. else
  4019. {
  4020. return $this->get_term();
  4021. }
  4022. }
  4023. }
  4024. /**
  4025. * Content-type sniffing
  4026. *
  4027. * Based on the rules in http://tools.ietf.org/html/draft-abarth-mime-sniff-06
  4028. *
  4029. * This is used since we can't always trust Content-Type headers, and is based
  4030. * upon the HTML5 parsing rules.
  4031. *
  4032. *
  4033. * This class can be overloaded with {@see SimplePie::set_content_type_sniffer_class()}
  4034. *
  4035. * @package SimplePie
  4036. * @subpackage HTTP
  4037. */
  4038. class SimplePie_Content_Type_Sniffer
  4039. {
  4040. /**
  4041. * File object
  4042. *
  4043. * @var SimplePie_File
  4044. */
  4045. var $file;
  4046. /**
  4047. * Create an instance of the class with the input file
  4048. *
  4049. * @param SimplePie_Content_Type_Sniffer $file Input file
  4050. */
  4051. public function __construct($file)
  4052. {
  4053. $this->file = $file;
  4054. }
  4055. /**
  4056. * Get the Content-Type of the specified file
  4057. *
  4058. * @return string Actual Content-Type
  4059. */
  4060. public function get_type()
  4061. {
  4062. if (isset($this->file->headers['content-type']))
  4063. {
  4064. if (!isset($this->file->headers['content-encoding'])
  4065. && ($this->file->headers['content-type'] === 'text/plain'
  4066. || $this->file->headers['content-type'] === 'text/plain; charset=ISO-8859-1'
  4067. || $this->file->headers['content-type'] === 'text/plain; charset=iso-8859-1'
  4068. || $this->file->headers['content-type'] === 'text/plain; charset=UTF-8'))
  4069. {
  4070. return $this->text_or_binary();
  4071. }
  4072. if (($pos = strpos($this->file->headers['content-type'], ';')) !== false)
  4073. {
  4074. $official = substr($this->file->headers['content-type'], 0, $pos);
  4075. }
  4076. else
  4077. {
  4078. $official = $this->file->headers['content-type'];
  4079. }
  4080. $official = trim(strtolower($official));
  4081. if ($official === 'unknown/unknown'
  4082. || $official === 'application/unknown')
  4083. {
  4084. return $this->unknown();
  4085. }
  4086. elseif (substr($official, -4) === '+xml'
  4087. || $official === 'text/xml'
  4088. || $official === 'application/xml')
  4089. {
  4090. return $official;
  4091. }
  4092. elseif (substr($official, 0, 6) === 'image/')
  4093. {
  4094. if ($return = $this->image())
  4095. {
  4096. return $return;
  4097. }
  4098. else
  4099. {
  4100. return $official;
  4101. }
  4102. }
  4103. elseif ($official === 'text/html')
  4104. {
  4105. return $this->feed_or_html();
  4106. }
  4107. else
  4108. {
  4109. return $official;
  4110. }
  4111. }
  4112. else
  4113. {
  4114. return $this->unknown();
  4115. }
  4116. }
  4117. /**
  4118. * Sniff text or binary
  4119. *
  4120. * @return string Actual Content-Type
  4121. */
  4122. public function text_or_binary()
  4123. {
  4124. if (substr($this->file->body, 0, 2) === "\xFE\xFF"
  4125. || substr($this->file->body, 0, 2) === "\xFF\xFE"
  4126. || substr($this->file->body, 0, 4) === "\x00\x00\xFE\xFF"
  4127. || substr($this->file->body, 0, 3) === "\xEF\xBB\xBF")
  4128. {
  4129. return 'text/plain';
  4130. }
  4131. elseif (preg_match('/[\x00-\x08\x0E-\x1A\x1C-\x1F]/', $this->file->body))
  4132. {
  4133. return 'application/octect-stream';
  4134. }
  4135. else
  4136. {
  4137. return 'text/plain';
  4138. }
  4139. }
  4140. /**
  4141. * Sniff unknown
  4142. *
  4143. * @return string Actual Content-Type
  4144. */
  4145. public function unknown()
  4146. {
  4147. $ws = strspn($this->file->body, "\x09\x0A\x0B\x0C\x0D\x20");
  4148. if (strtolower(substr($this->file->body, $ws, 14)) === '<!doctype html'
  4149. || strtolower(substr($this->file->body, $ws, 5)) === '<html'
  4150. || strtolower(substr($this->file->body, $ws, 7)) === '<script')
  4151. {
  4152. return 'text/html';
  4153. }
  4154. elseif (substr($this->file->body, 0, 5) === '%PDF-')
  4155. {
  4156. return 'application/pdf';
  4157. }
  4158. elseif (substr($this->file->body, 0, 11) === '%!PS-Adobe-')
  4159. {
  4160. return 'application/postscript';
  4161. }
  4162. elseif (substr($this->file->body, 0, 6) === 'GIF87a'
  4163. || substr($this->file->body, 0, 6) === 'GIF89a')
  4164. {
  4165. return 'image/gif';
  4166. }
  4167. elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
  4168. {
  4169. return 'image/png';
  4170. }
  4171. elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF")
  4172. {
  4173. return 'image/jpeg';
  4174. }
  4175. elseif (substr($this->file->body, 0, 2) === "\x42\x4D")
  4176. {
  4177. return 'image/bmp';
  4178. }
  4179. elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00")
  4180. {
  4181. return 'image/vnd.microsoft.icon';
  4182. }
  4183. else
  4184. {
  4185. return $this->text_or_binary();
  4186. }
  4187. }
  4188. /**
  4189. * Sniff images
  4190. *
  4191. * @return string Actual Content-Type
  4192. */
  4193. public function image()
  4194. {
  4195. if (substr($this->file->body, 0, 6) === 'GIF87a'
  4196. || substr($this->file->body, 0, 6) === 'GIF89a')
  4197. {
  4198. return 'image/gif';
  4199. }
  4200. elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
  4201. {
  4202. return 'image/png';
  4203. }
  4204. elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF")
  4205. {
  4206. return 'image/jpeg';
  4207. }
  4208. elseif (substr($this->file->body, 0, 2) === "\x42\x4D")
  4209. {
  4210. return 'image/bmp';
  4211. }
  4212. elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00")
  4213. {
  4214. return 'image/vnd.microsoft.icon';
  4215. }
  4216. else
  4217. {
  4218. return false;
  4219. }
  4220. }
  4221. /**
  4222. * Sniff HTML
  4223. *
  4224. * @return string Actual Content-Type
  4225. */
  4226. public function feed_or_html()
  4227. {
  4228. $len = strlen($this->file->body);
  4229. $pos = strspn($this->file->body, "\x09\x0A\x0D\x20");
  4230. while ($pos < $len)
  4231. {
  4232. switch ($this->file->body[$pos])
  4233. {
  4234. case "\x09":
  4235. case "\x0A":
  4236. case "\x0D":
  4237. case "\x20":
  4238. $pos += strspn($this->file->body, "\x09\x0A\x0D\x20", $pos);
  4239. continue 2;
  4240. case '<':
  4241. $pos++;
  4242. break;
  4243. default:
  4244. return 'text/html';
  4245. }
  4246. if (substr($this->file->body, $pos, 3) === '!--')
  4247. {
  4248. $pos += 3;
  4249. if ($pos < $len && ($pos = strpos($this->file->body, '-->', $pos)) !== false)
  4250. {
  4251. $pos += 3;
  4252. }
  4253. else
  4254. {
  4255. return 'text/html';
  4256. }
  4257. }
  4258. elseif (substr($this->file->body, $pos, 1) === '!')
  4259. {
  4260. if ($pos < $len && ($pos = strpos($this->file->body, '>', $pos)) !== false)
  4261. {
  4262. $pos++;
  4263. }
  4264. else
  4265. {
  4266. return 'text/html';
  4267. }
  4268. }
  4269. elseif (substr($this->file->body, $pos, 1) === '?')
  4270. {
  4271. if ($pos < $len && ($pos = strpos($this->file->body, '?>', $pos)) !== false)
  4272. {
  4273. $pos += 2;
  4274. }
  4275. else
  4276. {
  4277. return 'text/html';
  4278. }
  4279. }
  4280. elseif (substr($this->file->body, $pos, 3) === 'rss'
  4281. || substr($this->file->body, $pos, 7) === 'rdf:RDF')
  4282. {
  4283. return 'application/rss+xml';
  4284. }
  4285. elseif (substr($this->file->body, $pos, 4) === 'feed')
  4286. {
  4287. return 'application/atom+xml';
  4288. }
  4289. else
  4290. {
  4291. return 'text/html';
  4292. }
  4293. }
  4294. return 'text/html';
  4295. }
  4296. }
  4297. /**
  4298. * Manages `<media:copyright>` copyright tags as defined in Media RSS
  4299. *
  4300. * Used by {@see SimplePie_Enclosure::get_copyright()}
  4301. *
  4302. * This class can be overloaded with {@see SimplePie::set_copyright_class()}
  4303. *
  4304. * @package SimplePie
  4305. * @subpackage API
  4306. */
  4307. class SimplePie_Copyright
  4308. {
  4309. /**
  4310. * Copyright URL
  4311. *
  4312. * @var string
  4313. * @see get_url()
  4314. */
  4315. var $url;
  4316. /**
  4317. * Attribution
  4318. *
  4319. * @var string
  4320. * @see get_attribution()
  4321. */
  4322. var $label;
  4323. /**
  4324. * Constructor, used to input the data
  4325. *
  4326. * For documentation on all the parameters, see the corresponding
  4327. * properties and their accessors
  4328. */
  4329. public function __construct($url = null, $label = null)
  4330. {
  4331. $this->url = $url;
  4332. $this->label = $label;
  4333. }
  4334. /**
  4335. * String-ified version
  4336. *
  4337. * @return string
  4338. */
  4339. public function __toString()
  4340. {
  4341. // There is no $this->data here
  4342. return md5(serialize($this));
  4343. }
  4344. /**
  4345. * Get the copyright URL
  4346. *
  4347. * @return string|null URL to copyright information
  4348. */
  4349. public function get_url()
  4350. {
  4351. if ($this->url !== null)
  4352. {
  4353. return $this->url;
  4354. }
  4355. else
  4356. {
  4357. return null;
  4358. }
  4359. }
  4360. /**
  4361. * Get the attribution text
  4362. *
  4363. * @return string|null
  4364. */
  4365. public function get_attribution()
  4366. {
  4367. if ($this->label !== null)
  4368. {
  4369. return $this->label;
  4370. }
  4371. else
  4372. {
  4373. return null;
  4374. }
  4375. }
  4376. }
  4377. /**
  4378. * SimplePie class.
  4379. *
  4380. * Class for backward compatibility.
  4381. *
  4382. * @deprecated Use {@see SimplePie} directly
  4383. * @package SimplePie
  4384. * @subpackage API
  4385. */
  4386. class SimplePie_Core extends SimplePie
  4387. {
  4388. }
  4389. /**
  4390. * Handles `<media:credit>` as defined in Media RSS
  4391. *
  4392. * Used by {@see SimplePie_Enclosure::get_credit()} and {@see SimplePie_Enclosure::get_credits()}
  4393. *
  4394. * This class can be overloaded with {@see SimplePie::set_credit_class()}
  4395. *
  4396. * @package SimplePie
  4397. * @subpackage API
  4398. */
  4399. class SimplePie_Credit
  4400. {
  4401. /**
  4402. * Credited role
  4403. *
  4404. * @var string
  4405. * @see get_role()
  4406. */
  4407. var $role;
  4408. /**
  4409. * Organizational scheme
  4410. *
  4411. * @var string
  4412. * @see get_scheme()
  4413. */
  4414. var $scheme;
  4415. /**
  4416. * Credited name
  4417. *
  4418. * @var string
  4419. * @see get_name()
  4420. */
  4421. var $name;
  4422. /**
  4423. * Constructor, used to input the data
  4424. *
  4425. * For documentation on all the parameters, see the corresponding
  4426. * properties and their accessors
  4427. */
  4428. public function __construct($role = null, $scheme = null, $name = null)
  4429. {
  4430. $this->role = $role;
  4431. $this->scheme = $scheme;
  4432. $this->name = $name;
  4433. }
  4434. /**
  4435. * String-ified version
  4436. *
  4437. * @return string
  4438. */
  4439. public function __toString()
  4440. {
  4441. // There is no $this->data here
  4442. return md5(serialize($this));
  4443. }
  4444. /**
  4445. * Get the role of the person receiving credit
  4446. *
  4447. * @return string|null
  4448. */
  4449. public function get_role()
  4450. {
  4451. if ($this->role !== null)
  4452. {
  4453. return $this->role;
  4454. }
  4455. else
  4456. {
  4457. return null;
  4458. }
  4459. }
  4460. /**
  4461. * Get the organizational scheme
  4462. *
  4463. * @return string|null
  4464. */
  4465. public function get_scheme()
  4466. {
  4467. if ($this->scheme !== null)
  4468. {
  4469. return $this->scheme;
  4470. }
  4471. else
  4472. {
  4473. return null;
  4474. }
  4475. }
  4476. /**
  4477. * Get the credited person/entity's name
  4478. *
  4479. * @return string|null
  4480. */
  4481. public function get_name()
  4482. {
  4483. if ($this->name !== null)
  4484. {
  4485. return $this->name;
  4486. }
  4487. else
  4488. {
  4489. return null;
  4490. }
  4491. }
  4492. }
  4493. /**
  4494. * Decode HTML Entities
  4495. *
  4496. * This implements HTML5 as of revision 967 (2007-06-28)
  4497. *
  4498. * @deprecated Use DOMDocument instead!
  4499. * @package SimplePie
  4500. */
  4501. class SimplePie_Decode_HTML_Entities
  4502. {
  4503. /**
  4504. * Data to be parsed
  4505. *
  4506. * @access private
  4507. * @var string
  4508. */
  4509. var $data = '';
  4510. /**
  4511. * Currently consumed bytes
  4512. *
  4513. * @access private
  4514. * @var string
  4515. */
  4516. var $consumed = '';
  4517. /**
  4518. * Position of the current byte being parsed
  4519. *
  4520. * @access private
  4521. * @var int
  4522. */
  4523. var $position = 0;
  4524. /**
  4525. * Create an instance of the class with the input data
  4526. *
  4527. * @access public
  4528. * @param string $data Input data
  4529. */
  4530. public function __construct($data)
  4531. {
  4532. $this->data = $data;
  4533. }
  4534. /**
  4535. * Parse the input data
  4536. *
  4537. * @access public
  4538. * @return string Output data
  4539. */
  4540. public function parse()
  4541. {
  4542. while (($this->position = strpos($this->data, '&', $this->position)) !== false)
  4543. {
  4544. $this->consume();
  4545. $this->entity();
  4546. $this->consumed = '';
  4547. }
  4548. return $this->data;
  4549. }
  4550. /**
  4551. * Consume the next byte
  4552. *
  4553. * @access private
  4554. * @return mixed The next byte, or false, if there is no more data
  4555. */
  4556. public function consume()
  4557. {
  4558. if (isset($this->data[$this->position]))
  4559. {
  4560. $this->consumed .= $this->data[$this->position];
  4561. return $this->data[$this->position++];
  4562. }
  4563. else
  4564. {
  4565. return false;
  4566. }
  4567. }
  4568. /**
  4569. * Consume a range of characters
  4570. *
  4571. * @access private
  4572. * @param string $chars Characters to consume
  4573. * @return mixed A series of characters that match the range, or false
  4574. */
  4575. public function consume_range($chars)
  4576. {
  4577. if ($len = strspn($this->data, $chars, $this->position))
  4578. {
  4579. $data = substr($this->data, $this->position, $len);
  4580. $this->consumed .= $data;
  4581. $this->position += $len;
  4582. return $data;
  4583. }
  4584. else
  4585. {
  4586. return false;
  4587. }
  4588. }
  4589. /**
  4590. * Unconsume one byte
  4591. *
  4592. * @access private
  4593. */
  4594. public function unconsume()
  4595. {
  4596. $this->consumed = substr($this->consumed, 0, -1);
  4597. $this->position--;
  4598. }
  4599. /**
  4600. * Decode an entity
  4601. *
  4602. * @access private
  4603. */
  4604. public function entity()
  4605. {
  4606. switch ($this->consume())
  4607. {
  4608. case "\x09":
  4609. case "\x0A":
  4610. case "\x0B":
  4611. case "\x0B":
  4612. case "\x0C":
  4613. case "\x20":
  4614. case "\x3C":
  4615. case "\x26":
  4616. case false:
  4617. break;
  4618. case "\x23":
  4619. switch ($this->consume())
  4620. {
  4621. case "\x78":
  4622. case "\x58":
  4623. $range = '0123456789ABCDEFabcdef';
  4624. $hex = true;
  4625. break;
  4626. default:
  4627. $range = '0123456789';
  4628. $hex = false;
  4629. $this->unconsume();
  4630. break;
  4631. }
  4632. if ($codepoint = $this->consume_range($range))
  4633. {
  4634. static $windows_1252_specials = array(0x0D => "\x0A", 0x80 => "\xE2\x82\xAC", 0x81 => "\xEF\xBF\xBD", 0x82 => "\xE2\x80\x9A", 0x83 => "\xC6\x92", 0x84 => "\xE2\x80\x9E", 0x85 => "\xE2\x80\xA6", 0x86 => "\xE2\x80\xA0", 0x87 => "\xE2\x80\xA1", 0x88 => "\xCB\x86", 0x89 => "\xE2\x80\xB0", 0x8A => "\xC5\xA0", 0x8B => "\xE2\x80\xB9", 0x8C => "\xC5\x92", 0x8D => "\xEF\xBF\xBD", 0x8E => "\xC5\xBD", 0x8F => "\xEF\xBF\xBD", 0x90 => "\xEF\xBF\xBD", 0x91 => "\xE2\x80\x98", 0x92 => "\xE2\x80\x99", 0x93 => "\xE2\x80\x9C", 0x94 => "\xE2\x80\x9D", 0x95 => "\xE2\x80\xA2", 0x96 => "\xE2\x80\x93", 0x97 => "\xE2\x80\x94", 0x98 => "\xCB\x9C", 0x99 => "\xE2\x84\xA2", 0x9A => "\xC5\xA1", 0x9B => "\xE2\x80\xBA", 0x9C => "\xC5\x93", 0x9D => "\xEF\xBF\xBD", 0x9E => "\xC5\xBE", 0x9F => "\xC5\xB8");
  4635. if ($hex)
  4636. {
  4637. $codepoint = hexdec($codepoint);
  4638. }
  4639. else
  4640. {
  4641. $codepoint = intval($codepoint);
  4642. }
  4643. if (isset($windows_1252_specials[$codepoint]))
  4644. {
  4645. $replacement = $windows_1252_specials[$codepoint];
  4646. }
  4647. else
  4648. {
  4649. $replacement = SimplePie_Misc::codepoint_to_utf8($codepoint);
  4650. }
  4651. if (!in_array($this->consume(), array(';', false), true))
  4652. {
  4653. $this->unconsume();
  4654. }
  4655. $consumed_length = strlen($this->consumed);
  4656. $this->data = substr_replace($this->data, $replacement, $this->position - $consumed_length, $consumed_length);
  4657. $this->position += strlen($replacement) - $consumed_length;
  4658. }
  4659. break;
  4660. default:
  4661. static $entities = array(
  4662. 'Aacute' => "\xC3\x81",
  4663. 'aacute' => "\xC3\xA1",
  4664. 'Aacute;' => "\xC3\x81",
  4665. 'aacute;' => "\xC3\xA1",
  4666. 'Acirc' => "\xC3\x82",
  4667. 'acirc' => "\xC3\xA2",
  4668. 'Acirc;' => "\xC3\x82",
  4669. 'acirc;' => "\xC3\xA2",
  4670. 'acute' => "\xC2\xB4",
  4671. 'acute;' => "\xC2\xB4",
  4672. 'AElig' => "\xC3\x86",
  4673. 'aelig' => "\xC3\xA6",
  4674. 'AElig;' => "\xC3\x86",
  4675. 'aelig;' => "\xC3\xA6",
  4676. 'Agrave' => "\xC3\x80",
  4677. 'agrave' => "\xC3\xA0",
  4678. 'Agrave;' => "\xC3\x80",
  4679. 'agrave;' => "\xC3\xA0",
  4680. 'alefsym;' => "\xE2\x84\xB5",
  4681. 'Alpha;' => "\xCE\x91",
  4682. 'alpha;' => "\xCE\xB1",
  4683. 'AMP' => "\x26",
  4684. 'amp' => "\x26",
  4685. 'AMP;' => "\x26",
  4686. 'amp;' => "\x26",
  4687. 'and;' => "\xE2\x88\xA7",
  4688. 'ang;' => "\xE2\x88\xA0",
  4689. 'apos;' => "\x27",
  4690. 'Aring' => "\xC3\x85",
  4691. 'aring' => "\xC3\xA5",
  4692. 'Aring;' => "\xC3\x85",
  4693. 'aring;' => "\xC3\xA5",
  4694. 'asymp;' => "\xE2\x89\x88",
  4695. 'Atilde' => "\xC3\x83",
  4696. 'atilde' => "\xC3\xA3",
  4697. 'Atilde;' => "\xC3\x83",
  4698. 'atilde;' => "\xC3\xA3",
  4699. 'Auml' => "\xC3\x84",
  4700. 'auml' => "\xC3\xA4",
  4701. 'Auml;' => "\xC3\x84",
  4702. 'auml;' => "\xC3\xA4",
  4703. 'bdquo;' => "\xE2\x80\x9E",
  4704. 'Beta;' => "\xCE\x92",
  4705. 'beta;' => "\xCE\xB2",
  4706. 'brvbar' => "\xC2\xA6",
  4707. 'brvbar;' => "\xC2\xA6",
  4708. 'bull;' => "\xE2\x80\xA2",
  4709. 'cap;' => "\xE2\x88\xA9",
  4710. 'Ccedil' => "\xC3\x87",
  4711. 'ccedil' => "\xC3\xA7",
  4712. 'Ccedil;' => "\xC3\x87",
  4713. 'ccedil;' => "\xC3\xA7",
  4714. 'cedil' => "\xC2\xB8",
  4715. 'cedil;' => "\xC2\xB8",
  4716. 'cent' => "\xC2\xA2",
  4717. 'cent;' => "\xC2\xA2",
  4718. 'Chi;' => "\xCE\xA7",
  4719. 'chi;' => "\xCF\x87",
  4720. 'circ;' => "\xCB\x86",
  4721. 'clubs;' => "\xE2\x99\xA3",
  4722. 'cong;' => "\xE2\x89\x85",
  4723. 'COPY' => "\xC2\xA9",
  4724. 'copy' => "\xC2\xA9",
  4725. 'COPY;' => "\xC2\xA9",
  4726. 'copy;' => "\xC2\xA9",
  4727. 'crarr;' => "\xE2\x86\xB5",
  4728. 'cup;' => "\xE2\x88\xAA",
  4729. 'curren' => "\xC2\xA4",
  4730. 'curren;' => "\xC2\xA4",
  4731. 'Dagger;' => "\xE2\x80\xA1",
  4732. 'dagger;' => "\xE2\x80\xA0",
  4733. 'dArr;' => "\xE2\x87\x93",
  4734. 'darr;' => "\xE2\x86\x93",
  4735. 'deg' => "\xC2\xB0",
  4736. 'deg;' => "\xC2\xB0",
  4737. 'Delta;' => "\xCE\x94",
  4738. 'delta;' => "\xCE\xB4",
  4739. 'diams;' => "\xE2\x99\xA6",
  4740. 'divide' => "\xC3\xB7",
  4741. 'divide;' => "\xC3\xB7",
  4742. 'Eacute' => "\xC3\x89",
  4743. 'eacute' => "\xC3\xA9",
  4744. 'Eacute;' => "\xC3\x89",
  4745. 'eacute;' => "\xC3\xA9",
  4746. 'Ecirc' => "\xC3\x8A",
  4747. 'ecirc' => "\xC3\xAA",
  4748. 'Ecirc;' => "\xC3\x8A",
  4749. 'ecirc;' => "\xC3\xAA",
  4750. 'Egrave' => "\xC3\x88",
  4751. 'egrave' => "\xC3\xA8",
  4752. 'Egrave;' => "\xC3\x88",
  4753. 'egrave;' => "\xC3\xA8",
  4754. 'empty;' => "\xE2\x88\x85",
  4755. 'emsp;' => "\xE2\x80\x83",
  4756. 'ensp;' => "\xE2\x80\x82",
  4757. 'Epsilon;' => "\xCE\x95",
  4758. 'epsilon;' => "\xCE\xB5",
  4759. 'equiv;' => "\xE2\x89\xA1",
  4760. 'Eta;' => "\xCE\x97",
  4761. 'eta;' => "\xCE\xB7",
  4762. 'ETH' => "\xC3\x90",
  4763. 'eth' => "\xC3\xB0",
  4764. 'ETH;' => "\xC3\x90",
  4765. 'eth;' => "\xC3\xB0",
  4766. 'Euml' => "\xC3\x8B",
  4767. 'euml' => "\xC3\xAB",
  4768. 'Euml;' => "\xC3\x8B",
  4769. 'euml;' => "\xC3\xAB",
  4770. 'euro;' => "\xE2\x82\xAC",
  4771. 'exist;' => "\xE2\x88\x83",
  4772. 'fnof;' => "\xC6\x92",
  4773. 'forall;' => "\xE2\x88\x80",
  4774. 'frac12' => "\xC2\xBD",
  4775. 'frac12;' => "\xC2\xBD",
  4776. 'frac14' => "\xC2\xBC",
  4777. 'frac14;' => "\xC2\xBC",
  4778. 'frac34' => "\xC2\xBE",
  4779. 'frac34;' => "\xC2\xBE",
  4780. 'frasl;' => "\xE2\x81\x84",
  4781. 'Gamma;' => "\xCE\x93",
  4782. 'gamma;' => "\xCE\xB3",
  4783. 'ge;' => "\xE2\x89\xA5",
  4784. 'GT' => "\x3E",
  4785. 'gt' => "\x3E",
  4786. 'GT;' => "\x3E",
  4787. 'gt;' => "\x3E",
  4788. 'hArr;' => "\xE2\x87\x94",
  4789. 'harr;' => "\xE2\x86\x94",
  4790. 'hearts;' => "\xE2\x99\xA5",
  4791. 'hellip;' => "\xE2\x80\xA6",
  4792. 'Iacute' => "\xC3\x8D",
  4793. 'iacute' => "\xC3\xAD",
  4794. 'Iacute;' => "\xC3\x8D",
  4795. 'iacute;' => "\xC3\xAD",
  4796. 'Icirc' => "\xC3\x8E",
  4797. 'icirc' => "\xC3\xAE",
  4798. 'Icirc;' => "\xC3\x8E",
  4799. 'icirc;' => "\xC3\xAE",
  4800. 'iexcl' => "\xC2\xA1",
  4801. 'iexcl;' => "\xC2\xA1",
  4802. 'Igrave' => "\xC3\x8C",
  4803. 'igrave' => "\xC3\xAC",
  4804. 'Igrave;' => "\xC3\x8C",
  4805. 'igrave;' => "\xC3\xAC",
  4806. 'image;' => "\xE2\x84\x91",
  4807. 'infin;' => "\xE2\x88\x9E",
  4808. 'int;' => "\xE2\x88\xAB",
  4809. 'Iota;' => "\xCE\x99",
  4810. 'iota;' => "\xCE\xB9",
  4811. 'iquest' => "\xC2\xBF",
  4812. 'iquest;' => "\xC2\xBF",
  4813. 'isin;' => "\xE2\x88\x88",
  4814. 'Iuml' => "\xC3\x8F",
  4815. 'iuml' => "\xC3\xAF",
  4816. 'Iuml;' => "\xC3\x8F",
  4817. 'iuml;' => "\xC3\xAF",
  4818. 'Kappa;' => "\xCE\x9A",
  4819. 'kappa;' => "\xCE\xBA",
  4820. 'Lambda;' => "\xCE\x9B",
  4821. 'lambda;' => "\xCE\xBB",
  4822. 'lang;' => "\xE3\x80\x88",
  4823. 'laquo' => "\xC2\xAB",
  4824. 'laquo;' => "\xC2\xAB",
  4825. 'lArr;' => "\xE2\x87\x90",
  4826. 'larr;' => "\xE2\x86\x90",
  4827. 'lceil;' => "\xE2\x8C\x88",
  4828. 'ldquo;' => "\xE2\x80\x9C",
  4829. 'le;' => "\xE2\x89\xA4",
  4830. 'lfloor;' => "\xE2\x8C\x8A",
  4831. 'lowast;' => "\xE2\x88\x97",
  4832. 'loz;' => "\xE2\x97\x8A",
  4833. 'lrm;' => "\xE2\x80\x8E",
  4834. 'lsaquo;' => "\xE2\x80\xB9",
  4835. 'lsquo;' => "\xE2\x80\x98",
  4836. 'LT' => "\x3C",
  4837. 'lt' => "\x3C",
  4838. 'LT;' => "\x3C",
  4839. 'lt;' => "\x3C",
  4840. 'macr' => "\xC2\xAF",
  4841. 'macr;' => "\xC2\xAF",
  4842. 'mdash;' => "\xE2\x80\x94",
  4843. 'micro' => "\xC2\xB5",
  4844. 'micro;' => "\xC2\xB5",
  4845. 'middot' => "\xC2\xB7",
  4846. 'middot;' => "\xC2\xB7",
  4847. 'minus;' => "\xE2\x88\x92",
  4848. 'Mu;' => "\xCE\x9C",
  4849. 'mu;' => "\xCE\xBC",
  4850. 'nabla;' => "\xE2\x88\x87",
  4851. 'nbsp' => "\xC2\xA0",
  4852. 'nbsp;' => "\xC2\xA0",
  4853. 'ndash;' => "\xE2\x80\x93",
  4854. 'ne;' => "\xE2\x89\xA0",
  4855. 'ni;' => "\xE2\x88\x8B",
  4856. 'not' => "\xC2\xAC",
  4857. 'not;' => "\xC2\xAC",
  4858. 'notin;' => "\xE2\x88\x89",
  4859. 'nsub;' => "\xE2\x8A\x84",
  4860. 'Ntilde' => "\xC3\x91",
  4861. 'ntilde' => "\xC3\xB1",
  4862. 'Ntilde;' => "\xC3\x91",
  4863. 'ntilde;' => "\xC3\xB1",
  4864. 'Nu;' => "\xCE\x9D",
  4865. 'nu;' => "\xCE\xBD",
  4866. 'Oacute' => "\xC3\x93",
  4867. 'oacute' => "\xC3\xB3",
  4868. 'Oacute;' => "\xC3\x93",
  4869. 'oacute;' => "\xC3\xB3",
  4870. 'Ocirc' => "\xC3\x94",
  4871. 'ocirc' => "\xC3\xB4",
  4872. 'Ocirc;' => "\xC3\x94",
  4873. 'ocirc;' => "\xC3\xB4",
  4874. 'OElig;' => "\xC5\x92",
  4875. 'oelig;' => "\xC5\x93",
  4876. 'Ograve' => "\xC3\x92",
  4877. 'ograve' => "\xC3\xB2",
  4878. 'Ograve;' => "\xC3\x92",
  4879. 'ograve;' => "\xC3\xB2",
  4880. 'oline;' => "\xE2\x80\xBE",
  4881. 'Omega;' => "\xCE\xA9",
  4882. 'omega;' => "\xCF\x89",
  4883. 'Omicron;' => "\xCE\x9F",
  4884. 'omicron;' => "\xCE\xBF",
  4885. 'oplus;' => "\xE2\x8A\x95",
  4886. 'or;' => "\xE2\x88\xA8",
  4887. 'ordf' => "\xC2\xAA",
  4888. 'ordf;' => "\xC2\xAA",
  4889. 'ordm' => "\xC2\xBA",
  4890. 'ordm;' => "\xC2\xBA",
  4891. 'Oslash' => "\xC3\x98",
  4892. 'oslash' => "\xC3\xB8",
  4893. 'Oslash;' => "\xC3\x98",
  4894. 'oslash;' => "\xC3\xB8",
  4895. 'Otilde' => "\xC3\x95",
  4896. 'otilde' => "\xC3\xB5",
  4897. 'Otilde;' => "\xC3\x95",
  4898. 'otilde;' => "\xC3\xB5",
  4899. 'otimes;' => "\xE2\x8A\x97",
  4900. 'Ouml' => "\xC3\x96",
  4901. 'ouml' => "\xC3\xB6",
  4902. 'Ouml;' => "\xC3\x96",
  4903. 'ouml;' => "\xC3\xB6",
  4904. 'para' => "\xC2\xB6",
  4905. 'para;' => "\xC2\xB6",
  4906. 'part;' => "\xE2\x88\x82",
  4907. 'permil;' => "\xE2\x80\xB0",
  4908. 'perp;' => "\xE2\x8A\xA5",
  4909. 'Phi;' => "\xCE\xA6",
  4910. 'phi;' => "\xCF\x86",
  4911. 'Pi;' => "\xCE\xA0",
  4912. 'pi;' => "\xCF\x80",
  4913. 'piv;' => "\xCF\x96",
  4914. 'plusmn' => "\xC2\xB1",
  4915. 'plusmn;' => "\xC2\xB1",
  4916. 'pound' => "\xC2\xA3",
  4917. 'pound;' => "\xC2\xA3",
  4918. 'Prime;' => "\xE2\x80\xB3",
  4919. 'prime;' => "\xE2\x80\xB2",
  4920. 'prod;' => "\xE2\x88\x8F",
  4921. 'prop;' => "\xE2\x88\x9D",
  4922. 'Psi;' => "\xCE\xA8",
  4923. 'psi;' => "\xCF\x88",
  4924. 'QUOT' => "\x22",
  4925. 'quot' => "\x22",
  4926. 'QUOT;' => "\x22",
  4927. 'quot;' => "\x22",
  4928. 'radic;' => "\xE2\x88\x9A",
  4929. 'rang;' => "\xE3\x80\x89",
  4930. 'raquo' => "\xC2\xBB",
  4931. 'raquo;' => "\xC2\xBB",
  4932. 'rArr;' => "\xE2\x87\x92",
  4933. 'rarr;' => "\xE2\x86\x92",
  4934. 'rceil;' => "\xE2\x8C\x89",
  4935. 'rdquo;' => "\xE2\x80\x9D",
  4936. 'real;' => "\xE2\x84\x9C",
  4937. 'REG' => "\xC2\xAE",
  4938. 'reg' => "\xC2\xAE",
  4939. 'REG;' => "\xC2\xAE",
  4940. 'reg;' => "\xC2\xAE",
  4941. 'rfloor;' => "\xE2\x8C\x8B",
  4942. 'Rho;' => "\xCE\xA1",
  4943. 'rho;' => "\xCF\x81",
  4944. 'rlm;' => "\xE2\x80\x8F",
  4945. 'rsaquo;' => "\xE2\x80\xBA",
  4946. 'rsquo;' => "\xE2\x80\x99",
  4947. 'sbquo;' => "\xE2\x80\x9A",
  4948. 'Scaron;' => "\xC5\xA0",
  4949. 'scaron;' => "\xC5\xA1",
  4950. 'sdot;' => "\xE2\x8B\x85",
  4951. 'sect' => "\xC2\xA7",
  4952. 'sect;' => "\xC2\xA7",
  4953. 'shy' => "\xC2\xAD",
  4954. 'shy;' => "\xC2\xAD",
  4955. 'Sigma;' => "\xCE\xA3",
  4956. 'sigma;' => "\xCF\x83",
  4957. 'sigmaf;' => "\xCF\x82",
  4958. 'sim;' => "\xE2\x88\xBC",
  4959. 'spades;' => "\xE2\x99\xA0",
  4960. 'sub;' => "\xE2\x8A\x82",
  4961. 'sube;' => "\xE2\x8A\x86",
  4962. 'sum;' => "\xE2\x88\x91",
  4963. 'sup;' => "\xE2\x8A\x83",
  4964. 'sup1' => "\xC2\xB9",
  4965. 'sup1;' => "\xC2\xB9",
  4966. 'sup2' => "\xC2\xB2",
  4967. 'sup2;' => "\xC2\xB2",
  4968. 'sup3' => "\xC2\xB3",
  4969. 'sup3;' => "\xC2\xB3",
  4970. 'supe;' => "\xE2\x8A\x87",
  4971. 'szlig' => "\xC3\x9F",
  4972. 'szlig;' => "\xC3\x9F",
  4973. 'Tau;' => "\xCE\xA4",
  4974. 'tau;' => "\xCF\x84",
  4975. 'there4;' => "\xE2\x88\xB4",
  4976. 'Theta;' => "\xCE\x98",
  4977. 'theta;' => "\xCE\xB8",
  4978. 'thetasym;' => "\xCF\x91",
  4979. 'thinsp;' => "\xE2\x80\x89",
  4980. 'THORN' => "\xC3\x9E",
  4981. 'thorn' => "\xC3\xBE",
  4982. 'THORN;' => "\xC3\x9E",
  4983. 'thorn;' => "\xC3\xBE",
  4984. 'tilde;' => "\xCB\x9C",
  4985. 'times' => "\xC3\x97",
  4986. 'times;' => "\xC3\x97",
  4987. 'TRADE;' => "\xE2\x84\xA2",
  4988. 'trade;' => "\xE2\x84\xA2",
  4989. 'Uacute' => "\xC3\x9A",
  4990. 'uacute' => "\xC3\xBA",
  4991. 'Uacute;' => "\xC3\x9A",
  4992. 'uacute;' => "\xC3\xBA",
  4993. 'uArr;' => "\xE2\x87\x91",
  4994. 'uarr;' => "\xE2\x86\x91",
  4995. 'Ucirc' => "\xC3\x9B",
  4996. 'ucirc' => "\xC3\xBB",
  4997. 'Ucirc;' => "\xC3\x9B",
  4998. 'ucirc;' => "\xC3\xBB",
  4999. 'Ugrave' => "\xC3\x99",
  5000. 'ugrave' => "\xC3\xB9",
  5001. 'Ugrave;' => "\xC3\x99",
  5002. 'ugrave;' => "\xC3\xB9",
  5003. 'uml' => "\xC2\xA8",
  5004. 'uml;' => "\xC2\xA8",
  5005. 'upsih;' => "\xCF\x92",
  5006. 'Upsilon;' => "\xCE\xA5",
  5007. 'upsilon;' => "\xCF\x85",
  5008. 'Uuml' => "\xC3\x9C",
  5009. 'uuml' => "\xC3\xBC",
  5010. 'Uuml;' => "\xC3\x9C",
  5011. 'uuml;' => "\xC3\xBC",
  5012. 'weierp;' => "\xE2\x84\x98",
  5013. 'Xi;' => "\xCE\x9E",
  5014. 'xi;' => "\xCE\xBE",
  5015. 'Yacute' => "\xC3\x9D",
  5016. 'yacute' => "\xC3\xBD",
  5017. 'Yacute;' => "\xC3\x9D",
  5018. 'yacute;' => "\xC3\xBD",
  5019. 'yen' => "\xC2\xA5",
  5020. 'yen;' => "\xC2\xA5",
  5021. 'yuml' => "\xC3\xBF",
  5022. 'Yuml;' => "\xC5\xB8",
  5023. 'yuml;' => "\xC3\xBF",
  5024. 'Zeta;' => "\xCE\x96",
  5025. 'zeta;' => "\xCE\xB6",
  5026. 'zwj;' => "\xE2\x80\x8D",
  5027. 'zwnj;' => "\xE2\x80\x8C"
  5028. );
  5029. for ($i = 0, $match = null; $i < 9 && $this->consume() !== false; $i++)
  5030. {
  5031. $consumed = substr($this->consumed, 1);
  5032. if (isset($entities[$consumed]))
  5033. {
  5034. $match = $consumed;
  5035. }
  5036. }
  5037. if ($match !== null)
  5038. {
  5039. $this->data = substr_replace($this->data, $entities[$match], $this->position - strlen($consumed) - 1, strlen($match) + 1);
  5040. $this->position += strlen($entities[$match]) - strlen($consumed) - 1;
  5041. }
  5042. break;
  5043. }
  5044. }
  5045. }
  5046. /**
  5047. * Handles everything related to enclosures (including Media RSS and iTunes RSS)
  5048. *
  5049. * Used by {@see SimplePie_Item::get_enclosure()} and {@see SimplePie_Item::get_enclosures()}
  5050. *
  5051. * This class can be overloaded with {@see SimplePie::set_enclosure_class()}
  5052. *
  5053. * @package SimplePie
  5054. * @subpackage API
  5055. */
  5056. class SimplePie_Enclosure
  5057. {
  5058. /**
  5059. * @var string
  5060. * @see get_bitrate()
  5061. */
  5062. var $bitrate;
  5063. /**
  5064. * @var array
  5065. * @see get_captions()
  5066. */
  5067. var $captions;
  5068. /**
  5069. * @var array
  5070. * @see get_categories()
  5071. */
  5072. var $categories;
  5073. /**
  5074. * @var int
  5075. * @see get_channels()
  5076. */
  5077. var $channels;
  5078. /**
  5079. * @var SimplePie_Copyright
  5080. * @see get_copyright()
  5081. */
  5082. var $copyright;
  5083. /**
  5084. * @var array
  5085. * @see get_credits()
  5086. */
  5087. var $credits;
  5088. /**
  5089. * @var string
  5090. * @see get_description()
  5091. */
  5092. var $description;
  5093. /**
  5094. * @var int
  5095. * @see get_duration()
  5096. */
  5097. var $duration;
  5098. /**
  5099. * @var string
  5100. * @see get_expression()
  5101. */
  5102. var $expression;
  5103. /**
  5104. * @var string
  5105. * @see get_framerate()
  5106. */
  5107. var $framerate;
  5108. /**
  5109. * @var string
  5110. * @see get_handler()
  5111. */
  5112. var $handler;
  5113. /**
  5114. * @var array
  5115. * @see get_hashes()
  5116. */
  5117. var $hashes;
  5118. /**
  5119. * @var string
  5120. * @see get_height()
  5121. */
  5122. var $height;
  5123. /**
  5124. * @deprecated
  5125. * @var null
  5126. */
  5127. var $javascript;
  5128. /**
  5129. * @var array
  5130. * @see get_keywords()
  5131. */
  5132. var $keywords;
  5133. /**
  5134. * @var string
  5135. * @see get_language()
  5136. */
  5137. var $lang;
  5138. /**
  5139. * @var string
  5140. * @see get_length()
  5141. */
  5142. var $length;
  5143. /**
  5144. * @var string
  5145. * @see get_link()
  5146. */
  5147. var $link;
  5148. /**
  5149. * @var string
  5150. * @see get_medium()
  5151. */
  5152. var $medium;
  5153. /**
  5154. * @var string
  5155. * @see get_player()
  5156. */
  5157. var $player;
  5158. /**
  5159. * @var array
  5160. * @see get_ratings()
  5161. */
  5162. var $ratings;
  5163. /**
  5164. * @var array
  5165. * @see get_restrictions()
  5166. */
  5167. var $restrictions;
  5168. /**
  5169. * @var string
  5170. * @see get_sampling_rate()
  5171. */
  5172. var $samplingrate;
  5173. /**
  5174. * @var array
  5175. * @see get_thumbnails()
  5176. */
  5177. var $thumbnails;
  5178. /**
  5179. * @var string
  5180. * @see get_title()
  5181. */
  5182. var $title;
  5183. /**
  5184. * @var string
  5185. * @see get_type()
  5186. */
  5187. var $type;
  5188. /**
  5189. * @var string
  5190. * @see get_width()
  5191. */
  5192. var $width;
  5193. /**
  5194. * Constructor, used to input the data
  5195. *
  5196. * For documentation on all the parameters, see the corresponding
  5197. * properties and their accessors
  5198. *
  5199. * @uses idna_convert If available, this will convert an IDN
  5200. */
  5201. public function __construct($link = null, $type = null, $length = null, $javascript = null, $bitrate = null, $captions = null, $categories = null, $channels = null, $copyright = null, $credits = null, $description = null, $duration = null, $expression = null, $framerate = null, $hashes = null, $height = null, $keywords = null, $lang = null, $medium = null, $player = null, $ratings = null, $restrictions = null, $samplingrate = null, $thumbnails = null, $title = null, $width = null)
  5202. {
  5203. $this->bitrate = $bitrate;
  5204. $this->captions = $captions;
  5205. $this->categories = $categories;
  5206. $this->channels = $channels;
  5207. $this->copyright = $copyright;
  5208. $this->credits = $credits;
  5209. $this->description = $description;
  5210. $this->duration = $duration;
  5211. $this->expression = $expression;
  5212. $this->framerate = $framerate;
  5213. $this->hashes = $hashes;
  5214. $this->height = $height;
  5215. $this->keywords = $keywords;
  5216. $this->lang = $lang;
  5217. $this->length = $length;
  5218. $this->link = $link;
  5219. $this->medium = $medium;
  5220. $this->player = $player;
  5221. $this->ratings = $ratings;
  5222. $this->restrictions = $restrictions;
  5223. $this->samplingrate = $samplingrate;
  5224. $this->thumbnails = $thumbnails;
  5225. $this->title = $title;
  5226. $this->type = $type;
  5227. $this->width = $width;
  5228. if (class_exists('idna_convert'))
  5229. {
  5230. $idn = new idna_convert();
  5231. $parsed = SimplePie_Misc::parse_url($link);
  5232. $this->link = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
  5233. }
  5234. $this->handler = $this->get_handler(); // Needs to load last
  5235. }
  5236. /**
  5237. * String-ified version
  5238. *
  5239. * @return string
  5240. */
  5241. public function __toString()
  5242. {
  5243. // There is no $this->data here
  5244. return md5(serialize($this));
  5245. }
  5246. /**
  5247. * Get the bitrate
  5248. *
  5249. * @return string|null
  5250. */
  5251. public function get_bitrate()
  5252. {
  5253. if ($this->bitrate !== null)
  5254. {
  5255. return $this->bitrate;
  5256. }
  5257. else
  5258. {
  5259. return null;
  5260. }
  5261. }
  5262. /**
  5263. * Get a single caption
  5264. *
  5265. * @param int $key
  5266. * @return SimplePie_Caption|null
  5267. */
  5268. public function get_caption($key = 0)
  5269. {
  5270. $captions = $this->get_captions();
  5271. if (isset($captions[$key]))
  5272. {
  5273. return $captions[$key];
  5274. }
  5275. else
  5276. {
  5277. return null;
  5278. }
  5279. }
  5280. /**
  5281. * Get all captions
  5282. *
  5283. * @return array|null Array of {@see SimplePie_Caption} objects
  5284. */
  5285. public function get_captions()
  5286. {
  5287. if ($this->captions !== null)
  5288. {
  5289. return $this->captions;
  5290. }
  5291. else
  5292. {
  5293. return null;
  5294. }
  5295. }
  5296. /**
  5297. * Get a single category
  5298. *
  5299. * @param int $key
  5300. * @return SimplePie_Category|null
  5301. */
  5302. public function get_category($key = 0)
  5303. {
  5304. $categories = $this->get_categories();
  5305. if (isset($categories[$key]))
  5306. {
  5307. return $categories[$key];
  5308. }
  5309. else
  5310. {
  5311. return null;
  5312. }
  5313. }
  5314. /**
  5315. * Get all categories
  5316. *
  5317. * @return array|null Array of {@see SimplePie_Category} objects
  5318. */
  5319. public function get_categories()
  5320. {
  5321. if ($this->categories !== null)
  5322. {
  5323. return $this->categories;
  5324. }
  5325. else
  5326. {
  5327. return null;
  5328. }
  5329. }
  5330. /**
  5331. * Get the number of audio channels
  5332. *
  5333. * @return int|null
  5334. */
  5335. public function get_channels()
  5336. {
  5337. if ($this->channels !== null)
  5338. {
  5339. return $this->channels;
  5340. }
  5341. else
  5342. {
  5343. return null;
  5344. }
  5345. }
  5346. /**
  5347. * Get the copyright information
  5348. *
  5349. * @return SimplePie_Copyright|null
  5350. */
  5351. public function get_copyright()
  5352. {
  5353. if ($this->copyright !== null)
  5354. {
  5355. return $this->copyright;
  5356. }
  5357. else
  5358. {
  5359. return null;
  5360. }
  5361. }
  5362. /**
  5363. * Get a single credit
  5364. *
  5365. * @param int $key
  5366. * @return SimplePie_Credit|null
  5367. */
  5368. public function get_credit($key = 0)
  5369. {
  5370. $credits = $this->get_credits();
  5371. if (isset($credits[$key]))
  5372. {
  5373. return $credits[$key];
  5374. }
  5375. else
  5376. {
  5377. return null;
  5378. }
  5379. }
  5380. /**
  5381. * Get all credits
  5382. *
  5383. * @return array|null Array of {@see SimplePie_Credit} objects
  5384. */
  5385. public function get_credits()
  5386. {
  5387. if ($this->credits !== null)
  5388. {
  5389. return $this->credits;
  5390. }
  5391. else
  5392. {
  5393. return null;
  5394. }
  5395. }
  5396. /**
  5397. * Get the description of the enclosure
  5398. *
  5399. * @return string|null
  5400. */
  5401. public function get_description()
  5402. {
  5403. if ($this->description !== null)
  5404. {
  5405. return $this->description;
  5406. }
  5407. else
  5408. {
  5409. return null;
  5410. }
  5411. }
  5412. /**
  5413. * Get the duration of the enclosure
  5414. *
  5415. * @param string $convert Convert seconds into hh:mm:ss
  5416. * @return string|int|null 'hh:mm:ss' string if `$convert` was specified, otherwise integer (or null if none found)
  5417. */
  5418. public function get_duration($convert = false)
  5419. {
  5420. if ($this->duration !== null)
  5421. {
  5422. if ($convert)
  5423. {
  5424. $time = SimplePie_Misc::time_hms($this->duration);
  5425. return $time;
  5426. }
  5427. else
  5428. {
  5429. return $this->duration;
  5430. }
  5431. }
  5432. else
  5433. {
  5434. return null;
  5435. }
  5436. }
  5437. /**
  5438. * Get the expression
  5439. *
  5440. * @return string Probably one of 'sample', 'full', 'nonstop', 'clip'. Defaults to 'full'
  5441. */
  5442. public function get_expression()
  5443. {
  5444. if ($this->expression !== null)
  5445. {
  5446. return $this->expression;
  5447. }
  5448. else
  5449. {
  5450. return 'full';
  5451. }
  5452. }
  5453. /**
  5454. * Get the file extension
  5455. *
  5456. * @return string|null
  5457. */
  5458. public function get_extension()
  5459. {
  5460. if ($this->link !== null)
  5461. {
  5462. $url = SimplePie_Misc::parse_url($this->link);
  5463. if ($url['path'] !== '')
  5464. {
  5465. return pathinfo($url['path'], PATHINFO_EXTENSION);
  5466. }
  5467. }
  5468. return null;
  5469. }
  5470. /**
  5471. * Get the framerate (in frames-per-second)
  5472. *
  5473. * @return string|null
  5474. */
  5475. public function get_framerate()
  5476. {
  5477. if ($this->framerate !== null)
  5478. {
  5479. return $this->framerate;
  5480. }
  5481. else
  5482. {
  5483. return null;
  5484. }
  5485. }
  5486. /**
  5487. * Get the preferred handler
  5488. *
  5489. * @return string|null One of 'flash', 'fmedia', 'quicktime', 'wmedia', 'mp3'
  5490. */
  5491. public function get_handler()
  5492. {
  5493. return $this->get_real_type(true);
  5494. }
  5495. /**
  5496. * Get a single hash
  5497. *
  5498. * @link http://www.rssboard.org/media-rss#media-hash
  5499. * @param int $key
  5500. * @return string|null Hash as per `media:hash`, prefixed with "$algo:"
  5501. */
  5502. public function get_hash($key = 0)
  5503. {
  5504. $hashes = $this->get_hashes();
  5505. if (isset($hashes[$key]))
  5506. {
  5507. return $hashes[$key];
  5508. }
  5509. else
  5510. {
  5511. return null;
  5512. }
  5513. }
  5514. /**
  5515. * Get all credits
  5516. *
  5517. * @return array|null Array of strings, see {@see get_hash()}
  5518. */
  5519. public function get_hashes()
  5520. {
  5521. if ($this->hashes !== null)
  5522. {
  5523. return $this->hashes;
  5524. }
  5525. else
  5526. {
  5527. return null;
  5528. }
  5529. }
  5530. /**
  5531. * Get the height
  5532. *
  5533. * @return string|null
  5534. */
  5535. public function get_height()
  5536. {
  5537. if ($this->height !== null)
  5538. {
  5539. return $this->height;
  5540. }
  5541. else
  5542. {
  5543. return null;
  5544. }
  5545. }
  5546. /**
  5547. * Get the language
  5548. *
  5549. * @link http://tools.ietf.org/html/rfc3066
  5550. * @return string|null Language code as per RFC 3066
  5551. */
  5552. public function get_language()
  5553. {
  5554. if ($this->lang !== null)
  5555. {
  5556. return $this->lang;
  5557. }
  5558. else
  5559. {
  5560. return null;
  5561. }
  5562. }
  5563. /**
  5564. * Get a single keyword
  5565. *
  5566. * @param int $key
  5567. * @return string|null
  5568. */
  5569. public function get_keyword($key = 0)
  5570. {
  5571. $keywords = $this->get_keywords();
  5572. if (isset($keywords[$key]))
  5573. {
  5574. return $keywords[$key];
  5575. }
  5576. else
  5577. {
  5578. return null;
  5579. }
  5580. }
  5581. /**
  5582. * Get all keywords
  5583. *
  5584. * @return array|null Array of strings
  5585. */
  5586. public function get_keywords()
  5587. {
  5588. if ($this->keywords !== null)
  5589. {
  5590. return $this->keywords;
  5591. }
  5592. else
  5593. {
  5594. return null;
  5595. }
  5596. }
  5597. /**
  5598. * Get length
  5599. *
  5600. * @return float Length in bytes
  5601. */
  5602. public function get_length()
  5603. {
  5604. if ($this->length !== null)
  5605. {
  5606. return $this->length;
  5607. }
  5608. else
  5609. {
  5610. return null;
  5611. }
  5612. }
  5613. /**
  5614. * Get the URL
  5615. *
  5616. * @return string|null
  5617. */
  5618. public function get_link()
  5619. {
  5620. if ($this->link !== null)
  5621. {
  5622. return urldecode($this->link);
  5623. }
  5624. else
  5625. {
  5626. return null;
  5627. }
  5628. }
  5629. /**
  5630. * Get the medium
  5631. *
  5632. * @link http://www.rssboard.org/media-rss#media-content
  5633. * @return string|null Should be one of 'image', 'audio', 'video', 'document', 'executable'
  5634. */
  5635. public function get_medium()
  5636. {
  5637. if ($this->medium !== null)
  5638. {
  5639. return $this->medium;
  5640. }
  5641. else
  5642. {
  5643. return null;
  5644. }
  5645. }
  5646. /**
  5647. * Get the player URL
  5648. *
  5649. * Typically the same as {@see get_permalink()}
  5650. * @return string|null Player URL
  5651. */
  5652. public function get_player()
  5653. {
  5654. if ($this->player !== null)
  5655. {
  5656. return $this->player;
  5657. }
  5658. else
  5659. {
  5660. return null;
  5661. }
  5662. }
  5663. /**
  5664. * Get a single rating
  5665. *
  5666. * @param int $key
  5667. * @return SimplePie_Rating|null
  5668. */
  5669. public function get_rating($key = 0)
  5670. {
  5671. $ratings = $this->get_ratings();
  5672. if (isset($ratings[$key]))
  5673. {
  5674. return $ratings[$key];
  5675. }
  5676. else
  5677. {
  5678. return null;
  5679. }
  5680. }
  5681. /**
  5682. * Get all ratings
  5683. *
  5684. * @return array|null Array of {@see SimplePie_Rating} objects
  5685. */
  5686. public function get_ratings()
  5687. {
  5688. if ($this->ratings !== null)
  5689. {
  5690. return $this->ratings;
  5691. }
  5692. else
  5693. {
  5694. return null;
  5695. }
  5696. }
  5697. /**
  5698. * Get a single restriction
  5699. *
  5700. * @param int $key
  5701. * @return SimplePie_Restriction|null
  5702. */
  5703. public function get_restriction($key = 0)
  5704. {
  5705. $restrictions = $this->get_restrictions();
  5706. if (isset($restrictions[$key]))
  5707. {
  5708. return $restrictions[$key];
  5709. }
  5710. else
  5711. {
  5712. return null;
  5713. }
  5714. }
  5715. /**
  5716. * Get all restrictions
  5717. *
  5718. * @return array|null Array of {@see SimplePie_Restriction} objects
  5719. */
  5720. public function get_restrictions()
  5721. {
  5722. if ($this->restrictions !== null)
  5723. {
  5724. return $this->restrictions;
  5725. }
  5726. else
  5727. {
  5728. return null;
  5729. }
  5730. }
  5731. /**
  5732. * Get the sampling rate (in kHz)
  5733. *
  5734. * @return string|null
  5735. */
  5736. public function get_sampling_rate()
  5737. {
  5738. if ($this->samplingrate !== null)
  5739. {
  5740. return $this->samplingrate;
  5741. }
  5742. else
  5743. {
  5744. return null;
  5745. }
  5746. }
  5747. /**
  5748. * Get the file size (in MiB)
  5749. *
  5750. * @return float|null File size in mebibytes (1048 bytes)
  5751. */
  5752. public function get_size()
  5753. {
  5754. $length = $this->get_length();
  5755. if ($length !== null)
  5756. {
  5757. return round($length/1048576, 2);
  5758. }
  5759. else
  5760. {
  5761. return null;
  5762. }
  5763. }
  5764. /**
  5765. * Get a single thumbnail
  5766. *
  5767. * @param int $key
  5768. * @return string|null Thumbnail URL
  5769. */
  5770. public function get_thumbnail($key = 0)
  5771. {
  5772. $thumbnails = $this->get_thumbnails();
  5773. if (isset($thumbnails[$key]))
  5774. {
  5775. return $thumbnails[$key];
  5776. }
  5777. else
  5778. {
  5779. return null;
  5780. }
  5781. }
  5782. /**
  5783. * Get all thumbnails
  5784. *
  5785. * @return array|null Array of thumbnail URLs
  5786. */
  5787. public function get_thumbnails()
  5788. {
  5789. if ($this->thumbnails !== null)
  5790. {
  5791. return $this->thumbnails;
  5792. }
  5793. else
  5794. {
  5795. return null;
  5796. }
  5797. }
  5798. /**
  5799. * Get the title
  5800. *
  5801. * @return string|null
  5802. */
  5803. public function get_title()
  5804. {
  5805. if ($this->title !== null)
  5806. {
  5807. return $this->title;
  5808. }
  5809. else
  5810. {
  5811. return null;
  5812. }
  5813. }
  5814. /**
  5815. * Get mimetype of the enclosure
  5816. *
  5817. * @see get_real_type()
  5818. * @return string|null MIME type
  5819. */
  5820. public function get_type()
  5821. {
  5822. if ($this->type !== null)
  5823. {
  5824. return $this->type;
  5825. }
  5826. else
  5827. {
  5828. return null;
  5829. }
  5830. }
  5831. /**
  5832. * Get the width
  5833. *
  5834. * @return string|null
  5835. */
  5836. public function get_width()
  5837. {
  5838. if ($this->width !== null)
  5839. {
  5840. return $this->width;
  5841. }
  5842. else
  5843. {
  5844. return null;
  5845. }
  5846. }
  5847. /**
  5848. * Embed the enclosure using `<embed>`
  5849. *
  5850. * @deprecated Use the second parameter to {@see embed} instead
  5851. *
  5852. * @param array|string $options See first paramter to {@see embed}
  5853. * @return string HTML string to output
  5854. */
  5855. public function native_embed($options='')
  5856. {
  5857. return $this->embed($options, true);
  5858. }
  5859. /**
  5860. * Embed the enclosure using Javascript
  5861. *
  5862. * `$options` is an array or comma-separated key:value string, with the
  5863. * following properties:
  5864. *
  5865. * - `alt` (string): Alternate content for when an end-user does not have
  5866. * the appropriate handler installed or when a file type is
  5867. * unsupported. Can be any text or HTML. Defaults to blank.
  5868. * - `altclass` (string): If a file type is unsupported, the end-user will
  5869. * see the alt text (above) linked directly to the content. That link
  5870. * will have this value as its class name. Defaults to blank.
  5871. * - `audio` (string): This is an image that should be used as a
  5872. * placeholder for audio files before they're loaded (QuickTime-only).
  5873. * Can be any relative or absolute URL. Defaults to blank.
  5874. * - `bgcolor` (string): The background color for the media, if not
  5875. * already transparent. Defaults to `#ffffff`.
  5876. * - `height` (integer): The height of the embedded media. Accepts any
  5877. * numeric pixel value (such as `360`) or `auto`. Defaults to `auto`,
  5878. * and it is recommended that you use this default.
  5879. * - `loop` (boolean): Do you want the media to loop when its done?
  5880. * Defaults to `false`.
  5881. * - `mediaplayer` (string): The location of the included
  5882. * `mediaplayer.swf` file. This allows for the playback of Flash Video
  5883. * (`.flv`) files, and is the default handler for non-Odeo MP3's.
  5884. * Defaults to blank.
  5885. * - `video` (string): This is an image that should be used as a
  5886. * placeholder for video files before they're loaded (QuickTime-only).
  5887. * Can be any relative or absolute URL. Defaults to blank.
  5888. * - `width` (integer): The width of the embedded media. Accepts any
  5889. * numeric pixel value (such as `480`) or `auto`. Defaults to `auto`,
  5890. * and it is recommended that you use this default.
  5891. * - `widescreen` (boolean): Is the enclosure widescreen or standard?
  5892. * This applies only to video enclosures, and will automatically resize
  5893. * the content appropriately. Defaults to `false`, implying 4:3 mode.
  5894. *
  5895. * Note: Non-widescreen (4:3) mode with `width` and `height` set to `auto`
  5896. * will default to 480x360 video resolution. Widescreen (16:9) mode with
  5897. * `width` and `height` set to `auto` will default to 480x270 video resolution.
  5898. *
  5899. * @todo If the dimensions for media:content are defined, use them when width/height are set to 'auto'.
  5900. * @param array|string $options Comma-separated key:value list, or array
  5901. * @param bool $native Use `<embed>`
  5902. * @return string HTML string to output
  5903. */
  5904. public function embed($options = '', $native = false)
  5905. {
  5906. // Set up defaults
  5907. $audio = '';
  5908. $video = '';
  5909. $alt = '';
  5910. $altclass = '';
  5911. $loop = 'false';
  5912. $width = 'auto';
  5913. $height = 'auto';
  5914. $bgcolor = '#ffffff';
  5915. $mediaplayer = '';
  5916. $widescreen = false;
  5917. $handler = $this->get_handler();
  5918. $type = $this->get_real_type();
  5919. // Process options and reassign values as necessary
  5920. if (is_array($options))
  5921. {
  5922. extract($options);
  5923. }
  5924. else
  5925. {
  5926. $options = explode(',', $options);
  5927. foreach($options as $option)
  5928. {
  5929. $opt = explode(':', $option, 2);
  5930. if (isset($opt[0], $opt[1]))
  5931. {
  5932. $opt[0] = trim($opt[0]);
  5933. $opt[1] = trim($opt[1]);
  5934. switch ($opt[0])
  5935. {
  5936. case 'audio':
  5937. $audio = $opt[1];
  5938. break;
  5939. case 'video':
  5940. $video = $opt[1];
  5941. break;
  5942. case 'alt':
  5943. $alt = $opt[1];
  5944. break;
  5945. case 'altclass':
  5946. $altclass = $opt[1];
  5947. break;
  5948. case 'loop':
  5949. $loop = $opt[1];
  5950. break;
  5951. case 'width':
  5952. $width = $opt[1];
  5953. break;
  5954. case 'height':
  5955. $height = $opt[1];
  5956. break;
  5957. case 'bgcolor':
  5958. $bgcolor = $opt[1];
  5959. break;
  5960. case 'mediaplayer':
  5961. $mediaplayer = $opt[1];
  5962. break;
  5963. case 'widescreen':
  5964. $widescreen = $opt[1];
  5965. break;
  5966. }
  5967. }
  5968. }
  5969. }
  5970. $mime = explode('/', $type, 2);
  5971. $mime = $mime[0];
  5972. // Process values for 'auto'
  5973. if ($width === 'auto')
  5974. {
  5975. if ($mime === 'video')
  5976. {
  5977. if ($height === 'auto')
  5978. {
  5979. $width = 480;
  5980. }
  5981. elseif ($widescreen)
  5982. {
  5983. $width = round((intval($height)/9)*16);
  5984. }
  5985. else
  5986. {
  5987. $width = round((intval($height)/3)*4);
  5988. }
  5989. }
  5990. else
  5991. {
  5992. $width = '100%';
  5993. }
  5994. }
  5995. if ($height === 'auto')
  5996. {
  5997. if ($mime === 'audio')
  5998. {
  5999. $height = 0;
  6000. }
  6001. elseif ($mime === 'video')
  6002. {
  6003. if ($width === 'auto')
  6004. {
  6005. if ($widescreen)
  6006. {
  6007. $height = 270;
  6008. }
  6009. else
  6010. {
  6011. $height = 360;
  6012. }
  6013. }
  6014. elseif ($widescreen)
  6015. {
  6016. $height = round((intval($width)/16)*9);
  6017. }
  6018. else
  6019. {
  6020. $height = round((intval($width)/4)*3);
  6021. }
  6022. }
  6023. else
  6024. {
  6025. $height = 376;
  6026. }
  6027. }
  6028. elseif ($mime === 'audio')
  6029. {
  6030. $height = 0;
  6031. }
  6032. // Set proper placeholder value
  6033. if ($mime === 'audio')
  6034. {
  6035. $placeholder = $audio;
  6036. }
  6037. elseif ($mime === 'video')
  6038. {
  6039. $placeholder = $video;
  6040. }
  6041. $embed = '';
  6042. // Flash
  6043. if ($handler === 'flash')
  6044. {
  6045. if ($native)
  6046. {
  6047. $embed .= "<embed src=\"" . $this->get_link() . "\" pluginspage=\"http://adobe.com/go/getflashplayer\" type=\"$type\" quality=\"high\" width=\"$width\" height=\"$height\" bgcolor=\"$bgcolor\" loop=\"$loop\"></embed>";
  6048. }
  6049. else
  6050. {
  6051. $embed .= "<script type='text/javascript'>embed_flash('$bgcolor', '$width', '$height', '" . $this->get_link() . "', '$loop', '$type');</script>";
  6052. }
  6053. }
  6054. // Flash Media Player file types.
  6055. // Preferred handler for MP3 file types.
  6056. elseif ($handler === 'fmedia' || ($handler === 'mp3' && $mediaplayer !== ''))
  6057. {
  6058. $height += 20;
  6059. if ($native)
  6060. {
  6061. $embed .= "<embed src=\"$mediaplayer\" pluginspage=\"http://adobe.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" quality=\"high\" width=\"$width\" height=\"$height\" wmode=\"transparent\" flashvars=\"file=" . rawurlencode($this->get_link().'?file_extension=.'.$this->get_extension()) . "&autostart=false&repeat=$loop&showdigits=true&showfsbutton=false\"></embed>";
  6062. }
  6063. else
  6064. {
  6065. $embed .= "<script type='text/javascript'>embed_flv('$width', '$height', '" . rawurlencode($this->get_link().'?file_extension=.'.$this->get_extension()) . "', '$placeholder', '$loop', '$mediaplayer');</script>";
  6066. }
  6067. }
  6068. // QuickTime 7 file types. Need to test with QuickTime 6.
  6069. // Only handle MP3's if the Flash Media Player is not present.
  6070. elseif ($handler === 'quicktime' || ($handler === 'mp3' && $mediaplayer === ''))
  6071. {
  6072. $height += 16;
  6073. if ($native)
  6074. {
  6075. if ($placeholder !== '')
  6076. {
  6077. $embed .= "<embed type=\"$type\" style=\"cursor:hand; cursor:pointer;\" href=\"" . $this->get_link() . "\" src=\"$placeholder\" width=\"$width\" height=\"$height\" autoplay=\"false\" target=\"myself\" controller=\"false\" loop=\"$loop\" scale=\"aspect\" bgcolor=\"$bgcolor\" pluginspage=\"http://apple.com/quicktime/download/\"></embed>";
  6078. }
  6079. else
  6080. {
  6081. $embed .= "<embed type=\"$type\" style=\"cursor:hand; cursor:pointer;\" src=\"" . $this->get_link() . "\" width=\"$width\" height=\"$height\" autoplay=\"false\" target=\"myself\" controller=\"true\" loop=\"$loop\" scale=\"aspect\" bgcolor=\"$bgcolor\" pluginspage=\"http://apple.com/quicktime/download/\"></embed>";
  6082. }
  6083. }
  6084. else
  6085. {
  6086. $embed .= "<script type='text/javascript'>embed_quicktime('$type', '$bgcolor', '$width', '$height', '" . $this->get_link() . "', '$placeholder', '$loop');</script>";
  6087. }
  6088. }
  6089. // Windows Media
  6090. elseif ($handler === 'wmedia')
  6091. {
  6092. $height += 45;
  6093. if ($native)
  6094. {
  6095. $embed .= "<embed type=\"application/x-mplayer2\" src=\"" . $this->get_link() . "\" autosize=\"1\" width=\"$width\" height=\"$height\" showcontrols=\"1\" showstatusbar=\"0\" showdisplay=\"0\" autostart=\"0\"></embed>";
  6096. }
  6097. else
  6098. {
  6099. $embed .= "<script type='text/javascript'>embed_wmedia('$width', '$height', '" . $this->get_link() . "');</script>";
  6100. }
  6101. }
  6102. // Everything else
  6103. else $embed .= '<a href="' . $this->get_link() . '" class="' . $altclass . '">' . $alt . '</a>';
  6104. return $embed;
  6105. }
  6106. /**
  6107. * Get the real media type
  6108. *
  6109. * Often, feeds lie to us, necessitating a bit of deeper inspection. This
  6110. * converts types to their canonical representations based on the file
  6111. * extension
  6112. *
  6113. * @see get_type()
  6114. * @param bool $find_handler Internal use only, use {@see get_handler()} instead
  6115. * @return string MIME type
  6116. */
  6117. public function get_real_type($find_handler = false)
  6118. {
  6119. // Mime-types by handler.
  6120. $types_flash = array('application/x-shockwave-flash', 'application/futuresplash'); // Flash
  6121. $types_fmedia = array('video/flv', 'video/x-flv','flv-application/octet-stream'); // Flash Media Player
  6122. $types_quicktime = array('audio/3gpp', 'audio/3gpp2', 'audio/aac', 'audio/x-aac', 'audio/aiff', 'audio/x-aiff', 'audio/mid', 'audio/midi', 'audio/x-midi', 'audio/mp4', 'audio/m4a', 'audio/x-m4a', 'audio/wav', 'audio/x-wav', 'video/3gpp', 'video/3gpp2', 'video/m4v', 'video/x-m4v', 'video/mp4', 'video/mpeg', 'video/x-mpeg', 'video/quicktime', 'video/sd-video'); // QuickTime
  6123. $types_wmedia = array('application/asx', 'application/x-mplayer2', 'audio/x-ms-wma', 'audio/x-ms-wax', 'video/x-ms-asf-plugin', 'video/x-ms-asf', 'video/x-ms-wm', 'video/x-ms-wmv', 'video/x-ms-wvx'); // Windows Media
  6124. $types_mp3 = array('audio/mp3', 'audio/x-mp3', 'audio/mpeg', 'audio/x-mpeg'); // MP3
  6125. if ($this->get_type() !== null)
  6126. {
  6127. $type = strtolower($this->type);
  6128. }
  6129. else
  6130. {
  6131. $type = null;
  6132. }
  6133. // If we encounter an unsupported mime-type, check the file extension and guess intelligently.
  6134. if (!in_array($type, array_merge($types_flash, $types_fmedia, $types_quicktime, $types_wmedia, $types_mp3)))
  6135. {
  6136. switch (strtolower($this->get_extension()))
  6137. {
  6138. // Audio mime-types
  6139. case 'aac':
  6140. case 'adts':
  6141. $type = 'audio/acc';
  6142. break;
  6143. case 'aif':
  6144. case 'aifc':
  6145. case 'aiff':
  6146. case 'cdda':
  6147. $type = 'audio/aiff';
  6148. break;
  6149. case 'bwf':
  6150. $type = 'audio/wav';
  6151. break;
  6152. case 'kar':
  6153. case 'mid':
  6154. case 'midi':
  6155. case 'smf':
  6156. $type = 'audio/midi';
  6157. break;
  6158. case 'm4a':
  6159. $type = 'audio/x-m4a';
  6160. break;
  6161. case 'mp3':
  6162. case 'swa':
  6163. $type = 'audio/mp3';
  6164. break;
  6165. case 'wav':
  6166. $type = 'audio/wav';
  6167. break;
  6168. case 'wax':
  6169. $type = 'audio/x-ms-wax';
  6170. break;
  6171. case 'wma':
  6172. $type = 'audio/x-ms-wma';
  6173. break;
  6174. // Video mime-types
  6175. case '3gp':
  6176. case '3gpp':
  6177. $type = 'video/3gpp';
  6178. break;
  6179. case '3g2':
  6180. case '3gp2':
  6181. $type = 'video/3gpp2';
  6182. break;
  6183. case 'asf':
  6184. $type = 'video/x-ms-asf';
  6185. break;
  6186. case 'flv':
  6187. $type = 'video/x-flv';
  6188. break;
  6189. case 'm1a':
  6190. case 'm1s':
  6191. case 'm1v':
  6192. case 'm15':
  6193. case 'm75':
  6194. case 'mp2':
  6195. case 'mpa':
  6196. case 'mpeg':
  6197. case 'mpg':
  6198. case 'mpm':
  6199. case 'mpv':
  6200. $type = 'video/mpeg';
  6201. break;
  6202. case 'm4v':
  6203. $type = 'video/x-m4v';
  6204. break;
  6205. case 'mov':
  6206. case 'qt':
  6207. $type = 'video/quicktime';
  6208. break;
  6209. case 'mp4':
  6210. case 'mpg4':
  6211. $type = 'video/mp4';
  6212. break;
  6213. case 'sdv':
  6214. $type = 'video/sd-video';
  6215. break;
  6216. case 'wm':
  6217. $type = 'video/x-ms-wm';
  6218. break;
  6219. case 'wmv':
  6220. $type = 'video/x-ms-wmv';
  6221. break;
  6222. case 'wvx':
  6223. $type = 'video/x-ms-wvx';
  6224. break;
  6225. // Flash mime-types
  6226. case 'spl':
  6227. $type = 'application/futuresplash';
  6228. break;
  6229. case 'swf':
  6230. $type = 'application/x-shockwave-flash';
  6231. break;
  6232. }
  6233. }
  6234. if ($find_handler)
  6235. {
  6236. if (in_array($type, $types_flash))
  6237. {
  6238. return 'flash';
  6239. }
  6240. elseif (in_array($type, $types_fmedia))
  6241. {
  6242. return 'fmedia';
  6243. }
  6244. elseif (in_array($type, $types_quicktime))
  6245. {
  6246. return 'quicktime';
  6247. }
  6248. elseif (in_array($type, $types_wmedia))
  6249. {
  6250. return 'wmedia';
  6251. }
  6252. elseif (in_array($type, $types_mp3))
  6253. {
  6254. return 'mp3';
  6255. }
  6256. else
  6257. {
  6258. return null;
  6259. }
  6260. }
  6261. else
  6262. {
  6263. return $type;
  6264. }
  6265. }
  6266. }
  6267. /**
  6268. * General SimplePie exception class
  6269. *
  6270. * @package SimplePie
  6271. */
  6272. class SimplePie_Exception extends Exception
  6273. {
  6274. }
  6275. /**
  6276. * Used for fetching remote files and reading local files
  6277. *
  6278. * Supports HTTP 1.0 via cURL or fsockopen, with spotty HTTP 1.1 support
  6279. *
  6280. * This class can be overloaded with {@see SimplePie::set_file_class()}
  6281. *
  6282. * @package SimplePie
  6283. * @subpackage HTTP
  6284. * @todo Move to properly supporting RFC2616 (HTTP/1.1)
  6285. */
  6286. class SimplePie_File
  6287. {
  6288. var $url;
  6289. var $useragent;
  6290. var $success = true;
  6291. var $headers = array();
  6292. var $body;
  6293. var $status_code;
  6294. var $redirects = 0;
  6295. var $error;
  6296. var $method = SIMPLEPIE_FILE_SOURCE_NONE;
  6297. public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false)
  6298. {
  6299. if (class_exists('idna_convert'))
  6300. {
  6301. $idn = new idna_convert();
  6302. $parsed = SimplePie_Misc::parse_url($url);
  6303. $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
  6304. }
  6305. $this->url = $url;
  6306. $this->useragent = $useragent;
  6307. if (preg_match('/^http(s)?:\/\//i', $url))
  6308. {
  6309. if ($useragent === null)
  6310. {
  6311. $useragent = ini_get('user_agent');
  6312. $this->useragent = $useragent;
  6313. }
  6314. if (!is_array($headers))
  6315. {
  6316. $headers = array();
  6317. }
  6318. if (!$force_fsockopen && function_exists('curl_exec'))
  6319. {
  6320. $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
  6321. $fp = curl_init();
  6322. $headers2 = array();
  6323. foreach ($headers as $key => $value)
  6324. {
  6325. $headers2[] = "$key: $value";
  6326. }
  6327. if (version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>='))
  6328. {
  6329. curl_setopt($fp, CURLOPT_ENCODING, '');
  6330. }
  6331. curl_setopt($fp, CURLOPT_URL, $url);
  6332. curl_setopt($fp, CURLOPT_HEADER, 1);
  6333. curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
  6334. curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
  6335. curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout);
  6336. curl_setopt($fp, CURLOPT_REFERER, $url);
  6337. curl_setopt($fp, CURLOPT_USERAGENT, $useragent);
  6338. curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
  6339. if (!ini_get('open_basedir') && !ini_get('safe_mode') && version_compare(SimplePie_Misc::get_curl_version(), '7.15.2', '>='))
  6340. {
  6341. curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1);
  6342. curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects);
  6343. }
  6344. $this->headers = curl_exec($fp);
  6345. if (curl_errno($fp) === 23 || curl_errno($fp) === 61)
  6346. {
  6347. curl_setopt($fp, CURLOPT_ENCODING, 'none');
  6348. $this->headers = curl_exec($fp);
  6349. }
  6350. if (curl_errno($fp))
  6351. {
  6352. $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp);
  6353. $this->success = false;
  6354. }
  6355. else
  6356. {
  6357. $info = curl_getinfo($fp);
  6358. curl_close($fp);
  6359. $this->headers = explode("\r\n\r\n", $this->headers, $info['redirect_count'] + 1);
  6360. $this->headers = array_pop($this->headers);
  6361. $parser = new SimplePie_HTTP_Parser($this->headers);
  6362. if ($parser->parse())
  6363. {
  6364. $this->headers = $parser->headers;
  6365. $this->body = $parser->body;
  6366. $this->status_code = $parser->status_code;
  6367. if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects)
  6368. {
  6369. $this->redirects++;
  6370. $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
  6371. return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
  6372. }
  6373. }
  6374. }
  6375. }
  6376. else
  6377. {
  6378. $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN;
  6379. $url_parts = parse_url($url);
  6380. $socket_host = $url_parts['host'];
  6381. if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https')
  6382. {
  6383. $socket_host = "ssl://$url_parts[host]";
  6384. $url_parts['port'] = 443;
  6385. }
  6386. if (!isset($url_parts['port']))
  6387. {
  6388. $url_parts['port'] = 80;
  6389. }
  6390. $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout);
  6391. if (!$fp)
  6392. {
  6393. $this->error = 'fsockopen error: ' . $errstr;
  6394. $this->success = false;
  6395. }
  6396. else
  6397. {
  6398. stream_set_timeout($fp, $timeout);
  6399. if (isset($url_parts['path']))
  6400. {
  6401. if (isset($url_parts['query']))
  6402. {
  6403. $get = "$url_parts[path]?$url_parts[query]";
  6404. }
  6405. else
  6406. {
  6407. $get = $url_parts['path'];
  6408. }
  6409. }
  6410. else
  6411. {
  6412. $get = '/';
  6413. }
  6414. $out = "GET $get HTTP/1.1\r\n";
  6415. $out .= "Host: $url_parts[host]\r\n";
  6416. $out .= "User-Agent: $useragent\r\n";
  6417. if (extension_loaded('zlib'))
  6418. {
  6419. $out .= "Accept-Encoding: x-gzip,gzip,deflate\r\n";
  6420. }
  6421. if (isset($url_parts['user']) && isset($url_parts['pass']))
  6422. {
  6423. $out .= "Authorization: Basic " . base64_encode("$url_parts[user]:$url_parts[pass]") . "\r\n";
  6424. }
  6425. foreach ($headers as $key => $value)
  6426. {
  6427. $out .= "$key: $value\r\n";
  6428. }
  6429. $out .= "Connection: Close\r\n\r\n";
  6430. fwrite($fp, $out);
  6431. $info = stream_get_meta_data($fp);
  6432. $this->headers = '';
  6433. while (!$info['eof'] && !$info['timed_out'])
  6434. {
  6435. $this->headers .= fread($fp, 1160);
  6436. $info = stream_get_meta_data($fp);
  6437. }
  6438. if (!$info['timed_out'])
  6439. {
  6440. $parser = new SimplePie_HTTP_Parser($this->headers);
  6441. if ($parser->parse())
  6442. {
  6443. $this->headers = $parser->headers;
  6444. $this->body = $parser->body;
  6445. $this->status_code = $parser->status_code;
  6446. if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects)
  6447. {
  6448. $this->redirects++;
  6449. $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
  6450. return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
  6451. }
  6452. if (isset($this->headers['content-encoding']))
  6453. {
  6454. // Hey, we act dumb elsewhere, so let's do that here too
  6455. switch (strtolower(trim($this->headers['content-encoding'], "\x09\x0A\x0D\x20")))
  6456. {
  6457. case 'gzip':
  6458. case 'x-gzip':
  6459. $decoder = new SimplePie_gzdecode($this->body);
  6460. if (!$decoder->parse())
  6461. {
  6462. $this->error = 'Unable to decode HTTP "gzip" stream';
  6463. $this->success = false;
  6464. }
  6465. else
  6466. {
  6467. $this->body = $decoder->data;
  6468. }
  6469. break;
  6470. case 'deflate':
  6471. if (($decompressed = gzinflate($this->body)) !== false)
  6472. {
  6473. $this->body = $decompressed;
  6474. }
  6475. else if (($decompressed = gzuncompress($this->body)) !== false)
  6476. {
  6477. $this->body = $decompressed;
  6478. }
  6479. else if (function_exists('gzdecode') && ($decompressed = gzdecode($this->body)) !== false)
  6480. {
  6481. $this->body = $decompressed;
  6482. }
  6483. else
  6484. {
  6485. $this->error = 'Unable to decode HTTP "deflate" stream';
  6486. $this->success = false;
  6487. }
  6488. break;
  6489. default:
  6490. $this->error = 'Unknown content coding';
  6491. $this->success = false;
  6492. }
  6493. }
  6494. }
  6495. }
  6496. else
  6497. {
  6498. $this->error = 'fsocket timed out';
  6499. $this->success = false;
  6500. }
  6501. fclose($fp);
  6502. }
  6503. }
  6504. }
  6505. else
  6506. {
  6507. $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS;
  6508. if (!$this->body = file_get_contents($url))
  6509. {
  6510. $this->error = 'file_get_contents could not read the file';
  6511. $this->success = false;
  6512. }
  6513. }
  6514. }
  6515. }
  6516. /**
  6517. * Decode 'gzip' encoded HTTP data
  6518. *
  6519. * @package SimplePie
  6520. * @subpackage HTTP
  6521. * @link http://www.gzip.org/format.txt
  6522. */
  6523. class SimplePie_gzdecode
  6524. {
  6525. /**
  6526. * Compressed data
  6527. *
  6528. * @access private
  6529. * @var string
  6530. * @see gzdecode::$data
  6531. */
  6532. var $compressed_data;
  6533. /**
  6534. * Size of compressed data
  6535. *
  6536. * @access private
  6537. * @var int
  6538. */
  6539. var $compressed_size;
  6540. /**
  6541. * Minimum size of a valid gzip string
  6542. *
  6543. * @access private
  6544. * @var int
  6545. */
  6546. var $min_compressed_size = 18;
  6547. /**
  6548. * Current position of pointer
  6549. *
  6550. * @access private
  6551. * @var int
  6552. */
  6553. var $position = 0;
  6554. /**
  6555. * Flags (FLG)
  6556. *
  6557. * @access private
  6558. * @var int
  6559. */
  6560. var $flags;
  6561. /**
  6562. * Uncompressed data
  6563. *
  6564. * @access public
  6565. * @see gzdecode::$compressed_data
  6566. * @var string
  6567. */
  6568. var $data;
  6569. /**
  6570. * Modified time
  6571. *
  6572. * @access public
  6573. * @var int
  6574. */
  6575. var $MTIME;
  6576. /**
  6577. * Extra Flags
  6578. *
  6579. * @access public
  6580. * @var int
  6581. */
  6582. var $XFL;
  6583. /**
  6584. * Operating System
  6585. *
  6586. * @access public
  6587. * @var int
  6588. */
  6589. var $OS;
  6590. /**
  6591. * Subfield ID 1
  6592. *
  6593. * @access public
  6594. * @see gzdecode::$extra_field
  6595. * @see gzdecode::$SI2
  6596. * @var string
  6597. */
  6598. var $SI1;
  6599. /**
  6600. * Subfield ID 2
  6601. *
  6602. * @access public
  6603. * @see gzdecode::$extra_field
  6604. * @see gzdecode::$SI1
  6605. * @var string
  6606. */
  6607. var $SI2;
  6608. /**
  6609. * Extra field content
  6610. *
  6611. * @access public
  6612. * @see gzdecode::$SI1
  6613. * @see gzdecode::$SI2
  6614. * @var string
  6615. */
  6616. var $extra_field;
  6617. /**
  6618. * Original filename
  6619. *
  6620. * @access public
  6621. * @var string
  6622. */
  6623. var $filename;
  6624. /**
  6625. * Human readable comment
  6626. *
  6627. * @access public
  6628. * @var string
  6629. */
  6630. var $comment;
  6631. /**
  6632. * Don't allow anything to be set
  6633. *
  6634. * @param string $name
  6635. * @param mixed $value
  6636. */
  6637. public function __set($name, $value)
  6638. {
  6639. trigger_error("Cannot write property $name", E_USER_ERROR);
  6640. }
  6641. /**
  6642. * Set the compressed string and related properties
  6643. *
  6644. * @param string $data
  6645. */
  6646. public function __construct($data)
  6647. {
  6648. $this->compressed_data = $data;
  6649. $this->compressed_size = strlen($data);
  6650. }
  6651. /**
  6652. * Decode the GZIP stream
  6653. *
  6654. * @return bool Successfulness
  6655. */
  6656. public function parse()
  6657. {
  6658. if ($this->compressed_size >= $this->min_compressed_size)
  6659. {
  6660. // Check ID1, ID2, and CM
  6661. if (substr($this->compressed_data, 0, 3) !== "\x1F\x8B\x08")
  6662. {
  6663. return false;
  6664. }
  6665. // Get the FLG (FLaGs)
  6666. $this->flags = ord($this->compressed_data[3]);
  6667. // FLG bits above (1 << 4) are reserved
  6668. if ($this->flags > 0x1F)
  6669. {
  6670. return false;
  6671. }
  6672. // Advance the pointer after the above
  6673. $this->position += 4;
  6674. // MTIME
  6675. $mtime = substr($this->compressed_data, $this->position, 4);
  6676. // Reverse the string if we're on a big-endian arch because l is the only signed long and is machine endianness
  6677. if (current(unpack('S', "\x00\x01")) === 1)
  6678. {
  6679. $mtime = strrev($mtime);
  6680. }
  6681. $this->MTIME = current(unpack('l', $mtime));
  6682. $this->position += 4;
  6683. // Get the XFL (eXtra FLags)
  6684. $this->XFL = ord($this->compressed_data[$this->position++]);
  6685. // Get the OS (Operating System)
  6686. $this->OS = ord($this->compressed_data[$this->position++]);
  6687. // Parse the FEXTRA
  6688. if ($this->flags & 4)
  6689. {
  6690. // Read subfield IDs
  6691. $this->SI1 = $this->compressed_data[$this->position++];
  6692. $this->SI2 = $this->compressed_data[$this->position++];
  6693. // SI2 set to zero is reserved for future use
  6694. if ($this->SI2 === "\x00")
  6695. {
  6696. return false;
  6697. }
  6698. // Get the length of the extra field
  6699. $len = current(unpack('v', substr($this->compressed_data, $this->position, 2)));
  6700. $this->position += 2;
  6701. // Check the length of the string is still valid
  6702. $this->min_compressed_size += $len + 4;
  6703. if ($this->compressed_size >= $this->min_compressed_size)
  6704. {
  6705. // Set the extra field to the given data
  6706. $this->extra_field = substr($this->compressed_data, $this->position, $len);
  6707. $this->position += $len;
  6708. }
  6709. else
  6710. {
  6711. return false;
  6712. }
  6713. }
  6714. // Parse the FNAME
  6715. if ($this->flags & 8)
  6716. {
  6717. // Get the length of the filename
  6718. $len = strcspn($this->compressed_data, "\x00", $this->position);
  6719. // Check the length of the string is still valid
  6720. $this->min_compressed_size += $len + 1;
  6721. if ($this->compressed_size >= $this->min_compressed_size)
  6722. {
  6723. // Set the original filename to the given string
  6724. $this->filename = substr($this->compressed_data, $this->position, $len);
  6725. $this->position += $len + 1;
  6726. }
  6727. else
  6728. {
  6729. return false;
  6730. }
  6731. }
  6732. // Parse the FCOMMENT
  6733. if ($this->flags & 16)
  6734. {
  6735. // Get the length of the comment
  6736. $len = strcspn($this->compressed_data, "\x00", $this->position);
  6737. // Check the length of the string is still valid
  6738. $this->min_compressed_size += $len + 1;
  6739. if ($this->compressed_size >= $this->min_compressed_size)
  6740. {
  6741. // Set the original comment to the given string
  6742. $this->comment = substr($this->compressed_data, $this->position, $len);
  6743. $this->position += $len + 1;
  6744. }
  6745. else
  6746. {
  6747. return false;
  6748. }
  6749. }
  6750. // Parse the FHCRC
  6751. if ($this->flags & 2)
  6752. {
  6753. // Check the length of the string is still valid
  6754. $this->min_compressed_size += $len + 2;
  6755. if ($this->compressed_size >= $this->min_compressed_size)
  6756. {
  6757. // Read the CRC
  6758. $crc = current(unpack('v', substr($this->compressed_data, $this->position, 2)));
  6759. // Check the CRC matches
  6760. if ((crc32(substr($this->compressed_data, 0, $this->position)) & 0xFFFF) === $crc)
  6761. {
  6762. $this->position += 2;
  6763. }
  6764. else
  6765. {
  6766. return false;
  6767. }
  6768. }
  6769. else
  6770. {
  6771. return false;
  6772. }
  6773. }
  6774. // Decompress the actual data
  6775. if (($this->data = gzinflate(substr($this->compressed_data, $this->position, -8))) === false)
  6776. {
  6777. return false;
  6778. }
  6779. else
  6780. {
  6781. $this->position = $this->compressed_size - 8;
  6782. }
  6783. // Check CRC of data
  6784. $crc = current(unpack('V', substr($this->compressed_data, $this->position, 4)));
  6785. $this->position += 4;
  6786. /*if (extension_loaded('hash') && sprintf('%u', current(unpack('V', hash('crc32b', $this->data)))) !== sprintf('%u', $crc))
  6787. {
  6788. return false;
  6789. }*/
  6790. // Check ISIZE of data
  6791. $isize = current(unpack('V', substr($this->compressed_data, $this->position, 4)));
  6792. $this->position += 4;
  6793. if (sprintf('%u', strlen($this->data) & 0xFFFFFFFF) !== sprintf('%u', $isize))
  6794. {
  6795. return false;
  6796. }
  6797. // Wow, against all odds, we've actually got a valid gzip string
  6798. return true;
  6799. }
  6800. else
  6801. {
  6802. return false;
  6803. }
  6804. }
  6805. }
  6806. /**
  6807. * HTTP Response Parser
  6808. *
  6809. * @package SimplePie
  6810. * @subpackage HTTP
  6811. */
  6812. class SimplePie_HTTP_Parser
  6813. {
  6814. /**
  6815. * HTTP Version
  6816. *
  6817. * @var float
  6818. */
  6819. public $http_version = 0.0;
  6820. /**
  6821. * Status code
  6822. *
  6823. * @var int
  6824. */
  6825. public $status_code = 0;
  6826. /**
  6827. * Reason phrase
  6828. *
  6829. * @var string
  6830. */
  6831. public $reason = '';
  6832. /**
  6833. * Key/value pairs of the headers
  6834. *
  6835. * @var array
  6836. */
  6837. public $headers = array();
  6838. /**
  6839. * Body of the response
  6840. *
  6841. * @var string
  6842. */
  6843. public $body = '';
  6844. /**
  6845. * Current state of the state machine
  6846. *
  6847. * @var string
  6848. */
  6849. protected $state = 'http_version';
  6850. /**
  6851. * Input data
  6852. *
  6853. * @var string
  6854. */
  6855. protected $data = '';
  6856. /**
  6857. * Input data length (to avoid calling strlen() everytime this is needed)
  6858. *
  6859. * @var int
  6860. */
  6861. protected $data_length = 0;
  6862. /**
  6863. * Current position of the pointer
  6864. *
  6865. * @var int
  6866. */
  6867. protected $position = 0;
  6868. /**
  6869. * Name of the hedaer currently being parsed
  6870. *
  6871. * @var string
  6872. */
  6873. protected $name = '';
  6874. /**
  6875. * Value of the hedaer currently being parsed
  6876. *
  6877. * @var string
  6878. */
  6879. protected $value = '';
  6880. /**
  6881. * Create an instance of the class with the input data
  6882. *
  6883. * @param string $data Input data
  6884. */
  6885. public function __construct($data)
  6886. {
  6887. $this->data = $data;
  6888. $this->data_length = strlen($this->data);
  6889. }
  6890. /**
  6891. * Parse the input data
  6892. *
  6893. * @return bool true on success, false on failure
  6894. */
  6895. public function parse()
  6896. {
  6897. while ($this->state && $this->state !== 'emit' && $this->has_data())
  6898. {
  6899. $state = $this->state;
  6900. $this->$state();
  6901. }
  6902. $this->data = '';
  6903. if ($this->state === 'emit' || $this->state === 'body')
  6904. {
  6905. return true;
  6906. }
  6907. else
  6908. {
  6909. $this->http_version = '';
  6910. $this->status_code = '';
  6911. $this->reason = '';
  6912. $this->headers = array();
  6913. $this->body = '';
  6914. return false;
  6915. }
  6916. }
  6917. /**
  6918. * Check whether there is data beyond the pointer
  6919. *
  6920. * @return bool true if there is further data, false if not
  6921. */
  6922. protected function has_data()
  6923. {
  6924. return (bool) ($this->position < $this->data_length);
  6925. }
  6926. /**
  6927. * See if the next character is LWS
  6928. *
  6929. * @return bool true if the next character is LWS, false if not
  6930. */
  6931. protected function is_linear_whitespace()
  6932. {
  6933. return (bool) ($this->data[$this->position] === "\x09"
  6934. || $this->data[$this->position] === "\x20"
  6935. || ($this->data[$this->position] === "\x0A"
  6936. && isset($this->data[$this->position + 1])
  6937. && ($this->data[$this->position + 1] === "\x09" || $this->data[$this->position + 1] === "\x20")));
  6938. }
  6939. /**
  6940. * Parse the HTTP version
  6941. */
  6942. protected function http_version()
  6943. {
  6944. if (strpos($this->data, "\x0A") !== false && strtoupper(substr($this->data, 0, 5)) === 'HTTP/')
  6945. {
  6946. $len = strspn($this->data, '0123456789.', 5);
  6947. $this->http_version = substr($this->data, 5, $len);
  6948. $this->position += 5 + $len;
  6949. if (substr_count($this->http_version, '.') <= 1)
  6950. {
  6951. $this->http_version = (float) $this->http_version;
  6952. $this->position += strspn($this->data, "\x09\x20", $this->position);
  6953. $this->state = 'status';
  6954. }
  6955. else
  6956. {
  6957. $this->state = false;
  6958. }
  6959. }
  6960. else
  6961. {
  6962. $this->state = false;
  6963. }
  6964. }
  6965. /**
  6966. * Parse the status code
  6967. */
  6968. protected function status()
  6969. {
  6970. if ($len = strspn($this->data, '0123456789', $this->position))
  6971. {
  6972. $this->status_code = (int) substr($this->data, $this->position, $len);
  6973. $this->position += $len;
  6974. $this->state = 'reason';
  6975. }
  6976. else
  6977. {
  6978. $this->state = false;
  6979. }
  6980. }
  6981. /**
  6982. * Parse the reason phrase
  6983. */
  6984. protected function reason()
  6985. {
  6986. $len = strcspn($this->data, "\x0A", $this->position);
  6987. $this->reason = trim(substr($this->data, $this->position, $len), "\x09\x0D\x20");
  6988. $this->position += $len + 1;
  6989. $this->state = 'new_line';
  6990. }
  6991. /**
  6992. * Deal with a new line, shifting data around as needed
  6993. */
  6994. protected function new_line()
  6995. {
  6996. $this->value = trim($this->value, "\x0D\x20");
  6997. if ($this->name !== '' && $this->value !== '')
  6998. {
  6999. $this->name = strtolower($this->name);
  7000. // We should only use the last Content-Type header. c.f. issue #1
  7001. if (isset($this->headers[$this->name]) && $this->name !== 'content-type')
  7002. {
  7003. $this->headers[$this->name] .= ', ' . $this->value;
  7004. }
  7005. else
  7006. {
  7007. $this->headers[$this->name] = $this->value;
  7008. }
  7009. }
  7010. $this->name = '';
  7011. $this->value = '';
  7012. if (substr($this->data[$this->position], 0, 2) === "\x0D\x0A")
  7013. {
  7014. $this->position += 2;
  7015. $this->state = 'body';
  7016. }
  7017. elseif ($this->data[$this->position] === "\x0A")
  7018. {
  7019. $this->position++;
  7020. $this->state = 'body';
  7021. }
  7022. else
  7023. {
  7024. $this->state = 'name';
  7025. }
  7026. }
  7027. /**
  7028. * Parse a header name
  7029. */
  7030. protected function name()
  7031. {
  7032. $len = strcspn($this->data, "\x0A:", $this->position);
  7033. if (isset($this->data[$this->position + $len]))
  7034. {
  7035. if ($this->data[$this->position + $len] === "\x0A")
  7036. {
  7037. $this->position += $len;
  7038. $this->state = 'new_line';
  7039. }
  7040. else
  7041. {
  7042. $this->name = substr($this->data, $this->position, $len);
  7043. $this->position += $len + 1;
  7044. $this->state = 'value';
  7045. }
  7046. }
  7047. else
  7048. {
  7049. $this->state = false;
  7050. }
  7051. }
  7052. /**
  7053. * Parse LWS, replacing consecutive LWS characters with a single space
  7054. */
  7055. protected function linear_whitespace()
  7056. {
  7057. do
  7058. {
  7059. if (substr($this->data, $this->position, 2) === "\x0D\x0A")
  7060. {
  7061. $this->position += 2;
  7062. }
  7063. elseif ($this->data[$this->position] === "\x0A")
  7064. {
  7065. $this->position++;
  7066. }
  7067. $this->position += strspn($this->data, "\x09\x20", $this->position);
  7068. } while ($this->has_data() && $this->is_linear_whitespace());
  7069. $this->value .= "\x20";
  7070. }
  7071. /**
  7072. * See what state to move to while within non-quoted header values
  7073. */
  7074. protected function value()
  7075. {
  7076. if ($this->is_linear_whitespace())
  7077. {
  7078. $this->linear_whitespace();
  7079. }
  7080. else
  7081. {
  7082. switch ($this->data[$this->position])
  7083. {
  7084. case '"':
  7085. // Workaround for ETags: we have to include the quotes as
  7086. // part of the tag.
  7087. if (strtolower($this->name) === 'etag')
  7088. {
  7089. $this->value .= '"';
  7090. $this->position++;
  7091. $this->state = 'value_char';
  7092. break;
  7093. }
  7094. $this->position++;
  7095. $this->state = 'quote';
  7096. break;
  7097. case "\x0A":
  7098. $this->position++;
  7099. $this->state = 'new_line';
  7100. break;
  7101. default:
  7102. $this->state = 'value_char';
  7103. break;
  7104. }
  7105. }
  7106. }
  7107. /**
  7108. * Parse a header value while outside quotes
  7109. */
  7110. protected function value_char()
  7111. {
  7112. $len = strcspn($this->data, "\x09\x20\x0A\"", $this->position);
  7113. $this->value .= substr($this->data, $this->position, $len);
  7114. $this->position += $len;
  7115. $this->state = 'value';
  7116. }
  7117. /**
  7118. * See what state to move to while within quoted header values
  7119. */
  7120. protected function quote()
  7121. {
  7122. if ($this->is_linear_whitespace())
  7123. {
  7124. $this->linear_whitespace();
  7125. }
  7126. else
  7127. {
  7128. switch ($this->data[$this->position])
  7129. {
  7130. case '"':
  7131. $this->position++;
  7132. $this->state = 'value';
  7133. break;
  7134. case "\x0A":
  7135. $this->position++;
  7136. $this->state = 'new_line';
  7137. break;
  7138. case '\\':
  7139. $this->position++;
  7140. $this->state = 'quote_escaped';
  7141. break;
  7142. default:
  7143. $this->state = 'quote_char';
  7144. break;
  7145. }
  7146. }
  7147. }
  7148. /**
  7149. * Parse a header value while within quotes
  7150. */
  7151. protected function quote_char()
  7152. {
  7153. $len = strcspn($this->data, "\x09\x20\x0A\"\\", $this->position);
  7154. $this->value .= substr($this->data, $this->position, $len);
  7155. $this->position += $len;
  7156. $this->state = 'value';
  7157. }
  7158. /**
  7159. * Parse an escaped character within quotes
  7160. */
  7161. protected function quote_escaped()
  7162. {
  7163. $this->value .= $this->data[$this->position];
  7164. $this->position++;
  7165. $this->state = 'quote';
  7166. }
  7167. /**
  7168. * Parse the body
  7169. */
  7170. protected function body()
  7171. {
  7172. $this->body = substr($this->data, $this->position);
  7173. if (!empty($this->headers['transfer-encoding']))
  7174. {
  7175. unset($this->headers['transfer-encoding']);
  7176. $this->state = 'chunked';
  7177. }
  7178. else
  7179. {
  7180. $this->state = 'emit';
  7181. }
  7182. }
  7183. /**
  7184. * Parsed a "Transfer-Encoding: chunked" body
  7185. */
  7186. protected function chunked()
  7187. {
  7188. if (!preg_match('/^([0-9a-f]+)[^\r\n]*\r\n/i', trim($this->body)))
  7189. {
  7190. $this->state = 'emit';
  7191. return;
  7192. }
  7193. $decoded = '';
  7194. $encoded = $this->body;
  7195. while (true)
  7196. {
  7197. $is_chunked = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $encoded, $matches );
  7198. if (!$is_chunked)
  7199. {
  7200. // Looks like it's not chunked after all
  7201. $this->state = 'emit';
  7202. return;
  7203. }
  7204. $length = hexdec(trim($matches[1]));
  7205. if ($length === 0)
  7206. {
  7207. // Ignore trailer headers
  7208. $this->state = 'emit';
  7209. $this->body = $decoded;
  7210. return;
  7211. }
  7212. $chunk_length = strlen($matches[0]);
  7213. $decoded .= $part = substr($encoded, $chunk_length, $length);
  7214. $encoded = substr($encoded, $chunk_length + $length + 2);
  7215. if (trim($encoded) === '0' || empty($encoded))
  7216. {
  7217. $this->state = 'emit';
  7218. $this->body = $decoded;
  7219. return;
  7220. }
  7221. }
  7222. }
  7223. }
  7224. /**
  7225. * IRI parser/serialiser/normaliser
  7226. *
  7227. * @package SimplePie
  7228. * @subpackage HTTP
  7229. * @author Geoffrey Sneddon
  7230. * @author Steve Minutillo
  7231. * @author Ryan McCue
  7232. * @copyright 2007-2012 Geoffrey Sneddon, Steve Minutillo, Ryan McCue
  7233. * @license http://www.opensource.org/licenses/bsd-license.php
  7234. */
  7235. class SimplePie_IRI
  7236. {
  7237. /**
  7238. * Scheme
  7239. *
  7240. * @var string
  7241. */
  7242. protected $scheme = null;
  7243. /**
  7244. * User Information
  7245. *
  7246. * @var string
  7247. */
  7248. protected $iuserinfo = null;
  7249. /**
  7250. * ihost
  7251. *
  7252. * @var string
  7253. */
  7254. protected $ihost = null;
  7255. /**
  7256. * Port
  7257. *
  7258. * @var string
  7259. */
  7260. protected $port = null;
  7261. /**
  7262. * ipath
  7263. *
  7264. * @var string
  7265. */
  7266. protected $ipath = '';
  7267. /**
  7268. * iquery
  7269. *
  7270. * @var string
  7271. */
  7272. protected $iquery = null;
  7273. /**
  7274. * ifragment
  7275. *
  7276. * @var string
  7277. */
  7278. protected $ifragment = null;
  7279. /**
  7280. * Normalization database
  7281. *
  7282. * Each key is the scheme, each value is an array with each key as the IRI
  7283. * part and value as the default value for that part.
  7284. */
  7285. protected $normalization = array(
  7286. 'acap' => array(
  7287. 'port' => 674
  7288. ),
  7289. 'dict' => array(
  7290. 'port' => 2628
  7291. ),
  7292. 'file' => array(
  7293. 'ihost' => 'localhost'
  7294. ),
  7295. 'http' => array(
  7296. 'port' => 80,
  7297. 'ipath' => '/'
  7298. ),
  7299. 'https' => array(
  7300. 'port' => 443,
  7301. 'ipath' => '/'
  7302. ),
  7303. );
  7304. /**
  7305. * Return the entire IRI when you try and read the object as a string
  7306. *
  7307. * @return string
  7308. */
  7309. public function __toString()
  7310. {
  7311. return $this->get_iri();
  7312. }
  7313. /**
  7314. * Overload __set() to provide access via properties
  7315. *
  7316. * @param string $name Property name
  7317. * @param mixed $value Property value
  7318. */
  7319. public function __set($name, $value)
  7320. {
  7321. if (method_exists($this, 'set_' . $name))
  7322. {
  7323. call_user_func(array($this, 'set_' . $name), $value);
  7324. }
  7325. elseif (
  7326. $name === 'iauthority'
  7327. || $name === 'iuserinfo'
  7328. || $name === 'ihost'
  7329. || $name === 'ipath'
  7330. || $name === 'iquery'
  7331. || $name === 'ifragment'
  7332. )
  7333. {
  7334. call_user_func(array($this, 'set_' . substr($name, 1)), $value);
  7335. }
  7336. }
  7337. /**
  7338. * Overload __get() to provide access via properties
  7339. *
  7340. * @param string $name Property name
  7341. * @return mixed
  7342. */
  7343. public function __get($name)
  7344. {
  7345. // isset() returns false for null, we don't want to do that
  7346. // Also why we use array_key_exists below instead of isset()
  7347. $props = get_object_vars($this);
  7348. if (
  7349. $name === 'iri' ||
  7350. $name === 'uri' ||
  7351. $name === 'iauthority' ||
  7352. $name === 'authority'
  7353. )
  7354. {
  7355. $return = $this->{"get_$name"}();
  7356. }
  7357. elseif (array_key_exists($name, $props))
  7358. {
  7359. $return = $this->$name;
  7360. }
  7361. // host -> ihost
  7362. elseif (($prop = 'i' . $name) && array_key_exists($prop, $props))
  7363. {
  7364. $name = $prop;
  7365. $return = $this->$prop;
  7366. }
  7367. // ischeme -> scheme
  7368. elseif (($prop = substr($name, 1)) && array_key_exists($prop, $props))
  7369. {
  7370. $name = $prop;
  7371. $return = $this->$prop;
  7372. }
  7373. else
  7374. {
  7375. trigger_error('Undefined property: ' . get_class($this) . '::' . $name, E_USER_NOTICE);
  7376. $return = null;
  7377. }
  7378. if ($return === null && isset($this->normalization[$this->scheme][$name]))
  7379. {
  7380. return $this->normalization[$this->scheme][$name];
  7381. }
  7382. else
  7383. {
  7384. return $return;
  7385. }
  7386. }
  7387. /**
  7388. * Overload __isset() to provide access via properties
  7389. *
  7390. * @param string $name Property name
  7391. * @return bool
  7392. */
  7393. public function __isset($name)
  7394. {
  7395. if (method_exists($this, 'get_' . $name) || isset($this->$name))
  7396. {
  7397. return true;
  7398. }
  7399. else
  7400. {
  7401. return false;
  7402. }
  7403. }
  7404. /**
  7405. * Overload __unset() to provide access via properties
  7406. *
  7407. * @param string $name Property name
  7408. */
  7409. public function __unset($name)
  7410. {
  7411. if (method_exists($this, 'set_' . $name))
  7412. {
  7413. call_user_func(array($this, 'set_' . $name), '');
  7414. }
  7415. }
  7416. /**
  7417. * Create a new IRI object, from a specified string
  7418. *
  7419. * @param string $iri
  7420. */
  7421. public function __construct($iri = null)
  7422. {
  7423. $this->set_iri($iri);
  7424. }
  7425. /**
  7426. * Create a new IRI object by resolving a relative IRI
  7427. *
  7428. * Returns false if $base is not absolute, otherwise an IRI.
  7429. *
  7430. * @param IRI|string $base (Absolute) Base IRI
  7431. * @param IRI|string $relative Relative IRI
  7432. * @return IRI|false
  7433. */
  7434. public static function absolutize($base, $relative)
  7435. {
  7436. if (!($relative instanceof SimplePie_IRI))
  7437. {
  7438. $relative = new SimplePie_IRI($relative);
  7439. }
  7440. if (!$relative->is_valid())
  7441. {
  7442. return false;
  7443. }
  7444. elseif ($relative->scheme !== null)
  7445. {
  7446. return clone $relative;
  7447. }
  7448. else
  7449. {
  7450. if (!($base instanceof SimplePie_IRI))
  7451. {
  7452. $base = new SimplePie_IRI($base);
  7453. }
  7454. if ($base->scheme !== null && $base->is_valid())
  7455. {
  7456. if ($relative->get_iri() !== '')
  7457. {
  7458. if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null)
  7459. {
  7460. $target = clone $relative;
  7461. $target->scheme = $base->scheme;
  7462. }
  7463. else
  7464. {
  7465. $target = new SimplePie_IRI;
  7466. $target->scheme = $base->scheme;
  7467. $target->iuserinfo = $base->iuserinfo;
  7468. $target->ihost = $base->ihost;
  7469. $target->port = $base->port;
  7470. if ($relative->ipath !== '')
  7471. {
  7472. if ($relative->ipath[0] === '/')
  7473. {
  7474. $target->ipath = $relative->ipath;
  7475. }
  7476. elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '')
  7477. {
  7478. $target->ipath = '/' . $relative->ipath;
  7479. }
  7480. elseif (($last_segment = strrpos($base->ipath, '/')) !== false)
  7481. {
  7482. $target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath;
  7483. }
  7484. else
  7485. {
  7486. $target->ipath = $relative->ipath;
  7487. }
  7488. $target->ipath = $target->remove_dot_segments($target->ipath);
  7489. $target->iquery = $relative->iquery;
  7490. }
  7491. else
  7492. {
  7493. $target->ipath = $base->ipath;
  7494. if ($relative->iquery !== null)
  7495. {
  7496. $target->iquery = $relative->iquery;
  7497. }
  7498. elseif ($base->iquery !== null)
  7499. {
  7500. $target->iquery = $base->iquery;
  7501. }
  7502. }
  7503. $target->ifragment = $relative->ifragment;
  7504. }
  7505. }
  7506. else
  7507. {
  7508. $target = clone $base;
  7509. $target->ifragment = null;
  7510. }
  7511. $target->scheme_normalization();
  7512. return $target;
  7513. }
  7514. else
  7515. {
  7516. return false;
  7517. }
  7518. }
  7519. }
  7520. /**
  7521. * Parse an IRI into scheme/authority/path/query/fragment segments
  7522. *
  7523. * @param string $iri
  7524. * @return array
  7525. */
  7526. protected function parse_iri($iri)
  7527. {
  7528. $iri = trim($iri, "\x20\x09\x0A\x0C\x0D");
  7529. if (preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match))
  7530. {
  7531. if ($match[1] === '')
  7532. {
  7533. $match['scheme'] = null;
  7534. }
  7535. if (!isset($match[3]) || $match[3] === '')
  7536. {
  7537. $match['authority'] = null;
  7538. }
  7539. if (!isset($match[5]))
  7540. {
  7541. $match['path'] = '';
  7542. }
  7543. if (!isset($match[6]) || $match[6] === '')
  7544. {
  7545. $match['query'] = null;
  7546. }
  7547. if (!isset($match[8]) || $match[8] === '')
  7548. {
  7549. $match['fragment'] = null;
  7550. }
  7551. return $match;
  7552. }
  7553. else
  7554. {
  7555. // This can occur when a paragraph is accidentally parsed as a URI
  7556. return false;
  7557. }
  7558. }
  7559. /**
  7560. * Remove dot segments from a path
  7561. *
  7562. * @param string $input
  7563. * @return string
  7564. */
  7565. protected function remove_dot_segments($input)
  7566. {
  7567. $output = '';
  7568. while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..')
  7569. {
  7570. // A: If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise,
  7571. if (strpos($input, '../') === 0)
  7572. {
  7573. $input = substr($input, 3);
  7574. }
  7575. elseif (strpos($input, './') === 0)
  7576. {
  7577. $input = substr($input, 2);
  7578. }
  7579. // B: if the input buffer begins with a prefix of "/./" or "/.", where "." is a complete path segment, then replace that prefix with "/" in the input buffer; otherwise,
  7580. elseif (strpos($input, '/./') === 0)
  7581. {
  7582. $input = substr($input, 2);
  7583. }
  7584. elseif ($input === '/.')
  7585. {
  7586. $input = '/';
  7587. }
  7588. // C: if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; otherwise,
  7589. elseif (strpos($input, '/../') === 0)
  7590. {
  7591. $input = substr($input, 3);
  7592. $output = substr_replace($output, '', strrpos($output, '/'));
  7593. }
  7594. elseif ($input === '/..')
  7595. {
  7596. $input = '/';
  7597. $output = substr_replace($output, '', strrpos($output, '/'));
  7598. }
  7599. // D: if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise,
  7600. elseif ($input === '.' || $input === '..')
  7601. {
  7602. $input = '';
  7603. }
  7604. // E: move the first path segment in the input buffer to the end of the output buffer, including the initial "/" character (if any) and any subsequent characters up to, but not including, the next "/" character or the end of the input buffer
  7605. elseif (($pos = strpos($input, '/', 1)) !== false)
  7606. {
  7607. $output .= substr($input, 0, $pos);
  7608. $input = substr_replace($input, '', 0, $pos);
  7609. }
  7610. else
  7611. {
  7612. $output .= $input;
  7613. $input = '';
  7614. }
  7615. }
  7616. return $output . $input;
  7617. }
  7618. /**
  7619. * Replace invalid character with percent encoding
  7620. *
  7621. * @param string $string Input string
  7622. * @param string $extra_chars Valid characters not in iunreserved or
  7623. * iprivate (this is ASCII-only)
  7624. * @param bool $iprivate Allow iprivate
  7625. * @return string
  7626. */
  7627. protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false)
  7628. {
  7629. // Normalize as many pct-encoded sections as possible
  7630. $string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $string);
  7631. // Replace invalid percent characters
  7632. $string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);
  7633. // Add unreserved and % to $extra_chars (the latter is safe because all
  7634. // pct-encoded sections are now valid).
  7635. $extra_chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~%';
  7636. // Now replace any bytes that aren't allowed with their pct-encoded versions
  7637. $position = 0;
  7638. $strlen = strlen($string);
  7639. while (($position += strspn($string, $extra_chars, $position)) < $strlen)
  7640. {
  7641. $value = ord($string[$position]);
  7642. // Start position
  7643. $start = $position;
  7644. // By default we are valid
  7645. $valid = true;
  7646. // No one byte sequences are valid due to the while.
  7647. // Two byte sequence:
  7648. if (($value & 0xE0) === 0xC0)
  7649. {
  7650. $character = ($value & 0x1F) << 6;
  7651. $length = 2;
  7652. $remaining = 1;
  7653. }
  7654. // Three byte sequence:
  7655. elseif (($value & 0xF0) === 0xE0)
  7656. {
  7657. $character = ($value & 0x0F) << 12;
  7658. $length = 3;
  7659. $remaining = 2;
  7660. }
  7661. // Four byte sequence:
  7662. elseif (($value & 0xF8) === 0xF0)
  7663. {
  7664. $character = ($value & 0x07) << 18;
  7665. $length = 4;
  7666. $remaining = 3;
  7667. }
  7668. // Invalid byte:
  7669. else
  7670. {
  7671. $valid = false;
  7672. $length = 1;
  7673. $remaining = 0;
  7674. }
  7675. if ($remaining)
  7676. {
  7677. if ($position + $length <= $strlen)
  7678. {
  7679. for ($position++; $remaining; $position++)
  7680. {
  7681. $value = ord($string[$position]);
  7682. // Check that the byte is valid, then add it to the character:
  7683. if (($value & 0xC0) === 0x80)
  7684. {
  7685. $character |= ($value & 0x3F) << (--$remaining * 6);
  7686. }
  7687. // If it is invalid, count the sequence as invalid and reprocess the current byte:
  7688. else
  7689. {
  7690. $valid = false;
  7691. $position--;
  7692. break;
  7693. }
  7694. }
  7695. }
  7696. else
  7697. {
  7698. $position = $strlen - 1;
  7699. $valid = false;
  7700. }
  7701. }
  7702. // Percent encode anything invalid or not in ucschar
  7703. if (
  7704. // Invalid sequences
  7705. !$valid
  7706. // Non-shortest form sequences are invalid
  7707. || $length > 1 && $character <= 0x7F
  7708. || $length > 2 && $character <= 0x7FF
  7709. || $length > 3 && $character <= 0xFFFF
  7710. // Outside of range of ucschar codepoints
  7711. // Noncharacters
  7712. || ($character & 0xFFFE) === 0xFFFE
  7713. || $character >= 0xFDD0 && $character <= 0xFDEF
  7714. || (
  7715. // Everything else not in ucschar
  7716. $character > 0xD7FF && $character < 0xF900
  7717. || $character < 0xA0
  7718. || $character > 0xEFFFD
  7719. )
  7720. && (
  7721. // Everything not in iprivate, if it applies
  7722. !$iprivate
  7723. || $character < 0xE000
  7724. || $character > 0x10FFFD
  7725. )
  7726. )
  7727. {
  7728. // If we were a character, pretend we weren't, but rather an error.
  7729. if ($valid)
  7730. $position--;
  7731. for ($j = $start; $j <= $position; $j++)
  7732. {
  7733. $string = substr_replace($string, sprintf('%%%02X', ord($string[$j])), $j, 1);
  7734. $j += 2;
  7735. $position += 2;
  7736. $strlen += 2;
  7737. }
  7738. }
  7739. }
  7740. return $string;
  7741. }
  7742. /**
  7743. * Callback function for preg_replace_callback.
  7744. *
  7745. * Removes sequences of percent encoded bytes that represent UTF-8
  7746. * encoded characters in iunreserved
  7747. *
  7748. * @param array $match PCRE match
  7749. * @return string Replacement
  7750. */
  7751. protected function remove_iunreserved_percent_encoded($match)
  7752. {
  7753. // As we just have valid percent encoded sequences we can just explode
  7754. // and ignore the first member of the returned array (an empty string).
  7755. $bytes = explode('%', $match[0]);
  7756. // Initialize the new string (this is what will be returned) and that
  7757. // there are no bytes remaining in the current sequence (unsurprising
  7758. // at the first byte!).
  7759. $string = '';
  7760. $remaining = 0;
  7761. // Loop over each and every byte, and set $value to its value
  7762. for ($i = 1, $len = count($bytes); $i < $len; $i++)
  7763. {
  7764. $value = hexdec($bytes[$i]);
  7765. // If we're the first byte of sequence:
  7766. if (!$remaining)
  7767. {
  7768. // Start position
  7769. $start = $i;
  7770. // By default we are valid
  7771. $valid = true;
  7772. // One byte sequence:
  7773. if ($value <= 0x7F)
  7774. {
  7775. $character = $value;
  7776. $length = 1;
  7777. }
  7778. // Two byte sequence:
  7779. elseif (($value & 0xE0) === 0xC0)
  7780. {
  7781. $character = ($value & 0x1F) << 6;
  7782. $length = 2;
  7783. $remaining = 1;
  7784. }
  7785. // Three byte sequence:
  7786. elseif (($value & 0xF0) === 0xE0)
  7787. {
  7788. $character = ($value & 0x0F) << 12;
  7789. $length = 3;
  7790. $remaining = 2;
  7791. }
  7792. // Four byte sequence:
  7793. elseif (($value & 0xF8) === 0xF0)
  7794. {
  7795. $character = ($value & 0x07) << 18;
  7796. $length = 4;
  7797. $remaining = 3;
  7798. }
  7799. // Invalid byte:
  7800. else
  7801. {
  7802. $valid = false;
  7803. $remaining = 0;
  7804. }
  7805. }
  7806. // Continuation byte:
  7807. else
  7808. {
  7809. // Check that the byte is valid, then add it to the character:
  7810. if (($value & 0xC0) === 0x80)
  7811. {
  7812. $remaining--;
  7813. $character |= ($value & 0x3F) << ($remaining * 6);
  7814. }
  7815. // If it is invalid, count the sequence as invalid and reprocess the current byte as the start of a sequence:
  7816. else
  7817. {
  7818. $valid = false;
  7819. $remaining = 0;
  7820. $i--;
  7821. }
  7822. }
  7823. // If we've reached the end of the current byte sequence, append it to Unicode::$data
  7824. if (!$remaining)
  7825. {
  7826. // Percent encode anything invalid or not in iunreserved
  7827. if (
  7828. // Invalid sequences
  7829. !$valid
  7830. // Non-shortest form sequences are invalid
  7831. || $length > 1 && $character <= 0x7F
  7832. || $length > 2 && $character <= 0x7FF
  7833. || $length > 3 && $character <= 0xFFFF
  7834. // Outside of range of iunreserved codepoints
  7835. || $character < 0x2D
  7836. || $character > 0xEFFFD
  7837. // Noncharacters
  7838. || ($character & 0xFFFE) === 0xFFFE
  7839. || $character >= 0xFDD0 && $character <= 0xFDEF
  7840. // Everything else not in iunreserved (this is all BMP)
  7841. || $character === 0x2F
  7842. || $character > 0x39 && $character < 0x41
  7843. || $character > 0x5A && $character < 0x61
  7844. || $character > 0x7A && $character < 0x7E
  7845. || $character > 0x7E && $character < 0xA0
  7846. || $character > 0xD7FF && $character < 0xF900
  7847. )
  7848. {
  7849. for ($j = $start; $j <= $i; $j++)
  7850. {
  7851. $string .= '%' . strtoupper($bytes[$j]);
  7852. }
  7853. }
  7854. else
  7855. {
  7856. for ($j = $start; $j <= $i; $j++)
  7857. {
  7858. $string .= chr(hexdec($bytes[$j]));
  7859. }
  7860. }
  7861. }
  7862. }
  7863. // If we have any bytes left over they are invalid (i.e., we are
  7864. // mid-way through a multi-byte sequence)
  7865. if ($remaining)
  7866. {
  7867. for ($j = $start; $j < $len; $j++)
  7868. {
  7869. $string .= '%' . strtoupper($bytes[$j]);
  7870. }
  7871. }
  7872. return $string;
  7873. }
  7874. protected function scheme_normalization()
  7875. {
  7876. if (isset($this->normalization[$this->scheme]['iuserinfo']) && $this->iuserinfo === $this->normalization[$this->scheme]['iuserinfo'])
  7877. {
  7878. $this->iuserinfo = null;
  7879. }
  7880. if (isset($this->normalization[$this->scheme]['ihost']) && $this->ihost === $this->normalization[$this->scheme]['ihost'])
  7881. {
  7882. $this->ihost = null;
  7883. }
  7884. if (isset($this->normalization[$this->scheme]['port']) && $this->port === $this->normalization[$this->scheme]['port'])
  7885. {
  7886. $this->port = null;
  7887. }
  7888. if (isset($this->normalization[$this->scheme]['ipath']) && $this->ipath === $this->normalization[$this->scheme]['ipath'])
  7889. {
  7890. $this->ipath = '';
  7891. }
  7892. if (isset($this->normalization[$this->scheme]['iquery']) && $this->iquery === $this->normalization[$this->scheme]['iquery'])
  7893. {
  7894. $this->iquery = null;
  7895. }
  7896. if (isset($this->normalization[$this->scheme]['ifragment']) && $this->ifragment === $this->normalization[$this->scheme]['ifragment'])
  7897. {
  7898. $this->ifragment = null;
  7899. }
  7900. }
  7901. /**
  7902. * Check if the object represents a valid IRI. This needs to be done on each
  7903. * call as some things change depending on another part of the IRI.
  7904. *
  7905. * @return bool
  7906. */
  7907. public function is_valid()
  7908. {
  7909. $isauthority = $this->iuserinfo !== null || $this->ihost !== null || $this->port !== null;
  7910. if ($this->ipath !== '' &&
  7911. (
  7912. $isauthority && (
  7913. $this->ipath[0] !== '/' ||
  7914. substr($this->ipath, 0, 2) === '//'
  7915. ) ||
  7916. (
  7917. $this->scheme === null &&
  7918. !$isauthority &&
  7919. strpos($this->ipath, ':') !== false &&
  7920. (strpos($this->ipath, '/') === false ? true : strpos($this->ipath, ':') < strpos($this->ipath, '/'))
  7921. )
  7922. )
  7923. )
  7924. {
  7925. return false;
  7926. }
  7927. return true;
  7928. }
  7929. /**
  7930. * Set the entire IRI. Returns true on success, false on failure (if there
  7931. * are any invalid characters).
  7932. *
  7933. * @param string $iri
  7934. * @return bool
  7935. */
  7936. public function set_iri($iri)
  7937. {
  7938. static $cache;
  7939. if (!$cache)
  7940. {
  7941. $cache = array();
  7942. }
  7943. if ($iri === null)
  7944. {
  7945. return true;
  7946. }
  7947. elseif (isset($cache[$iri]))
  7948. {
  7949. list($this->scheme,
  7950. $this->iuserinfo,
  7951. $this->ihost,
  7952. $this->port,
  7953. $this->ipath,
  7954. $this->iquery,
  7955. $this->ifragment,
  7956. $return) = $cache[$iri];
  7957. return $return;
  7958. }
  7959. else
  7960. {
  7961. $parsed = $this->parse_iri((string) $iri);
  7962. if (!$parsed)
  7963. {
  7964. return false;
  7965. }
  7966. $return = $this->set_scheme($parsed['scheme'])
  7967. && $this->set_authority($parsed['authority'])
  7968. && $this->set_path($parsed['path'])
  7969. && $this->set_query($parsed['query'])
  7970. && $this->set_fragment($parsed['fragment']);
  7971. $cache[$iri] = array($this->scheme,
  7972. $this->iuserinfo,
  7973. $this->ihost,
  7974. $this->port,
  7975. $this->ipath,
  7976. $this->iquery,
  7977. $this->ifragment,
  7978. $return);
  7979. return $return;
  7980. }
  7981. }
  7982. /**
  7983. * Set the scheme. Returns true on success, false on failure (if there are
  7984. * any invalid characters).
  7985. *
  7986. * @param string $scheme
  7987. * @return bool
  7988. */
  7989. public function set_scheme($scheme)
  7990. {
  7991. if ($scheme === null)
  7992. {
  7993. $this->scheme = null;
  7994. }
  7995. elseif (!preg_match('/^[A-Za-z][0-9A-Za-z+\-.]*$/', $scheme))
  7996. {
  7997. $this->scheme = null;
  7998. return false;
  7999. }
  8000. else
  8001. {
  8002. $this->scheme = strtolower($scheme);
  8003. }
  8004. return true;
  8005. }
  8006. /**
  8007. * Set the authority. Returns true on success, false on failure (if there are
  8008. * any invalid characters).
  8009. *
  8010. * @param string $authority
  8011. * @return bool
  8012. */
  8013. public function set_authority($authority)
  8014. {
  8015. static $cache;
  8016. if (!$cache)
  8017. $cache = array();
  8018. if ($authority === null)
  8019. {
  8020. $this->iuserinfo = null;
  8021. $this->ihost = null;
  8022. $this->port = null;
  8023. return true;
  8024. }
  8025. elseif (isset($cache[$authority]))
  8026. {
  8027. list($this->iuserinfo,
  8028. $this->ihost,
  8029. $this->port,
  8030. $return) = $cache[$authority];
  8031. return $return;
  8032. }
  8033. else
  8034. {
  8035. $remaining = $authority;
  8036. if (($iuserinfo_end = strrpos($remaining, '@')) !== false)
  8037. {
  8038. $iuserinfo = substr($remaining, 0, $iuserinfo_end);
  8039. $remaining = substr($remaining, $iuserinfo_end + 1);
  8040. }
  8041. else
  8042. {
  8043. $iuserinfo = null;
  8044. }
  8045. if (($port_start = strpos($remaining, ':', strpos($remaining, ']'))) !== false)
  8046. {
  8047. if (($port = substr($remaining, $port_start + 1)) === false)
  8048. {
  8049. $port = null;
  8050. }
  8051. $remaining = substr($remaining, 0, $port_start);
  8052. }
  8053. else
  8054. {
  8055. $port = null;
  8056. }
  8057. $return = $this->set_userinfo($iuserinfo) &&
  8058. $this->set_host($remaining) &&
  8059. $this->set_port($port);
  8060. $cache[$authority] = array($this->iuserinfo,
  8061. $this->ihost,
  8062. $this->port,
  8063. $return);
  8064. return $return;
  8065. }
  8066. }
  8067. /**
  8068. * Set the iuserinfo.
  8069. *
  8070. * @param string $iuserinfo
  8071. * @return bool
  8072. */
  8073. public function set_userinfo($iuserinfo)
  8074. {
  8075. if ($iuserinfo === null)
  8076. {
  8077. $this->iuserinfo = null;
  8078. }
  8079. else
  8080. {
  8081. $this->iuserinfo = $this->replace_invalid_with_pct_encoding($iuserinfo, '!$&\'()*+,;=:');
  8082. $this->scheme_normalization();
  8083. }
  8084. return true;
  8085. }
  8086. /**
  8087. * Set the ihost. Returns true on success, false on failure (if there are
  8088. * any invalid characters).
  8089. *
  8090. * @param string $ihost
  8091. * @return bool
  8092. */
  8093. public function set_host($ihost)
  8094. {
  8095. if ($ihost === null)
  8096. {
  8097. $this->ihost = null;
  8098. return true;
  8099. }
  8100. elseif (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']')
  8101. {
  8102. if (SimplePie_Net_IPv6::check_ipv6(substr($ihost, 1, -1)))
  8103. {
  8104. $this->ihost = '[' . SimplePie_Net_IPv6::compress(substr($ihost, 1, -1)) . ']';
  8105. }
  8106. else
  8107. {
  8108. $this->ihost = null;
  8109. return false;
  8110. }
  8111. }
  8112. else
  8113. {
  8114. $ihost = $this->replace_invalid_with_pct_encoding($ihost, '!$&\'()*+,;=');
  8115. // Lowercase, but ignore pct-encoded sections (as they should
  8116. // remain uppercase). This must be done after the previous step
  8117. // as that can add unescaped characters.
  8118. $position = 0;
  8119. $strlen = strlen($ihost);
  8120. while (($position += strcspn($ihost, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ%', $position)) < $strlen)
  8121. {
  8122. if ($ihost[$position] === '%')
  8123. {
  8124. $position += 3;
  8125. }
  8126. else
  8127. {
  8128. $ihost[$position] = strtolower($ihost[$position]);
  8129. $position++;
  8130. }
  8131. }
  8132. $this->ihost = $ihost;
  8133. }
  8134. $this->scheme_normalization();
  8135. return true;
  8136. }
  8137. /**
  8138. * Set the port. Returns true on success, false on failure (if there are
  8139. * any invalid characters).
  8140. *
  8141. * @param string $port
  8142. * @return bool
  8143. */
  8144. public function set_port($port)
  8145. {
  8146. if ($port === null)
  8147. {
  8148. $this->port = null;
  8149. return true;
  8150. }
  8151. elseif (strspn($port, '0123456789') === strlen($port))
  8152. {
  8153. $this->port = (int) $port;
  8154. $this->scheme_normalization();
  8155. return true;
  8156. }
  8157. else
  8158. {
  8159. $this->port = null;
  8160. return false;
  8161. }
  8162. }
  8163. /**
  8164. * Set the ipath.
  8165. *
  8166. * @param string $ipath
  8167. * @return bool
  8168. */
  8169. public function set_path($ipath)
  8170. {
  8171. static $cache;
  8172. if (!$cache)
  8173. {
  8174. $cache = array();
  8175. }
  8176. $ipath = (string) $ipath;
  8177. if (isset($cache[$ipath]))
  8178. {
  8179. $this->ipath = $cache[$ipath][(int) ($this->scheme !== null)];
  8180. }
  8181. else
  8182. {
  8183. $valid = $this->replace_invalid_with_pct_encoding($ipath, '!$&\'()*+,;=@:/');
  8184. $removed = $this->remove_dot_segments($valid);
  8185. $cache[$ipath] = array($valid, $removed);
  8186. $this->ipath = ($this->scheme !== null) ? $removed : $valid;
  8187. }
  8188. $this->scheme_normalization();
  8189. return true;
  8190. }
  8191. /**
  8192. * Set the iquery.
  8193. *
  8194. * @param string $iquery
  8195. * @return bool
  8196. */
  8197. public function set_query($iquery)
  8198. {
  8199. if ($iquery === null)
  8200. {
  8201. $this->iquery = null;
  8202. }
  8203. else
  8204. {
  8205. $this->iquery = $this->replace_invalid_with_pct_encoding($iquery, '!$&\'()*+,;=:@/?', true);
  8206. $this->scheme_normalization();
  8207. }
  8208. return true;
  8209. }
  8210. /**
  8211. * Set the ifragment.
  8212. *
  8213. * @param string $ifragment
  8214. * @return bool
  8215. */
  8216. public function set_fragment($ifragment)
  8217. {
  8218. if ($ifragment === null)
  8219. {
  8220. $this->ifragment = null;
  8221. }
  8222. else
  8223. {
  8224. $this->ifragment = $this->replace_invalid_with_pct_encoding($ifragment, '!$&\'()*+,;=:@/?');
  8225. $this->scheme_normalization();
  8226. }
  8227. return true;
  8228. }
  8229. /**
  8230. * Convert an IRI to a URI (or parts thereof)
  8231. *
  8232. * @return string
  8233. */
  8234. public function to_uri($string)
  8235. {
  8236. static $non_ascii;
  8237. if (!$non_ascii)
  8238. {
  8239. $non_ascii = implode('', range("\x80", "\xFF"));
  8240. }
  8241. $position = 0;
  8242. $strlen = strlen($string);
  8243. while (($position += strcspn($string, $non_ascii, $position)) < $strlen)
  8244. {
  8245. $string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1);
  8246. $position += 3;
  8247. $strlen += 2;
  8248. }
  8249. return $string;
  8250. }
  8251. /**
  8252. * Get the complete IRI
  8253. *
  8254. * @return string
  8255. */
  8256. public function get_iri()
  8257. {
  8258. if (!$this->is_valid())
  8259. {
  8260. return false;
  8261. }
  8262. $iri = '';
  8263. if ($this->scheme !== null)
  8264. {
  8265. $iri .= $this->scheme . ':';
  8266. }
  8267. if (($iauthority = $this->get_iauthority()) !== null)
  8268. {
  8269. $iri .= '//' . $iauthority;
  8270. }
  8271. if ($this->ipath !== '')
  8272. {
  8273. $iri .= $this->ipath;
  8274. }
  8275. elseif (!empty($this->normalization[$this->scheme]['ipath']) && $iauthority !== null && $iauthority !== '')
  8276. {
  8277. $iri .= $this->normalization[$this->scheme]['ipath'];
  8278. }
  8279. if ($this->iquery !== null)
  8280. {
  8281. $iri .= '?' . $this->iquery;
  8282. }
  8283. if ($this->ifragment !== null)
  8284. {
  8285. $iri .= '#' . $this->ifragment;
  8286. }
  8287. return $iri;
  8288. }
  8289. /**
  8290. * Get the complete URI
  8291. *
  8292. * @return string
  8293. */
  8294. public function get_uri()
  8295. {
  8296. return $this->to_uri($this->get_iri());
  8297. }
  8298. /**
  8299. * Get the complete iauthority
  8300. *
  8301. * @return string
  8302. */
  8303. protected function get_iauthority()
  8304. {
  8305. if ($this->iuserinfo !== null || $this->ihost !== null || $this->port !== null)
  8306. {
  8307. $iauthority = '';
  8308. if ($this->iuserinfo !== null)
  8309. {
  8310. $iauthority .= $this->iuserinfo . '@';
  8311. }
  8312. if ($this->ihost !== null)
  8313. {
  8314. $iauthority .= $this->ihost;
  8315. }
  8316. if ($this->port !== null)
  8317. {
  8318. $iauthority .= ':' . $this->port;
  8319. }
  8320. return $iauthority;
  8321. }
  8322. else
  8323. {
  8324. return null;
  8325. }
  8326. }
  8327. /**
  8328. * Get the complete authority
  8329. *
  8330. * @return string
  8331. */
  8332. protected function get_authority()
  8333. {
  8334. $iauthority = $this->get_iauthority();
  8335. if (is_string($iauthority))
  8336. return $this->to_uri($iauthority);
  8337. else
  8338. return $iauthority;
  8339. }
  8340. }
  8341. /**
  8342. * Manages all item-related data
  8343. *
  8344. * Used by {@see SimplePie::get_item()} and {@see SimplePie::get_items()}
  8345. *
  8346. * This class can be overloaded with {@see SimplePie::set_item_class()}
  8347. *
  8348. * @package SimplePie
  8349. * @subpackage API
  8350. */
  8351. class SimplePie_Item
  8352. {
  8353. /**
  8354. * Parent feed
  8355. *
  8356. * @access private
  8357. * @var SimplePie
  8358. */
  8359. var $feed;
  8360. /**
  8361. * Raw data
  8362. *
  8363. * @access private
  8364. * @var array
  8365. */
  8366. var $data = array();
  8367. /**
  8368. * Registry object
  8369. *
  8370. * @see set_registry
  8371. * @var SimplePie_Registry
  8372. */
  8373. protected $registry;
  8374. /**
  8375. * Create a new item object
  8376. *
  8377. * This is usually used by {@see SimplePie::get_items} and
  8378. * {@see SimplePie::get_item}. Avoid creating this manually.
  8379. *
  8380. * @param SimplePie $feed Parent feed
  8381. * @param array $data Raw data
  8382. */
  8383. public function __construct($feed, $data)
  8384. {
  8385. $this->feed = $feed;
  8386. $this->data = $data;
  8387. }
  8388. /**
  8389. * Set the registry handler
  8390. *
  8391. * This is usually used by {@see SimplePie_Registry::create}
  8392. *
  8393. * @since 1.3
  8394. * @param SimplePie_Registry $registry
  8395. */
  8396. public function set_registry(SimplePie_Registry $registry)
  8397. {
  8398. $this->registry = $registry;
  8399. }
  8400. /**
  8401. * Get a string representation of the item
  8402. *
  8403. * @return string
  8404. */
  8405. public function __toString()
  8406. {
  8407. return md5(serialize($this->data));
  8408. }
  8409. /**
  8410. * Remove items that link back to this before destroying this object
  8411. */
  8412. public function __destruct()
  8413. {
  8414. if ((version_compare(PHP_VERSION, '5.3', '<') || !gc_enabled()) && !ini_get('zend.ze1_compatibility_mode'))
  8415. {
  8416. unset($this->feed);
  8417. }
  8418. }
  8419. /**
  8420. * Get data for an item-level element
  8421. *
  8422. * This method allows you to get access to ANY element/attribute that is a
  8423. * sub-element of the item/entry tag.
  8424. *
  8425. * See {@see SimplePie::get_feed_tags()} for a description of the return value
  8426. *
  8427. * @since 1.0
  8428. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  8429. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  8430. * @param string $tag Tag name
  8431. * @return array
  8432. */
  8433. public function get_item_tags($namespace, $tag)
  8434. {
  8435. if (isset($this->data['child'][$namespace][$tag]))
  8436. {
  8437. return $this->data['child'][$namespace][$tag];
  8438. }
  8439. else
  8440. {
  8441. return null;
  8442. }
  8443. }
  8444. /**
  8445. * Get the base URL value from the parent feed
  8446. *
  8447. * Uses `<xml:base>`
  8448. *
  8449. * @param array $element
  8450. * @return string
  8451. */
  8452. public function get_base($element = array())
  8453. {
  8454. return $this->feed->get_base($element);
  8455. }
  8456. /**
  8457. * Sanitize feed data
  8458. *
  8459. * @access private
  8460. * @see SimplePie::sanitize()
  8461. * @param string $data Data to sanitize
  8462. * @param int $type One of the SIMPLEPIE_CONSTRUCT_* constants
  8463. * @param string $base Base URL to resolve URLs against
  8464. * @return string Sanitized data
  8465. */
  8466. public function sanitize($data, $type, $base = '')
  8467. {
  8468. return $this->feed->sanitize($data, $type, $base);
  8469. }
  8470. /**
  8471. * Get the parent feed
  8472. *
  8473. * Note: this may not work as you think for multifeeds!
  8474. *
  8475. * @link http://simplepie.org/faq/typical_multifeed_gotchas#missing_data_from_feed
  8476. * @since 1.0
  8477. * @return SimplePie
  8478. */
  8479. public function get_feed()
  8480. {
  8481. return $this->feed;
  8482. }
  8483. /**
  8484. * Get the unique identifier for the item
  8485. *
  8486. * This is usually used when writing code to check for new items in a feed.
  8487. *
  8488. * Uses `<atom:id>`, `<guid>`, `<dc:identifier>` or the `about` attribute
  8489. * for RDF. If none of these are supplied (or `$hash` is true), creates an
  8490. * MD5 hash based on the permalink and title. If either of those are not
  8491. * supplied, creates a hash based on the full feed data.
  8492. *
  8493. * @since Beta 2
  8494. * @param boolean $hash Should we force using a hash instead of the supplied ID?
  8495. * @return string
  8496. */
  8497. public function get_id($hash = false)
  8498. {
  8499. if (!$hash)
  8500. {
  8501. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'id'))
  8502. {
  8503. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8504. }
  8505. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'id'))
  8506. {
  8507. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8508. }
  8509. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'guid'))
  8510. {
  8511. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8512. }
  8513. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'identifier'))
  8514. {
  8515. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8516. }
  8517. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'identifier'))
  8518. {
  8519. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8520. }
  8521. elseif (isset($this->data['attribs'][SIMPLEPIE_NAMESPACE_RDF]['about']))
  8522. {
  8523. return $this->sanitize($this->data['attribs'][SIMPLEPIE_NAMESPACE_RDF]['about'], SIMPLEPIE_CONSTRUCT_TEXT);
  8524. }
  8525. elseif (($return = $this->get_permalink()) !== null)
  8526. {
  8527. return $return;
  8528. }
  8529. elseif (($return = $this->get_title()) !== null)
  8530. {
  8531. return $return;
  8532. }
  8533. }
  8534. if ($this->get_permalink() !== null || $this->get_title() !== null)
  8535. {
  8536. return md5($this->get_permalink() . $this->get_title());
  8537. }
  8538. else
  8539. {
  8540. return md5(serialize($this->data));
  8541. }
  8542. }
  8543. /**
  8544. * Get the title of the item
  8545. *
  8546. * Uses `<atom:title>`, `<title>` or `<dc:title>`
  8547. *
  8548. * @since Beta 2 (previously called `get_item_title` since 0.8)
  8549. * @return string|null
  8550. */
  8551. public function get_title()
  8552. {
  8553. if (!isset($this->data['title']))
  8554. {
  8555. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'title'))
  8556. {
  8557. $this->data['title'] = $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  8558. }
  8559. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'title'))
  8560. {
  8561. $this->data['title'] = $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  8562. }
  8563. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  8564. {
  8565. $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  8566. }
  8567. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  8568. {
  8569. $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  8570. }
  8571. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  8572. {
  8573. $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  8574. }
  8575. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  8576. {
  8577. $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8578. }
  8579. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  8580. {
  8581. $this->data['title'] = $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8582. }
  8583. else
  8584. {
  8585. $this->data['title'] = null;
  8586. }
  8587. }
  8588. return $this->data['title'];
  8589. }
  8590. /**
  8591. * Get the content for the item
  8592. *
  8593. * Prefers summaries over full content , but will return full content if a
  8594. * summary does not exist.
  8595. *
  8596. * To prefer full content instead, use {@see get_content}
  8597. *
  8598. * Uses `<atom:summary>`, `<description>`, `<dc:description>` or
  8599. * `<itunes:subtitle>`
  8600. *
  8601. * @since 0.8
  8602. * @param boolean $description_only Should we avoid falling back to the content?
  8603. * @return string|null
  8604. */
  8605. public function get_description($description_only = false)
  8606. {
  8607. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'summary'))
  8608. {
  8609. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  8610. }
  8611. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'summary'))
  8612. {
  8613. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  8614. }
  8615. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'description'))
  8616. {
  8617. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  8618. }
  8619. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'description'))
  8620. {
  8621. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  8622. }
  8623. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'description'))
  8624. {
  8625. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8626. }
  8627. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'description'))
  8628. {
  8629. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8630. }
  8631. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'summary'))
  8632. {
  8633. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  8634. }
  8635. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'subtitle'))
  8636. {
  8637. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8638. }
  8639. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'description'))
  8640. {
  8641. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML);
  8642. }
  8643. elseif (!$description_only)
  8644. {
  8645. return $this->get_content(true);
  8646. }
  8647. else
  8648. {
  8649. return null;
  8650. }
  8651. }
  8652. /**
  8653. * Get the content for the item
  8654. *
  8655. * Prefers full content over summaries, but will return a summary if full
  8656. * content does not exist.
  8657. *
  8658. * To prefer summaries instead, use {@see get_description}
  8659. *
  8660. * Uses `<atom:content>` or `<content:encoded>` (RSS 1.0 Content Module)
  8661. *
  8662. * @since 1.0
  8663. * @param boolean $content_only Should we avoid falling back to the description?
  8664. * @return string|null
  8665. */
  8666. public function get_content($content_only = false)
  8667. {
  8668. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'content'))
  8669. {
  8670. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_content_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  8671. }
  8672. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'content'))
  8673. {
  8674. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  8675. }
  8676. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_10_MODULES_CONTENT, 'encoded'))
  8677. {
  8678. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  8679. }
  8680. elseif (!$content_only)
  8681. {
  8682. return $this->get_description(true);
  8683. }
  8684. else
  8685. {
  8686. return null;
  8687. }
  8688. }
  8689. /**
  8690. * Get a category for the item
  8691. *
  8692. * @since Beta 3 (previously called `get_categories()` since Beta 2)
  8693. * @param int $key The category that you want to return. Remember that arrays begin with 0, not 1
  8694. * @return SimplePie_Category|null
  8695. */
  8696. public function get_category($key = 0)
  8697. {
  8698. $categories = $this->get_categories();
  8699. if (isset($categories[$key]))
  8700. {
  8701. return $categories[$key];
  8702. }
  8703. else
  8704. {
  8705. return null;
  8706. }
  8707. }
  8708. /**
  8709. * Get all categories for the item
  8710. *
  8711. * Uses `<atom:category>`, `<category>` or `<dc:subject>`
  8712. *
  8713. * @since Beta 3
  8714. * @return array|null List of {@see SimplePie_Category} objects
  8715. */
  8716. public function get_categories()
  8717. {
  8718. $categories = array();
  8719. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'category') as $category)
  8720. {
  8721. $term = null;
  8722. $scheme = null;
  8723. $label = null;
  8724. if (isset($category['attribs']['']['term']))
  8725. {
  8726. $term = $this->sanitize($category['attribs']['']['term'], SIMPLEPIE_CONSTRUCT_TEXT);
  8727. }
  8728. if (isset($category['attribs']['']['scheme']))
  8729. {
  8730. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  8731. }
  8732. if (isset($category['attribs']['']['label']))
  8733. {
  8734. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  8735. }
  8736. $categories[] = $this->registry->create('Category', array($term, $scheme, $label));
  8737. }
  8738. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'category') as $category)
  8739. {
  8740. // This is really the label, but keep this as the term also for BC.
  8741. // Label will also work on retrieving because that falls back to term.
  8742. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8743. if (isset($category['attribs']['']['domain']))
  8744. {
  8745. $scheme = $this->sanitize($category['attribs']['']['domain'], SIMPLEPIE_CONSTRUCT_TEXT);
  8746. }
  8747. else
  8748. {
  8749. $scheme = null;
  8750. }
  8751. $categories[] = $this->registry->create('Category', array($term, $scheme, null));
  8752. }
  8753. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'subject') as $category)
  8754. {
  8755. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  8756. }
  8757. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'subject') as $category)
  8758. {
  8759. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  8760. }
  8761. if (!empty($categories))
  8762. {
  8763. return array_unique($categories);
  8764. }
  8765. else
  8766. {
  8767. return null;
  8768. }
  8769. }
  8770. /**
  8771. * Get an author for the item
  8772. *
  8773. * @since Beta 2
  8774. * @param int $key The author that you want to return. Remember that arrays begin with 0, not 1
  8775. * @return SimplePie_Author|null
  8776. */
  8777. public function get_author($key = 0)
  8778. {
  8779. $authors = $this->get_authors();
  8780. if (isset($authors[$key]))
  8781. {
  8782. return $authors[$key];
  8783. }
  8784. else
  8785. {
  8786. return null;
  8787. }
  8788. }
  8789. /**
  8790. * Get a contributor for the item
  8791. *
  8792. * @since 1.1
  8793. * @param int $key The contrbutor that you want to return. Remember that arrays begin with 0, not 1
  8794. * @return SimplePie_Author|null
  8795. */
  8796. public function get_contributor($key = 0)
  8797. {
  8798. $contributors = $this->get_contributors();
  8799. if (isset($contributors[$key]))
  8800. {
  8801. return $contributors[$key];
  8802. }
  8803. else
  8804. {
  8805. return null;
  8806. }
  8807. }
  8808. /**
  8809. * Get all contributors for the item
  8810. *
  8811. * Uses `<atom:contributor>`
  8812. *
  8813. * @since 1.1
  8814. * @return array|null List of {@see SimplePie_Author} objects
  8815. */
  8816. public function get_contributors()
  8817. {
  8818. $contributors = array();
  8819. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'contributor') as $contributor)
  8820. {
  8821. $name = null;
  8822. $uri = null;
  8823. $email = null;
  8824. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  8825. {
  8826. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8827. }
  8828. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  8829. {
  8830. $uri = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  8831. }
  8832. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  8833. {
  8834. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8835. }
  8836. if ($name !== null || $email !== null || $uri !== null)
  8837. {
  8838. $contributors[] = $this->registry->create('Author', array($name, $uri, $email));
  8839. }
  8840. }
  8841. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'contributor') as $contributor)
  8842. {
  8843. $name = null;
  8844. $url = null;
  8845. $email = null;
  8846. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  8847. {
  8848. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8849. }
  8850. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  8851. {
  8852. $url = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  8853. }
  8854. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  8855. {
  8856. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8857. }
  8858. if ($name !== null || $email !== null || $url !== null)
  8859. {
  8860. $contributors[] = $this->registry->create('Author', array($name, $url, $email));
  8861. }
  8862. }
  8863. if (!empty($contributors))
  8864. {
  8865. return array_unique($contributors);
  8866. }
  8867. else
  8868. {
  8869. return null;
  8870. }
  8871. }
  8872. /**
  8873. * Get all authors for the item
  8874. *
  8875. * Uses `<atom:author>`, `<author>`, `<dc:creator>` or `<itunes:author>`
  8876. *
  8877. * @since Beta 2
  8878. * @return array|null List of {@see SimplePie_Author} objects
  8879. */
  8880. public function get_authors()
  8881. {
  8882. $authors = array();
  8883. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author') as $author)
  8884. {
  8885. $name = null;
  8886. $uri = null;
  8887. $email = null;
  8888. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  8889. {
  8890. $name = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8891. }
  8892. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  8893. {
  8894. $uri = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  8895. }
  8896. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  8897. {
  8898. $email = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8899. }
  8900. if ($name !== null || $email !== null || $uri !== null)
  8901. {
  8902. $authors[] = $this->registry->create('Author', array($name, $uri, $email));
  8903. }
  8904. }
  8905. if ($author = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'author'))
  8906. {
  8907. $name = null;
  8908. $url = null;
  8909. $email = null;
  8910. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  8911. {
  8912. $name = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8913. }
  8914. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  8915. {
  8916. $url = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  8917. }
  8918. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  8919. {
  8920. $email = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8921. }
  8922. if ($name !== null || $email !== null || $url !== null)
  8923. {
  8924. $authors[] = $this->registry->create('Author', array($name, $url, $email));
  8925. }
  8926. }
  8927. if ($author = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'author'))
  8928. {
  8929. $authors[] = $this->registry->create('Author', array(null, null, $this->sanitize($author[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT)));
  8930. }
  8931. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'creator') as $author)
  8932. {
  8933. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  8934. }
  8935. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'creator') as $author)
  8936. {
  8937. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  8938. }
  8939. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'author') as $author)
  8940. {
  8941. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  8942. }
  8943. if (!empty($authors))
  8944. {
  8945. return array_unique($authors);
  8946. }
  8947. elseif (($source = $this->get_source()) && ($authors = $source->get_authors()))
  8948. {
  8949. return $authors;
  8950. }
  8951. elseif ($authors = $this->feed->get_authors())
  8952. {
  8953. return $authors;
  8954. }
  8955. else
  8956. {
  8957. return null;
  8958. }
  8959. }
  8960. /**
  8961. * Get the copyright info for the item
  8962. *
  8963. * Uses `<atom:rights>` or `<dc:rights>`
  8964. *
  8965. * @since 1.1
  8966. * @return string
  8967. */
  8968. public function get_copyright()
  8969. {
  8970. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'rights'))
  8971. {
  8972. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  8973. }
  8974. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'rights'))
  8975. {
  8976. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8977. }
  8978. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'rights'))
  8979. {
  8980. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  8981. }
  8982. else
  8983. {
  8984. return null;
  8985. }
  8986. }
  8987. /**
  8988. * Get the posting date/time for the item
  8989. *
  8990. * Uses `<atom:published>`, `<atom:updated>`, `<atom:issued>`,
  8991. * `<atom:modified>`, `<pubDate>` or `<dc:date>`
  8992. *
  8993. * Note: obeys PHP's timezone setting. To get a UTC date/time, use
  8994. * {@see get_gmdate}
  8995. *
  8996. * @since Beta 2 (previously called `get_item_date` since 0.8)
  8997. *
  8998. * @param string $date_format Supports any PHP date format from {@see http://php.net/date} (empty for the raw data)
  8999. * @return int|string|null
  9000. */
  9001. public function get_date($date_format = 'j F Y, g:i a')
  9002. {
  9003. if (!isset($this->data['date']))
  9004. {
  9005. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'published'))
  9006. {
  9007. $this->data['date']['raw'] = $return[0]['data'];
  9008. }
  9009. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated'))
  9010. {
  9011. $this->data['date']['raw'] = $return[0]['data'];
  9012. }
  9013. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'issued'))
  9014. {
  9015. $this->data['date']['raw'] = $return[0]['data'];
  9016. }
  9017. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'created'))
  9018. {
  9019. $this->data['date']['raw'] = $return[0]['data'];
  9020. }
  9021. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'modified'))
  9022. {
  9023. $this->data['date']['raw'] = $return[0]['data'];
  9024. }
  9025. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'pubDate'))
  9026. {
  9027. $this->data['date']['raw'] = $return[0]['data'];
  9028. }
  9029. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_11, 'date'))
  9030. {
  9031. $this->data['date']['raw'] = $return[0]['data'];
  9032. }
  9033. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_DC_10, 'date'))
  9034. {
  9035. $this->data['date']['raw'] = $return[0]['data'];
  9036. }
  9037. if (!empty($this->data['date']['raw']))
  9038. {
  9039. $parser = $this->registry->call('Parse_Date', 'get');
  9040. $this->data['date']['parsed'] = $parser->parse($this->data['date']['raw']);
  9041. }
  9042. else
  9043. {
  9044. $this->data['date'] = null;
  9045. }
  9046. }
  9047. if ($this->data['date'])
  9048. {
  9049. $date_format = (string) $date_format;
  9050. switch ($date_format)
  9051. {
  9052. case '':
  9053. return $this->sanitize($this->data['date']['raw'], SIMPLEPIE_CONSTRUCT_TEXT);
  9054. case 'U':
  9055. return $this->data['date']['parsed'];
  9056. default:
  9057. return date($date_format, $this->data['date']['parsed']);
  9058. }
  9059. }
  9060. else
  9061. {
  9062. return null;
  9063. }
  9064. }
  9065. /**
  9066. * Get the update date/time for the item
  9067. *
  9068. * Uses `<atom:updated>`
  9069. *
  9070. * Note: obeys PHP's timezone setting. To get a UTC date/time, use
  9071. * {@see get_gmdate}
  9072. *
  9073. * @param string $date_format Supports any PHP date format from {@see http://php.net/date} (empty for the raw data)
  9074. * @return int|string|null
  9075. */
  9076. public function get_updated_date($date_format = 'j F Y, g:i a')
  9077. {
  9078. if (!isset($this->data['updated']))
  9079. {
  9080. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated'))
  9081. {
  9082. $this->data['updated']['raw'] = $return[0]['data'];
  9083. }
  9084. if (!empty($this->data['updated']['raw']))
  9085. {
  9086. $parser = $this->registry->call('Parse_Date', 'get');
  9087. $this->data['updated']['parsed'] = $parser->parse($this->data['date']['raw']);
  9088. }
  9089. else
  9090. {
  9091. $this->data['updated'] = null;
  9092. }
  9093. }
  9094. if ($this->data['updated'])
  9095. {
  9096. $date_format = (string) $date_format;
  9097. switch ($date_format)
  9098. {
  9099. case '':
  9100. return $this->sanitize($this->data['updated']['raw'], SIMPLEPIE_CONSTRUCT_TEXT);
  9101. case 'U':
  9102. return $this->data['updated']['parsed'];
  9103. default:
  9104. return date($date_format, $this->data['updated']['parsed']);
  9105. }
  9106. }
  9107. else
  9108. {
  9109. return null;
  9110. }
  9111. }
  9112. /**
  9113. * Get the localized posting date/time for the item
  9114. *
  9115. * Returns the date formatted in the localized language. To display in
  9116. * languages other than the server's default, you need to change the locale
  9117. * with {@link http://php.net/setlocale setlocale()}. The available
  9118. * localizations depend on which ones are installed on your web server.
  9119. *
  9120. * @since 1.0
  9121. *
  9122. * @param string $date_format Supports any PHP date format from {@see http://php.net/strftime} (empty for the raw data)
  9123. * @return int|string|null
  9124. */
  9125. public function get_local_date($date_format = '%c')
  9126. {
  9127. if (!$date_format)
  9128. {
  9129. return $this->sanitize($this->get_date(''), SIMPLEPIE_CONSTRUCT_TEXT);
  9130. }
  9131. elseif (($date = $this->get_date('U')) !== null && $date !== false)
  9132. {
  9133. return strftime($date_format, $date);
  9134. }
  9135. else
  9136. {
  9137. return null;
  9138. }
  9139. }
  9140. /**
  9141. * Get the posting date/time for the item (UTC time)
  9142. *
  9143. * @see get_date
  9144. * @param string $date_format Supports any PHP date format from {@see http://php.net/date}
  9145. * @return int|string|null
  9146. */
  9147. public function get_gmdate($date_format = 'j F Y, g:i a')
  9148. {
  9149. $date = $this->get_date('U');
  9150. if ($date === null)
  9151. {
  9152. return null;
  9153. }
  9154. return gmdate($date_format, $date);
  9155. }
  9156. /**
  9157. * Get the update date/time for the item (UTC time)
  9158. *
  9159. * @see get_updated_date
  9160. * @param string $date_format Supports any PHP date format from {@see http://php.net/date}
  9161. * @return int|string|null
  9162. */
  9163. public function get_updated_gmdate($date_format = 'j F Y, g:i a')
  9164. {
  9165. $date = $this->get_updated_date('U');
  9166. if ($date === null)
  9167. {
  9168. return null;
  9169. }
  9170. return gmdate($date_format, $date);
  9171. }
  9172. /**
  9173. * Get the permalink for the item
  9174. *
  9175. * Returns the first link available with a relationship of "alternate".
  9176. * Identical to {@see get_link()} with key 0
  9177. *
  9178. * @see get_link
  9179. * @since 0.8
  9180. * @return string|null Permalink URL
  9181. */
  9182. public function get_permalink()
  9183. {
  9184. $link = $this->get_link();
  9185. $enclosure = $this->get_enclosure(0);
  9186. if ($link !== null)
  9187. {
  9188. return $link;
  9189. }
  9190. elseif ($enclosure !== null)
  9191. {
  9192. return $enclosure->get_link();
  9193. }
  9194. else
  9195. {
  9196. return null;
  9197. }
  9198. }
  9199. /**
  9200. * Get a single link for the item
  9201. *
  9202. * @since Beta 3
  9203. * @param int $key The link that you want to return. Remember that arrays begin with 0, not 1
  9204. * @param string $rel The relationship of the link to return
  9205. * @return string|null Link URL
  9206. */
  9207. public function get_link($key = 0, $rel = 'alternate')
  9208. {
  9209. $links = $this->get_links($rel);
  9210. if ($links[$key] !== null)
  9211. {
  9212. return $links[$key];
  9213. }
  9214. else
  9215. {
  9216. return null;
  9217. }
  9218. }
  9219. /**
  9220. * Get all links for the item
  9221. *
  9222. * Uses `<atom:link>`, `<link>` or `<guid>`
  9223. *
  9224. * @since Beta 2
  9225. * @param string $rel The relationship of links to return
  9226. * @return array|null Links found for the item (strings)
  9227. */
  9228. public function get_links($rel = 'alternate')
  9229. {
  9230. if (!isset($this->data['links']))
  9231. {
  9232. $this->data['links'] = array();
  9233. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link') as $link)
  9234. {
  9235. if (isset($link['attribs']['']['href']))
  9236. {
  9237. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  9238. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  9239. }
  9240. }
  9241. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link') as $link)
  9242. {
  9243. if (isset($link['attribs']['']['href']))
  9244. {
  9245. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  9246. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  9247. }
  9248. }
  9249. if ($links = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link'))
  9250. {
  9251. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  9252. }
  9253. if ($links = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link'))
  9254. {
  9255. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  9256. }
  9257. if ($links = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link'))
  9258. {
  9259. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  9260. }
  9261. if ($links = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'guid'))
  9262. {
  9263. if (!isset($links[0]['attribs']['']['isPermaLink']) || strtolower(trim($links[0]['attribs']['']['isPermaLink'])) === 'true')
  9264. {
  9265. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  9266. }
  9267. }
  9268. $keys = array_keys($this->data['links']);
  9269. foreach ($keys as $key)
  9270. {
  9271. if ($this->registry->call('Misc', 'is_isegment_nz_nc', array($key)))
  9272. {
  9273. if (isset($this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]))
  9274. {
  9275. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]);
  9276. $this->data['links'][$key] =& $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key];
  9277. }
  9278. else
  9279. {
  9280. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] =& $this->data['links'][$key];
  9281. }
  9282. }
  9283. elseif (substr($key, 0, 41) === SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY)
  9284. {
  9285. $this->data['links'][substr($key, 41)] =& $this->data['links'][$key];
  9286. }
  9287. $this->data['links'][$key] = array_unique($this->data['links'][$key]);
  9288. }
  9289. }
  9290. if (isset($this->data['links'][$rel]))
  9291. {
  9292. return $this->data['links'][$rel];
  9293. }
  9294. else
  9295. {
  9296. return null;
  9297. }
  9298. }
  9299. /**
  9300. * Get an enclosure from the item
  9301. *
  9302. * Supports the <enclosure> RSS tag, as well as Media RSS and iTunes RSS.
  9303. *
  9304. * @since Beta 2
  9305. * @todo Add ability to prefer one type of content over another (in a media group).
  9306. * @param int $key The enclosure that you want to return. Remember that arrays begin with 0, not 1
  9307. * @return SimplePie_Enclosure|null
  9308. */
  9309. public function get_enclosure($key = 0, $prefer = null)
  9310. {
  9311. $enclosures = $this->get_enclosures();
  9312. if (isset($enclosures[$key]))
  9313. {
  9314. return $enclosures[$key];
  9315. }
  9316. else
  9317. {
  9318. return null;
  9319. }
  9320. }
  9321. /**
  9322. * Get all available enclosures (podcasts, etc.)
  9323. *
  9324. * Supports the <enclosure> RSS tag, as well as Media RSS and iTunes RSS.
  9325. *
  9326. * At this point, we're pretty much assuming that all enclosures for an item
  9327. * are the same content. Anything else is too complicated to
  9328. * properly support.
  9329. *
  9330. * @since Beta 2
  9331. * @todo Add support for end-user defined sorting of enclosures by type/handler (so we can prefer the faster-loading FLV over MP4).
  9332. * @todo If an element exists at a level, but it's value is empty, we should fall back to the value from the parent (if it exists).
  9333. * @return array|null List of SimplePie_Enclosure items
  9334. */
  9335. public function get_enclosures()
  9336. {
  9337. if (!isset($this->data['enclosures']))
  9338. {
  9339. $this->data['enclosures'] = array();
  9340. // Elements
  9341. $captions_parent = null;
  9342. $categories_parent = null;
  9343. $copyrights_parent = null;
  9344. $credits_parent = null;
  9345. $description_parent = null;
  9346. $duration_parent = null;
  9347. $hashes_parent = null;
  9348. $keywords_parent = null;
  9349. $player_parent = null;
  9350. $ratings_parent = null;
  9351. $restrictions_parent = null;
  9352. $thumbnails_parent = null;
  9353. $title_parent = null;
  9354. // Let's do the channel and item-level ones first, and just re-use them if we need to.
  9355. $parent = $this->get_feed();
  9356. // CAPTIONS
  9357. if ($captions = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'text'))
  9358. {
  9359. foreach ($captions as $caption)
  9360. {
  9361. $caption_type = null;
  9362. $caption_lang = null;
  9363. $caption_startTime = null;
  9364. $caption_endTime = null;
  9365. $caption_text = null;
  9366. if (isset($caption['attribs']['']['type']))
  9367. {
  9368. $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  9369. }
  9370. if (isset($caption['attribs']['']['lang']))
  9371. {
  9372. $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  9373. }
  9374. if (isset($caption['attribs']['']['start']))
  9375. {
  9376. $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT);
  9377. }
  9378. if (isset($caption['attribs']['']['end']))
  9379. {
  9380. $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT);
  9381. }
  9382. if (isset($caption['data']))
  9383. {
  9384. $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9385. }
  9386. $captions_parent[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text));
  9387. }
  9388. }
  9389. elseif ($captions = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'text'))
  9390. {
  9391. foreach ($captions as $caption)
  9392. {
  9393. $caption_type = null;
  9394. $caption_lang = null;
  9395. $caption_startTime = null;
  9396. $caption_endTime = null;
  9397. $caption_text = null;
  9398. if (isset($caption['attribs']['']['type']))
  9399. {
  9400. $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  9401. }
  9402. if (isset($caption['attribs']['']['lang']))
  9403. {
  9404. $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  9405. }
  9406. if (isset($caption['attribs']['']['start']))
  9407. {
  9408. $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT);
  9409. }
  9410. if (isset($caption['attribs']['']['end']))
  9411. {
  9412. $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT);
  9413. }
  9414. if (isset($caption['data']))
  9415. {
  9416. $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9417. }
  9418. $captions_parent[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text));
  9419. }
  9420. }
  9421. if (is_array($captions_parent))
  9422. {
  9423. $captions_parent = array_values(array_unique($captions_parent));
  9424. }
  9425. // CATEGORIES
  9426. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'category') as $category)
  9427. {
  9428. $term = null;
  9429. $scheme = null;
  9430. $label = null;
  9431. if (isset($category['data']))
  9432. {
  9433. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9434. }
  9435. if (isset($category['attribs']['']['scheme']))
  9436. {
  9437. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  9438. }
  9439. else
  9440. {
  9441. $scheme = 'http://search.yahoo.com/mrss/category_schema';
  9442. }
  9443. if (isset($category['attribs']['']['label']))
  9444. {
  9445. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  9446. }
  9447. $categories_parent[] = $this->registry->create('Category', array($term, $scheme, $label));
  9448. }
  9449. foreach ((array) $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'category') as $category)
  9450. {
  9451. $term = null;
  9452. $scheme = null;
  9453. $label = null;
  9454. if (isset($category['data']))
  9455. {
  9456. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9457. }
  9458. if (isset($category['attribs']['']['scheme']))
  9459. {
  9460. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  9461. }
  9462. else
  9463. {
  9464. $scheme = 'http://search.yahoo.com/mrss/category_schema';
  9465. }
  9466. if (isset($category['attribs']['']['label']))
  9467. {
  9468. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  9469. }
  9470. $categories_parent[] = $this->registry->create('Category', array($term, $scheme, $label));
  9471. }
  9472. foreach ((array) $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'category') as $category)
  9473. {
  9474. $term = null;
  9475. $scheme = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
  9476. $label = null;
  9477. if (isset($category['attribs']['']['text']))
  9478. {
  9479. $label = $this->sanitize($category['attribs']['']['text'], SIMPLEPIE_CONSTRUCT_TEXT);
  9480. }
  9481. $categories_parent[] = $this->registry->create('Category', array($term, $scheme, $label));
  9482. if (isset($category['child'][SIMPLEPIE_NAMESPACE_ITUNES]['category']))
  9483. {
  9484. foreach ((array) $category['child'][SIMPLEPIE_NAMESPACE_ITUNES]['category'] as $subcategory)
  9485. {
  9486. if (isset($subcategory['attribs']['']['text']))
  9487. {
  9488. $label = $this->sanitize($subcategory['attribs']['']['text'], SIMPLEPIE_CONSTRUCT_TEXT);
  9489. }
  9490. $categories_parent[] = $this->registry->create('Category', array($term, $scheme, $label));
  9491. }
  9492. }
  9493. }
  9494. if (is_array($categories_parent))
  9495. {
  9496. $categories_parent = array_values(array_unique($categories_parent));
  9497. }
  9498. // COPYRIGHT
  9499. if ($copyright = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'copyright'))
  9500. {
  9501. $copyright_url = null;
  9502. $copyright_label = null;
  9503. if (isset($copyright[0]['attribs']['']['url']))
  9504. {
  9505. $copyright_url = $this->sanitize($copyright[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT);
  9506. }
  9507. if (isset($copyright[0]['data']))
  9508. {
  9509. $copyright_label = $this->sanitize($copyright[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9510. }
  9511. $copyrights_parent = $this->registry->create('Copyright', array($copyright_url, $copyright_label));
  9512. }
  9513. elseif ($copyright = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'copyright'))
  9514. {
  9515. $copyright_url = null;
  9516. $copyright_label = null;
  9517. if (isset($copyright[0]['attribs']['']['url']))
  9518. {
  9519. $copyright_url = $this->sanitize($copyright[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT);
  9520. }
  9521. if (isset($copyright[0]['data']))
  9522. {
  9523. $copyright_label = $this->sanitize($copyright[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9524. }
  9525. $copyrights_parent = $this->registry->create('Copyright', array($copyright_url, $copyright_label));
  9526. }
  9527. // CREDITS
  9528. if ($credits = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'credit'))
  9529. {
  9530. foreach ($credits as $credit)
  9531. {
  9532. $credit_role = null;
  9533. $credit_scheme = null;
  9534. $credit_name = null;
  9535. if (isset($credit['attribs']['']['role']))
  9536. {
  9537. $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT);
  9538. }
  9539. if (isset($credit['attribs']['']['scheme']))
  9540. {
  9541. $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  9542. }
  9543. else
  9544. {
  9545. $credit_scheme = 'urn:ebu';
  9546. }
  9547. if (isset($credit['data']))
  9548. {
  9549. $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9550. }
  9551. $credits_parent[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name));
  9552. }
  9553. }
  9554. elseif ($credits = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'credit'))
  9555. {
  9556. foreach ($credits as $credit)
  9557. {
  9558. $credit_role = null;
  9559. $credit_scheme = null;
  9560. $credit_name = null;
  9561. if (isset($credit['attribs']['']['role']))
  9562. {
  9563. $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT);
  9564. }
  9565. if (isset($credit['attribs']['']['scheme']))
  9566. {
  9567. $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  9568. }
  9569. else
  9570. {
  9571. $credit_scheme = 'urn:ebu';
  9572. }
  9573. if (isset($credit['data']))
  9574. {
  9575. $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9576. }
  9577. $credits_parent[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name));
  9578. }
  9579. }
  9580. if (is_array($credits_parent))
  9581. {
  9582. $credits_parent = array_values(array_unique($credits_parent));
  9583. }
  9584. // DESCRIPTION
  9585. if ($description_parent = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'description'))
  9586. {
  9587. if (isset($description_parent[0]['data']))
  9588. {
  9589. $description_parent = $this->sanitize($description_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9590. }
  9591. }
  9592. elseif ($description_parent = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'description'))
  9593. {
  9594. if (isset($description_parent[0]['data']))
  9595. {
  9596. $description_parent = $this->sanitize($description_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9597. }
  9598. }
  9599. // DURATION
  9600. if ($duration_parent = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'duration'))
  9601. {
  9602. $seconds = null;
  9603. $minutes = null;
  9604. $hours = null;
  9605. if (isset($duration_parent[0]['data']))
  9606. {
  9607. $temp = explode(':', $this->sanitize($duration_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT));
  9608. if (sizeof($temp) > 0)
  9609. {
  9610. $seconds = (int) array_pop($temp);
  9611. }
  9612. if (sizeof($temp) > 0)
  9613. {
  9614. $minutes = (int) array_pop($temp);
  9615. $seconds += $minutes * 60;
  9616. }
  9617. if (sizeof($temp) > 0)
  9618. {
  9619. $hours = (int) array_pop($temp);
  9620. $seconds += $hours * 3600;
  9621. }
  9622. unset($temp);
  9623. $duration_parent = $seconds;
  9624. }
  9625. }
  9626. // HASHES
  9627. if ($hashes_iterator = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'hash'))
  9628. {
  9629. foreach ($hashes_iterator as $hash)
  9630. {
  9631. $value = null;
  9632. $algo = null;
  9633. if (isset($hash['data']))
  9634. {
  9635. $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9636. }
  9637. if (isset($hash['attribs']['']['algo']))
  9638. {
  9639. $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT);
  9640. }
  9641. else
  9642. {
  9643. $algo = 'md5';
  9644. }
  9645. $hashes_parent[] = $algo.':'.$value;
  9646. }
  9647. }
  9648. elseif ($hashes_iterator = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'hash'))
  9649. {
  9650. foreach ($hashes_iterator as $hash)
  9651. {
  9652. $value = null;
  9653. $algo = null;
  9654. if (isset($hash['data']))
  9655. {
  9656. $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9657. }
  9658. if (isset($hash['attribs']['']['algo']))
  9659. {
  9660. $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT);
  9661. }
  9662. else
  9663. {
  9664. $algo = 'md5';
  9665. }
  9666. $hashes_parent[] = $algo.':'.$value;
  9667. }
  9668. }
  9669. if (is_array($hashes_parent))
  9670. {
  9671. $hashes_parent = array_values(array_unique($hashes_parent));
  9672. }
  9673. // KEYWORDS
  9674. if ($keywords = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'keywords'))
  9675. {
  9676. if (isset($keywords[0]['data']))
  9677. {
  9678. $temp = explode(',', $this->sanitize($keywords[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT));
  9679. foreach ($temp as $word)
  9680. {
  9681. $keywords_parent[] = trim($word);
  9682. }
  9683. }
  9684. unset($temp);
  9685. }
  9686. elseif ($keywords = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'keywords'))
  9687. {
  9688. if (isset($keywords[0]['data']))
  9689. {
  9690. $temp = explode(',', $this->sanitize($keywords[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT));
  9691. foreach ($temp as $word)
  9692. {
  9693. $keywords_parent[] = trim($word);
  9694. }
  9695. }
  9696. unset($temp);
  9697. }
  9698. elseif ($keywords = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'keywords'))
  9699. {
  9700. if (isset($keywords[0]['data']))
  9701. {
  9702. $temp = explode(',', $this->sanitize($keywords[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT));
  9703. foreach ($temp as $word)
  9704. {
  9705. $keywords_parent[] = trim($word);
  9706. }
  9707. }
  9708. unset($temp);
  9709. }
  9710. elseif ($keywords = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'keywords'))
  9711. {
  9712. if (isset($keywords[0]['data']))
  9713. {
  9714. $temp = explode(',', $this->sanitize($keywords[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT));
  9715. foreach ($temp as $word)
  9716. {
  9717. $keywords_parent[] = trim($word);
  9718. }
  9719. }
  9720. unset($temp);
  9721. }
  9722. if (is_array($keywords_parent))
  9723. {
  9724. $keywords_parent = array_values(array_unique($keywords_parent));
  9725. }
  9726. // PLAYER
  9727. if ($player_parent = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'player'))
  9728. {
  9729. if (isset($player_parent[0]['attribs']['']['url']))
  9730. {
  9731. $player_parent = $this->sanitize($player_parent[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  9732. }
  9733. }
  9734. elseif ($player_parent = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'player'))
  9735. {
  9736. if (isset($player_parent[0]['attribs']['']['url']))
  9737. {
  9738. $player_parent = $this->sanitize($player_parent[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  9739. }
  9740. }
  9741. // RATINGS
  9742. if ($ratings = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'rating'))
  9743. {
  9744. foreach ($ratings as $rating)
  9745. {
  9746. $rating_scheme = null;
  9747. $rating_value = null;
  9748. if (isset($rating['attribs']['']['scheme']))
  9749. {
  9750. $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  9751. }
  9752. else
  9753. {
  9754. $rating_scheme = 'urn:simple';
  9755. }
  9756. if (isset($rating['data']))
  9757. {
  9758. $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9759. }
  9760. $ratings_parent[] = $this->registry->create('Rating', array($rating_scheme, $rating_value));
  9761. }
  9762. }
  9763. elseif ($ratings = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'explicit'))
  9764. {
  9765. foreach ($ratings as $rating)
  9766. {
  9767. $rating_scheme = 'urn:itunes';
  9768. $rating_value = null;
  9769. if (isset($rating['data']))
  9770. {
  9771. $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9772. }
  9773. $ratings_parent[] = $this->registry->create('Rating', array($rating_scheme, $rating_value));
  9774. }
  9775. }
  9776. elseif ($ratings = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'rating'))
  9777. {
  9778. foreach ($ratings as $rating)
  9779. {
  9780. $rating_scheme = null;
  9781. $rating_value = null;
  9782. if (isset($rating['attribs']['']['scheme']))
  9783. {
  9784. $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  9785. }
  9786. else
  9787. {
  9788. $rating_scheme = 'urn:simple';
  9789. }
  9790. if (isset($rating['data']))
  9791. {
  9792. $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9793. }
  9794. $ratings_parent[] = $this->registry->create('Rating', array($rating_scheme, $rating_value));
  9795. }
  9796. }
  9797. elseif ($ratings = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'explicit'))
  9798. {
  9799. foreach ($ratings as $rating)
  9800. {
  9801. $rating_scheme = 'urn:itunes';
  9802. $rating_value = null;
  9803. if (isset($rating['data']))
  9804. {
  9805. $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9806. }
  9807. $ratings_parent[] = $this->registry->create('Rating', array($rating_scheme, $rating_value));
  9808. }
  9809. }
  9810. if (is_array($ratings_parent))
  9811. {
  9812. $ratings_parent = array_values(array_unique($ratings_parent));
  9813. }
  9814. // RESTRICTIONS
  9815. if ($restrictions = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'restriction'))
  9816. {
  9817. foreach ($restrictions as $restriction)
  9818. {
  9819. $restriction_relationship = null;
  9820. $restriction_type = null;
  9821. $restriction_value = null;
  9822. if (isset($restriction['attribs']['']['relationship']))
  9823. {
  9824. $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT);
  9825. }
  9826. if (isset($restriction['attribs']['']['type']))
  9827. {
  9828. $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  9829. }
  9830. if (isset($restriction['data']))
  9831. {
  9832. $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9833. }
  9834. $restrictions_parent[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value));
  9835. }
  9836. }
  9837. elseif ($restrictions = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'block'))
  9838. {
  9839. foreach ($restrictions as $restriction)
  9840. {
  9841. $restriction_relationship = 'allow';
  9842. $restriction_type = null;
  9843. $restriction_value = 'itunes';
  9844. if (isset($restriction['data']) && strtolower($restriction['data']) === 'yes')
  9845. {
  9846. $restriction_relationship = 'deny';
  9847. }
  9848. $restrictions_parent[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value));
  9849. }
  9850. }
  9851. elseif ($restrictions = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'restriction'))
  9852. {
  9853. foreach ($restrictions as $restriction)
  9854. {
  9855. $restriction_relationship = null;
  9856. $restriction_type = null;
  9857. $restriction_value = null;
  9858. if (isset($restriction['attribs']['']['relationship']))
  9859. {
  9860. $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT);
  9861. }
  9862. if (isset($restriction['attribs']['']['type']))
  9863. {
  9864. $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  9865. }
  9866. if (isset($restriction['data']))
  9867. {
  9868. $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9869. }
  9870. $restrictions_parent[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value));
  9871. }
  9872. }
  9873. elseif ($restrictions = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'block'))
  9874. {
  9875. foreach ($restrictions as $restriction)
  9876. {
  9877. $restriction_relationship = 'allow';
  9878. $restriction_type = null;
  9879. $restriction_value = 'itunes';
  9880. if (isset($restriction['data']) && strtolower($restriction['data']) === 'yes')
  9881. {
  9882. $restriction_relationship = 'deny';
  9883. }
  9884. $restrictions_parent[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value));
  9885. }
  9886. }
  9887. if (is_array($restrictions_parent))
  9888. {
  9889. $restrictions_parent = array_values(array_unique($restrictions_parent));
  9890. }
  9891. else
  9892. {
  9893. $restrictions_parent = array(new SimplePie_Restriction('allow', null, 'default'));
  9894. }
  9895. // THUMBNAILS
  9896. if ($thumbnails = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'thumbnail'))
  9897. {
  9898. foreach ($thumbnails as $thumbnail)
  9899. {
  9900. if (isset($thumbnail['attribs']['']['url']))
  9901. {
  9902. $thumbnails_parent[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  9903. }
  9904. }
  9905. }
  9906. elseif ($thumbnails = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'thumbnail'))
  9907. {
  9908. foreach ($thumbnails as $thumbnail)
  9909. {
  9910. if (isset($thumbnail['attribs']['']['url']))
  9911. {
  9912. $thumbnails_parent[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  9913. }
  9914. }
  9915. }
  9916. // TITLES
  9917. if ($title_parent = $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'title'))
  9918. {
  9919. if (isset($title_parent[0]['data']))
  9920. {
  9921. $title_parent = $this->sanitize($title_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9922. }
  9923. }
  9924. elseif ($title_parent = $parent->get_channel_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'title'))
  9925. {
  9926. if (isset($title_parent[0]['data']))
  9927. {
  9928. $title_parent = $this->sanitize($title_parent[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  9929. }
  9930. }
  9931. // Clear the memory
  9932. unset($parent);
  9933. // Attributes
  9934. $bitrate = null;
  9935. $channels = null;
  9936. $duration = null;
  9937. $expression = null;
  9938. $framerate = null;
  9939. $height = null;
  9940. $javascript = null;
  9941. $lang = null;
  9942. $length = null;
  9943. $medium = null;
  9944. $samplingrate = null;
  9945. $type = null;
  9946. $url = null;
  9947. $width = null;
  9948. // Elements
  9949. $captions = null;
  9950. $categories = null;
  9951. $copyrights = null;
  9952. $credits = null;
  9953. $description = null;
  9954. $hashes = null;
  9955. $keywords = null;
  9956. $player = null;
  9957. $ratings = null;
  9958. $restrictions = null;
  9959. $thumbnails = null;
  9960. $title = null;
  9961. // If we have media:group tags, loop through them.
  9962. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'group') as $group)
  9963. {
  9964. if(isset($group['child']) && isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content']))
  9965. {
  9966. // If we have media:content tags, loop through them.
  9967. foreach ((array) $group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'] as $content)
  9968. {
  9969. if (isset($content['attribs']['']['url']))
  9970. {
  9971. // Attributes
  9972. $bitrate = null;
  9973. $channels = null;
  9974. $duration = null;
  9975. $expression = null;
  9976. $framerate = null;
  9977. $height = null;
  9978. $javascript = null;
  9979. $lang = null;
  9980. $length = null;
  9981. $medium = null;
  9982. $samplingrate = null;
  9983. $type = null;
  9984. $url = null;
  9985. $width = null;
  9986. // Elements
  9987. $captions = null;
  9988. $categories = null;
  9989. $copyrights = null;
  9990. $credits = null;
  9991. $description = null;
  9992. $hashes = null;
  9993. $keywords = null;
  9994. $player = null;
  9995. $ratings = null;
  9996. $restrictions = null;
  9997. $thumbnails = null;
  9998. $title = null;
  9999. // Start checking the attributes of media:content
  10000. if (isset($content['attribs']['']['bitrate']))
  10001. {
  10002. $bitrate = $this->sanitize($content['attribs']['']['bitrate'], SIMPLEPIE_CONSTRUCT_TEXT);
  10003. }
  10004. if (isset($content['attribs']['']['channels']))
  10005. {
  10006. $channels = $this->sanitize($content['attribs']['']['channels'], SIMPLEPIE_CONSTRUCT_TEXT);
  10007. }
  10008. if (isset($content['attribs']['']['duration']))
  10009. {
  10010. $duration = $this->sanitize($content['attribs']['']['duration'], SIMPLEPIE_CONSTRUCT_TEXT);
  10011. }
  10012. else
  10013. {
  10014. $duration = $duration_parent;
  10015. }
  10016. if (isset($content['attribs']['']['expression']))
  10017. {
  10018. $expression = $this->sanitize($content['attribs']['']['expression'], SIMPLEPIE_CONSTRUCT_TEXT);
  10019. }
  10020. if (isset($content['attribs']['']['framerate']))
  10021. {
  10022. $framerate = $this->sanitize($content['attribs']['']['framerate'], SIMPLEPIE_CONSTRUCT_TEXT);
  10023. }
  10024. if (isset($content['attribs']['']['height']))
  10025. {
  10026. $height = $this->sanitize($content['attribs']['']['height'], SIMPLEPIE_CONSTRUCT_TEXT);
  10027. }
  10028. if (isset($content['attribs']['']['lang']))
  10029. {
  10030. $lang = $this->sanitize($content['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  10031. }
  10032. if (isset($content['attribs']['']['fileSize']))
  10033. {
  10034. $length = ceil($content['attribs']['']['fileSize']);
  10035. }
  10036. if (isset($content['attribs']['']['medium']))
  10037. {
  10038. $medium = $this->sanitize($content['attribs']['']['medium'], SIMPLEPIE_CONSTRUCT_TEXT);
  10039. }
  10040. if (isset($content['attribs']['']['samplingrate']))
  10041. {
  10042. $samplingrate = $this->sanitize($content['attribs']['']['samplingrate'], SIMPLEPIE_CONSTRUCT_TEXT);
  10043. }
  10044. if (isset($content['attribs']['']['type']))
  10045. {
  10046. $type = $this->sanitize($content['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10047. }
  10048. if (isset($content['attribs']['']['width']))
  10049. {
  10050. $width = $this->sanitize($content['attribs']['']['width'], SIMPLEPIE_CONSTRUCT_TEXT);
  10051. }
  10052. $url = $this->sanitize($content['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  10053. // Checking the other optional media: elements. Priority: media:content, media:group, item, channel
  10054. // CAPTIONS
  10055. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text']))
  10056. {
  10057. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'] as $caption)
  10058. {
  10059. $caption_type = null;
  10060. $caption_lang = null;
  10061. $caption_startTime = null;
  10062. $caption_endTime = null;
  10063. $caption_text = null;
  10064. if (isset($caption['attribs']['']['type']))
  10065. {
  10066. $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10067. }
  10068. if (isset($caption['attribs']['']['lang']))
  10069. {
  10070. $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  10071. }
  10072. if (isset($caption['attribs']['']['start']))
  10073. {
  10074. $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT);
  10075. }
  10076. if (isset($caption['attribs']['']['end']))
  10077. {
  10078. $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT);
  10079. }
  10080. if (isset($caption['data']))
  10081. {
  10082. $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10083. }
  10084. $captions[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text));
  10085. }
  10086. if (is_array($captions))
  10087. {
  10088. $captions = array_values(array_unique($captions));
  10089. }
  10090. }
  10091. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text']))
  10092. {
  10093. foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'] as $caption)
  10094. {
  10095. $caption_type = null;
  10096. $caption_lang = null;
  10097. $caption_startTime = null;
  10098. $caption_endTime = null;
  10099. $caption_text = null;
  10100. if (isset($caption['attribs']['']['type']))
  10101. {
  10102. $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10103. }
  10104. if (isset($caption['attribs']['']['lang']))
  10105. {
  10106. $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  10107. }
  10108. if (isset($caption['attribs']['']['start']))
  10109. {
  10110. $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT);
  10111. }
  10112. if (isset($caption['attribs']['']['end']))
  10113. {
  10114. $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT);
  10115. }
  10116. if (isset($caption['data']))
  10117. {
  10118. $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10119. }
  10120. $captions[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text));
  10121. }
  10122. if (is_array($captions))
  10123. {
  10124. $captions = array_values(array_unique($captions));
  10125. }
  10126. }
  10127. else
  10128. {
  10129. $captions = $captions_parent;
  10130. }
  10131. // CATEGORIES
  10132. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category']))
  10133. {
  10134. foreach ((array) $content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'] as $category)
  10135. {
  10136. $term = null;
  10137. $scheme = null;
  10138. $label = null;
  10139. if (isset($category['data']))
  10140. {
  10141. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10142. }
  10143. if (isset($category['attribs']['']['scheme']))
  10144. {
  10145. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10146. }
  10147. else
  10148. {
  10149. $scheme = 'http://search.yahoo.com/mrss/category_schema';
  10150. }
  10151. if (isset($category['attribs']['']['label']))
  10152. {
  10153. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  10154. }
  10155. $categories[] = $this->registry->create('Category', array($term, $scheme, $label));
  10156. }
  10157. }
  10158. if (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category']))
  10159. {
  10160. foreach ((array) $group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'] as $category)
  10161. {
  10162. $term = null;
  10163. $scheme = null;
  10164. $label = null;
  10165. if (isset($category['data']))
  10166. {
  10167. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10168. }
  10169. if (isset($category['attribs']['']['scheme']))
  10170. {
  10171. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10172. }
  10173. else
  10174. {
  10175. $scheme = 'http://search.yahoo.com/mrss/category_schema';
  10176. }
  10177. if (isset($category['attribs']['']['label']))
  10178. {
  10179. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  10180. }
  10181. $categories[] = $this->registry->create('Category', array($term, $scheme, $label));
  10182. }
  10183. }
  10184. if (is_array($categories) && is_array($categories_parent))
  10185. {
  10186. $categories = array_values(array_unique(array_merge($categories, $categories_parent)));
  10187. }
  10188. elseif (is_array($categories))
  10189. {
  10190. $categories = array_values(array_unique($categories));
  10191. }
  10192. elseif (is_array($categories_parent))
  10193. {
  10194. $categories = array_values(array_unique($categories_parent));
  10195. }
  10196. // COPYRIGHTS
  10197. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright']))
  10198. {
  10199. $copyright_url = null;
  10200. $copyright_label = null;
  10201. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url']))
  10202. {
  10203. $copyright_url = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT);
  10204. }
  10205. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data']))
  10206. {
  10207. $copyright_label = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10208. }
  10209. $copyrights = $this->registry->create('Copyright', array($copyright_url, $copyright_label));
  10210. }
  10211. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright']))
  10212. {
  10213. $copyright_url = null;
  10214. $copyright_label = null;
  10215. if (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url']))
  10216. {
  10217. $copyright_url = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT);
  10218. }
  10219. if (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data']))
  10220. {
  10221. $copyright_label = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10222. }
  10223. $copyrights = $this->registry->create('Copyright', array($copyright_url, $copyright_label));
  10224. }
  10225. else
  10226. {
  10227. $copyrights = $copyrights_parent;
  10228. }
  10229. // CREDITS
  10230. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit']))
  10231. {
  10232. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'] as $credit)
  10233. {
  10234. $credit_role = null;
  10235. $credit_scheme = null;
  10236. $credit_name = null;
  10237. if (isset($credit['attribs']['']['role']))
  10238. {
  10239. $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT);
  10240. }
  10241. if (isset($credit['attribs']['']['scheme']))
  10242. {
  10243. $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10244. }
  10245. else
  10246. {
  10247. $credit_scheme = 'urn:ebu';
  10248. }
  10249. if (isset($credit['data']))
  10250. {
  10251. $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10252. }
  10253. $credits[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name));
  10254. }
  10255. if (is_array($credits))
  10256. {
  10257. $credits = array_values(array_unique($credits));
  10258. }
  10259. }
  10260. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit']))
  10261. {
  10262. foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'] as $credit)
  10263. {
  10264. $credit_role = null;
  10265. $credit_scheme = null;
  10266. $credit_name = null;
  10267. if (isset($credit['attribs']['']['role']))
  10268. {
  10269. $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT);
  10270. }
  10271. if (isset($credit['attribs']['']['scheme']))
  10272. {
  10273. $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10274. }
  10275. else
  10276. {
  10277. $credit_scheme = 'urn:ebu';
  10278. }
  10279. if (isset($credit['data']))
  10280. {
  10281. $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10282. }
  10283. $credits[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name));
  10284. }
  10285. if (is_array($credits))
  10286. {
  10287. $credits = array_values(array_unique($credits));
  10288. }
  10289. }
  10290. else
  10291. {
  10292. $credits = $credits_parent;
  10293. }
  10294. // DESCRIPTION
  10295. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description']))
  10296. {
  10297. $description = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10298. }
  10299. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description']))
  10300. {
  10301. $description = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10302. }
  10303. else
  10304. {
  10305. $description = $description_parent;
  10306. }
  10307. // HASHES
  10308. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash']))
  10309. {
  10310. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'] as $hash)
  10311. {
  10312. $value = null;
  10313. $algo = null;
  10314. if (isset($hash['data']))
  10315. {
  10316. $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10317. }
  10318. if (isset($hash['attribs']['']['algo']))
  10319. {
  10320. $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT);
  10321. }
  10322. else
  10323. {
  10324. $algo = 'md5';
  10325. }
  10326. $hashes[] = $algo.':'.$value;
  10327. }
  10328. if (is_array($hashes))
  10329. {
  10330. $hashes = array_values(array_unique($hashes));
  10331. }
  10332. }
  10333. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash']))
  10334. {
  10335. foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'] as $hash)
  10336. {
  10337. $value = null;
  10338. $algo = null;
  10339. if (isset($hash['data']))
  10340. {
  10341. $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10342. }
  10343. if (isset($hash['attribs']['']['algo']))
  10344. {
  10345. $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT);
  10346. }
  10347. else
  10348. {
  10349. $algo = 'md5';
  10350. }
  10351. $hashes[] = $algo.':'.$value;
  10352. }
  10353. if (is_array($hashes))
  10354. {
  10355. $hashes = array_values(array_unique($hashes));
  10356. }
  10357. }
  10358. else
  10359. {
  10360. $hashes = $hashes_parent;
  10361. }
  10362. // KEYWORDS
  10363. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords']))
  10364. {
  10365. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data']))
  10366. {
  10367. $temp = explode(',', $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT));
  10368. foreach ($temp as $word)
  10369. {
  10370. $keywords[] = trim($word);
  10371. }
  10372. unset($temp);
  10373. }
  10374. if (is_array($keywords))
  10375. {
  10376. $keywords = array_values(array_unique($keywords));
  10377. }
  10378. }
  10379. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords']))
  10380. {
  10381. if (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data']))
  10382. {
  10383. $temp = explode(',', $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT));
  10384. foreach ($temp as $word)
  10385. {
  10386. $keywords[] = trim($word);
  10387. }
  10388. unset($temp);
  10389. }
  10390. if (is_array($keywords))
  10391. {
  10392. $keywords = array_values(array_unique($keywords));
  10393. }
  10394. }
  10395. else
  10396. {
  10397. $keywords = $keywords_parent;
  10398. }
  10399. // PLAYER
  10400. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player']))
  10401. {
  10402. $player = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  10403. }
  10404. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player']))
  10405. {
  10406. $player = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  10407. }
  10408. else
  10409. {
  10410. $player = $player_parent;
  10411. }
  10412. // RATINGS
  10413. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating']))
  10414. {
  10415. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'] as $rating)
  10416. {
  10417. $rating_scheme = null;
  10418. $rating_value = null;
  10419. if (isset($rating['attribs']['']['scheme']))
  10420. {
  10421. $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10422. }
  10423. else
  10424. {
  10425. $rating_scheme = 'urn:simple';
  10426. }
  10427. if (isset($rating['data']))
  10428. {
  10429. $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10430. }
  10431. $ratings[] = $this->registry->create('Rating', array($rating_scheme, $rating_value));
  10432. }
  10433. if (is_array($ratings))
  10434. {
  10435. $ratings = array_values(array_unique($ratings));
  10436. }
  10437. }
  10438. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating']))
  10439. {
  10440. foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'] as $rating)
  10441. {
  10442. $rating_scheme = null;
  10443. $rating_value = null;
  10444. if (isset($rating['attribs']['']['scheme']))
  10445. {
  10446. $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10447. }
  10448. else
  10449. {
  10450. $rating_scheme = 'urn:simple';
  10451. }
  10452. if (isset($rating['data']))
  10453. {
  10454. $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10455. }
  10456. $ratings[] = $this->registry->create('Rating', array($rating_scheme, $rating_value));
  10457. }
  10458. if (is_array($ratings))
  10459. {
  10460. $ratings = array_values(array_unique($ratings));
  10461. }
  10462. }
  10463. else
  10464. {
  10465. $ratings = $ratings_parent;
  10466. }
  10467. // RESTRICTIONS
  10468. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction']))
  10469. {
  10470. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'] as $restriction)
  10471. {
  10472. $restriction_relationship = null;
  10473. $restriction_type = null;
  10474. $restriction_value = null;
  10475. if (isset($restriction['attribs']['']['relationship']))
  10476. {
  10477. $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT);
  10478. }
  10479. if (isset($restriction['attribs']['']['type']))
  10480. {
  10481. $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10482. }
  10483. if (isset($restriction['data']))
  10484. {
  10485. $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10486. }
  10487. $restrictions[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value));
  10488. }
  10489. if (is_array($restrictions))
  10490. {
  10491. $restrictions = array_values(array_unique($restrictions));
  10492. }
  10493. }
  10494. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction']))
  10495. {
  10496. foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'] as $restriction)
  10497. {
  10498. $restriction_relationship = null;
  10499. $restriction_type = null;
  10500. $restriction_value = null;
  10501. if (isset($restriction['attribs']['']['relationship']))
  10502. {
  10503. $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT);
  10504. }
  10505. if (isset($restriction['attribs']['']['type']))
  10506. {
  10507. $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10508. }
  10509. if (isset($restriction['data']))
  10510. {
  10511. $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10512. }
  10513. $restrictions[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value));
  10514. }
  10515. if (is_array($restrictions))
  10516. {
  10517. $restrictions = array_values(array_unique($restrictions));
  10518. }
  10519. }
  10520. else
  10521. {
  10522. $restrictions = $restrictions_parent;
  10523. }
  10524. // THUMBNAILS
  10525. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail']))
  10526. {
  10527. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail)
  10528. {
  10529. $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  10530. }
  10531. if (is_array($thumbnails))
  10532. {
  10533. $thumbnails = array_values(array_unique($thumbnails));
  10534. }
  10535. }
  10536. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail']))
  10537. {
  10538. foreach ($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail)
  10539. {
  10540. $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  10541. }
  10542. if (is_array($thumbnails))
  10543. {
  10544. $thumbnails = array_values(array_unique($thumbnails));
  10545. }
  10546. }
  10547. else
  10548. {
  10549. $thumbnails = $thumbnails_parent;
  10550. }
  10551. // TITLES
  10552. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title']))
  10553. {
  10554. $title = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10555. }
  10556. elseif (isset($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title']))
  10557. {
  10558. $title = $this->sanitize($group['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10559. }
  10560. else
  10561. {
  10562. $title = $title_parent;
  10563. }
  10564. $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions, $categories, $channels, $copyrights, $credits, $description, $duration, $expression, $framerate, $hashes, $height, $keywords, $lang, $medium, $player, $ratings, $restrictions, $samplingrate, $thumbnails, $title, $width));
  10565. }
  10566. }
  10567. }
  10568. }
  10569. // If we have standalone media:content tags, loop through them.
  10570. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content']))
  10571. {
  10572. foreach ((array) $this->data['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'] as $content)
  10573. {
  10574. if (isset($content['attribs']['']['url']) || isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player']))
  10575. {
  10576. // Attributes
  10577. $bitrate = null;
  10578. $channels = null;
  10579. $duration = null;
  10580. $expression = null;
  10581. $framerate = null;
  10582. $height = null;
  10583. $javascript = null;
  10584. $lang = null;
  10585. $length = null;
  10586. $medium = null;
  10587. $samplingrate = null;
  10588. $type = null;
  10589. $url = null;
  10590. $width = null;
  10591. // Elements
  10592. $captions = null;
  10593. $categories = null;
  10594. $copyrights = null;
  10595. $credits = null;
  10596. $description = null;
  10597. $hashes = null;
  10598. $keywords = null;
  10599. $player = null;
  10600. $ratings = null;
  10601. $restrictions = null;
  10602. $thumbnails = null;
  10603. $title = null;
  10604. // Start checking the attributes of media:content
  10605. if (isset($content['attribs']['']['bitrate']))
  10606. {
  10607. $bitrate = $this->sanitize($content['attribs']['']['bitrate'], SIMPLEPIE_CONSTRUCT_TEXT);
  10608. }
  10609. if (isset($content['attribs']['']['channels']))
  10610. {
  10611. $channels = $this->sanitize($content['attribs']['']['channels'], SIMPLEPIE_CONSTRUCT_TEXT);
  10612. }
  10613. if (isset($content['attribs']['']['duration']))
  10614. {
  10615. $duration = $this->sanitize($content['attribs']['']['duration'], SIMPLEPIE_CONSTRUCT_TEXT);
  10616. }
  10617. else
  10618. {
  10619. $duration = $duration_parent;
  10620. }
  10621. if (isset($content['attribs']['']['expression']))
  10622. {
  10623. $expression = $this->sanitize($content['attribs']['']['expression'], SIMPLEPIE_CONSTRUCT_TEXT);
  10624. }
  10625. if (isset($content['attribs']['']['framerate']))
  10626. {
  10627. $framerate = $this->sanitize($content['attribs']['']['framerate'], SIMPLEPIE_CONSTRUCT_TEXT);
  10628. }
  10629. if (isset($content['attribs']['']['height']))
  10630. {
  10631. $height = $this->sanitize($content['attribs']['']['height'], SIMPLEPIE_CONSTRUCT_TEXT);
  10632. }
  10633. if (isset($content['attribs']['']['lang']))
  10634. {
  10635. $lang = $this->sanitize($content['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  10636. }
  10637. if (isset($content['attribs']['']['fileSize']))
  10638. {
  10639. $length = ceil($content['attribs']['']['fileSize']);
  10640. }
  10641. if (isset($content['attribs']['']['medium']))
  10642. {
  10643. $medium = $this->sanitize($content['attribs']['']['medium'], SIMPLEPIE_CONSTRUCT_TEXT);
  10644. }
  10645. if (isset($content['attribs']['']['samplingrate']))
  10646. {
  10647. $samplingrate = $this->sanitize($content['attribs']['']['samplingrate'], SIMPLEPIE_CONSTRUCT_TEXT);
  10648. }
  10649. if (isset($content['attribs']['']['type']))
  10650. {
  10651. $type = $this->sanitize($content['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10652. }
  10653. if (isset($content['attribs']['']['width']))
  10654. {
  10655. $width = $this->sanitize($content['attribs']['']['width'], SIMPLEPIE_CONSTRUCT_TEXT);
  10656. }
  10657. if (isset($content['attribs']['']['url']))
  10658. {
  10659. $url = $this->sanitize($content['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  10660. }
  10661. // Checking the other optional media: elements. Priority: media:content, media:group, item, channel
  10662. // CAPTIONS
  10663. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text']))
  10664. {
  10665. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['text'] as $caption)
  10666. {
  10667. $caption_type = null;
  10668. $caption_lang = null;
  10669. $caption_startTime = null;
  10670. $caption_endTime = null;
  10671. $caption_text = null;
  10672. if (isset($caption['attribs']['']['type']))
  10673. {
  10674. $caption_type = $this->sanitize($caption['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10675. }
  10676. if (isset($caption['attribs']['']['lang']))
  10677. {
  10678. $caption_lang = $this->sanitize($caption['attribs']['']['lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  10679. }
  10680. if (isset($caption['attribs']['']['start']))
  10681. {
  10682. $caption_startTime = $this->sanitize($caption['attribs']['']['start'], SIMPLEPIE_CONSTRUCT_TEXT);
  10683. }
  10684. if (isset($caption['attribs']['']['end']))
  10685. {
  10686. $caption_endTime = $this->sanitize($caption['attribs']['']['end'], SIMPLEPIE_CONSTRUCT_TEXT);
  10687. }
  10688. if (isset($caption['data']))
  10689. {
  10690. $caption_text = $this->sanitize($caption['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10691. }
  10692. $captions[] = $this->registry->create('Caption', array($caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text));
  10693. }
  10694. if (is_array($captions))
  10695. {
  10696. $captions = array_values(array_unique($captions));
  10697. }
  10698. }
  10699. else
  10700. {
  10701. $captions = $captions_parent;
  10702. }
  10703. // CATEGORIES
  10704. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category']))
  10705. {
  10706. foreach ((array) $content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['category'] as $category)
  10707. {
  10708. $term = null;
  10709. $scheme = null;
  10710. $label = null;
  10711. if (isset($category['data']))
  10712. {
  10713. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10714. }
  10715. if (isset($category['attribs']['']['scheme']))
  10716. {
  10717. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10718. }
  10719. else
  10720. {
  10721. $scheme = 'http://search.yahoo.com/mrss/category_schema';
  10722. }
  10723. if (isset($category['attribs']['']['label']))
  10724. {
  10725. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  10726. }
  10727. $categories[] = $this->registry->create('Category', array($term, $scheme, $label));
  10728. }
  10729. }
  10730. if (is_array($categories) && is_array($categories_parent))
  10731. {
  10732. $categories = array_values(array_unique(array_merge($categories, $categories_parent)));
  10733. }
  10734. elseif (is_array($categories))
  10735. {
  10736. $categories = array_values(array_unique($categories));
  10737. }
  10738. elseif (is_array($categories_parent))
  10739. {
  10740. $categories = array_values(array_unique($categories_parent));
  10741. }
  10742. else
  10743. {
  10744. $categories = null;
  10745. }
  10746. // COPYRIGHTS
  10747. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright']))
  10748. {
  10749. $copyright_url = null;
  10750. $copyright_label = null;
  10751. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url']))
  10752. {
  10753. $copyright_url = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_TEXT);
  10754. }
  10755. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data']))
  10756. {
  10757. $copyright_label = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['copyright'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10758. }
  10759. $copyrights = $this->registry->create('Copyright', array($copyright_url, $copyright_label));
  10760. }
  10761. else
  10762. {
  10763. $copyrights = $copyrights_parent;
  10764. }
  10765. // CREDITS
  10766. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit']))
  10767. {
  10768. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['credit'] as $credit)
  10769. {
  10770. $credit_role = null;
  10771. $credit_scheme = null;
  10772. $credit_name = null;
  10773. if (isset($credit['attribs']['']['role']))
  10774. {
  10775. $credit_role = $this->sanitize($credit['attribs']['']['role'], SIMPLEPIE_CONSTRUCT_TEXT);
  10776. }
  10777. if (isset($credit['attribs']['']['scheme']))
  10778. {
  10779. $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10780. }
  10781. else
  10782. {
  10783. $credit_scheme = 'urn:ebu';
  10784. }
  10785. if (isset($credit['data']))
  10786. {
  10787. $credit_name = $this->sanitize($credit['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10788. }
  10789. $credits[] = $this->registry->create('Credit', array($credit_role, $credit_scheme, $credit_name));
  10790. }
  10791. if (is_array($credits))
  10792. {
  10793. $credits = array_values(array_unique($credits));
  10794. }
  10795. }
  10796. else
  10797. {
  10798. $credits = $credits_parent;
  10799. }
  10800. // DESCRIPTION
  10801. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description']))
  10802. {
  10803. $description = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['description'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10804. }
  10805. else
  10806. {
  10807. $description = $description_parent;
  10808. }
  10809. // HASHES
  10810. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash']))
  10811. {
  10812. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['hash'] as $hash)
  10813. {
  10814. $value = null;
  10815. $algo = null;
  10816. if (isset($hash['data']))
  10817. {
  10818. $value = $this->sanitize($hash['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10819. }
  10820. if (isset($hash['attribs']['']['algo']))
  10821. {
  10822. $algo = $this->sanitize($hash['attribs']['']['algo'], SIMPLEPIE_CONSTRUCT_TEXT);
  10823. }
  10824. else
  10825. {
  10826. $algo = 'md5';
  10827. }
  10828. $hashes[] = $algo.':'.$value;
  10829. }
  10830. if (is_array($hashes))
  10831. {
  10832. $hashes = array_values(array_unique($hashes));
  10833. }
  10834. }
  10835. else
  10836. {
  10837. $hashes = $hashes_parent;
  10838. }
  10839. // KEYWORDS
  10840. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords']))
  10841. {
  10842. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data']))
  10843. {
  10844. $temp = explode(',', $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['keywords'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT));
  10845. foreach ($temp as $word)
  10846. {
  10847. $keywords[] = trim($word);
  10848. }
  10849. unset($temp);
  10850. }
  10851. if (is_array($keywords))
  10852. {
  10853. $keywords = array_values(array_unique($keywords));
  10854. }
  10855. }
  10856. else
  10857. {
  10858. $keywords = $keywords_parent;
  10859. }
  10860. // PLAYER
  10861. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player']))
  10862. {
  10863. $player = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['player'][0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  10864. }
  10865. else
  10866. {
  10867. $player = $player_parent;
  10868. }
  10869. // RATINGS
  10870. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating']))
  10871. {
  10872. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['rating'] as $rating)
  10873. {
  10874. $rating_scheme = null;
  10875. $rating_value = null;
  10876. if (isset($rating['attribs']['']['scheme']))
  10877. {
  10878. $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  10879. }
  10880. else
  10881. {
  10882. $rating_scheme = 'urn:simple';
  10883. }
  10884. if (isset($rating['data']))
  10885. {
  10886. $rating_value = $this->sanitize($rating['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10887. }
  10888. $ratings[] = $this->registry->create('Rating', array($rating_scheme, $rating_value));
  10889. }
  10890. if (is_array($ratings))
  10891. {
  10892. $ratings = array_values(array_unique($ratings));
  10893. }
  10894. }
  10895. else
  10896. {
  10897. $ratings = $ratings_parent;
  10898. }
  10899. // RESTRICTIONS
  10900. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction']))
  10901. {
  10902. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['restriction'] as $restriction)
  10903. {
  10904. $restriction_relationship = null;
  10905. $restriction_type = null;
  10906. $restriction_value = null;
  10907. if (isset($restriction['attribs']['']['relationship']))
  10908. {
  10909. $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], SIMPLEPIE_CONSTRUCT_TEXT);
  10910. }
  10911. if (isset($restriction['attribs']['']['type']))
  10912. {
  10913. $restriction_type = $this->sanitize($restriction['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10914. }
  10915. if (isset($restriction['data']))
  10916. {
  10917. $restriction_value = $this->sanitize($restriction['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10918. }
  10919. $restrictions[] = $this->registry->create('Restriction', array($restriction_relationship, $restriction_type, $restriction_value));
  10920. }
  10921. if (is_array($restrictions))
  10922. {
  10923. $restrictions = array_values(array_unique($restrictions));
  10924. }
  10925. }
  10926. else
  10927. {
  10928. $restrictions = $restrictions_parent;
  10929. }
  10930. // THUMBNAILS
  10931. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail']))
  10932. {
  10933. foreach ($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail)
  10934. {
  10935. $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI);
  10936. }
  10937. if (is_array($thumbnails))
  10938. {
  10939. $thumbnails = array_values(array_unique($thumbnails));
  10940. }
  10941. }
  10942. else
  10943. {
  10944. $thumbnails = $thumbnails_parent;
  10945. }
  10946. // TITLES
  10947. if (isset($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title']))
  10948. {
  10949. $title = $this->sanitize($content['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['title'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  10950. }
  10951. else
  10952. {
  10953. $title = $title_parent;
  10954. }
  10955. $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions, $categories, $channels, $copyrights, $credits, $description, $duration, $expression, $framerate, $hashes, $height, $keywords, $lang, $medium, $player, $ratings, $restrictions, $samplingrate, $thumbnails, $title, $width));
  10956. }
  10957. }
  10958. }
  10959. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link') as $link)
  10960. {
  10961. if (isset($link['attribs']['']['href']) && !empty($link['attribs']['']['rel']) && $link['attribs']['']['rel'] === 'enclosure')
  10962. {
  10963. // Attributes
  10964. $bitrate = null;
  10965. $channels = null;
  10966. $duration = null;
  10967. $expression = null;
  10968. $framerate = null;
  10969. $height = null;
  10970. $javascript = null;
  10971. $lang = null;
  10972. $length = null;
  10973. $medium = null;
  10974. $samplingrate = null;
  10975. $type = null;
  10976. $url = null;
  10977. $width = null;
  10978. $url = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  10979. if (isset($link['attribs']['']['type']))
  10980. {
  10981. $type = $this->sanitize($link['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  10982. }
  10983. if (isset($link['attribs']['']['length']))
  10984. {
  10985. $length = ceil($link['attribs']['']['length']);
  10986. }
  10987. // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor
  10988. $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width));
  10989. }
  10990. }
  10991. foreach ((array) $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link') as $link)
  10992. {
  10993. if (isset($link['attribs']['']['href']) && !empty($link['attribs']['']['rel']) && $link['attribs']['']['rel'] === 'enclosure')
  10994. {
  10995. // Attributes
  10996. $bitrate = null;
  10997. $channels = null;
  10998. $duration = null;
  10999. $expression = null;
  11000. $framerate = null;
  11001. $height = null;
  11002. $javascript = null;
  11003. $lang = null;
  11004. $length = null;
  11005. $medium = null;
  11006. $samplingrate = null;
  11007. $type = null;
  11008. $url = null;
  11009. $width = null;
  11010. $url = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  11011. if (isset($link['attribs']['']['type']))
  11012. {
  11013. $type = $this->sanitize($link['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  11014. }
  11015. if (isset($link['attribs']['']['length']))
  11016. {
  11017. $length = ceil($link['attribs']['']['length']);
  11018. }
  11019. // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor
  11020. $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width));
  11021. }
  11022. }
  11023. if ($enclosure = $this->get_item_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'enclosure'))
  11024. {
  11025. if (isset($enclosure[0]['attribs']['']['url']))
  11026. {
  11027. // Attributes
  11028. $bitrate = null;
  11029. $channels = null;
  11030. $duration = null;
  11031. $expression = null;
  11032. $framerate = null;
  11033. $height = null;
  11034. $javascript = null;
  11035. $lang = null;
  11036. $length = null;
  11037. $medium = null;
  11038. $samplingrate = null;
  11039. $type = null;
  11040. $url = null;
  11041. $width = null;
  11042. $url = $this->sanitize($enclosure[0]['attribs']['']['url'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($enclosure[0]));
  11043. if (isset($enclosure[0]['attribs']['']['type']))
  11044. {
  11045. $type = $this->sanitize($enclosure[0]['attribs']['']['type'], SIMPLEPIE_CONSTRUCT_TEXT);
  11046. }
  11047. if (isset($enclosure[0]['attribs']['']['length']))
  11048. {
  11049. $length = ceil($enclosure[0]['attribs']['']['length']);
  11050. }
  11051. // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor
  11052. $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width));
  11053. }
  11054. }
  11055. if (sizeof($this->data['enclosures']) === 0 && ($url || $type || $length || $bitrate || $captions_parent || $categories_parent || $channels || $copyrights_parent || $credits_parent || $description_parent || $duration_parent || $expression || $framerate || $hashes_parent || $height || $keywords_parent || $lang || $medium || $player_parent || $ratings_parent || $restrictions_parent || $samplingrate || $thumbnails_parent || $title_parent || $width))
  11056. {
  11057. // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor
  11058. $this->data['enclosures'][] = $this->registry->create('Enclosure', array($url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width));
  11059. }
  11060. $this->data['enclosures'] = array_values(array_unique($this->data['enclosures']));
  11061. }
  11062. if (!empty($this->data['enclosures']))
  11063. {
  11064. return $this->data['enclosures'];
  11065. }
  11066. else
  11067. {
  11068. return null;
  11069. }
  11070. }
  11071. /**
  11072. * Get the latitude coordinates for the item
  11073. *
  11074. * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications
  11075. *
  11076. * Uses `<geo:lat>` or `<georss:point>`
  11077. *
  11078. * @since 1.0
  11079. * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo
  11080. * @link http://www.georss.org/ GeoRSS
  11081. * @return string|null
  11082. */
  11083. public function get_latitude()
  11084. {
  11085. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lat'))
  11086. {
  11087. return (float) $return[0]['data'];
  11088. }
  11089. elseif (($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  11090. {
  11091. return (float) $match[1];
  11092. }
  11093. else
  11094. {
  11095. return null;
  11096. }
  11097. }
  11098. /**
  11099. * Get the longitude coordinates for the item
  11100. *
  11101. * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications
  11102. *
  11103. * Uses `<geo:long>`, `<geo:lon>` or `<georss:point>`
  11104. *
  11105. * @since 1.0
  11106. * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo
  11107. * @link http://www.georss.org/ GeoRSS
  11108. * @return string|null
  11109. */
  11110. public function get_longitude()
  11111. {
  11112. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'long'))
  11113. {
  11114. return (float) $return[0]['data'];
  11115. }
  11116. elseif ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lon'))
  11117. {
  11118. return (float) $return[0]['data'];
  11119. }
  11120. elseif (($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  11121. {
  11122. return (float) $match[2];
  11123. }
  11124. else
  11125. {
  11126. return null;
  11127. }
  11128. }
  11129. /**
  11130. * Get the `<atom:source>` for the item
  11131. *
  11132. * @since 1.1
  11133. * @return SimplePie_Source|null
  11134. */
  11135. public function get_source()
  11136. {
  11137. if ($return = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'source'))
  11138. {
  11139. return $this->registry->create('Source', array($this, $return[0]));
  11140. }
  11141. else
  11142. {
  11143. return null;
  11144. }
  11145. }
  11146. }
  11147. /**
  11148. * Used for feed auto-discovery
  11149. *
  11150. *
  11151. * This class can be overloaded with {@see SimplePie::set_locator_class()}
  11152. *
  11153. * @package SimplePie
  11154. */
  11155. class SimplePie_Locator
  11156. {
  11157. var $useragent;
  11158. var $timeout;
  11159. var $file;
  11160. var $local = array();
  11161. var $elsewhere = array();
  11162. var $cached_entities = array();
  11163. var $http_base;
  11164. var $base;
  11165. var $base_location = 0;
  11166. var $checked_feeds = 0;
  11167. var $max_checked_feeds = 10;
  11168. protected $registry;
  11169. public function __construct(SimplePie_File $file, $timeout = 10, $useragent = null, $max_checked_feeds = 10)
  11170. {
  11171. $this->file = $file;
  11172. $this->useragent = $useragent;
  11173. $this->timeout = $timeout;
  11174. $this->max_checked_feeds = $max_checked_feeds;
  11175. if (class_exists('DOMDocument'))
  11176. {
  11177. $this->dom = new DOMDocument();
  11178. set_error_handler(array('SimplePie_Misc', 'silence_errors'));
  11179. $this->dom->loadHTML($this->file->body);
  11180. restore_error_handler();
  11181. }
  11182. else
  11183. {
  11184. $this->dom = null;
  11185. }
  11186. }
  11187. public function set_registry(SimplePie_Registry $registry)
  11188. {
  11189. $this->registry = $registry;
  11190. }
  11191. public function find($type = SIMPLEPIE_LOCATOR_ALL, &$working)
  11192. {
  11193. if ($this->is_feed($this->file))
  11194. {
  11195. return $this->file;
  11196. }
  11197. if ($this->file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
  11198. {
  11199. $sniffer = $this->registry->create('Content_Type_Sniffer', array($this->file));
  11200. if ($sniffer->get_type() !== 'text/html')
  11201. {
  11202. return null;
  11203. }
  11204. }
  11205. if ($type & ~SIMPLEPIE_LOCATOR_NONE)
  11206. {
  11207. $this->get_base();
  11208. }
  11209. if ($type & SIMPLEPIE_LOCATOR_AUTODISCOVERY && $working = $this->autodiscovery())
  11210. {
  11211. return $working[0];
  11212. }
  11213. if ($type & (SIMPLEPIE_LOCATOR_LOCAL_EXTENSION | SIMPLEPIE_LOCATOR_LOCAL_BODY | SIMPLEPIE_LOCATOR_REMOTE_EXTENSION | SIMPLEPIE_LOCATOR_REMOTE_BODY) && $this->get_links())
  11214. {
  11215. if ($type & SIMPLEPIE_LOCATOR_LOCAL_EXTENSION && $working = $this->extension($this->local))
  11216. {
  11217. return $working;
  11218. }
  11219. if ($type & SIMPLEPIE_LOCATOR_LOCAL_BODY && $working = $this->body($this->local))
  11220. {
  11221. return $working;
  11222. }
  11223. if ($type & SIMPLEPIE_LOCATOR_REMOTE_EXTENSION && $working = $this->extension($this->elsewhere))
  11224. {
  11225. return $working;
  11226. }
  11227. if ($type & SIMPLEPIE_LOCATOR_REMOTE_BODY && $working = $this->body($this->elsewhere))
  11228. {
  11229. return $working;
  11230. }
  11231. }
  11232. return null;
  11233. }
  11234. public function is_feed($file)
  11235. {
  11236. if ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
  11237. {
  11238. $sniffer = $this->registry->create('Content_Type_Sniffer', array($file));
  11239. $sniffed = $sniffer->get_type();
  11240. if (in_array($sniffed, array('application/rss+xml', 'application/rdf+xml', 'text/rdf', 'application/atom+xml', 'text/xml', 'application/xml')))
  11241. {
  11242. return true;
  11243. }
  11244. else
  11245. {
  11246. return false;
  11247. }
  11248. }
  11249. elseif ($file->method & SIMPLEPIE_FILE_SOURCE_LOCAL)
  11250. {
  11251. return true;
  11252. }
  11253. else
  11254. {
  11255. return false;
  11256. }
  11257. }
  11258. public function get_base()
  11259. {
  11260. if ($this->dom === null)
  11261. {
  11262. throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
  11263. }
  11264. $this->http_base = $this->file->url;
  11265. $this->base = $this->http_base;
  11266. $elements = $this->dom->getElementsByTagName('base');
  11267. foreach ($elements as $element)
  11268. {
  11269. if ($element->hasAttribute('href'))
  11270. {
  11271. $base = $this->registry->call('Misc', 'absolutize_url', array(trim($element->getAttribute('href')), $this->http_base));
  11272. if ($base === false)
  11273. {
  11274. continue;
  11275. }
  11276. $this->base = $base;
  11277. $this->base_location = method_exists($element, 'getLineNo') ? $element->getLineNo() : 0;
  11278. break;
  11279. }
  11280. }
  11281. }
  11282. public function autodiscovery()
  11283. {
  11284. $done = array();
  11285. $feeds = array();
  11286. $feeds = array_merge($feeds, $this->search_elements_by_tag('link', $done, $feeds));
  11287. $feeds = array_merge($feeds, $this->search_elements_by_tag('a', $done, $feeds));
  11288. $feeds = array_merge($feeds, $this->search_elements_by_tag('area', $done, $feeds));
  11289. if (!empty($feeds))
  11290. {
  11291. return array_values($feeds);
  11292. }
  11293. else
  11294. {
  11295. return null;
  11296. }
  11297. }
  11298. protected function search_elements_by_tag($name, &$done, $feeds)
  11299. {
  11300. if ($this->dom === null)
  11301. {
  11302. throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
  11303. }
  11304. $links = $this->dom->getElementsByTagName($name);
  11305. foreach ($links as $link)
  11306. {
  11307. if ($this->checked_feeds === $this->max_checked_feeds)
  11308. {
  11309. break;
  11310. }
  11311. if ($link->hasAttribute('href') && $link->hasAttribute('rel'))
  11312. {
  11313. $rel = array_unique($this->registry->call('Misc', 'space_seperated_tokens', array(strtolower($link->getAttribute('rel')))));
  11314. $line = method_exists($link, 'getLineNo') ? $link->getLineNo() : 1;
  11315. if ($this->base_location < $line)
  11316. {
  11317. $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->base));
  11318. }
  11319. else
  11320. {
  11321. $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->http_base));
  11322. }
  11323. if ($href === false)
  11324. {
  11325. continue;
  11326. }
  11327. if (!in_array($href, $done) && in_array('feed', $rel) || (in_array('alternate', $rel) && !in_array('stylesheet', $rel) && $link->hasAttribute('type') && in_array(strtolower($this->registry->call('Misc', 'parse_mime', array($link->getAttribute('type')))), array('application/rss+xml', 'application/atom+xml'))) && !isset($feeds[$href]))
  11328. {
  11329. $this->checked_feeds++;
  11330. $headers = array(
  11331. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  11332. );
  11333. $feed = $this->registry->create('File', array($href, $this->timeout, 5, $headers, $this->useragent));
  11334. if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))
  11335. {
  11336. $feeds[$href] = $feed;
  11337. }
  11338. }
  11339. $done[] = $href;
  11340. }
  11341. }
  11342. return $feeds;
  11343. }
  11344. public function get_links()
  11345. {
  11346. if ($this->dom === null)
  11347. {
  11348. throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
  11349. }
  11350. $links = $this->dom->getElementsByTagName('a');
  11351. foreach ($links as $link)
  11352. {
  11353. if ($link->hasAttribute('href'))
  11354. {
  11355. $href = trim($link->getAttribute('href'));
  11356. $parsed = $this->registry->call('Misc', 'parse_url', array($href));
  11357. if ($parsed['scheme'] === '' || preg_match('/^(http(s)|feed)?$/i', $parsed['scheme']))
  11358. {
  11359. if ($this->base_location < $link->getLineNo())
  11360. {
  11361. $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->base));
  11362. }
  11363. else
  11364. {
  11365. $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->http_base));
  11366. }
  11367. if ($href === false)
  11368. {
  11369. continue;
  11370. }
  11371. $current = $this->registry->call('Misc', 'parse_url', array($this->file->url));
  11372. if ($parsed['authority'] === '' || $parsed['authority'] === $current['authority'])
  11373. {
  11374. $this->local[] = $href;
  11375. }
  11376. else
  11377. {
  11378. $this->elsewhere[] = $href;
  11379. }
  11380. }
  11381. }
  11382. }
  11383. $this->local = array_unique($this->local);
  11384. $this->elsewhere = array_unique($this->elsewhere);
  11385. if (!empty($this->local) || !empty($this->elsewhere))
  11386. {
  11387. return true;
  11388. }
  11389. return null;
  11390. }
  11391. public function extension(&$array)
  11392. {
  11393. foreach ($array as $key => $value)
  11394. {
  11395. if ($this->checked_feeds === $this->max_checked_feeds)
  11396. {
  11397. break;
  11398. }
  11399. if (in_array(strtolower(strrchr($value, '.')), array('.rss', '.rdf', '.atom', '.xml')))
  11400. {
  11401. $this->checked_feeds++;
  11402. $headers = array(
  11403. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  11404. );
  11405. $feed = $this->registry->create('File', array($value, $this->timeout, 5, $headers, $this->useragent));
  11406. if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))
  11407. {
  11408. return $feed;
  11409. }
  11410. else
  11411. {
  11412. unset($array[$key]);
  11413. }
  11414. }
  11415. }
  11416. return null;
  11417. }
  11418. public function body(&$array)
  11419. {
  11420. foreach ($array as $key => $value)
  11421. {
  11422. if ($this->checked_feeds === $this->max_checked_feeds)
  11423. {
  11424. break;
  11425. }
  11426. if (preg_match('/(rss|rdf|atom|xml)/i', $value))
  11427. {
  11428. $this->checked_feeds++;
  11429. $headers = array(
  11430. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  11431. );
  11432. $feed = $this->registry->create('File', array($value, $this->timeout, 5, null, $this->useragent));
  11433. if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))
  11434. {
  11435. return $feed;
  11436. }
  11437. else
  11438. {
  11439. unset($array[$key]);
  11440. }
  11441. }
  11442. }
  11443. return null;
  11444. }
  11445. }
  11446. /**
  11447. * Miscellanous utilities
  11448. *
  11449. * @package SimplePie
  11450. */
  11451. class SimplePie_Misc
  11452. {
  11453. public static function time_hms($seconds)
  11454. {
  11455. $time = '';
  11456. $hours = floor($seconds / 3600);
  11457. $remainder = $seconds % 3600;
  11458. if ($hours > 0)
  11459. {
  11460. $time .= $hours.':';
  11461. }
  11462. $minutes = floor($remainder / 60);
  11463. $seconds = $remainder % 60;
  11464. if ($minutes < 10 && $hours > 0)
  11465. {
  11466. $minutes = '0' . $minutes;
  11467. }
  11468. if ($seconds < 10)
  11469. {
  11470. $seconds = '0' . $seconds;
  11471. }
  11472. $time .= $minutes.':';
  11473. $time .= $seconds;
  11474. return $time;
  11475. }
  11476. public static function absolutize_url($relative, $base)
  11477. {
  11478. $iri = SimplePie_IRI::absolutize(new SimplePie_IRI($base), $relative);
  11479. if ($iri === false)
  11480. {
  11481. return false;
  11482. }
  11483. return $iri->get_uri();
  11484. }
  11485. /**
  11486. * Get a HTML/XML element from a HTML string
  11487. *
  11488. * @deprecated Use DOMDocument instead (parsing HTML with regex is bad!)
  11489. * @param string $realname Element name (including namespace prefix if applicable)
  11490. * @param string $string HTML document
  11491. * @return array
  11492. */
  11493. public static function get_element($realname, $string)
  11494. {
  11495. $return = array();
  11496. $name = preg_quote($realname, '/');
  11497. if (preg_match_all("/<($name)" . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . "(>(.*)<\/$name>|(\/)?>)/siU", $string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
  11498. {
  11499. for ($i = 0, $total_matches = count($matches); $i < $total_matches; $i++)
  11500. {
  11501. $return[$i]['tag'] = $realname;
  11502. $return[$i]['full'] = $matches[$i][0][0];
  11503. $return[$i]['offset'] = $matches[$i][0][1];
  11504. if (strlen($matches[$i][3][0]) <= 2)
  11505. {
  11506. $return[$i]['self_closing'] = true;
  11507. }
  11508. else
  11509. {
  11510. $return[$i]['self_closing'] = false;
  11511. $return[$i]['content'] = $matches[$i][4][0];
  11512. }
  11513. $return[$i]['attribs'] = array();
  11514. if (isset($matches[$i][2][0]) && preg_match_all('/[\x09\x0A\x0B\x0C\x0D\x20]+([^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*)(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"([^"]*)"|\'([^\']*)\'|([^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?/', ' ' . $matches[$i][2][0] . ' ', $attribs, PREG_SET_ORDER))
  11515. {
  11516. for ($j = 0, $total_attribs = count($attribs); $j < $total_attribs; $j++)
  11517. {
  11518. if (count($attribs[$j]) === 2)
  11519. {
  11520. $attribs[$j][2] = $attribs[$j][1];
  11521. }
  11522. $return[$i]['attribs'][strtolower($attribs[$j][1])]['data'] = SimplePie_Misc::entities_decode(end($attribs[$j]), 'UTF-8');
  11523. }
  11524. }
  11525. }
  11526. }
  11527. return $return;
  11528. }
  11529. public static function element_implode($element)
  11530. {
  11531. $full = "<$element[tag]";
  11532. foreach ($element['attribs'] as $key => $value)
  11533. {
  11534. $key = strtolower($key);
  11535. $full .= " $key=\"" . htmlspecialchars($value['data']) . '"';
  11536. }
  11537. if ($element['self_closing'])
  11538. {
  11539. $full .= ' />';
  11540. }
  11541. else
  11542. {
  11543. $full .= ">$element[content]</$element[tag]>";
  11544. }
  11545. return $full;
  11546. }
  11547. public static function error($message, $level, $file, $line)
  11548. {
  11549. if ((ini_get('error_reporting') & $level) > 0)
  11550. {
  11551. switch ($level)
  11552. {
  11553. case E_USER_ERROR:
  11554. $note = 'PHP Error';
  11555. break;
  11556. case E_USER_WARNING:
  11557. $note = 'PHP Warning';
  11558. break;
  11559. case E_USER_NOTICE:
  11560. $note = 'PHP Notice';
  11561. break;
  11562. default:
  11563. $note = 'Unknown Error';
  11564. break;
  11565. }
  11566. $log_error = true;
  11567. if (!function_exists('error_log'))
  11568. {
  11569. $log_error = false;
  11570. }
  11571. $log_file = @ini_get('error_log');
  11572. if (!empty($log_file) && ('syslog' !== $log_file) && !@is_writable($log_file))
  11573. {
  11574. $log_error = false;
  11575. }
  11576. if ($log_error)
  11577. {
  11578. @error_log("$note: $message in $file on line $line", 0);
  11579. }
  11580. }
  11581. return $message;
  11582. }
  11583. public static function fix_protocol($url, $http = 1)
  11584. {
  11585. $url = SimplePie_Misc::normalize_url($url);
  11586. $parsed = SimplePie_Misc::parse_url($url);
  11587. if ($parsed['scheme'] !== '' && $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https')
  11588. {
  11589. return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['authority'], $parsed['path'], $parsed['query'], $parsed['fragment']), $http);
  11590. }
  11591. if ($parsed['scheme'] === '' && $parsed['authority'] === '' && !file_exists($url))
  11592. {
  11593. return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['path'], '', $parsed['query'], $parsed['fragment']), $http);
  11594. }
  11595. if ($http === 2 && $parsed['scheme'] !== '')
  11596. {
  11597. return "feed:$url";
  11598. }
  11599. elseif ($http === 3 && strtolower($parsed['scheme']) === 'http')
  11600. {
  11601. return substr_replace($url, 'podcast', 0, 4);
  11602. }
  11603. elseif ($http === 4 && strtolower($parsed['scheme']) === 'http')
  11604. {
  11605. return substr_replace($url, 'itpc', 0, 4);
  11606. }
  11607. else
  11608. {
  11609. return $url;
  11610. }
  11611. }
  11612. public static function parse_url($url)
  11613. {
  11614. $iri = new SimplePie_IRI($url);
  11615. return array(
  11616. 'scheme' => (string) $iri->scheme,
  11617. 'authority' => (string) $iri->authority,
  11618. 'path' => (string) $iri->path,
  11619. 'query' => (string) $iri->query,
  11620. 'fragment' => (string) $iri->fragment
  11621. );
  11622. }
  11623. public static function compress_parse_url($scheme = '', $authority = '', $path = '', $query = '', $fragment = '')
  11624. {
  11625. $iri = new SimplePie_IRI('');
  11626. $iri->scheme = $scheme;
  11627. $iri->authority = $authority;
  11628. $iri->path = $path;
  11629. $iri->query = $query;
  11630. $iri->fragment = $fragment;
  11631. return $iri->get_uri();
  11632. }
  11633. public static function normalize_url($url)
  11634. {
  11635. $iri = new SimplePie_IRI($url);
  11636. return $iri->get_uri();
  11637. }
  11638. public static function percent_encoding_normalization($match)
  11639. {
  11640. $integer = hexdec($match[1]);
  11641. if ($integer >= 0x41 && $integer <= 0x5A || $integer >= 0x61 && $integer <= 0x7A || $integer >= 0x30 && $integer <= 0x39 || $integer === 0x2D || $integer === 0x2E || $integer === 0x5F || $integer === 0x7E)
  11642. {
  11643. return chr($integer);
  11644. }
  11645. else
  11646. {
  11647. return strtoupper($match[0]);
  11648. }
  11649. }
  11650. /**
  11651. * Converts a Windows-1252 encoded string to a UTF-8 encoded string
  11652. *
  11653. * @static
  11654. * @param string $string Windows-1252 encoded string
  11655. * @return string UTF-8 encoded string
  11656. */
  11657. public static function windows_1252_to_utf8($string)
  11658. {
  11659. static $convert_table = array("\x80" => "\xE2\x82\xAC", "\x81" => "\xEF\xBF\xBD", "\x82" => "\xE2\x80\x9A", "\x83" => "\xC6\x92", "\x84" => "\xE2\x80\x9E", "\x85" => "\xE2\x80\xA6", "\x86" => "\xE2\x80\xA0", "\x87" => "\xE2\x80\xA1", "\x88" => "\xCB\x86", "\x89" => "\xE2\x80\xB0", "\x8A" => "\xC5\xA0", "\x8B" => "\xE2\x80\xB9", "\x8C" => "\xC5\x92", "\x8D" => "\xEF\xBF\xBD", "\x8E" => "\xC5\xBD", "\x8F" => "\xEF\xBF\xBD", "\x90" => "\xEF\xBF\xBD", "\x91" => "\xE2\x80\x98", "\x92" => "\xE2\x80\x99", "\x93" => "\xE2\x80\x9C", "\x94" => "\xE2\x80\x9D", "\x95" => "\xE2\x80\xA2", "\x96" => "\xE2\x80\x93", "\x97" => "\xE2\x80\x94", "\x98" => "\xCB\x9C", "\x99" => "\xE2\x84\xA2", "\x9A" => "\xC5\xA1", "\x9B" => "\xE2\x80\xBA", "\x9C" => "\xC5\x93", "\x9D" => "\xEF\xBF\xBD", "\x9E" => "\xC5\xBE", "\x9F" => "\xC5\xB8", "\xA0" => "\xC2\xA0", "\xA1" => "\xC2\xA1", "\xA2" => "\xC2\xA2", "\xA3" => "\xC2\xA3", "\xA4" => "\xC2\xA4", "\xA5" => "\xC2\xA5", "\xA6" => "\xC2\xA6", "\xA7" => "\xC2\xA7", "\xA8" => "\xC2\xA8", "\xA9" => "\xC2\xA9", "\xAA" => "\xC2\xAA", "\xAB" => "\xC2\xAB", "\xAC" => "\xC2\xAC", "\xAD" => "\xC2\xAD", "\xAE" => "\xC2\xAE", "\xAF" => "\xC2\xAF", "\xB0" => "\xC2\xB0", "\xB1" => "\xC2\xB1", "\xB2" => "\xC2\xB2", "\xB3" => "\xC2\xB3", "\xB4" => "\xC2\xB4", "\xB5" => "\xC2\xB5", "\xB6" => "\xC2\xB6", "\xB7" => "\xC2\xB7", "\xB8" => "\xC2\xB8", "\xB9" => "\xC2\xB9", "\xBA" => "\xC2\xBA", "\xBB" => "\xC2\xBB", "\xBC" => "\xC2\xBC", "\xBD" => "\xC2\xBD", "\xBE" => "\xC2\xBE", "\xBF" => "\xC2\xBF", "\xC0" => "\xC3\x80", "\xC1" => "\xC3\x81", "\xC2" => "\xC3\x82", "\xC3" => "\xC3\x83", "\xC4" => "\xC3\x84", "\xC5" => "\xC3\x85", "\xC6" => "\xC3\x86", "\xC7" => "\xC3\x87", "\xC8" => "\xC3\x88", "\xC9" => "\xC3\x89", "\xCA" => "\xC3\x8A", "\xCB" => "\xC3\x8B", "\xCC" => "\xC3\x8C", "\xCD" => "\xC3\x8D", "\xCE" => "\xC3\x8E", "\xCF" => "\xC3\x8F", "\xD0" => "\xC3\x90", "\xD1" => "\xC3\x91", "\xD2" => "\xC3\x92", "\xD3" => "\xC3\x93", "\xD4" => "\xC3\x94", "\xD5" => "\xC3\x95", "\xD6" => "\xC3\x96", "\xD7" => "\xC3\x97", "\xD8" => "\xC3\x98", "\xD9" => "\xC3\x99", "\xDA" => "\xC3\x9A", "\xDB" => "\xC3\x9B", "\xDC" => "\xC3\x9C", "\xDD" => "\xC3\x9D", "\xDE" => "\xC3\x9E", "\xDF" => "\xC3\x9F", "\xE0" => "\xC3\xA0", "\xE1" => "\xC3\xA1", "\xE2" => "\xC3\xA2", "\xE3" => "\xC3\xA3", "\xE4" => "\xC3\xA4", "\xE5" => "\xC3\xA5", "\xE6" => "\xC3\xA6", "\xE7" => "\xC3\xA7", "\xE8" => "\xC3\xA8", "\xE9" => "\xC3\xA9", "\xEA" => "\xC3\xAA", "\xEB" => "\xC3\xAB", "\xEC" => "\xC3\xAC", "\xED" => "\xC3\xAD", "\xEE" => "\xC3\xAE", "\xEF" => "\xC3\xAF", "\xF0" => "\xC3\xB0", "\xF1" => "\xC3\xB1", "\xF2" => "\xC3\xB2", "\xF3" => "\xC3\xB3", "\xF4" => "\xC3\xB4", "\xF5" => "\xC3\xB5", "\xF6" => "\xC3\xB6", "\xF7" => "\xC3\xB7", "\xF8" => "\xC3\xB8", "\xF9" => "\xC3\xB9", "\xFA" => "\xC3\xBA", "\xFB" => "\xC3\xBB", "\xFC" => "\xC3\xBC", "\xFD" => "\xC3\xBD", "\xFE" => "\xC3\xBE", "\xFF" => "\xC3\xBF");
  11660. return strtr($string, $convert_table);
  11661. }
  11662. /**
  11663. * Change a string from one encoding to another
  11664. *
  11665. * @param string $data Raw data in $input encoding
  11666. * @param string $input Encoding of $data
  11667. * @param string $output Encoding you want
  11668. * @return string|boolean False if we can't convert it
  11669. */
  11670. public static function change_encoding($data, $input, $output)
  11671. {
  11672. $input = SimplePie_Misc::encoding($input);
  11673. $output = SimplePie_Misc::encoding($output);
  11674. // We fail to fail on non US-ASCII bytes
  11675. if ($input === 'US-ASCII')
  11676. {
  11677. static $non_ascii_octects = '';
  11678. if (!$non_ascii_octects)
  11679. {
  11680. for ($i = 0x80; $i <= 0xFF; $i++)
  11681. {
  11682. $non_ascii_octects .= chr($i);
  11683. }
  11684. }
  11685. $data = substr($data, 0, strcspn($data, $non_ascii_octects));
  11686. }
  11687. // This is first, as behaviour of this is completely predictable
  11688. if ($input === 'windows-1252' && $output === 'UTF-8')
  11689. {
  11690. return SimplePie_Misc::windows_1252_to_utf8($data);
  11691. }
  11692. // This is second, as behaviour of this varies only with PHP version (the middle part of this expression checks the encoding is supported).
  11693. elseif (function_exists('mb_convert_encoding') && ($return = SimplePie_Misc::change_encoding_mbstring($data, $input, $output)))
  11694. {
  11695. return $return;
  11696. }
  11697. // This is last, as behaviour of this varies with OS userland and PHP version
  11698. elseif (function_exists('iconv') && ($return = SimplePie_Misc::change_encoding_iconv($data, $input, $output)))
  11699. {
  11700. return $return;
  11701. }
  11702. // If we can't do anything, just fail
  11703. else
  11704. {
  11705. return false;
  11706. }
  11707. }
  11708. protected static function change_encoding_mbstring($data, $input, $output)
  11709. {
  11710. if ($input === 'windows-949')
  11711. {
  11712. $input = 'EUC-KR';
  11713. }
  11714. if ($output === 'windows-949')
  11715. {
  11716. $output = 'EUC-KR';
  11717. }
  11718. if ($input === 'Windows-31J')
  11719. {
  11720. $input = 'SJIS';
  11721. }
  11722. if ($output === 'Windows-31J')
  11723. {
  11724. $output = 'SJIS';
  11725. }
  11726. // Check that the encoding is supported
  11727. if (@mb_convert_encoding("\x80", 'UTF-16BE', $input) === "\x00\x80")
  11728. {
  11729. return false;
  11730. }
  11731. if (!in_array($input, mb_list_encodings()))
  11732. {
  11733. return false;
  11734. }
  11735. // Let's do some conversion
  11736. if ($return = @mb_convert_encoding($data, $output, $input))
  11737. {
  11738. return $return;
  11739. }
  11740. return false;
  11741. }
  11742. protected static function change_encoding_iconv($data, $input, $output)
  11743. {
  11744. return @iconv($input, $output, $data);
  11745. }
  11746. /**
  11747. * Normalize an encoding name
  11748. *
  11749. * This is automatically generated by create.php
  11750. *
  11751. * To generate it, run `php create.php` on the command line, and copy the
  11752. * output to replace this function.
  11753. *
  11754. * @param string $charset Character set to standardise
  11755. * @return string Standardised name
  11756. */
  11757. public static function encoding($charset)
  11758. {
  11759. // Normalization from UTS #22
  11760. switch (strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\1', $charset)))
  11761. {
  11762. case 'adobestandardencoding':
  11763. case 'csadobestandardencoding':
  11764. return 'Adobe-Standard-Encoding';
  11765. case 'adobesymbolencoding':
  11766. case 'cshppsmath':
  11767. return 'Adobe-Symbol-Encoding';
  11768. case 'ami1251':
  11769. case 'amiga1251':
  11770. return 'Amiga-1251';
  11771. case 'ansix31101983':
  11772. case 'csat5001983':
  11773. case 'csiso99naplps':
  11774. case 'isoir99':
  11775. case 'naplps':
  11776. return 'ANSI_X3.110-1983';
  11777. case 'arabic7':
  11778. case 'asmo449':
  11779. case 'csiso89asmo449':
  11780. case 'iso9036':
  11781. case 'isoir89':
  11782. return 'ASMO_449';
  11783. case 'big5':
  11784. case 'csbig5':
  11785. return 'Big5';
  11786. case 'big5hkscs':
  11787. return 'Big5-HKSCS';
  11788. case 'bocu1':
  11789. case 'csbocu1':
  11790. return 'BOCU-1';
  11791. case 'brf':
  11792. case 'csbrf':
  11793. return 'BRF';
  11794. case 'bs4730':
  11795. case 'csiso4unitedkingdom':
  11796. case 'gb':
  11797. case 'iso646gb':
  11798. case 'isoir4':
  11799. case 'uk':
  11800. return 'BS_4730';
  11801. case 'bsviewdata':
  11802. case 'csiso47bsviewdata':
  11803. case 'isoir47':
  11804. return 'BS_viewdata';
  11805. case 'cesu8':
  11806. case 'cscesu8':
  11807. return 'CESU-8';
  11808. case 'ca':
  11809. case 'csa71':
  11810. case 'csaz243419851':
  11811. case 'csiso121canadian1':
  11812. case 'iso646ca':
  11813. case 'isoir121':
  11814. return 'CSA_Z243.4-1985-1';
  11815. case 'csa72':
  11816. case 'csaz243419852':
  11817. case 'csiso122canadian2':
  11818. case 'iso646ca2':
  11819. case 'isoir122':
  11820. return 'CSA_Z243.4-1985-2';
  11821. case 'csaz24341985gr':
  11822. case 'csiso123csaz24341985gr':
  11823. case 'isoir123':
  11824. return 'CSA_Z243.4-1985-gr';
  11825. case 'csiso139csn369103':
  11826. case 'csn369103':
  11827. case 'isoir139':
  11828. return 'CSN_369103';
  11829. case 'csdecmcs':
  11830. case 'dec':
  11831. case 'decmcs':
  11832. return 'DEC-MCS';
  11833. case 'csiso21german':
  11834. case 'de':
  11835. case 'din66003':
  11836. case 'iso646de':
  11837. case 'isoir21':
  11838. return 'DIN_66003';
  11839. case 'csdkus':
  11840. case 'dkus':
  11841. return 'dk-us';
  11842. case 'csiso646danish':
  11843. case 'dk':
  11844. case 'ds2089':
  11845. case 'iso646dk':
  11846. return 'DS_2089';
  11847. case 'csibmebcdicatde':
  11848. case 'ebcdicatde':
  11849. return 'EBCDIC-AT-DE';
  11850. case 'csebcdicatdea':
  11851. case 'ebcdicatdea':
  11852. return 'EBCDIC-AT-DE-A';
  11853. case 'csebcdiccafr':
  11854. case 'ebcdiccafr':
  11855. return 'EBCDIC-CA-FR';
  11856. case 'csebcdicdkno':
  11857. case 'ebcdicdkno':
  11858. return 'EBCDIC-DK-NO';
  11859. case 'csebcdicdknoa':
  11860. case 'ebcdicdknoa':
  11861. return 'EBCDIC-DK-NO-A';
  11862. case 'csebcdices':
  11863. case 'ebcdices':
  11864. return 'EBCDIC-ES';
  11865. case 'csebcdicesa':
  11866. case 'ebcdicesa':
  11867. return 'EBCDIC-ES-A';
  11868. case 'csebcdicess':
  11869. case 'ebcdicess':
  11870. return 'EBCDIC-ES-S';
  11871. case 'csebcdicfise':
  11872. case 'ebcdicfise':
  11873. return 'EBCDIC-FI-SE';
  11874. case 'csebcdicfisea':
  11875. case 'ebcdicfisea':
  11876. return 'EBCDIC-FI-SE-A';
  11877. case 'csebcdicfr':
  11878. case 'ebcdicfr':
  11879. return 'EBCDIC-FR';
  11880. case 'csebcdicit':
  11881. case 'ebcdicit':
  11882. return 'EBCDIC-IT';
  11883. case 'csebcdicpt':
  11884. case 'ebcdicpt':
  11885. return 'EBCDIC-PT';
  11886. case 'csebcdicuk':
  11887. case 'ebcdicuk':
  11888. return 'EBCDIC-UK';
  11889. case 'csebcdicus':
  11890. case 'ebcdicus':
  11891. return 'EBCDIC-US';
  11892. case 'csiso111ecmacyrillic':
  11893. case 'ecmacyrillic':
  11894. case 'isoir111':
  11895. case 'koi8e':
  11896. return 'ECMA-cyrillic';
  11897. case 'csiso17spanish':
  11898. case 'es':
  11899. case 'iso646es':
  11900. case 'isoir17':
  11901. return 'ES';
  11902. case 'csiso85spanish2':
  11903. case 'es2':
  11904. case 'iso646es2':
  11905. case 'isoir85':
  11906. return 'ES2';
  11907. case 'cseucpkdfmtjapanese':
  11908. case 'eucjp':
  11909. case 'extendedunixcodepackedformatforjapanese':
  11910. return 'EUC-JP';
  11911. case 'cseucfixwidjapanese':
  11912. case 'extendedunixcodefixedwidthforjapanese':
  11913. return 'Extended_UNIX_Code_Fixed_Width_for_Japanese';
  11914. case 'gb18030':
  11915. return 'GB18030';
  11916. case 'chinese':
  11917. case 'cp936':
  11918. case 'csgb2312':
  11919. case 'csiso58gb231280':
  11920. case 'gb2312':
  11921. case 'gb231280':
  11922. case 'gbk':
  11923. case 'isoir58':
  11924. case 'ms936':
  11925. case 'windows936':
  11926. return 'GBK';
  11927. case 'cn':
  11928. case 'csiso57gb1988':
  11929. case 'gb198880':
  11930. case 'iso646cn':
  11931. case 'isoir57':
  11932. return 'GB_1988-80';
  11933. case 'csiso153gost1976874':
  11934. case 'gost1976874':
  11935. case 'isoir153':
  11936. case 'stsev35888':
  11937. return 'GOST_19768-74';
  11938. case 'csiso150':
  11939. case 'csiso150greekccitt':
  11940. case 'greekccitt':
  11941. case 'isoir150':
  11942. return 'greek-ccitt';
  11943. case 'csiso88greek7':
  11944. case 'greek7':
  11945. case 'isoir88':
  11946. return 'greek7';
  11947. case 'csiso18greek7old':
  11948. case 'greek7old':
  11949. case 'isoir18':
  11950. return 'greek7-old';
  11951. case 'cshpdesktop':
  11952. case 'hpdesktop':
  11953. return 'HP-DeskTop';
  11954. case 'cshplegal':
  11955. case 'hplegal':
  11956. return 'HP-Legal';
  11957. case 'cshpmath8':
  11958. case 'hpmath8':
  11959. return 'HP-Math8';
  11960. case 'cshppifont':
  11961. case 'hppifont':
  11962. return 'HP-Pi-font';
  11963. case 'cshproman8':
  11964. case 'hproman8':
  11965. case 'r8':
  11966. case 'roman8':
  11967. return 'hp-roman8';
  11968. case 'hzgb2312':
  11969. return 'HZ-GB-2312';
  11970. case 'csibmsymbols':
  11971. case 'ibmsymbols':
  11972. return 'IBM-Symbols';
  11973. case 'csibmthai':
  11974. case 'ibmthai':
  11975. return 'IBM-Thai';
  11976. case 'cp37':
  11977. case 'csibm37':
  11978. case 'ebcdiccpca':
  11979. case 'ebcdiccpnl':
  11980. case 'ebcdiccpus':
  11981. case 'ebcdiccpwt':
  11982. case 'ibm37':
  11983. return 'IBM037';
  11984. case 'cp38':
  11985. case 'csibm38':
  11986. case 'ebcdicint':
  11987. case 'ibm38':
  11988. return 'IBM038';
  11989. case 'cp273':
  11990. case 'csibm273':
  11991. case 'ibm273':
  11992. return 'IBM273';
  11993. case 'cp274':
  11994. case 'csibm274':
  11995. case 'ebcdicbe':
  11996. case 'ibm274':
  11997. return 'IBM274';
  11998. case 'cp275':
  11999. case 'csibm275':
  12000. case 'ebcdicbr':
  12001. case 'ibm275':
  12002. return 'IBM275';
  12003. case 'csibm277':
  12004. case 'ebcdiccpdk':
  12005. case 'ebcdiccpno':
  12006. case 'ibm277':
  12007. return 'IBM277';
  12008. case 'cp278':
  12009. case 'csibm278':
  12010. case 'ebcdiccpfi':
  12011. case 'ebcdiccpse':
  12012. case 'ibm278':
  12013. return 'IBM278';
  12014. case 'cp280':
  12015. case 'csibm280':
  12016. case 'ebcdiccpit':
  12017. case 'ibm280':
  12018. return 'IBM280';
  12019. case 'cp281':
  12020. case 'csibm281':
  12021. case 'ebcdicjpe':
  12022. case 'ibm281':
  12023. return 'IBM281';
  12024. case 'cp284':
  12025. case 'csibm284':
  12026. case 'ebcdiccpes':
  12027. case 'ibm284':
  12028. return 'IBM284';
  12029. case 'cp285':
  12030. case 'csibm285':
  12031. case 'ebcdiccpgb':
  12032. case 'ibm285':
  12033. return 'IBM285';
  12034. case 'cp290':
  12035. case 'csibm290':
  12036. case 'ebcdicjpkana':
  12037. case 'ibm290':
  12038. return 'IBM290';
  12039. case 'cp297':
  12040. case 'csibm297':
  12041. case 'ebcdiccpfr':
  12042. case 'ibm297':
  12043. return 'IBM297';
  12044. case 'cp420':
  12045. case 'csibm420':
  12046. case 'ebcdiccpar1':
  12047. case 'ibm420':
  12048. return 'IBM420';
  12049. case 'cp423':
  12050. case 'csibm423':
  12051. case 'ebcdiccpgr':
  12052. case 'ibm423':
  12053. return 'IBM423';
  12054. case 'cp424':
  12055. case 'csibm424':
  12056. case 'ebcdiccphe':
  12057. case 'ibm424':
  12058. return 'IBM424';
  12059. case '437':
  12060. case 'cp437':
  12061. case 'cspc8codepage437':
  12062. case 'ibm437':
  12063. return 'IBM437';
  12064. case 'cp500':
  12065. case 'csibm500':
  12066. case 'ebcdiccpbe':
  12067. case 'ebcdiccpch':
  12068. case 'ibm500':
  12069. return 'IBM500';
  12070. case 'cp775':
  12071. case 'cspc775baltic':
  12072. case 'ibm775':
  12073. return 'IBM775';
  12074. case '850':
  12075. case 'cp850':
  12076. case 'cspc850multilingual':
  12077. case 'ibm850':
  12078. return 'IBM850';
  12079. case '851':
  12080. case 'cp851':
  12081. case 'csibm851':
  12082. case 'ibm851':
  12083. return 'IBM851';
  12084. case '852':
  12085. case 'cp852':
  12086. case 'cspcp852':
  12087. case 'ibm852':
  12088. return 'IBM852';
  12089. case '855':
  12090. case 'cp855':
  12091. case 'csibm855':
  12092. case 'ibm855':
  12093. return 'IBM855';
  12094. case '857':
  12095. case 'cp857':
  12096. case 'csibm857':
  12097. case 'ibm857':
  12098. return 'IBM857';
  12099. case 'ccsid858':
  12100. case 'cp858':
  12101. case 'ibm858':
  12102. case 'pcmultilingual850euro':
  12103. return 'IBM00858';
  12104. case '860':
  12105. case 'cp860':
  12106. case 'csibm860':
  12107. case 'ibm860':
  12108. return 'IBM860';
  12109. case '861':
  12110. case 'cp861':
  12111. case 'cpis':
  12112. case 'csibm861':
  12113. case 'ibm861':
  12114. return 'IBM861';
  12115. case '862':
  12116. case 'cp862':
  12117. case 'cspc862latinhebrew':
  12118. case 'ibm862':
  12119. return 'IBM862';
  12120. case '863':
  12121. case 'cp863':
  12122. case 'csibm863':
  12123. case 'ibm863':
  12124. return 'IBM863';
  12125. case 'cp864':
  12126. case 'csibm864':
  12127. case 'ibm864':
  12128. return 'IBM864';
  12129. case '865':
  12130. case 'cp865':
  12131. case 'csibm865':
  12132. case 'ibm865':
  12133. return 'IBM865';
  12134. case '866':
  12135. case 'cp866':
  12136. case 'csibm866':
  12137. case 'ibm866':
  12138. return 'IBM866';
  12139. case 'cp868':
  12140. case 'cpar':
  12141. case 'csibm868':
  12142. case 'ibm868':
  12143. return 'IBM868';
  12144. case '869':
  12145. case 'cp869':
  12146. case 'cpgr':
  12147. case 'csibm869':
  12148. case 'ibm869':
  12149. return 'IBM869';
  12150. case 'cp870':
  12151. case 'csibm870':
  12152. case 'ebcdiccproece':
  12153. case 'ebcdiccpyu':
  12154. case 'ibm870':
  12155. return 'IBM870';
  12156. case 'cp871':
  12157. case 'csibm871':
  12158. case 'ebcdiccpis':
  12159. case 'ibm871':
  12160. return 'IBM871';
  12161. case 'cp880':
  12162. case 'csibm880':
  12163. case 'ebcdiccyrillic':
  12164. case 'ibm880':
  12165. return 'IBM880';
  12166. case 'cp891':
  12167. case 'csibm891':
  12168. case 'ibm891':
  12169. return 'IBM891';
  12170. case 'cp903':
  12171. case 'csibm903':
  12172. case 'ibm903':
  12173. return 'IBM903';
  12174. case '904':
  12175. case 'cp904':
  12176. case 'csibbm904':
  12177. case 'ibm904':
  12178. return 'IBM904';
  12179. case 'cp905':
  12180. case 'csibm905':
  12181. case 'ebcdiccptr':
  12182. case 'ibm905':
  12183. return 'IBM905';
  12184. case 'cp918':
  12185. case 'csibm918':
  12186. case 'ebcdiccpar2':
  12187. case 'ibm918':
  12188. return 'IBM918';
  12189. case 'ccsid924':
  12190. case 'cp924':
  12191. case 'ebcdiclatin9euro':
  12192. case 'ibm924':
  12193. return 'IBM00924';
  12194. case 'cp1026':
  12195. case 'csibm1026':
  12196. case 'ibm1026':
  12197. return 'IBM1026';
  12198. case 'ibm1047':
  12199. return 'IBM1047';
  12200. case 'ccsid1140':
  12201. case 'cp1140':
  12202. case 'ebcdicus37euro':
  12203. case 'ibm1140':
  12204. return 'IBM01140';
  12205. case 'ccsid1141':
  12206. case 'cp1141':
  12207. case 'ebcdicde273euro':
  12208. case 'ibm1141':
  12209. return 'IBM01141';
  12210. case 'ccsid1142':
  12211. case 'cp1142':
  12212. case 'ebcdicdk277euro':
  12213. case 'ebcdicno277euro':
  12214. case 'ibm1142':
  12215. return 'IBM01142';
  12216. case 'ccsid1143':
  12217. case 'cp1143':
  12218. case 'ebcdicfi278euro':
  12219. case 'ebcdicse278euro':
  12220. case 'ibm1143':
  12221. return 'IBM01143';
  12222. case 'ccsid1144':
  12223. case 'cp1144':
  12224. case 'ebcdicit280euro':
  12225. case 'ibm1144':
  12226. return 'IBM01144';
  12227. case 'ccsid1145':
  12228. case 'cp1145':
  12229. case 'ebcdices284euro':
  12230. case 'ibm1145':
  12231. return 'IBM01145';
  12232. case 'ccsid1146':
  12233. case 'cp1146':
  12234. case 'ebcdicgb285euro':
  12235. case 'ibm1146':
  12236. return 'IBM01146';
  12237. case 'ccsid1147':
  12238. case 'cp1147':
  12239. case 'ebcdicfr297euro':
  12240. case 'ibm1147':
  12241. return 'IBM01147';
  12242. case 'ccsid1148':
  12243. case 'cp1148':
  12244. case 'ebcdicinternational500euro':
  12245. case 'ibm1148':
  12246. return 'IBM01148';
  12247. case 'ccsid1149':
  12248. case 'cp1149':
  12249. case 'ebcdicis871euro':
  12250. case 'ibm1149':
  12251. return 'IBM01149';
  12252. case 'csiso143iecp271':
  12253. case 'iecp271':
  12254. case 'isoir143':
  12255. return 'IEC_P27-1';
  12256. case 'csiso49inis':
  12257. case 'inis':
  12258. case 'isoir49':
  12259. return 'INIS';
  12260. case 'csiso50inis8':
  12261. case 'inis8':
  12262. case 'isoir50':
  12263. return 'INIS-8';
  12264. case 'csiso51iniscyrillic':
  12265. case 'iniscyrillic':
  12266. case 'isoir51':
  12267. return 'INIS-cyrillic';
  12268. case 'csinvariant':
  12269. case 'invariant':
  12270. return 'INVARIANT';
  12271. case 'iso2022cn':
  12272. return 'ISO-2022-CN';
  12273. case 'iso2022cnext':
  12274. return 'ISO-2022-CN-EXT';
  12275. case 'csiso2022jp':
  12276. case 'iso2022jp':
  12277. return 'ISO-2022-JP';
  12278. case 'csiso2022jp2':
  12279. case 'iso2022jp2':
  12280. return 'ISO-2022-JP-2';
  12281. case 'csiso2022kr':
  12282. case 'iso2022kr':
  12283. return 'ISO-2022-KR';
  12284. case 'cswindows30latin1':
  12285. case 'iso88591windows30latin1':
  12286. return 'ISO-8859-1-Windows-3.0-Latin-1';
  12287. case 'cswindows31latin1':
  12288. case 'iso88591windows31latin1':
  12289. return 'ISO-8859-1-Windows-3.1-Latin-1';
  12290. case 'csisolatin2':
  12291. case 'iso88592':
  12292. case 'iso885921987':
  12293. case 'isoir101':
  12294. case 'l2':
  12295. case 'latin2':
  12296. return 'ISO-8859-2';
  12297. case 'cswindows31latin2':
  12298. case 'iso88592windowslatin2':
  12299. return 'ISO-8859-2-Windows-Latin-2';
  12300. case 'csisolatin3':
  12301. case 'iso88593':
  12302. case 'iso885931988':
  12303. case 'isoir109':
  12304. case 'l3':
  12305. case 'latin3':
  12306. return 'ISO-8859-3';
  12307. case 'csisolatin4':
  12308. case 'iso88594':
  12309. case 'iso885941988':
  12310. case 'isoir110':
  12311. case 'l4':
  12312. case 'latin4':
  12313. return 'ISO-8859-4';
  12314. case 'csisolatincyrillic':
  12315. case 'cyrillic':
  12316. case 'iso88595':
  12317. case 'iso885951988':
  12318. case 'isoir144':
  12319. return 'ISO-8859-5';
  12320. case 'arabic':
  12321. case 'asmo708':
  12322. case 'csisolatinarabic':
  12323. case 'ecma114':
  12324. case 'iso88596':
  12325. case 'iso885961987':
  12326. case 'isoir127':
  12327. return 'ISO-8859-6';
  12328. case 'csiso88596e':
  12329. case 'iso88596e':
  12330. return 'ISO-8859-6-E';
  12331. case 'csiso88596i':
  12332. case 'iso88596i':
  12333. return 'ISO-8859-6-I';
  12334. case 'csisolatingreek':
  12335. case 'ecma118':
  12336. case 'elot928':
  12337. case 'greek':
  12338. case 'greek8':
  12339. case 'iso88597':
  12340. case 'iso885971987':
  12341. case 'isoir126':
  12342. return 'ISO-8859-7';
  12343. case 'csisolatinhebrew':
  12344. case 'hebrew':
  12345. case 'iso88598':
  12346. case 'iso885981988':
  12347. case 'isoir138':
  12348. return 'ISO-8859-8';
  12349. case 'csiso88598e':
  12350. case 'iso88598e':
  12351. return 'ISO-8859-8-E';
  12352. case 'csiso88598i':
  12353. case 'iso88598i':
  12354. return 'ISO-8859-8-I';
  12355. case 'cswindows31latin5':
  12356. case 'iso88599windowslatin5':
  12357. return 'ISO-8859-9-Windows-Latin-5';
  12358. case 'csisolatin6':
  12359. case 'iso885910':
  12360. case 'iso8859101992':
  12361. case 'isoir157':
  12362. case 'l6':
  12363. case 'latin6':
  12364. return 'ISO-8859-10';
  12365. case 'iso885913':
  12366. return 'ISO-8859-13';
  12367. case 'iso885914':
  12368. case 'iso8859141998':
  12369. case 'isoceltic':
  12370. case 'isoir199':
  12371. case 'l8':
  12372. case 'latin8':
  12373. return 'ISO-8859-14';
  12374. case 'iso885915':
  12375. case 'latin9':
  12376. return 'ISO-8859-15';
  12377. case 'iso885916':
  12378. case 'iso8859162001':
  12379. case 'isoir226':
  12380. case 'l10':
  12381. case 'latin10':
  12382. return 'ISO-8859-16';
  12383. case 'iso10646j1':
  12384. return 'ISO-10646-J-1';
  12385. case 'csunicode':
  12386. case 'iso10646ucs2':
  12387. return 'ISO-10646-UCS-2';
  12388. case 'csucs4':
  12389. case 'iso10646ucs4':
  12390. return 'ISO-10646-UCS-4';
  12391. case 'csunicodeascii':
  12392. case 'iso10646ucsbasic':
  12393. return 'ISO-10646-UCS-Basic';
  12394. case 'csunicodelatin1':
  12395. case 'iso10646':
  12396. case 'iso10646unicodelatin1':
  12397. return 'ISO-10646-Unicode-Latin1';
  12398. case 'csiso10646utf1':
  12399. case 'iso10646utf1':
  12400. return 'ISO-10646-UTF-1';
  12401. case 'csiso115481':
  12402. case 'iso115481':
  12403. case 'isotr115481':
  12404. return 'ISO-11548-1';
  12405. case 'csiso90':
  12406. case 'isoir90':
  12407. return 'iso-ir-90';
  12408. case 'csunicodeibm1261':
  12409. case 'isounicodeibm1261':
  12410. return 'ISO-Unicode-IBM-1261';
  12411. case 'csunicodeibm1264':
  12412. case 'isounicodeibm1264':
  12413. return 'ISO-Unicode-IBM-1264';
  12414. case 'csunicodeibm1265':
  12415. case 'isounicodeibm1265':
  12416. return 'ISO-Unicode-IBM-1265';
  12417. case 'csunicodeibm1268':
  12418. case 'isounicodeibm1268':
  12419. return 'ISO-Unicode-IBM-1268';
  12420. case 'csunicodeibm1276':
  12421. case 'isounicodeibm1276':
  12422. return 'ISO-Unicode-IBM-1276';
  12423. case 'csiso646basic1983':
  12424. case 'iso646basic1983':
  12425. case 'ref':
  12426. return 'ISO_646.basic:1983';
  12427. case 'csiso2intlrefversion':
  12428. case 'irv':
  12429. case 'iso646irv1983':
  12430. case 'isoir2':
  12431. return 'ISO_646.irv:1983';
  12432. case 'csiso2033':
  12433. case 'e13b':
  12434. case 'iso20331983':
  12435. case 'isoir98':
  12436. return 'ISO_2033-1983';
  12437. case 'csiso5427cyrillic':
  12438. case 'iso5427':
  12439. case 'isoir37':
  12440. return 'ISO_5427';
  12441. case 'iso5427cyrillic1981':
  12442. case 'iso54271981':
  12443. case 'isoir54':
  12444. return 'ISO_5427:1981';
  12445. case 'csiso5428greek':
  12446. case 'iso54281980':
  12447. case 'isoir55':
  12448. return 'ISO_5428:1980';
  12449. case 'csiso6937add':
  12450. case 'iso6937225':
  12451. case 'isoir152':
  12452. return 'ISO_6937-2-25';
  12453. case 'csisotextcomm':
  12454. case 'iso69372add':
  12455. case 'isoir142':
  12456. return 'ISO_6937-2-add';
  12457. case 'csiso8859supp':
  12458. case 'iso8859supp':
  12459. case 'isoir154':
  12460. case 'latin125':
  12461. return 'ISO_8859-supp';
  12462. case 'csiso10367box':
  12463. case 'iso10367box':
  12464. case 'isoir155':
  12465. return 'ISO_10367-box';
  12466. case 'csiso15italian':
  12467. case 'iso646it':
  12468. case 'isoir15':
  12469. case 'it':
  12470. return 'IT';
  12471. case 'csiso13jisc6220jp':
  12472. case 'isoir13':
  12473. case 'jisc62201969':
  12474. case 'jisc62201969jp':
  12475. case 'katakana':
  12476. case 'x2017':
  12477. return 'JIS_C6220-1969-jp';
  12478. case 'csiso14jisc6220ro':
  12479. case 'iso646jp':
  12480. case 'isoir14':
  12481. case 'jisc62201969ro':
  12482. case 'jp':
  12483. return 'JIS_C6220-1969-ro';
  12484. case 'csiso42jisc62261978':
  12485. case 'isoir42':
  12486. case 'jisc62261978':
  12487. return 'JIS_C6226-1978';
  12488. case 'csiso87jisx208':
  12489. case 'isoir87':
  12490. case 'jisc62261983':
  12491. case 'jisx2081983':
  12492. case 'x208':
  12493. return 'JIS_C6226-1983';
  12494. case 'csiso91jisc62291984a':
  12495. case 'isoir91':
  12496. case 'jisc62291984a':
  12497. case 'jpocra':
  12498. return 'JIS_C6229-1984-a';
  12499. case 'csiso92jisc62991984b':
  12500. case 'iso646jpocrb':
  12501. case 'isoir92':
  12502. case 'jisc62291984b':
  12503. case 'jpocrb':
  12504. return 'JIS_C6229-1984-b';
  12505. case 'csiso93jis62291984badd':
  12506. case 'isoir93':
  12507. case 'jisc62291984badd':
  12508. case 'jpocrbadd':
  12509. return 'JIS_C6229-1984-b-add';
  12510. case 'csiso94jis62291984hand':
  12511. case 'isoir94':
  12512. case 'jisc62291984hand':
  12513. case 'jpocrhand':
  12514. return 'JIS_C6229-1984-hand';
  12515. case 'csiso95jis62291984handadd':
  12516. case 'isoir95':
  12517. case 'jisc62291984handadd':
  12518. case 'jpocrhandadd':
  12519. return 'JIS_C6229-1984-hand-add';
  12520. case 'csiso96jisc62291984kana':
  12521. case 'isoir96':
  12522. case 'jisc62291984kana':
  12523. return 'JIS_C6229-1984-kana';
  12524. case 'csjisencoding':
  12525. case 'jisencoding':
  12526. return 'JIS_Encoding';
  12527. case 'cshalfwidthkatakana':
  12528. case 'jisx201':
  12529. case 'x201':
  12530. return 'JIS_X0201';
  12531. case 'csiso159jisx2121990':
  12532. case 'isoir159':
  12533. case 'jisx2121990':
  12534. case 'x212':
  12535. return 'JIS_X0212-1990';
  12536. case 'csiso141jusib1002':
  12537. case 'iso646yu':
  12538. case 'isoir141':
  12539. case 'js':
  12540. case 'jusib1002':
  12541. case 'yu':
  12542. return 'JUS_I.B1.002';
  12543. case 'csiso147macedonian':
  12544. case 'isoir147':
  12545. case 'jusib1003mac':
  12546. case 'macedonian':
  12547. return 'JUS_I.B1.003-mac';
  12548. case 'csiso146serbian':
  12549. case 'isoir146':
  12550. case 'jusib1003serb':
  12551. case 'serbian':
  12552. return 'JUS_I.B1.003-serb';
  12553. case 'koi7switched':
  12554. return 'KOI7-switched';
  12555. case 'cskoi8r':
  12556. case 'koi8r':
  12557. return 'KOI8-R';
  12558. case 'koi8u':
  12559. return 'KOI8-U';
  12560. case 'csksc5636':
  12561. case 'iso646kr':
  12562. case 'ksc5636':
  12563. return 'KSC5636';
  12564. case 'cskz1048':
  12565. case 'kz1048':
  12566. case 'rk1048':
  12567. case 'strk10482002':
  12568. return 'KZ-1048';
  12569. case 'csiso19latingreek':
  12570. case 'isoir19':
  12571. case 'latingreek':
  12572. return 'latin-greek';
  12573. case 'csiso27latingreek1':
  12574. case 'isoir27':
  12575. case 'latingreek1':
  12576. return 'Latin-greek-1';
  12577. case 'csiso158lap':
  12578. case 'isoir158':
  12579. case 'lap':
  12580. case 'latinlap':
  12581. return 'latin-lap';
  12582. case 'csmacintosh':
  12583. case 'mac':
  12584. case 'macintosh':
  12585. return 'macintosh';
  12586. case 'csmicrosoftpublishing':
  12587. case 'microsoftpublishing':
  12588. return 'Microsoft-Publishing';
  12589. case 'csmnem':
  12590. case 'mnem':
  12591. return 'MNEM';
  12592. case 'csmnemonic':
  12593. case 'mnemonic':
  12594. return 'MNEMONIC';
  12595. case 'csiso86hungarian':
  12596. case 'hu':
  12597. case 'iso646hu':
  12598. case 'isoir86':
  12599. case 'msz77953':
  12600. return 'MSZ_7795.3';
  12601. case 'csnatsdano':
  12602. case 'isoir91':
  12603. case 'natsdano':
  12604. return 'NATS-DANO';
  12605. case 'csnatsdanoadd':
  12606. case 'isoir92':
  12607. case 'natsdanoadd':
  12608. return 'NATS-DANO-ADD';
  12609. case 'csnatssefi':
  12610. case 'isoir81':
  12611. case 'natssefi':
  12612. return 'NATS-SEFI';
  12613. case 'csnatssefiadd':
  12614. case 'isoir82':
  12615. case 'natssefiadd':
  12616. return 'NATS-SEFI-ADD';
  12617. case 'csiso151cuba':
  12618. case 'cuba':
  12619. case 'iso646cu':
  12620. case 'isoir151':
  12621. case 'ncnc1081':
  12622. return 'NC_NC00-10:81';
  12623. case 'csiso69french':
  12624. case 'fr':
  12625. case 'iso646fr':
  12626. case 'isoir69':
  12627. case 'nfz62010':
  12628. return 'NF_Z_62-010';
  12629. case 'csiso25french':
  12630. case 'iso646fr1':
  12631. case 'isoir25':
  12632. case 'nfz620101973':
  12633. return 'NF_Z_62-010_(1973)';
  12634. case 'csiso60danishnorwegian':
  12635. case 'csiso60norwegian1':
  12636. case 'iso646no':
  12637. case 'isoir60':
  12638. case 'no':
  12639. case 'ns45511':
  12640. return 'NS_4551-1';
  12641. case 'csiso61norwegian2':
  12642. case 'iso646no2':
  12643. case 'isoir61':
  12644. case 'no2':
  12645. case 'ns45512':
  12646. return 'NS_4551-2';
  12647. case 'osdebcdicdf3irv':
  12648. return 'OSD_EBCDIC_DF03_IRV';
  12649. case 'osdebcdicdf41':
  12650. return 'OSD_EBCDIC_DF04_1';
  12651. case 'osdebcdicdf415':
  12652. return 'OSD_EBCDIC_DF04_15';
  12653. case 'cspc8danishnorwegian':
  12654. case 'pc8danishnorwegian':
  12655. return 'PC8-Danish-Norwegian';
  12656. case 'cspc8turkish':
  12657. case 'pc8turkish':
  12658. return 'PC8-Turkish';
  12659. case 'csiso16portuguese':
  12660. case 'iso646pt':
  12661. case 'isoir16':
  12662. case 'pt':
  12663. return 'PT';
  12664. case 'csiso84portuguese2':
  12665. case 'iso646pt2':
  12666. case 'isoir84':
  12667. case 'pt2':
  12668. return 'PT2';
  12669. case 'cp154':
  12670. case 'csptcp154':
  12671. case 'cyrillicasian':
  12672. case 'pt154':
  12673. case 'ptcp154':
  12674. return 'PTCP154';
  12675. case 'scsu':
  12676. return 'SCSU';
  12677. case 'csiso10swedish':
  12678. case 'fi':
  12679. case 'iso646fi':
  12680. case 'iso646se':
  12681. case 'isoir10':
  12682. case 'se':
  12683. case 'sen850200b':
  12684. return 'SEN_850200_B';
  12685. case 'csiso11swedishfornames':
  12686. case 'iso646se2':
  12687. case 'isoir11':
  12688. case 'se2':
  12689. case 'sen850200c':
  12690. return 'SEN_850200_C';
  12691. case 'csiso102t617bit':
  12692. case 'isoir102':
  12693. case 't617bit':
  12694. return 'T.61-7bit';
  12695. case 'csiso103t618bit':
  12696. case 'isoir103':
  12697. case 't61':
  12698. case 't618bit':
  12699. return 'T.61-8bit';
  12700. case 'csiso128t101g2':
  12701. case 'isoir128':
  12702. case 't101g2':
  12703. return 'T.101-G2';
  12704. case 'cstscii':
  12705. case 'tscii':
  12706. return 'TSCII';
  12707. case 'csunicode11':
  12708. case 'unicode11':
  12709. return 'UNICODE-1-1';
  12710. case 'csunicode11utf7':
  12711. case 'unicode11utf7':
  12712. return 'UNICODE-1-1-UTF-7';
  12713. case 'csunknown8bit':
  12714. case 'unknown8bit':
  12715. return 'UNKNOWN-8BIT';
  12716. case 'ansix341968':
  12717. case 'ansix341986':
  12718. case 'ascii':
  12719. case 'cp367':
  12720. case 'csascii':
  12721. case 'ibm367':
  12722. case 'iso646irv1991':
  12723. case 'iso646us':
  12724. case 'isoir6':
  12725. case 'us':
  12726. case 'usascii':
  12727. return 'US-ASCII';
  12728. case 'csusdk':
  12729. case 'usdk':
  12730. return 'us-dk';
  12731. case 'utf7':
  12732. return 'UTF-7';
  12733. case 'utf8':
  12734. return 'UTF-8';
  12735. case 'utf16':
  12736. return 'UTF-16';
  12737. case 'utf16be':
  12738. return 'UTF-16BE';
  12739. case 'utf16le':
  12740. return 'UTF-16LE';
  12741. case 'utf32':
  12742. return 'UTF-32';
  12743. case 'utf32be':
  12744. return 'UTF-32BE';
  12745. case 'utf32le':
  12746. return 'UTF-32LE';
  12747. case 'csventurainternational':
  12748. case 'venturainternational':
  12749. return 'Ventura-International';
  12750. case 'csventuramath':
  12751. case 'venturamath':
  12752. return 'Ventura-Math';
  12753. case 'csventuraus':
  12754. case 'venturaus':
  12755. return 'Ventura-US';
  12756. case 'csiso70videotexsupp1':
  12757. case 'isoir70':
  12758. case 'videotexsuppl':
  12759. return 'videotex-suppl';
  12760. case 'csviqr':
  12761. case 'viqr':
  12762. return 'VIQR';
  12763. case 'csviscii':
  12764. case 'viscii':
  12765. return 'VISCII';
  12766. case 'csshiftjis':
  12767. case 'cswindows31j':
  12768. case 'mskanji':
  12769. case 'shiftjis':
  12770. case 'windows31j':
  12771. return 'Windows-31J';
  12772. case 'iso885911':
  12773. case 'tis620':
  12774. return 'windows-874';
  12775. case 'cseuckr':
  12776. case 'csksc56011987':
  12777. case 'euckr':
  12778. case 'isoir149':
  12779. case 'korean':
  12780. case 'ksc5601':
  12781. case 'ksc56011987':
  12782. case 'ksc56011989':
  12783. case 'windows949':
  12784. return 'windows-949';
  12785. case 'windows1250':
  12786. return 'windows-1250';
  12787. case 'windows1251':
  12788. return 'windows-1251';
  12789. case 'cp819':
  12790. case 'csisolatin1':
  12791. case 'ibm819':
  12792. case 'iso88591':
  12793. case 'iso885911987':
  12794. case 'isoir100':
  12795. case 'l1':
  12796. case 'latin1':
  12797. case 'windows1252':
  12798. return 'windows-1252';
  12799. case 'windows1253':
  12800. return 'windows-1253';
  12801. case 'csisolatin5':
  12802. case 'iso88599':
  12803. case 'iso885991989':
  12804. case 'isoir148':
  12805. case 'l5':
  12806. case 'latin5':
  12807. case 'windows1254':
  12808. return 'windows-1254';
  12809. case 'windows1255':
  12810. return 'windows-1255';
  12811. case 'windows1256':
  12812. return 'windows-1256';
  12813. case 'windows1257':
  12814. return 'windows-1257';
  12815. case 'windows1258':
  12816. return 'windows-1258';
  12817. default:
  12818. return $charset;
  12819. }
  12820. }
  12821. public static function get_curl_version()
  12822. {
  12823. if (is_array($curl = curl_version()))
  12824. {
  12825. $curl = $curl['version'];
  12826. }
  12827. elseif (substr($curl, 0, 5) === 'curl/')
  12828. {
  12829. $curl = substr($curl, 5, strcspn($curl, "\x09\x0A\x0B\x0C\x0D", 5));
  12830. }
  12831. elseif (substr($curl, 0, 8) === 'libcurl/')
  12832. {
  12833. $curl = substr($curl, 8, strcspn($curl, "\x09\x0A\x0B\x0C\x0D", 8));
  12834. }
  12835. else
  12836. {
  12837. $curl = 0;
  12838. }
  12839. return $curl;
  12840. }
  12841. /**
  12842. * Strip HTML comments
  12843. *
  12844. * @param string $data Data to strip comments from
  12845. * @return string Comment stripped string
  12846. */
  12847. public static function strip_comments($data)
  12848. {
  12849. $output = '';
  12850. while (($start = strpos($data, '<!--')) !== false)
  12851. {
  12852. $output .= substr($data, 0, $start);
  12853. if (($end = strpos($data, '-->', $start)) !== false)
  12854. {
  12855. $data = substr_replace($data, '', 0, $end + 3);
  12856. }
  12857. else
  12858. {
  12859. $data = '';
  12860. }
  12861. }
  12862. return $output . $data;
  12863. }
  12864. public static function parse_date($dt)
  12865. {
  12866. $parser = SimplePie_Parse_Date::get();
  12867. return $parser->parse($dt);
  12868. }
  12869. /**
  12870. * Decode HTML entities
  12871. *
  12872. * @deprecated Use DOMDocument instead
  12873. * @param string $data Input data
  12874. * @return string Output data
  12875. */
  12876. public static function entities_decode($data)
  12877. {
  12878. $decoder = new SimplePie_Decode_HTML_Entities($data);
  12879. return $decoder->parse();
  12880. }
  12881. /**
  12882. * Remove RFC822 comments
  12883. *
  12884. * @param string $data Data to strip comments from
  12885. * @return string Comment stripped string
  12886. */
  12887. public static function uncomment_rfc822($string)
  12888. {
  12889. $string = (string) $string;
  12890. $position = 0;
  12891. $length = strlen($string);
  12892. $depth = 0;
  12893. $output = '';
  12894. while ($position < $length && ($pos = strpos($string, '(', $position)) !== false)
  12895. {
  12896. $output .= substr($string, $position, $pos - $position);
  12897. $position = $pos + 1;
  12898. if ($string[$pos - 1] !== '\\')
  12899. {
  12900. $depth++;
  12901. while ($depth && $position < $length)
  12902. {
  12903. $position += strcspn($string, '()', $position);
  12904. if ($string[$position - 1] === '\\')
  12905. {
  12906. $position++;
  12907. continue;
  12908. }
  12909. elseif (isset($string[$position]))
  12910. {
  12911. switch ($string[$position])
  12912. {
  12913. case '(':
  12914. $depth++;
  12915. break;
  12916. case ')':
  12917. $depth--;
  12918. break;
  12919. }
  12920. $position++;
  12921. }
  12922. else
  12923. {
  12924. break;
  12925. }
  12926. }
  12927. }
  12928. else
  12929. {
  12930. $output .= '(';
  12931. }
  12932. }
  12933. $output .= substr($string, $position);
  12934. return $output;
  12935. }
  12936. public static function parse_mime($mime)
  12937. {
  12938. if (($pos = strpos($mime, ';')) === false)
  12939. {
  12940. return trim($mime);
  12941. }
  12942. else
  12943. {
  12944. return trim(substr($mime, 0, $pos));
  12945. }
  12946. }
  12947. public static function atom_03_construct_type($attribs)
  12948. {
  12949. if (isset($attribs['']['mode']) && strtolower(trim($attribs['']['mode']) === 'base64'))
  12950. {
  12951. $mode = SIMPLEPIE_CONSTRUCT_BASE64;
  12952. }
  12953. else
  12954. {
  12955. $mode = SIMPLEPIE_CONSTRUCT_NONE;
  12956. }
  12957. if (isset($attribs['']['type']))
  12958. {
  12959. switch (strtolower(trim($attribs['']['type'])))
  12960. {
  12961. case 'text':
  12962. case 'text/plain':
  12963. return SIMPLEPIE_CONSTRUCT_TEXT | $mode;
  12964. case 'html':
  12965. case 'text/html':
  12966. return SIMPLEPIE_CONSTRUCT_HTML | $mode;
  12967. case 'xhtml':
  12968. case 'application/xhtml+xml':
  12969. return SIMPLEPIE_CONSTRUCT_XHTML | $mode;
  12970. default:
  12971. return SIMPLEPIE_CONSTRUCT_NONE | $mode;
  12972. }
  12973. }
  12974. else
  12975. {
  12976. return SIMPLEPIE_CONSTRUCT_TEXT | $mode;
  12977. }
  12978. }
  12979. public static function atom_10_construct_type($attribs)
  12980. {
  12981. if (isset($attribs['']['type']))
  12982. {
  12983. switch (strtolower(trim($attribs['']['type'])))
  12984. {
  12985. case 'text':
  12986. return SIMPLEPIE_CONSTRUCT_TEXT;
  12987. case 'html':
  12988. return SIMPLEPIE_CONSTRUCT_HTML;
  12989. case 'xhtml':
  12990. return SIMPLEPIE_CONSTRUCT_XHTML;
  12991. default:
  12992. return SIMPLEPIE_CONSTRUCT_NONE;
  12993. }
  12994. }
  12995. return SIMPLEPIE_CONSTRUCT_TEXT;
  12996. }
  12997. public static function atom_10_content_construct_type($attribs)
  12998. {
  12999. if (isset($attribs['']['type']))
  13000. {
  13001. $type = strtolower(trim($attribs['']['type']));
  13002. switch ($type)
  13003. {
  13004. case 'text':
  13005. return SIMPLEPIE_CONSTRUCT_TEXT;
  13006. case 'html':
  13007. return SIMPLEPIE_CONSTRUCT_HTML;
  13008. case 'xhtml':
  13009. return SIMPLEPIE_CONSTRUCT_XHTML;
  13010. }
  13011. if (in_array(substr($type, -4), array('+xml', '/xml')) || substr($type, 0, 5) === 'text/')
  13012. {
  13013. return SIMPLEPIE_CONSTRUCT_NONE;
  13014. }
  13015. else
  13016. {
  13017. return SIMPLEPIE_CONSTRUCT_BASE64;
  13018. }
  13019. }
  13020. else
  13021. {
  13022. return SIMPLEPIE_CONSTRUCT_TEXT;
  13023. }
  13024. }
  13025. public static function is_isegment_nz_nc($string)
  13026. {
  13027. return (bool) preg_match('/^([A-Za-z0-9\-._~\x{A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}\x{10000}-\x{1FFFD}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}\x{40000}-\x{4FFFD}\x{50000}-\x{5FFFD}\x{60000}-\x{6FFFD}\x{70000}-\x{7FFFD}\x{80000}-\x{8FFFD}\x{90000}-\x{9FFFD}\x{A0000}-\x{AFFFD}\x{B0000}-\x{BFFFD}\x{C0000}-\x{CFFFD}\x{D0000}-\x{DFFFD}\x{E1000}-\x{EFFFD}!$&\'()*+,;=@]|(%[0-9ABCDEF]{2}))+$/u', $string);
  13028. }
  13029. public static function space_seperated_tokens($string)
  13030. {
  13031. $space_characters = "\x20\x09\x0A\x0B\x0C\x0D";
  13032. $string_length = strlen($string);
  13033. $position = strspn($string, $space_characters);
  13034. $tokens = array();
  13035. while ($position < $string_length)
  13036. {
  13037. $len = strcspn($string, $space_characters, $position);
  13038. $tokens[] = substr($string, $position, $len);
  13039. $position += $len;
  13040. $position += strspn($string, $space_characters, $position);
  13041. }
  13042. return $tokens;
  13043. }
  13044. /**
  13045. * Converts a unicode codepoint to a UTF-8 character
  13046. *
  13047. * @static
  13048. * @param int $codepoint Unicode codepoint
  13049. * @return string UTF-8 character
  13050. */
  13051. public static function codepoint_to_utf8($codepoint)
  13052. {
  13053. $codepoint = (int) $codepoint;
  13054. if ($codepoint < 0)
  13055. {
  13056. return false;
  13057. }
  13058. else if ($codepoint <= 0x7f)
  13059. {
  13060. return chr($codepoint);
  13061. }
  13062. else if ($codepoint <= 0x7ff)
  13063. {
  13064. return chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x3f));
  13065. }
  13066. else if ($codepoint <= 0xffff)
  13067. {
  13068. return chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  13069. }
  13070. else if ($codepoint <= 0x10ffff)
  13071. {
  13072. return chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  13073. }
  13074. else
  13075. {
  13076. // U+FFFD REPLACEMENT CHARACTER
  13077. return "\xEF\xBF\xBD";
  13078. }
  13079. }
  13080. /**
  13081. * Similar to parse_str()
  13082. *
  13083. * Returns an associative array of name/value pairs, where the value is an
  13084. * array of values that have used the same name
  13085. *
  13086. * @static
  13087. * @param string $str The input string.
  13088. * @return array
  13089. */
  13090. public static function parse_str($str)
  13091. {
  13092. $return = array();
  13093. $str = explode('&', $str);
  13094. foreach ($str as $section)
  13095. {
  13096. if (strpos($section, '=') !== false)
  13097. {
  13098. list($name, $value) = explode('=', $section, 2);
  13099. $return[urldecode($name)][] = urldecode($value);
  13100. }
  13101. else
  13102. {
  13103. $return[urldecode($section)][] = null;
  13104. }
  13105. }
  13106. return $return;
  13107. }
  13108. /**
  13109. * Detect XML encoding, as per XML 1.0 Appendix F.1
  13110. *
  13111. * @todo Add support for EBCDIC
  13112. * @param string $data XML data
  13113. * @param SimplePie_Registry $registry Class registry
  13114. * @return array Possible encodings
  13115. */
  13116. public static function xml_encoding($data, $registry)
  13117. {
  13118. // UTF-32 Big Endian BOM
  13119. if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
  13120. {
  13121. $encoding[] = 'UTF-32BE';
  13122. }
  13123. // UTF-32 Little Endian BOM
  13124. elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
  13125. {
  13126. $encoding[] = 'UTF-32LE';
  13127. }
  13128. // UTF-16 Big Endian BOM
  13129. elseif (substr($data, 0, 2) === "\xFE\xFF")
  13130. {
  13131. $encoding[] = 'UTF-16BE';
  13132. }
  13133. // UTF-16 Little Endian BOM
  13134. elseif (substr($data, 0, 2) === "\xFF\xFE")
  13135. {
  13136. $encoding[] = 'UTF-16LE';
  13137. }
  13138. // UTF-8 BOM
  13139. elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
  13140. {
  13141. $encoding[] = 'UTF-8';
  13142. }
  13143. // UTF-32 Big Endian Without BOM
  13144. elseif (substr($data, 0, 20) === "\x00\x00\x00\x3C\x00\x00\x00\x3F\x00\x00\x00\x78\x00\x00\x00\x6D\x00\x00\x00\x6C")
  13145. {
  13146. if ($pos = strpos($data, "\x00\x00\x00\x3F\x00\x00\x00\x3E"))
  13147. {
  13148. $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 20), 'UTF-32BE', 'UTF-8')));
  13149. if ($parser->parse())
  13150. {
  13151. $encoding[] = $parser->encoding;
  13152. }
  13153. }
  13154. $encoding[] = 'UTF-32BE';
  13155. }
  13156. // UTF-32 Little Endian Without BOM
  13157. elseif (substr($data, 0, 20) === "\x3C\x00\x00\x00\x3F\x00\x00\x00\x78\x00\x00\x00\x6D\x00\x00\x00\x6C\x00\x00\x00")
  13158. {
  13159. if ($pos = strpos($data, "\x3F\x00\x00\x00\x3E\x00\x00\x00"))
  13160. {
  13161. $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 20), 'UTF-32LE', 'UTF-8')));
  13162. if ($parser->parse())
  13163. {
  13164. $encoding[] = $parser->encoding;
  13165. }
  13166. }
  13167. $encoding[] = 'UTF-32LE';
  13168. }
  13169. // UTF-16 Big Endian Without BOM
  13170. elseif (substr($data, 0, 10) === "\x00\x3C\x00\x3F\x00\x78\x00\x6D\x00\x6C")
  13171. {
  13172. if ($pos = strpos($data, "\x00\x3F\x00\x3E"))
  13173. {
  13174. $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 10), 'UTF-16BE', 'UTF-8')));
  13175. if ($parser->parse())
  13176. {
  13177. $encoding[] = $parser->encoding;
  13178. }
  13179. }
  13180. $encoding[] = 'UTF-16BE';
  13181. }
  13182. // UTF-16 Little Endian Without BOM
  13183. elseif (substr($data, 0, 10) === "\x3C\x00\x3F\x00\x78\x00\x6D\x00\x6C\x00")
  13184. {
  13185. if ($pos = strpos($data, "\x3F\x00\x3E\x00"))
  13186. {
  13187. $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 10), 'UTF-16LE', 'UTF-8')));
  13188. if ($parser->parse())
  13189. {
  13190. $encoding[] = $parser->encoding;
  13191. }
  13192. }
  13193. $encoding[] = 'UTF-16LE';
  13194. }
  13195. // US-ASCII (or superset)
  13196. elseif (substr($data, 0, 5) === "\x3C\x3F\x78\x6D\x6C")
  13197. {
  13198. if ($pos = strpos($data, "\x3F\x3E"))
  13199. {
  13200. $parser = $registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5)));
  13201. if ($parser->parse())
  13202. {
  13203. $encoding[] = $parser->encoding;
  13204. }
  13205. }
  13206. $encoding[] = 'UTF-8';
  13207. }
  13208. // Fallback to UTF-8
  13209. else
  13210. {
  13211. $encoding[] = 'UTF-8';
  13212. }
  13213. return $encoding;
  13214. }
  13215. public static function output_javascript()
  13216. {
  13217. if (function_exists('ob_gzhandler'))
  13218. {
  13219. ob_start('ob_gzhandler');
  13220. }
  13221. header('Content-type: text/javascript; charset: UTF-8');
  13222. header('Cache-Control: must-revalidate');
  13223. header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 604800) . ' GMT'); // 7 days
  13224. ?>
  13225. function embed_quicktime(type, bgcolor, width, height, link, placeholder, loop) {
  13226. if (placeholder != '') {
  13227. document.writeln('<embed type="'+type+'" style="cursor:hand; cursor:pointer;" href="'+link+'" src="'+placeholder+'" width="'+width+'" height="'+height+'" autoplay="false" target="myself" controller="false" loop="'+loop+'" scale="aspect" bgcolor="'+bgcolor+'" pluginspage="http://www.apple.com/quicktime/download/"></embed>');
  13228. }
  13229. else {
  13230. document.writeln('<embed type="'+type+'" style="cursor:hand; cursor:pointer;" src="'+link+'" width="'+width+'" height="'+height+'" autoplay="false" target="myself" controller="true" loop="'+loop+'" scale="aspect" bgcolor="'+bgcolor+'" pluginspage="http://www.apple.com/quicktime/download/"></embed>');
  13231. }
  13232. }
  13233. function embed_flash(bgcolor, width, height, link, loop, type) {
  13234. document.writeln('<embed src="'+link+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="'+type+'" quality="high" width="'+width+'" height="'+height+'" bgcolor="'+bgcolor+'" loop="'+loop+'"></embed>');
  13235. }
  13236. function embed_flv(width, height, link, placeholder, loop, player) {
  13237. document.writeln('<embed src="'+player+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" quality="high" width="'+width+'" height="'+height+'" wmode="transparent" flashvars="file='+link+'&autostart=false&repeat='+loop+'&showdigits=true&showfsbutton=false"></embed>');
  13238. }
  13239. function embed_wmedia(width, height, link) {
  13240. document.writeln('<embed type="application/x-mplayer2" src="'+link+'" autosize="1" width="'+width+'" height="'+height+'" showcontrols="1" showstatusbar="0" showdisplay="0" autostart="0"></embed>');
  13241. }
  13242. <?php
  13243. }
  13244. /**
  13245. * Get the SimplePie build timestamp
  13246. *
  13247. * Uses the git index if it exists, otherwise uses the modification time
  13248. * of the newest file.
  13249. */
  13250. public static function get_build()
  13251. {
  13252. $root = dirname(dirname(__FILE__));
  13253. if (file_exists($root . '/.git/index'))
  13254. {
  13255. return filemtime($root . '/.git/index');
  13256. }
  13257. elseif (file_exists($root . '/SimplePie'))
  13258. {
  13259. $time = 0;
  13260. foreach (glob($root . '/SimplePie/*.php') as $file)
  13261. {
  13262. if (($mtime = filemtime($file)) > $time)
  13263. {
  13264. $time = $mtime;
  13265. }
  13266. }
  13267. return $time;
  13268. }
  13269. elseif (file_exists(dirname(__FILE__) . '/Core.php'))
  13270. {
  13271. return filemtime(dirname(__FILE__) . '/Core.php');
  13272. }
  13273. else
  13274. {
  13275. return filemtime(__FILE__);
  13276. }
  13277. }
  13278. /**
  13279. * Format debugging information
  13280. */
  13281. public static function debug(&$sp)
  13282. {
  13283. $info = 'SimplePie ' . SIMPLEPIE_VERSION . ' Build ' . SIMPLEPIE_BUILD . "\n";
  13284. $info .= 'PHP ' . PHP_VERSION . "\n";
  13285. if ($sp->error() !== null)
  13286. {
  13287. $info .= 'Error occurred: ' . $sp->error() . "\n";
  13288. }
  13289. else
  13290. {
  13291. $info .= "No error found.\n";
  13292. }
  13293. $info .= "Extensions:\n";
  13294. $extensions = array('pcre', 'curl', 'zlib', 'mbstring', 'iconv', 'xmlreader', 'xml');
  13295. foreach ($extensions as $ext)
  13296. {
  13297. if (extension_loaded($ext))
  13298. {
  13299. $info .= " $ext loaded\n";
  13300. switch ($ext)
  13301. {
  13302. case 'pcre':
  13303. $info .= ' Version ' . PCRE_VERSION . "\n";
  13304. break;
  13305. case 'curl':
  13306. $version = curl_version();
  13307. $info .= ' Version ' . $version['version'] . "\n";
  13308. break;
  13309. case 'mbstring':
  13310. $info .= ' Overloading: ' . mb_get_info('func_overload') . "\n";
  13311. break;
  13312. case 'iconv':
  13313. $info .= ' Version ' . ICONV_VERSION . "\n";
  13314. break;
  13315. case 'xml':
  13316. $info .= ' Version ' . LIBXML_DOTTED_VERSION . "\n";
  13317. break;
  13318. }
  13319. }
  13320. else
  13321. {
  13322. $info .= " $ext not loaded\n";
  13323. }
  13324. }
  13325. return $info;
  13326. }
  13327. public static function silence_errors($num, $str)
  13328. {
  13329. // No-op
  13330. }
  13331. }
  13332. /**
  13333. * Class to validate and to work with IPv6 addresses.
  13334. *
  13335. * @package SimplePie
  13336. * @subpackage HTTP
  13337. * @copyright 2003-2005 The PHP Group
  13338. * @license http://www.opensource.org/licenses/bsd-license.php
  13339. * @link http://pear.php.net/package/Net_IPv6
  13340. * @author Alexander Merz <alexander.merz@web.de>
  13341. * @author elfrink at introweb dot nl
  13342. * @author Josh Peck <jmp at joshpeck dot org>
  13343. * @author Geoffrey Sneddon <geoffers@gmail.com>
  13344. */
  13345. class SimplePie_Net_IPv6
  13346. {
  13347. /**
  13348. * Uncompresses an IPv6 address
  13349. *
  13350. * RFC 4291 allows you to compress concecutive zero pieces in an address to
  13351. * '::'. This method expects a valid IPv6 address and expands the '::' to
  13352. * the required number of zero pieces.
  13353. *
  13354. * Example: FF01::101 -> FF01:0:0:0:0:0:0:101
  13355. * ::1 -> 0:0:0:0:0:0:0:1
  13356. *
  13357. * @author Alexander Merz <alexander.merz@web.de>
  13358. * @author elfrink at introweb dot nl
  13359. * @author Josh Peck <jmp at joshpeck dot org>
  13360. * @copyright 2003-2005 The PHP Group
  13361. * @license http://www.opensource.org/licenses/bsd-license.php
  13362. * @param string $ip An IPv6 address
  13363. * @return string The uncompressed IPv6 address
  13364. */
  13365. public static function uncompress($ip)
  13366. {
  13367. $c1 = -1;
  13368. $c2 = -1;
  13369. if (substr_count($ip, '::') === 1)
  13370. {
  13371. list($ip1, $ip2) = explode('::', $ip);
  13372. if ($ip1 === '')
  13373. {
  13374. $c1 = -1;
  13375. }
  13376. else
  13377. {
  13378. $c1 = substr_count($ip1, ':');
  13379. }
  13380. if ($ip2 === '')
  13381. {
  13382. $c2 = -1;
  13383. }
  13384. else
  13385. {
  13386. $c2 = substr_count($ip2, ':');
  13387. }
  13388. if (strpos($ip2, '.') !== false)
  13389. {
  13390. $c2++;
  13391. }
  13392. // ::
  13393. if ($c1 === -1 && $c2 === -1)
  13394. {
  13395. $ip = '0:0:0:0:0:0:0:0';
  13396. }
  13397. // ::xxx
  13398. else if ($c1 === -1)
  13399. {
  13400. $fill = str_repeat('0:', 7 - $c2);
  13401. $ip = str_replace('::', $fill, $ip);
  13402. }
  13403. // xxx::
  13404. else if ($c2 === -1)
  13405. {
  13406. $fill = str_repeat(':0', 7 - $c1);
  13407. $ip = str_replace('::', $fill, $ip);
  13408. }
  13409. // xxx::xxx
  13410. else
  13411. {
  13412. $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
  13413. $ip = str_replace('::', $fill, $ip);
  13414. }
  13415. }
  13416. return $ip;
  13417. }
  13418. /**
  13419. * Compresses an IPv6 address
  13420. *
  13421. * RFC 4291 allows you to compress concecutive zero pieces in an address to
  13422. * '::'. This method expects a valid IPv6 address and compresses consecutive
  13423. * zero pieces to '::'.
  13424. *
  13425. * Example: FF01:0:0:0:0:0:0:101 -> FF01::101
  13426. * 0:0:0:0:0:0:0:1 -> ::1
  13427. *
  13428. * @see uncompress()
  13429. * @param string $ip An IPv6 address
  13430. * @return string The compressed IPv6 address
  13431. */
  13432. public static function compress($ip)
  13433. {
  13434. // Prepare the IP to be compressed
  13435. $ip = self::uncompress($ip);
  13436. $ip_parts = self::split_v6_v4($ip);
  13437. // Replace all leading zeros
  13438. $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
  13439. // Find bunches of zeros
  13440. if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE))
  13441. {
  13442. $max = 0;
  13443. $pos = null;
  13444. foreach ($matches[0] as $match)
  13445. {
  13446. if (strlen($match[0]) > $max)
  13447. {
  13448. $max = strlen($match[0]);
  13449. $pos = $match[1];
  13450. }
  13451. }
  13452. $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
  13453. }
  13454. if ($ip_parts[1] !== '')
  13455. {
  13456. return implode(':', $ip_parts);
  13457. }
  13458. else
  13459. {
  13460. return $ip_parts[0];
  13461. }
  13462. }
  13463. /**
  13464. * Splits an IPv6 address into the IPv6 and IPv4 representation parts
  13465. *
  13466. * RFC 4291 allows you to represent the last two parts of an IPv6 address
  13467. * using the standard IPv4 representation
  13468. *
  13469. * Example: 0:0:0:0:0:0:13.1.68.3
  13470. * 0:0:0:0:0:FFFF:129.144.52.38
  13471. *
  13472. * @param string $ip An IPv6 address
  13473. * @return array [0] contains the IPv6 represented part, and [1] the IPv4 represented part
  13474. */
  13475. private static function split_v6_v4($ip)
  13476. {
  13477. if (strpos($ip, '.') !== false)
  13478. {
  13479. $pos = strrpos($ip, ':');
  13480. $ipv6_part = substr($ip, 0, $pos);
  13481. $ipv4_part = substr($ip, $pos + 1);
  13482. return array($ipv6_part, $ipv4_part);
  13483. }
  13484. else
  13485. {
  13486. return array($ip, '');
  13487. }
  13488. }
  13489. /**
  13490. * Checks an IPv6 address
  13491. *
  13492. * Checks if the given IP is a valid IPv6 address
  13493. *
  13494. * @param string $ip An IPv6 address
  13495. * @return bool true if $ip is a valid IPv6 address
  13496. */
  13497. public static function check_ipv6($ip)
  13498. {
  13499. $ip = self::uncompress($ip);
  13500. list($ipv6, $ipv4) = self::split_v6_v4($ip);
  13501. $ipv6 = explode(':', $ipv6);
  13502. $ipv4 = explode('.', $ipv4);
  13503. if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4)
  13504. {
  13505. foreach ($ipv6 as $ipv6_part)
  13506. {
  13507. // The section can't be empty
  13508. if ($ipv6_part === '')
  13509. return false;
  13510. // Nor can it be over four characters
  13511. if (strlen($ipv6_part) > 4)
  13512. return false;
  13513. // Remove leading zeros (this is safe because of the above)
  13514. $ipv6_part = ltrim($ipv6_part, '0');
  13515. if ($ipv6_part === '')
  13516. $ipv6_part = '0';
  13517. // Check the value is valid
  13518. $value = hexdec($ipv6_part);
  13519. if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF)
  13520. return false;
  13521. }
  13522. if (count($ipv4) === 4)
  13523. {
  13524. foreach ($ipv4 as $ipv4_part)
  13525. {
  13526. $value = (int) $ipv4_part;
  13527. if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF)
  13528. return false;
  13529. }
  13530. }
  13531. return true;
  13532. }
  13533. else
  13534. {
  13535. return false;
  13536. }
  13537. }
  13538. /**
  13539. * Checks if the given IP is a valid IPv6 address
  13540. *
  13541. * @codeCoverageIgnore
  13542. * @deprecated Use {@see SimplePie_Net_IPv6::check_ipv6()} instead
  13543. * @see check_ipv6
  13544. * @param string $ip An IPv6 address
  13545. * @return bool true if $ip is a valid IPv6 address
  13546. */
  13547. public static function checkIPv6($ip)
  13548. {
  13549. return self::check_ipv6($ip);
  13550. }
  13551. }
  13552. /**
  13553. * Date Parser
  13554. *
  13555. * @package SimplePie
  13556. * @subpackage Parsing
  13557. */
  13558. class SimplePie_Parse_Date
  13559. {
  13560. /**
  13561. * Input data
  13562. *
  13563. * @access protected
  13564. * @var string
  13565. */
  13566. var $date;
  13567. /**
  13568. * List of days, calendar day name => ordinal day number in the week
  13569. *
  13570. * @access protected
  13571. * @var array
  13572. */
  13573. var $day = array(
  13574. // English
  13575. 'mon' => 1,
  13576. 'monday' => 1,
  13577. 'tue' => 2,
  13578. 'tuesday' => 2,
  13579. 'wed' => 3,
  13580. 'wednesday' => 3,
  13581. 'thu' => 4,
  13582. 'thursday' => 4,
  13583. 'fri' => 5,
  13584. 'friday' => 5,
  13585. 'sat' => 6,
  13586. 'saturday' => 6,
  13587. 'sun' => 7,
  13588. 'sunday' => 7,
  13589. // Dutch
  13590. 'maandag' => 1,
  13591. 'dinsdag' => 2,
  13592. 'woensdag' => 3,
  13593. 'donderdag' => 4,
  13594. 'vrijdag' => 5,
  13595. 'zaterdag' => 6,
  13596. 'zondag' => 7,
  13597. // French
  13598. 'lundi' => 1,
  13599. 'mardi' => 2,
  13600. 'mercredi' => 3,
  13601. 'jeudi' => 4,
  13602. 'vendredi' => 5,
  13603. 'samedi' => 6,
  13604. 'dimanche' => 7,
  13605. // German
  13606. 'montag' => 1,
  13607. 'dienstag' => 2,
  13608. 'mittwoch' => 3,
  13609. 'donnerstag' => 4,
  13610. 'freitag' => 5,
  13611. 'samstag' => 6,
  13612. 'sonnabend' => 6,
  13613. 'sonntag' => 7,
  13614. // Italian
  13615. 'lunedì' => 1,
  13616. 'martedì' => 2,
  13617. 'mercoledì' => 3,
  13618. 'giovedì' => 4,
  13619. 'venerdì' => 5,
  13620. 'sabato' => 6,
  13621. 'domenica' => 7,
  13622. // Spanish
  13623. 'lunes' => 1,
  13624. 'martes' => 2,
  13625. 'miércoles' => 3,
  13626. 'jueves' => 4,
  13627. 'viernes' => 5,
  13628. 'sábado' => 6,
  13629. 'domingo' => 7,
  13630. // Finnish
  13631. 'maanantai' => 1,
  13632. 'tiistai' => 2,
  13633. 'keskiviikko' => 3,
  13634. 'torstai' => 4,
  13635. 'perjantai' => 5,
  13636. 'lauantai' => 6,
  13637. 'sunnuntai' => 7,
  13638. // Hungarian
  13639. 'hétfő' => 1,
  13640. 'kedd' => 2,
  13641. 'szerda' => 3,
  13642. 'csütörtok' => 4,
  13643. 'péntek' => 5,
  13644. 'szombat' => 6,
  13645. 'vasárnap' => 7,
  13646. // Greek
  13647. 'Δευ' => 1,
  13648. 'Τρι' => 2,
  13649. 'Τετ' => 3,
  13650. 'Πεμ' => 4,
  13651. 'Παρ' => 5,
  13652. 'Σαβ' => 6,
  13653. 'Κυρ' => 7,
  13654. );
  13655. /**
  13656. * List of months, calendar month name => calendar month number
  13657. *
  13658. * @access protected
  13659. * @var array
  13660. */
  13661. var $month = array(
  13662. // English
  13663. 'jan' => 1,
  13664. 'january' => 1,
  13665. 'feb' => 2,
  13666. 'february' => 2,
  13667. 'mar' => 3,
  13668. 'march' => 3,
  13669. 'apr' => 4,
  13670. 'april' => 4,
  13671. 'may' => 5,
  13672. // No long form of May
  13673. 'jun' => 6,
  13674. 'june' => 6,
  13675. 'jul' => 7,
  13676. 'july' => 7,
  13677. 'aug' => 8,
  13678. 'august' => 8,
  13679. 'sep' => 9,
  13680. 'september' => 8,
  13681. 'oct' => 10,
  13682. 'october' => 10,
  13683. 'nov' => 11,
  13684. 'november' => 11,
  13685. 'dec' => 12,
  13686. 'december' => 12,
  13687. // Dutch
  13688. 'januari' => 1,
  13689. 'februari' => 2,
  13690. 'maart' => 3,
  13691. 'april' => 4,
  13692. 'mei' => 5,
  13693. 'juni' => 6,
  13694. 'juli' => 7,
  13695. 'augustus' => 8,
  13696. 'september' => 9,
  13697. 'oktober' => 10,
  13698. 'november' => 11,
  13699. 'december' => 12,
  13700. // French
  13701. 'janvier' => 1,
  13702. 'février' => 2,
  13703. 'mars' => 3,
  13704. 'avril' => 4,
  13705. 'mai' => 5,
  13706. 'juin' => 6,
  13707. 'juillet' => 7,
  13708. 'août' => 8,
  13709. 'septembre' => 9,
  13710. 'octobre' => 10,
  13711. 'novembre' => 11,
  13712. 'décembre' => 12,
  13713. // German
  13714. 'januar' => 1,
  13715. 'februar' => 2,
  13716. 'märz' => 3,
  13717. 'april' => 4,
  13718. 'mai' => 5,
  13719. 'juni' => 6,
  13720. 'juli' => 7,
  13721. 'august' => 8,
  13722. 'september' => 9,
  13723. 'oktober' => 10,
  13724. 'november' => 11,
  13725. 'dezember' => 12,
  13726. // Italian
  13727. 'gennaio' => 1,
  13728. 'febbraio' => 2,
  13729. 'marzo' => 3,
  13730. 'aprile' => 4,
  13731. 'maggio' => 5,
  13732. 'giugno' => 6,
  13733. 'luglio' => 7,
  13734. 'agosto' => 8,
  13735. 'settembre' => 9,
  13736. 'ottobre' => 10,
  13737. 'novembre' => 11,
  13738. 'dicembre' => 12,
  13739. // Spanish
  13740. 'enero' => 1,
  13741. 'febrero' => 2,
  13742. 'marzo' => 3,
  13743. 'abril' => 4,
  13744. 'mayo' => 5,
  13745. 'junio' => 6,
  13746. 'julio' => 7,
  13747. 'agosto' => 8,
  13748. 'septiembre' => 9,
  13749. 'setiembre' => 9,
  13750. 'octubre' => 10,
  13751. 'noviembre' => 11,
  13752. 'diciembre' => 12,
  13753. // Finnish
  13754. 'tammikuu' => 1,
  13755. 'helmikuu' => 2,
  13756. 'maaliskuu' => 3,
  13757. 'huhtikuu' => 4,
  13758. 'toukokuu' => 5,
  13759. 'kesäkuu' => 6,
  13760. 'heinäkuu' => 7,
  13761. 'elokuu' => 8,
  13762. 'suuskuu' => 9,
  13763. 'lokakuu' => 10,
  13764. 'marras' => 11,
  13765. 'joulukuu' => 12,
  13766. // Hungarian
  13767. 'január' => 1,
  13768. 'február' => 2,
  13769. 'március' => 3,
  13770. 'április' => 4,
  13771. 'május' => 5,
  13772. 'június' => 6,
  13773. 'július' => 7,
  13774. 'augusztus' => 8,
  13775. 'szeptember' => 9,
  13776. 'október' => 10,
  13777. 'november' => 11,
  13778. 'december' => 12,
  13779. // Greek
  13780. 'Ιαν' => 1,
  13781. 'Φεβ' => 2,
  13782. 'Μάώ' => 3,
  13783. 'Μαώ' => 3,
  13784. 'Απρ' => 4,
  13785. 'Μάι' => 5,
  13786. 'Μαϊ' => 5,
  13787. 'Μαι' => 5,
  13788. 'Ιούν' => 6,
  13789. 'Ιον' => 6,
  13790. 'Ιούλ' => 7,
  13791. 'Ιολ' => 7,
  13792. 'Αύγ' => 8,
  13793. 'Αυγ' => 8,
  13794. 'Σεπ' => 9,
  13795. 'Οκτ' => 10,
  13796. 'Νοέ' => 11,
  13797. 'Δεκ' => 12,
  13798. );
  13799. /**
  13800. * List of timezones, abbreviation => offset from UTC
  13801. *
  13802. * @access protected
  13803. * @var array
  13804. */
  13805. var $timezone = array(
  13806. 'ACDT' => 37800,
  13807. 'ACIT' => 28800,
  13808. 'ACST' => 34200,
  13809. 'ACT' => -18000,
  13810. 'ACWDT' => 35100,
  13811. 'ACWST' => 31500,
  13812. 'AEDT' => 39600,
  13813. 'AEST' => 36000,
  13814. 'AFT' => 16200,
  13815. 'AKDT' => -28800,
  13816. 'AKST' => -32400,
  13817. 'AMDT' => 18000,
  13818. 'AMT' => -14400,
  13819. 'ANAST' => 46800,
  13820. 'ANAT' => 43200,
  13821. 'ART' => -10800,
  13822. 'AZOST' => -3600,
  13823. 'AZST' => 18000,
  13824. 'AZT' => 14400,
  13825. 'BIOT' => 21600,
  13826. 'BIT' => -43200,
  13827. 'BOT' => -14400,
  13828. 'BRST' => -7200,
  13829. 'BRT' => -10800,
  13830. 'BST' => 3600,
  13831. 'BTT' => 21600,
  13832. 'CAST' => 18000,
  13833. 'CAT' => 7200,
  13834. 'CCT' => 23400,
  13835. 'CDT' => -18000,
  13836. 'CEDT' => 7200,
  13837. 'CET' => 3600,
  13838. 'CGST' => -7200,
  13839. 'CGT' => -10800,
  13840. 'CHADT' => 49500,
  13841. 'CHAST' => 45900,
  13842. 'CIST' => -28800,
  13843. 'CKT' => -36000,
  13844. 'CLDT' => -10800,
  13845. 'CLST' => -14400,
  13846. 'COT' => -18000,
  13847. 'CST' => -21600,
  13848. 'CVT' => -3600,
  13849. 'CXT' => 25200,
  13850. 'DAVT' => 25200,
  13851. 'DTAT' => 36000,
  13852. 'EADT' => -18000,
  13853. 'EAST' => -21600,
  13854. 'EAT' => 10800,
  13855. 'ECT' => -18000,
  13856. 'EDT' => -14400,
  13857. 'EEST' => 10800,
  13858. 'EET' => 7200,
  13859. 'EGT' => -3600,
  13860. 'EKST' => 21600,
  13861. 'EST' => -18000,
  13862. 'FJT' => 43200,
  13863. 'FKDT' => -10800,
  13864. 'FKST' => -14400,
  13865. 'FNT' => -7200,
  13866. 'GALT' => -21600,
  13867. 'GEDT' => 14400,
  13868. 'GEST' => 10800,
  13869. 'GFT' => -10800,
  13870. 'GILT' => 43200,
  13871. 'GIT' => -32400,
  13872. 'GST' => 14400,
  13873. 'GST' => -7200,
  13874. 'GYT' => -14400,
  13875. 'HAA' => -10800,
  13876. 'HAC' => -18000,
  13877. 'HADT' => -32400,
  13878. 'HAE' => -14400,
  13879. 'HAP' => -25200,
  13880. 'HAR' => -21600,
  13881. 'HAST' => -36000,
  13882. 'HAT' => -9000,
  13883. 'HAY' => -28800,
  13884. 'HKST' => 28800,
  13885. 'HMT' => 18000,
  13886. 'HNA' => -14400,
  13887. 'HNC' => -21600,
  13888. 'HNE' => -18000,
  13889. 'HNP' => -28800,
  13890. 'HNR' => -25200,
  13891. 'HNT' => -12600,
  13892. 'HNY' => -32400,
  13893. 'IRDT' => 16200,
  13894. 'IRKST' => 32400,
  13895. 'IRKT' => 28800,
  13896. 'IRST' => 12600,
  13897. 'JFDT' => -10800,
  13898. 'JFST' => -14400,
  13899. 'JST' => 32400,
  13900. 'KGST' => 21600,
  13901. 'KGT' => 18000,
  13902. 'KOST' => 39600,
  13903. 'KOVST' => 28800,
  13904. 'KOVT' => 25200,
  13905. 'KRAST' => 28800,
  13906. 'KRAT' => 25200,
  13907. 'KST' => 32400,
  13908. 'LHDT' => 39600,
  13909. 'LHST' => 37800,
  13910. 'LINT' => 50400,
  13911. 'LKT' => 21600,
  13912. 'MAGST' => 43200,
  13913. 'MAGT' => 39600,
  13914. 'MAWT' => 21600,
  13915. 'MDT' => -21600,
  13916. 'MESZ' => 7200,
  13917. 'MEZ' => 3600,
  13918. 'MHT' => 43200,
  13919. 'MIT' => -34200,
  13920. 'MNST' => 32400,
  13921. 'MSDT' => 14400,
  13922. 'MSST' => 10800,
  13923. 'MST' => -25200,
  13924. 'MUT' => 14400,
  13925. 'MVT' => 18000,
  13926. 'MYT' => 28800,
  13927. 'NCT' => 39600,
  13928. 'NDT' => -9000,
  13929. 'NFT' => 41400,
  13930. 'NMIT' => 36000,
  13931. 'NOVST' => 25200,
  13932. 'NOVT' => 21600,
  13933. 'NPT' => 20700,
  13934. 'NRT' => 43200,
  13935. 'NST' => -12600,
  13936. 'NUT' => -39600,
  13937. 'NZDT' => 46800,
  13938. 'NZST' => 43200,
  13939. 'OMSST' => 25200,
  13940. 'OMST' => 21600,
  13941. 'PDT' => -25200,
  13942. 'PET' => -18000,
  13943. 'PETST' => 46800,
  13944. 'PETT' => 43200,
  13945. 'PGT' => 36000,
  13946. 'PHOT' => 46800,
  13947. 'PHT' => 28800,
  13948. 'PKT' => 18000,
  13949. 'PMDT' => -7200,
  13950. 'PMST' => -10800,
  13951. 'PONT' => 39600,
  13952. 'PST' => -28800,
  13953. 'PWT' => 32400,
  13954. 'PYST' => -10800,
  13955. 'PYT' => -14400,
  13956. 'RET' => 14400,
  13957. 'ROTT' => -10800,
  13958. 'SAMST' => 18000,
  13959. 'SAMT' => 14400,
  13960. 'SAST' => 7200,
  13961. 'SBT' => 39600,
  13962. 'SCDT' => 46800,
  13963. 'SCST' => 43200,
  13964. 'SCT' => 14400,
  13965. 'SEST' => 3600,
  13966. 'SGT' => 28800,
  13967. 'SIT' => 28800,
  13968. 'SRT' => -10800,
  13969. 'SST' => -39600,
  13970. 'SYST' => 10800,
  13971. 'SYT' => 7200,
  13972. 'TFT' => 18000,
  13973. 'THAT' => -36000,
  13974. 'TJT' => 18000,
  13975. 'TKT' => -36000,
  13976. 'TMT' => 18000,
  13977. 'TOT' => 46800,
  13978. 'TPT' => 32400,
  13979. 'TRUT' => 36000,
  13980. 'TVT' => 43200,
  13981. 'TWT' => 28800,
  13982. 'UYST' => -7200,
  13983. 'UYT' => -10800,
  13984. 'UZT' => 18000,
  13985. 'VET' => -14400,
  13986. 'VLAST' => 39600,
  13987. 'VLAT' => 36000,
  13988. 'VOST' => 21600,
  13989. 'VUT' => 39600,
  13990. 'WAST' => 7200,
  13991. 'WAT' => 3600,
  13992. 'WDT' => 32400,
  13993. 'WEST' => 3600,
  13994. 'WFT' => 43200,
  13995. 'WIB' => 25200,
  13996. 'WIT' => 32400,
  13997. 'WITA' => 28800,
  13998. 'WKST' => 18000,
  13999. 'WST' => 28800,
  14000. 'YAKST' => 36000,
  14001. 'YAKT' => 32400,
  14002. 'YAPT' => 36000,
  14003. 'YEKST' => 21600,
  14004. 'YEKT' => 18000,
  14005. );
  14006. /**
  14007. * Cached PCRE for SimplePie_Parse_Date::$day
  14008. *
  14009. * @access protected
  14010. * @var string
  14011. */
  14012. var $day_pcre;
  14013. /**
  14014. * Cached PCRE for SimplePie_Parse_Date::$month
  14015. *
  14016. * @access protected
  14017. * @var string
  14018. */
  14019. var $month_pcre;
  14020. /**
  14021. * Array of user-added callback methods
  14022. *
  14023. * @access private
  14024. * @var array
  14025. */
  14026. var $built_in = array();
  14027. /**
  14028. * Array of user-added callback methods
  14029. *
  14030. * @access private
  14031. * @var array
  14032. */
  14033. var $user = array();
  14034. /**
  14035. * Create new SimplePie_Parse_Date object, and set self::day_pcre,
  14036. * self::month_pcre, and self::built_in
  14037. *
  14038. * @access private
  14039. */
  14040. public function __construct()
  14041. {
  14042. $this->day_pcre = '(' . implode(array_keys($this->day), '|') . ')';
  14043. $this->month_pcre = '(' . implode(array_keys($this->month), '|') . ')';
  14044. static $cache;
  14045. if (!isset($cache[get_class($this)]))
  14046. {
  14047. $all_methods = get_class_methods($this);
  14048. foreach ($all_methods as $method)
  14049. {
  14050. if (strtolower(substr($method, 0, 5)) === 'date_')
  14051. {
  14052. $cache[get_class($this)][] = $method;
  14053. }
  14054. }
  14055. }
  14056. foreach ($cache[get_class($this)] as $method)
  14057. {
  14058. $this->built_in[] = $method;
  14059. }
  14060. }
  14061. /**
  14062. * Get the object
  14063. *
  14064. * @access public
  14065. */
  14066. public static function get()
  14067. {
  14068. static $object;
  14069. if (!$object)
  14070. {
  14071. $object = new SimplePie_Parse_Date;
  14072. }
  14073. return $object;
  14074. }
  14075. /**
  14076. * Parse a date
  14077. *
  14078. * @final
  14079. * @access public
  14080. * @param string $date Date to parse
  14081. * @return int Timestamp corresponding to date string, or false on failure
  14082. */
  14083. public function parse($date)
  14084. {
  14085. foreach ($this->user as $method)
  14086. {
  14087. if (($returned = call_user_func($method, $date)) !== false)
  14088. {
  14089. return $returned;
  14090. }
  14091. }
  14092. foreach ($this->built_in as $method)
  14093. {
  14094. if (($returned = call_user_func(array($this, $method), $date)) !== false)
  14095. {
  14096. return $returned;
  14097. }
  14098. }
  14099. return false;
  14100. }
  14101. /**
  14102. * Add a callback method to parse a date
  14103. *
  14104. * @final
  14105. * @access public
  14106. * @param callback $callback
  14107. */
  14108. public function add_callback($callback)
  14109. {
  14110. if (is_callable($callback))
  14111. {
  14112. $this->user[] = $callback;
  14113. }
  14114. else
  14115. {
  14116. trigger_error('User-supplied function must be a valid callback', E_USER_WARNING);
  14117. }
  14118. }
  14119. /**
  14120. * Parse a superset of W3C-DTF (allows hyphens and colons to be omitted, as
  14121. * well as allowing any of upper or lower case "T", horizontal tabs, or
  14122. * spaces to be used as the time seperator (including more than one))
  14123. *
  14124. * @access protected
  14125. * @return int Timestamp
  14126. */
  14127. public function date_w3cdtf($date)
  14128. {
  14129. static $pcre;
  14130. if (!$pcre)
  14131. {
  14132. $year = '([0-9]{4})';
  14133. $month = $day = $hour = $minute = $second = '([0-9]{2})';
  14134. $decimal = '([0-9]*)';
  14135. $zone = '(?:(Z)|([+\-])([0-9]{1,2}):?([0-9]{1,2}))';
  14136. $pcre = '/^' . $year . '(?:-?' . $month . '(?:-?' . $day . '(?:[Tt\x09\x20]+' . $hour . '(?::?' . $minute . '(?::?' . $second . '(?:.' . $decimal . ')?)?)?' . $zone . ')?)?)?$/';
  14137. }
  14138. if (preg_match($pcre, $date, $match))
  14139. {
  14140. /*
  14141. Capturing subpatterns:
  14142. 1: Year
  14143. 2: Month
  14144. 3: Day
  14145. 4: Hour
  14146. 5: Minute
  14147. 6: Second
  14148. 7: Decimal fraction of a second
  14149. 8: Zulu
  14150. 9: Timezone ±
  14151. 10: Timezone hours
  14152. 11: Timezone minutes
  14153. */
  14154. // Fill in empty matches
  14155. for ($i = count($match); $i <= 3; $i++)
  14156. {
  14157. $match[$i] = '1';
  14158. }
  14159. for ($i = count($match); $i <= 7; $i++)
  14160. {
  14161. $match[$i] = '0';
  14162. }
  14163. // Numeric timezone
  14164. if (isset($match[9]) && $match[9] !== '')
  14165. {
  14166. $timezone = $match[10] * 3600;
  14167. $timezone += $match[11] * 60;
  14168. if ($match[9] === '-')
  14169. {
  14170. $timezone = 0 - $timezone;
  14171. }
  14172. }
  14173. else
  14174. {
  14175. $timezone = 0;
  14176. }
  14177. // Convert the number of seconds to an integer, taking decimals into account
  14178. $second = round($match[6] + $match[7] / pow(10, strlen($match[7])));
  14179. return gmmktime($match[4], $match[5], $second, $match[2], $match[3], $match[1]) - $timezone;
  14180. }
  14181. else
  14182. {
  14183. return false;
  14184. }
  14185. }
  14186. /**
  14187. * Remove RFC822 comments
  14188. *
  14189. * @access protected
  14190. * @param string $data Data to strip comments from
  14191. * @return string Comment stripped string
  14192. */
  14193. public function remove_rfc2822_comments($string)
  14194. {
  14195. $string = (string) $string;
  14196. $position = 0;
  14197. $length = strlen($string);
  14198. $depth = 0;
  14199. $output = '';
  14200. while ($position < $length && ($pos = strpos($string, '(', $position)) !== false)
  14201. {
  14202. $output .= substr($string, $position, $pos - $position);
  14203. $position = $pos + 1;
  14204. if ($string[$pos - 1] !== '\\')
  14205. {
  14206. $depth++;
  14207. while ($depth && $position < $length)
  14208. {
  14209. $position += strcspn($string, '()', $position);
  14210. if ($string[$position - 1] === '\\')
  14211. {
  14212. $position++;
  14213. continue;
  14214. }
  14215. elseif (isset($string[$position]))
  14216. {
  14217. switch ($string[$position])
  14218. {
  14219. case '(':
  14220. $depth++;
  14221. break;
  14222. case ')':
  14223. $depth--;
  14224. break;
  14225. }
  14226. $position++;
  14227. }
  14228. else
  14229. {
  14230. break;
  14231. }
  14232. }
  14233. }
  14234. else
  14235. {
  14236. $output .= '(';
  14237. }
  14238. }
  14239. $output .= substr($string, $position);
  14240. return $output;
  14241. }
  14242. /**
  14243. * Parse RFC2822's date format
  14244. *
  14245. * @access protected
  14246. * @return int Timestamp
  14247. */
  14248. public function date_rfc2822($date)
  14249. {
  14250. static $pcre;
  14251. if (!$pcre)
  14252. {
  14253. $wsp = '[\x09\x20]';
  14254. $fws = '(?:' . $wsp . '+|' . $wsp . '*(?:\x0D\x0A' . $wsp . '+)+)';
  14255. $optional_fws = $fws . '?';
  14256. $day_name = $this->day_pcre;
  14257. $month = $this->month_pcre;
  14258. $day = '([0-9]{1,2})';
  14259. $hour = $minute = $second = '([0-9]{2})';
  14260. $year = '([0-9]{2,4})';
  14261. $num_zone = '([+\-])([0-9]{2})([0-9]{2})';
  14262. $character_zone = '([A-Z]{1,5})';
  14263. $zone = '(?:' . $num_zone . '|' . $character_zone . ')';
  14264. $pcre = '/(?:' . $optional_fws . $day_name . $optional_fws . ',)?' . $optional_fws . $day . $fws . $month . $fws . $year . $fws . $hour . $optional_fws . ':' . $optional_fws . $minute . '(?:' . $optional_fws . ':' . $optional_fws . $second . ')?' . $fws . $zone . '/i';
  14265. }
  14266. if (preg_match($pcre, $this->remove_rfc2822_comments($date), $match))
  14267. {
  14268. /*
  14269. Capturing subpatterns:
  14270. 1: Day name
  14271. 2: Day
  14272. 3: Month
  14273. 4: Year
  14274. 5: Hour
  14275. 6: Minute
  14276. 7: Second
  14277. 8: Timezone ±
  14278. 9: Timezone hours
  14279. 10: Timezone minutes
  14280. 11: Alphabetic timezone
  14281. */
  14282. // Find the month number
  14283. $month = $this->month[strtolower($match[3])];
  14284. // Numeric timezone
  14285. if ($match[8] !== '')
  14286. {
  14287. $timezone = $match[9] * 3600;
  14288. $timezone += $match[10] * 60;
  14289. if ($match[8] === '-')
  14290. {
  14291. $timezone = 0 - $timezone;
  14292. }
  14293. }
  14294. // Character timezone
  14295. elseif (isset($this->timezone[strtoupper($match[11])]))
  14296. {
  14297. $timezone = $this->timezone[strtoupper($match[11])];
  14298. }
  14299. // Assume everything else to be -0000
  14300. else
  14301. {
  14302. $timezone = 0;
  14303. }
  14304. // Deal with 2/3 digit years
  14305. if ($match[4] < 50)
  14306. {
  14307. $match[4] += 2000;
  14308. }
  14309. elseif ($match[4] < 1000)
  14310. {
  14311. $match[4] += 1900;
  14312. }
  14313. // Second is optional, if it is empty set it to zero
  14314. if ($match[7] !== '')
  14315. {
  14316. $second = $match[7];
  14317. }
  14318. else
  14319. {
  14320. $second = 0;
  14321. }
  14322. return gmmktime($match[5], $match[6], $second, $month, $match[2], $match[4]) - $timezone;
  14323. }
  14324. else
  14325. {
  14326. return false;
  14327. }
  14328. }
  14329. /**
  14330. * Parse RFC850's date format
  14331. *
  14332. * @access protected
  14333. * @return int Timestamp
  14334. */
  14335. public function date_rfc850($date)
  14336. {
  14337. static $pcre;
  14338. if (!$pcre)
  14339. {
  14340. $space = '[\x09\x20]+';
  14341. $day_name = $this->day_pcre;
  14342. $month = $this->month_pcre;
  14343. $day = '([0-9]{1,2})';
  14344. $year = $hour = $minute = $second = '([0-9]{2})';
  14345. $zone = '([A-Z]{1,5})';
  14346. $pcre = '/^' . $day_name . ',' . $space . $day . '-' . $month . '-' . $year . $space . $hour . ':' . $minute . ':' . $second . $space . $zone . '$/i';
  14347. }
  14348. if (preg_match($pcre, $date, $match))
  14349. {
  14350. /*
  14351. Capturing subpatterns:
  14352. 1: Day name
  14353. 2: Day
  14354. 3: Month
  14355. 4: Year
  14356. 5: Hour
  14357. 6: Minute
  14358. 7: Second
  14359. 8: Timezone
  14360. */
  14361. // Month
  14362. $month = $this->month[strtolower($match[3])];
  14363. // Character timezone
  14364. if (isset($this->timezone[strtoupper($match[8])]))
  14365. {
  14366. $timezone = $this->timezone[strtoupper($match[8])];
  14367. }
  14368. // Assume everything else to be -0000
  14369. else
  14370. {
  14371. $timezone = 0;
  14372. }
  14373. // Deal with 2 digit year
  14374. if ($match[4] < 50)
  14375. {
  14376. $match[4] += 2000;
  14377. }
  14378. else
  14379. {
  14380. $match[4] += 1900;
  14381. }
  14382. return gmmktime($match[5], $match[6], $match[7], $month, $match[2], $match[4]) - $timezone;
  14383. }
  14384. else
  14385. {
  14386. return false;
  14387. }
  14388. }
  14389. /**
  14390. * Parse C99's asctime()'s date format
  14391. *
  14392. * @access protected
  14393. * @return int Timestamp
  14394. */
  14395. public function date_asctime($date)
  14396. {
  14397. static $pcre;
  14398. if (!$pcre)
  14399. {
  14400. $space = '[\x09\x20]+';
  14401. $wday_name = $this->day_pcre;
  14402. $mon_name = $this->month_pcre;
  14403. $day = '([0-9]{1,2})';
  14404. $hour = $sec = $min = '([0-9]{2})';
  14405. $year = '([0-9]{4})';
  14406. $terminator = '\x0A?\x00?';
  14407. $pcre = '/^' . $wday_name . $space . $mon_name . $space . $day . $space . $hour . ':' . $min . ':' . $sec . $space . $year . $terminator . '$/i';
  14408. }
  14409. if (preg_match($pcre, $date, $match))
  14410. {
  14411. /*
  14412. Capturing subpatterns:
  14413. 1: Day name
  14414. 2: Month
  14415. 3: Day
  14416. 4: Hour
  14417. 5: Minute
  14418. 6: Second
  14419. 7: Year
  14420. */
  14421. $month = $this->month[strtolower($match[2])];
  14422. return gmmktime($match[4], $match[5], $match[6], $month, $match[3], $match[7]);
  14423. }
  14424. else
  14425. {
  14426. return false;
  14427. }
  14428. }
  14429. /**
  14430. * Parse dates using strtotime()
  14431. *
  14432. * @access protected
  14433. * @return int Timestamp
  14434. */
  14435. public function date_strtotime($date)
  14436. {
  14437. $strtotime = strtotime($date);
  14438. if ($strtotime === -1 || $strtotime === false)
  14439. {
  14440. return false;
  14441. }
  14442. else
  14443. {
  14444. return $strtotime;
  14445. }
  14446. }
  14447. }
  14448. /**
  14449. * Parses XML into something sane
  14450. *
  14451. *
  14452. * This class can be overloaded with {@see SimplePie::set_parser_class()}
  14453. *
  14454. * @package SimplePie
  14455. * @subpackage Parsing
  14456. */
  14457. class SimplePie_Parser
  14458. {
  14459. var $error_code;
  14460. var $error_string;
  14461. var $current_line;
  14462. var $current_column;
  14463. var $current_byte;
  14464. var $separator = ' ';
  14465. var $namespace = array('');
  14466. var $element = array('');
  14467. var $xml_base = array('');
  14468. var $xml_base_explicit = array(false);
  14469. var $xml_lang = array('');
  14470. var $data = array();
  14471. var $datas = array(array());
  14472. var $current_xhtml_construct = -1;
  14473. var $encoding;
  14474. protected $registry;
  14475. public function set_registry(SimplePie_Registry $registry)
  14476. {
  14477. $this->registry = $registry;
  14478. }
  14479. public function parse(&$data, $encoding)
  14480. {
  14481. // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
  14482. if (strtoupper($encoding) === 'US-ASCII')
  14483. {
  14484. $this->encoding = 'UTF-8';
  14485. }
  14486. else
  14487. {
  14488. $this->encoding = $encoding;
  14489. }
  14490. // Strip BOM:
  14491. // UTF-32 Big Endian BOM
  14492. if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
  14493. {
  14494. $data = substr($data, 4);
  14495. }
  14496. // UTF-32 Little Endian BOM
  14497. elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
  14498. {
  14499. $data = substr($data, 4);
  14500. }
  14501. // UTF-16 Big Endian BOM
  14502. elseif (substr($data, 0, 2) === "\xFE\xFF")
  14503. {
  14504. $data = substr($data, 2);
  14505. }
  14506. // UTF-16 Little Endian BOM
  14507. elseif (substr($data, 0, 2) === "\xFF\xFE")
  14508. {
  14509. $data = substr($data, 2);
  14510. }
  14511. // UTF-8 BOM
  14512. elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
  14513. {
  14514. $data = substr($data, 3);
  14515. }
  14516. if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
  14517. {
  14518. $declaration = $this->registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5)));
  14519. if ($declaration->parse())
  14520. {
  14521. $data = substr($data, $pos + 2);
  14522. $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . $data;
  14523. }
  14524. else
  14525. {
  14526. $this->error_string = 'SimplePie bug! Please report this!';
  14527. return false;
  14528. }
  14529. }
  14530. $return = true;
  14531. static $xml_is_sane = null;
  14532. if ($xml_is_sane === null)
  14533. {
  14534. $parser_check = xml_parser_create();
  14535. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  14536. xml_parser_free($parser_check);
  14537. $xml_is_sane = isset($values[0]['value']);
  14538. }
  14539. // Create the parser
  14540. if ($xml_is_sane)
  14541. {
  14542. $xml = xml_parser_create_ns($this->encoding, $this->separator);
  14543. xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
  14544. xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
  14545. xml_set_object($xml, $this);
  14546. xml_set_character_data_handler($xml, 'cdata');
  14547. xml_set_element_handler($xml, 'tag_open', 'tag_close');
  14548. // Parse!
  14549. if (!xml_parse($xml, $data, true))
  14550. {
  14551. $this->error_code = xml_get_error_code($xml);
  14552. $this->error_string = xml_error_string($this->error_code);
  14553. $return = false;
  14554. }
  14555. $this->current_line = xml_get_current_line_number($xml);
  14556. $this->current_column = xml_get_current_column_number($xml);
  14557. $this->current_byte = xml_get_current_byte_index($xml);
  14558. xml_parser_free($xml);
  14559. return $return;
  14560. }
  14561. else
  14562. {
  14563. libxml_clear_errors();
  14564. $xml = new XMLReader();
  14565. $xml->xml($data);
  14566. while (@$xml->read())
  14567. {
  14568. switch ($xml->nodeType)
  14569. {
  14570. case constant('XMLReader::END_ELEMENT'):
  14571. if ($xml->namespaceURI !== '')
  14572. {
  14573. $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
  14574. }
  14575. else
  14576. {
  14577. $tagName = $xml->localName;
  14578. }
  14579. $this->tag_close(null, $tagName);
  14580. break;
  14581. case constant('XMLReader::ELEMENT'):
  14582. $empty = $xml->isEmptyElement;
  14583. if ($xml->namespaceURI !== '')
  14584. {
  14585. $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
  14586. }
  14587. else
  14588. {
  14589. $tagName = $xml->localName;
  14590. }
  14591. $attributes = array();
  14592. while ($xml->moveToNextAttribute())
  14593. {
  14594. if ($xml->namespaceURI !== '')
  14595. {
  14596. $attrName = $xml->namespaceURI . $this->separator . $xml->localName;
  14597. }
  14598. else
  14599. {
  14600. $attrName = $xml->localName;
  14601. }
  14602. $attributes[$attrName] = $xml->value;
  14603. }
  14604. $this->tag_open(null, $tagName, $attributes);
  14605. if ($empty)
  14606. {
  14607. $this->tag_close(null, $tagName);
  14608. }
  14609. break;
  14610. case constant('XMLReader::TEXT'):
  14611. case constant('XMLReader::CDATA'):
  14612. $this->cdata(null, $xml->value);
  14613. break;
  14614. }
  14615. }
  14616. if ($error = libxml_get_last_error())
  14617. {
  14618. $this->error_code = $error->code;
  14619. $this->error_string = $error->message;
  14620. $this->current_line = $error->line;
  14621. $this->current_column = $error->column;
  14622. return false;
  14623. }
  14624. else
  14625. {
  14626. return true;
  14627. }
  14628. }
  14629. }
  14630. public function get_error_code()
  14631. {
  14632. return $this->error_code;
  14633. }
  14634. public function get_error_string()
  14635. {
  14636. return $this->error_string;
  14637. }
  14638. public function get_current_line()
  14639. {
  14640. return $this->current_line;
  14641. }
  14642. public function get_current_column()
  14643. {
  14644. return $this->current_column;
  14645. }
  14646. public function get_current_byte()
  14647. {
  14648. return $this->current_byte;
  14649. }
  14650. public function get_data()
  14651. {
  14652. return $this->data;
  14653. }
  14654. public function tag_open($parser, $tag, $attributes)
  14655. {
  14656. list($this->namespace[], $this->element[]) = $this->split_ns($tag);
  14657. $attribs = array();
  14658. foreach ($attributes as $name => $value)
  14659. {
  14660. list($attrib_namespace, $attribute) = $this->split_ns($name);
  14661. $attribs[$attrib_namespace][$attribute] = $value;
  14662. }
  14663. if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['base']))
  14664. {
  14665. $base = $this->registry->call('Misc', 'absolutize_url', array($attribs[SIMPLEPIE_NAMESPACE_XML]['base'], end($this->xml_base)));
  14666. if ($base !== false)
  14667. {
  14668. $this->xml_base[] = $base;
  14669. $this->xml_base_explicit[] = true;
  14670. }
  14671. }
  14672. else
  14673. {
  14674. $this->xml_base[] = end($this->xml_base);
  14675. $this->xml_base_explicit[] = end($this->xml_base_explicit);
  14676. }
  14677. if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['lang']))
  14678. {
  14679. $this->xml_lang[] = $attribs[SIMPLEPIE_NAMESPACE_XML]['lang'];
  14680. }
  14681. else
  14682. {
  14683. $this->xml_lang[] = end($this->xml_lang);
  14684. }
  14685. if ($this->current_xhtml_construct >= 0)
  14686. {
  14687. $this->current_xhtml_construct++;
  14688. if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML)
  14689. {
  14690. $this->data['data'] .= '<' . end($this->element);
  14691. if (isset($attribs['']))
  14692. {
  14693. foreach ($attribs[''] as $name => $value)
  14694. {
  14695. $this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"';
  14696. }
  14697. }
  14698. $this->data['data'] .= '>';
  14699. }
  14700. }
  14701. else
  14702. {
  14703. $this->datas[] =& $this->data;
  14704. $this->data =& $this->data['child'][end($this->namespace)][end($this->element)][];
  14705. $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));
  14706. 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')
  14707. || (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')
  14708. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_20 && in_array(end($this->element), array('title')))
  14709. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_090 && in_array(end($this->element), array('title')))
  14710. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_10 && in_array(end($this->element), array('title'))))
  14711. {
  14712. $this->current_xhtml_construct = 0;
  14713. }
  14714. }
  14715. }
  14716. public function cdata($parser, $cdata)
  14717. {
  14718. if ($this->current_xhtml_construct >= 0)
  14719. {
  14720. $this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding);
  14721. }
  14722. else
  14723. {
  14724. $this->data['data'] .= $cdata;
  14725. }
  14726. }
  14727. public function tag_close($parser, $tag)
  14728. {
  14729. if ($this->current_xhtml_construct >= 0)
  14730. {
  14731. $this->current_xhtml_construct--;
  14732. 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')))
  14733. {
  14734. $this->data['data'] .= '</' . end($this->element) . '>';
  14735. }
  14736. }
  14737. if ($this->current_xhtml_construct === -1)
  14738. {
  14739. $this->data =& $this->datas[count($this->datas) - 1];
  14740. array_pop($this->datas);
  14741. }
  14742. array_pop($this->element);
  14743. array_pop($this->namespace);
  14744. array_pop($this->xml_base);
  14745. array_pop($this->xml_base_explicit);
  14746. array_pop($this->xml_lang);
  14747. }
  14748. public function split_ns($string)
  14749. {
  14750. static $cache = array();
  14751. if (!isset($cache[$string]))
  14752. {
  14753. if ($pos = strpos($string, $this->separator))
  14754. {
  14755. static $separator_length;
  14756. if (!$separator_length)
  14757. {
  14758. $separator_length = strlen($this->separator);
  14759. }
  14760. $namespace = substr($string, 0, $pos);
  14761. $local_name = substr($string, $pos + $separator_length);
  14762. if (strtolower($namespace) === SIMPLEPIE_NAMESPACE_ITUNES)
  14763. {
  14764. $namespace = SIMPLEPIE_NAMESPACE_ITUNES;
  14765. }
  14766. // Normalize the Media RSS namespaces
  14767. if ($namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG ||
  14768. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2 ||
  14769. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3 ||
  14770. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4 ||
  14771. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5 )
  14772. {
  14773. $namespace = SIMPLEPIE_NAMESPACE_MEDIARSS;
  14774. }
  14775. $cache[$string] = array($namespace, $local_name);
  14776. }
  14777. else
  14778. {
  14779. $cache[$string] = array('', $string);
  14780. }
  14781. }
  14782. return $cache[$string];
  14783. }
  14784. }
  14785. /**
  14786. * Handles `<media:rating>` or `<itunes:explicit>` tags as defined in Media RSS and iTunes RSS respectively
  14787. *
  14788. * Used by {@see SimplePie_Enclosure::get_rating()} and {@see SimplePie_Enclosure::get_ratings()}
  14789. *
  14790. * This class can be overloaded with {@see SimplePie::set_rating_class()}
  14791. *
  14792. * @package SimplePie
  14793. * @subpackage API
  14794. */
  14795. class SimplePie_Rating
  14796. {
  14797. /**
  14798. * Rating scheme
  14799. *
  14800. * @var string
  14801. * @see get_scheme()
  14802. */
  14803. var $scheme;
  14804. /**
  14805. * Rating value
  14806. *
  14807. * @var string
  14808. * @see get_value()
  14809. */
  14810. var $value;
  14811. /**
  14812. * Constructor, used to input the data
  14813. *
  14814. * For documentation on all the parameters, see the corresponding
  14815. * properties and their accessors
  14816. */
  14817. public function __construct($scheme = null, $value = null)
  14818. {
  14819. $this->scheme = $scheme;
  14820. $this->value = $value;
  14821. }
  14822. /**
  14823. * String-ified version
  14824. *
  14825. * @return string
  14826. */
  14827. public function __toString()
  14828. {
  14829. // There is no $this->data here
  14830. return md5(serialize($this));
  14831. }
  14832. /**
  14833. * Get the organizational scheme for the rating
  14834. *
  14835. * @return string|null
  14836. */
  14837. public function get_scheme()
  14838. {
  14839. if ($this->scheme !== null)
  14840. {
  14841. return $this->scheme;
  14842. }
  14843. else
  14844. {
  14845. return null;
  14846. }
  14847. }
  14848. /**
  14849. * Get the value of the rating
  14850. *
  14851. * @return string|null
  14852. */
  14853. public function get_value()
  14854. {
  14855. if ($this->value !== null)
  14856. {
  14857. return $this->value;
  14858. }
  14859. else
  14860. {
  14861. return null;
  14862. }
  14863. }
  14864. }
  14865. /**
  14866. * Handles creating objects and calling methods
  14867. *
  14868. * Access this via {@see SimplePie::get_registry()}
  14869. *
  14870. * @package SimplePie
  14871. */
  14872. class SimplePie_Registry
  14873. {
  14874. /**
  14875. * Default class mapping
  14876. *
  14877. * Overriding classes *must* subclass these.
  14878. *
  14879. * @var array
  14880. */
  14881. protected $default = array(
  14882. 'Cache' => 'SimplePie_Cache',
  14883. 'Locator' => 'SimplePie_Locator',
  14884. 'Parser' => 'SimplePie_Parser',
  14885. 'File' => 'SimplePie_File',
  14886. 'Sanitize' => 'SimplePie_Sanitize',
  14887. 'Item' => 'SimplePie_Item',
  14888. 'Author' => 'SimplePie_Author',
  14889. 'Category' => 'SimplePie_Category',
  14890. 'Enclosure' => 'SimplePie_Enclosure',
  14891. 'Caption' => 'SimplePie_Caption',
  14892. 'Copyright' => 'SimplePie_Copyright',
  14893. 'Credit' => 'SimplePie_Credit',
  14894. 'Rating' => 'SimplePie_Rating',
  14895. 'Restriction' => 'SimplePie_Restriction',
  14896. 'Content_Type_Sniffer' => 'SimplePie_Content_Type_Sniffer',
  14897. 'Source' => 'SimplePie_Source',
  14898. 'Misc' => 'SimplePie_Misc',
  14899. 'XML_Declaration_Parser' => 'SimplePie_XML_Declaration_Parser',
  14900. 'Parse_Date' => 'SimplePie_Parse_Date',
  14901. );
  14902. /**
  14903. * Class mapping
  14904. *
  14905. * @see register()
  14906. * @var array
  14907. */
  14908. protected $classes = array();
  14909. /**
  14910. * Legacy classes
  14911. *
  14912. * @see register()
  14913. * @var array
  14914. */
  14915. protected $legacy = array();
  14916. /**
  14917. * Constructor
  14918. *
  14919. * No-op
  14920. */
  14921. public function __construct() { }
  14922. /**
  14923. * Register a class
  14924. *
  14925. * @param string $type See {@see $default} for names
  14926. * @param string $class Class name, must subclass the corresponding default
  14927. * @param bool $legacy Whether to enable legacy support for this class
  14928. * @return bool Successfulness
  14929. */
  14930. public function register($type, $class, $legacy = false)
  14931. {
  14932. if (!is_subclass_of($class, $this->default[$type]))
  14933. {
  14934. return false;
  14935. }
  14936. $this->classes[$type] = $class;
  14937. if ($legacy)
  14938. {
  14939. $this->legacy[] = $class;
  14940. }
  14941. return true;
  14942. }
  14943. /**
  14944. * Get the class registered for a type
  14945. *
  14946. * Where possible, use {@see create()} or {@see call()} instead
  14947. *
  14948. * @param string $type
  14949. * @return string|null
  14950. */
  14951. public function get_class($type)
  14952. {
  14953. if (!empty($this->classes[$type]))
  14954. {
  14955. return $this->classes[$type];
  14956. }
  14957. if (!empty($this->default[$type]))
  14958. {
  14959. return $this->default[$type];
  14960. }
  14961. return null;
  14962. }
  14963. /**
  14964. * Create a new instance of a given type
  14965. *
  14966. * @param string $type
  14967. * @param array $parameters Parameters to pass to the constructor
  14968. * @return object Instance of class
  14969. */
  14970. public function &create($type, $parameters = array())
  14971. {
  14972. $class = $this->get_class($type);
  14973. if (in_array($class, $this->legacy))
  14974. {
  14975. switch ($type)
  14976. {
  14977. case 'locator':
  14978. // Legacy: file, timeout, useragent, file_class, max_checked_feeds, content_type_sniffer_class
  14979. // Specified: file, timeout, useragent, max_checked_feeds
  14980. $replacement = array($this->get_class('file'), $parameters[3], $this->get_class('content_type_sniffer'));
  14981. array_splice($parameters, 3, 1, $replacement);
  14982. break;
  14983. }
  14984. }
  14985. if (!method_exists($class, '__construct'))
  14986. {
  14987. $instance = new $class;
  14988. }
  14989. else
  14990. {
  14991. $reflector = new ReflectionClass($class);
  14992. $instance = $reflector->newInstanceArgs($parameters);
  14993. }
  14994. if (method_exists($instance, 'set_registry'))
  14995. {
  14996. $instance->set_registry($this);
  14997. }
  14998. return $instance;
  14999. }
  15000. /**
  15001. * Call a static method for a type
  15002. *
  15003. * @param string $type
  15004. * @param string $method
  15005. * @param array $parameters
  15006. * @return mixed
  15007. */
  15008. public function &call($type, $method, $parameters = array())
  15009. {
  15010. $class = $this->get_class($type);
  15011. if (in_array($class, $this->legacy))
  15012. {
  15013. switch ($type)
  15014. {
  15015. case 'Cache':
  15016. // For backwards compatibility with old non-static
  15017. // Cache::create() methods
  15018. if ($method === 'get_handler')
  15019. {
  15020. $result = @call_user_func_array(array($class, 'create'), $parameters);
  15021. return $result;
  15022. }
  15023. break;
  15024. }
  15025. }
  15026. $result = call_user_func_array(array($class, $method), $parameters);
  15027. return $result;
  15028. }
  15029. }
  15030. /**
  15031. * Handles `<media:restriction>` as defined in Media RSS
  15032. *
  15033. * Used by {@see SimplePie_Enclosure::get_restriction()} and {@see SimplePie_Enclosure::get_restrictions()}
  15034. *
  15035. * This class can be overloaded with {@see SimplePie::set_restriction_class()}
  15036. *
  15037. * @package SimplePie
  15038. * @subpackage API
  15039. */
  15040. class SimplePie_Restriction
  15041. {
  15042. /**
  15043. * Relationship ('allow'/'deny')
  15044. *
  15045. * @var string
  15046. * @see get_relationship()
  15047. */
  15048. var $relationship;
  15049. /**
  15050. * Type of restriction
  15051. *
  15052. * @var string
  15053. * @see get_type()
  15054. */
  15055. var $type;
  15056. /**
  15057. * Restricted values
  15058. *
  15059. * @var string
  15060. * @see get_value()
  15061. */
  15062. var $value;
  15063. /**
  15064. * Constructor, used to input the data
  15065. *
  15066. * For documentation on all the parameters, see the corresponding
  15067. * properties and their accessors
  15068. */
  15069. public function __construct($relationship = null, $type = null, $value = null)
  15070. {
  15071. $this->relationship = $relationship;
  15072. $this->type = $type;
  15073. $this->value = $value;
  15074. }
  15075. /**
  15076. * String-ified version
  15077. *
  15078. * @return string
  15079. */
  15080. public function __toString()
  15081. {
  15082. // There is no $this->data here
  15083. return md5(serialize($this));
  15084. }
  15085. /**
  15086. * Get the relationship
  15087. *
  15088. * @return string|null Either 'allow' or 'deny'
  15089. */
  15090. public function get_relationship()
  15091. {
  15092. if ($this->relationship !== null)
  15093. {
  15094. return $this->relationship;
  15095. }
  15096. else
  15097. {
  15098. return null;
  15099. }
  15100. }
  15101. /**
  15102. * Get the type
  15103. *
  15104. * @return string|null
  15105. */
  15106. public function get_type()
  15107. {
  15108. if ($this->type !== null)
  15109. {
  15110. return $this->type;
  15111. }
  15112. else
  15113. {
  15114. return null;
  15115. }
  15116. }
  15117. /**
  15118. * Get the list of restricted things
  15119. *
  15120. * @return string|null
  15121. */
  15122. public function get_value()
  15123. {
  15124. if ($this->value !== null)
  15125. {
  15126. return $this->value;
  15127. }
  15128. else
  15129. {
  15130. return null;
  15131. }
  15132. }
  15133. }
  15134. /**
  15135. * Used for data cleanup and post-processing
  15136. *
  15137. *
  15138. * This class can be overloaded with {@see SimplePie::set_sanitize_class()}
  15139. *
  15140. * @package SimplePie
  15141. * @todo Move to using an actual HTML parser (this will allow tags to be properly stripped, and to switch between HTML and XHTML), this will also make it easier to shorten a string while preserving HTML tags
  15142. */
  15143. class SimplePie_Sanitize
  15144. {
  15145. // Private vars
  15146. var $base;
  15147. // Options
  15148. var $remove_div = true;
  15149. var $image_handler = '';
  15150. var $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  15151. var $encode_instead_of_strip = false;
  15152. var $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  15153. var $strip_comments = false;
  15154. var $output_encoding = 'UTF-8';
  15155. var $enable_cache = true;
  15156. var $cache_location = './cache';
  15157. var $cache_name_function = 'md5';
  15158. var $timeout = 10;
  15159. var $useragent = '';
  15160. var $force_fsockopen = false;
  15161. var $replace_url_attributes = null;
  15162. public function __construct()
  15163. {
  15164. // Set defaults
  15165. $this->set_url_replacements(null);
  15166. }
  15167. public function remove_div($enable = true)
  15168. {
  15169. $this->remove_div = (bool) $enable;
  15170. }
  15171. public function set_image_handler($page = false)
  15172. {
  15173. if ($page)
  15174. {
  15175. $this->image_handler = (string) $page;
  15176. }
  15177. else
  15178. {
  15179. $this->image_handler = false;
  15180. }
  15181. }
  15182. public function set_registry(SimplePie_Registry $registry)
  15183. {
  15184. $this->registry = $registry;
  15185. }
  15186. public function pass_cache_data($enable_cache = true, $cache_location = './cache', $cache_name_function = 'md5', $cache_class = 'SimplePie_Cache')
  15187. {
  15188. if (isset($enable_cache))
  15189. {
  15190. $this->enable_cache = (bool) $enable_cache;
  15191. }
  15192. if ($cache_location)
  15193. {
  15194. $this->cache_location = (string) $cache_location;
  15195. }
  15196. if ($cache_name_function)
  15197. {
  15198. $this->cache_name_function = (string) $cache_name_function;
  15199. }
  15200. }
  15201. public function pass_file_data($file_class = 'SimplePie_File', $timeout = 10, $useragent = '', $force_fsockopen = false)
  15202. {
  15203. if ($timeout)
  15204. {
  15205. $this->timeout = (string) $timeout;
  15206. }
  15207. if ($useragent)
  15208. {
  15209. $this->useragent = (string) $useragent;
  15210. }
  15211. if ($force_fsockopen)
  15212. {
  15213. $this->force_fsockopen = (string) $force_fsockopen;
  15214. }
  15215. }
  15216. public function strip_htmltags($tags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style'))
  15217. {
  15218. if ($tags)
  15219. {
  15220. if (is_array($tags))
  15221. {
  15222. $this->strip_htmltags = $tags;
  15223. }
  15224. else
  15225. {
  15226. $this->strip_htmltags = explode(',', $tags);
  15227. }
  15228. }
  15229. else
  15230. {
  15231. $this->strip_htmltags = false;
  15232. }
  15233. }
  15234. public function encode_instead_of_strip($encode = false)
  15235. {
  15236. $this->encode_instead_of_strip = (bool) $encode;
  15237. }
  15238. public function strip_attributes($attribs = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc'))
  15239. {
  15240. if ($attribs)
  15241. {
  15242. if (is_array($attribs))
  15243. {
  15244. $this->strip_attributes = $attribs;
  15245. }
  15246. else
  15247. {
  15248. $this->strip_attributes = explode(',', $attribs);
  15249. }
  15250. }
  15251. else
  15252. {
  15253. $this->strip_attributes = false;
  15254. }
  15255. }
  15256. public function strip_comments($strip = false)
  15257. {
  15258. $this->strip_comments = (bool) $strip;
  15259. }
  15260. public function set_output_encoding($encoding = 'UTF-8')
  15261. {
  15262. $this->output_encoding = (string) $encoding;
  15263. }
  15264. /**
  15265. * Set element/attribute key/value pairs of HTML attributes
  15266. * containing URLs that need to be resolved relative to the feed
  15267. *
  15268. * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite,
  15269. * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite,
  15270. * |q|@cite
  15271. *
  15272. * @since 1.0
  15273. * @param array|null $element_attribute Element/attribute key/value pairs, null for default
  15274. */
  15275. public function set_url_replacements($element_attribute = null)
  15276. {
  15277. if ($element_attribute === null)
  15278. {
  15279. $element_attribute = array(
  15280. 'a' => 'href',
  15281. 'area' => 'href',
  15282. 'blockquote' => 'cite',
  15283. 'del' => 'cite',
  15284. 'form' => 'action',
  15285. 'img' => array(
  15286. 'longdesc',
  15287. 'src'
  15288. ),
  15289. 'input' => 'src',
  15290. 'ins' => 'cite',
  15291. 'q' => 'cite'
  15292. );
  15293. }
  15294. $this->replace_url_attributes = (array) $element_attribute;
  15295. }
  15296. public function sanitize($data, $type, $base = '')
  15297. {
  15298. $data = trim($data);
  15299. if ($data !== '' || $type & SIMPLEPIE_CONSTRUCT_IRI)
  15300. {
  15301. if ($type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML)
  15302. {
  15303. if (preg_match('/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*' . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>)/', $data))
  15304. {
  15305. $type |= SIMPLEPIE_CONSTRUCT_HTML;
  15306. }
  15307. else
  15308. {
  15309. $type |= SIMPLEPIE_CONSTRUCT_TEXT;
  15310. }
  15311. }
  15312. if ($type & SIMPLEPIE_CONSTRUCT_BASE64)
  15313. {
  15314. $data = base64_decode($data);
  15315. }
  15316. if ($type & (SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML))
  15317. {
  15318. $document = new DOMDocument();
  15319. $document->encoding = 'UTF-8';
  15320. $data = $this->preprocess($data, $type);
  15321. set_error_handler(array('SimplePie_Misc', 'silence_errors'));
  15322. $document->loadHTML($data);
  15323. restore_error_handler();
  15324. // Strip comments
  15325. if ($this->strip_comments)
  15326. {
  15327. $xpath = new DOMXPath($document);
  15328. $comments = $xpath->query('//comment()');
  15329. foreach ($comments as $comment)
  15330. {
  15331. $comment->parentNode->removeChild($comment);
  15332. }
  15333. }
  15334. // Strip out HTML tags and attributes that might cause various security problems.
  15335. // Based on recommendations by Mark Pilgrim at:
  15336. // http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely
  15337. if ($this->strip_htmltags)
  15338. {
  15339. foreach ($this->strip_htmltags as $tag)
  15340. {
  15341. $this->strip_tag($tag, $document, $type);
  15342. }
  15343. }
  15344. if ($this->strip_attributes)
  15345. {
  15346. foreach ($this->strip_attributes as $attrib)
  15347. {
  15348. $this->strip_attr($attrib, $document);
  15349. }
  15350. }
  15351. // Replace relative URLs
  15352. $this->base = $base;
  15353. foreach ($this->replace_url_attributes as $element => $attributes)
  15354. {
  15355. $this->replace_urls($document, $element, $attributes);
  15356. }
  15357. // If image handling (caching, etc.) is enabled, cache and rewrite all the image tags.
  15358. if (isset($this->image_handler) && ((string) $this->image_handler) !== '' && $this->enable_cache)
  15359. {
  15360. $images = $document->getElementsByTagName('img');
  15361. foreach ($images as $img)
  15362. {
  15363. if ($img->hasAttribute('src'))
  15364. {
  15365. $image_url = call_user_func($this->cache_name_function, $img->getAttribute('src'));
  15366. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, $image_url, 'spi'));
  15367. if ($cache->load())
  15368. {
  15369. $img->setAttribute('src', $this->image_handler . $image_url);
  15370. }
  15371. else
  15372. {
  15373. $file = $this->registry->create('File', array($img['attribs']['src']['data'], $this->timeout, 5, array('X-FORWARDED-FOR' => $_SERVER['REMOTE_ADDR']), $this->useragent, $this->force_fsockopen));
  15374. $headers = $file->headers;
  15375. if ($file->success && ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  15376. {
  15377. if ($cache->save(array('headers' => $file->headers, 'body' => $file->body)))
  15378. {
  15379. $img->setAttribute('src', $this->image_handler . $image_url);
  15380. }
  15381. else
  15382. {
  15383. trigger_error("$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  15384. }
  15385. }
  15386. }
  15387. }
  15388. }
  15389. }
  15390. // Remove the DOCTYPE
  15391. // Seems to cause segfaulting if we don't do this
  15392. if ($document->firstChild instanceof DOMDocumentType)
  15393. {
  15394. $document->removeChild($document->firstChild);
  15395. }
  15396. // Move everything from the body to the root
  15397. $real_body = $document->getElementsByTagName('body')->item(0)->childNodes->item(0);
  15398. $document->replaceChild($real_body, $document->firstChild);
  15399. // Finally, convert to a HTML string
  15400. $data = trim($document->saveHTML());
  15401. if ($this->remove_div)
  15402. {
  15403. $data = preg_replace('/^<div' . SIMPLEPIE_PCRE_XML_ATTRIBUTE . '>/', '', $data);
  15404. $data = preg_replace('/<\/div>$/', '', $data);
  15405. }
  15406. else
  15407. {
  15408. $data = preg_replace('/^<div' . SIMPLEPIE_PCRE_XML_ATTRIBUTE . '>/', '<div>', $data);
  15409. }
  15410. }
  15411. if ($type & SIMPLEPIE_CONSTRUCT_IRI)
  15412. {
  15413. $absolute = $this->registry->call('Misc', 'absolutize_url', array($data, $base));
  15414. if ($absolute !== false)
  15415. {
  15416. $data = $absolute;
  15417. }
  15418. }
  15419. if ($type & (SIMPLEPIE_CONSTRUCT_TEXT | SIMPLEPIE_CONSTRUCT_IRI))
  15420. {
  15421. $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
  15422. }
  15423. if ($this->output_encoding !== 'UTF-8')
  15424. {
  15425. $data = $this->registry->call('Misc', 'change_encoding', array($data, 'UTF-8', $this->output_encoding));
  15426. }
  15427. }
  15428. return $data;
  15429. }
  15430. protected function preprocess($html, $type)
  15431. {
  15432. $ret = '';
  15433. if ($type & ~SIMPLEPIE_CONSTRUCT_XHTML)
  15434. {
  15435. // Atom XHTML constructs are wrapped with a div by default
  15436. // Note: No protection if $html contains a stray </div>!
  15437. $html = '<div>' . $html . '</div>';
  15438. $ret .= '<!DOCTYPE html>';
  15439. $content_type = 'text/html';
  15440. }
  15441. else
  15442. {
  15443. $ret .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
  15444. $content_type = 'application/xhtml+xml';
  15445. }
  15446. $ret .= '<html><head>';
  15447. $ret .= '<meta http-equiv="Content-Type" content="' . $content_type . '; charset=utf-8" />';
  15448. $ret .= '</head><body>' . $html . '</body></html>';
  15449. return $ret;
  15450. }
  15451. public function replace_urls($document, $tag, $attributes)
  15452. {
  15453. if (!is_array($attributes))
  15454. {
  15455. $attributes = array($attributes);
  15456. }
  15457. if (!is_array($this->strip_htmltags) || !in_array($tag, $this->strip_htmltags))
  15458. {
  15459. $elements = $document->getElementsByTagName($tag);
  15460. foreach ($elements as $element)
  15461. {
  15462. foreach ($attributes as $attribute)
  15463. {
  15464. if ($element->hasAttribute($attribute))
  15465. {
  15466. $value = $this->registry->call('Misc', 'absolutize_url', array($element->getAttribute($attribute), $this->base));
  15467. if ($value !== false)
  15468. {
  15469. $element->setAttribute($attribute, $value);
  15470. }
  15471. }
  15472. }
  15473. }
  15474. }
  15475. }
  15476. public function do_strip_htmltags($match)
  15477. {
  15478. if ($this->encode_instead_of_strip)
  15479. {
  15480. if (isset($match[4]) && !in_array(strtolower($match[1]), array('script', 'style')))
  15481. {
  15482. $match[1] = htmlspecialchars($match[1], ENT_COMPAT, 'UTF-8');
  15483. $match[2] = htmlspecialchars($match[2], ENT_COMPAT, 'UTF-8');
  15484. return "&lt;$match[1]$match[2]&gt;$match[3]&lt;/$match[1]&gt;";
  15485. }
  15486. else
  15487. {
  15488. return htmlspecialchars($match[0], ENT_COMPAT, 'UTF-8');
  15489. }
  15490. }
  15491. elseif (isset($match[4]) && !in_array(strtolower($match[1]), array('script', 'style')))
  15492. {
  15493. return $match[4];
  15494. }
  15495. else
  15496. {
  15497. return '';
  15498. }
  15499. }
  15500. protected function strip_tag($tag, $document, $type)
  15501. {
  15502. $xpath = new DOMXPath($document);
  15503. $elements = $xpath->query('body//' . $tag);
  15504. if ($this->encode_instead_of_strip)
  15505. {
  15506. foreach ($elements as $element)
  15507. {
  15508. $fragment = $document->createDocumentFragment();
  15509. // For elements which aren't script or style, include the tag itself
  15510. if (!in_array($tag, array('script', 'style')))
  15511. {
  15512. $text = '<' . $tag;
  15513. if ($element->hasAttributes())
  15514. {
  15515. $attrs = array();
  15516. foreach ($element->attributes as $name => $attr)
  15517. {
  15518. $value = $attr->value;
  15519. // In XHTML, empty values should never exist, so we repeat the value
  15520. if (empty($value) && ($type & SIMPLEPIE_CONSTRUCT_XHTML))
  15521. {
  15522. $value = $name;
  15523. }
  15524. // For HTML, empty is fine
  15525. elseif (empty($value) && ($type & SIMPLEPIE_CONSTRUCT_HTML))
  15526. {
  15527. $attrs[] = $name;
  15528. continue;
  15529. }
  15530. // Standard attribute text
  15531. $attrs[] = $name . '="' . $attr->value . '"';
  15532. }
  15533. $text .= ' ' . implode(' ', $attrs);
  15534. }
  15535. $text .= '>';
  15536. $fragment->appendChild(new DOMText($text));
  15537. }
  15538. $number = $element->childNodes->length;
  15539. for ($i = $number; $i > 0; $i--)
  15540. {
  15541. $child = $element->childNodes->item(0);
  15542. $fragment->appendChild($child);
  15543. }
  15544. if (!in_array($tag, array('script', 'style')))
  15545. {
  15546. $fragment->appendChild(new DOMText('</' . $tag . '>'));
  15547. }
  15548. $element->parentNode->replaceChild($fragment, $element);
  15549. }
  15550. return;
  15551. }
  15552. elseif (in_array($tag, array('script', 'style')))
  15553. {
  15554. foreach ($elements as $element)
  15555. {
  15556. $element->parentNode->removeChild($element);
  15557. }
  15558. return;
  15559. }
  15560. else
  15561. {
  15562. foreach ($elements as $element)
  15563. {
  15564. $fragment = $document->createDocumentFragment();
  15565. $number = $element->childNodes->length;
  15566. for ($i = $number; $i > 0; $i--)
  15567. {
  15568. $child = $element->childNodes->item(0);
  15569. $fragment->appendChild($child);
  15570. }
  15571. $element->parentNode->replaceChild($fragment, $element);
  15572. }
  15573. }
  15574. }
  15575. protected function strip_attr($attrib, $document)
  15576. {
  15577. $xpath = new DOMXPath($document);
  15578. $elements = $xpath->query('//*[@' . $attrib . ']');
  15579. foreach ($elements as $element)
  15580. {
  15581. $element->removeAttribute($attrib);
  15582. }
  15583. }
  15584. }
  15585. /**
  15586. * Handles `<atom:source>`
  15587. *
  15588. * Used by {@see SimplePie_Item::get_source()}
  15589. *
  15590. * This class can be overloaded with {@see SimplePie::set_source_class()}
  15591. *
  15592. * @package SimplePie
  15593. * @subpackage API
  15594. */
  15595. class SimplePie_Source
  15596. {
  15597. var $item;
  15598. var $data = array();
  15599. protected $registry;
  15600. public function __construct($item, $data)
  15601. {
  15602. $this->item = $item;
  15603. $this->data = $data;
  15604. }
  15605. public function set_registry(SimplePie_Registry $registry)
  15606. {
  15607. $this->registry = $registry;
  15608. }
  15609. public function __toString()
  15610. {
  15611. return md5(serialize($this->data));
  15612. }
  15613. public function get_source_tags($namespace, $tag)
  15614. {
  15615. if (isset($this->data['child'][$namespace][$tag]))
  15616. {
  15617. return $this->data['child'][$namespace][$tag];
  15618. }
  15619. else
  15620. {
  15621. return null;
  15622. }
  15623. }
  15624. public function get_base($element = array())
  15625. {
  15626. return $this->item->get_base($element);
  15627. }
  15628. public function sanitize($data, $type, $base = '')
  15629. {
  15630. return $this->item->sanitize($data, $type, $base);
  15631. }
  15632. public function get_item()
  15633. {
  15634. return $this->item;
  15635. }
  15636. public function get_title()
  15637. {
  15638. if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'title'))
  15639. {
  15640. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  15641. }
  15642. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'title'))
  15643. {
  15644. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  15645. }
  15646. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  15647. {
  15648. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  15649. }
  15650. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  15651. {
  15652. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  15653. }
  15654. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  15655. {
  15656. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  15657. }
  15658. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  15659. {
  15660. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15661. }
  15662. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  15663. {
  15664. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15665. }
  15666. else
  15667. {
  15668. return null;
  15669. }
  15670. }
  15671. public function get_category($key = 0)
  15672. {
  15673. $categories = $this->get_categories();
  15674. if (isset($categories[$key]))
  15675. {
  15676. return $categories[$key];
  15677. }
  15678. else
  15679. {
  15680. return null;
  15681. }
  15682. }
  15683. public function get_categories()
  15684. {
  15685. $categories = array();
  15686. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'category') as $category)
  15687. {
  15688. $term = null;
  15689. $scheme = null;
  15690. $label = null;
  15691. if (isset($category['attribs']['']['term']))
  15692. {
  15693. $term = $this->sanitize($category['attribs']['']['term'], SIMPLEPIE_CONSTRUCT_TEXT);
  15694. }
  15695. if (isset($category['attribs']['']['scheme']))
  15696. {
  15697. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  15698. }
  15699. if (isset($category['attribs']['']['label']))
  15700. {
  15701. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  15702. }
  15703. $categories[] = $this->registry->create('Category', array($term, $scheme, $label));
  15704. }
  15705. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'category') as $category)
  15706. {
  15707. // This is really the label, but keep this as the term also for BC.
  15708. // Label will also work on retrieving because that falls back to term.
  15709. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15710. if (isset($category['attribs']['']['domain']))
  15711. {
  15712. $scheme = $this->sanitize($category['attribs']['']['domain'], SIMPLEPIE_CONSTRUCT_TEXT);
  15713. }
  15714. else
  15715. {
  15716. $scheme = null;
  15717. }
  15718. $categories[] = $this->registry->create('Category', array($term, $scheme, null));
  15719. }
  15720. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11, 'subject') as $category)
  15721. {
  15722. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  15723. }
  15724. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10, 'subject') as $category)
  15725. {
  15726. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  15727. }
  15728. if (!empty($categories))
  15729. {
  15730. return array_unique($categories);
  15731. }
  15732. else
  15733. {
  15734. return null;
  15735. }
  15736. }
  15737. public function get_author($key = 0)
  15738. {
  15739. $authors = $this->get_authors();
  15740. if (isset($authors[$key]))
  15741. {
  15742. return $authors[$key];
  15743. }
  15744. else
  15745. {
  15746. return null;
  15747. }
  15748. }
  15749. public function get_authors()
  15750. {
  15751. $authors = array();
  15752. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author') as $author)
  15753. {
  15754. $name = null;
  15755. $uri = null;
  15756. $email = null;
  15757. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  15758. {
  15759. $name = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15760. }
  15761. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  15762. {
  15763. $uri = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  15764. }
  15765. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  15766. {
  15767. $email = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15768. }
  15769. if ($name !== null || $email !== null || $uri !== null)
  15770. {
  15771. $authors[] = $this->registry->create('Author', array($name, $uri, $email));
  15772. }
  15773. }
  15774. if ($author = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'author'))
  15775. {
  15776. $name = null;
  15777. $url = null;
  15778. $email = null;
  15779. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  15780. {
  15781. $name = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15782. }
  15783. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  15784. {
  15785. $url = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  15786. }
  15787. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  15788. {
  15789. $email = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15790. }
  15791. if ($name !== null || $email !== null || $url !== null)
  15792. {
  15793. $authors[] = $this->registry->create('Author', array($name, $url, $email));
  15794. }
  15795. }
  15796. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11, 'creator') as $author)
  15797. {
  15798. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  15799. }
  15800. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10, 'creator') as $author)
  15801. {
  15802. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  15803. }
  15804. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'author') as $author)
  15805. {
  15806. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  15807. }
  15808. if (!empty($authors))
  15809. {
  15810. return array_unique($authors);
  15811. }
  15812. else
  15813. {
  15814. return null;
  15815. }
  15816. }
  15817. public function get_contributor($key = 0)
  15818. {
  15819. $contributors = $this->get_contributors();
  15820. if (isset($contributors[$key]))
  15821. {
  15822. return $contributors[$key];
  15823. }
  15824. else
  15825. {
  15826. return null;
  15827. }
  15828. }
  15829. public function get_contributors()
  15830. {
  15831. $contributors = array();
  15832. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'contributor') as $contributor)
  15833. {
  15834. $name = null;
  15835. $uri = null;
  15836. $email = null;
  15837. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  15838. {
  15839. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15840. }
  15841. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  15842. {
  15843. $uri = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  15844. }
  15845. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  15846. {
  15847. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15848. }
  15849. if ($name !== null || $email !== null || $uri !== null)
  15850. {
  15851. $contributors[] = $this->registry->create('Author', array($name, $uri, $email));
  15852. }
  15853. }
  15854. foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'contributor') as $contributor)
  15855. {
  15856. $name = null;
  15857. $url = null;
  15858. $email = null;
  15859. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  15860. {
  15861. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15862. }
  15863. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  15864. {
  15865. $url = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  15866. }
  15867. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  15868. {
  15869. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15870. }
  15871. if ($name !== null || $email !== null || $url !== null)
  15872. {
  15873. $contributors[] = $this->registry->create('Author', array($name, $url, $email));
  15874. }
  15875. }
  15876. if (!empty($contributors))
  15877. {
  15878. return array_unique($contributors);
  15879. }
  15880. else
  15881. {
  15882. return null;
  15883. }
  15884. }
  15885. public function get_link($key = 0, $rel = 'alternate')
  15886. {
  15887. $links = $this->get_links($rel);
  15888. if (isset($links[$key]))
  15889. {
  15890. return $links[$key];
  15891. }
  15892. else
  15893. {
  15894. return null;
  15895. }
  15896. }
  15897. /**
  15898. * Added for parity between the parent-level and the item/entry-level.
  15899. */
  15900. public function get_permalink()
  15901. {
  15902. return $this->get_link(0);
  15903. }
  15904. public function get_links($rel = 'alternate')
  15905. {
  15906. if (!isset($this->data['links']))
  15907. {
  15908. $this->data['links'] = array();
  15909. if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link'))
  15910. {
  15911. foreach ($links as $link)
  15912. {
  15913. if (isset($link['attribs']['']['href']))
  15914. {
  15915. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  15916. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  15917. }
  15918. }
  15919. }
  15920. if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link'))
  15921. {
  15922. foreach ($links as $link)
  15923. {
  15924. if (isset($link['attribs']['']['href']))
  15925. {
  15926. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  15927. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  15928. }
  15929. }
  15930. }
  15931. if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link'))
  15932. {
  15933. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  15934. }
  15935. if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link'))
  15936. {
  15937. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  15938. }
  15939. if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link'))
  15940. {
  15941. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  15942. }
  15943. $keys = array_keys($this->data['links']);
  15944. foreach ($keys as $key)
  15945. {
  15946. if ($this->registry->call('Misc', 'is_isegment_nz_nc', array($key)))
  15947. {
  15948. if (isset($this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]))
  15949. {
  15950. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]);
  15951. $this->data['links'][$key] =& $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key];
  15952. }
  15953. else
  15954. {
  15955. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] =& $this->data['links'][$key];
  15956. }
  15957. }
  15958. elseif (substr($key, 0, 41) === SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY)
  15959. {
  15960. $this->data['links'][substr($key, 41)] =& $this->data['links'][$key];
  15961. }
  15962. $this->data['links'][$key] = array_unique($this->data['links'][$key]);
  15963. }
  15964. }
  15965. if (isset($this->data['links'][$rel]))
  15966. {
  15967. return $this->data['links'][$rel];
  15968. }
  15969. else
  15970. {
  15971. return null;
  15972. }
  15973. }
  15974. public function get_description()
  15975. {
  15976. if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'subtitle'))
  15977. {
  15978. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  15979. }
  15980. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'tagline'))
  15981. {
  15982. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  15983. }
  15984. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'description'))
  15985. {
  15986. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  15987. }
  15988. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'description'))
  15989. {
  15990. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  15991. }
  15992. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'description'))
  15993. {
  15994. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  15995. }
  15996. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11, 'description'))
  15997. {
  15998. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  15999. }
  16000. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10, 'description'))
  16001. {
  16002. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  16003. }
  16004. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'summary'))
  16005. {
  16006. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  16007. }
  16008. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'subtitle'))
  16009. {
  16010. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  16011. }
  16012. else
  16013. {
  16014. return null;
  16015. }
  16016. }
  16017. public function get_copyright()
  16018. {
  16019. if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'rights'))
  16020. {
  16021. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  16022. }
  16023. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'copyright'))
  16024. {
  16025. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  16026. }
  16027. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'copyright'))
  16028. {
  16029. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  16030. }
  16031. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11, 'rights'))
  16032. {
  16033. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  16034. }
  16035. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10, 'rights'))
  16036. {
  16037. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  16038. }
  16039. else
  16040. {
  16041. return null;
  16042. }
  16043. }
  16044. public function get_language()
  16045. {
  16046. if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'language'))
  16047. {
  16048. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  16049. }
  16050. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11, 'language'))
  16051. {
  16052. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  16053. }
  16054. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10, 'language'))
  16055. {
  16056. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  16057. }
  16058. elseif (isset($this->data['xml_lang']))
  16059. {
  16060. return $this->sanitize($this->data['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  16061. }
  16062. else
  16063. {
  16064. return null;
  16065. }
  16066. }
  16067. public function get_latitude()
  16068. {
  16069. if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lat'))
  16070. {
  16071. return (float) $return[0]['data'];
  16072. }
  16073. elseif (($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  16074. {
  16075. return (float) $match[1];
  16076. }
  16077. else
  16078. {
  16079. return null;
  16080. }
  16081. }
  16082. public function get_longitude()
  16083. {
  16084. if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'long'))
  16085. {
  16086. return (float) $return[0]['data'];
  16087. }
  16088. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lon'))
  16089. {
  16090. return (float) $return[0]['data'];
  16091. }
  16092. elseif (($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  16093. {
  16094. return (float) $match[2];
  16095. }
  16096. else
  16097. {
  16098. return null;
  16099. }
  16100. }
  16101. public function get_image_url()
  16102. {
  16103. if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'image'))
  16104. {
  16105. return $this->sanitize($return[0]['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI);
  16106. }
  16107. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'logo'))
  16108. {
  16109. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  16110. }
  16111. elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'icon'))
  16112. {
  16113. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  16114. }
  16115. else
  16116. {
  16117. return null;
  16118. }
  16119. }
  16120. }
  16121. /**
  16122. * Parses the XML Declaration
  16123. *
  16124. * @package SimplePie
  16125. * @subpackage Parsing
  16126. */
  16127. class SimplePie_XML_Declaration_Parser
  16128. {
  16129. /**
  16130. * XML Version
  16131. *
  16132. * @access public
  16133. * @var string
  16134. */
  16135. var $version = '1.0';
  16136. /**
  16137. * Encoding
  16138. *
  16139. * @access public
  16140. * @var string
  16141. */
  16142. var $encoding = 'UTF-8';
  16143. /**
  16144. * Standalone
  16145. *
  16146. * @access public
  16147. * @var bool
  16148. */
  16149. var $standalone = false;
  16150. /**
  16151. * Current state of the state machine
  16152. *
  16153. * @access private
  16154. * @var string
  16155. */
  16156. var $state = 'before_version_name';
  16157. /**
  16158. * Input data
  16159. *
  16160. * @access private
  16161. * @var string
  16162. */
  16163. var $data = '';
  16164. /**
  16165. * Input data length (to avoid calling strlen() everytime this is needed)
  16166. *
  16167. * @access private
  16168. * @var int
  16169. */
  16170. var $data_length = 0;
  16171. /**
  16172. * Current position of the pointer
  16173. *
  16174. * @var int
  16175. * @access private
  16176. */
  16177. var $position = 0;
  16178. /**
  16179. * Create an instance of the class with the input data
  16180. *
  16181. * @access public
  16182. * @param string $data Input data
  16183. */
  16184. public function __construct($data)
  16185. {
  16186. $this->data = $data;
  16187. $this->data_length = strlen($this->data);
  16188. }
  16189. /**
  16190. * Parse the input data
  16191. *
  16192. * @access public
  16193. * @return bool true on success, false on failure
  16194. */
  16195. public function parse()
  16196. {
  16197. while ($this->state && $this->state !== 'emit' && $this->has_data())
  16198. {
  16199. $state = $this->state;
  16200. $this->$state();
  16201. }
  16202. $this->data = '';
  16203. if ($this->state === 'emit')
  16204. {
  16205. return true;
  16206. }
  16207. else
  16208. {
  16209. $this->version = '';
  16210. $this->encoding = '';
  16211. $this->standalone = '';
  16212. return false;
  16213. }
  16214. }
  16215. /**
  16216. * Check whether there is data beyond the pointer
  16217. *
  16218. * @access private
  16219. * @return bool true if there is further data, false if not
  16220. */
  16221. public function has_data()
  16222. {
  16223. return (bool) ($this->position < $this->data_length);
  16224. }
  16225. /**
  16226. * Advance past any whitespace
  16227. *
  16228. * @return int Number of whitespace characters passed
  16229. */
  16230. public function skip_whitespace()
  16231. {
  16232. $whitespace = strspn($this->data, "\x09\x0A\x0D\x20", $this->position);
  16233. $this->position += $whitespace;
  16234. return $whitespace;
  16235. }
  16236. /**
  16237. * Read value
  16238. */
  16239. public function get_value()
  16240. {
  16241. $quote = substr($this->data, $this->position, 1);
  16242. if ($quote === '"' || $quote === "'")
  16243. {
  16244. $this->position++;
  16245. $len = strcspn($this->data, $quote, $this->position);
  16246. if ($this->has_data())
  16247. {
  16248. $value = substr($this->data, $this->position, $len);
  16249. $this->position += $len + 1;
  16250. return $value;
  16251. }
  16252. }
  16253. return false;
  16254. }
  16255. public function before_version_name()
  16256. {
  16257. if ($this->skip_whitespace())
  16258. {
  16259. $this->state = 'version_name';
  16260. }
  16261. else
  16262. {
  16263. $this->state = false;
  16264. }
  16265. }
  16266. public function version_name()
  16267. {
  16268. if (substr($this->data, $this->position, 7) === 'version')
  16269. {
  16270. $this->position += 7;
  16271. $this->skip_whitespace();
  16272. $this->state = 'version_equals';
  16273. }
  16274. else
  16275. {
  16276. $this->state = false;
  16277. }
  16278. }
  16279. public function version_equals()
  16280. {
  16281. if (substr($this->data, $this->position, 1) === '=')
  16282. {
  16283. $this->position++;
  16284. $this->skip_whitespace();
  16285. $this->state = 'version_value';
  16286. }
  16287. else
  16288. {
  16289. $this->state = false;
  16290. }
  16291. }
  16292. public function version_value()
  16293. {
  16294. if ($this->version = $this->get_value())
  16295. {
  16296. $this->skip_whitespace();
  16297. if ($this->has_data())
  16298. {
  16299. $this->state = 'encoding_name';
  16300. }
  16301. else
  16302. {
  16303. $this->state = 'emit';
  16304. }
  16305. }
  16306. else
  16307. {
  16308. $this->state = false;
  16309. }
  16310. }
  16311. public function encoding_name()
  16312. {
  16313. if (substr($this->data, $this->position, 8) === 'encoding')
  16314. {
  16315. $this->position += 8;
  16316. $this->skip_whitespace();
  16317. $this->state = 'encoding_equals';
  16318. }
  16319. else
  16320. {
  16321. $this->state = 'standalone_name';
  16322. }
  16323. }
  16324. public function encoding_equals()
  16325. {
  16326. if (substr($this->data, $this->position, 1) === '=')
  16327. {
  16328. $this->position++;
  16329. $this->skip_whitespace();
  16330. $this->state = 'encoding_value';
  16331. }
  16332. else
  16333. {
  16334. $this->state = false;
  16335. }
  16336. }
  16337. public function encoding_value()
  16338. {
  16339. if ($this->encoding = $this->get_value())
  16340. {
  16341. $this->skip_whitespace();
  16342. if ($this->has_data())
  16343. {
  16344. $this->state = 'standalone_name';
  16345. }
  16346. else
  16347. {
  16348. $this->state = 'emit';
  16349. }
  16350. }
  16351. else
  16352. {
  16353. $this->state = false;
  16354. }
  16355. }
  16356. public function standalone_name()
  16357. {
  16358. if (substr($this->data, $this->position, 10) === 'standalone')
  16359. {
  16360. $this->position += 10;
  16361. $this->skip_whitespace();
  16362. $this->state = 'standalone_equals';
  16363. }
  16364. else
  16365. {
  16366. $this->state = false;
  16367. }
  16368. }
  16369. public function standalone_equals()
  16370. {
  16371. if (substr($this->data, $this->position, 1) === '=')
  16372. {
  16373. $this->position++;
  16374. $this->skip_whitespace();
  16375. $this->state = 'standalone_value';
  16376. }
  16377. else
  16378. {
  16379. $this->state = false;
  16380. }
  16381. }
  16382. public function standalone_value()
  16383. {
  16384. if ($standalone = $this->get_value())
  16385. {
  16386. switch ($standalone)
  16387. {
  16388. case 'yes':
  16389. $this->standalone = true;
  16390. break;
  16391. case 'no':
  16392. $this->standalone = false;
  16393. break;
  16394. default:
  16395. $this->state = false;
  16396. return;
  16397. }
  16398. $this->skip_whitespace();
  16399. if ($this->has_data())
  16400. {
  16401. $this->state = false;
  16402. }
  16403. else
  16404. {
  16405. $this->state = 'emit';
  16406. }
  16407. }
  16408. else
  16409. {
  16410. $this->state = false;
  16411. }
  16412. }
  16413. }