PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/contrib/feedcreator.class.php

https://github.com/matthiask/swisdk2
PHP | 1794 lines | 910 code | 190 blank | 694 comment | 133 complexity | 842b8f65ec5fe90809a35a79b96e9ca7 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /***************************************************************************
  3. FeedCreator class v1.7.7(BH)
  4. originally (c) Kai Blankenhorn
  5. www.bitfolge.de
  6. kaib@bitfolge.de
  7. v1.3 work by Scott Reynen (scott@randomchaos.com) and Kai Blankenhorn
  8. v1.5 OPML support by Dirk Clemens
  9. This library is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU Lesser General Public
  11. License as published by the Free Software Foundation; either
  12. version 2.1 of the License, or (at your option) any later version.
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. Lesser General Public License for more details.
  17. You should have received a copy of the GNU Lesser General Public
  18. License along with this library; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. ****************************************************************************
  21. Changelog:
  22. v1.7.7(BH) 28-03-06
  23. added GPX Feed (Barry Hunter)
  24. v1.7.6(BH) 20-02-06
  25. added GeoRSS Feed (Barry Hunter)
  26. v1.7.5(BH) 16-11-05
  27. added BASE Feed (Barry Hunter)
  28. v1.7.4(BH) 05-07-05
  29. added KML Feed (Barry Hunter)
  30. v1.7.3(BH) 05-07-05
  31. added PHP Feed (Barry Hunter)
  32. v1.7.2 10-11-04
  33. license changed to LGPL
  34. v1.7.1
  35. fixed a syntax bug
  36. fixed left over debug code
  37. v1.7 07-18-04
  38. added HTML and JavaScript feeds (configurable via CSS) (thanks to Pascal Van Hecke)
  39. added HTML descriptions for all feed formats (thanks to Pascal Van Hecke)
  40. added a switch to select an external stylesheet (thanks to Pascal Van Hecke)
  41. changed default content-type to application/xml
  42. added character encoding setting
  43. fixed numerous smaller bugs (thanks to Sören Fuhrmann of golem.de)
  44. improved changing ATOM versions handling (thanks to August Trometer)
  45. improved the UniversalFeedCreator's useCached method (thanks to Sören Fuhrmann of golem.de)
  46. added charset output in HTTP headers (thanks to Sören Fuhrmann of golem.de)
  47. added Slashdot namespace to RSS 1.0 (thanks to Sören Fuhrmann of golem.de)
  48. v1.6 05-10-04
  49. added stylesheet to RSS 1.0 feeds
  50. fixed generator comment (thanks Kevin L. Papendick and Tanguy Pruvot)
  51. fixed RFC822 date bug (thanks Tanguy Pruvot)
  52. added TimeZone customization for RFC8601 (thanks Tanguy Pruvot)
  53. fixed Content-type could be empty (thanks Tanguy Pruvot)
  54. fixed author/creator in RSS1.0 (thanks Tanguy Pruvot)
  55. v1.6 beta 02-28-04
  56. added Atom 0.3 support (not all features, though)
  57. improved OPML 1.0 support (hopefully - added more elements)
  58. added support for arbitrary additional elements (use with caution)
  59. code beautification :-)
  60. considered beta due to some internal changes
  61. v1.5.1 01-27-04
  62. fixed some RSS 1.0 glitches (thanks to Stéphane Vanpoperynghe)
  63. fixed some inconsistencies between documentation and code (thanks to Timothy Martin)
  64. v1.5 01-06-04
  65. added support for OPML 1.0
  66. added more documentation
  67. v1.4 11-11-03
  68. optional feed saving and caching
  69. improved documentation
  70. minor improvements
  71. v1.3 10-02-03
  72. renamed to FeedCreator, as it not only creates RSS anymore
  73. added support for mbox
  74. tentative support for echo/necho/atom/pie/???
  75. v1.2 07-20-03
  76. intelligent auto-truncating of RSS 0.91 attributes
  77. don't create some attributes when they're not set
  78. documentation improved
  79. fixed a real and a possible bug with date conversions
  80. code cleanup
  81. v1.1 06-29-03
  82. added images to feeds
  83. now includes most RSS 0.91 attributes
  84. added RSS 2.0 feeds
  85. v1.0 06-24-03
  86. initial release
  87. ***************************************************************************/
  88. /*** GENERAL USAGE *********************************************************
  89. include("feedcreator.class.php");
  90. $rss = new UniversalFeedCreator();
  91. $rss->useCached(); // use cached version if age<1 hour
  92. $rss->title = "PHP news";
  93. $rss->description = "daily news from the PHP scripting world";
  94. //optional
  95. $rss->descriptionTruncSize = 500;
  96. $rss->descriptionHtmlSyndicated = true;
  97. $rss->link = "http://www.dailyphp.net/news";
  98. $rss->syndicationURL = "http://www.dailyphp.net/".$_SERVER["PHP_SELF"];
  99. $image = new FeedImage();
  100. $image->title = "dailyphp.net logo";
  101. $image->url = "http://www.dailyphp.net/images/logo.gif";
  102. $image->link = "http://www.dailyphp.net";
  103. $image->description = "Feed provided by dailyphp.net. Click to visit.";
  104. //optional
  105. $image->descriptionTruncSize = 500;
  106. $image->descriptionHtmlSyndicated = true;
  107. $rss->image = $image;
  108. // get your news items from somewhere, e.g. your database:
  109. mysql_select_db($dbHost, $dbUser, $dbPass);
  110. $res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
  111. while ($data = mysql_fetch_object($res)) {
  112. $item = new FeedItem();
  113. $item->title = $data->title;
  114. $item->link = $data->url;
  115. $item->description = $data->short;
  116. //optional
  117. item->descriptionTruncSize = 500;
  118. item->descriptionHtmlSyndicated = true;
  119. $item->date = $data->newsdate;
  120. $item->source = "http://www.dailyphp.net";
  121. $item->author = "John Doe";
  122. $rss->addItem($item);
  123. }
  124. // valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
  125. // MBOX, OPML, ATOM, ATOM0.3, HTML, JS, PHP
  126. echo $rss->saveFeed("RSS1.0", "news/feed.xml");
  127. ***************************************************************************
  128. * A little setup *
  129. **************************************************************************/
  130. // your local timezone, set to "" to disable or for GMT
  131. define("TIME_ZONE","");
  132. /**
  133. * Version string.
  134. **/
  135. define("FEEDCREATOR_VERSION", "FeedCreator 1.7.6(BH)");
  136. /**
  137. * A FeedItem is a part of a FeedCreator feed.
  138. *
  139. * @author Kai Blankenhorn <kaib@bitfolge.de>
  140. * @since 1.3
  141. */
  142. class FeedItem extends HtmlDescribable {
  143. /**
  144. * Mandatory attributes of an item.
  145. */
  146. var $title, $description, $link;
  147. /**
  148. * Optional attributes of an item.
  149. */
  150. var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator, $thumb, $thumbdata;
  151. /**
  152. * Publishing date of an item. May be in one of the following formats:
  153. *
  154. * RFC 822:
  155. * "Mon, 20 Jan 03 18:05:41 +0400"
  156. * "20 Jan 03 18:05:41 +0000"
  157. *
  158. * ISO 8601:
  159. * "2003-01-20T18:05:41+04:00"
  160. *
  161. * Unix:
  162. * 1043082341
  163. */
  164. var $date;
  165. /**
  166. * Any additional elements to include as an assiciated array. All $key => $value pairs
  167. * will be included unencoded in the feed item in the form
  168. * <$key>$value</$key>
  169. * Again: No encoding will be used! This means you can invalidate or enhance the feed
  170. * if $value contains markup. This may be abused to embed tags not implemented by
  171. * the FeedCreator class used.
  172. */
  173. var $additionalElements = Array();
  174. // on hold
  175. // var $source;
  176. }
  177. /**
  178. * An FeedImage may be added to a FeedCreator feed.
  179. * @author Kai Blankenhorn <kaib@bitfolge.de>
  180. * @since 1.3
  181. */
  182. class FeedImage extends HtmlDescribable {
  183. /**
  184. * Mandatory attributes of an image.
  185. */
  186. var $title, $url, $link;
  187. /**
  188. * Optional attributes of an image.
  189. */
  190. var $width, $height, $description;
  191. }
  192. /**
  193. * An HtmlDescribable is an item within a feed that can have a description that may
  194. * include HTML markup.
  195. */
  196. class HtmlDescribable {
  197. /**
  198. * Indicates whether the description field should be rendered in HTML.
  199. */
  200. var $descriptionHtmlSyndicated;
  201. /**
  202. * Indicates whether and to how many characters a description should be truncated.
  203. */
  204. var $descriptionTruncSize;
  205. /**
  206. * Returns a formatted description field, depending on descriptionHtmlSyndicated and
  207. * $descriptionTruncSize properties
  208. * @return string the formatted description
  209. */
  210. function getDescription($overrideSyndicateHtml = false) {
  211. $descriptionField = new FeedHtmlField($this->description);
  212. $descriptionField->syndicateHtml = $overrideSyndicateHtml || $this->descriptionHtmlSyndicated;
  213. $descriptionField->truncSize = $this->descriptionTruncSize;
  214. return $descriptionField->output();
  215. }
  216. }
  217. /**
  218. * An FeedHtmlField describes and generates
  219. * a feed, item or image html field (probably a description). Output is
  220. * generated based on $truncSize, $syndicateHtml properties.
  221. * @author Pascal Van Hecke <feedcreator.class.php@vanhecke.info>
  222. * @version 1.6
  223. */
  224. class FeedHtmlField {
  225. /**
  226. * Mandatory attributes of a FeedHtmlField.
  227. */
  228. var $rawFieldContent;
  229. /**
  230. * Optional attributes of a FeedHtmlField.
  231. *
  232. */
  233. var $truncSize, $syndicateHtml;
  234. /**
  235. * Creates a new instance of FeedHtmlField.
  236. * @param $string: if given, sets the rawFieldContent property
  237. */
  238. function FeedHtmlField($parFieldContent) {
  239. if ($parFieldContent) {
  240. $this->rawFieldContent = $parFieldContent;
  241. }
  242. }
  243. /**
  244. * Creates the right output, depending on $truncSize, $syndicateHtml properties.
  245. * @return string the formatted field
  246. */
  247. function output() {
  248. // when field available and syndicated in html we assume
  249. // - valid html in $rawFieldContent and we enclose in CDATA tags
  250. // - no truncation (truncating risks producing invalid html)
  251. if (!$this->rawFieldContent) {
  252. $result = "";
  253. } elseif ($this->syndicateHtml) {
  254. $result = "<![CDATA[".$this->rawFieldContent."]]>";
  255. } else {
  256. if ($this->truncSize and is_int($this->truncSize)) {
  257. $result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
  258. } else {
  259. $result = htmlspecialchars($this->rawFieldContent);
  260. }
  261. }
  262. return $result;
  263. }
  264. }
  265. /**
  266. * UniversalFeedCreator lets you choose during runtime which
  267. * format to build.
  268. * For general usage of a feed class, see the FeedCreator class
  269. * below or the example above.
  270. *
  271. * @since 1.3
  272. * @author Kai Blankenhorn <kaib@bitfolge.de>
  273. */
  274. class UniversalFeedCreator extends FeedCreator {
  275. var $_feed;
  276. function _setFormat($format) {
  277. switch (strtoupper($format)) {
  278. case "BASE":
  279. $this->format = $format;
  280. case "2.0":
  281. // fall through
  282. case "RSS2.0":
  283. $this->_feed = new RSSCreator20();
  284. break;
  285. case "GEOPHOTORSS":
  286. case "PHOTORSS":
  287. case "GEORSS":
  288. $this->format = $format;
  289. case "1.0":
  290. // fall through
  291. case "RSS1.0":
  292. $this->_feed = new RSSCreator10();
  293. break;
  294. case "0.91":
  295. // fall through
  296. case "RSS0.91":
  297. $this->_feed = new RSSCreator091();
  298. break;
  299. case "PIE0.1":
  300. $this->_feed = new PIECreator01();
  301. break;
  302. case "MBOX":
  303. $this->_feed = new MBOXCreator();
  304. break;
  305. case "OPML":
  306. $this->_feed = new OPMLCreator();
  307. break;
  308. case "TOOLBAR":
  309. $this->format = $format;
  310. case "ATOM":
  311. // fall through: always the latest ATOM version
  312. case "ATOM0.3":
  313. $this->_feed = new AtomCreator03();
  314. break;
  315. case "HTML":
  316. $this->_feed = new HTMLCreator();
  317. break;
  318. case "PHP":
  319. $this->_feed = new PHPCreator();
  320. break;
  321. case "GPX":
  322. $this->_feed = new GPXCreator();
  323. break;
  324. case "KML":
  325. $this->_feed = new KMLCreator();
  326. break;
  327. case "JS":
  328. // fall through
  329. case "JAVASCRIPT":
  330. $this->_feed = new JSCreator();
  331. break;
  332. default:
  333. $this->_feed = new RSSCreator091();
  334. break;
  335. }
  336. $vars = get_object_vars($this);
  337. foreach ($vars as $key => $value) {
  338. // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
  339. if (!in_array($key, array("_feed", "contentType", "encoding"))) {
  340. $this->_feed->{$key} = $this->{$key};
  341. }
  342. }
  343. }
  344. /**
  345. * Creates a syndication feed based on the items previously added.
  346. *
  347. * @see FeedCreator::addItem()
  348. * @param string format format the feed should comply to. Valid values are:
  349. * "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
  350. * @return string the contents of the feed.
  351. */
  352. function createFeed($format = "RSS0.91") {
  353. $this->_setFormat($format);
  354. return $this->_feed->createFeed();
  355. }
  356. /**
  357. * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
  358. * header may be sent to redirect the use to the newly created file.
  359. * @since 1.4
  360. *
  361. * @param string format format the feed should comply to. Valid values are:
  362. * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", "ATOM0.3", "HTML", "JS"
  363. * @param string filename optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
  364. * @param boolean displayContents optional send the content of the file or not. If true, the file will be sent in the body of the response.
  365. */
  366. function saveFeed($format="RSS0.91", $filename="", $displayContents=true) {
  367. $this->_setFormat($format);
  368. $this->_feed->saveFeed($filename, $displayContents);
  369. }
  370. /**
  371. * Turns on caching and checks if there is a recent version of this feed in the cache.
  372. * If there is, an HTTP redirect header is sent.
  373. * To effectively use caching, you should create the FeedCreator object and call this method
  374. * before anything else, especially before you do the time consuming task to build the feed
  375. * (web fetching, for example).
  376. *
  377. * @param string format format the feed should comply to. Valid values are:
  378. * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
  379. * @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
  380. * @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
  381. */
  382. function useCached($format="RSS0.91", $filename="", $timeout=3600) {
  383. $this->_setFormat($format);
  384. $this->_feed->useCached($filename, $timeout);
  385. }
  386. }
  387. /**
  388. * FeedCreator is the abstract base implementation for concrete
  389. * implementations that implement a specific format of syndication.
  390. *
  391. * @abstract
  392. * @author Kai Blankenhorn <kaib@bitfolge.de>
  393. * @since 1.4
  394. */
  395. class FeedCreator extends HtmlDescribable {
  396. /**
  397. * Mandatory attributes of a feed.
  398. */
  399. var $title, $description, $link;
  400. /**
  401. * Optional attributes of a feed.
  402. */
  403. var $syndicationURL, $image, $language, $copyright, $pubDate, $lastBuildDate, $editor, $editorEmail, $webmaster, $category, $docs, $ttl, $rating, $skipHours, $skipDays;
  404. /**
  405. * The url of the external xsl stylesheet used to format the naked rss feed.
  406. * Ignored in the output when empty.
  407. */
  408. var $xslStyleSheet = "";
  409. /**
  410. * @access private
  411. */
  412. var $items = Array();
  413. /**
  414. * This feed's MIME content type.
  415. * @since 1.4
  416. * @access private
  417. */
  418. var $contentType = "application/xml";
  419. /**
  420. * This feed's character encoding.
  421. * @since 1.6.1
  422. **/
  423. var $encoding = "UTF-8";
  424. /**
  425. * Any additional elements to include as an assiciated array. All $key => $value pairs
  426. * will be included unencoded in the feed in the form
  427. * <$key>$value</$key>
  428. * Again: No encoding will be used! This means you can invalidate or enhance the feed
  429. * if $value contains markup. This may be abused to embed tags not implemented by
  430. * the FeedCreator class used.
  431. */
  432. var $additionalElements = Array();
  433. /**
  434. * Adds an FeedItem to the feed.
  435. *
  436. * @param object FeedItem $item The FeedItem to add to the feed.
  437. * @access public
  438. */
  439. function addItem($item) {
  440. $this->items[] = $item;
  441. }
  442. /**
  443. * Truncates a string to a certain length at the most sensible point.
  444. * First, if there's a '.' character near the end of the string, the string is truncated after this character.
  445. * If there is no '.', the string is truncated after the last ' ' character.
  446. * If the string is truncated, " ..." is appended.
  447. * If the string is already shorter than $length, it is returned unchanged.
  448. *
  449. * @static
  450. * @param string string A string to be truncated.
  451. * @param int length the maximum length the string should be truncated to
  452. * @return string the truncated string
  453. */
  454. static function iTrunc($string, $length) {
  455. if (strlen($string)<=$length) {
  456. return $string;
  457. }
  458. $pos = strrpos($string,".");
  459. if ($pos>=$length-4) {
  460. $string = substr($string,0,$length-4);
  461. $pos = strrpos($string,".");
  462. }
  463. if ($pos>=$length*0.4) {
  464. return substr($string,0,$pos+1)." ...";
  465. }
  466. $pos = strrpos($string," ");
  467. if ($pos>=$length-4) {
  468. $string = substr($string,0,$length-4);
  469. $pos = strrpos($string," ");
  470. }
  471. if ($pos>=$length*0.4) {
  472. return substr($string,0,$pos)." ...";
  473. }
  474. return substr($string,0,$length-4)." ...";
  475. }
  476. /**
  477. * Creates a comment indicating the generator of this feed.
  478. * The format of this comment seems to be recognized by
  479. * Syndic8.com.
  480. */
  481. function _createGeneratorComment() {
  482. return "<!-- generator=\"".FEEDCREATOR_VERSION."\" -->\n";
  483. }
  484. /**
  485. * Creates a string containing all additional elements specified in
  486. * $additionalElements.
  487. * @param elements array an associative array containing key => value pairs
  488. * @param indentString string a string that will be inserted before every generated line
  489. * @return string the XML tags corresponding to $additionalElements
  490. */
  491. function _createAdditionalElements($elements, $indentString="") {
  492. $ae = "";
  493. if (is_array($elements)) {
  494. foreach($elements AS $key => $value) {
  495. $ae.= $indentString."<$key>$value</$key>\n";
  496. }
  497. }
  498. return $ae;
  499. }
  500. function _createStylesheetReferences() {
  501. $xml = "";
  502. if (!empty($this->cssStyleSheet)) $xml .= "<?xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"?>\n";
  503. if (!empty($this->xslStyleSheet)) $xml .= "<?xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"?>\n";
  504. return $xml;
  505. }
  506. /**
  507. * Builds the feed's text.
  508. * @abstract
  509. * @return string the feed's complete text
  510. */
  511. function createFeed() {
  512. }
  513. /**
  514. * Generate a filename for the feed cache file. The result will be $_SERVER["PHP_SELF"] with the extension changed to .xml.
  515. * For example:
  516. *
  517. * echo $_SERVER["PHP_SELF"]."\n";
  518. * echo FeedCreator::_generateFilename();
  519. *
  520. * would produce:
  521. *
  522. * /rss/latestnews.php
  523. * latestnews.xml
  524. *
  525. * @return string the feed cache filename
  526. * @since 1.4
  527. * @access private
  528. */
  529. function _generateFilename() {
  530. $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  531. return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";
  532. }
  533. /**
  534. * @since 1.4
  535. * @access private
  536. */
  537. function _redirect($filename) {
  538. // attention, heavily-commented-out-area
  539. // maybe use this in addition to file time checking
  540. //Header("Expires: ".date("r",time()+$this->_timeout));
  541. /* no caching at all, doesn't seem to work as good:
  542. Header("Cache-Control: no-cache");
  543. Header("Pragma: no-cache");
  544. */
  545. // HTTP redirect, some feed readers' simple HTTP implementations don't follow it
  546. //Header("Location: ".$filename);
  547. Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename));
  548. if (preg_match("/\.(kml|gpx)$/",$filename)) {
  549. Header("Content-Disposition: attachment; filename=".basename($filename));
  550. } else {
  551. Header("Content-Disposition: inline; filename=".basename($filename));
  552. }
  553. readfile($filename, "r");
  554. die();
  555. }
  556. /**
  557. * Turns on caching and checks if there is a recent version of this feed in the cache.
  558. * If there is, an HTTP redirect header is sent.
  559. * To effectively use caching, you should create the FeedCreator object and call this method
  560. * before anything else, especially before you do the time consuming task to build the feed
  561. * (web fetching, for example).
  562. * @since 1.4
  563. * @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
  564. * @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
  565. */
  566. function useCached($filename="", $timeout=3600) {
  567. $this->_timeout = $timeout;
  568. if ($filename=="") {
  569. $filename = $this->_generateFilename();
  570. }
  571. if (file_exists($filename) AND (time()-filemtime($filename) < $timeout)) {
  572. $this->_redirect($filename);
  573. }
  574. }
  575. /**
  576. * Saves this feed as a file on the local disk. After the file is saved, a redirect
  577. * header may be sent to redirect the user to the newly created file.
  578. * @since 1.4
  579. *
  580. * @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
  581. * @param redirect boolean optional send an HTTP redirect header or not. If true, the user will be automatically redirected to the created file.
  582. */
  583. function saveFeed($filename="", $displayContents=true) {
  584. if ($filename=="") {
  585. $filename = $this->_generateFilename();
  586. }
  587. $feedFile = fopen($filename, "w+");
  588. if ($feedFile) {
  589. fputs($feedFile,$this->createFeed());
  590. fclose($feedFile);
  591. if ($displayContents) {
  592. $this->_redirect($filename);
  593. }
  594. } else {
  595. echo "<br /><b>Error creating feed file, please check write permissions.</b><br />";
  596. }
  597. }
  598. }
  599. /**
  600. * FeedDate is an internal class that stores a date for a feed or feed item.
  601. * Usually, you won't need to use this.
  602. */
  603. class FeedDate {
  604. var $unix;
  605. /**
  606. * Creates a new instance of FeedDate representing a given date.
  607. * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
  608. * @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and time is used.
  609. */
  610. function FeedDate($dateString="") {
  611. if ($dateString=="") $dateString = date("r");
  612. if (is_integer($dateString)) {
  613. $this->unix = $dateString;
  614. return;
  615. }
  616. if (preg_match("~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",$dateString,$matches)) {
  617. $months = Array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12);
  618. $this->unix = mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
  619. if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
  620. $tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
  621. } else {
  622. if (strlen($matches[7])==1) {
  623. $oneHour = 3600;
  624. $ord = ord($matches[7]);
  625. if ($ord < ord("M")) {
  626. $tzOffset = (ord("A") - $ord - 1) * $oneHour;
  627. } elseif ($ord >= ord("M") AND $matches[7]!="Z") {
  628. $tzOffset = ($ord - ord("M")) * $oneHour;
  629. } elseif ($matches[7]=="Z") {
  630. $tzOffset = 0;
  631. }
  632. }
  633. switch ($matches[7]) {
  634. case "UT":
  635. case "GMT": $tzOffset = 0;
  636. }
  637. }
  638. $this->unix += $tzOffset;
  639. return;
  640. }
  641. if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)) {
  642. $this->unix = mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
  643. if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
  644. $tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
  645. } else {
  646. if ($matches[7]=="Z") {
  647. $tzOffset = 0;
  648. }
  649. }
  650. $this->unix += $tzOffset;
  651. return;
  652. }
  653. $this->unix = 0;
  654. }
  655. /**
  656. * Gets the date stored in this FeedDate as an RFC 822 date.
  657. *
  658. * @return a date in RFC 822 format
  659. */
  660. function rfc822() {
  661. //return gmdate("r",$this->unix);
  662. $date = gmdate("D, d M Y H:i:s", $this->unix);
  663. if (TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE);
  664. return $date;
  665. }
  666. /**
  667. * Gets the date stored in this FeedDate as an ISO 8601 date.
  668. *
  669. * @return a date in ISO 8601 format
  670. */
  671. function iso8601() {
  672. $date = gmdate("Y-m-d\TH:i:sO",$this->unix);
  673. $date = substr($date,0,22) . ':' . substr($date,-2);
  674. if (TIME_ZONE!="") $date = str_replace("+00:00",TIME_ZONE,$date);
  675. return $date;
  676. }
  677. /**
  678. * Gets the date stored in this FeedDate as unix time stamp.
  679. *
  680. * @return a date as a unix time stamp
  681. */
  682. function unix() {
  683. return $this->unix;
  684. }
  685. }
  686. /**
  687. * RSSCreator10 is a FeedCreator that implements RDF Site Summary (RSS) 1.0.
  688. *
  689. * @see http://www.purl.org/rss/1.0/
  690. * @since 1.3
  691. * @author Kai Blankenhorn <kaib@bitfolge.de>
  692. */
  693. class RSSCreator10 extends FeedCreator {
  694. /**
  695. * Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
  696. * The feed will contain all items previously added in the same order.
  697. * @return string the feed's complete text
  698. */
  699. function createFeed() {
  700. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  701. $feed.= $this->_createGeneratorComment();
  702. if (empty($this->cssStyleSheet)) {
  703. $this->cssStyleSheet = "http://www.w3.org/2000/08/w3c-synd/style.css";
  704. }
  705. $feed.= $this->_createStylesheetReferences();
  706. $feed.= "<rdf:RDF\n";
  707. $feed.= " xmlns=\"http://purl.org/rss/1.0/\"\n";
  708. $feed.= " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
  709. $feed.= " xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n";
  710. if ($this->items[0]->thumb!="")
  711. $feed.= " xmlns:photo=\"http://www.pheed.com/pheed/\"\n";
  712. if ($this->items[0]->lat!="")
  713. $feed.= " xmlns:georss=\"http://www.georss.org/georss/\"\n";
  714. $feed.= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
  715. $feed.= " <channel rdf:about=\"".$this->syndicationURL."\">\n";
  716. $feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
  717. $feed.= " <description>".htmlspecialchars($this->description)."</description>\n";
  718. $feed.= " <link>".$this->link."</link>\n";
  719. if ($this->image!=null) {
  720. $feed.= " <image rdf:resource=\"".$this->image->url."\" />\n";
  721. }
  722. $now = new FeedDate();
  723. $feed.= " <dc:date>".htmlspecialchars($now->iso8601())."</dc:date>\n";
  724. $feed.= " <items>\n";
  725. $feed.= " <rdf:Seq>\n";
  726. for ($i=0;$i<count($this->items);$i++) {
  727. $feed.= " <rdf:li rdf:resource=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
  728. }
  729. $feed.= " </rdf:Seq>\n";
  730. $feed.= " </items>\n";
  731. $feed.= " </channel>\n";
  732. if ($this->image!=null) {
  733. $feed.= " <image rdf:about=\"".$this->image->url."\">\n";
  734. $feed.= " <title>".$this->image->title."</title>\n";
  735. $feed.= " <link>".$this->image->link."</link>\n";
  736. $feed.= " <url>".$this->image->url."</url>\n";
  737. $feed.= " </image>\n";
  738. }
  739. $feed.= $this->_createAdditionalElements($this->additionalElements, " ");
  740. for ($i=0;$i<count($this->items);$i++) {
  741. $feed.= " <item rdf:about=\"".htmlspecialchars($this->items[$i]->link)."\">\n";
  742. //$feed.= " <dc:type>Posting</dc:type>\n";
  743. $feed.= " <dc:format>text/html</dc:format>\n";
  744. if ($this->items[$i]->date!=null) {
  745. $itemDate = new FeedDate($this->items[$i]->date);
  746. $feed.= " <dc:date>".htmlspecialchars($itemDate->iso8601())."</dc:date>\n";
  747. }
  748. if ($this->items[$i]->source!="") {
  749. $feed.= " <dc:source>".htmlspecialchars($this->items[$i]->source)."</dc:source>\n";
  750. }
  751. if ($this->items[$i]->author!="") {
  752. $feed.= " <dc:creator>".htmlspecialchars($this->items[$i]->author)."</dc:creator>\n";
  753. }
  754. if ($this->items[$i]->lat!="") {
  755. $feed.= " <georss:point>".$this->items[$i]->lat." ".$this->items[$i]->long."</georss:point>\n";
  756. }
  757. if ($this->items[$i]->thumb!="") {
  758. $feed.= " <photo:thumbnail>".htmlspecialchars($this->items[$i]->thumb)."</photo:thumbnail>\n";
  759. }
  760. $feed.= " <title>".htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")))."</title>\n";
  761. $feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
  762. $feed.= " <description>".htmlspecialchars($this->items[$i]->description)."</description>\n";
  763. $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
  764. $feed.= " </item>\n";
  765. }
  766. $feed.= "</rdf:RDF>\n";
  767. return $feed;
  768. }
  769. }
  770. /**
  771. * RSSCreator091 is a FeedCreator that implements RSS 0.91 Spec, revision 3.
  772. *
  773. * @see http://my.netscape.com/publish/formats/rss-spec-0.91.html
  774. * @since 1.3
  775. * @author Kai Blankenhorn <kaib@bitfolge.de>
  776. */
  777. class RSSCreator091 extends FeedCreator {
  778. /**
  779. * Stores this RSS feed's version number.
  780. * @access private
  781. */
  782. var $RSSVersion;
  783. var $format;
  784. function RSSCreator091() {
  785. $this->_setRSSVersion("0.91");
  786. $this->contentType = "application/rss+xml";
  787. }
  788. /**
  789. * Sets this RSS feed's version number.
  790. * @access private
  791. */
  792. function _setRSSVersion($version) {
  793. $this->RSSVersion = $version;
  794. }
  795. /**
  796. * Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
  797. * The feed will contain all items previously added in the same order.
  798. * @return string the feed's complete text
  799. */
  800. function createFeed() {
  801. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  802. $feed.= $this->_createGeneratorComment();
  803. $feed.= $this->_createStylesheetReferences();
  804. $feed.= "<rss version=\"".$this->RSSVersion."\">\n";
  805. if ($this->format == 'BASE') {
  806. $feed.= " <channel xmlns:g=\"http://base.google.com/ns/1.0\">\n";
  807. } else {
  808. $feed.= " <channel>\n";
  809. }
  810. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";
  811. $this->descriptionTruncSize = 500;
  812. $feed.= " <description>".$this->getDescription()."</description>\n";
  813. $feed.= " <link>".$this->link."</link>\n";
  814. $now = new FeedDate();
  815. $feed.= " <lastBuildDate>".htmlspecialchars($now->rfc822())."</lastBuildDate>\n";
  816. $feed.= " <generator>".FEEDCREATOR_VERSION."</generator>\n";
  817. if ($this->image!=null) {
  818. $feed.= " <image>\n";
  819. $feed.= " <url>".$this->image->url."</url>\n";
  820. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title),100)."</title>\n";
  821. $feed.= " <link>".$this->image->link."</link>\n";
  822. if ($this->image->width!="") {
  823. $feed.= " <width>".$this->image->width."</width>\n";
  824. }
  825. if ($this->image->height!="") {
  826. $feed.= " <height>".$this->image->height."</height>\n";
  827. }
  828. if ($this->image->description!="") {
  829. $feed.= " <description>".$this->image->getDescription()."</description>\n";
  830. }
  831. $feed.= " </image>\n";
  832. }
  833. if ($this->language!="") {
  834. $feed.= " <language>".$this->language."</language>\n";
  835. }
  836. if ($this->copyright!="") {
  837. $feed.= " <copyright>".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."</copyright>\n";
  838. }
  839. if ($this->editor!="") {
  840. $feed.= " <managingEditor>".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."</managingEditor>\n";
  841. }
  842. if ($this->webmaster!="") {
  843. $feed.= " <webMaster>".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."</webMaster>\n";
  844. }
  845. if ($this->pubDate!="") {
  846. $pubDate = new FeedDate($this->pubDate);
  847. $feed.= " <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n";
  848. }
  849. if ($this->category!="") {
  850. $feed.= " <category>".htmlspecialchars($this->category)."</category>\n";
  851. }
  852. if ($this->docs!="") {
  853. $feed.= " <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."</docs>\n";
  854. }
  855. if ($this->ttl!="") {
  856. $feed.= " <ttl>".htmlspecialchars($this->ttl)."</ttl>\n";
  857. }
  858. if ($this->rating!="") {
  859. $feed.= " <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating),500)."</rating>\n";
  860. }
  861. if ($this->skipHours!="") {
  862. $feed.= " <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n";
  863. }
  864. if ($this->skipDays!="") {
  865. $feed.= " <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n";
  866. }
  867. $feed.= $this->_createAdditionalElements($this->additionalElements, " ");
  868. for ($i=0;$i<count($this->items);$i++) {
  869. $feed.= " <item>\n";
  870. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";
  871. $feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
  872. $feed.= " <description>".$this->items[$i]->getDescription()."</description>\n";
  873. if ($this->items[$i]->author!="") {
  874. $feed.= " <author>".htmlspecialchars($this->items[$i]->author)."</author>\n";
  875. }
  876. /*
  877. // on hold
  878. if ($this->items[$i]->source!="") {
  879. $feed.= " <source>".htmlspecialchars($this->items[$i]->source)."</source>\n";
  880. }
  881. */
  882. if ($this->items[$i]->category!="") {
  883. $feed.= " <category>".htmlspecialchars($this->items[$i]->category)."</category>\n";
  884. }
  885. if ($this->items[$i]->comments!="") {
  886. $feed.= " <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n";
  887. }
  888. if ($this->items[$i]->date!="") {
  889. $itemDate = new FeedDate($this->items[$i]->date);
  890. $feed.= " <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n";
  891. }
  892. if ($this->items[$i]->guid!="") {
  893. $feed.= " <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n";
  894. }
  895. if ($this->items[$i]->thumb!="") {
  896. $feed.= " <g:image_link>".htmlspecialchars($this->items[$i]->thumb)."</g:image_link>\n";
  897. }
  898. $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
  899. $feed.= " </item>\n";
  900. }
  901. $feed.= " </channel>\n";
  902. $feed.= "</rss>\n";
  903. return $feed;
  904. }
  905. }
  906. /**
  907. * RSSCreator20 is a FeedCreator that implements RDF Site Summary (RSS) 2.0.
  908. *
  909. * @see http://backend.userland.com/rss
  910. * @since 1.3
  911. * @author Kai Blankenhorn <kaib@bitfolge.de>
  912. */
  913. class RSSCreator20 extends RSSCreator091 {
  914. function RSSCreator20() {
  915. parent::_setRSSVersion("2.0");
  916. }
  917. }
  918. /**
  919. * KMLCreator is a FeedCreator that implements a KML output, suitable for Keyhole/Google Earth
  920. *
  921. * @since 1.7.3
  922. * @author Barry Hunter <geo@barryhunter.co.uk>
  923. */
  924. class KMLCreator extends FeedCreator {
  925. function KMLCreator() {
  926. $this->contentType = "application/vnd.google-earth.kml+xml";
  927. $this->encoding = "utf-8";
  928. }
  929. function createFeed() {
  930. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  931. $feed.= $this->_createStylesheetReferences();
  932. $feed.= "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n";
  933. $feed.= "<Document>\n";
  934. if ($_GET['LinkControl'])
  935. $feed.= "<NetworkLinkControl>\n<minRefreshPeriod>3600</minRefreshPeriod>\n</NetworkLinkControl>\n";
  936. if (!empty($_GET['simple']) && count($this->items) > 0) {
  937. $feed.= "<Style id=\"defaultIcon\">
  938. <LabelStyle>
  939. <scale>0</scale>
  940. </LabelStyle>
  941. </Style>
  942. <Style id=\"hoverIcon\">".
  943. (($this->items[0]->thumb!="")?"
  944. <IconStyle id=\"hoverIcon\">
  945. <scale>2.1</scale>
  946. </IconStyle>":'')."
  947. </Style>
  948. <StyleMap id=\"defaultStyle\">
  949. <Pair>
  950. <key>normal</key>
  951. <styleUrl>#defaultIcon</styleUrl>
  952. </Pair>
  953. <Pair>
  954. <key>highlight</key>
  955. <styleUrl>#hoverIcon</styleUrl>
  956. </Pair>
  957. </StyleMap>
  958. ";
  959. $style = "#defaultStyle";
  960. } else {
  961. $style = "root://styleMaps#default?iconId=0x307";
  962. }
  963. $feed.= "<Folder>\n";
  964. $feed.= " <name>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</name>
  965. <description>".$this->getDescription()."</description>
  966. <visibility>1</visibility>\n";
  967. $this->truncSize = 500;
  968. for ($i=0;$i<count($this->items);$i++) {
  969. //added here beucase description gets auto surrounded by cdata
  970. if ($this->items[$i]->thumbTag!="") {
  971. $this->items[$i]->description = "<a href=\"".htmlspecialchars($this->items[$i]->link)."\">".$this->items[$i]->thumbTag."</a><br/>".$this->items[$i]->description;
  972. }
  973. $this->items[$i]->description = "<p align=\"center\"><b>".$this->items[$i]->description."</b><br/>
  974. ".$this->items[$i]->licence."
  975. <br/><br/><a href=\"".htmlspecialchars($this->items[$i]->link)."\">View Online</a></b>";
  976. $feed.= "
  977. <Placemark>
  978. <description>".$this->items[$i]->getDescription(true)."</description>
  979. <name>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</name>
  980. <visibility>1</visibility>
  981. <Point>
  982. <coordinates>".$this->items[$i]->long.",".$this->items[$i]->lat.",25</coordinates>
  983. </Point>";
  984. if ($this->items[$i]->thumb!="") {
  985. $feed.= "
  986. <styleUrl>$style</styleUrl>
  987. <Style>
  988. <icon>".htmlspecialchars($this->items[$i]->thumb)."</icon>
  989. </Style>";
  990. }
  991. $feed.= "
  992. </Placemark>\n";
  993. }
  994. $feed .= "</Folder>\n</Document>\n</kml>\n";
  995. return $feed;
  996. }
  997. /**
  998. * Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types.
  999. * @return string the feed cache filename
  1000. * @since 1.4
  1001. * @access private
  1002. */
  1003. function _generateFilename() {
  1004. $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  1005. return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".kml";
  1006. }
  1007. }
  1008. /**
  1009. * GPXCreator is a FeedCreator that implements a GPX output, suitable for a GIS packages
  1010. *
  1011. * @since 1.7.6
  1012. * @author Barry Hunter <geo@barryhunter.co.uk>
  1013. */
  1014. class GPXCreator extends FeedCreator {
  1015. function GPXCreator() {
  1016. $this->contentType = "text/xml";
  1017. $this->encoding = "utf-8";
  1018. }
  1019. function createFeed() {
  1020. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  1021. $feed.= $this->_createStylesheetReferences();
  1022. $feed.= "<gpx xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"1.0\"
  1023. creator=\"".FEEDCREATOR_VERSION."\"
  1024. xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\" xmlns=\"http://www.topografix.com/GPX/1/0\">\n";
  1025. $now = new FeedDate();
  1026. $feed.= "<desc>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</desc>
  1027. <author>{$http_host}</author>
  1028. <url>".htmlspecialchars($this->link)."</url>
  1029. <time>".htmlspecialchars($now->iso8601())."</time>
  1030. \n";
  1031. for ($i=0;$i<count($this->items);$i++) {
  1032. $feed.= "<wpt lat=\"".$this->items[$i]->lat."\" lon=\"".$this->items[$i]->long."\">
  1033. <name>".substr(htmlspecialchars(strip_tags($this->items[$i]->title)),0,6)."</name>
  1034. <desc>".htmlspecialchars(strip_tags($this->items[$i]->title))."</desc>
  1035. <src>".htmlspecialchars($this->items[$i]->author)."</src>
  1036. <url>".htmlspecialchars($this->items[$i]->link)."</url>
  1037. </wpt>\n";
  1038. }
  1039. $feed .= "</gpx>\n";
  1040. return $feed;
  1041. }
  1042. }
  1043. /**
  1044. * PHPCreator is a FeedCreator that implements a PHP output, suitable for a include
  1045. *
  1046. * @since 1.7.3
  1047. * @author Barry Hunter <geo@barryhunter.co.uk>
  1048. */
  1049. class PHPCreator extends FeedCreator {
  1050. function PHPCreator() {
  1051. $this->contentType = "text/plain";
  1052. $this->encoding = "utf-8";
  1053. }
  1054. function createFeed() {
  1055. $feed = "<?php\n";
  1056. $feed.= "class FeedItem {}\n";
  1057. $feed.= " \$feedTitle='".addslashes(FeedCreator::iTrunc(htmlspecialchars($this->title),100))."';\n";
  1058. $this->truncSize = 500;
  1059. $feed.= " \$feedDescription='".addslashes($this->getDescription())."';\n";
  1060. $feed.= " \$feedLink='".$this->link."';\n";
  1061. $feed.= " \$feedItem = array();\n";
  1062. for ($i=0;$i<count($this->items);$i++) {
  1063. $feed.= " \$feedItem[$i] = new FeedItem();\n";
  1064. if ($this->items[$i]->guid!="") {
  1065. $feed.= " \$feedItem[$i]->id='".htmlspecialchars($this->items[$i]->guid)."';\n";
  1066. }
  1067. $feed.= " \$feedItem[$i]->title='".addslashes(FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100))."';\n";
  1068. $feed.= " \$feedItem[$i]->link='".htmlspecialchars($this->items[$i]->link)."';\n";
  1069. $feed.= " \$feedItem[$i]->date=".htmlspecialchars($this->items[$i]->date).";\n";
  1070. if ($this->items[$i]->author!="") {
  1071. $feed.= " \$feedItem[$i]->author='".htmlspecialchars($this->items[$i]->author)."';\n";
  1072. if ($this->items[$i]->authorEmail!="") {
  1073. $feed.= " \$feedItem[$i]->authorEmail='".$this->items[$i]->authorEmail."';\n";
  1074. }
  1075. }
  1076. $feed.= " \$feedItem[$i]->description='".addslashes($this->items[$i]->getDescription())."';\n";
  1077. if ($this->items[$i]->thumb!="") {
  1078. $feed.= " \$feedItem[$i]->thumbURL='".htmlspecialchars($this->items[$i]->thumb)."';\n";
  1079. }
  1080. }
  1081. $feed .= "?>\n";
  1082. return $feed;
  1083. }
  1084. }
  1085. /**
  1086. * PIECreator01 is a FeedCreator that implements the emerging PIE specification,
  1087. * as in http://intertwingly.net/wiki/pie/Syntax.
  1088. *
  1089. * @deprecated
  1090. * @since 1.3
  1091. * @author Scott Reynen <scott@randomchaos.com> and Kai Blankenhorn <kaib@bitfolge.de>
  1092. */
  1093. class PIECreator01 extends FeedCreator {
  1094. function PIECreator01() {
  1095. $this->encoding = "utf-8";
  1096. }
  1097. function createFeed() {
  1098. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  1099. $feed.= $this->_createStylesheetReferences();
  1100. $feed.= "<feed version=\"0.1\" xmlns=\"http://example.com/newformat#\">\n";
  1101. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";
  1102. $this->truncSize = 500;
  1103. $feed.= " <subtitle>".$this->getDescription()."</subtitle>\n";
  1104. $feed.= " <link>".$this->link."</link>\n";
  1105. for ($i=0;$i<count($this->items);$i++) {
  1106. $feed.= " <entry>\n";
  1107. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";
  1108. $feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
  1109. $itemDate = new FeedDate($this->items[$i]->date);
  1110. $feed.= " <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";
  1111. $feed.= " <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";
  1112. $feed.= " <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";
  1113. $feed.= " <id>".htmlspecialchars($this->items[$i]->guid)."</id>\n";
  1114. if ($this->items[$i]->author!="") {
  1115. $feed.= " <author>\n";
  1116. $feed.= " <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
  1117. if ($this->items[$i]->authorEmail!="") {
  1118. $feed.= " <email>".$this->items[$i]->authorEmail."</email>\n";
  1119. }
  1120. $feed.=" </author>\n";
  1121. }
  1122. $feed.= " <content type=\"text/html\" xml:lang=\"en-us\">\n";
  1123. $feed.= " <div xmlns=\"http://www.w3.org/1999/xhtml\">".$this->items[$i]->getDescription()."</div>\n";
  1124. $feed.= " </content>\n";
  1125. $feed.= " </entry>\n";
  1126. }
  1127. $feed.= "</feed>\n";
  1128. return $feed;
  1129. }
  1130. }
  1131. /**
  1132. * AtomCreator03 is a FeedCreator that implements the atom specification,
  1133. * as in http://www.intertwingly.net/wiki/pie/FrontPage.
  1134. * Please note that just by using AtomCreator03 you won't automatically
  1135. * produce valid atom files. For example, you have to specify either an editor
  1136. * for the feed or an author for every single feed item.
  1137. *
  1138. * Some elements have not been implemented yet. These are (incomplete list):
  1139. * author URL, item author's email and URL, item contents, alternate links,
  1140. * other link content types than text/html. Some of them may be created with
  1141. * AtomCreator03::additionalElements.
  1142. *
  1143. * @see FeedCreator#additionalElements
  1144. * @since 1.6
  1145. * @author Kai Blankenhorn <kaib@bitfolge.de>, Scott Reynen <scott@randomchaos.com>
  1146. */
  1147. class AtomCreator03 extends FeedCreator {
  1148. var $format;
  1149. function AtomCreator03() {
  1150. $this->contentType = "application/atom+xml";
  1151. $this->encoding = "utf-8";
  1152. }
  1153. function createFeed() {
  1154. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  1155. $feed.= $this->_createGeneratorComment();
  1156. $feed.= $this->_createStylesheetReferences();
  1157. $feed.= "<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\"";
  1158. if ($this->format=='TOOLBAR') {
  1159. $feed.= " xmlns:gtb=\"http://toolbar.google.com/custombuttons/\"";
  1160. }
  1161. if ($this->language!="") {
  1162. $feed.= " xml:lang=\"".$this->language."\"";
  1163. }
  1164. $feed.= ">\n";
  1165. $feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
  1166. $feed.= " <tagline>".htmlspecialchars($this->description)."</tagline>\n";
  1167. $feed.= " <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->link)."\"/>\n";
  1168. $feed.= " <id>".htmlspecialchars($this->link)."</id>\n";
  1169. $now = new FeedDate();
  1170. $feed.= " <modified>".htmlspecialchars($now->iso8601())."</modified>\n";
  1171. if ($this->editor!="") {
  1172. $feed.= " <author>\n";
  1173. $feed.= " <name>".$this->editor."</name>\n";
  1174. if ($this->editorEmail!="") {
  1175. $feed.= " <email>".$this->editorEmail."</email>\n";
  1176. }
  1177. $feed.= " </author>\n";
  1178. }
  1179. $feed.= " <generator>".FEEDCREATOR_VERSION."</generator>\n";
  1180. $feed.= $this->_createAdditionalElements($this->additionalElements, " ");
  1181. for ($i=0;$i<count($this->items);$i++) {
  1182. $feed.= " <entry>\n";
  1183. $feed.= " <title>".htmlspecialchars(strip_tags($this->items[$i]->title))."</title>\n";
  1184. $feed.= " <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
  1185. if ($this->items[$i]->date=="") {
  1186. $this->items[$i]->date = time();
  1187. }
  1188. $itemDate = new FeedDate($this->items[$i]->date);
  1189. $feed.= " <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";
  1190. $feed.= " <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";
  1191. $feed.= " <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";
  1192. $feed.= " <id>".htmlspecialchars($this->items[$i]->link)."</id>\n";
  1193. $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
  1194. if ($this->items[$i]->author!="") {
  1195. $feed.= " <author>\n";
  1196. $feed.= " <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
  1197. $feed.= " </author>\n";
  1198. }
  1199. if ($this->items[$i]->description!="") {
  1200. $feed.= " <summary>".htmlspecialchars($this->items[$i]->description)."</summary>\n";
  1201. }
  1202. if ($this->items[$i]->thumbdata) {
  1203. $feed.= " <gtb:icon mode=\"base64\" type=\"image/jpeg\">\n";
  1204. $feed.= chunk_split(base64_encode($this->items[$i]->thumbdata))."\n";
  1205. $feed.= " </gtb:icon>\n";
  1206. }
  1207. $feed.= " </entry>\n";
  1208. }
  1209. $feed.= "</feed>\n";
  1210. return $feed;
  1211. }
  1212. }
  1213. /**
  1214. * MBOXCreator is a FeedCreator that implements the mbox format
  1215. * as described in http://www.qmail.org/man/man5/mbox.html
  1216. *
  1217. * @since 1.3
  1218. * @author Kai Blankenhorn <kaib@bitfolge.de>
  1219. */
  1220. class MBOXCreator extends FeedCreator {
  1221. function MBOXCreator() {
  1222. $this->contentType = "text/plain";
  1223. $this->encoding = "ISO-8859-15";
  1224. }
  1225. function qp_enc($input = "", $line_max = 76) {
  1226. $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  1227. $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
  1228. $eol = "\r\n";
  1229. $escape = "=";
  1230. $output = "";
  1231. while( list(, $line) = each($lines) ) {
  1232. //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
  1233. $linlen = strlen($line);
  1234. $newline = "";
  1235. for($i = 0; $i < $linlen; $i++) {
  1236. $c = substr($line, $i, 1);
  1237. $dec = ord($c);
  1238. if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
  1239. $c = "=20";
  1240. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  1241. $h2 = floor($dec/16); $h1 = floor($dec%16);
  1242. $c = $escape.$hex["$h2"].$hex["$h1"];
  1243. }
  1244. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  1245. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  1246. $newline = "";
  1247. }
  1248. $newline .= $c;
  1249. } // end of for
  1250. $output .= $newline.$eol;
  1251. }
  1252. return trim($output);
  1253. }
  1254. /**
  1255. * Builds the MBOX contents.
  1256. * @return string the feed's complete text
  1257. */
  1258. function createFeed() {
  1259. for ($i=0;$i<count($this->items);$i++) {
  1260. if ($this->items[$i]->author!="") {
  1261. $from = $this->items[$i]->author;
  1262. } else {
  1263. $from = $this->title;
  1264. }
  1265. $itemDate = new FeedDate($this->items[$i]->date);
  1266. $feed.= "From ".strtr(MBOXCreator::qp_enc($from)," ","_")." ".date("D M d H:i:s Y",$itemDate->unix())."\n";
  1267. $feed.= "Content-Type: text/plain;\n";
  1268. $feed.= " charset=\"".$this->encoding."\"\n";
  1269. $feed.= "Content-Transfer-Encoding: quoted-printable\n";
  1270. $feed.= "Content-Type: text/plain\n";
  1271. $feed.= "From: \"".MBOXCreator::qp_enc($from)."\"\n";
  1272. $feed.= "Date: ".$itemDate->rfc822()."\n";
  1273. $feed.= "Subject: ".MBOXCreator::qp_enc(FeedCreator::iTrunc($this->items[$i]->title,100))."\n";
  1274. $feed.= "\n";
  1275. $body = chunk_split(MBOXCreator::qp_enc($this->items[$i]->description));
  1276. $feed.= preg_replace("~\nFrom ([^\n]*)(\n?)~","\n>From $1$2\n",$body);
  1277. $feed.= "\n";
  1278. $feed.= "\n";
  1279. }
  1280. return $feed;
  1281. }
  1282. /**
  1283. * Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types.
  1284. * @return string the feed cache filename
  1285. * @since 1.4
  1286. * @access private
  1287. */
  1288. function _generateFilename() {
  1289. $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  1290. return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".mbox";
  1291. }
  1292. }
  1293. /**
  1294. * OPMLCreator is a FeedCreator that implements OPML 1.0.
  1295. *
  1296. * @see http://opml.scripting.com/spec
  1297. * @author Dirk Clemens, Kai Blankenhorn
  1298. * @since 1.5
  1299. */
  1300. class OPMLCreator extends FeedCreator {
  1301. function OPMLCreator() {
  1302. $this->encoding = "utf-8";
  1303. }
  1304. function createFeed() {
  1305. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  1306. $feed.= $this->_createGeneratorComment();
  1307. $feed.= $this->_createStylesheetReferences();
  1308. $feed.= "<opml xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
  1309. $feed.= " <head>\n";
  1310. $feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
  1311. if ($this->pubDate!="") {
  1312. $date = new FeedDate($this->pubDate);
  1313. $feed.= " <dateCreated>".$date->rfc822()."</dateCreated>\n";
  1314. }
  1315. if ($this->lastBuildDate!="") {
  1316. $date = new FeedDate($this->lastBuildDate);
  1317. $feed.= " <dateModifi…

Large files files are truncated, but you can click here to view the full file