Source for file feedcreator.class.php

Documentation is available at feedcreator.class.php

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

Documentation generated on Mon, 05 May 2008 16:19:39 +0400 by phpDocumentor 1.4.0