Guest User

Untitled

a guest
Oct 15th, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 52.70 KB | None | 0 0
  1. <?php
  2. /***************************************************************************
  3.  
  4. FeedCreator class v1.7.2
  5. originally (c) Kai Blankenhorn
  6. www.bitfolge.de
  7. v1.3 work by Scott Reynen ([email protected]) and Kai Blankenhorn
  8. v1.5 OPML support by Dirk Clemens
  9.  
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 2.1 of the License, or (at your option) any later version.
  14.  
  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.  
  20. You should have received a copy of the GNU Lesser General Public
  21. License along with this library; if not, write to the Free Software
  22. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23.  
  24. ****************************************************************************
  25. ds-syndicate 1.1.0
  26. Use 'r' date() format for rfc822 dates (uses server time zone, PHP 4.0.4 or later)
  27. define TIME_ZONE only if not already defined (not used for rfc822 dates).
  28. class FeedCreator: added instance variable $cssStyleSheet = ""; already referenced in one of its functions.
  29. saveFeed(): if write error, also logs to web server error log
  30. When appropriate, return HTTP 304 Not Modified instead of redirected file.
  31. Log that oh-so-common permissions error
  32. RSSCreator20(): set contentType.
  33.  
  34. Changelog:
  35.  
  36. v1.7.2 10-11-04
  37. license changed to LGPL
  38.  
  39. v1.7.1
  40. fixed a syntax bug
  41. fixed left over debug code
  42.  
  43. v1.7 07-18-04
  44. added HTML and JavaScript feeds (configurable via CSS) (thanks to Pascal Van Hecke)
  45. added HTML descriptions for all feed formats (thanks to Pascal Van Hecke)
  46. added a switch to select an external stylesheet (thanks to Pascal Van Hecke)
  47. changed default content-type to application/xml
  48. added character encoding setting
  49. fixed numerous smaller bugs (thanks to S�ren Fuhrmann of golem.de)
  50. improved changing ATOM versions handling (thanks to August Trometer)
  51. improved the UniversalFeedCreator's useCached method (thanks to S�ren Fuhrmann of golem.de)
  52. added charset output in HTTP headers (thanks to S�ren Fuhrmann of golem.de)
  53. added Slashdot namespace to RSS 1.0 (thanks to S�ren Fuhrmann of golem.de)
  54.  
  55. v1.6 05-10-04
  56. added stylesheet to RSS 1.0 feeds
  57. fixed generator comment (thanks Kevin L. Papendick and Tanguy Pruvot)
  58. fixed RFC822 date bug (thanks Tanguy Pruvot)
  59. added TimeZone customization for RFC8601 (thanks Tanguy Pruvot)
  60. fixed Content-type could be empty (thanks Tanguy Pruvot)
  61. fixed author/creator in RSS1.0 (thanks Tanguy Pruvot)
  62.  
  63. v1.6 beta 02-28-04
  64. added Atom 0.3 support (not all features, though)
  65. improved OPML 1.0 support (hopefully - added more elements)
  66. added support for arbitrary additional elements (use with caution)
  67. code beautification :-)
  68. considered beta due to some internal changes
  69.  
  70. v1.5.1 01-27-04
  71. fixed some RSS 1.0 glitches (thanks to St�phane Vanpoperynghe)
  72. fixed some inconsistencies between documentation and code (thanks to Timothy Martin)
  73.  
  74. v1.5 01-06-04
  75. added support for OPML 1.0
  76. added more documentation
  77.  
  78. v1.4 11-11-03
  79. optional feed saving and caching
  80. improved documentation
  81. minor improvements
  82.  
  83. v1.3 10-02-03
  84. renamed to FeedCreator, as it not only creates RSS anymore
  85. added support for mbox
  86. tentative support for echo/necho/atom/pie/???
  87.  
  88. v1.2 07-20-03
  89. intelligent auto-truncating of RSS 0.91 attributes
  90. don't create some attributes when they're not set
  91. documentation improved
  92. fixed a real and a possible bug with date conversions
  93. code cleanup
  94.  
  95. v1.1 06-29-03
  96. added images to feeds
  97. now includes most RSS 0.91 attributes
  98. added RSS 2.0 feeds
  99.  
  100. v1.0 06-24-03
  101. initial release
  102.  
  103.  
  104.  
  105. ***************************************************************************/
  106.  
  107. /*** GENERAL USAGE *********************************************************
  108.  
  109. include("feedcreator.class.php");
  110.  
  111. $rss = new UniversalFeedCreator();
  112. $rss->useCached(); // use cached version if age<1 hour
  113. $rss->title = "PHP news";
  114. $rss->description = "daily news from the PHP scripting world";
  115.  
  116. //optional
  117. $rss->descriptionTruncSize = 500;
  118. $rss->descriptionHtmlSyndicated = true;
  119.  
  120. $rss->link = "http://www.dailyphp.net/news";
  121. $rss->syndicationURL = "http://www.dailyphp.net/".$_SERVER["PHP_SELF"];
  122.  
  123. $image = new FeedImage();
  124. $image->title = "dailyphp.net logo";
  125. $image->url = "http://www.dailyphp.net/images/logo.gif";
  126. $image->link = "http://www.dailyphp.net";
  127. $image->description = "Feed provided by dailyphp.net. Click to visit.";
  128.  
  129. //optional
  130. $image->descriptionTruncSize = 500;
  131. $image->descriptionHtmlSyndicated = true;
  132.  
  133. $rss->image = $image;
  134.  
  135. // get your news items from somewhere, e.g. your database:
  136. mysql_select_db($dbHost, $dbUser, $dbPass);
  137. $res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
  138. while ($data = mysql_fetch_object($res)) {
  139. $item = new FeedItem();
  140. $item->title = $data->title;
  141. $item->link = $data->url;
  142. $item->description = $data->short;
  143.  
  144. //optional
  145. item->descriptionTruncSize = 500;
  146. item->descriptionHtmlSyndicated = true;
  147.  
  148. $item->date = $data->newsdate;
  149. $item->source = "http://www.dailyphp.net";
  150. $item->author = "John Doe";
  151.  
  152. $rss->addItem($item);
  153. }
  154.  
  155. // valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
  156. // MBOX, OPML, ATOM, ATOM0.3, HTML, JS
  157. echo $rss->saveFeed("RSS1.0", "news/feed.xml");
  158.  
  159.  
  160. ***************************************************************************
  161. * A little setup *
  162. **************************************************************************/
  163.  
  164. // your local timezone, set to "" to disable or for GMT
  165. if (!defined("TIME_ZONE")) {
  166. define("TIME_ZONE","+01:00");
  167. }
  168.  
  169. /**
  170. * Version string.
  171. **/
  172. define("FEEDCREATOR_VERSION", "FeedCreator 1.7.2");
  173.  
  174.  
  175.  
  176. /**
  177. * A FeedItem is a part of a FeedCreator feed.
  178. *
  179. * @author Kai Blankenhorn <[email protected]>
  180. * @since 1.3
  181. */
  182. class FeedItem extends HtmlDescribable {
  183. /**
  184. * Mandatory attributes of an item.
  185. */
  186. var $title, $description, $link;
  187.  
  188. /**
  189. * Optional attributes of an item.
  190. */
  191. var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator;
  192.  
  193. /**
  194. * Publishing date of an item. May be in one of the following formats:
  195. *
  196. * RFC 822:
  197. * "Mon, 20 Jan 03 18:05:41 +0400"
  198. * "20 Jan 03 18:05:41 +0000"
  199. *
  200. * ISO 8601:
  201. * "2003-01-20T18:05:41+04:00"
  202. *
  203. * Unix:
  204. * 1043082341
  205. */
  206. var $date;
  207.  
  208. /**
  209. * Any additional elements to include as an assiciated array. All $key => $value pairs
  210. * will be included unencoded in the feed item in the form
  211. * <$key>$value</$key>
  212. * Again: No encoding will be used! This means you can invalidate or enhance the feed
  213. * if $value contains markup. This may be abused to embed tags not implemented by
  214. * the FeedCreator class used.
  215. */
  216. var $additionalElements = Array();
  217.  
  218. // on hold
  219. // var $source;
  220. }
  221.  
  222.  
  223.  
  224. /**
  225. * An FeedImage may be added to a FeedCreator feed.
  226. * @author Kai Blankenhorn <[email protected]>
  227. * @since 1.3
  228. */
  229. class FeedImage extends HtmlDescribable {
  230. /**
  231. * Mandatory attributes of an image.
  232. */
  233. var $title, $url, $link;
  234.  
  235. /**
  236. * Optional attributes of an image.
  237. */
  238. var $width, $height, $description;
  239. }
  240.  
  241.  
  242.  
  243. /**
  244. * An HtmlDescribable is an item within a feed that can have a description that may
  245. * include HTML markup.
  246. */
  247. class HtmlDescribable {
  248. /**
  249. * Indicates whether the description field should be rendered in HTML.
  250. */
  251. var $descriptionHtmlSyndicated;
  252.  
  253. /**
  254. * Indicates whether and to how many characters a description should be truncated.
  255. */
  256. var $descriptionTruncSize;
  257.  
  258. /**
  259. * Returns a formatted description field, depending on descriptionHtmlSyndicated and
  260. * $descriptionTruncSize properties
  261. * @return string the formatted description
  262. */
  263. function getDescription() {
  264. $descriptionField = new FeedHtmlField($this->description);
  265. $descriptionField->syndicateHtml = $this->descriptionHtmlSyndicated;
  266. $descriptionField->truncSize = $this->descriptionTruncSize;
  267. return $descriptionField->output();
  268. }
  269.  
  270. }
  271.  
  272.  
  273. /**
  274. * An FeedHtmlField describes and generates
  275. * a feed, item or image html field (probably a description). Output is
  276. * generated based on $truncSize, $syndicateHtml properties.
  277. * @author Pascal Van Hecke <[email protected]>
  278. * @version 1.6
  279. */
  280. class FeedHtmlField {
  281. /**
  282. * Mandatory attributes of a FeedHtmlField.
  283. */
  284. var $rawFieldContent;
  285.  
  286. /**
  287. * Optional attributes of a FeedHtmlField.
  288. *
  289. */
  290. var $truncSize, $syndicateHtml;
  291.  
  292. /**
  293. * Creates a new instance of FeedHtmlField.
  294. * @param $string: if given, sets the rawFieldContent property
  295. */
  296. function FeedHtmlField($parFieldContent) {
  297. if ($parFieldContent) {
  298. $this->rawFieldContent = $parFieldContent;
  299. }
  300. }
  301.  
  302.  
  303. /**
  304. * Creates the right output, depending on $truncSize, $syndicateHtml properties.
  305. * @return string the formatted field
  306. */
  307. function output() {
  308. // when field available and syndicated in html we assume
  309. // - valid html in $rawFieldContent and we enclose in CDATA tags
  310. // - no truncation (truncating risks producing invalid html)
  311. if (!$this->rawFieldContent) {
  312. $result = "";
  313. } elseif ($this->syndicateHtml) {
  314. $result = "<![CDATA[".$this->rawFieldContent."]]>";
  315. } else {
  316. if ($this->truncSize and is_int($this->truncSize)) {
  317. $result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
  318. } else {
  319. $result = htmlspecialchars($this->rawFieldContent);
  320. }
  321. }
  322. return $result;
  323. }
  324.  
  325. }
  326.  
  327.  
  328.  
  329. /**
  330. * UniversalFeedCreator lets you choose during runtime which
  331. * format to build.
  332. * For general usage of a feed class, see the FeedCreator class
  333. * below or the example above.
  334. *
  335. * @since 1.3
  336. * @author Kai Blankenhorn <[email protected]>
  337. */
  338. class UniversalFeedCreator extends FeedCreator {
  339. var $_feed;
  340.  
  341. function _setFormat($format) {
  342. switch (strtoupper($format)) {
  343.  
  344. case "2.0":
  345. // fall through
  346. case "RSS2.0":
  347. $this->_feed = new RSSCreator20();
  348. break;
  349.  
  350. case "1.0":
  351. // fall through
  352. case "RSS1.0":
  353. $this->_feed = new RSSCreator10();
  354. break;
  355.  
  356. case "0.91":
  357. // fall through
  358. case "RSS0.91":
  359. $this->_feed = new RSSCreator091();
  360. break;
  361.  
  362. case "PIE0.1":
  363. $this->_feed = new PIECreator01();
  364. break;
  365.  
  366. case "MBOX":
  367. $this->_feed = new MBOXCreator();
  368. break;
  369.  
  370. case "OPML":
  371. $this->_feed = new OPMLCreator();
  372. break;
  373.  
  374. case "ATOM":
  375. // fall through: always the latest ATOM version
  376.  
  377. case "ATOM0.3":
  378. $this->_feed = new AtomCreator03();
  379. break;
  380.  
  381. case "HTML":
  382. $this->_feed = new HTMLCreator();
  383. break;
  384.  
  385. case "JS":
  386. // fall through
  387. case "JAVASCRIPT":
  388. $this->_feed = new JSCreator();
  389. break;
  390.  
  391. default:
  392. $this->_feed = new RSSCreator091();
  393. break;
  394. }
  395.  
  396. $vars = get_object_vars($this);
  397. foreach ($vars as $key => $value) {
  398. // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
  399. if (!in_array($key, array("_feed", "contentType", "encoding"))) {
  400. $this->_feed->{$key} = $this->{$key};
  401. }
  402. }
  403. }
  404.  
  405. /**
  406. * Creates a syndication feed based on the items previously added.
  407. *
  408. * @see FeedCreator::addItem()
  409. * @param string format format the feed should comply to. Valid values are:
  410. * "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
  411. * @return string the contents of the feed.
  412. */
  413. function createFeed($format = "RSS0.91") {
  414. $this->_setFormat($format);
  415. return $this->_feed->createFeed();
  416. }
  417.  
  418.  
  419.  
  420. /**
  421. * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
  422. * header may be sent to redirect the use to the newly created file.
  423. * @since 1.4
  424. *
  425. * @param string format format the feed should comply to. Valid values are:
  426. * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", "ATOM0.3", "HTML", "JS"
  427. * @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()).
  428. * @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.
  429. */
  430. function saveFeed($format="RSS0.91", $filename="", $displayContents=true) {
  431. $this->_setFormat($format);
  432. $this->_feed->saveFeed($filename, $displayContents);
  433. }
  434.  
  435.  
  436. /**
  437. * Turns on caching and checks if there is a recent version of this feed in the cache.
  438. * If there is, an HTTP redirect header is sent.
  439. * To effectively use caching, you should create the FeedCreator object and call this method
  440. * before anything else, especially before you do the time consuming task to build the feed
  441. * (web fetching, for example).
  442. *
  443. * @param string format format the feed should comply to. Valid values are:
  444. * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
  445. * @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()).
  446. * @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
  447. */
  448. function useCached($format="RSS0.91", $filename="", $timeout=3600) {
  449. $this->_setFormat($format);
  450. $this->_feed->useCached($filename, $timeout);
  451. }
  452.  
  453. }
  454.  
  455.  
  456. /**
  457. * FeedCreator is the abstract base implementation for concrete
  458. * implementations that implement a specific format of syndication.
  459. *
  460. * @abstract
  461. * @author Kai Blankenhorn <[email protected]>
  462. * @since 1.4
  463. */
  464. class FeedCreator extends HtmlDescribable {
  465.  
  466. /**
  467. * Mandatory attributes of a feed.
  468. */
  469. var $title, $description, $link;
  470.  
  471.  
  472. /**
  473. * Optional attributes of a feed.
  474. */
  475. var $syndicationURL, $image, $language, $copyright, $pubDate, $lastBuildDate, $editor, $editorEmail, $webmaster, $category, $docs, $ttl, $rating, $skipHours, $skipDays;
  476.  
  477. /**
  478. * The url of the external xsl stylesheet used to format the naked rss feed.
  479. * Ignored in the output when empty.
  480. */
  481. var $xslStyleSheet = "";
  482.  
  483. // needed for _createStylesheetReferences()
  484. var $cssStyleSheet = "";
  485.  
  486. /**
  487. * @access private
  488. */
  489. var $items = Array();
  490.  
  491.  
  492. /**
  493. * This feed's MIME content type.
  494. * @since 1.4
  495. * @access private
  496. */
  497. var $contentType = "application/xml";
  498.  
  499.  
  500. /**
  501. * This feed's character encoding.
  502. * @since 1.6.1
  503. **/
  504. var $encoding = "UTF-8";
  505.  
  506.  
  507. /**
  508. * Any additional elements to include as an assiciated array. All $key => $value pairs
  509. * will be included unencoded in the feed in the form
  510. * <$key>$value</$key>
  511. * Again: No encoding will be used! This means you can invalidate or enhance the feed
  512. * if $value contains markup. This may be abused to embed tags not implemented by
  513. * the FeedCreator class used.
  514. */
  515. var $additionalElements = Array();
  516.  
  517.  
  518. /**
  519. * Adds an FeedItem to the feed.
  520. *
  521. * @param object FeedItem $item The FeedItem to add to the feed.
  522. * @access public
  523. */
  524. function addItem($item) {
  525. $this->items[] = $item;
  526. }
  527.  
  528.  
  529. /**
  530. * Truncates a string to a certain length at the most sensible point.
  531. * First, if there's a '.' character near the end of the string, the string is truncated after this character.
  532. * If there is no '.', the string is truncated after the last ' ' character.
  533. * If the string is truncated, " ..." is appended.
  534. * If the string is already shorter than $length, it is returned unchanged.
  535. *
  536. * @static
  537. * @param string string A string to be truncated.
  538. * @param int length the maximum length the string should be truncated to
  539. * @return string the truncated string
  540. */
  541. function iTrunc($string, $length) {
  542. if (strlen($string)<=$length) {
  543. return $string;
  544. }
  545.  
  546. $pos = strrpos($string,".");
  547. if ($pos>=$length-4) {
  548. $string = substr($string,0,$length-4);
  549. $pos = strrpos($string,".");
  550. }
  551. if ($pos>=$length*0.4) {
  552. return substr($string,0,$pos+1)." ...";
  553. }
  554.  
  555. $pos = strrpos($string," ");
  556. if ($pos>=$length-4) {
  557. $string = substr($string,0,$length-4);
  558. $pos = strrpos($string," ");
  559. }
  560. if ($pos>=$length*0.4) {
  561. return substr($string,0,$pos)." ...";
  562. }
  563.  
  564. return substr($string,0,$length-4)." ...";
  565.  
  566. }
  567.  
  568.  
  569. /**
  570. * Creates a comment indicating the generator of this feed.
  571. * The format of this comment seems to be recognized by
  572. * Syndic8.com.
  573. */
  574. function _createGeneratorComment() {
  575. return "<!-- generator=\"".FEEDCREATOR_VERSION."\" -->\n";
  576. }
  577.  
  578.  
  579. /**
  580. * Creates a string containing all additional elements specified in
  581. * $additionalElements.
  582. * @param elements array an associative array containing key => value pairs
  583. * @param indentString string a string that will be inserted before every generated line
  584. * @return string the XML tags corresponding to $additionalElements
  585. */
  586. function _createAdditionalElements($elements, $indentString="") {
  587. $ae = "";
  588. if (is_array($elements)) {
  589. foreach($elements AS $key => $value) {
  590. $ae.= $indentString."<$key>$value</$key>\n";
  591. }
  592. }
  593. return $ae;
  594. }
  595.  
  596. function _createStylesheetReferences() {
  597. $xml = "";
  598. if (isset($this->cssStyleSheet)) $xml .= "<?xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"?>\n";
  599. if (isset($this->xslStyleSheet)) $xml .= "<?xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"?>\n";
  600. return $xml;
  601. }
  602.  
  603.  
  604. /**
  605. * Builds the feed's text.
  606. * @abstract
  607. * @return string the feed's complete text
  608. */
  609. function createFeed() {
  610. }
  611.  
  612. /**
  613. * Generate a filename for the feed cache file. The result will be $_SERVER["PHP_SELF"] with the extension changed to .xml.
  614. * For example:
  615. *
  616. * echo $_SERVER["PHP_SELF"]."\n";
  617. * echo FeedCreator::_generateFilename();
  618. *
  619. * would produce:
  620. *
  621. * /rss/latestnews.php
  622. * latestnews.xml
  623. *
  624. * @return string the feed cache filename
  625. * @since 1.4
  626. * @access private
  627. */
  628. function _generateFilename() {
  629. $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  630. return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";
  631. }
  632.  
  633.  
  634. /**
  635. * @since 1.4
  636. * @access private
  637. */
  638. function _redirect($filename) {
  639. // attention, heavily-commented-out-area
  640.  
  641. // maybe use this in addition to file time checking
  642. //Header("Expires: ".date("r",time()+$this->_timeout));
  643.  
  644. /* no caching at all, doesn't seem to work as good:
  645. Header("Cache-Control: no-cache");
  646. Header("Pragma: no-cache");
  647. */
  648.  
  649. // HTTP redirect, some feed readers' simple HTTP implementations don't follow it
  650. //Header("Location: ".$filename);
  651.  
  652. Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename));
  653. Header("Content-Disposition: inline; filename=".basename($filename));
  654. readfile($filename, "r");
  655. die();
  656. }
  657.  
  658. /**
  659. * Turns on caching and checks if there is a recent version of this feed in the cache.
  660. * If there is, an HTTP redirect header is sent.
  661. * To effectively use caching, you should create the FeedCreator object and call this method
  662. * before anything else, especially before you do the time consuming task to build the feed
  663. * (web fetching, for example).
  664. * When appropriate, returns HTTP 304 Not Modified instead of file.
  665. * @since 1.4
  666. * @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()).
  667. * @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
  668. */
  669. function useCached($filename="", $timeout=3600) {
  670. $this->_timeout = $timeout;
  671. if ($filename=="") {
  672. $filename = $this->_generateFilename();
  673. }
  674. if (file_exists($filename) AND (time()-filemtime($filename) < $timeout)) {
  675. // But first: Conditional GET: Use HTTP 1.1 field to see if client already has latest version
  676. doConditionalGet(filemtime($filename)); // May exit
  677. $this->_redirect($filename);
  678. }
  679. }
  680.  
  681.  
  682. /**
  683. * Saves this feed as a file on the local disk. After the file is saved, a redirect
  684. * header may be sent to redirect the user to the newly created file.
  685. * @since 1.4
  686. *
  687. * @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()).
  688. * @param redirect boolean optional send an HTTP redirect header or not. If true, the user will be automatically redirected to the created file.
  689. */
  690. function saveFeed($filename="", $displayContents=true) {
  691. if ($filename=="") {
  692. $filename = $this->_generateFilename();
  693. }
  694. $feedFile = fopen($filename, "w+");
  695. if ($feedFile) {
  696. fputs($feedFile,$this->createFeed());
  697. fclose($feedFile);
  698. if ($displayContents) {
  699. $this->_redirect($filename);
  700. }
  701. } else {
  702. error_log("FeedCreator: Error creating feed file, please check write permissions.",0);
  703. echo "<br /><b>Error creating feed file, please check write permissions.</b><br />";
  704. }
  705. }
  706.  
  707. }
  708.  
  709.  
  710. /**
  711. * FeedDate is an internal class that stores a date for a feed or feed item.
  712. * Usually, you won't need to use this.
  713. */
  714. class FeedDate {
  715. var $unix;
  716.  
  717. /**
  718. * Creates a new instance of FeedDate representing a given date.
  719. * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
  720. * @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and time is used.
  721. */
  722. function FeedDate($dateString="") {
  723. if ($dateString=="") $dateString = time();
  724.  
  725. if (is_integer($dateString)) {
  726. $this->unix = $dateString;
  727. return;
  728. }
  729. 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)) {
  730. $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);
  731. $this->unix = mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
  732. if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
  733. $tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
  734. } else {
  735. if (strlen($matches[7])==1) {
  736. $oneHour = 3600;
  737. $ord = ord($matches[7]);
  738. if ($ord < ord("M")) {
  739. $tzOffset = (ord("A") - $ord - 1) * $oneHour;
  740. } elseif ($ord >= ord("M") AND $matches[7]!="Z") {
  741. $tzOffset = ($ord - ord("M")) * $oneHour;
  742. } elseif ($matches[7]=="Z") {
  743. $tzOffset = 0;
  744. }
  745. }
  746. switch ($matches[7]) {
  747. case "UT":
  748. case "GMT": $tzOffset = 0;
  749. }
  750. }
  751. $this->unix += $tzOffset;
  752. return;
  753. }
  754. if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)) {
  755. $this->unix = mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
  756. if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
  757. $tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
  758. } else {
  759. if ($matches[7]=="Z") {
  760. $tzOffset = 0;
  761. }
  762. }
  763. $this->unix += $tzOffset;
  764. return;
  765. }
  766. $this->unix = 0;
  767. }
  768.  
  769. /**
  770. * Gets the date stored in this FeedDate as an RFC 822 date.
  771. *
  772. * @return a date in RFC 822 format
  773. */
  774. function rfc822() {
  775. return gmdate("r",$this->unix);
  776. /*
  777. $date = gmdate("D, d M Y H:i:s", $this->unix);
  778. if (TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE);
  779. return $date;
  780. */
  781. }
  782.  
  783. /**
  784. * Gets the date stored in this FeedDate as an ISO 8601 date.
  785. *
  786. * @return a date in ISO 8601 format
  787. */
  788. function iso8601() {
  789. $date = gmdate("Y-m-d\TH:i:sO",$this->unix);
  790. $date = substr($date,0,22) . ':' . substr($date,-2);
  791. if (TIME_ZONE!="") $date = str_replace("+00:00",TIME_ZONE,$date);
  792. return $date;
  793. }
  794.  
  795. /**
  796. * Gets the date stored in this FeedDate as unix time stamp.
  797. *
  798. * @return a date as a unix time stamp
  799. */
  800. function unix() {
  801. return $this->unix;
  802. }
  803. }
  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 <[email protected]>
  812. */
  813. class RSSCreator10 extends FeedCreator {
  814.  
  815. /**
  816. * Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
  817. * The feed will contain all items previously added in the same order.
  818. * @return string the feed's complete text
  819. */
  820. function createFeed() {
  821. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  822. $feed.= $this->_createGeneratorComment();
  823. if ($this->cssStyleSheet=="") {
  824. $cssStyleSheet = "http://www.w3.org/2000/08/w3c-synd/style.css";
  825. }
  826. $feed.= $this->_createStylesheetReferences();
  827. $feed.= "<rdf:RDF\n";
  828. $feed.= " xmlns=\"http://purl.org/rss/1.0/\"\n";
  829. $feed.= " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
  830. $feed.= " xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n";
  831. $feed.= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
  832. $feed.= " <channel rdf:about=\"".$this->syndicationURL."\">\n";
  833. $feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
  834. $feed.= " <description>".htmlspecialchars($this->description)."</description>\n";
  835. $feed.= " <link>".$this->link."</link>\n";
  836. if ($this->image!=null) {
  837. $feed.= " <image rdf:resource=\"".$this->image->url."\" />\n";
  838. }
  839. $now = new FeedDate();
  840. $feed.= " <dc:date>".htmlspecialchars($now->iso8601())."</dc:date>\n";
  841. $feed.= " <items>\n";
  842. $feed.= " <rdf:Seq>\n";
  843. for ($i=0;$i<count($this->items);$i++) {
  844. $feed.= " <rdf:li rdf:resource=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
  845. }
  846. $feed.= " </rdf:Seq>\n";
  847. $feed.= " </items>\n";
  848. $feed.= " </channel>\n";
  849. if ($this->image!=null) {
  850. $feed.= " <image rdf:about=\"".$this->image->url."\">\n";
  851. $feed.= " <title>".$this->image->title."</title>\n";
  852. $feed.= " <link>".$this->image->link."</link>\n";
  853. $feed.= " <url>".$this->image->url."</url>\n";
  854. $feed.= " </image>\n";
  855. }
  856. $feed.= $this->_createAdditionalElements($this->additionalElements, " ");
  857.  
  858. for ($i=0;$i<count($this->items);$i++) {
  859. $feed.= " <item rdf:about=\"".htmlspecialchars($this->items[$i]->link)."\">\n";
  860. //$feed.= " <dc:type>Posting</dc:type>\n";
  861. $feed.= " <dc:format>text/html</dc:format>\n";
  862. if ($this->items[$i]->date!=null) {
  863. $itemDate = new FeedDate($this->items[$i]->date);
  864. $feed.= " <dc:date>".htmlspecialchars($itemDate->iso8601())."</dc:date>\n";
  865. }
  866. if ($this->items[$i]->source!="") {
  867. $feed.= " <dc:source>".htmlspecialchars($this->items[$i]->source)."</dc:source>\n";
  868. }
  869. if ($this->items[$i]->author!="") {
  870. $feed.= " <dc:creator>".htmlspecialchars($this->items[$i]->author)."</dc:creator>\n";
  871. }
  872. $feed.= " <title>".htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")))."</title>\n";
  873. $feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
  874. $feed.= " <description>".htmlspecialchars($this->items[$i]->description)."</description>\n";
  875. $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
  876. $feed.= " </item>\n";
  877. }
  878. $feed.= "</rdf:RDF>\n";
  879. return $feed;
  880. }
  881. }
  882.  
  883.  
  884.  
  885. /**
  886. * RSSCreator091 is a FeedCreator that implements RSS 0.91 Spec, revision 3.
  887. *
  888. * @see http://my.netscape.com/publish/formats/rss-spec-0.91.html
  889. * @since 1.3
  890. * @author Kai Blankenhorn <[email protected]>
  891. */
  892. class RSSCreator091 extends FeedCreator {
  893.  
  894. /**
  895. * Stores this RSS feed's version number.
  896. * @access private
  897. */
  898. var $RSSVersion;
  899.  
  900. function RSSCreator091() {
  901. $this->_setRSSVersion("0.91");
  902. $this->contentType = "application/rss+xml";
  903. }
  904.  
  905. /**
  906. * Sets this RSS feed's version number.
  907. * @access private
  908. */
  909. function _setRSSVersion($version) {
  910. $this->RSSVersion = $version;
  911. }
  912.  
  913. /**
  914. * Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
  915. * The feed will contain all items previously added in the same order.
  916. * @return string the feed's complete text
  917. */
  918. function createFeed() {
  919. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  920. $feed.= $this->_createGeneratorComment();
  921. $feed.= $this->_createStylesheetReferences();
  922. $feed.= "<rss version=\"".$this->RSSVersion."\">\n";
  923. $feed.= " <channel>\n";
  924. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";
  925. $this->descriptionTruncSize = 500;
  926. $feed.= " <description>".$this->getDescription()."</description>\n";
  927. $feed.= " <link>".$this->link."</link>\n";
  928. $now = new FeedDate();
  929. $feed.= " <lastBuildDate>".htmlspecialchars($now->rfc822())."</lastBuildDate>\n";
  930. $feed.= " <generator>".FEEDCREATOR_VERSION."</generator>\n";
  931.  
  932. if ($this->image!=null) {
  933. $feed.= " <image>\n";
  934. $feed.= " <url>".$this->image->url."</url>\n";
  935. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title),100)."</title>\n";
  936. $feed.= " <link>".$this->image->link."</link>\n";
  937. if ($this->image->width!="") {
  938. $feed.= " <width>".$this->image->width."</width>\n";
  939. }
  940. if ($this->image->height!="") {
  941. $feed.= " <height>".$this->image->height."</height>\n";
  942. }
  943. if ($this->image->description!="") {
  944. $feed.= " <description>".$this->image->getDescription()."</description>\n";
  945. }
  946. $feed.= " </image>\n";
  947. }
  948. if ($this->language!="") {
  949. $feed.= " <language>".$this->language."</language>\n";
  950. }
  951. if ($this->copyright!="") {
  952. $feed.= " <copyright>".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."</copyright>\n";
  953. }
  954. if ($this->editor!="") {
  955. $feed.= " <managingEditor>".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."</managingEditor>\n";
  956. }
  957. if ($this->webmaster!="") {
  958. $feed.= " <webMaster>".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."</webMaster>\n";
  959. }
  960. if ($this->pubDate!="") {
  961. $pubDate = new FeedDate($this->pubDate);
  962. $feed.= " <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n";
  963. }
  964. if ($this->category!="") {
  965. $feed.= " <category>".htmlspecialchars($this->category)."</category>\n";
  966. }
  967. if ($this->docs!="") {
  968. $feed.= " <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."</docs>\n";
  969. }
  970. if ($this->ttl!="") {
  971. $feed.= " <ttl>".htmlspecialchars($this->ttl)."</ttl>\n";
  972. }
  973. if ($this->rating!="") {
  974. $feed.= " <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating),500)."</rating>\n";
  975. }
  976. if ($this->skipHours!="") {
  977. $feed.= " <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n";
  978. }
  979. if ($this->skipDays!="") {
  980. $feed.= " <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n";
  981. }
  982. $feed.= $this->_createAdditionalElements($this->additionalElements, " ");
  983.  
  984. for ($i=0;$i<count($this->items);$i++) {
  985. $feed.= " <item>\n";
  986. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)." (".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->voted)),10) .") </title>\n";
  987. $feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
  988. $feed.= " <description>".$this->items[$i]->getDescription()."</description>\n";
  989.  
  990. if ($this->items[$i]->author!="") {
  991. $feed.= " <author>".htmlspecialchars($this->items[$i]->author)."</author>\n";
  992. }
  993. /*
  994. // on hold
  995. if ($this->items[$i]->source!="") {
  996. $feed.= " <source>".htmlspecialchars($this->items[$i]->source)."</source>\n";
  997. }
  998. */
  999. if ($this->items[$i]->category!="") {
  1000. $feed.= " <category>".htmlspecialchars($this->items[$i]->category)."</category>\n";
  1001. }
  1002. if ($this->items[$i]->comments!="") {
  1003. $feed.= " <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n";
  1004. }
  1005. if ($this->items[$i]->date!="") {
  1006. $itemDate = new FeedDate($this->items[$i]->date);
  1007. $feed.= " <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n";
  1008. }
  1009. if ($this->items[$i]->guid!="") {
  1010. $feed.= " <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n";
  1011. }
  1012. $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
  1013. $feed.= " </item>\n";
  1014. }
  1015. $feed.= " </channel>\n";
  1016. $feed.= "</rss>\n";
  1017. //die($feed."sadsadasa");
  1018. return $feed;
  1019. }
  1020. }
  1021.  
  1022.  
  1023.  
  1024. /**
  1025. * RSSCreator20 is a FeedCreator that implements RDF Site Summary (RSS) 2.0.
  1026. *
  1027. * @see http://backend.userland.com/rss
  1028. * @since 1.3
  1029. * @author Kai Blankenhorn <[email protected]>
  1030. */
  1031. class RSSCreator20 extends RSSCreator091 {
  1032.  
  1033. function RSSCreator20() {
  1034. parent::_setRSSVersion("2.0");
  1035. $this->contentType = "application/rss+xml";
  1036. }
  1037.  
  1038. }
  1039.  
  1040.  
  1041. /**
  1042. * PIECreator01 is a FeedCreator that implements the emerging PIE specification,
  1043. * as in http://intertwingly.net/wiki/pie/Syntax.
  1044. *
  1045. * @deprecated
  1046. * @since 1.3
  1047. * @author Scott Reynen <[email protected]> and Kai Blankenhorn <[email protected]>
  1048. */
  1049. class PIECreator01 extends FeedCreator {
  1050.  
  1051. function PIECreator01() {
  1052. $this->encoding = "utf-8";
  1053. }
  1054.  
  1055. function createFeed() {
  1056. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  1057. $feed.= $this->_createStylesheetReferences();
  1058. $feed.= "<feed version=\"0.1\" xmlns=\"http://example.com/newformat#\">\n";
  1059. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";
  1060. $this->truncSize = 500;
  1061. $feed.= " <subtitle>".$this->getDescription()."</subtitle>\n";
  1062. $feed.= " <link>".$this->link."</link>\n";
  1063. for ($i=0;$i<count($this->items);$i++) {
  1064. $feed.= " <entry>\n";
  1065. $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";
  1066. $feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
  1067. $itemDate = new FeedDate($this->items[$i]->date);
  1068. $feed.= " <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";
  1069. $feed.= " <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";
  1070. $feed.= " <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";
  1071. $feed.= " <id>".htmlspecialchars($this->items[$i]->guid)."</id>\n";
  1072. if ($this->items[$i]->author!="") {
  1073. $feed.= " <author>\n";
  1074. $feed.= " <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
  1075. if ($this->items[$i]->authorEmail!="") {
  1076. $feed.= " <email>".$this->items[$i]->authorEmail."</email>\n";
  1077. }
  1078. $feed.=" </author>\n";
  1079. }
  1080. $feed.= " <content type=\"text/html\" xml:lang=\"en-us\">\n";
  1081. $feed.= " <div xmlns=\"http://www.w3.org/1999/xhtml\">".$this->items[$i]->getDescription()."</div>\n";
  1082. $feed.= " </content>\n";
  1083. $feed.= " </entry>\n";
  1084. }
  1085. $feed.= "</feed>\n";
  1086. return $feed;
  1087. }
  1088. }
  1089.  
  1090.  
  1091. /**
  1092. * AtomCreator03 is a FeedCreator that implements the atom specification,
  1093. * as in http://www.intertwingly.net/wiki/pie/FrontPage.
  1094. * Please note that just by using AtomCreator03 you won't automatically
  1095. * produce valid atom files. For example, you have to specify either an editor
  1096. * for the feed or an author for every single feed item.
  1097. *
  1098. * Some elements have not been implemented yet. These are (incomplete list):
  1099. * author URL, item author's email and URL, item contents, alternate links,
  1100. * other link content types than text/html. Some of them may be created with
  1101. * AtomCreator03::additionalElements.
  1102. *
  1103. * @see FeedCreator#additionalElements
  1104. * @since 1.6
  1105. * @author Kai Blankenhorn <[email protected]>, Scott Reynen <[email protected]>
  1106. */
  1107. class AtomCreator03 extends FeedCreator {
  1108.  
  1109. function AtomCreator03() {
  1110. $this->contentType = "application/atom+xml";
  1111. $this->encoding = "utf-8";
  1112. }
  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 version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\"";
  1119. if ($this->language!="") {
  1120. $feed.= " xml:lang=\"".$this->language."\"";
  1121. }
  1122. $feed.= ">\n";
  1123. $feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
  1124. $feed.= " <tagline>".htmlspecialchars($this->description)."</tagline>\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.= " <modified>".htmlspecialchars($now->iso8601())."</modified>\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.= $this->_createAdditionalElements($this->additionalElements, " ");
  1139. for ($i=0;$i<count($this->items);$i++) {
  1140. $feed.= " <entry>\n";
  1141. $feed.= " <title>".htmlspecialchars(strip_tags($this->items[$i]->title))."</title>\n";
  1142. $feed.= " <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
  1143. if ($this->items[$i]->date=="") {
  1144. $this->items[$i]->date = time();
  1145. }
  1146. $itemDate = new FeedDate($this->items[$i]->date);
  1147. $feed.= " <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";
  1148. $feed.= " <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";
  1149. $feed.= " <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\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. $feed.= " </entry>\n";
  1161. }
  1162. $feed.= "</feed>\n";
  1163. return $feed;
  1164. }
  1165. }
  1166.  
  1167.  
  1168. /**
  1169. * MBOXCreator is a FeedCreator that implements the mbox format
  1170. * as described in http://www.qmail.org/man/man5/mbox.html
  1171. *
  1172. * @since 1.3
  1173. * @author Kai Blankenhorn <[email protected]>
  1174. */
  1175. class MBOXCreator extends FeedCreator {
  1176.  
  1177. function MBOXCreator() {
  1178. $this->contentType = "text/plain";
  1179. $this->encoding = "ISO-8859-15";
  1180. }
  1181.  
  1182. function qp_enc($input = "", $line_max = 76) {
  1183. $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  1184. $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
  1185. $eol = "\r\n";
  1186. $escape = "=";
  1187. $output = "";
  1188. while( list(, $line) = each($lines) ) {
  1189. //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
  1190. $linlen = strlen($line);
  1191. $newline = "";
  1192. for($i = 0; $i < $linlen; $i++) {
  1193. $c = substr($line, $i, 1);
  1194. $dec = ord($c);
  1195. if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
  1196. $c = "=20";
  1197. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  1198. $h2 = floor($dec/16); $h1 = floor($dec%16);
  1199. $c = $escape.$hex["$h2"].$hex["$h1"];
  1200. }
  1201. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  1202. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  1203. $newline = "";
  1204. }
  1205. $newline .= $c;
  1206. } // end of for
  1207. $output .= $newline.$eol;
  1208. }
  1209. return trim($output);
  1210. }
  1211.  
  1212.  
  1213. /**
  1214. * Builds the MBOX contents.
  1215. * @return string the feed's complete text
  1216. */
  1217. function createFeed() {
  1218. for ($i=0;$i<count($this->items);$i++) {
  1219. if ($this->items[$i]->author!="") {
  1220. $from = $this->items[$i]->author;
  1221. } else {
  1222. $from = $this->title;
  1223. }
  1224. $itemDate = new FeedDate($this->items[$i]->date);
  1225. $feed.= "From ".strtr(MBOXCreator::qp_enc($from)," ","_")." ".date("D M d H:i:s Y",$itemDate->unix())."\n";
  1226. $feed.= "Content-Type: text/plain;\n";
  1227. $feed.= " charset=\"".$this->encoding."\"\n";
  1228. $feed.= "Content-Transfer-Encoding: quoted-printable\n";
  1229. $feed.= "Content-Type: text/plain\n";
  1230. $feed.= "From: \"".MBOXCreator::qp_enc($from)."\"\n";
  1231. $feed.= "Date: ".$itemDate->rfc822()."\n";
  1232. $feed.= "Subject: ".MBOXCreator::qp_enc(FeedCreator::iTrunc($this->items[$i]->title,100))."\n";
  1233. $feed.= "\n";
  1234. $body = chunk_split(MBOXCreator::qp_enc($this->items[$i]->description));
  1235. $feed.= preg_replace("~\nFrom ([^\n]*)(\n?)~","\n>From $1$2\n",$body);
  1236. $feed.= "\n";
  1237. $feed.= "\n";
  1238. }
  1239. return $feed;
  1240. }
  1241.  
  1242. /**
  1243. * Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types.
  1244. * @return string the feed cache filename
  1245. * @since 1.4
  1246. * @access private
  1247. */
  1248. function _generateFilename() {
  1249. $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  1250. return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".mbox";
  1251. }
  1252. }
  1253.  
  1254.  
  1255. /**
  1256. * OPMLCreator is a FeedCreator that implements OPML 1.0.
  1257. *
  1258. * @see http://opml.scripting.com/spec
  1259. * @author Dirk Clemens, Kai Blankenhorn
  1260. * @since 1.5
  1261. */
  1262. class OPMLCreator extends FeedCreator {
  1263.  
  1264. function OPMLCreator() {
  1265. $this->encoding = "utf-8";
  1266. }
  1267.  
  1268. function createFeed() {
  1269. $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
  1270. $feed.= $this->_createGeneratorComment();
  1271. $feed.= $this->_createStylesheetReferences();
  1272. $feed.= "<opml xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
  1273. $feed.= " <head>\n";
  1274. $feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
  1275. if ($this->pubDate!="") {
  1276. $date = new FeedDate($this->pubDate);
  1277. $feed.= " <dateCreated>".$date->rfc822()."</dateCreated>\n";
  1278. }
  1279. if ($this->lastBuildDate!="") {
  1280. $date = new FeedDate($this->lastBuildDate);
  1281. $feed.= " <dateModified>".$date->rfc822()."</dateModified>\n";
  1282. }
  1283. if ($this->editor!="") {
  1284. $feed.= " <ownerName>".$this->editor."</ownerName>\n";
  1285. }
  1286. if ($this->editorEmail!="") {
  1287. $feed.= " <ownerEmail>".$this->editorEmail."</ownerEmail>\n";
  1288. }
  1289. $feed.= " </head>\n";
  1290. $feed.= " <body>\n";
  1291. for ($i=0;$i<count($this->items);$i++) {
  1292. $feed.= " <outline type=\"rss\" ";
  1293. $title = htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")));
  1294. $feed.= " title=\"".$title."\"";
  1295. $feed.= " text=\"".$title."\"";
  1296. //$feed.= " description=\"".htmlspecialchars($this->items[$i]->description)."\"";
  1297. $feed.= " url=\"".htmlspecialchars($this->items[$i]->link)."\"";
  1298. $feed.= "/>\n";
  1299. }
  1300. $feed.= " </body>\n";
  1301. $feed.= "</opml>\n";
  1302. return $feed;
  1303. }
  1304. }
  1305.  
  1306.  
  1307.  
  1308. /**
  1309. * HTMLCreator is a FeedCreator that writes an HTML feed file to a specific
  1310. * location, overriding the createFeed method of the parent FeedCreator.
  1311. * The HTML produced can be included over http by scripting languages, or serve
  1312. * as the source for an IFrame.
  1313. * All output by this class is embedded in <div></div> tags to enable formatting
  1314. * using CSS.
  1315. *
  1316. * @author Pascal Van Hecke
  1317. * @since 1.7
  1318. */
  1319. class HTMLCreator extends FeedCreator {
  1320.  
  1321. var $contentType = "text/html";
  1322.  
  1323. /**
  1324. * Contains HTML to be output at the start of the feed's html representation.
  1325. */
  1326. var $header;
  1327.  
  1328. /**
  1329. * Contains HTML to be output at the end of the feed's html representation.
  1330. */
  1331. var $footer ;
  1332.  
  1333. /**
  1334. * Contains HTML to be output between entries. A separator is only used in
  1335. * case of multiple entries.
  1336. */
  1337. var $separator;
  1338.  
  1339. /**
  1340. * Used to prefix the stylenames to make sure they are unique
  1341. * and do not clash with stylenames on the users' page.
  1342. */
  1343. var $stylePrefix;
  1344.  
  1345. /**
  1346. * Determines whether the links open in a new window or not.
  1347. */
  1348. var $openInNewWindow = true;
  1349.  
  1350. var $imageAlign ="right";
  1351.  
  1352. /**
  1353. * In case of very simple output you may want to get rid of the style tags,
  1354. * hence this variable. There's no equivalent on item level, but of course you can
  1355. * add strings to it while iterating over the items ($this->stylelessOutput .= ...)
  1356. * and when it is non-empty, ONLY the styleless output is printed, the rest is ignored
  1357. * in the function createFeed().
  1358. */
  1359. var $stylelessOutput ="";
  1360.  
  1361. /**
  1362. * Writes the HTML.
  1363. * @return string the scripts's complete text
  1364. */
  1365. function createFeed() {
  1366. // if there is styleless output, use the content of this variable and ignore the rest
  1367. if ($this->stylelessOutput!="") {
  1368. return $this->stylelessOutput;
  1369. }
  1370.  
  1371. //if no stylePrefix is set, generate it yourself depending on the script name
  1372. if ($this->stylePrefix=="") {
  1373. $this->stylePrefix = str_replace(".", "_", $this->_generateFilename())."_";
  1374. }
  1375.  
  1376. //set an openInNewWindow_token_to be inserted or not
  1377. if ($this->openInNewWindow) {
  1378. $targetInsert = " target='_blank'";
  1379. }
  1380.  
  1381. // use this array to put the lines in and implode later with "document.write" javascript
  1382. $feedArray = array();
  1383. if ($this->image!=null) {
  1384. $imageStr = "<a href='".$this->image->link."'".$targetInsert.">".
  1385. "<img src='".$this->image->url."' border='0' alt='".
  1386. FeedCreator::iTrunc(htmlspecialchars($this->image->title),100).
  1387. "' align='".$this->imageAlign."' ";
  1388. if ($this->image->width) {
  1389. $imageStr .=" width='".$this->image->width. "' ";
  1390. }
  1391. if ($this->image->height) {
  1392. $imageStr .=" height='".$this->image->height."' ";
  1393. }
  1394. $imageStr .="/></a>";
  1395. $feedArray[] = $imageStr;
  1396. }
  1397.  
  1398. if ($this->title) {
  1399. $feedArray[] = "<div class='".$this->stylePrefix."title'><a href='".$this->link."' ".$targetInsert." class='".$this->stylePrefix."title'>".
  1400. FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</a></div>";
  1401. }
  1402. if ($this->getDescription()) {
  1403. $feedArray[] = "<div class='".$this->stylePrefix."description'>".
  1404. str_replace("]]>", "", str_replace("<![CDATA[", "", $this->getDescription())).
  1405. "</div>";
  1406. }
  1407.  
  1408. if ($this->header) {
  1409. $feedArray[] = "<div class='".$this->stylePrefix."header'>".$this->header."</div>";
  1410. }
  1411.  
  1412. for ($i=0;$i<count($this->items);$i++) {
  1413. if ($this->separator and $i > 0) {
  1414. $feedArray[] = "<div class='".$this->stylePrefix."separator'>".$this->separator."</div>";
  1415. }
  1416.  
  1417. if ($this->items[$i]->title) {
  1418. if ($this->items[$i]->link) {
  1419. $feedArray[] =
  1420. "<div class='".$this->stylePrefix."item_title'><a href='".$this->items[$i]->link."' class='".$this->stylePrefix.
  1421. "item_title'".$targetInsert.">".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).
  1422. "</a></div>";
  1423. } else {
  1424. $feedArray[] =
  1425. "<div class='".$this->stylePrefix."item_title'>".
  1426. FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).
  1427. "</div>";
  1428. }
  1429. }
  1430. if ($this->items[$i]->getDescription()) {
  1431. $feedArray[] =
  1432. "<div class='".$this->stylePrefix."item_description'>".
  1433. str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).
  1434. "</div>";
  1435. }
  1436. }
  1437. if ($this->footer) {
  1438. $feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";
  1439. }
  1440.  
  1441. $feed= "".join($feedArray, "\r\n");
  1442. return $feed;
  1443. }
  1444.  
  1445. /**
  1446. * Overrrides parent to produce .html extensions
  1447. *
  1448. * @return string the feed cache filename
  1449. * @since 1.4
  1450. * @access private
  1451. */
  1452. function _generateFilename() {
  1453. $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  1454. return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".html";
  1455. }
  1456. }
  1457.  
  1458.  
  1459. /**
  1460. * JSCreator is a class that writes a js file to a specific
  1461. * location, overriding the createFeed method of the parent HTMLCreator.
  1462. *
  1463. * @author Pascal Van Hecke
  1464. */
  1465. class JSCreator extends HTMLCreator {
  1466. var $contentType = "text/javascript";
  1467.  
  1468. /**
  1469. * writes the javascript
  1470. * @return string the scripts's complete text
  1471. */
  1472. function createFeed()
  1473. {
  1474. $feed = parent::createFeed();
  1475. $feedArray = explode("\n",$feed);
  1476.  
  1477. $jsFeed = "";
  1478. foreach ($feedArray as $value) {
  1479. $jsFeed .= "document.write('".trim(addslashes($value))."');\n";
  1480. }
  1481. return $jsFeed;
  1482. }
  1483.  
  1484. /**
  1485. * Overrrides parent to produce .js extensions
  1486. *
  1487. * @return string the feed cache filename
  1488. * @since 1.4
  1489. * @access private
  1490. */
  1491. function _generateFilename() {
  1492. $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
  1493. return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".js";
  1494. }
  1495.  
  1496. }
  1497.  
  1498. // Given $timestamp is modify date of the requested xml file, if client already has it, return HTTP 304 and quit
  1499. function doConditionalGet($timestamp) {
  1500. // A PHP implementation of conditional get, see
  1501. // http://simon.incutio.com/archive/2003/04/23/conditionalGet
  1502. // http://fishbowl.pastiche.org/archives/001132.html
  1503. $last_modified = gmdate("D, d M Y H:i:s", $timestamp).' GMT';
  1504. $etag = '"'.md5($last_modified).'"';
  1505. // Send the headers
  1506. header("Last-Modified: $last_modified");
  1507. header("ETag: $etag");
  1508. // See if the client has provided the required headers
  1509. $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
  1510. stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
  1511. false;
  1512. $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
  1513. stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) :
  1514. false;
  1515. if (!$if_modified_since && !$if_none_match) {
  1516. return;
  1517. }
  1518. // At least one of the headers is there - check them
  1519. if ($if_none_match && $if_none_match != $etag) {
  1520. return; // etag is there but doesn't match
  1521. }
  1522. if ($if_modified_since && $if_modified_since != $last_modified) {
  1523. return; // if-modified-since is there but doesn't match
  1524. }
  1525. // Nothing has changed since their last request - serve a 304 and exit
  1526. header('HTTP/1.0 304 Not Modified');
  1527. exit;
  1528. }
  1529.  
  1530.  
  1531.  
  1532. /*** TEST SCRIPT *********************************************************
  1533.  
  1534. //include("feedcreator.class.php");
  1535.  
  1536. $rss = new UniversalFeedCreator();
  1537. $rss->useCached();
  1538. $rss->title = "PHP news";
  1539. $rss->description = "daily news from the PHP scripting world";
  1540.  
  1541. //optional
  1542. //$rss->descriptionTruncSize = 500;
  1543. //$rss->descriptionHtmlSyndicated = true;
  1544. //$rss->xslStyleSheet = "http://feedster.com/rss20.xsl";
  1545.  
  1546. $rss->link = "http://www.dailyphp.net/news";
  1547. $rss->feedURL = "http://www.dailyphp.net/".$PHP_SELF;
  1548.  
  1549. $image = new FeedImage();
  1550. $image->title = "dailyphp.net logo";
  1551. $image->url = "http://www.dailyphp.net/images/logo.gif";
  1552. $image->link = "http://www.dailyphp.net";
  1553. $image->description = "Feed provided by dailyphp.net. Click to visit.";
  1554.  
  1555. //optional
  1556. $image->descriptionTruncSize = 500;
  1557. $image->descriptionHtmlSyndicated = true;
  1558.  
  1559. $rss->image = $image;
  1560.  
  1561. // get your news items from somewhere, e.g. your database:
  1562. //mysql_select_db($dbHost, $dbUser, $dbPass);
  1563. //$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
  1564. //while ($data = mysql_fetch_object($res)) {
  1565. $item = new FeedItem();
  1566. $item->title = "This is an the test title of an item";
  1567. $item->link = "http://localhost/item/";
  1568. $item->description = "<b>description in </b><br/>HTML";
  1569.  
  1570. //optional
  1571. //item->descriptionTruncSize = 500;
  1572. $item->descriptionHtmlSyndicated = true;
  1573.  
  1574. $item->date = time();
  1575. $item->source = "http://www.dailyphp.net";
  1576. $item->author = "John Doe";
  1577.  
  1578. $rss->addItem($item);
  1579. //}
  1580.  
  1581. // valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1, MBOX, OPML, ATOM0.3, HTML, JS
  1582. echo $rss->saveFeed("RSS0.91", "feed.xml");
  1583.  
  1584.  
  1585.  
  1586. ***************************************************************************/
  1587.  
  1588. ?>
Advertisement
Add Comment
Please, Sign In to add comment