Rss.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Feed_Abstract
  23. */
  24. require_once 'Zend/Feed/Abstract.php';
  25. /**
  26. * @see Zend_Feed_Entry_Rss
  27. */
  28. require_once 'Zend/Feed/Entry/Rss.php';
  29. /**
  30. * RSS channel class
  31. *
  32. * The Zend_Feed_Rss class is a concrete subclass of
  33. * Zend_Feed_Abstract meant for representing RSS channels. It does not
  34. * add any methods to its parent, just provides a classname to check
  35. * against with the instanceof operator, and expects to be handling
  36. * RSS-formatted data instead of Atom.
  37. *
  38. * @category Zend
  39. * @package Zend_Feed
  40. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Feed_Rss extends Zend_Feed_Abstract
  44. {
  45. /**
  46. * The classname for individual channel elements.
  47. *
  48. * @var string
  49. */
  50. protected $_entryClassName = 'Zend_Feed_Entry_Rss';
  51. /**
  52. * The element name for individual channel elements (RSS <item>s).
  53. *
  54. * @var string
  55. */
  56. protected $_entryElementName = 'item';
  57. /**
  58. * The default namespace for RSS channels.
  59. *
  60. * @var string
  61. */
  62. protected $_defaultNamespace = 'rss';
  63. /**
  64. * Override Zend_Feed_Abstract to set up the $_element and $_entries aliases.
  65. *
  66. * @return void
  67. * @throws Zend_Feed_Exception
  68. */
  69. public function __wakeup()
  70. {
  71. parent::__wakeup();
  72. // Find the base channel element and create an alias to it.
  73. if ($this->_element->firstChild->nodeName == 'rdf:RDF') {
  74. $this->_element = $this->_element->firstChild;
  75. } else {
  76. $this->_element = $this->_element->getElementsByTagName('channel')->item(0);
  77. }
  78. if (!$this->_element) {
  79. /**
  80. * @see Zend_Feed_Exception
  81. */
  82. require_once 'Zend/Feed/Exception.php';
  83. throw new Zend_Feed_Exception('No root <channel> element found, cannot parse channel.');
  84. }
  85. // Find the entries and save a pointer to them for speed and
  86. // simplicity.
  87. $this->_buildEntryCache();
  88. }
  89. /**
  90. * Make accessing some individual elements of the channel easier.
  91. *
  92. * Special accessors 'item' and 'items' are provided so that if
  93. * you wish to iterate over an RSS channel's items, you can do so
  94. * using foreach ($channel->items as $item) or foreach
  95. * ($channel->item as $item).
  96. *
  97. * @param string $var The property to access.
  98. * @return mixed
  99. */
  100. public function __get($var)
  101. {
  102. switch ($var) {
  103. case 'item':
  104. // fall through to the next case
  105. case 'items':
  106. return $this;
  107. default:
  108. return parent::__get($var);
  109. }
  110. }
  111. /**
  112. * Generate the header of the feed when working in write mode
  113. *
  114. * @param array $array the data to use
  115. * @return DOMElement root node
  116. */
  117. protected function _mapFeedHeaders($array)
  118. {
  119. $channel = $this->_element->createElement('channel');
  120. $title = $this->_element->createElement('title');
  121. $title->appendChild($this->_element->createCDATASection($array->title));
  122. $channel->appendChild($title);
  123. $link = $this->_element->createElement('link', $array->link);
  124. $channel->appendChild($link);
  125. $desc = isset($array->description) ? $array->description : '';
  126. $description = $this->_element->createElement('description');
  127. $description->appendChild($this->_element->createCDATASection($desc));
  128. $channel->appendChild($description);
  129. $pubdate = isset($array->lastUpdate) ? $array->lastUpdate : time();
  130. $pubdate = $this->_element->createElement('pubDate', gmdate('r', $pubdate));
  131. $channel->appendChild($pubdate);
  132. if (isset($array->published)) {
  133. $lastBuildDate = $this->_element->createElement('lastBuildDate', gmdate('r', $array->published));
  134. $channel->appendChild($lastBuildDate);
  135. }
  136. $editor = '';
  137. if (!empty($array->email)) {
  138. $editor .= $array->email;
  139. }
  140. if (!empty($array->author)) {
  141. $editor .= ' (' . $array->author . ')';
  142. }
  143. if (!empty($editor)) {
  144. $author = $this->_element->createElement('managingEditor', ltrim($editor));
  145. $channel->appendChild($author);
  146. }
  147. if (isset($array->webmaster)) {
  148. $channel->appendChild($this->_element->createElement('webMaster', $array->webmaster));
  149. }
  150. if (!empty($array->copyright)) {
  151. $copyright = $this->_element->createElement('copyright', $array->copyright);
  152. $channel->appendChild($copyright);
  153. }
  154. if (isset($array->category)) {
  155. $category = $this->_element->createElement('category', $array->category);
  156. $channel->appendChild($category);
  157. }
  158. if (!empty($array->image)) {
  159. $image = $this->_element->createElement('image');
  160. $url = $this->_element->createElement('url', $array->image);
  161. $image->appendChild($url);
  162. $imagetitle = $this->_element->createElement('title');
  163. $imagetitle->appendChild($this->_element->createCDATASection($array->title));
  164. $image->appendChild($imagetitle);
  165. $imagelink = $this->_element->createElement('link', $array->link);
  166. $image->appendChild($imagelink);
  167. $channel->appendChild($image);
  168. }
  169. $generator = !empty($array->generator) ? $array->generator : 'Zend_Feed';
  170. $generator = $this->_element->createElement('generator', $generator);
  171. $channel->appendChild($generator);
  172. if (!empty($array->language)) {
  173. $language = $this->_element->createElement('language', $array->language);
  174. $channel->appendChild($language);
  175. }
  176. $doc = $this->_element->createElement('docs', 'http://blogs.law.harvard.edu/tech/rss');
  177. $channel->appendChild($doc);
  178. if (isset($array->cloud)) {
  179. $cloud = $this->_element->createElement('cloud');
  180. $cloud->setAttribute('domain', $array->cloud['uri']->getHost());
  181. $cloud->setAttribute('port', $array->cloud['uri']->getPort());
  182. $cloud->setAttribute('path', $array->cloud['uri']->getPath());
  183. $cloud->setAttribute('registerProcedure', $array->cloud['procedure']);
  184. $cloud->setAttribute('protocol', $array->cloud['protocol']);
  185. $channel->appendChild($cloud);
  186. }
  187. if (isset($array->ttl)) {
  188. $ttl = $this->_element->createElement('ttl', $array->ttl);
  189. $channel->appendChild($ttl);
  190. }
  191. if (isset($array->rating)) {
  192. $rating = $this->_element->createElement('rating', $array->rating);
  193. $channel->appendChild($rating);
  194. }
  195. if (isset($array->textInput)) {
  196. $textinput = $this->_element->createElement('textInput');
  197. $textinput->appendChild($this->_element->createElement('title', $array->textInput['title']));
  198. $textinput->appendChild($this->_element->createElement('description', $array->textInput['description']));
  199. $textinput->appendChild($this->_element->createElement('name', $array->textInput['name']));
  200. $textinput->appendChild($this->_element->createElement('link', $array->textInput['link']));
  201. $channel->appendChild($textinput);
  202. }
  203. if (isset($array->skipHours)) {
  204. $skipHours = $this->_element->createElement('skipHours');
  205. foreach ($array->skipHours as $hour) {
  206. $skipHours->appendChild($this->_element->createElement('hour', $hour));
  207. }
  208. $channel->appendChild($skipHours);
  209. }
  210. if (isset($array->skipDays)) {
  211. $skipDays = $this->_element->createElement('skipDays');
  212. foreach ($array->skipDays as $day) {
  213. $skipDays->appendChild($this->_element->createElement('day', $day));
  214. }
  215. $channel->appendChild($skipDays);
  216. }
  217. if (isset($array->itunes)) {
  218. $this->_buildiTunes($channel, $array);
  219. }
  220. return $channel;
  221. }
  222. /**
  223. * Adds the iTunes extensions to a root node
  224. *
  225. * @param DOMElement $root
  226. * @param array $array
  227. * @return void
  228. */
  229. private function _buildiTunes(DOMElement $root, $array)
  230. {
  231. /* author node */
  232. $author = '';
  233. if (isset($array->itunes->author)) {
  234. $author = $array->itunes->author;
  235. } elseif (isset($array->author)) {
  236. $author = $array->author;
  237. }
  238. if (!empty($author)) {
  239. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:author', $author);
  240. $root->appendChild($node);
  241. }
  242. /* owner node */
  243. $author = '';
  244. $email = '';
  245. if (isset($array->itunes->owner)) {
  246. if (isset($array->itunes->owner['name'])) {
  247. $author = $array->itunes->owner['name'];
  248. }
  249. if (isset($array->itunes->owner['email'])) {
  250. $email = $array->itunes->owner['email'];
  251. }
  252. }
  253. if (empty($author) && isset($array->author)) {
  254. $author = $array->author;
  255. }
  256. if (empty($email) && isset($array->email)) {
  257. $email = $array->email;
  258. }
  259. if (!empty($author) || !empty($email)) {
  260. $owner = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:owner');
  261. if (!empty($author)) {
  262. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:name', $author);
  263. $owner->appendChild($node);
  264. }
  265. if (!empty($email)) {
  266. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:email', $email);
  267. $owner->appendChild($node);
  268. }
  269. $root->appendChild($owner);
  270. }
  271. $image = '';
  272. if (isset($array->itunes->image)) {
  273. $image = $array->itunes->image;
  274. } elseif (isset($array->image)) {
  275. $image = $array->image;
  276. }
  277. if (!empty($image)) {
  278. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:image');
  279. $node->setAttribute('href', $image);
  280. $root->appendChild($node);
  281. }
  282. $subtitle = '';
  283. if (isset($array->itunes->subtitle)) {
  284. $subtitle = $array->itunes->subtitle;
  285. } elseif (isset($array->description)) {
  286. $subtitle = $array->description;
  287. }
  288. if (!empty($subtitle)) {
  289. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:subtitle', $subtitle);
  290. $root->appendChild($node);
  291. }
  292. $summary = '';
  293. if (isset($array->itunes->summary)) {
  294. $summary = $array->itunes->summary;
  295. } elseif (isset($array->description)) {
  296. $summary = $array->description;
  297. }
  298. if (!empty($summary)) {
  299. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:summary', $summary);
  300. $root->appendChild($node);
  301. }
  302. if (isset($array->itunes->block)) {
  303. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:block', $array->itunes->block);
  304. $root->appendChild($node);
  305. }
  306. if (isset($array->itunes->explicit)) {
  307. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:explicit', $array->itunes->explicit);
  308. $root->appendChild($node);
  309. }
  310. if (isset($array->itunes->keywords)) {
  311. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:keywords', $array->itunes->keywords);
  312. $root->appendChild($node);
  313. }
  314. if (isset($array->itunes->new_feed_url)) {
  315. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:new-feed-url', $array->itunes->new_feed_url);
  316. $root->appendChild($node);
  317. }
  318. if (isset($array->itunes->category)) {
  319. foreach ($array->itunes->category as $i => $category) {
  320. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:category');
  321. $node->setAttribute('text', $category['main']);
  322. $root->appendChild($node);
  323. $add_end_category = false;
  324. if (!empty($category['sub'])) {
  325. $add_end_category = true;
  326. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:category');
  327. $node->setAttribute('text', $category['sub']);
  328. $root->appendChild($node);
  329. }
  330. if ($i > 0 || $add_end_category) {
  331. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:category');
  332. $root->appendChild($node);
  333. }
  334. }
  335. }
  336. }
  337. /**
  338. * Generate the entries of the feed when working in write mode
  339. *
  340. * The following nodes are constructed for each feed entry
  341. * <item>
  342. * <title>entry title</title>
  343. * <link>url to feed entry</link>
  344. * <guid>url to feed entry</guid>
  345. * <description>short text</description>
  346. * <content:encoded>long version, can contain html</content:encoded>
  347. * </item>
  348. *
  349. * @param DOMElement $root the root node to use
  350. * @param array $array the data to use
  351. * @return void
  352. */
  353. protected function _mapFeedEntries(DOMElement $root, $array)
  354. {
  355. Zend_Feed::registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
  356. foreach ($array as $dataentry) {
  357. $item = $this->_element->createElement('item');
  358. $title = $this->_element->createElement('title');
  359. $title->appendChild($this->_element->createCDATASection($dataentry->title));
  360. $item->appendChild($title);
  361. $link = $this->_element->createElement('link', $dataentry->link);
  362. $item->appendChild($link);
  363. if (isset($dataentry->guid)) {
  364. $guid = $this->_element->createElement('guid', $dataentry->guid);
  365. $item->appendChild($guid);
  366. }
  367. $description = $this->_element->createElement('description');
  368. $description->appendChild($this->_element->createCDATASection($dataentry->description));
  369. $item->appendChild($description);
  370. if (isset($dataentry->content)) {
  371. $content = $this->_element->createElement('content:encoded');
  372. $content->appendChild($this->_element->createCDATASection($dataentry->content));
  373. $item->appendChild($content);
  374. }
  375. $pubdate = isset($dataentry->lastUpdate) ? $dataentry->lastUpdate : time();
  376. $pubdate = $this->_element->createElement('pubDate', gmdate('r', $pubdate));
  377. $item->appendChild($pubdate);
  378. if (isset($dataentry->category)) {
  379. foreach ($dataentry->category as $category) {
  380. $node = $this->_element->createElement('category', $category['term']);
  381. if (isset($category['scheme'])) {
  382. $node->setAttribute('domain', $category['scheme']);
  383. }
  384. $item->appendChild($node);
  385. }
  386. }
  387. if (isset($dataentry->source)) {
  388. $source = $this->_element->createElement('source', $dataentry->source['title']);
  389. $source->setAttribute('url', $dataentry->source['url']);
  390. $item->appendChild($source);
  391. }
  392. if (isset($dataentry->comments)) {
  393. $comments = $this->_element->createElement('comments', $dataentry->comments);
  394. $item->appendChild($comments);
  395. }
  396. if (isset($dataentry->commentRss)) {
  397. $comments = $this->_element->createElementNS('http://wellformedweb.org/CommentAPI/',
  398. 'wfw:commentRss',
  399. $dataentry->commentRss);
  400. $item->appendChild($comments);
  401. }
  402. if (isset($dataentry->enclosure)) {
  403. foreach ($dataentry->enclosure as $enclosure) {
  404. $node = $this->_element->createElement('enclosure');
  405. $node->setAttribute('url', $enclosure['url']);
  406. if (isset($enclosure['type'])) {
  407. $node->setAttribute('type', $enclosure['type']);
  408. }
  409. if (isset($enclosure['length'])) {
  410. $node->setAttribute('length', $enclosure['length']);
  411. }
  412. $item->appendChild($node);
  413. }
  414. }
  415. $root->appendChild($item);
  416. }
  417. }
  418. /**
  419. * Override Zend_Feed_Element to include <rss> root node
  420. *
  421. * @return string
  422. */
  423. public function saveXml()
  424. {
  425. // Return a complete document including XML prologue.
  426. $doc = new DOMDocument($this->_element->ownerDocument->version,
  427. $this->_element->ownerDocument->actualEncoding);
  428. $root = $doc->createElement('rss');
  429. // Use rss version 2.0
  430. $root->setAttribute('version', '2.0');
  431. // Content namespace
  432. $root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:content', 'http://purl.org/rss/1.0/modules/content/');
  433. $root->appendChild($doc->importNode($this->_element, true));
  434. // Append root node
  435. $doc->appendChild($root);
  436. // Format output
  437. $doc->formatOutput = true;
  438. return $doc->saveXML();
  439. }
  440. /**
  441. * Send feed to a http client with the correct header
  442. *
  443. * @return void
  444. * @throws Zend_Feed_Exception if headers have already been sent
  445. */
  446. public function send()
  447. {
  448. if (headers_sent()) {
  449. /**
  450. * @see Zend_Feed_Exception
  451. */
  452. require_once 'Zend/Feed/Exception.php';
  453. throw new Zend_Feed_Exception('Cannot send RSS because headers have already been sent.');
  454. }
  455. header('Content-Type: application/rss+xml; charset=' . $this->_element->ownerDocument->actualEncoding);
  456. echo $this->saveXml();
  457. }
  458. }