* @category typecho * @package Feed * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org) * @license GNU General Public License 2.0 * @version $Id: Feed.php 219 2008-05-27 09:06:15Z magike.net $ */ /** * Typecho_Feed * * @author qining * @category typecho * @package Feed * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org) * @license GNU General Public License 2.0 */ class Typecho_Feed { /** 定义RSS 1.0类型 */ const RSS1 = 'RSS 1.0'; /** 定义RSS 2.0类型 */ const RSS2 = 'RSS 2.0'; /** 定义ATOM 1.0类型 */ const ATOM1 = 'ATOM 1.0'; /** 定义RSS时间格式 */ const DATE_RFC822 = 'r'; /** 定义ATOM时间格式 */ const DATE_W3CDTF = 'c'; /** 定义行结束符 */ const EOL = "\n"; /** * feed状态 * * @access private * @var string */ private $_type; /** * 字符集编码 * * @access private * @var string */ private $_charset; /** * 语言状态 * * @access private * @var string */ private $_lang; /** * 聚合地址 * * @access private * @var string */ private $_feedUrl; /** * 基本地址 * * @access private * @var unknown */ private $_baseUrl; /** * 聚合标题 * * @access private * @var string */ private $_title; /** * 聚合副标题 * * @access private * @var string */ private $_subTitle; /** * 版本信息 * * @access private * @var string */ private $_version; /** * 所有的items * * @access private * @var array */ private $_items = array(); /** * 创建Feed对象 * * @access public * @return void */ public function __construct($version, $type = self::RSS2, $charset = 'UTF-8', $lang = 'en') { $this->_version = $version; $this->_type = $type; $this->_charset = $charset; $this->_lang = $lang; } /** * 设置标题 * * @access public * @param string $title 标题 * @return void */ public function setTitle($title) { $this->_title = $title; } /** * 设置副标题 * * @access public * @param string $subTitle 副标题 * @return void */ public function setSubTitle($subTitle) { $this->_subTitle = $subTitle; } /** * 设置聚合地址 * * @access public * @param string $feedUrl 聚合地址 * @return void */ public function setFeedUrl($feedUrl) { $this->_feedUrl = $feedUrl; } /** * 设置主页 * * @access public * @param string $baseUrl 主页地址 * @return void */ public function setBaseUrl($baseUrl) { $this->_baseUrl = $baseUrl; } /** * 获取Feed时间格式 * * @access public * @param integer $stamp 时间戳 * @return string */ public function dateFormat($stamp) { if (self::RSS2 == $this->_type) { return date(self::DATE_RFC822, $stamp); } else if (self::RSS1 == $this->_type || self::ATOM1 == $this->_type) { return date(self::DATE_W3CDTF, $stamp); } } /** * $item的格式为 * * array ( * 'title' => 'xxx', * 'content' => 'xxx', * 'excerpt' => 'xxx', * 'date' => 'xxx', * 'link' => 'xxx', * 'author' => 'xxx', * 'comments' => 'xxx', * 'commentsUrl'=> 'xxx', * 'commentsFeedUrl' => 'xxx', * ) * * * @access public * @param array $item * @return unknown */ public function addItem(array $item) { $this->_items[] = $item; } /** * 输出字符串 * * @access public * @return string */ public function __toString() { $result = '_charset . '"?>' . self::EOL; if (self::RSS1 == $this->_type) { $result .= '' . self::EOL; $content = ''; $links = array(); $lastUpdate = 0; foreach ($this->_items as $item) { $content .= '' . self::EOL; $content .= '' . htmlspecialchars($item['title']) . '' . self::EOL; $content .= '' . $item['link'] . '' . self::EOL; $content .= '' . $this->dateFormat($item['date']) . '' . self::EOL; $content .= '' . strip_tags($item['content']) . '' . self::EOL; if (!empty($item['suffix'])) { $content .= $item['suffix']; } $content .= '' . self::EOL; $links[] = $item['link']; if ($item['date'] > $lastUpdate) { $lastUpdate = $item['date']; } } $result .= ' ' . htmlspecialchars($this->_title) . ' ' . $this->_baseUrl . ' ' . htmlspecialchars($this->_subTitle) . ' ' . self::EOL; foreach ($links as $link) { $result .= '' . self::EOL; } $result .= ' ' . self::EOL; $result .= $content . ''; } else if (self::RSS2 == $this->_type) { $result .= ' ' . self::EOL; $content = ''; $lastUpdate = 0; foreach ($this->_items as $item) { $content .= '' . self::EOL; $content .= '' . htmlspecialchars($item['title']) . '' . self::EOL; $content .= '' . $item['link'] . '' . self::EOL; $content .= '' . $item['link'] . '' . self::EOL; $content .= '' . $this->dateFormat($item['date']) . '' . self::EOL; $content .= '' . htmlspecialchars($item['author']->screenName) . '' . self::EOL; if (!empty($item['category']) && is_array($item['category'])) { foreach ($item['category'] as $category) { $content .= '' . self::EOL; } } if (!empty($item['excerpt'])) { $content .= '' . self::EOL; } if (!empty($item['content'])) { $content .= '' . self::EOL; } if (isset($item['comments']) && strlen($item['comments']) > 0) { $content .= '' . $item['comments'] . '' . self::EOL; } $content .= '' . $item['link'] . '#comments' . self::EOL; if (!empty($item['commentsFeedUrl'])) { $content .= '' . $item['commentsFeedUrl'] . '' . self::EOL; } if (!empty($item['suffix'])) { $content .= $item['suffix']; } $content .= '' . self::EOL; if ($item['date'] > $lastUpdate) { $lastUpdate = $item['date']; } } $result .= '' . htmlspecialchars($this->_title) . ' ' . $this->_baseUrl . ' ' . $this->_lang . ' ' . htmlspecialchars($this->_subTitle) . ' ' . $this->dateFormat($lastUpdate) . ' ' . $this->dateFormat($lastUpdate) . '' . self::EOL; $result .= $content . ' '; } else if (self::ATOM1 == $this->_type) { $result .= '' . self::EOL; $content = ''; $lastUpdate = 0; foreach ($this->_items as $item) { $content .= '' . self::EOL; $content .= '<![CDATA[' . $item['title'] . ']]>' . self::EOL; $content .= '' . self::EOL; $content .= '' . $item['link'] . '' . self::EOL; $content .= '' . $this->dateFormat($item['date']) . '' . self::EOL; $content .= '' . $this->dateFormat($item['date']) . '' . self::EOL; $content .= ' ' . $item['author']->screenName . ' ' . $item['author']->url . ' ' . self::EOL; if (!empty($item['category']) && is_array($item['category'])) { foreach ($item['category'] as $category) { $content .= '' . self::EOL; } } if (!empty($item['excerpt'])) { $content .= '' . self::EOL; } if (!empty($item['content'])) { $content .= '' . self::EOL; } if (isset($item['comments']) && strlen($item['comments']) > 0) { $content .= '' . self::EOL; if (!empty($item['commentsFeedUrl'])) { $content .= '' . self::EOL; } } if (!empty($item['suffix'])) { $content .= $item['suffix']; } $content .= '' . self::EOL; if ($item['date'] > $lastUpdate) { $lastUpdate = $item['date']; } } $result .= '' . htmlspecialchars($this->_title) . ' ' . htmlspecialchars($this->_subTitle) . ' ' . $this->dateFormat($lastUpdate) . ' Typecho ' . $this->_feedUrl . ' '; $result .= $content . ''; } return $result; } }