PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/rss.php

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