Source for file feedcreator.php

Documentation is available at feedcreator.php

  1. <?php
  2. /*************************************************************************
  3. FeedCreator class v1.7.2
  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.2 10-11-04
  23. license changed to LGPL
  24. v1.7.1
  25. fixed a syntax bug
  26. fixed left over debug code
  27. v1.7 07-18-04
  28. added HTML and JavaScript feeds (configurable via CSS) (thanks to Pascal Van Hecke)
  29. added HTML descriptions for all feed formats (thanks to Pascal Van Hecke)
  30. added a switch to select an external stylesheet (thanks to Pascal Van Hecke)
  31. changed default content-type to application/xml
  32. added character encoding setting
  33. fixed numerous smaller bugs (thanks to Sören Fuhrmann of golem.de)
  34. improved changing ATOM versions handling (thanks to August Trometer)
  35. improved the UniversalFeedCreator's useCached method (thanks to Sören Fuhrmann of golem.de)
  36. added charset output in HTTP headers (thanks to Sören Fuhrmann of golem.de)
  37. added Slashdot namespace to RSS 1.0 (thanks to Sören Fuhrmann of golem.de)
  38. v1.6 05-10-04
  39. added stylesheet to RSS 1.0 feeds
  40. fixed generator comment (thanks Kevin L. Papendick and Tanguy Pruvot)
  41. fixed RFC822 date bug (thanks Tanguy Pruvot)
  42. added TimeZone customization for RFC8601 (thanks Tanguy Pruvot)
  43. fixed Content-type could be empty (thanks Tanguy Pruvot)
  44. fixed author/creator in RSS1.0 (thanks Tanguy Pruvot)
  45. v1.6 beta 02-28-04
  46. added Atom 0.3 support (not all features, though)
  47. improved OPML 1.0 support (hopefully - added more elements)
  48. added support for arbitrary additional elements (use with caution)
  49. code beautification :-)
  50. considered beta due to some internal changes
  51. v1.5.1 01-27-04
  52. fixed some RSS 1.0 glitches (thanks to Stéphane Vanpoperynghe)
  53. fixed some inconsistencies between documentation and code (thanks to Timothy Martin)
  54. v1.5 01-06-04
  55. added support for OPML 1.0
  56. added more documentation
  57. v1.4 11-11-03
  58. optional feed saving and caching
  59. improved documentation
  60. minor improvements
  61. v1.3 10-02-03
  62. renamed to FeedCreator, as it not only creates RSS anymore
  63. added support for mbox
  64. tentative support for echo/necho/atom/pie/???
  65. v1.2 07-20-03
  66. intelligent auto-truncating of RSS 0.91 attributes
  67. don't create some attributes when they're not set
  68. documentation improved
  69. fixed a real and a possible bug with date conversions
  70. code cleanup
  71. v1.1 06-29-03
  72. added images to feeds
  73. now includes most RSS 0.91 attributes
  74. added RSS 2.0 feeds
  75. v1.0 06-24-03
  76. initial release
  77. ***************************************************************************/
  78.  
  79.  
  80. /*** GENERAL USAGE *********************************************************
  81. include("feedcreator.class.php");
  82. $rss = new UniversalFeedCreator();
  83. $rss->useCached(); // use cached version if age<1 hour
  84. $rss->title = "PHP news";
  85. $rss->description = "daily news from the PHP scripting world";
  86. //optional
  87. $rss->descriptionTruncSize = 500;
  88. $rss->descriptionHtmlSyndicated = true;
  89. $rss->link = "http://www.dailyphp.net/news";
  90. $rss->syndicationURL = "http://www.dailyphp.net/".$_SERVER["PHP_SELF"];
  91. $image = new FeedImage();
  92. $image->title = "dailyphp.net logo";
  93. $image->url = "http://www.dailyphp.net/images/logo.gif";
  94. $image->link = "http://www.dailyphp.net";
  95. $image->description = "Feed provided by dailyphp.net. Click to visit.";
  96. //optional
  97. $image->descriptionTruncSize = 500;
  98. $image->descriptionHtmlSyndicated = true;
  99. $rss->image = $image;
  100. // get your news items from somewhere, e.g. your database:
  101. mysql_select_db($dbHost, $dbUser, $dbPass);
  102. $res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
  103. while ($data = mysql_fetch_object($res)) {
  104. $item = new FeedItem();
  105. $item->title = $data->title;
  106. $item->link = $data->url;
  107. $item->description = $data->short;
  108. //optional
  109. item->descriptionTruncSize = 500;
  110. item->descriptionHtmlSyndicated = true;
  111. $item->date = $data->newsdate;
  112. $item->source = "http://www.dailyphp.net";
  113. $item->author = "John Doe";
  114. $rss->addItem($item);
  115. }
  116. // valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
  117. // MBOX, OPML, ATOM, ATOM0.3, HTML, JS
  118. echo $rss->saveFeed("RSS1.0", "news/feed.xml");
  119. **************************************************************************
  120. * A little setup *
  121. **************************************************************************/
  122.  
  123.  
  124. // your local timezone, set to "" to disable or for GMT
  125. define("TIME_ZONE","+01:00");
  126.  
  127.  
  128.  
  129.  
  130. /**
  131. * Version string.
  132. **/
  133.  
  134. define("FEEDCREATOR_VERSION", "FeedCreator 1.7.2");
  135.  
  136.  
  137.  
  138. /**
  139. * A FeedItem is a part of a FeedCreator feed.
  140. *
  141. * @author Kai Blankenhorn <kaib@bitfolge.de>
  142. * @since 1.3
  143. */
  144. class FeedItem extends HtmlDescribable {
  145. /**
  146. * Mandatory attributes of an item.
  147. */
  148. var $title, $description, $link;
  149. /**
  150. * Optional attributes of an item.
  151. */
  152. var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator;
  153. /**
  154. * Publishing date of an item. May be in one of the following formats:
  155. *
  156. * RFC 822:
  157. * "Mon, 20 Jan 03 18:05:41 +0400"
  158. * "20 Jan 03 18:05:41 +0000"
  159. *
  160. * ISO 8601:
  161. * "2003-01-20T18:05:41+04:00"
  162. *
  163. * Unix:
  164. * 1043082341
  165. */
  166. var $date;
  167. /**
  168. * Any additional elements to include as an assiciated array. All $key => $value pairs
  169. * will be included unencoded in the feed item in the form
  170. * <$key>$value</$key>
  171. * Again: No encoding will be used! This means you can invalidate or enhance the feed
  172. * if $value contains markup. This may be abused to embed tags not implemented by
  173. * the FeedCreator class used.
  174. */
  175. var $additionalElements = Array();
  176.  
  177. // on hold
  178. // var $source;
  179.  
  180. }
  181.  
  182.  
  183.  
  184. /**
  185. * An FeedImage may be added to a FeedCreator feed.
  186. * @author Kai Blankenhorn <kaib@bitfolge.de>
  187. * @since 1.3
  188. */
  189. class FeedImage extends HtmlDescribable {
  190. /**
  191. * Mandatory attributes of an image.
  192. */
  193. var $title, $url, $link;
  194. /**
  195. * Optional attributes of an image.
  196. */
  197. var $width, $height, $description;
  198. }
  199.  
  200.  
  201.  
  202. /**
  203. * An HtmlDescribable is an item within a feed that can have a description that may
  204. * include HTML markup.
  205. */
  206. class HtmlDescribable {
  207. /**
  208. * Indicates whether the description field should be rendered in HTML.
  209. */
  210. var $descriptionHtmlSyndicated;
  211. /**
  212. * Indicates whether and to how many characters a description should be truncated.
  213. */
  214. var $descriptionTruncSize;
  215. /**
  216. * Returns a formatted description field, depending on descriptionHtmlSyndicated and
  217. * $descriptionTruncSize properties
  218. * @return string the formatted description
  219. */
  220. function getDescription() {
  221. $descriptionField = new FeedHtmlField($this->description);
  222. $descriptionField->syndicateHtml = $this->descriptionHtmlSyndicated;
  223. $descriptionField->truncSize = $this->descriptionTruncSize;
  224. return $descriptionField->output();
  225. }
  226.  
  227. }
  228.  
  229.  
  230. /**
  231. * An FeedHtmlField describes and generates
  232. * a feed, item or image html field (probably a description). Output is
  233. * generated based on $truncSize, $syndicateHtml properties.
  234. * @author Pascal Van Hecke <feedcreator.class.php@vanhecke.info>
  235. * @version 1.6
  236. */
  237. class FeedHtmlField {
  238. /**
  239. * Mandatory attributes of a FeedHtmlField.
  240. */
  241. var $rawFieldContent;
  242. /**
  243. * Optional attributes of a FeedHtmlField.
  244. *
  245. */
  246. var $truncSize, $syndicateHtml;
  247. /**
  248. * Creates a new instance of FeedHtmlField.
  249. * @param $string: if given, sets the rawFieldContent property
  250. */
  251. function FeedHtmlField($parFieldContent) {
  252. if ($parFieldContent) {
  253. $this->rawFieldContent = $parFieldContent;
  254. }
  255. }
  256. /**
  257. * Creates the right output, depending on $truncSize, $syndicateHtml properties.
  258. * @return string the formatted field
  259. */
  260. function output() {
  261. // when field available and syndicated in html we assume
  262. // - valid html in $rawFieldContent and we enclose in CDATA tags
  263. // - no truncation (truncating risks producing invalid html)
  264. if (!$this->rawFieldContent) {
  265. $result = "";
  266. } elseif ($this->syndicateHtml) {
  267. $result = "<![CDATA[".$this->rawFieldContent."]]>";
  268. } else {
  269. if ($this->truncSize and is_int($this->truncSize)) {
  270. $result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
  271. } else {
  272. $result = htmlspecialchars($this->rawFieldContent);
  273. }
  274. }
  275. return $result;
  276. }
  277.  
  278. }
  279.  
  280.  
  281.  
  282. /**
  283. * UniversalFeedCreator lets you choose during runtime which
  284. * format to build.
  285. * For general usage of a feed class, see the FeedCreator class
  286. * below or the example above.
  287. *
  288. * @since 1.3
  289. * @author Kai Blankenhorn <kaib@bitfolge.de>
  290. */
  291. class UniversalFeedCreator extends FeedCreator {
  292. var $_feed;
  293. function _setFormat($format) {
  294. switch (strtoupper($format)) {
  295. case "2.0":
  296. // fall through
  297. case "RSS2.0":
  298. $this->_feed = new RSSCreator20();
  299. break;
  300. case "1.0":
  301. // fall through
  302. case "RSS1.0":
  303. $this->_feed = new RSSCreator10();
  304. break;
  305. case "0.91":
  306. // fall through
  307. case "RSS0.91":
  308. $this->_feed = new RSSCreator091();
  309. break;
  310. case "PIE0.1":
  311. $this->_feed = new PIECreator01();
  312. break;
  313. case "MBOX":
  314. $this->_feed = new MBOXCreator();
  315. break;
  316. case "OPML":
  317. $this->_feed = new OPMLCreator();
  318. break;
  319. case "ATOM":
  320. // fall through: always the latest ATOM version
  321. case "ATOM0.3":
  322. $this->_feed = new AtomCreator03();
  323. break;
  324. case "HTML":
  325. $this->_feed = new HTMLCreator();
  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.  
  372. /**
  373. * Turns on caching and checks if there is a recent version of this feed in the cache.
  374. * If there is, an HTTP redirect header is sent.
  375. * To effectively use caching, you should create the FeedCreator object and call this method
  376. * before anything else, especially before you do the time consuming task to build the feed
  377. * (web fetching, for example).
  378. *
  379. * @param string format format the feed should comply to. Valid values are:
  380. * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
  381. * @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()).
  382. * @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
  383. */
  384. function useCached($format="RSS0.91", $filename="", $timeout=3600) {
  385. $this->_setFormat($format);
  386. $this->_feed->useCached($filename, $timeout);
  387. }
  388.  
  389. }
  390.  
  391.  
  392. /**
  393. * FeedCreator is the abstract base implementation for concrete
  394. * implementations that implement a specific format of syndication.
  395. *
  396. * @abstract
  397. * @author Kai Blankenhorn <kaib@bitfolge.de>
  398. * @since 1.4
  399. */
  400. class FeedCreator extends HtmlDescribable {
  401.  
  402. /**
  403. * Mandatory attributes of a feed.
  404. */
  405. var $title, $description, $link;
  406. /**
  407. * Optional attributes of a feed.
  408. */
  409. var $syndicationURL, $image, $language, $copyright, $pubDate, $lastBuildDate, $editor, $editorEmail, $webmaster, $category, $docs, $ttl, $rating, $skipHours, $skipDays;
  410.  
  411. /**
  412. * The url of the external xsl stylesheet used to format the naked rss feed.
  413. * Ignored in the output when empty.
  414. */
  415. var $xslStyleSheet = "";
  416. /**
  417. * @access private
  418. */
  419. var $items = Array();
  420. /**
  421. * This feed's MIME content type.
  422. * @since 1.4
  423. * @access private
  424. */
  425. var $contentType = "application/xml";
  426. /**
  427. * This feed's character encoding.
  428. * @since 1.6.1
  429. ***/
  430. var $encoding = "ISO-8859-1";
  431. /**
  432. * Any additional elements to include as an assiciated array. All $key => $value pairs
  433. * will be included unencoded in the feed in the form
  434. * <$key>$value</$key>
  435. * Again: No encoding will be used! This means you can invalidate or enhance the feed
  436. * if $value contains markup. This may be abused to embed tags not implemented by
  437. * the FeedCreator class used.
  438. */
  439. var $additionalElements = Array();
  440. /**
  441. * Adds an FeedItem to the feed.
  442. *
  443. * @param object FeedItem $item The FeedItem to add to the feed.
  444. * @access public
  445. */
  446. function addItem($item) {
  447. $this->items[] = $item;
  448. }
  449. /**
  450. * Truncates a string to a certain length at the most sensible point.
  451. * First, if there's a '.' character near the end of the string, the string is truncated after this character.
  452. * If there is no '.', the string is truncated after the last ' ' character.
  453. * If the string is truncated, " ..." is appended.
  454. * If the string is already shorter than $length, it is returned unchanged.
  455. *
  456. * @static
  457. * @param string string A string to be truncated.
  458. * @param int length the maximum length the string should be truncated to
  459. * @return string the truncated string
  460. */
  461. function iTrunc($string, $length) {
  462. if (strlen($string)<=$length) {
  463. return $string;
  464. }
  465. $pos = strrpos($string,".");
  466. if ($pos>=$length-4) {
  467. $string = substr($string,0,$length-4);
  468. $pos = strrpos($string,".");
  469. }
  470. if ($pos>=$length*0.4) {
  471. return substr($string,0,$pos+1)." ...";
  472. }
  473. $pos = strrpos($string," ");
  474. if ($pos>=$length-4) {
  475. $string = substr($string,0,$length-4);
  476. $pos = strrpos($string," ");
  477. }
  478. if ($pos>=$length*0.4) {
  479. return substr($string,0,$pos)." ...";
  480. }
  481. return substr($string,0,$length-4)." ...";
  482. }
  483. /**
  484. * Creates a comment indicating the generator of this feed.
  485. * The format of this comment seems to be recognized by
  486. * Syndic8.com.
  487. */
  488. function _createGeneratorComment() {
  489. return "<!-- generator=\"".FEEDCREATOR_VERSION."\" -->\n";
  490. }
  491. /**
  492. * Creates a string containing all additional elements specified in
  493. * $additionalElements.
  494. * @param elements array an associative array containing key => value pairs
  495. * @param indentString string a string that will be inserted before every generated line
  496. * @return string the XML tags corresponding to $additionalElements
  497. */
  498. function _createAdditionalElements($elements, $indentString="") {
  499. $ae = "";
  500. if (is_array($elements)) {
  501. foreach($elements AS $key => $value) {
  502. $ae.= $indentString."<$key>$value</$key>\n";
  503. }
  504. }
  505. return $ae;
  506. }
  507. function _createStylesheetReferences() {
  508. $xml = "";
  509. if ($this->cssStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"?>\n";
  510. if ($this->xslStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"?>\n";
  511. return $xml;
  512. }
  513. /**
  514. * Builds the feed's text.
  515. * @abstract
  516. * @return string the feed's complete text
  517. */
  518. function createFeed() {
  519. }
  520. /**
  521. * Generate a filename for the feed cache file. The result will be $_SERVER["PHP_SELF"] with the extension changed to .xml.
  522. * For example:
  523. *
  524. * echo $_SERVER["PHP_SELF"]."\n";
  525. * echo FeedCreator::_generateFilename();
  526. *
  527. * would produce:
  528. *
  529. * /rss/latestnews.php
  530. * latestnews.xml
  531. *
  532. * @return string the feed cache filename
  533. * @since 1.4
  534. * @access private
  535. */
  536. function _generateFilename() {
  537. $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  538. return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";
  539. }
  540. /**
  541. * @since 1.4
  542. * @access private
  543. */
  544. function _redirect($filename) {
  545. // attention, heavily-commented-out-area
  546. // maybe use this in addition to file time checking
  547. //Header("Expires: ".date("r",time()+$this->_timeout));
  548. /* no caching at all, doesn't seem to work as good:
  549. Header("Cache-Control: no-cache");
  550. Header("Pragma: no-cache");
  551. */
  552. // HTTP redirect, some feed readers' simple HTTP implementations don't follow it
  553. //Header("Location: ".$filename);
  554.  
  555. Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename));
  556. Header("Content-Disposition: inline; filename=".basename($filename));
  557. readfile($filename, "r");
  558. die();
  559. }
  560. /**
  561. * Turns on caching and checks if there is a recent version of this feed in the cache.
  562. * If there is, an HTTP redirect header is sent.
  563. * To effectively use caching, you should create the FeedCreator object and call this method
  564. * before anything else, especially before you do the time consuming task to build the feed
  565. * (web fetching, for example).
  566. * @since 1.4
  567. * @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()).
  568. * @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
  569. */
  570. function useCached($filename="", $timeout=3600) {
  571. $this->_timeout = $timeout;
  572. if ($filename=="") {
  573. $filename = $this->_generateFilename();
  574. }
  575. if (file_exists($filename) AND (time()-filemtime($filename) < $timeout)) {
  576. $this->_redirect($filename);
  577. }
  578. }
  579. /**
  580. * Saves this feed as a file on the local disk. After the file is saved, a redirect
  581. * header may be sent to redirect the user to the newly created file.
  582. * @since 1.4
  583. *
  584. * @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()).
  585. * @param redirect boolean optional send an HTTP redirect header or not. If true, the user will be automatically redirected to the created file.
  586. */
  587. function saveFeed($filename="", $displayContents=true) {
  588. if ($filename=="") {
  589. $filename = $this->_generateFilename();
  590. }
  591. $feedFile = fopen($filename, "w+");
  592. if ($feedFile) {
  593. fputs($feedFile,$this->createFeed());
  594. fclose($feedFile);
  595. if ($displayContents) {
  596. $this->_redirect($filename);
  597. }
  598. } else {
  599. echo "<br /><b>Error creating feed file, please check write permissions.</b><br />";
  600. }
  601. }
  602. }
  603.  
  604.  
  605. /**
  606. * FeedDate is an internal class that stores a date for a feed or feed item.
  607. * Usually, you won't need to use this.
  608. */
  609. class FeedDate {
  610. var $unix;
  611. /**
  612. * Creates a new instance of FeedDate representing a given date.
  613. * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
  614. * @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and time is used.
  615. */
  616. function FeedDate($dateString="") {
  617. if ($dateString=="") $dateString = date("r");
  618. if (is_integer($dateString)) {
  619. $this->unix = $dateString;
  620. return;
  621. }
  622. 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)) {
  623. $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);
  624. $this->unix = mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
  625. if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
  626. $tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
  627. } else {
  628. if (strlen($matches[7])==1) {
  629. $oneHour = 3600;
  630. $ord = ord($matches[7]);
  631. if ($ord < ord("M")) {
  632. $tzOffset = (ord("A") - $ord - 1) * $oneHour;
  633. } elseif ($ord >= ord("M") AND $matches[7]!="Z") {
  634. $tzOffset = ($ord - ord("M")) * $oneHour;
  635. } elseif ($matches[7]=="Z") {
  636. $tzOffset = 0;
  637. }
  638. }
  639. switch ($matches[7]) {
  640. case "UT":
  641. case "GMT": $tzOffset = 0;
  642. }
  643. }
  644. $this->unix += $tzOffset;
  645. return;
  646. }
  647. if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)) {
  648. $this->unix = mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
  649. if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
  650. $tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
  651. } else {
  652. if ($matches[7]=="Z") {
  653. $tzOffset = 0;
  654. }
  655. }
  656. $this->unix += $tzOffset;
  657. return;
  658. }
  659. $this->unix = 0;
  660. }
  661.  
  662. /**
  663. * Gets the date stored in this FeedDate as an RFC 822 date.
  664. *
  665. * @return a date in RFC 822 format
  666. */
  667. function rfc822() {
  668. //return gmdate("r",$this->unix);
  669. $date = gmdate("D, d M Y H:i:s", $this->unix);
  670. if (TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE);
  671. return $date;
  672. }
  673. /**
  674. * Gets the date stored in this FeedDate as an ISO 8601 date.
  675. *
  676. * @return a date in ISO 8601 format
  677. */
  678. function iso8601() {
  679. $date = gmdate("Y-m-d\TH:i:sO",$this->unix);
  680. $date = substr($date,0,22) . ':' . substr($date,-2);
  681. if (TIME_ZONE!="") $date = str_replace("+00:00",TIME_ZONE,$date);
  682. return $date;
  683. }
  684. /**
  685. * Gets the date stored in this FeedDate as unix time stamp.
  686. *
  687. * @return a date as a unix time stamp
  688. */
  689. function unix() {
  690. return $this->unix;
  691. }
  692. }
  693.  
  694.  
  695. /**
  696. * RSSCreator10 is a FeedCreator that implements RDF Site Summary (RSS) 1.0.
  697. *
  698. * @see http://www.purl.org/rss/1.0/
  699. * @since 1.3
  700. * @author Kai Blankenhorn <kaib@bitfolge.de>
  701. */
  702. class RSSCreator10 extends FeedCreator {
  703.  
  704. /**
  705. * Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
  706. * The feed will contain all items previously added in the same order.
  707. * @return string the feed's complete text
  708. */
  709. function createFeed() {
  710. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  711. $feed.= $this->_createGeneratorComment();
  712. if ($this->cssStyleSheet=="") {
  713. $cssStyleSheet = "http://www.w3.org/2000/08/w3c-synd/style.css";
  714. }
  715. $feed.= $this->_createStylesheetReferences();
  716. $feed.= "<rdf:RDF\n";
  717. $feed.= " xmlns=\"http://purl.org/rss/1.0/\"\n";
  718. $feed.= " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
  719. $feed.= " xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n";
  720. $feed.= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
  721. $feed.= " <channel rdf:about=\"".$this->syndicationURL."\">\n";
  722. $feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
  723. $feed.= " <description>".htmlspecialchars($this->description)."</description>\n";
  724. $feed.= " <link>".$this->link."</link>\n";
  725. if ($this->image!=null) {
  726. $feed.= " <image rdf:resource=\"".$this->image->url."\" />\n";
  727. }
  728. $now = new FeedDate();
  729. $feed.= " <dc:date>".htmlspecialchars($now->iso8601())."</dc:date>\n";
  730. $feed.= " <items>\n";
  731. $feed.= " <rdf:Seq>\n";
  732. for ($i=0;$i<count($this->items);$i++) {
  733. $feed.= " <rdf:li rdf:resource=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
  734. }
  735. $feed.= " </rdf:Seq>\n";
  736. $feed.= " </items>\n";
  737. $feed.= " </channel>\n";
  738. if ($this->image!=null) {
  739. $feed.= " <image rdf:about=\"".$this->image->url."\">\n";
  740. $feed.= " <title>".$this->image->title."</title>\n";
  741. $feed.= " <link>".$this->image->link."</link>\n";
  742. $feed.= " <url>".$this->image->url."</url>\n";
  743. $feed.=