Source for file feedcreator.class.php
Documentation is available at feedcreator.class.php
* @version $Header: /cvsroot/bitweaver/_bit_rss/feedcreator.class.php,v 1.5 2007/01/05 20:12:27 squareing Exp $
/*************************************************************************
originally (c) Kai Blankenhorn
v1.3 work by Scott Reynen (scott@randomchaos.com) and Kai Blankenhorn
v1.5 OPML support by Dirk Clemens
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************
fixed left over debug code
added HTML and JavaScript feeds (configurable via CSS) (thanks to Pascal Van Hecke)
added HTML descriptions for all feed formats (thanks to Pascal Van Hecke)
added a switch to select an external stylesheet (thanks to Pascal Van Hecke)
changed default content-type to application/xml
added character encoding setting
fixed numerous smaller bugs (thanks to S�en Fuhrmann of golem.de)
improved changing ATOM versions handling (thanks to August Trometer)
improved the UniversalFeedCreator's useCached method (thanks to S�en Fuhrmann of golem.de)
added charset output in HTTP headers (thanks to S�en Fuhrmann of golem.de)
added Slashdot namespace to RSS 1.0 (thanks to S�en Fuhrmann of golem.de)
added stylesheet to RSS 1.0 feeds
fixed generator comment (thanks Kevin L. Papendick and Tanguy Pruvot)
fixed RFC822 date bug (thanks Tanguy Pruvot)
added TimeZone customization for RFC8601 (thanks Tanguy Pruvot)
fixed Content-type could be empty (thanks Tanguy Pruvot)
fixed author/creator in RSS1.0 (thanks Tanguy Pruvot)
added Atom 0.3 support (not all features, though)
improved OPML 1.0 support (hopefully - added more elements)
added support for arbitrary additional elements (use with caution)
considered beta due to some internal changes
fixed some RSS 1.0 glitches (thanks to St�hane Vanpoperynghe)
fixed some inconsistencies between documentation and code (thanks to Timothy Martin)
added support for OPML 1.0
optional feed saving and caching
renamed to FeedCreator, as it not only creates RSS anymore
tentative support for echo/necho/atom/pie/???
intelligent auto-truncating of RSS 0.91 attributes
don't create some attributes when they're not set
fixed a real and a possible bug with date conversions
now includes most RSS 0.91 attributes
***************************************************************************/
/************************************************************************
**************************************************************************/
// your local timezone, set to "" to disable or for GMT
define("FEEDCREATOR_VERSION", "FeedCreator 1.7.2");
* A FeedItem is a part of a FeedCreator feed.
* @author Kai Blankenhorn <kaib@bitfolge.de>
* Mandatory attributes of an item.
* Optional attributes of an item.
* Publishing date of an item. May be in one of the following formats:
* "Mon, 20 Jan 03 18:05:41 +0400"
* "20 Jan 03 18:05:41 +0000"
* "2003-01-20T18:05:41+04:00"
* Any additional elements to include as an assiciated array. All $key => $value pairs
* will be included unencoded in the feed item in the form
* Again: No encoding will be used! This means you can invalidate or enhance the feed
* if $value contains markup. This may be abused to embed tags not implemented by
* the FeedCreator class used.
* An FeedImage may be added to a FeedCreator feed.
* @author Kai Blankenhorn <kaib@bitfolge.de>
* Mandatory attributes of an image.
* Optional attributes of an image.
* An HtmlDescribable is an item within a feed that can have a description that may
* Indicates whether the description field should be rendered in HTML.
* Indicates whether and to how many characters a description should be truncated.
* Returns a formatted description field, depending on descriptionHtmlSyndicated and
* $descriptionTruncSize properties
* @return string the formatted description
return $descriptionField->output();
* An FeedHtmlField describes and generates
* a feed, item or image html field (probably a description). Output is
* generated based on $truncSize, $syndicateHtml properties.
* @author Pascal Van Hecke <feedcreator.class.php@vanhecke.info>
* Mandatory attributes of a FeedHtmlField.
* Optional attributes of a FeedHtmlField.
* Creates a new instance of FeedHtmlField.
* @param $string: if given, sets the rawFieldContent property
* Creates the right output, depending on $truncSize, $syndicateHtml properties.
* @return string the formatted field
// when field available and syndicated in html we assume
// - valid html in $rawFieldContent and we enclose in CDATA tags
// - no truncation (truncating risks producing invalid html)
* UniversalFeedCreator lets you choose during runtime which
* For general usage of a feed class, see the FeedCreator class
* below or the example above.
* @author Kai Blankenhorn <kaib@bitfolge.de>
// fall through: always the latest ATOM version
foreach ($vars as $key => $value) {
// prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
if (!in_array($key, array("_feed", "contentType", "encoding"))) {
$this->_feed->{$key} = $this->{$key};
* Creates a syndication feed based on the items previously added.
* @see FeedCreator::addItem()
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
* @return string the contents of the feed.
* Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
* header may be sent to redirect the use to the newly created file.
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", "ATOM0.3", "HTML", "JS"
* @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()).
* @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.
function saveFeed($format= "RSS0.91", $filename= "", $displayContents= true) {
* Turns on caching and checks if there is a recent version of this feed in the cache.
* If there is, an HTTP redirect header is sent.
* To effectively use caching, you should create the FeedCreator object and call this method
* before anything else, especially before you do the time consuming task to build the feed
* (web fetching, for example).
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
* @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()).
* @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
function useCached($format= "RSS0.91", $filename= "", $timeout= 3600) {
* FeedCreator is the abstract base implementation for concrete
* implementations that implement a specific format of syndication.
* @author Kai Blankenhorn <kaib@bitfolge.de>
* Mandatory attributes of a feed.
* Optional attributes of a feed.
var $syndicationURL, $image, $language, $copyright, $pubDate, $lastBuildDate, $editor, $editorEmail, $webmaster, $category, $docs, $ttl, $rating, $skipHours, $skipDays;
* The url of the external xsl stylesheet used to format the naked rss feed.
* Ignored in the output when empty.
* This feed's MIME content type.
var $contentType = "application/xml";
* This feed's character encoding.
* Any additional elements to include as an assiciated array. All $key => $value pairs
* will be included unencoded in the feed in the form
* Again: No encoding will be used! This means you can invalidate or enhance the feed
* if $value contains markup. This may be abused to embed tags not implemented by
* the FeedCreator class used.
* Adds an FeedItem to the feed.
* @param object FeedItem $item The FeedItem to add to the feed.
* Truncates a string to a certain length at the most sensible point.
* First, if there's a '.' character near the end of the string, the string is truncated after this character.
* If there is no '.', the string is truncated after the last ' ' character.
* If the string is truncated, " ..." is appended.
* If the string is already shorter than $length, it is returned unchanged.
* @param string string A string to be truncated.
* @param int length the maximum length the string should be truncated to
* @return string the truncated string
function iTrunc($string, $length) {
if (strlen($string)<= $length) {
$string = substr($string,0,$length- 4);
return substr($string,0,$pos+ 1). " ...";
$string = substr($string,0,$length- 4);
return substr($string,0,$pos). " ...";
return substr($string,0,$length- 4). " ...";
* Creates a comment indicating the generator of this feed.
* The format of this comment seems to be recognized by
* Creates a string containing all additional elements specified in
* @param elements array an associative array containing key => value pairs
* @param indentString string a string that will be inserted before every generated line
* @return string the XML tags corresponding to $additionalElements
foreach($elements AS $key => $value) {
$ae.= $indentString. "<$key>$value</$key>\n";
if (!empty($this->cssStyleSheet)) {
$xml .= "<?xml-stylesheet href=\"". $this->cssStyleSheet. "\" type=\"text/css\"?>\n";
$xml .= "<?xml-stylesheet href=\"http://www.w3.org/2000/08/w3c-synd/style.css\" type=\"text/css\"?>\n";
* Builds the feed's text.
* @return string the feed's complete text
* Generate a filename for the feed cache file. The result will be $_SERVER["PHP_SELF"] with the extension changed to .xml.
* echo $_SERVER["PHP_SELF"]."\n";
* echo FeedCreator::_generateFilename();
* @return string the feed cache filename
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,- (strlen($fileInfo["extension"])+ 1)). ".xml";
function _redirect($filename) {
// attention, heavily-commented-out-area
// maybe use this in addition to file time checking
//Header("Expires: ".date("r",time()+$this->_timeout));
/* no caching at all, doesn't seem to work as good:
Header("Cache-Control: no-cache");
Header("Pragma: no-cache");
// HTTP redirect, some feed readers' simple HTTP implementations don't follow it
//Header("Location: ".$filename);
Header("Content-Type: ". $this->contentType. "; charset=". $this->encoding. "; filename=". basename($filename));
Header("Content-Disposition: inline; filename=". basename($filename));
* Turns on caching and checks if there is a recent version of this feed in the cache.
* If there is, an HTTP redirect header is sent.
* To effectively use caching, you should create the FeedCreator object and call this method
* before anything else, especially before you do the time consuming task to build the feed
* (web fetching, for example).
* @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()).
* @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
function useCached($filename= "", $timeout= 3600) {
$this->_timeout = $timeout;
|