PageRenderTime 999ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/rss.php

https://gitlab.com/webkod3r/tripolis
PHP | 956 lines | 661 code | 120 blank | 175 comment | 163 complexity | cfe2939146287cb5f1dca28fa1690f6e MD5 | raw file
  1. <?php
  2. /**
  3. * MagpieRSS: a simple RSS integration tool
  4. *
  5. * A compiled file for RSS syndication
  6. *
  7. * @author Kellan Elliott-McCrea <kellan@protest.net>
  8. * @version 0.51
  9. * @license GPL
  10. *
  11. * @package External
  12. * @subpackage MagpieRSS
  13. * @deprecated 3.0.0 Use SimplePie instead.
  14. */
  15. /**
  16. * Deprecated. Use SimplePie (class-simplepie.php) instead.
  17. */
  18. _deprecated_file( basename( __FILE__ ), '3.0', WPINC . '/class-simplepie.php' );
  19. /**
  20. * Fires before MagpieRSS is loaded, to optionally replace it.
  21. *
  22. * @since 2.3.0
  23. * @deprecated 3.0.0
  24. */
  25. do_action( 'load_feed_engine' );
  26. /** RSS feed constant. */
  27. define('RSS', 'RSS');
  28. define('ATOM', 'Atom');
  29. define('MAGPIE_USER_AGENT', 'WordPress/' . $GLOBALS['wp_version']);
  30. class MagpieRSS {
  31. var $parser;
  32. var $current_item = array(); // item currently being parsed
  33. var $items = array(); // collection of parsed items
  34. var $channel = array(); // hash of channel fields
  35. var $textinput = array();
  36. var $image = array();
  37. var $feed_type;
  38. var $feed_version;
  39. // parser variables
  40. var $stack = array(); // parser stack
  41. var $inchannel = false;
  42. var $initem = false;
  43. var $incontent = false; // if in Atom <content mode="xml"> field
  44. var $intextinput = false;
  45. var $inimage = false;
  46. var $current_field = '';
  47. var $current_namespace = false;
  48. //var $ERROR = "";
  49. var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
  50. /**
  51. * PHP5 constructor.
  52. */
  53. function __construct( $source ) {
  54. # if PHP xml isn't compiled in, die
  55. #
  56. if ( !function_exists('xml_parser_create') )
  57. trigger_error( "Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php" );
  58. $parser = @xml_parser_create();
  59. if ( !is_resource($parser) )
  60. trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");
  61. $this->parser = $parser;
  62. # pass in parser, and a reference to this object
  63. # set up handlers
  64. #
  65. xml_set_object( $this->parser, $this );
  66. xml_set_element_handler($this->parser,
  67. 'feed_start_element', 'feed_end_element' );
  68. xml_set_character_data_handler( $this->parser, 'feed_cdata' );
  69. $status = xml_parse( $this->parser, $source );
  70. if (! $status ) {
  71. $errorcode = xml_get_error_code( $this->parser );
  72. if ( $errorcode != XML_ERROR_NONE ) {
  73. $xml_error = xml_error_string( $errorcode );
  74. $error_line = xml_get_current_line_number($this->parser);
  75. $error_col = xml_get_current_column_number($this->parser);
  76. $errormsg = "$xml_error at line $error_line, column $error_col";
  77. $this->error( $errormsg );
  78. }
  79. }
  80. xml_parser_free( $this->parser );
  81. $this->normalize();
  82. }
  83. /**
  84. * PHP4 constructor.
  85. */
  86. public function MagpieRSS( $source ) {
  87. self::__construct( $source );
  88. }
  89. function feed_start_element($p, $element, &$attrs) {
  90. $el = $element = strtolower($element);
  91. $attrs = array_change_key_case($attrs, CASE_LOWER);
  92. // check for a namespace, and split if found
  93. $ns = false;
  94. if ( strpos( $element, ':' ) ) {
  95. list($ns, $el) = split( ':', $element, 2);
  96. }
  97. if ( $ns and $ns != 'rdf' ) {
  98. $this->current_namespace = $ns;
  99. }
  100. # if feed type isn't set, then this is first element of feed
  101. # identify feed from root element
  102. #
  103. if (!isset($this->feed_type) ) {
  104. if ( $el == 'rdf' ) {
  105. $this->feed_type = RSS;
  106. $this->feed_version = '1.0';
  107. }
  108. elseif ( $el == 'rss' ) {
  109. $this->feed_type = RSS;
  110. $this->feed_version = $attrs['version'];
  111. }
  112. elseif ( $el == 'feed' ) {
  113. $this->feed_type = ATOM;
  114. $this->feed_version = $attrs['version'];
  115. $this->inchannel = true;
  116. }
  117. return;
  118. }
  119. if ( $el == 'channel' )
  120. {
  121. $this->inchannel = true;
  122. }
  123. elseif ($el == 'item' or $el == 'entry' )
  124. {
  125. $this->initem = true;
  126. if ( isset($attrs['rdf:about']) ) {
  127. $this->current_item['about'] = $attrs['rdf:about'];
  128. }
  129. }
  130. // if we're in the default namespace of an RSS feed,
  131. // record textinput or image fields
  132. elseif (
  133. $this->feed_type == RSS and
  134. $this->current_namespace == '' and
  135. $el == 'textinput' )
  136. {
  137. $this->intextinput = true;
  138. }
  139. elseif (
  140. $this->feed_type == RSS and
  141. $this->current_namespace == '' and
  142. $el == 'image' )
  143. {
  144. $this->inimage = true;
  145. }
  146. # handle atom content constructs
  147. elseif ( $this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
  148. {
  149. // avoid clashing w/ RSS mod_content
  150. if ($el == 'content' ) {
  151. $el = 'atom_content';
  152. }
  153. $this->incontent = $el;
  154. }
  155. // if inside an Atom content construct (e.g. content or summary) field treat tags as text
  156. elseif ($this->feed_type == ATOM and $this->incontent )
  157. {
  158. // if tags are inlined, then flatten
  159. $attrs_str = join(' ',
  160. array_map(array('MagpieRSS', 'map_attrs'),
  161. array_keys($attrs),
  162. array_values($attrs) ) );
  163. $this->append_content( "<$element $attrs_str>" );
  164. array_unshift( $this->stack, $el );
  165. }
  166. // Atom support many links per containging element.
  167. // Magpie treats link elements of type rel='alternate'
  168. // as being equivalent to RSS's simple link element.
  169. //
  170. elseif ($this->feed_type == ATOM and $el == 'link' )
  171. {
  172. if ( isset($attrs['rel']) and $attrs['rel'] == 'alternate' )
  173. {
  174. $link_el = 'link';
  175. }
  176. else {
  177. $link_el = 'link_' . $attrs['rel'];
  178. }
  179. $this->append($link_el, $attrs['href']);
  180. }
  181. // set stack[0] to current element
  182. else {
  183. array_unshift($this->stack, $el);
  184. }
  185. }
  186. function feed_cdata ($p, $text) {
  187. if ($this->feed_type == ATOM and $this->incontent)
  188. {
  189. $this->append_content( $text );
  190. }
  191. else {
  192. $current_el = join('_', array_reverse($this->stack));
  193. $this->append($current_el, $text);
  194. }
  195. }
  196. function feed_end_element ($p, $el) {
  197. $el = strtolower($el);
  198. if ( $el == 'item' or $el == 'entry' )
  199. {
  200. $this->items[] = $this->current_item;
  201. $this->current_item = array();
  202. $this->initem = false;
  203. }
  204. elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' )
  205. {
  206. $this->intextinput = false;
  207. }
  208. elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' )
  209. {
  210. $this->inimage = false;
  211. }
  212. elseif ($this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
  213. {
  214. $this->incontent = false;
  215. }
  216. elseif ($el == 'channel' or $el == 'feed' )
  217. {
  218. $this->inchannel = false;
  219. }
  220. elseif ($this->feed_type == ATOM and $this->incontent ) {
  221. // balance tags properly
  222. // note: This may not actually be necessary
  223. if ( $this->stack[0] == $el )
  224. {
  225. $this->append_content("</$el>");
  226. }
  227. else {
  228. $this->append_content("<$el />");
  229. }
  230. array_shift( $this->stack );
  231. }
  232. else {
  233. array_shift( $this->stack );
  234. }
  235. $this->current_namespace = false;
  236. }
  237. function concat (&$str1, $str2="") {
  238. if (!isset($str1) ) {
  239. $str1="";
  240. }
  241. $str1 .= $str2;
  242. }
  243. function append_content($text) {
  244. if ( $this->initem ) {
  245. $this->concat( $this->current_item[ $this->incontent ], $text );
  246. }
  247. elseif ( $this->inchannel ) {
  248. $this->concat( $this->channel[ $this->incontent ], $text );
  249. }
  250. }
  251. // smart append - field and namespace aware
  252. function append($el, $text) {
  253. if (!$el) {
  254. return;
  255. }
  256. if ( $this->current_namespace )
  257. {
  258. if ( $this->initem ) {
  259. $this->concat(
  260. $this->current_item[ $this->current_namespace ][ $el ], $text);
  261. }
  262. elseif ($this->inchannel) {
  263. $this->concat(
  264. $this->channel[ $this->current_namespace][ $el ], $text );
  265. }
  266. elseif ($this->intextinput) {
  267. $this->concat(
  268. $this->textinput[ $this->current_namespace][ $el ], $text );
  269. }
  270. elseif ($this->inimage) {
  271. $this->concat(
  272. $this->image[ $this->current_namespace ][ $el ], $text );
  273. }
  274. }
  275. else {
  276. if ( $this->initem ) {
  277. $this->concat(
  278. $this->current_item[ $el ], $text);
  279. }
  280. elseif ($this->intextinput) {
  281. $this->concat(
  282. $this->textinput[ $el ], $text );
  283. }
  284. elseif ($this->inimage) {
  285. $this->concat(
  286. $this->image[ $el ], $text );
  287. }
  288. elseif ($this->inchannel) {
  289. $this->concat(
  290. $this->channel[ $el ], $text );
  291. }
  292. }
  293. }
  294. function normalize () {
  295. // if atom populate rss fields
  296. if ( $this->is_atom() ) {
  297. $this->channel['descripton'] = $this->channel['tagline'];
  298. for ( $i = 0; $i < count($this->items); $i++) {
  299. $item = $this->items[$i];
  300. if ( isset($item['summary']) )
  301. $item['description'] = $item['summary'];
  302. if ( isset($item['atom_content']))
  303. $item['content']['encoded'] = $item['atom_content'];
  304. $this->items[$i] = $item;
  305. }
  306. }
  307. elseif ( $this->is_rss() ) {
  308. $this->channel['tagline'] = $this->channel['description'];
  309. for ( $i = 0; $i < count($this->items); $i++) {
  310. $item = $this->items[$i];
  311. if ( isset($item['description']))
  312. $item['summary'] = $item['description'];
  313. if ( isset($item['content']['encoded'] ) )
  314. $item['atom_content'] = $item['content']['encoded'];
  315. $this->items[$i] = $item;
  316. }
  317. }
  318. }
  319. function is_rss () {
  320. if ( $this->feed_type == RSS ) {
  321. return $this->feed_version;
  322. }
  323. else {
  324. return false;
  325. }
  326. }
  327. function is_atom() {
  328. if ( $this->feed_type == ATOM ) {
  329. return $this->feed_version;
  330. }
  331. else {
  332. return false;
  333. }
  334. }
  335. function map_attrs($k, $v) {
  336. return "$k=\"$v\"";
  337. }
  338. function error( $errormsg, $lvl = E_USER_WARNING ) {
  339. // append PHP's error message if track_errors enabled
  340. if ( isset($php_errormsg) ) {
  341. $errormsg .= " ($php_errormsg)";
  342. }
  343. if ( MAGPIE_DEBUG ) {
  344. trigger_error( $errormsg, $lvl);
  345. } else {
  346. error_log( $errormsg, 0);
  347. }
  348. }
  349. }
  350. if ( !function_exists('fetch_rss') ) :
  351. /**
  352. * Build Magpie object based on RSS from URL.
  353. *
  354. * @since 1.5.0
  355. * @package External
  356. * @subpackage MagpieRSS
  357. *
  358. * @param string $url URL to retrieve feed
  359. * @return bool|MagpieRSS false on failure or MagpieRSS object on success.
  360. */
  361. function fetch_rss ($url) {
  362. // initialize constants
  363. init();
  364. if ( !isset($url) ) {
  365. // error("fetch_rss called without a url");
  366. return false;
  367. }
  368. // if cache is disabled
  369. if ( !MAGPIE_CACHE_ON ) {
  370. // fetch file, and parse it
  371. $resp = _fetch_remote_file( $url );
  372. if ( is_success( $resp->status ) ) {
  373. return _response_to_rss( $resp );
  374. }
  375. else {
  376. // error("Failed to fetch $url and cache is off");
  377. return false;
  378. }
  379. }
  380. // else cache is ON
  381. else {
  382. // Flow
  383. // 1. check cache
  384. // 2. if there is a hit, make sure it's fresh
  385. // 3. if cached obj fails freshness check, fetch remote
  386. // 4. if remote fails, return stale object, or error
  387. $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
  388. if (MAGPIE_DEBUG and $cache->ERROR) {
  389. debug($cache->ERROR, E_USER_WARNING);
  390. }
  391. $cache_status = 0; // response of check_cache
  392. $request_headers = array(); // HTTP headers to send with fetch
  393. $rss = 0; // parsed RSS object
  394. $errormsg = 0; // errors, if any
  395. if (!$cache->ERROR) {
  396. // return cache HIT, MISS, or STALE
  397. $cache_status = $cache->check_cache( $url );
  398. }
  399. // if object cached, and cache is fresh, return cached obj
  400. if ( $cache_status == 'HIT' ) {
  401. $rss = $cache->get( $url );
  402. if ( isset($rss) and $rss ) {
  403. $rss->from_cache = 1;
  404. if ( MAGPIE_DEBUG > 1) {
  405. debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
  406. }
  407. return $rss;
  408. }
  409. }
  410. // else attempt a conditional get
  411. // set up headers
  412. if ( $cache_status == 'STALE' ) {
  413. $rss = $cache->get( $url );
  414. if ( isset($rss->etag) and $rss->last_modified ) {
  415. $request_headers['If-None-Match'] = $rss->etag;
  416. $request_headers['If-Last-Modified'] = $rss->last_modified;
  417. }
  418. }
  419. $resp = _fetch_remote_file( $url, $request_headers );
  420. if (isset($resp) and $resp) {
  421. if ($resp->status == '304' ) {
  422. // we have the most current copy
  423. if ( MAGPIE_DEBUG > 1) {
  424. debug("Got 304 for $url");
  425. }
  426. // reset cache on 304 (at minutillo insistent prodding)
  427. $cache->set($url, $rss);
  428. return $rss;
  429. }
  430. elseif ( is_success( $resp->status ) ) {
  431. $rss = _response_to_rss( $resp );
  432. if ( $rss ) {
  433. if (MAGPIE_DEBUG > 1) {
  434. debug("Fetch successful");
  435. }
  436. // add object to cache
  437. $cache->set( $url, $rss );
  438. return $rss;
  439. }
  440. }
  441. else {
  442. $errormsg = "Failed to fetch $url. ";
  443. if ( $resp->error ) {
  444. # compensate for Snoopy's annoying habbit to tacking
  445. # on '\n'
  446. $http_error = substr($resp->error, 0, -2);
  447. $errormsg .= "(HTTP Error: $http_error)";
  448. }
  449. else {
  450. $errormsg .= "(HTTP Response: " . $resp->response_code .')';
  451. }
  452. }
  453. }
  454. else {
  455. $errormsg = "Unable to retrieve RSS file for unknown reasons.";
  456. }
  457. // else fetch failed
  458. // attempt to return cached object
  459. if ($rss) {
  460. if ( MAGPIE_DEBUG ) {
  461. debug("Returning STALE object for $url");
  462. }
  463. return $rss;
  464. }
  465. // else we totally failed
  466. // error( $errormsg );
  467. return false;
  468. } // end if ( !MAGPIE_CACHE_ON ) {
  469. } // end fetch_rss()
  470. endif;
  471. /**
  472. * Retrieve URL headers and content using WP HTTP Request API.
  473. *
  474. * @since 1.5.0
  475. * @package External
  476. * @subpackage MagpieRSS
  477. *
  478. * @param string $url URL to retrieve
  479. * @param array $headers Optional. Headers to send to the URL.
  480. * @return Snoopy style response
  481. */
  482. function _fetch_remote_file($url, $headers = "" ) {
  483. $resp = wp_safe_remote_request( $url, array( 'headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT ) );
  484. if ( is_wp_error($resp) ) {
  485. $error = array_shift($resp->errors);
  486. $resp = new stdClass;
  487. $resp->status = 500;
  488. $resp->response_code = 500;
  489. $resp->error = $error[0] . "\n"; //\n = Snoopy compatibility
  490. return $resp;
  491. }
  492. // Snoopy returns headers unprocessed.
  493. // Also note, WP_HTTP lowercases all keys, Snoopy did not.
  494. $return_headers = array();
  495. foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
  496. if ( !is_array($value) ) {
  497. $return_headers[] = "$key: $value";
  498. } else {
  499. foreach ( $value as $v )
  500. $return_headers[] = "$key: $v";
  501. }
  502. }
  503. $response = new stdClass;
  504. $response->status = wp_remote_retrieve_response_code( $resp );
  505. $response->response_code = wp_remote_retrieve_response_code( $resp );
  506. $response->headers = $return_headers;
  507. $response->results = wp_remote_retrieve_body( $resp );
  508. return $response;
  509. }
  510. /**
  511. * Retrieve
  512. *
  513. * @since 1.5.0
  514. * @package External
  515. * @subpackage MagpieRSS
  516. *
  517. * @param array $resp
  518. * @return MagpieRSS|bool
  519. */
  520. function _response_to_rss ($resp) {
  521. $rss = new MagpieRSS( $resp->results );
  522. // if RSS parsed successfully
  523. if ( $rss && (!isset($rss->ERROR) || !$rss->ERROR) ) {
  524. // find Etag, and Last-Modified
  525. foreach ( (array) $resp->headers as $h) {
  526. // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
  527. if (strpos($h, ": ")) {
  528. list($field, $val) = explode(": ", $h, 2);
  529. }
  530. else {
  531. $field = $h;
  532. $val = "";
  533. }
  534. if ( $field == 'etag' ) {
  535. $rss->etag = $val;
  536. }
  537. if ( $field == 'last-modified' ) {
  538. $rss->last_modified = $val;
  539. }
  540. }
  541. return $rss;
  542. } // else construct error message
  543. else {
  544. $errormsg = "Failed to parse RSS file.";
  545. if ($rss) {
  546. $errormsg .= " (" . $rss->ERROR . ")";
  547. }
  548. // error($errormsg);
  549. return false;
  550. } // end if ($rss and !$rss->error)
  551. }
  552. /**
  553. * Set up constants with default values, unless user overrides.
  554. *
  555. * @since 1.5.0
  556. * @package External
  557. * @subpackage MagpieRSS
  558. */
  559. function init () {
  560. if ( defined('MAGPIE_INITALIZED') ) {
  561. return;
  562. }
  563. else {
  564. define('MAGPIE_INITALIZED', 1);
  565. }
  566. if ( !defined('MAGPIE_CACHE_ON') ) {
  567. define('MAGPIE_CACHE_ON', 1);
  568. }
  569. if ( !defined('MAGPIE_CACHE_DIR') ) {
  570. define('MAGPIE_CACHE_DIR', './cache');
  571. }
  572. if ( !defined('MAGPIE_CACHE_AGE') ) {
  573. define('MAGPIE_CACHE_AGE', 60*60); // one hour
  574. }
  575. if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {
  576. define('MAGPIE_CACHE_FRESH_ONLY', 0);
  577. }
  578. if ( !defined('MAGPIE_DEBUG') ) {
  579. define('MAGPIE_DEBUG', 0);
  580. }
  581. if ( !defined('MAGPIE_USER_AGENT') ) {
  582. $ua = 'WordPress/' . $GLOBALS['wp_version'];
  583. if ( MAGPIE_CACHE_ON ) {
  584. $ua = $ua . ')';
  585. }
  586. else {
  587. $ua = $ua . '; No cache)';
  588. }
  589. define('MAGPIE_USER_AGENT', $ua);
  590. }
  591. if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
  592. define('MAGPIE_FETCH_TIME_OUT', 2); // 2 second timeout
  593. }
  594. // use gzip encoding to fetch rss files if supported?
  595. if ( !defined('MAGPIE_USE_GZIP') ) {
  596. define('MAGPIE_USE_GZIP', true);
  597. }
  598. }
  599. function is_info ($sc) {
  600. return $sc >= 100 && $sc < 200;
  601. }
  602. function is_success ($sc) {
  603. return $sc >= 200 && $sc < 300;
  604. }
  605. function is_redirect ($sc) {
  606. return $sc >= 300 && $sc < 400;
  607. }
  608. function is_error ($sc) {
  609. return $sc >= 400 && $sc < 600;
  610. }
  611. function is_client_error ($sc) {
  612. return $sc >= 400 && $sc < 500;
  613. }
  614. function is_server_error ($sc) {
  615. return $sc >= 500 && $sc < 600;
  616. }
  617. class RSSCache {
  618. var $BASE_CACHE; // where the cache files are stored
  619. var $MAX_AGE = 43200; // when are files stale, default twelve hours
  620. var $ERROR = ''; // accumulate error messages
  621. /**
  622. * PHP5 constructor.
  623. */
  624. function __construct( $base = '', $age = '' ) {
  625. $this->BASE_CACHE = WP_CONTENT_DIR . '/cache';
  626. if ( $base ) {
  627. $this->BASE_CACHE = $base;
  628. }
  629. if ( $age ) {
  630. $this->MAX_AGE = $age;
  631. }
  632. }
  633. /**
  634. * PHP4 constructor.
  635. */
  636. public function RSSCache( $base = '', $age = '' ) {
  637. self::__construct( $base, $age );
  638. }
  639. /*=======================================================================*\
  640. Function: set
  641. Purpose: add an item to the cache, keyed on url
  642. Input: url from wich the rss file was fetched
  643. Output: true on success
  644. \*=======================================================================*/
  645. function set ($url, $rss) {
  646. $cache_option = 'rss_' . $this->file_name( $url );
  647. set_transient($cache_option, $rss, $this->MAX_AGE);
  648. return $cache_option;
  649. }
  650. /*=======================================================================*\
  651. Function: get
  652. Purpose: fetch an item from the cache
  653. Input: url from wich the rss file was fetched
  654. Output: cached object on HIT, false on MISS
  655. \*=======================================================================*/
  656. function get ($url) {
  657. $this->ERROR = "";
  658. $cache_option = 'rss_' . $this->file_name( $url );
  659. if ( ! $rss = get_transient( $cache_option ) ) {
  660. $this->debug(
  661. "Cache doesn't contain: $url (cache option: $cache_option)"
  662. );
  663. return 0;
  664. }
  665. return $rss;
  666. }
  667. /*=======================================================================*\
  668. Function: check_cache
  669. Purpose: check a url for membership in the cache
  670. and whether the object is older then MAX_AGE (ie. STALE)
  671. Input: url from wich the rss file was fetched
  672. Output: cached object on HIT, false on MISS
  673. \*=======================================================================*/
  674. function check_cache ( $url ) {
  675. $this->ERROR = "";
  676. $cache_option = 'rss_' . $this->file_name( $url );
  677. if ( get_transient($cache_option) ) {
  678. // object exists and is current
  679. return 'HIT';
  680. } else {
  681. // object does not exist
  682. return 'MISS';
  683. }
  684. }
  685. /*=======================================================================*\
  686. Function: serialize
  687. \*=======================================================================*/
  688. function serialize ( $rss ) {
  689. return serialize( $rss );
  690. }
  691. /*=======================================================================*\
  692. Function: unserialize
  693. \*=======================================================================*/
  694. function unserialize ( $data ) {
  695. return unserialize( $data );
  696. }
  697. /*=======================================================================*\
  698. Function: file_name
  699. Purpose: map url to location in cache
  700. Input: url from wich the rss file was fetched
  701. Output: a file name
  702. \*=======================================================================*/
  703. function file_name ($url) {
  704. return md5( $url );
  705. }
  706. /*=======================================================================*\
  707. Function: error
  708. Purpose: register error
  709. \*=======================================================================*/
  710. function error ($errormsg, $lvl=E_USER_WARNING) {
  711. // append PHP's error message if track_errors enabled
  712. if ( isset($php_errormsg) ) {
  713. $errormsg .= " ($php_errormsg)";
  714. }
  715. $this->ERROR = $errormsg;
  716. if ( MAGPIE_DEBUG ) {
  717. trigger_error( $errormsg, $lvl);
  718. }
  719. else {
  720. error_log( $errormsg, 0);
  721. }
  722. }
  723. function debug ($debugmsg, $lvl=E_USER_NOTICE) {
  724. if ( MAGPIE_DEBUG ) {
  725. $this->error("MagpieRSS [debug] $debugmsg", $lvl);
  726. }
  727. }
  728. }
  729. if ( !function_exists('parse_w3cdtf') ) :
  730. function parse_w3cdtf ( $date_str ) {
  731. # regex to match wc3dtf
  732. $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
  733. if ( preg_match( $pat, $date_str, $match ) ) {
  734. list( $year, $month, $day, $hours, $minutes, $seconds) =
  735. array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[7]);
  736. # calc epoch for current date assuming GMT
  737. $epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
  738. $offset = 0;
  739. if ( $match[11] == 'Z' ) {
  740. # zulu time, aka GMT
  741. }
  742. else {
  743. list( $tz_mod, $tz_hour, $tz_min ) =
  744. array( $match[8], $match[9], $match[10]);
  745. # zero out the variables
  746. if ( ! $tz_hour ) { $tz_hour = 0; }
  747. if ( ! $tz_min ) { $tz_min = 0; }
  748. $offset_secs = (($tz_hour*60)+$tz_min)*60;
  749. # is timezone ahead of GMT? then subtract offset
  750. #
  751. if ( $tz_mod == '+' ) {
  752. $offset_secs = $offset_secs * -1;
  753. }
  754. $offset = $offset_secs;
  755. }
  756. $epoch = $epoch + $offset;
  757. return $epoch;
  758. }
  759. else {
  760. return -1;
  761. }
  762. }
  763. endif;
  764. if ( !function_exists('wp_rss') ) :
  765. /**
  766. * Display all RSS items in a HTML ordered list.
  767. *
  768. * @since 1.5.0
  769. * @package External
  770. * @subpackage MagpieRSS
  771. *
  772. * @param string $url URL of feed to display. Will not auto sense feed URL.
  773. * @param int $num_items Optional. Number of items to display, default is all.
  774. */
  775. function wp_rss( $url, $num_items = -1 ) {
  776. if ( $rss = fetch_rss( $url ) ) {
  777. echo '<ul>';
  778. if ( $num_items !== -1 ) {
  779. $rss->items = array_slice( $rss->items, 0, $num_items );
  780. }
  781. foreach ( (array) $rss->items as $item ) {
  782. printf(
  783. '<li><a href="%1$s" title="%2$s">%3$s</a></li>',
  784. esc_url( $item['link'] ),
  785. esc_attr( strip_tags( $item['description'] ) ),
  786. esc_html( $item['title'] )
  787. );
  788. }
  789. echo '</ul>';
  790. } else {
  791. _e( 'An error has occurred, which probably means the feed is down. Try again later.' );
  792. }
  793. }
  794. endif;
  795. if ( !function_exists('get_rss') ) :
  796. /**
  797. * Display RSS items in HTML list items.
  798. *
  799. * You have to specify which HTML list you want, either ordered or unordered
  800. * before using the function. You also have to specify how many items you wish
  801. * to display. You can't display all of them like you can with wp_rss()
  802. * function.
  803. *
  804. * @since 1.5.0
  805. * @package External
  806. * @subpackage MagpieRSS
  807. *
  808. * @param string $url URL of feed to display. Will not auto sense feed URL.
  809. * @param int $num_items Optional. Number of items to display, default is all.
  810. * @return bool False on failure.
  811. */
  812. function get_rss ($url, $num_items = 5) { // Like get posts, but for RSS
  813. $rss = fetch_rss($url);
  814. if ( $rss ) {
  815. $rss->items = array_slice($rss->items, 0, $num_items);
  816. foreach ( (array) $rss->items as $item ) {
  817. echo "<li>\n";
  818. echo "<a href='$item[link]' title='$item[description]'>";
  819. echo esc_html($item['title']);
  820. echo "</a><br />\n";
  821. echo "</li>\n";
  822. }
  823. } else {
  824. return false;
  825. }
  826. }
  827. endif;