PageRenderTime 33ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/libraries/Simplepie.php

https://github.com/JamieLomas/pyrocms
PHP | 2213 lines | 1780 code | 390 blank | 43 comment | 150 complexity | 5111fb9463f20c3acff29d8507bf1be4 MD5 | raw file
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2010, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @version 1.3-dev
  37. * @copyright 2004-2010 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Geoffrey Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ SimplePie
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. * @todo phpDoc comments
  44. */
  45. class SimplePie extends SimplePie_Core
  46. {
  47. }
  48. class SimplePie_Author
  49. {
  50. var $name;
  51. var $link;
  52. var $email;
  53. public function __construct($name = null, $link = null, $email = null)
  54. {
  55. $this->name = $name;
  56. $this->link = $link;
  57. $this->email = $email;
  58. }
  59. public function __toString()
  60. {
  61. return md5(serialize($this));
  62. }
  63. public function get_name()
  64. {
  65. if ($this->name !== null)
  66. {
  67. return $this->name;
  68. }
  69. else
  70. {
  71. return null;
  72. }
  73. }
  74. public function get_link()
  75. {
  76. if ($this->link !== null)
  77. {
  78. return $this->link;
  79. }
  80. else
  81. {
  82. return null;
  83. }
  84. }
  85. public function get_email()
  86. {
  87. if ($this->email !== null)
  88. {
  89. return $this->email;
  90. }
  91. else
  92. {
  93. return null;
  94. }
  95. }
  96. }
  97. class SimplePie_Cache_DB
  98. {
  99. public function prepare_simplepie_object_for_cache($data)
  100. {
  101. $items = $data->get_items();
  102. $items_by_id = array();
  103. if (!empty($items))
  104. {
  105. foreach ($items as $item)
  106. {
  107. $items_by_id[$item->get_id()] = $item;
  108. }
  109. if (count($items_by_id) !== count($items))
  110. {
  111. $items_by_id = array();
  112. foreach ($items as $item)
  113. {
  114. $items_by_id[$item->get_id(true)] = $item;
  115. }
  116. }
  117. if (isset($data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]))
  118. {
  119. $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0];
  120. }
  121. elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]))
  122. {
  123. $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0];
  124. }
  125. elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]))
  126. {
  127. $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0];
  128. }
  129. elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['channel'][0]))
  130. {
  131. $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['channel'][0];
  132. }
  133. else
  134. {
  135. $channel = null;
  136. }
  137. if ($channel !== null)
  138. {
  139. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry']))
  140. {
  141. unset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry']);
  142. }
  143. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['entry']))
  144. {
  145. unset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['entry']);
  146. }
  147. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item']))
  148. {
  149. unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item']);
  150. }
  151. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item']))
  152. {
  153. unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item']);
  154. }
  155. if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_20]['item']))
  156. {
  157. unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_20]['item']);
  158. }
  159. }
  160. if (isset($data->data['items']))
  161. {
  162. unset($data->data['items']);
  163. }
  164. if (isset($data->data['ordered_items']))
  165. {
  166. unset($data->data['ordered_items']);
  167. }
  168. }
  169. return array(serialize($data->data), $items_by_id);
  170. }
  171. }
  172. class SimplePie_Cache_File
  173. {
  174. var $location;
  175. var $filename;
  176. var $extension;
  177. var $name;
  178. public function __construct($location, $filename, $extension)
  179. {
  180. $this->location = $location;
  181. $this->filename = $filename;
  182. $this->extension = $extension;
  183. $this->name = "$this->location/$this->filename.$this->extension";
  184. }
  185. public function save($data)
  186. {
  187. if (file_exists($this->name) && is_writeable($this->name) || file_exists($this->location) && is_writeable($this->location))
  188. {
  189. if (is_a($data, 'SimplePie'))
  190. {
  191. $data = $data->data;
  192. }
  193. $data = serialize($data);
  194. return (bool) file_put_contents($this->name, $data);
  195. }
  196. return false;
  197. }
  198. public function load()
  199. {
  200. if (file_exists($this->name) && is_readable($this->name))
  201. {
  202. return unserialize(file_get_contents($this->name));
  203. }
  204. return false;
  205. }
  206. public function mtime()
  207. {
  208. if (file_exists($this->name))
  209. {
  210. return filemtime($this->name);
  211. }
  212. return false;
  213. }
  214. public function touch()
  215. {
  216. if (file_exists($this->name))
  217. {
  218. return touch($this->name);
  219. }
  220. return false;
  221. }
  222. public function unlink()
  223. {
  224. if (file_exists($this->name))
  225. {
  226. return unlink($this->name);
  227. }
  228. return false;
  229. }
  230. }
  231. class SimplePie_Cache_MySQL extends SimplePie_Cache_DB
  232. {
  233. var $mysql;
  234. var $options;
  235. var $id;
  236. public function __construct($mysql_location, $name, $extension)
  237. {
  238. $host = $mysql_location->get_host();
  239. if (stripos($host, 'unix(') === 0 && substr($host, -1) === ')')
  240. {
  241. $server = ':' . substr($host, 5, -1);
  242. }
  243. else
  244. {
  245. $server = $host;
  246. if ($mysql_location->get_port() !== null)
  247. {
  248. $server .= ':' . $mysql_location->get_port();
  249. }
  250. }
  251. if (strpos($mysql_location->get_userinfo(), ':') !== false)
  252. {
  253. list($username, $password) = explode(':', $mysql_location->get_userinfo(), 2);
  254. }
  255. else
  256. {
  257. $username = $mysql_location->get_userinfo();
  258. $password = null;
  259. }
  260. if ($this->mysql = mysql_connect($server, $username, $password))
  261. {
  262. $this->id = $name . $extension;
  263. $this->options = SimplePie_Misc::parse_str($mysql_location->get_query());
  264. if (!isset($this->options['prefix'][0]))
  265. {
  266. $this->options['prefix'][0] = '';
  267. }
  268. if (mysql_select_db(ltrim($mysql_location->get_path(), '/'))
  269. && mysql_query('SET NAMES utf8')
  270. && ($query = mysql_unbuffered_query('SHOW TABLES')))
  271. {
  272. $db = array();
  273. while ($row = mysql_fetch_row($query))
  274. {
  275. $db[] = $row[0];
  276. }
  277. if (!in_array($this->options['prefix'][0] . 'cache_data', $db))
  278. {
  279. if (!mysql_query('CREATE TABLE `' . $this->options['prefix'][0] . '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)))'))
  280. {
  281. $this->mysql = null;
  282. }
  283. }
  284. if (!in_array($this->options['prefix'][0] . 'items', $db))
  285. {
  286. if (!mysql_query('CREATE TABLE `' . $this->options['prefix'][0] . '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)))'))
  287. {
  288. $this->mysql = null;
  289. }
  290. }
  291. }
  292. else
  293. {
  294. $this->mysql = null;
  295. }
  296. }
  297. }
  298. public function save($data)
  299. {
  300. if ($this->mysql)
  301. {
  302. $feed_id = "'" . mysql_real_escape_string($this->id) . "'";
  303. if (is_a($data, 'SimplePie'))
  304. {
  305. $data = clone $data;
  306. $prepared = $this->prepare_simplepie_object_for_cache($data);
  307. if ($query = mysql_query('SELECT `id` FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = ' . $feed_id, $this->mysql))
  308. {
  309. if (mysql_num_rows($query))
  310. {
  311. $items = count($prepared[1]);
  312. if ($items)
  313. {
  314. $sql = 'UPDATE `' . $this->options['prefix'][0] . 'cache_data` SET `items` = ' . $items . ', `data` = \'' . mysql_real_escape_string($prepared[0]) . '\', `mtime` = ' . time() . ' WHERE `id` = ' . $feed_id;
  315. }
  316. else
  317. {
  318. $sql = 'UPDATE `' . $this->options['prefix'][0] . 'cache_data` SET `data` = \'' . mysql_real_escape_string($prepared[0]) . '\', `mtime` = ' . time() . ' WHERE `id` = ' . $feed_id;
  319. }
  320. if (!mysql_query($sql, $this->mysql))
  321. {
  322. return false;
  323. }
  324. }
  325. elseif (!mysql_query('INSERT INTO `' . $this->options['prefix'][0] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(' . $feed_id . ', ' . count($prepared[1]) . ', \'' . mysql_real_escape_string($prepared[0]) . '\', ' . time() . ')', $this->mysql))
  326. {
  327. return false;
  328. }
  329. $ids = array_keys($prepared[1]);
  330. if (!empty($ids))
  331. {
  332. foreach ($ids as $id)
  333. {
  334. $database_ids[] = mysql_real_escape_string($id);
  335. }
  336. if ($query = mysql_unbuffered_query('SELECT `id` FROM `' . $this->options['prefix'][0] . 'items` WHERE `id` = \'' . implode('\' OR `id` = \'', $database_ids) . '\' AND `feed_id` = ' . $feed_id, $this->mysql))
  337. {
  338. $existing_ids = array();
  339. while ($row = mysql_fetch_row($query))
  340. {
  341. $existing_ids[] = $row[0];
  342. }
  343. $new_ids = array_diff($ids, $existing_ids);
  344. foreach ($new_ids as $new_id)
  345. {
  346. if (!($date = $prepared[1][$new_id]->get_date('U')))
  347. {
  348. $date = time();
  349. }
  350. if (!mysql_query('INSERT INTO `' . $this->options['prefix'][0] . 'items` (`feed_id`, `id`, `data`, `posted`) VALUES(' . $feed_id . ', \'' . mysql_real_escape_string($new_id) . '\', \'' . mysql_real_escape_string(serialize($prepared[1][$new_id]->data)) . '\', ' . $date . ')', $this->mysql))
  351. {
  352. return false;
  353. }
  354. }
  355. return true;
  356. }
  357. }
  358. else
  359. {
  360. return true;
  361. }
  362. }
  363. }
  364. elseif ($query = mysql_query('SELECT `id` FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = ' . $feed_id, $this->mysql))
  365. {
  366. if (mysql_num_rows($query))
  367. {
  368. if (mysql_query('UPDATE `' . $this->options['prefix'][0] . 'cache_data` SET `items` = 0, `data` = \'' . mysql_real_escape_string(serialize($data)) . '\', `mtime` = ' . time() . ' WHERE `id` = ' . $feed_id, $this->mysql))
  369. {
  370. return true;
  371. }
  372. }
  373. elseif (mysql_query('INSERT INTO `' . $this->options['prefix'][0] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(\'' . mysql_real_escape_string($this->id) . '\', 0, \'' . mysql_real_escape_string(serialize($data)) . '\', ' . time() . ')', $this->mysql))
  374. {
  375. return true;
  376. }
  377. }
  378. }
  379. return false;
  380. }
  381. public function load()
  382. {
  383. if ($this->mysql && ($query = mysql_query('SELECT `items`, `data` FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)) && ($row = mysql_fetch_row($query)))
  384. {
  385. $data = unserialize($row[1]);
  386. if (isset($this->options['items'][0]))
  387. {
  388. $items = (int) $this->options['items'][0];
  389. }
  390. else
  391. {
  392. $items = (int) $row[0];
  393. }
  394. if ($items !== 0)
  395. {
  396. if (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]))
  397. {
  398. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0];
  399. }
  400. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]))
  401. {
  402. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0];
  403. }
  404. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]))
  405. {
  406. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0];
  407. }
  408. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]))
  409. {
  410. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0];
  411. }
  412. else
  413. {
  414. $feed = null;
  415. }
  416. if ($feed !== null)
  417. {
  418. $sql = 'SELECT `data` FROM `' . $this->options['prefix'][0] . 'items` WHERE `feed_id` = \'' . mysql_real_escape_string($this->id) . '\' ORDER BY `posted` DESC';
  419. if ($items > 0)
  420. {
  421. $sql .= ' LIMIT ' . $items;
  422. }
  423. if ($query = mysql_unbuffered_query($sql, $this->mysql))
  424. {
  425. while ($row = mysql_fetch_row($query))
  426. {
  427. $feed['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry'][] = unserialize($row[0]);
  428. }
  429. }
  430. else
  431. {
  432. return false;
  433. }
  434. }
  435. }
  436. return $data;
  437. }
  438. return false;
  439. }
  440. public function mtime()
  441. {
  442. if ($this->mysql && ($query = mysql_query('SELECT `mtime` FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)) && ($row = mysql_fetch_row($query)))
  443. {
  444. return $row[0];
  445. }
  446. else
  447. {
  448. return false;
  449. }
  450. }
  451. public function touch()
  452. {
  453. if ($this->mysql && ($query = mysql_query('UPDATE `' . $this->options['prefix'][0] . 'cache_data` SET `mtime` = ' . time() . ' WHERE `id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)) && mysql_affected_rows($this->mysql))
  454. {
  455. return true;
  456. }
  457. else
  458. {
  459. return false;
  460. }
  461. }
  462. public function unlink()
  463. {
  464. if ($this->mysql && ($query = mysql_query('DELETE FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)) && ($query2 = mysql_query('DELETE FROM `' . $this->options['prefix'][0] . 'items` WHERE `feed_id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)))
  465. {
  466. return true;
  467. }
  468. else
  469. {
  470. return false;
  471. }
  472. }
  473. }
  474. class SimplePie_Cache
  475. {
  476. private function __construct()
  477. {
  478. trigger_error('Please call SimplePie_Cache::create() instead of the constructor', E_USER_ERROR);
  479. }
  480. public static function create($location, $filename, $extension)
  481. {
  482. $location_iri = new SimplePie_IRI($location);
  483. switch ($location_iri->get_scheme())
  484. {
  485. case 'mysql':
  486. if (extension_loaded('mysql'))
  487. {
  488. return new SimplePie_Cache_MySQL($location_iri, $filename, $extension);
  489. }
  490. break;
  491. default:
  492. return new SimplePie_Cache_File($location, $filename, $extension);
  493. }
  494. }
  495. }
  496. class SimplePie_Caption
  497. {
  498. var $type;
  499. var $lang;
  500. var $startTime;
  501. var $endTime;
  502. var $text;
  503. public function __construct($type = null, $lang = null, $startTime = null, $endTime = null, $text = null)
  504. {
  505. $this->type = $type;
  506. $this->lang = $lang;
  507. $this->startTime = $startTime;
  508. $this->endTime = $endTime;
  509. $this->text = $text;
  510. }
  511. public function __toString()
  512. {
  513. return md5(serialize($this));
  514. }
  515. public function get_endtime()
  516. {
  517. if ($this->endTime !== null)
  518. {
  519. return $this->endTime;
  520. }
  521. else
  522. {
  523. return null;
  524. }
  525. }
  526. public function get_language()
  527. {
  528. if ($this->lang !== null)
  529. {
  530. return $this->lang;
  531. }
  532. else
  533. {
  534. return null;
  535. }
  536. }
  537. public function get_starttime()
  538. {
  539. if ($this->startTime !== null)
  540. {
  541. return $this->startTime;
  542. }
  543. else
  544. {
  545. return null;
  546. }
  547. }
  548. public function get_text()
  549. {
  550. if ($this->text !== null)
  551. {
  552. return $this->text;
  553. }
  554. else
  555. {
  556. return null;
  557. }
  558. }
  559. public function get_type()
  560. {
  561. if ($this->type !== null)
  562. {
  563. return $this->type;
  564. }
  565. else
  566. {
  567. return null;
  568. }
  569. }
  570. }
  571. class SimplePie_Category
  572. {
  573. var $term;
  574. var $scheme;
  575. var $label;
  576. public function __construct($term = null, $scheme = null, $label = null)
  577. {
  578. $this->term = $term;
  579. $this->scheme = $scheme;
  580. $this->label = $label;
  581. }
  582. public function __toString()
  583. {
  584. return md5(serialize($this));
  585. }
  586. public function get_term()
  587. {
  588. if ($this->term !== null)
  589. {
  590. return $this->term;
  591. }
  592. else
  593. {
  594. return null;
  595. }
  596. }
  597. public function get_scheme()
  598. {
  599. if ($this->scheme !== null)
  600. {
  601. return $this->scheme;
  602. }
  603. else
  604. {
  605. return null;
  606. }
  607. }
  608. public function get_label()
  609. {
  610. if ($this->label !== null)
  611. {
  612. return $this->label;
  613. }
  614. else
  615. {
  616. return $this->get_term();
  617. }
  618. }
  619. }
  620. class SimplePie_Content_Type_Sniffer
  621. {
  622. var $file;
  623. public function __construct($file)
  624. {
  625. $this->file = $file;
  626. }
  627. public function get_type()
  628. {
  629. if (isset($this->file->headers['content-type']))
  630. {
  631. if (!isset($this->file->headers['content-encoding'])
  632. && ($this->file->headers['content-type'] === 'text/plain'
  633. || $this->file->headers['content-type'] === 'text/plain; charset=ISO-8859-1'
  634. || $this->file->headers['content-type'] === 'text/plain; charset=iso-8859-1'))
  635. {
  636. return $this->text_or_binary();
  637. }
  638. if (($pos = strpos($this->file->headers['content-type'], ';')) !== false)
  639. {
  640. $official = substr($this->file->headers['content-type'], 0, $pos);
  641. }
  642. else
  643. {
  644. $official = $this->file->headers['content-type'];
  645. }
  646. $official = strtolower($official);
  647. if ($official === 'unknown/unknown'
  648. || $official === 'application/unknown')
  649. {
  650. return $this->unknown();
  651. }
  652. elseif (substr($official, -4) === '+xml'
  653. || $official === 'text/xml'
  654. || $official === 'application/xml')
  655. {
  656. return $official;
  657. }
  658. elseif (substr($official, 0, 6) === 'image/')
  659. {
  660. if ($return = $this->image())
  661. {
  662. return $return;
  663. }
  664. else
  665. {
  666. return $official;
  667. }
  668. }
  669. elseif ($official === 'text/html')
  670. {
  671. return $this->feed_or_html();
  672. }
  673. else
  674. {
  675. return $official;
  676. }
  677. }
  678. else
  679. {
  680. return $this->unknown();
  681. }
  682. }
  683. public function text_or_binary()
  684. {
  685. if (substr($this->file->body, 0, 2) === "\xFE\xFF"
  686. || substr($this->file->body, 0, 2) === "\xFF\xFE"
  687. || substr($this->file->body, 0, 4) === "\x00\x00\xFE\xFF"
  688. || substr($this->file->body, 0, 3) === "\xEF\xBB\xBF")
  689. {
  690. return 'text/plain';
  691. }
  692. elseif (preg_match('/[\x00-\x08\x0E-\x1A\x1C-\x1F]/', $this->file->body))
  693. {
  694. return 'application/octect-stream';
  695. }
  696. else
  697. {
  698. return 'text/plain';
  699. }
  700. }
  701. public function unknown()
  702. {
  703. $ws = strspn($this->file->body, "\x09\x0A\x0B\x0C\x0D\x20");
  704. if (strtolower(substr($this->file->body, $ws, 14)) === '<!doctype html'
  705. || strtolower(substr($this->file->body, $ws, 5)) === '<html'
  706. || strtolower(substr($this->file->body, $ws, 7)) === '<script')
  707. {
  708. return 'text/html';
  709. }
  710. elseif (substr($this->file->body, 0, 5) === '%PDF-')
  711. {
  712. return 'application/pdf';
  713. }
  714. elseif (substr($this->file->body, 0, 11) === '%!PS-Adobe-')
  715. {
  716. return 'application/postscript';
  717. }
  718. elseif (substr($this->file->body, 0, 6) === 'GIF87a'
  719. || substr($this->file->body, 0, 6) === 'GIF89a')
  720. {
  721. return 'image/gif';
  722. }
  723. elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
  724. {
  725. return 'image/png';
  726. }
  727. elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF")
  728. {
  729. return 'image/jpeg';
  730. }
  731. elseif (substr($this->file->body, 0, 2) === "\x42\x4D")
  732. {
  733. return 'image/bmp';
  734. }
  735. else
  736. {
  737. return $this->text_or_binary();
  738. }
  739. }
  740. public function image()
  741. {
  742. if (substr($this->file->body, 0, 6) === 'GIF87a'
  743. || substr($this->file->body, 0, 6) === 'GIF89a')
  744. {
  745. return 'image/gif';
  746. }
  747. elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
  748. {
  749. return 'image/png';
  750. }
  751. elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF")
  752. {
  753. return 'image/jpeg';
  754. }
  755. elseif (substr($this->file->body, 0, 2) === "\x42\x4D")
  756. {
  757. return 'image/bmp';
  758. }
  759. else
  760. {
  761. return false;
  762. }
  763. }
  764. public function feed_or_html()
  765. {
  766. $len = strlen($this->file->body);
  767. $pos = strspn($this->file->body, "\x09\x0A\x0D\x20");
  768. while ($pos < $len)
  769. {
  770. switch ($this->file->body[$pos])
  771. {
  772. case "\x09":
  773. case "\x0A":
  774. case "\x0D":
  775. case "\x20":
  776. $pos += strspn($this->file->body, "\x09\x0A\x0D\x20", $pos);
  777. continue 2;
  778. case '<':
  779. $pos++;
  780. break;
  781. default:
  782. return 'text/html';
  783. }
  784. if (substr($this->file->body, $pos, 3) === '!--')
  785. {
  786. $pos += 3;
  787. if ($pos < $len && ($pos = strpos($this->file->body, '-->', $pos)) !== false)
  788. {
  789. $pos += 3;
  790. }
  791. else
  792. {
  793. return 'text/html';
  794. }
  795. }
  796. elseif (substr($this->file->body, $pos, 1) === '!')
  797. {
  798. if ($pos < $len && ($pos = strpos($this->file->body, '>', $pos)) !== false)
  799. {
  800. $pos++;
  801. }
  802. else
  803. {
  804. return 'text/html';
  805. }
  806. }
  807. elseif (substr($this->file->body, $pos, 1) === '?')
  808. {
  809. if ($pos < $len && ($pos = strpos($this->file->body, '?>', $pos)) !== false)
  810. {
  811. $pos += 2;
  812. }
  813. else
  814. {
  815. return 'text/html';
  816. }
  817. }
  818. elseif (substr($this->file->body, $pos, 3) === 'rss'
  819. || substr($this->file->body, $pos, 7) === 'rdf:RDF')
  820. {
  821. return 'application/rss+xml';
  822. }
  823. elseif (substr($this->file->body, $pos, 4) === 'feed')
  824. {
  825. return 'application/atom+xml';
  826. }
  827. else
  828. {
  829. return 'text/html';
  830. }
  831. }
  832. return 'text/html';
  833. }
  834. }
  835. class SimplePie_Copyright
  836. {
  837. var $url;
  838. var $label;
  839. public function __construct($url = null, $label = null)
  840. {
  841. $this->url = $url;
  842. $this->label = $label;
  843. }
  844. public function __toString()
  845. {
  846. return md5(serialize($this));
  847. }
  848. public function get_url()
  849. {
  850. if ($this->url !== null)
  851. {
  852. return $this->url;
  853. }
  854. else
  855. {
  856. return null;
  857. }
  858. }
  859. public function get_attribution()
  860. {
  861. if ($this->label !== null)
  862. {
  863. return $this->label;
  864. }
  865. else
  866. {
  867. return null;
  868. }
  869. }
  870. }
  871. define('SIMPLEPIE_NAME', 'SimplePie');
  872. define('SIMPLEPIE_VERSION', '1.3-dev');
  873. define('SIMPLEPIE_BUILD', gmdate('YmdHis', SimplePie_Misc::parse_date(substr('$Date$', 7, 25)) ? SimplePie_Misc::parse_date(substr('$Date$', 7, 25)) : filemtime(__FILE__)));
  874. define('SIMPLEPIE_URL', 'http://simplepie.org');
  875. define('SIMPLEPIE_USERAGENT', SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION . ' (Feed Parser; ' . SIMPLEPIE_URL . '; Allow like Gecko) Build/' . SIMPLEPIE_BUILD);
  876. define('SIMPLEPIE_LINKBACK', '<a href="' . SIMPLEPIE_URL . '" title="' . SIMPLEPIE_NAME . ' ' . SIMPLEPIE_VERSION . '">' . SIMPLEPIE_NAME . '</a>');
  877. define('SIMPLEPIE_LOCATOR_NONE', 0);
  878. define('SIMPLEPIE_LOCATOR_AUTODISCOVERY', 1);
  879. define('SIMPLEPIE_LOCATOR_LOCAL_EXTENSION', 2);
  880. define('SIMPLEPIE_LOCATOR_LOCAL_BODY', 4);
  881. define('SIMPLEPIE_LOCATOR_REMOTE_EXTENSION', 8);
  882. define('SIMPLEPIE_LOCATOR_REMOTE_BODY', 16);
  883. define('SIMPLEPIE_LOCATOR_ALL', 31);
  884. define('SIMPLEPIE_TYPE_NONE', 0);
  885. define('SIMPLEPIE_TYPE_RSS_090', 1);
  886. define('SIMPLEPIE_TYPE_RSS_091_NETSCAPE', 2);
  887. define('SIMPLEPIE_TYPE_RSS_091_USERLAND', 4);
  888. define('SIMPLEPIE_TYPE_RSS_091', 6);
  889. define('SIMPLEPIE_TYPE_RSS_092', 8);
  890. define('SIMPLEPIE_TYPE_RSS_093', 16);
  891. define('SIMPLEPIE_TYPE_RSS_094', 32);
  892. define('SIMPLEPIE_TYPE_RSS_10', 64);
  893. define('SIMPLEPIE_TYPE_RSS_20', 128);
  894. define('SIMPLEPIE_TYPE_RSS_RDF', 65);
  895. define('SIMPLEPIE_TYPE_RSS_SYNDICATION', 190);
  896. define('SIMPLEPIE_TYPE_RSS_ALL', 255);
  897. define('SIMPLEPIE_TYPE_ATOM_03', 256);
  898. define('SIMPLEPIE_TYPE_ATOM_10', 512);
  899. define('SIMPLEPIE_TYPE_ATOM_ALL', 768);
  900. define('SIMPLEPIE_TYPE_ALL', 1023);
  901. define('SIMPLEPIE_CONSTRUCT_NONE', 0);
  902. define('SIMPLEPIE_CONSTRUCT_TEXT', 1);
  903. define('SIMPLEPIE_CONSTRUCT_HTML', 2);
  904. define('SIMPLEPIE_CONSTRUCT_XHTML', 4);
  905. define('SIMPLEPIE_CONSTRUCT_BASE64', 8);
  906. define('SIMPLEPIE_CONSTRUCT_IRI', 16);
  907. define('SIMPLEPIE_CONSTRUCT_MAYBE_HTML', 32);
  908. define('SIMPLEPIE_CONSTRUCT_ALL', 63);
  909. define('SIMPLEPIE_SAME_CASE', 1);
  910. define('SIMPLEPIE_LOWERCASE', 2);
  911. define('SIMPLEPIE_UPPERCASE', 4);
  912. 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]*');
  913. define('SIMPLEPIE_PCRE_XML_ATTRIBUTE', '((?:\s+(?:(?:[^\s:]+:)?[^\s:]+)\s*=\s*(?:"(?:[^"]*)"|\'(?:[^\']*)\'))*)\s*');
  914. define('SIMPLEPIE_NAMESPACE_XML', 'http://www.w3.org/XML/1998/namespace');
  915. define('SIMPLEPIE_NAMESPACE_ATOM_10', 'http://www.w3.org/2005/Atom');
  916. define('SIMPLEPIE_NAMESPACE_ATOM_03', 'http://purl.org/atom/ns#');
  917. define('SIMPLEPIE_NAMESPACE_RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  918. define('SIMPLEPIE_NAMESPACE_RSS_090', 'http://my.netscape.com/rdf/simple/0.9/');
  919. define('SIMPLEPIE_NAMESPACE_RSS_10', 'http://purl.org/rss/1.0/');
  920. define('SIMPLEPIE_NAMESPACE_RSS_10_MODULES_CONTENT', 'http://purl.org/rss/1.0/modules/content/');
  921. define('SIMPLEPIE_NAMESPACE_RSS_20', '');
  922. define('SIMPLEPIE_NAMESPACE_DC_10', 'http://purl.org/dc/elements/1.0/');
  923. define('SIMPLEPIE_NAMESPACE_DC_11', 'http://purl.org/dc/elements/1.1/');
  924. define('SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
  925. define('SIMPLEPIE_NAMESPACE_GEORSS', 'http://www.georss.org/georss');
  926. define('SIMPLEPIE_NAMESPACE_MEDIARSS', 'http://search.yahoo.com/mrss/');
  927. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG', 'http://search.yahoo.com/mrss');
  928. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2', 'http://video.search.yahoo.com/mrss');
  929. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3', 'http://video.search.yahoo.com/mrss/');
  930. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4', 'http://www.rssboard.org/media-rss');
  931. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5', 'http://www.rssboard.org/media-rss/');
  932. define('SIMPLEPIE_NAMESPACE_ITUNES', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
  933. define('SIMPLEPIE_NAMESPACE_XHTML', 'http://www.w3.org/1999/xhtml');
  934. define('SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY', 'http://www.iana.org/assignments/relation/');
  935. define('SIMPLEPIE_PHP5', version_compare(PHP_VERSION, '5.0.0', '>='));
  936. define('SIMPLEPIE_FILE_SOURCE_NONE', 0);
  937. define('SIMPLEPIE_FILE_SOURCE_REMOTE', 1);
  938. define('SIMPLEPIE_FILE_SOURCE_LOCAL', 2);
  939. define('SIMPLEPIE_FILE_SOURCE_FSOCKOPEN', 4);
  940. define('SIMPLEPIE_FILE_SOURCE_CURL', 8);
  941. define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);
  942. class SimplePie_Core
  943. {
  944. public $data = array();
  945. public $error;
  946. public $sanitize;
  947. public $useragent = SIMPLEPIE_USERAGENT;
  948. public $feed_url;
  949. public $file;
  950. public $raw_data;
  951. public $timeout = 10;
  952. public $force_fsockopen = false;
  953. public $force_feed = false;
  954. public $xml_dump = false;
  955. public $cache = true;
  956. public $cache_duration = 3600;
  957. public $autodiscovery_cache_duration = 604800;
  958. public $cache_location = './cache';
  959. public $cache_name_function = 'md5';
  960. public $order_by_date = true;
  961. public $input_encoding = false;
  962. public $autodiscovery = SIMPLEPIE_LOCATOR_ALL;
  963. public $cache_class = 'SimplePie_Cache';
  964. public $locator_class = 'SimplePie_Locator';
  965. public $parser_class = 'SimplePie_Parser';
  966. public $file_class = 'SimplePie_File';
  967. public $item_class = 'SimplePie_Item';
  968. public $author_class = 'SimplePie_Author';
  969. public $category_class = 'SimplePie_Category';
  970. public $enclosure_class = 'SimplePie_Enclosure';
  971. public $caption_class = 'SimplePie_Caption';
  972. public $copyright_class = 'SimplePie_Copyright';
  973. public $credit_class = 'SimplePie_Credit';
  974. public $rating_class = 'SimplePie_Rating';
  975. public $restriction_class = 'SimplePie_Restriction';
  976. public $content_type_sniffer_class = 'SimplePie_Content_Type_Sniffer';
  977. public $source_class = 'SimplePie_Source';
  978. public $javascript = 'js';
  979. public $max_checked_feeds = 10;
  980. public $all_discovered_feeds = array();
  981. public $image_handler = '';
  982. public $multifeed_url = array();
  983. public $multifeed_objects = array();
  984. public $config_settings = null;
  985. public $item_limit = 0;
  986. public $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  987. public $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  988. public function __construct()
  989. {
  990. if (version_compare(PHP_VERSION, '5.0', '<'))
  991. {
  992. trigger_error('PHP 4.x is no longer supported. Please upgrade to PHP 5.2 or newer.');
  993. die();
  994. }
  995. $this->sanitize = new SimplePie_Sanitize();
  996. if (func_num_args() > 0)
  997. {
  998. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_location() directly.');
  999. }
  1000. }
  1001. public function __toString()
  1002. {
  1003. return md5(serialize($this->data));
  1004. }
  1005. public function __destruct()
  1006. {
  1007. if ((version_compare(PHP_VERSION, '5.3', '<') || !gc_enabled()) && !ini_get('zend.ze1_compatibility_mode'))
  1008. {
  1009. if (!empty($this->data['items']))
  1010. {
  1011. foreach ($this->data['items'] as $item)
  1012. {
  1013. $item->__destruct();
  1014. }
  1015. unset($item, $this->data['items']);
  1016. }
  1017. if (!empty($this->data['ordered_items']))
  1018. {
  1019. foreach ($this->data['ordered_items'] as $item)
  1020. {
  1021. $item->__destruct();
  1022. }
  1023. unset($item, $this->data['ordered_items']);
  1024. }
  1025. }
  1026. }
  1027. public function force_feed($enable = false)
  1028. {
  1029. $this->force_feed = (bool) $enable;
  1030. }
  1031. public function set_feed_url($url)
  1032. {
  1033. if (is_array($url))
  1034. {
  1035. $this->multifeed_url = array();
  1036. foreach ($url as $value)
  1037. {
  1038. $this->multifeed_url[] = SimplePie_Misc::fix_protocol($value, 1);
  1039. }
  1040. }
  1041. else
  1042. {
  1043. $this->feed_url = SimplePie_Misc::fix_protocol($url, 1);
  1044. }
  1045. }
  1046. public function set_file(&$file)
  1047. {
  1048. if (is_a($file, 'SimplePie_File'))
  1049. {
  1050. $this->feed_url = $file->url;
  1051. $this->file =& $file;
  1052. return true;
  1053. }
  1054. return false;
  1055. }
  1056. public function set_raw_data($data)
  1057. {
  1058. $this->raw_data = $data;
  1059. }
  1060. public function set_timeout($timeout = 10)
  1061. {
  1062. $this->timeout = (int) $timeout;
  1063. }
  1064. public function force_fsockopen($enable = false)
  1065. {
  1066. $this->force_fsockopen = (bool) $enable;
  1067. }
  1068. public function enable_cache($enable = true)
  1069. {
  1070. $this->cache = (bool) $enable;
  1071. }
  1072. public function set_cache_duration($seconds = 3600)
  1073. {
  1074. $this->cache_duration = (int) $seconds;
  1075. }
  1076. public function set_autodiscovery_cache_duration($seconds = 604800)
  1077. {
  1078. $this->autodiscovery_cache_duration = (int) $seconds;
  1079. }
  1080. public function set_cache_location($location = './cache')
  1081. {
  1082. $this->cache_location = (string) $location;
  1083. }
  1084. public function enable_order_by_date($enable = true)
  1085. {
  1086. $this->order_by_date = (bool) $enable;
  1087. }
  1088. public function set_input_encoding($encoding = false)
  1089. {
  1090. if ($encoding)
  1091. {
  1092. $this->input_encoding = (string) $encoding;
  1093. }
  1094. else
  1095. {
  1096. $this->input_encoding = false;
  1097. }
  1098. }
  1099. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  1100. {
  1101. $this->autodiscovery = (int) $level;
  1102. }
  1103. public function set_cache_class($class = 'SimplePie_Cache')
  1104. {
  1105. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Cache'))
  1106. {
  1107. $this->cache_class = $class;
  1108. return true;
  1109. }
  1110. return false;
  1111. }
  1112. public function set_locator_class($class = 'SimplePie_Locator')
  1113. {
  1114. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Locator'))
  1115. {
  1116. $this->locator_class = $class;
  1117. return true;
  1118. }
  1119. return false;
  1120. }
  1121. public function set_parser_class($class = 'SimplePie_Parser')
  1122. {
  1123. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Parser'))
  1124. {
  1125. $this->parser_class = $class;
  1126. return true;
  1127. }
  1128. return false;
  1129. }
  1130. public function set_file_class($class = 'SimplePie_File')
  1131. {
  1132. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_File'))
  1133. {
  1134. $this->file_class = $class;
  1135. return true;
  1136. }
  1137. return false;
  1138. }
  1139. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  1140. {
  1141. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Sanitize'))
  1142. {
  1143. $this->sanitize = new $class();
  1144. return true;
  1145. }
  1146. return false;
  1147. }
  1148. public function set_item_class($class = 'SimplePie_Item')
  1149. {
  1150. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Item'))
  1151. {
  1152. $this->item_class = $class;
  1153. return true;
  1154. }
  1155. return false;
  1156. }
  1157. public function set_author_class($class = 'SimplePie_Author')
  1158. {
  1159. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Author'))
  1160. {
  1161. $this->author_class = $class;
  1162. return true;
  1163. }
  1164. return false;
  1165. }
  1166. public function set_category_class($class = 'SimplePie_Category')
  1167. {
  1168. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Category'))
  1169. {
  1170. $this->category_class = $class;
  1171. return true;
  1172. }
  1173. return false;
  1174. }
  1175. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  1176. {
  1177. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Enclosure'))
  1178. {
  1179. $this->enclosure_class = $class;
  1180. return true;
  1181. }
  1182. return false;
  1183. }
  1184. public function set_caption_class($class = 'SimplePie_Caption')
  1185. {
  1186. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Caption'))
  1187. {
  1188. $this->caption_class = $class;
  1189. return true;
  1190. }
  1191. return false;
  1192. }
  1193. public function set_copyright_class($class = 'SimplePie_Copyright')
  1194. {
  1195. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Copyright'))
  1196. {
  1197. $this->copyright_class = $class;
  1198. return true;
  1199. }
  1200. return false;
  1201. }
  1202. public function set_credit_class($class = 'SimplePie_Credit')
  1203. {
  1204. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Credit'))
  1205. {
  1206. $this->credit_class = $class;
  1207. return true;
  1208. }
  1209. return false;
  1210. }
  1211. public function set_rating_class($class = 'SimplePie_Rating')
  1212. {
  1213. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Rating'))
  1214. {
  1215. $this->rating_class = $class;
  1216. return true;
  1217. }
  1218. return false;
  1219. }
  1220. public function set_restriction_class($class = 'SimplePie_Restriction')
  1221. {
  1222. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Restriction'))
  1223. {
  1224. $this->restriction_class = $class;
  1225. return true;
  1226. }
  1227. return false;
  1228. }
  1229. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  1230. {
  1231. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Content_Type_Sniffer'))
  1232. {
  1233. $this->content_type_sniffer_class = $class;
  1234. return true;
  1235. }
  1236. return false;
  1237. }
  1238. public function set_source_class($class = 'SimplePie_Source')
  1239. {
  1240. if (SimplePie_Misc::is_subclass_of($class, 'SimplePie_Source'))
  1241. {
  1242. $this->source_class = $class;
  1243. return true;
  1244. }
  1245. return false;
  1246. }
  1247. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  1248. {
  1249. $this->useragent = (string) $ua;
  1250. }
  1251. public function set_cache_name_function($function = 'md5')
  1252. {
  1253. if (is_callable($function))
  1254. {
  1255. $this->cache_name_function = $function;
  1256. }
  1257. }
  1258. public function set_javascript($get = 'js')
  1259. {
  1260. if ($get)
  1261. {
  1262. $this->javascript = (string) $get;
  1263. }
  1264. else
  1265. {
  1266. $this->javascript = false;
  1267. }
  1268. }
  1269. public function set_stupidly_fast($set = false)
  1270. {
  1271. if ($set)
  1272. {
  1273. $this->enable_order_by_date(false);
  1274. $this->remove_div(false);
  1275. $this->strip_comments(false);
  1276. $this->strip_htmltags(false);
  1277. $this->strip_attributes(false);
  1278. $this->set_image_handler(false);
  1279. }
  1280. }
  1281. public function set_max_checked_feeds($max = 10)
  1282. {
  1283. $this->max_checked_feeds = (int) $max;
  1284. }
  1285. public function remove_div($enable = true)
  1286. {
  1287. $this->sanitize->remove_div($enable);
  1288. }
  1289. public function strip_htmltags($tags = '', $encode = null)
  1290. {
  1291. if ($tags === '')
  1292. {
  1293. $tags = $this->strip_htmltags;
  1294. }
  1295. $this->sanitize->strip_htmltags($tags);
  1296. if ($encode !== null)
  1297. {
  1298. $this->sanitize->encode_instead_of_strip($tags);
  1299. }
  1300. }
  1301. public function encode_instead_of_strip($enable = true)
  1302. {
  1303. $this->sanitize->encode_instead_of_strip($enable);
  1304. }
  1305. public function strip_attributes($attribs = '')
  1306. {
  1307. if ($attribs === '')
  1308. {
  1309. $attribs = $this->strip_attributes;
  1310. }
  1311. $this->sanitize->strip_attributes($attribs);
  1312. }
  1313. public function set_output_encoding($encoding = 'UTF-8')
  1314. {
  1315. $this->sanitize->set_output_encoding($encoding);
  1316. }
  1317. public function strip_comments($strip = false)
  1318. {
  1319. $this->sanitize->strip_comments($strip);
  1320. }
  1321. public function set_url_replacements($element_attribute = array('a' => 'href', 'area' => 'href', 'blockquote' => 'cite', 'del' => 'cite', 'form' => 'action', 'img' => array('longdesc', 'src'), 'input' => 'src', 'ins' => 'cite', 'q' => 'cite'))
  1322. {
  1323. $this->sanitize->set_url_replacements($element_attribute);
  1324. }
  1325. public function set_image_handler($page = false, $qs = 'i')
  1326. {
  1327. if ($page !== false)
  1328. {
  1329. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1330. }
  1331. else
  1332. {
  1333. $this->image_handler = '';
  1334. }
  1335. }
  1336. public function set_item_limit($limit = 0)
  1337. {
  1338. $this->item_limit = (int) $limit;
  1339. }
  1340. public function init()
  1341. {
  1342. if ((function_exists('version_compare') && version_compare(PHP_VERSION, '5.0', '<')) || !extension_loaded('xml') || !extension_loaded('pcre'))
  1343. {
  1344. return false;
  1345. }
  1346. elseif (!extension_loaded('xmlreader'))
  1347. {
  1348. static $xml_is_sane = null;
  1349. if ($xml_is_sane === null)
  1350. {
  1351. $parser_check = xml_parser_create();
  1352. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1353. xml_parser_free($parser_check);
  1354. $xml_is_sane = isset($values[0]['value']);
  1355. }
  1356. if (!$xml_is_sane)
  1357. {
  1358. return false;
  1359. }
  1360. }
  1361. if (isset($_GET[$this->javascript]))
  1362. {
  1363. SimplePie_Misc::output_javascript();
  1364. exit;
  1365. }
  1366. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->cache_class);
  1367. $this->sanitize->pass_file_data($this->file_class, $this->timeout, $this->useragent, $this->force_fsockopen);
  1368. if ($this->feed_url !== null || $this->raw_data !== null)
  1369. {
  1370. $this->error = null;
  1371. $this->data = array();
  1372. $this->multifeed_objects = array();
  1373. $cache = false;
  1374. if ($this->feed_url !== null)
  1375. {
  1376. $parsed_feed_url = SimplePie_Misc::parse_url($this->feed_url);
  1377. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1378. {
  1379. $cache = call_user_func(array($this->cache_class, 'create'), $this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc');
  1380. }
  1381. if ($cache && !$this->xml_dump)
  1382. {
  1383. $this->data = $cache->load();
  1384. if (!empty($this->data))
  1385. {
  1386. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1387. {
  1388. $cache->unlink();
  1389. $this->data = array();
  1390. }
  1391. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1392. {
  1393. $cache = false;
  1394. $this->data = array();
  1395. }
  1396. elseif (isset($this->data['feed_url']))
  1397. {
  1398. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1399. {
  1400. if ($this->data['feed_url'] === $this->data['url'])
  1401. {
  1402. $cache->unlink();
  1403. $this->data = array();
  1404. }
  1405. else
  1406. {
  1407. $this->set_feed_url($this->data['feed_url']);
  1408. return $this->init();
  1409. }
  1410. }
  1411. }
  1412. elseif ($cache->mtime() + $this->cache_duration < time())
  1413. {
  1414. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1415. {
  1416. $headers = array();
  1417. if (isset($this->data['headers']['last-modified']))
  1418. {
  1419. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1420. }
  1421. if (isset($this->data['headers']['etag']))
  1422. {
  1423. $headers['if-none-match'] = '"' . $this->data['headers']['etag'] . '"';
  1424. }
  1425. $file = new $this->file_class($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen);
  1426. if ($file->success)
  1427. {
  1428. if ($file->status_code === 304)
  1429. {
  1430. $cache->touch();
  1431. return true;
  1432. }
  1433. else
  1434. {
  1435. $headers = $file->headers;
  1436. }
  1437. }
  1438. else
  1439. {
  1440. unset($file);
  1441. }
  1442. }
  1443. }
  1444. else
  1445. {
  1446. return true;
  1447. }
  1448. }
  1449. else
  1450. {
  1451. $cache->unlink();
  1452. $this->data = array();
  1453. }
  1454. }
  1455. if (!isset($file))
  1456. {
  1457. if (is_a($this->file, 'SimplePie_File') && $this->file->url === $this->feed_url)
  1458. {
  1459. $file =& $this->file;
  1460. }
  1461. else
  1462. {
  1463. $file = new $this->file_class($this->feed_url, $this->timeout, 5, null, $this->useragent, $this->force_fsockopen);
  1464. }
  1465. }
  1466. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1467. {
  1468. $this->error = $file->error;
  1469. if (!empty($this->data))
  1470. {
  1471. return true;
  1472. }
  1473. else
  1474. {
  1475. return false;
  1476. }
  1477. }
  1478. if (!$this->force_feed)
  1479. {
  1480. $locate = new $this->locator_class($file, $this->timeout, $this->useragent, $this->file_class, $this->max_checked_feeds, $this->content_type_sniffer_class);
  1481. if (!$locate->is_feed($file))
  1482. {
  1483. unset($file);
  1484. if ($file = $locate->find($this->autodiscovery, $this->all_discovered_feeds))
  1485. {
  1486. if ($cache)
  1487. {
  1488. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1489. if (!$cache->save($this))
  1490. {
  1491. 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);
  1492. }
  1493. $cache = call_user_func(array($this->cache_class, 'create'), $this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc');
  1494. }
  1495. $this->feed_url = $file->url;
  1496. }
  1497. else
  1498. {
  1499. $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.";
  1500. SimplePie_Misc::error($this->error, E_USER_NOTICE, __FILE__, __LINE__);
  1501. return false;
  1502. }
  1503. }
  1504. $locate = null;
  1505. }
  1506. $headers = $file->headers;
  1507. $data = $file->body;
  1508. $sniffer = new $this->content_type_sniffer_class($file);
  1509. $sniffed = $sniffer->get_type();
  1510. }
  1511. else
  1512. {
  1513. $data = $this->raw_data;
  1514. }
  1515. $encodings = array();
  1516. if ($this->input_encoding !== false)
  1517. {
  1518. $encodings[] = $this->input_encoding;
  1519. }
  1520. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1521. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1522. if (isset($sniffed))
  1523. {
  1524. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1525. {
  1526. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1527. {
  1528. $encodings[] = strtoupper($charset[1]);
  1529. }
  1530. $encodings = array_merge($encodings, SimplePie_Misc::xml_encoding($data));
  1531. $encodings[] = 'UTF-8';
  1532. }
  1533. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1534. {
  1535. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1536. {
  1537. $encodings[] = $charset[1];
  1538. }
  1539. $encodings[] = 'US-ASCII';
  1540. }
  1541. elseif (substr($sniffed, 0, 5) === 'text/')
  1542. {
  1543. $encodings[] = 'US-ASCII';
  1544. }
  1545. }
  1546. $encodings = array_merge($encodings, SimplePie_Misc::xml_encoding($data));
  1547. $encodings[] = 'UTF-8';
  1548. $encodings[] = 'ISO-8859-1';
  1549. $encodings = array_unique($encodings);
  1550. if ($this->xml_dump)
  1551. {
  1552. header('Content-type: text/xml; charset=' . $encodings[0]);
  1553. echo $data;
  1554. exit;
  1555. }
  1556. foreach ($encodings as $encoding)
  1557. {
  1558. if ($utf8_data = SimplePie_Misc::change_encoding($data, $encoding, 'UTF-8'))
  1559. {
  1560. $parser = new $this->parser_class();
  1561. if ($parser->parse($utf8_data, 'UTF-8'))
  1562. {
  1563. $this->data = $parser->get_data();
  1564. if ($this->get_type() & ~SIMPLEPIE_TYPE_NONE)
  1565. {
  1566. if (isset($headers))
  1567. {
  1568. $this->data['headers'] = $headers;
  1569. }
  1570. $this->data['build'] = SIMPLEPIE_BUILD;
  1571. if ($cache && !$cache->save($this))
  1572. {
  1573. 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);
  1574. }
  1575. return true;
  1576. }
  1577. else
  1578. {
  1579. $this->error = "A feed could not be found at $this->feed_url. This does not appear to be a valid RSS or Atom feed.";
  1580. SimplePie_Misc::error($this->error, E_USER_NOTICE, __FILE__, __LINE__);
  1581. return false;
  1582. }
  1583. }
  1584. }
  1585. }
  1586. if (isset($parser))
  1587. {
  1588. $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());
  1589. }
  1590. else
  1591. {
  1592. $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.';
  1593. }
  1594. SimplePie_Misc::error($this->error, E_USER_NOTICE, __FILE__, __LINE__);
  1595. return false;
  1596. }
  1597. elseif (!empty($this->multifeed_url))
  1598. {
  1599. $i = 0;
  1600. $success = 0;
  1601. $this->multifeed_objects = array();
  1602. foreach ($this->multifeed_url as $url)
  1603. {
  1604. $this->multifeed_objects[$i] = clone $this;
  1605. $this->multifeed_objects[$i]->set_feed_url($url);
  1606. $success |= $this->multifeed_objects[$i]->init();
  1607. $i++;
  1608. }
  1609. return (bool) $success;
  1610. }
  1611. else
  1612. {
  1613. return false;
  1614. }
  1615. }
  1616. public function error()
  1617. {
  1618. return $this->error;
  1619. }
  1620. public function get_encoding()
  1621. {
  1622. return $this->sanitize->output_encoding;
  1623. }
  1624. public function handle_content_type($mime = 'text/html')
  1625. {
  1626. if (!headers_sent())
  1627. {
  1628. $header = "Content-type: $mime;";
  1629. if ($this->get_encoding())
  1630. {
  1631. $header .= ' charset=' . $this->get_encoding();
  1632. }
  1633. else
  1634. {
  1635. $header .= ' charset=UTF-8';
  1636. }
  1637. header($header);
  1638. }
  1639. }
  1640. public function get_type()
  1641. {
  1642. if (!isset($this->data['type']))
  1643. {
  1644. $this->data['type'] = SIMPLEPIE_TYPE_ALL;
  1645. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed']))
  1646. {
  1647. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_10;
  1648. }
  1649. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed']))
  1650. {
  1651. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_03;
  1652. }
  1653. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF']))
  1654. {
  1655. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['channel'])
  1656. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['image'])
  1657. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item'])
  1658. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['textinput']))
  1659. {
  1660. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_10;
  1661. }
  1662. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['channel'])
  1663. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['image'])
  1664. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item'])
  1665. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['textinput']))
  1666. {
  1667. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_090;
  1668. }
  1669. }
  1670. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss']))
  1671. {
  1672. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_ALL;
  1673. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1674. {
  1675. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1676. {
  1677. case '0.91':
  1678. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091;
  1679. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1680. {
  1681. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1682. {
  1683. case '0':
  1684. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_NETSCAPE;
  1685. break;
  1686. case '24':
  1687. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_USERLAND;
  1688. break;
  1689. }
  1690. }
  1691. break;
  1692. case '0.92':
  1693. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_092;
  1694. break;
  1695. case '0.93':
  1696. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_093;
  1697. break;
  1698. case '0.94':
  1699. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_094;
  1700. break;
  1701. case '2.0':
  1702. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_20;
  1703. break;
  1704. }
  1705. }
  1706. }
  1707. else
  1708. {
  1709. $this->data['type'] = SIMPLEPIE_TYPE_NONE;
  1710. }
  1711. }
  1712. return $this->data['type'];
  1713. }
  1714. public function subscribe_url()
  1715. {
  1716. if ($this->feed_url !== null)
  1717. {
  1718. return $this->sanitize($this->feed_url, SIMPLEPIE_CONSTRUCT_IRI);
  1719. }
  1720. else
  1721. {
  1722. return null;
  1723. }
  1724. }
  1725. public function get_feed_tags($namespace, $tag)
  1726. {
  1727. $type = $this->get_type();
  1728. if ($type & SIMPLEPIE_TYPE_ATOM_10)
  1729. {
  1730. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['