Feed.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. namespace Typecho;
  3. /**
  4. * Feed
  5. *
  6. * @package Feed
  7. */
  8. class Feed
  9. {
  10. /** 定义RSS 1.0类型 */
  11. public const RSS1 = 'RSS 1.0';
  12. /** 定义RSS 2.0类型 */
  13. public const RSS2 = 'RSS 2.0';
  14. /** 定义ATOM 1.0类型 */
  15. public const ATOM1 = 'ATOM 1.0';
  16. /** 定义RSS时间格式 */
  17. public const DATE_RFC822 = 'r';
  18. /** 定义ATOM时间格式 */
  19. public const DATE_W3CDTF = 'c';
  20. /** 定义行结束符 */
  21. public const EOL = "\n";
  22. /**
  23. * feed状态
  24. *
  25. * @access private
  26. * @var string
  27. */
  28. private $type;
  29. /**
  30. * 字符集编码
  31. *
  32. * @access private
  33. * @var string
  34. */
  35. private $charset;
  36. /**
  37. * 语言状态
  38. *
  39. * @access private
  40. * @var string
  41. */
  42. private $lang;
  43. /**
  44. * 聚合地址
  45. *
  46. * @access private
  47. * @var string
  48. */
  49. private $feedUrl;
  50. /**
  51. * 基本地址
  52. *
  53. * @access private
  54. * @var string
  55. */
  56. private $baseUrl;
  57. /**
  58. * 聚合标题
  59. *
  60. * @access private
  61. * @var string
  62. */
  63. private $title;
  64. /**
  65. * 聚合副标题
  66. *
  67. * @access private
  68. * @var string
  69. */
  70. private $subTitle;
  71. /**
  72. * 版本信息
  73. *
  74. * @access private
  75. * @var string
  76. */
  77. private $version;
  78. /**
  79. * 所有的items
  80. *
  81. * @access private
  82. * @var array
  83. */
  84. private $items = [];
  85. /**
  86. * 创建Feed对象
  87. *
  88. * @param $version
  89. * @param string $type
  90. * @param string $charset
  91. * @param string $lang
  92. */
  93. public function __construct($version, string $type = self::RSS2, string $charset = 'UTF-8', string $lang = 'en')
  94. {
  95. $this->version = $version;
  96. $this->type = $type;
  97. $this->charset = $charset;
  98. $this->lang = $lang;
  99. }
  100. /**
  101. * 设置标题
  102. *
  103. * @param string $title 标题
  104. */
  105. public function setTitle(string $title)
  106. {
  107. $this->title = $title;
  108. }
  109. /**
  110. * 设置副标题
  111. *
  112. * @param string|null $subTitle 副标题
  113. */
  114. public function setSubTitle(?string $subTitle)
  115. {
  116. $this->subTitle = $subTitle;
  117. }
  118. /**
  119. * 设置聚合地址
  120. *
  121. * @param string $feedUrl 聚合地址
  122. */
  123. public function setFeedUrl(string $feedUrl)
  124. {
  125. $this->feedUrl = $feedUrl;
  126. }
  127. /**
  128. * 设置主页
  129. *
  130. * @param string $baseUrl 主页地址
  131. */
  132. public function setBaseUrl(string $baseUrl)
  133. {
  134. $this->baseUrl = $baseUrl;
  135. }
  136. /**
  137. * $item的格式为
  138. * <code>
  139. * array (
  140. * 'title' => 'xxx',
  141. * 'content' => 'xxx',
  142. * 'excerpt' => 'xxx',
  143. * 'date' => 'xxx',
  144. * 'link' => 'xxx',
  145. * 'author' => 'xxx',
  146. * 'comments' => 'xxx',
  147. * 'commentsUrl'=> 'xxx',
  148. * 'commentsFeedUrl' => 'xxx',
  149. * )
  150. * </code>
  151. *
  152. * @param array $item
  153. */
  154. public function addItem(array $item)
  155. {
  156. $this->items[] = $item;
  157. }
  158. /**
  159. * 输出字符串
  160. *
  161. * @return string
  162. */
  163. public function __toString(): string
  164. {
  165. $result = '<?xml version="1.0" encoding="' . $this->charset . '"?>' . self::EOL;
  166. if (self::RSS1 == $this->type) {
  167. $result .= '<rdf:RDF
  168. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  169. xmlns="http://purl.org/rss/1.0/"
  170. xmlns:dc="http://purl.org/dc/elements/1.1/">' . self::EOL;
  171. $content = '';
  172. $links = [];
  173. $lastUpdate = 0;
  174. foreach ($this->items as $item) {
  175. $content .= '<item rdf:about="' . $item['link'] . '">' . self::EOL;
  176. $content .= '<title>' . htmlspecialchars($item['title']) . '</title>' . self::EOL;
  177. $content .= '<link>' . $item['link'] . '</link>' . self::EOL;
  178. $content .= '<dc:date>' . $this->dateFormat($item['date']) . '</dc:date>' . self::EOL;
  179. $content .= '<description>' . strip_tags($item['content']) . '</description>' . self::EOL;
  180. if (!empty($item['suffix'])) {
  181. $content .= $item['suffix'];
  182. }
  183. $content .= '</item>' . self::EOL;
  184. $links[] = $item['link'];
  185. if ($item['date'] > $lastUpdate) {
  186. $lastUpdate = $item['date'];
  187. }
  188. }
  189. $result .= '<channel rdf:about="' . $this->feedUrl . '">
  190. <title>' . htmlspecialchars($this->title) . '</title>
  191. <link>' . $this->baseUrl . '</link>
  192. <description>' . htmlspecialchars($this->subTitle ?? '') . '</description>
  193. <items>
  194. <rdf:Seq>' . self::EOL;
  195. foreach ($links as $link) {
  196. $result .= '<rdf:li resource="' . $link . '"/>' . self::EOL;
  197. }
  198. $result .= '</rdf:Seq>
  199. </items>
  200. </channel>' . self::EOL;
  201. $result .= $content . '</rdf:RDF>';
  202. } elseif (self::RSS2 == $this->type) {
  203. $result .= '<rss version="2.0"
  204. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  205. xmlns:dc="http://purl.org/dc/elements/1.1/"
  206. xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  207. xmlns:atom="http://www.w3.org/2005/Atom"
  208. xmlns:wfw="http://wellformedweb.org/CommentAPI/">
  209. <channel>' . self::EOL;
  210. $content = '';
  211. $lastUpdate = 0;
  212. foreach ($this->items as $item) {
  213. $content .= '<item>' . self::EOL;
  214. $content .= '<title>' . htmlspecialchars($item['title']) . '</title>' . self::EOL;
  215. $content .= '<link>' . $item['link'] . '</link>' . self::EOL;
  216. $content .= '<guid>' . $item['link'] . '</guid>' . self::EOL;
  217. $content .= '<pubDate>' . $this->dateFormat($item['date']) . '</pubDate>' . self::EOL;
  218. $content .= '<dc:creator>' . htmlspecialchars($item['author']->screenName)
  219. . '</dc:creator>' . self::EOL;
  220. if (!empty($item['category']) && is_array($item['category'])) {
  221. foreach ($item['category'] as $category) {
  222. $content .= '<category><![CDATA[' . $category['name'] . ']]></category>' . self::EOL;
  223. }
  224. }
  225. if (!empty($item['excerpt'])) {
  226. $content .= '<description><![CDATA[' . strip_tags($item['excerpt'])
  227. . ']]></description>' . self::EOL;
  228. }
  229. if (!empty($item['content'])) {
  230. $content .= '<content:encoded xml:lang="' . $this->lang . '"><![CDATA['
  231. . self::EOL .
  232. $item['content'] . self::EOL .
  233. ']]></content:encoded>' . self::EOL;
  234. }
  235. if (isset($item['comments']) && strlen($item['comments']) > 0) {
  236. $content .= '<slash:comments>' . $item['comments'] . '</slash:comments>' . self::EOL;
  237. }
  238. $content .= '<comments>' . $item['link'] . '#comments</comments>' . self::EOL;
  239. if (!empty($item['commentsFeedUrl'])) {
  240. $content .= '<wfw:commentRss>' . $item['commentsFeedUrl'] . '</wfw:commentRss>' . self::EOL;
  241. }
  242. if (!empty($item['suffix'])) {
  243. $content .= $item['suffix'];
  244. }
  245. $content .= '</item>' . self::EOL;
  246. if ($item['date'] > $lastUpdate) {
  247. $lastUpdate = $item['date'];
  248. }
  249. }
  250. $result .= '<title>' . htmlspecialchars($this->title) . '</title>
  251. <link>' . $this->baseUrl . '</link>
  252. <atom:link href="' . $this->feedUrl . '" rel="self" type="application/rss+xml" />
  253. <language>' . $this->lang . '</language>
  254. <description>' . htmlspecialchars($this->subTitle ?? '') . '</description>
  255. <lastBuildDate>' . $this->dateFormat($lastUpdate) . '</lastBuildDate>
  256. <pubDate>' . $this->dateFormat($lastUpdate) . '</pubDate>' . self::EOL;
  257. $result .= $content . '</channel>
  258. </rss>';
  259. } elseif (self::ATOM1 == $this->type) {
  260. $result .= '<feed xmlns="http://www.w3.org/2005/Atom"
  261. xmlns:thr="http://purl.org/syndication/thread/1.0"
  262. xml:lang="' . $this->lang . '"
  263. xml:base="' . $this->baseUrl . '"
  264. >' . self::EOL;
  265. $content = '';
  266. $lastUpdate = 0;
  267. foreach ($this->items as $item) {
  268. $content .= '<entry>' . self::EOL;
  269. $content .= '<title type="html"><![CDATA[' . $item['title'] . ']]></title>' . self::EOL;
  270. $content .= '<link rel="alternate" type="text/html" href="' . $item['link'] . '" />' . self::EOL;
  271. $content .= '<id>' . $item['link'] . '</id>' . self::EOL;
  272. $content .= '<updated>' . $this->dateFormat($item['date']) . '</updated>' . self::EOL;
  273. $content .= '<published>' . $this->dateFormat($item['date']) . '</published>' . self::EOL;
  274. $content .= '<author>
  275. <name>' . $item['author']->screenName . '</name>
  276. <uri>' . $item['author']->url . '</uri>
  277. </author>' . self::EOL;
  278. if (!empty($item['category']) && is_array($item['category'])) {
  279. foreach ($item['category'] as $category) {
  280. $content .= '<category scheme="' . $category['permalink'] . '" term="'
  281. . $category['name'] . '" />' . self::EOL;
  282. }
  283. }
  284. if (!empty($item['excerpt'])) {
  285. $content .= '<summary type="html"><![CDATA[' . htmlspecialchars($item['excerpt'])
  286. . ']]></summary>' . self::EOL;
  287. }
  288. if (!empty($item['content'])) {
  289. $content .= '<content type="html" xml:base="' . $item['link']
  290. . '" xml:lang="' . $this->lang . '"><![CDATA['
  291. . self::EOL .
  292. $item['content'] . self::EOL .
  293. ']]></content>' . self::EOL;
  294. }
  295. if (isset($item['comments']) && strlen($item['comments']) > 0) {
  296. $content .= '<link rel="replies" type="text/html" href="' . $item['link']
  297. . '#comments" thr:count="' . $item['comments'] . '" />' . self::EOL;
  298. if (!empty($item['commentsFeedUrl'])) {
  299. $content .= '<link rel="replies" type="application/atom+xml" href="'
  300. . $item['commentsFeedUrl'] . '" thr:count="' . $item['comments'] . '"/>' . self::EOL;
  301. }
  302. }
  303. if (!empty($item['suffix'])) {
  304. $content .= $item['suffix'];
  305. }
  306. $content .= '</entry>' . self::EOL;
  307. if ($item['date'] > $lastUpdate) {
  308. $lastUpdate = $item['date'];
  309. }
  310. }
  311. $result .= '<title type="text">' . htmlspecialchars($this->title) . '</title>
  312. <subtitle type="text">' . htmlspecialchars($this->subTitle ?? '') . '</subtitle>
  313. <updated>' . $this->dateFormat($lastUpdate) . '</updated>
  314. <generator uri="http://typecho.org/" version="' . $this->version . '">Typecho</generator>
  315. <link rel="alternate" type="text/html" href="' . $this->baseUrl . '" />
  316. <id>' . $this->feedUrl . '</id>
  317. <link rel="self" type="application/atom+xml" href="' . $this->feedUrl . '" />
  318. ';
  319. $result .= $content . '</feed>';
  320. }
  321. return $result;
  322. }
  323. /**
  324. * 获取Feed时间格式
  325. *
  326. * @param integer $stamp 时间戳
  327. * @return string
  328. */
  329. public function dateFormat(int $stamp): string
  330. {
  331. if (self::RSS2 == $this->type) {
  332. return date(self::DATE_RFC822, $stamp);
  333. } elseif (self::RSS1 == $this->type || self::ATOM1 == $this->type) {
  334. return date(self::DATE_W3CDTF, $stamp);
  335. }
  336. return '';
  337. }
  338. }