feedcreator.class.php
来自「php 开发的内容管理系统」· PHP 代码 · 共 1,572 行 · 第 1/4 页
PHP
1,572 行
}
/**
* MBOXCreator is a FeedCreator that implements the mbox format
* as described in http://www.qmail.org/man/man5/mbox.html
*
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class MBOXCreator extends FeedCreator {
function MBOXCreator() {
$this->contentType = "text/plain";
$this->encoding = "ISO-8859-15";
}
function qp_enc($input = "", $line_max = 76) {
$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
$lines = preg_split("/(?:\r\n|\r|\n)/", $input);
$eol = "\r\n";
$escape = "=";
$output = "";
while( list(, $line) = each($lines) ) {
//$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
$linlen = strlen($line);
$newline = "";
for($i = 0; $i < $linlen; $i++) {
$c = substr($line, $i, 1);
$dec = ord($c);
if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
$c = "=20";
} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
$h2 = floor($dec/16); $h1 = floor($dec%16);
$c = $escape.$hex["$h2"].$hex["$h1"];
}
if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
$output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
$newline = "";
}
$newline .= $c;
} // end of for
$output .= $newline.$eol;
}
return trim($output);
}
/**
* Builds the MBOX contents.
* @return string the feed's complete text
*/
function createFeed() {
for ($i=0;$i<count($this->items);$i++) {
if ($this->items[$i]->author!="") {
$from = $this->items[$i]->author;
} else {
$from = $this->title;
}
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= "From ".strtr(MBOXCreator::qp_enc($from)," ","_")." ".date("D M d H:i:s Y",$itemDate->unix())."\n";
$feed.= "Content-Type: text/plain;\n";
$feed.= " charset=\"".$this->encoding."\"\n";
$feed.= "Content-Transfer-Encoding: quoted-printable\n";
$feed.= "Content-Type: text/plain\n";
$feed.= "From: \"".MBOXCreator::qp_enc($from)."\"\n";
$feed.= "Date: ".$itemDate->rfc822()."\n";
$feed.= "Subject: ".MBOXCreator::qp_enc(FeedCreator::iTrunc($this->items[$i]->title,100))."\n";
$feed.= "\n";
$body = chunk_split(MBOXCreator::qp_enc($this->items[$i]->description));
$feed.= preg_replace("~\nFrom ([^\n]*)(\n?)~","\n>From $1$2\n",$body);
$feed.= "\n";
$feed.= "\n";
}
return $feed;
}
/**
* Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types.
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".mbox";
}
}
/**
* OPMLCreator is a FeedCreator that implements OPML 1.0.
*
* @see http://opml.scripting.com/spec
* @author Dirk Clemens, Kai Blankenhorn
* @since 1.5
*/
class OPMLCreator extends FeedCreator {
function OPMLCreator() {
$this->encoding = "utf-8";
}
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
$feed.= $this->_createStylesheetReferences();
$feed.= "<opml xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
$feed.= " <head>\n";
$feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
if ($this->pubDate!="") {
$date = new FeedDate($this->pubDate);
$feed.= " <dateCreated>".$date->rfc822()."</dateCreated>\n";
}
if ($this->lastBuildDate!="") {
$date = new FeedDate($this->lastBuildDate);
$feed.= " <dateModified>".$date->rfc822()."</dateModified>\n";
}
if ($this->editor!="") {
$feed.= " <ownerName>".$this->editor."</ownerName>\n";
}
if ($this->editorEmail!="") {
$feed.= " <ownerEmail>".$this->editorEmail."</ownerEmail>\n";
}
$feed.= " </head>\n";
$feed.= " <body>\n";
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <outline type=\"rss\" ";
$title = htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")));
$feed.= " title=\"".$title."\"";
$feed.= " text=\"".$title."\"";
//$feed.= " description=\"".htmlspecialchars($this->items[$i]->description)."\"";
$feed.= " url=\"".htmlspecialchars($this->items[$i]->link)."\"";
$feed.= "/>\n";
}
$feed.= " </body>\n";
$feed.= "</opml>\n";
return $feed;
}
}
/**
* HTMLCreator is a FeedCreator that writes an HTML feed file to a specific
* location, overriding the createFeed method of the parent FeedCreator.
* The HTML produced can be included over http by scripting languages, or serve
* as the source for an IFrame.
* All output by this class is embedded in <div></div> tags to enable formatting
* using CSS.
*
* @author Pascal Van Hecke
* @since 1.7
*/
class HTMLCreator extends FeedCreator {
var $contentType = "text/html";
/**
* Contains HTML to be output at the start of the feed's html representation.
*/
var $header;
/**
* Contains HTML to be output at the end of the feed's html representation.
*/
var $footer ;
/**
* Contains HTML to be output between entries. A separator is only used in
* case of multiple entries.
*/
var $separator;
/**
* Used to prefix the stylenames to make sure they are unique
* and do not clash with stylenames on the users' page.
*/
var $stylePrefix;
/**
* Determines whether the links open in a new window or not.
*/
var $openInNewWindow = true;
var $imageAlign ="right";
/**
* In case of very simple output you may want to get rid of the style tags,
* hence this variable. There's no equivalent on item level, but of course you can
* add strings to it while iterating over the items ($this->stylelessOutput .= ...)
* and when it is non-empty, ONLY the styleless output is printed, the rest is ignored
* in the function createFeed().
*/
var $stylelessOutput ="";
/**
* Writes the HTML.
* @return string the scripts's complete text
*/
function createFeed() {
// if there is styleless output, use the content of this variable and ignore the rest
if ($this->stylelessOutput!="") {
return $this->stylelessOutput;
}
//if no stylePrefix is set, generate it yourself depending on the script name
if ($this->stylePrefix=="") {
$this->stylePrefix = str_replace(".", "_", $this->_generateFilename())."_";
}
//set an openInNewWindow_token_to be inserted or not
if ($this->openInNewWindow) {
$targetInsert = " target='_blank'";
}
// use this array to put the lines in and implode later with "document.write" javascript
$feedArray = array();
if ($this->image!=null) {
$imageStr = "<a href='".$this->image->link."'".$targetInsert.">".
"<img src='".$this->image->url."' border='0' alt='".
FeedCreator::iTrunc(htmlspecialchars($this->image->title),100).
"' align='".$this->imageAlign."' ";
if ($this->image->width) {
$imageStr .=" width='".$this->image->width. "' ";
}
if ($this->image->height) {
$imageStr .=" height='".$this->image->height."' ";
}
$imageStr .="/></a>";
$feedArray[] = $imageStr;
}
if ($this->title) {
$feedArray[] = "<div class='".$this->stylePrefix."title'><a href='".$this->link."' ".$targetInsert." class='".$this->stylePrefix."title'>".
FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</a></div>";
}
if ($this->getDescription()) {
$feedArray[] = "<div class='".$this->stylePrefix."description'>".
str_replace("]]>", "", str_replace("<![CDATA[", "", $this->getDescription())).
"</div>";
}
if ($this->header) {
$feedArray[] = "<div class='".$this->stylePrefix."header'>".$this->header."</div>";
}
for ($i=0;$i<count($this->items);$i++) {
if ($this->separator and $i > 0) {
$feedArray[] = "<div class='".$this->stylePrefix."separator'>".$this->separator."</div>";
}
if ($this->items[$i]->title) {
if ($this->items[$i]->link) {
$feedArray[] =
"<div class='".$this->stylePrefix."item_title'><a href='".$this->items[$i]->link."' class='".$this->stylePrefix.
"item_title'".$targetInsert.">".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).
"</a></div>";
} else {
$feedArray[] =
"<div class='".$this->stylePrefix."item_title'>".
FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).
"</div>";
}
}
if ($this->items[$i]->getDescription()) {
$feedArray[] =
"<div class='".$this->stylePrefix."item_description'>".
str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).
"</div>";
}
}
if ($this->footer) {
$feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";
}
$feed= "".join($feedArray, "\r\n");
return $feed;
}
/**
* Overrrides parent to produce .html extensions
*
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".html";
}
}
/**
* JSCreator is a class that writes a js file to a specific
* location, overriding the createFeed method of the parent HTMLCreator.
*
* @author Pascal Van Hecke
*/
class JSCreator extends HTMLCreator {
var $contentType = "text/javascript";
/**
* writes the javascript
* @return string the scripts's complete text
*/
function createFeed()
{
$feed = parent::createFeed();
$feedArray = explode("\n",$feed);
$jsFeed = "";
foreach ($feedArray as $value) {
$jsFeed .= "document.write('".trim(addslashes($value))."');\n";
}
return $jsFeed;
}
/**
* Overrrides parent to produce .js extensions
*
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".js";
}
}
/*** TEST SCRIPT *********************************************************
//include("feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->useCached();
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
//optional
//$rss->descriptionTruncSize = 500;
//$rss->descriptionHtmlSyndicated = true;
//$rss->xslStyleSheet = "http://feedster.com/rss20.xsl";
$rss->link = "http://www.dailyphp.net/news";
$rss->feedURL = "http://www.dailyphp.net/".$PHP_SELF;
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;
$rss->image = $image;
// get your news items from somewhere, e.g. your database:
//mysql_select_db($dbHost, $dbUser, $dbPass);
//$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
//while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = "This is an the test title of an item";
$item->link = "http://localhost/item/";
$item->description = "<b>description in </b><br/>HTML";
//optional
//item->descriptionTruncSize = 500;
$item->descriptionHtmlSyndicated = true;
$item->date = time();
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
$rss->addItem($item);
//}
// valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1, MBOX, OPML, ATOM0.3, HTML, JS
echo $rss->saveFeed("RSS0.91", "feed.xml");
***************************************************************************/
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?