123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- <?php
- namespace Typecho;
- /**
- * Feed
- *
- * @package Feed
- */
- class Feed
- {
- /** 定义RSS 1.0类型 */
- public const RSS1 = 'RSS 1.0';
- /** 定义RSS 2.0类型 */
- public const RSS2 = 'RSS 2.0';
- /** 定义ATOM 1.0类型 */
- public const ATOM1 = 'ATOM 1.0';
- /** 定义RSS时间格式 */
- public const DATE_RFC822 = 'r';
- /** 定义ATOM时间格式 */
- public const DATE_W3CDTF = 'c';
- /** 定义行结束符 */
- public 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 string
- */
- 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 = [];
- /**
- * 创建Feed对象
- *
- * @param $version
- * @param string $type
- * @param string $charset
- * @param string $lang
- */
- public function __construct($version, string $type = self::RSS2, string $charset = 'UTF-8', string $lang = 'en')
- {
- $this->version = $version;
- $this->type = $type;
- $this->charset = $charset;
- $this->lang = $lang;
- }
- /**
- * 设置标题
- *
- * @param string $title 标题
- */
- public function setTitle(string $title)
- {
- $this->title = $title;
- }
- /**
- * 设置副标题
- *
- * @param string|null $subTitle 副标题
- */
- public function setSubTitle(?string $subTitle)
- {
- $this->subTitle = $subTitle;
- }
- /**
- * 设置聚合地址
- *
- * @param string $feedUrl 聚合地址
- */
- public function setFeedUrl(string $feedUrl)
- {
- $this->feedUrl = $feedUrl;
- }
- /**
- * 设置主页
- *
- * @param string $baseUrl 主页地址
- */
- public function setBaseUrl(string $baseUrl)
- {
- $this->baseUrl = $baseUrl;
- }
- /**
- * $item的格式为
- * <code>
- * array (
- * 'title' => 'xxx',
- * 'content' => 'xxx',
- * 'excerpt' => 'xxx',
- * 'date' => 'xxx',
- * 'link' => 'xxx',
- * 'author' => 'xxx',
- * 'comments' => 'xxx',
- * 'commentsUrl'=> 'xxx',
- * 'commentsFeedUrl' => 'xxx',
- * )
- * </code>
- *
- * @param array $item
- */
- public function addItem(array $item)
- {
- $this->items[] = $item;
- }
- /**
- * 输出字符串
- *
- * @return string
- */
- public function __toString(): string
- {
- $result = '<?xml version="1.0" encoding="' . $this->charset . '"?>' . self::EOL;
- if (self::RSS1 == $this->type) {
- $result .= '<rdf:RDF
- xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
- xmlns="http://purl.org/rss/1.0/"
- xmlns:dc="http://purl.org/dc/elements/1.1/">' . self::EOL;
- $content = '';
- $links = [];
- $lastUpdate = 0;
- foreach ($this->items as $item) {
- $content .= '<item rdf:about="' . $item['link'] . '">' . self::EOL;
- $content .= '<title>' . htmlspecialchars($item['title']) . '</title>' . self::EOL;
- $content .= '<link>' . $item['link'] . '</link>' . self::EOL;
- $content .= '<dc:date>' . $this->dateFormat($item['date']) . '</dc:date>' . self::EOL;
- $content .= '<description>' . strip_tags($item['content']) . '</description>' . self::EOL;
- if (!empty($item['suffix'])) {
- $content .= $item['suffix'];
- }
- $content .= '</item>' . self::EOL;
- $links[] = $item['link'];
- if ($item['date'] > $lastUpdate) {
- $lastUpdate = $item['date'];
- }
- }
- $result .= '<channel rdf:about="' . $this->feedUrl . '">
- <title>' . htmlspecialchars($this->title) . '</title>
- <link>' . $this->baseUrl . '</link>
- <description>' . htmlspecialchars($this->subTitle ?? '') . '</description>
- <items>
- <rdf:Seq>' . self::EOL;
- foreach ($links as $link) {
- $result .= '<rdf:li resource="' . $link . '"/>' . self::EOL;
- }
- $result .= '</rdf:Seq>
- </items>
- </channel>' . self::EOL;
- $result .= $content . '</rdf:RDF>';
- } elseif (self::RSS2 == $this->type) {
- $result .= '<rss version="2.0"
- xmlns:content="http://purl.org/rss/1.0/modules/content/"
- xmlns:dc="http://purl.org/dc/elements/1.1/"
- xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
- xmlns:atom="http://www.w3.org/2005/Atom"
- xmlns:wfw="http://wellformedweb.org/CommentAPI/">
- <channel>' . self::EOL;
- $content = '';
- $lastUpdate = 0;
- foreach ($this->items as $item) {
- $content .= '<item>' . self::EOL;
- $content .= '<title>' . htmlspecialchars($item['title']) . '</title>' . self::EOL;
- $content .= '<link>' . $item['link'] . '</link>' . self::EOL;
- $content .= '<guid>' . $item['link'] . '</guid>' . self::EOL;
- $content .= '<pubDate>' . $this->dateFormat($item['date']) . '</pubDate>' . self::EOL;
- $content .= '<dc:creator>' . htmlspecialchars($item['author']->screenName)
- . '</dc:creator>' . self::EOL;
- if (!empty($item['category']) && is_array($item['category'])) {
- foreach ($item['category'] as $category) {
- $content .= '<category><![CDATA[' . $category['name'] . ']]></category>' . self::EOL;
- }
- }
- if (!empty($item['excerpt'])) {
- $content .= '<description><![CDATA[' . strip_tags($item['excerpt'])
- . ']]></description>' . self::EOL;
- }
- if (!empty($item['content'])) {
- $content .= '<content:encoded xml:lang="' . $this->lang . '"><![CDATA['
- . self::EOL .
- $item['content'] . self::EOL .
- ']]></content:encoded>' . self::EOL;
- }
- if (isset($item['comments']) && strlen($item['comments']) > 0) {
- $content .= '<slash:comments>' . $item['comments'] . '</slash:comments>' . self::EOL;
- }
- $content .= '<comments>' . $item['link'] . '#comments</comments>' . self::EOL;
- if (!empty($item['commentsFeedUrl'])) {
- $content .= '<wfw:commentRss>' . $item['commentsFeedUrl'] . '</wfw:commentRss>' . self::EOL;
- }
- if (!empty($item['suffix'])) {
- $content .= $item['suffix'];
- }
- $content .= '</item>' . self::EOL;
- if ($item['date'] > $lastUpdate) {
- $lastUpdate = $item['date'];
- }
- }
- $result .= '<title>' . htmlspecialchars($this->title) . '</title>
- <link>' . $this->baseUrl . '</link>
- <atom:link href="' . $this->feedUrl . '" rel="self" type="application/rss+xml" />
- <language>' . $this->lang . '</language>
- <description>' . htmlspecialchars($this->subTitle ?? '') . '</description>
- <lastBuildDate>' . $this->dateFormat($lastUpdate) . '</lastBuildDate>
- <pubDate>' . $this->dateFormat($lastUpdate) . '</pubDate>' . self::EOL;
- $result .= $content . '</channel>
- </rss>';
- } elseif (self::ATOM1 == $this->type) {
- $result .= '<feed xmlns="http://www.w3.org/2005/Atom"
- xmlns:thr="http://purl.org/syndication/thread/1.0"
- xml:lang="' . $this->lang . '"
- xml:base="' . $this->baseUrl . '"
- >' . self::EOL;
- $content = '';
- $lastUpdate = 0;
- foreach ($this->items as $item) {
- $content .= '<entry>' . self::EOL;
- $content .= '<title type="html"><![CDATA[' . $item['title'] . ']]></title>' . self::EOL;
- $content .= '<link rel="alternate" type="text/html" href="' . $item['link'] . '" />' . self::EOL;
- $content .= '<id>' . $item['link'] . '</id>' . self::EOL;
- $content .= '<updated>' . $this->dateFormat($item['date']) . '</updated>' . self::EOL;
- $content .= '<published>' . $this->dateFormat($item['date']) . '</published>' . self::EOL;
- $content .= '<author>
- <name>' . $item['author']->screenName . '</name>
- <uri>' . $item['author']->url . '</uri>
- </author>' . self::EOL;
- if (!empty($item['category']) && is_array($item['category'])) {
- foreach ($item['category'] as $category) {
- $content .= '<category scheme="' . $category['permalink'] . '" term="'
- . $category['name'] . '" />' . self::EOL;
- }
- }
- if (!empty($item['excerpt'])) {
- $content .= '<summary type="html"><![CDATA[' . htmlspecialchars($item['excerpt'])
- . ']]></summary>' . self::EOL;
- }
- if (!empty($item['content'])) {
- $content .= '<content type="html" xml:base="' . $item['link']
- . '" xml:lang="' . $this->lang . '"><![CDATA['
- . self::EOL .
- $item['content'] . self::EOL .
- ']]></content>' . self::EOL;
- }
- if (isset($item['comments']) && strlen($item['comments']) > 0) {
- $content .= '<link rel="replies" type="text/html" href="' . $item['link']
- . '#comments" thr:count="' . $item['comments'] . '" />' . self::EOL;
- if (!empty($item['commentsFeedUrl'])) {
- $content .= '<link rel="replies" type="application/atom+xml" href="'
- . $item['commentsFeedUrl'] . '" thr:count="' . $item['comments'] . '"/>' . self::EOL;
- }
- }
- if (!empty($item['suffix'])) {
- $content .= $item['suffix'];
- }
- $content .= '</entry>' . self::EOL;
- if ($item['date'] > $lastUpdate) {
- $lastUpdate = $item['date'];
- }
- }
- $result .= '<title type="text">' . htmlspecialchars($this->title) . '</title>
- <subtitle type="text">' . htmlspecialchars($this->subTitle ?? '') . '</subtitle>
- <updated>' . $this->dateFormat($lastUpdate) . '</updated>
- <generator uri="http://typecho.org/" version="' . $this->version . '">Typecho</generator>
- <link rel="alternate" type="text/html" href="' . $this->baseUrl . '" />
- <id>' . $this->feedUrl . '</id>
- <link rel="self" type="application/atom+xml" href="' . $this->feedUrl . '" />
- ';
- $result .= $content . '</feed>';
- }
- return $result;
- }
- /**
- * 获取Feed时间格式
- *
- * @param integer $stamp 时间戳
- * @return string
- */
- public function dateFormat(int $stamp): string
- {
- if (self::RSS2 == $this->type) {
- return date(self::DATE_RFC822, $stamp);
- } elseif (self::RSS1 == $this->type || self::ATOM1 == $this->type) {
- return date(self::DATE_W3CDTF, $stamp);
- }
- return '';
- }
- }
|