铃木风暴太子150复古:PHP生成RSS类
来源:百度文库 编辑:中财网 时间:2024/05/04 14:39:09
/**
* rss操作类
*
* FeedCreator class v1.7.2
* originally (c) Kai Blankenhorn
* www.bitfolge.de
* kaib@bitfolge.de
*
*/
// your local timezone, set to "" to disable or for GMT
define("TIME_ZONE","");
/**
* Version string.
**/
define("FEEDCREATOR_VERSION", "www.273.cn");
/**
* A FeedItem is a part of a FeedCreator feed.
*
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.3
*/
class FeedItem extends HtmlDescribable {
/**
* Mandatory attributes of an item.
*/
var $title, $description, $link;
/**
* Optional attributes of an item.
*/
var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator;
/**
* Publishing date of an item. May be in one of the following formats:
*
* RFC 822:
* "Mon, 20 Jan 03 18:05:41 +0400"
* "20 Jan 03 18:05:41 +0000"
*
* ISO 8601:
* "2003-01-20T18:05:41+04:00"
*
* Unix:
* 1043082341
*/
var $date;
/**
* Any additional elements to include as an assiciated array. All $key => $value pairs
* will be included unencoded in the feed item in the form
* <$key>$value$key>
* 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.
*/
var $additionalElements = Array();
// on hold
// var $source;
}
/**
* An FeedImage may be added to a FeedCreator feed.
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.3
*/
class FeedImage extends HtmlDescribable {
/**
* Mandatory attributes of an image.
*/
var $title, $url, $link;
/**
* Optional attributes of an image.
*/
var $width, $height, $description;
}
/**
* An HtmlDescribable is an item within a feed that can have a description that may
* include HTML markup.
*/
class HtmlDescribable {
/**
* Indicates whether the description field should be rendered in HTML.
*/
var $descriptionHtmlSyndicated;
/**
* Indicates whether and to how many characters a description should be truncated.
*/
var $descriptionTruncSize;
/**
* Returns a formatted description field, depending on descriptionHtmlSyndicated and
* $descriptionTruncSize properties
* @return string the formatted description
*/
function getDescription() {
$descriptionField = new FeedHtmlField($this->description);
$descriptionField->syndicateHtml = $this->descriptionHtmlSyndicated;
$descriptionField->truncSize = $this->descriptionTruncSize;
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>
* @version 1.6
*/
class FeedHtmlField {
/**
* Mandatory attributes of a FeedHtmlField.
*/
var $rawFieldContent;
/**
* Optional attributes of a FeedHtmlField.
*
*/
var $truncSize, $syndicateHtml;
/**
* Creates a new instance of FeedHtmlField.
* @param $string: if given, sets the rawFieldContent property
*/
function FeedHtmlField($parFieldContent) {
if ($parFieldContent) {
$this->rawFieldContent = $parFieldContent;
}
}
/**
* Creates the right output, depending on $truncSize, $syndicateHtml properties.
* @return string the formatted field
*/
function output() {
// 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)
if (!$this->rawFieldContent) {
$result = "";
} elseif ($this->syndicateHtml) {
$result = "rawFieldContent."]]>";
} else {
if ($this->truncSize and is_int($this->truncSize)) {
$result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
} else {
$result = htmlspecialchars($this->rawFieldContent);
}
}
return $result;
}
}
/**
* UniversalFeedCreator lets you choose during runtime which
* format to build.
* For general usage of a feed class, see the FeedCreator class
* below or the example above.
*
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class UniversalFeedCreator extends FeedCreator {
var $_feed;
function _setFormat($format) {
switch (strtoupper($format)) {
case "2.0":
// fall through
case "RSS2.0":
$this->_feed = new RSSCreator20();
break;
case "0.91":
// fall through
case "RSS0.91":
$this->_feed = new RSSCreator091();
break;
default:
$this->_feed = new RSSCreator091();
break;
}
$vars = get_object_vars($this);
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.
*/
function createFeed($format = "RSS0.91") {
$this->_setFormat($format);
return $this->_feed->createFeed();
}
/**
* 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.
* @since 1.4
*
* @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) {
$this->_setFormat($format);
$this->_feed->saveFeed($filename, $displayContents);
}
/**
* 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) {
$this->_setFormat($format);
$this->_feed->useCached($filename, $timeout);
}
}
/**
* FeedCreator is the abstract base implementation for concrete
* implementations that implement a specific format of syndication.
*
* @abstract
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.4
*/
class FeedCreator extends HtmlDescribable {
/**
* Mandatory attributes of a feed.
*/
var $title, $description, $link;
/**
* 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.
*/
var $xslStyleSheet = "";
/**
* @access private
*/
var $items = Array();
/**
* This feed's MIME content type.
* @since 1.4
* @access private
*/
var $contentType = "application/xml";
/**
* This feed's character encoding.
* @since 1.6.1
**/
var $encoding = "utf-8";
/**
* Any additional elements to include as an assiciated array. All $key => $value pairs
* will be included unencoded in the feed in the form
* <$key>$value$key>
* 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.
*/
var $additionalElements = Array();
/**
* Adds an FeedItem to the feed.
*
* @param object FeedItem $item The FeedItem to add to the feed.
====================================================================================
使用:
include_once('Rss.class.php');
$rss = new UniversalFeedCreator();
$rss->title = "PHP开源项目";
$rss->link = "http://www.coderhome.net";
$rss->description = "最全最新最丰富的PHP开源项目";
$softList = $query->select('select * from soft_list where status=1 order by id desc','array',1,100);
foreach ($softList as $rs) {
$item = new FeedItem();
$item->title = $rs['title'];
$item->link = 'http://www.coderhome.net/?id='.$rs['id'];
$item->description = $rs['content'];
$rss->addItem($item);
}
$rss->saveFeed("RSS2.0", "rss.xml");