PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/bitfolge/feedcreator.php

https://github.com/viollarr/alab
PHP | 1883 lines | 898 code | 217 blank | 768 comment | 125 complexity | 88090250f557aaf5d848fce9753b152f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-3.0, Apache-2.0, BSD-3-Clause, GPL-3.0

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

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

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