PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/app/sitesearch/lib/Zend/Feed/Rss.php

https://github.com/cbrunet/sitellite
PHP | 496 lines | 314 code | 60 blank | 122 comment | 54 complexity | 4662a2d441fd963f4bbdde282bc67d0f MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  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-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Rss.php,v 1.1.1.1 2007/08/12 09:24:29 lux Exp $
  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-2007 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. * @throws Zend_Feed_Exception
  67. * @return void
  68. */
  69. public function __wakeup()
  70. {
  71. parent::__wakeup();
  72. // Find the base channel element and create an alias to it.
  73. $this->_element = $this->_element->getElementsByTagName('channel')->item(0);
  74. if (!$this->_element) {
  75. throw new Zend_Feed_Exception('No root <channel> element found, cannot parse channel.');
  76. }
  77. // Find the entries and save a pointer to them for speed and
  78. // simplicity.
  79. $this->_buildEntryCache();
  80. }
  81. /**
  82. * Make accessing some individual elements of the channel easier.
  83. *
  84. * Special accessors 'item' and 'items' are provided so that if
  85. * you wish to iterate over an RSS channel's items, you can do so
  86. * using foreach ($channel->items as $item) or foreach
  87. * ($channel->item as $item).
  88. *
  89. * @param string $var The property to access.
  90. * @return mixed
  91. */
  92. public function __get($var)
  93. {
  94. switch ($var) {
  95. case 'item':
  96. // fall through to the next case
  97. case 'items':
  98. return $this;
  99. default:
  100. return parent::__get($var);
  101. }
  102. }
  103. /**
  104. * Generate the header of the feed when working in write mode
  105. *
  106. * @param array $array the data to use
  107. * @return DOMElement root node
  108. */
  109. protected function _mapFeedHeaders($array)
  110. {
  111. $channel = $this->_element->createElement('channel');
  112. $title = $this->_element->createElement('title');
  113. $title->appendChild($this->_element->createCDATASection($array->title));
  114. $channel->appendChild($title);
  115. $link = $this->_element->createElement('link', $array->link);
  116. $channel->appendChild($link);
  117. $desc = isset($array->description) ? $array->description : '';
  118. $description = $this->_element->createElement('description');
  119. $description->appendChild($this->_element->createCDATASection($desc));
  120. $channel->appendChild($description);
  121. $pubdate = isset($array->lastUpdate) ? $array->lastUpdate : time();
  122. $pubdate = $this->_element->createElement('pubDate', gmdate('r', $pubdate));
  123. $channel->appendChild($pubdate);
  124. if (isset($array->published)) {
  125. $lastBuildDate = $this->_element->createElement('lastBuildDate', gmdate('r', $array->published));
  126. }
  127. $editor = '';
  128. if (!empty($array->email)) {
  129. $editor .= $array->email;
  130. }
  131. if (!empty($array->author)) {
  132. $editor .= ' (' . $array->author . ')';
  133. }
  134. if (!empty($editor)) {
  135. $author = $this->_element->createElement('managingEditor', ltrim($editor));
  136. $channel->appendChild($author);
  137. }
  138. if (isset($array->webmaster)) {
  139. $channel->appendChild($this->_element->createElement('webMaster', $array->webmaster));
  140. }
  141. if (!empty($array->copyright)) {
  142. $copyright = $this->_element->createElement('copyright', $array->copyright);
  143. $channel->appendChild($copyright);
  144. }
  145. if (!empty($array->image)) {
  146. $image = $this->_element->createElement('image');
  147. $url = $this->_element->createElement('url', $array->image);
  148. $image->appendChild($url);
  149. $imagetitle = $this->_element->createElement('title', $array->title);
  150. $image->appendChild($imagetitle);
  151. $imagelink = $this->_element->createElement('link', $array->link);
  152. $image->appendChild($imagelink);
  153. $channel->appendChild($image);
  154. }
  155. $generator = !empty($array->generator) ? $array->generator : 'Zend_Feed';
  156. $generator = $this->_element->createElement('generator', $generator);
  157. $channel->appendChild($generator);
  158. if (!empty($array->language)) {
  159. $language = $this->_element->createElement('language', $array->language);
  160. $channel->appendChild($language);
  161. }
  162. $doc = $this->_element->createElement('docs', 'http://blogs.law.harvard.edu/tech/rss');
  163. $channel->appendChild($doc);
  164. if (isset($array->cloud)) {
  165. $cloud = $this->_element->createElement('cloud');
  166. $cloud->setAttribute('domain', $array->cloud['uri']->getHost());
  167. $cloud->setAttribute('port', $array->cloud['uri']->getPort());
  168. $cloud->setAttribute('path', $array->cloud['uri']->getPath());
  169. $cloud->setAttribute('registerProcedure', $array->cloud['procedure']);
  170. $cloud->setAttribute('protocol', $array->cloud['protocol']);
  171. $channel->appendChild($cloud);
  172. }
  173. if (isset($array->rating)) {
  174. $rating = $this->_element->createElement('rating', $array->rating);
  175. $channel->appendChild($rating);
  176. }
  177. if (isset($array->textInput)) {
  178. $textinput = $this->_element->createElement('textInput');
  179. $textinput->appendChild($this->_element->createElement('title', $array->textInput['title']));
  180. $textinput->appendChild($this->_element->createElement('description', $array->textInput['description']));
  181. $textinput->appendChild($this->_element->createElement('name', $array->textInput['name']));
  182. $textinput->appendChild($this->_element->createElement('link', $array->textInput['link']));
  183. $channel->appendChild($textinput);
  184. }
  185. if (isset($array->skipHours)) {
  186. $skipHours = $this->_element->createElement('skipHours');
  187. foreach ($array->skipHours as $hour) {
  188. $skipHours->appendChild($this->_element->createElement('hour', $hour));
  189. }
  190. $channel->appendChild($skipHours);
  191. }
  192. if (isset($array->skipDays)) {
  193. $skipDays = $this->_element->createElement('skipDays');
  194. foreach ($array->skipDays as $day) {
  195. $skipDays->appendChild($this->_element->createElement('day', $day));
  196. }
  197. $channel->appendChild($skipDays);
  198. }
  199. if (isset($array->itunes)) {
  200. $this->_buildiTunes($channel, $array);
  201. }
  202. return $channel;
  203. }
  204. /**
  205. * Adds the iTunes extensions to a root node
  206. *
  207. * @param DOMElement $root
  208. * @param array $array
  209. * @return void
  210. */
  211. private function _buildiTunes(DOMElement $root, $array)
  212. {
  213. /* author node */
  214. $author = '';
  215. if (isset($array->itunes->author)) {
  216. $author = $array->itunes->author;
  217. } elseif (isset($array->author)) {
  218. $author = $array->author;
  219. }
  220. if (!empty($author)) {
  221. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:author', $author);
  222. $root->appendChild($node);
  223. }
  224. /* owner node */
  225. $author = '';
  226. $email = '';
  227. if (isset($array->itunes->owner)) {
  228. if (isset($array->itunes->owner['name'])) {
  229. $author = $array->itunes->owner['name'];
  230. }
  231. if (isset($array->itunes->owner['email'])) {
  232. $email = $array->itunes->owner['email'];
  233. }
  234. }
  235. if (empty($author) && isset($array->author)) {
  236. $author = $array->author;
  237. }
  238. if (empty($email) && isset($array->email)) {
  239. $email = $array->email;
  240. }
  241. if (!empty($author) || !empty($email)) {
  242. $owner = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:owner');
  243. if (!empty($author)) {
  244. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:name', $author);
  245. $owner->appendChild($node);
  246. }
  247. if (!empty($email)) {
  248. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:email', $email);
  249. $owner->appendChild($node);
  250. }
  251. $root->appendChild($owner);
  252. }
  253. $image = '';
  254. if (isset($array->itunes->image)) {
  255. $image = $array->itunes->image;
  256. } elseif (isset($array->image)) {
  257. $image = $array->image;
  258. }
  259. if (!empty($image)) {
  260. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:image');
  261. $node->setAttribute('href', $image);
  262. $root->appendChild($node);
  263. }
  264. $subtitle = '';
  265. if (isset($array->itunes->subtitle)) {
  266. $subtitle = $array->itunes->subtitle;
  267. } elseif (isset($array->description)) {
  268. $subtitle = $array->description;
  269. }
  270. if (!empty($subtitle)) {
  271. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:subtitle', $subtitle);
  272. $root->appendChild($node);
  273. }
  274. $summary = '';
  275. if (isset($array->itunes->summary)) {
  276. $summary = $array->itunes->summary;
  277. } elseif (isset($array->description)) {
  278. $summary = $array->description;
  279. }
  280. if (!empty($summary)) {
  281. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:summary', $summary);
  282. $root->appendChild($node);
  283. }
  284. if (isset($array->itunes->block)) {
  285. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:block', $array->itunes->block);
  286. $root->appendChild($node);
  287. }
  288. if (isset($array->itunes->explicit)) {
  289. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:explicit', $array->itunes->explicit);
  290. $root->appendChild($node);
  291. }
  292. if (isset($array->itunes->keywords)) {
  293. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:keywords', $array->itunes->keywords);
  294. $root->appendChild($node);
  295. }
  296. if (isset($array->itunes->new_feed_url)) {
  297. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:new-feed-url', $array->itunes->new_feed_url);
  298. $root->appendChild($node);
  299. }
  300. if (isset($array->itunes->category)) {
  301. foreach ($array->itunes->category as $i => $category) {
  302. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:category');
  303. $node->setAttribute('text', $category['main']);
  304. $root->appendChild($node);
  305. $add_end_category = false;
  306. if (!empty($category['sub'])) {
  307. $add_end_category = true;
  308. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:category');
  309. $node->setAttribute('text', $category['sub']);
  310. $root->appendChild($node);
  311. }
  312. if ($i > 0 || $add_end_category) {
  313. $node = $this->_element->createElementNS('http://www.itunes.com/DTDs/Podcast-1.0.dtd', 'itunes:category');
  314. $root->appendChild($node);
  315. }
  316. }
  317. }
  318. }
  319. /**
  320. * Generate the entries of the feed when working in write mode
  321. *
  322. * The following nodes are constructed for each feed entry
  323. * <item>
  324. * <title>entry title</title>
  325. * <link>url to feed entry</link>
  326. * <guid>url to feed entry</guid>
  327. * <description>short text</description>
  328. * <content:encoded>long version, can contain html</content:encoded>
  329. * </item>
  330. *
  331. * @param DOMElement $root the root node to use
  332. * @param array $array the data to use
  333. * @return void
  334. */
  335. protected function _mapFeedEntries(DOMElement $root, $array)
  336. {
  337. Zend_Feed::registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
  338. foreach ($array as $dataentry) {
  339. $item = $this->_element->createElement('item');
  340. $title = $this->_element->createElement('title');
  341. $title->appendChild($this->_element->createCDATASection($dataentry->title));
  342. $item->appendChild($title);
  343. $link = $this->_element->createElement('link', $dataentry->link);
  344. $item->appendChild($link);
  345. if (isset($dataentry->guid)) {
  346. $guid = $this->_element->createElement('guid', $dataentry->guid);
  347. $item->appendChild($guid);
  348. }
  349. $description = $this->_element->createElement('description');
  350. $description->appendChild($this->_element->createCDATASection($dataentry->description));
  351. $item->appendChild($description);
  352. if (isset($dataentry->content)) {
  353. $content = $this->_element->createElement('content:encoded');
  354. $content->appendChild($this->_element->createCDATASection($dataentry->content));
  355. $item->appendChild($content);
  356. }
  357. $pubdate = isset($dataentry->lastUpdate) ? $dataentry->lastUpdate : time();
  358. $pubdate = $this->_element->createElement('pubDate', gmdate('r', $pubdate));
  359. $item->appendChild($pubdate);
  360. if (isset($dataentry->category)) {
  361. foreach ($dataentry->category as $category) {
  362. $node = $this->_element->createElement('category', $category['term']);
  363. if (isset($category['scheme'])) {
  364. $node->setAttribute('domain', $category['scheme']);
  365. }
  366. $item->appendChild($node);
  367. }
  368. }
  369. if (isset($dataentry->source)) {
  370. $source = $this->_element->createElement('source', $dataentry->source['title']);
  371. $source->setAttribute('url', $dataentry->source['url']);
  372. $item->appendChild($source);
  373. }
  374. if (isset($dataentry->comments)) {
  375. $comments = $this->_element->createElement('comments', $dataentry->comments);
  376. $item->appendChild($comments);
  377. }
  378. if (isset($dataentry->commentRss)) {
  379. $comments = $this->_element->createElementNS('http://wellformedweb.org/CommentAPI/',
  380. 'wfw:commentRss',
  381. $dataentry->commentRss);
  382. $item->appendChild($comments);
  383. }
  384. if (isset($dataentry->enclosure)) {
  385. foreach ($dataentry->enclosure as $enclosure) {
  386. $node = $this->_element->createElement('enclosure');
  387. $node->setAttribute('url', $enclosure['url']);
  388. if (isset($enclosure['type'])) {
  389. $node->setAttribute('type', $enclosure['type']);
  390. }
  391. if (isset($enclosure['length'])) {
  392. $node->setAttribute('length', $enclosure['length']);
  393. }
  394. $item->appendChild($node);
  395. }
  396. }
  397. $root->appendChild($item);
  398. }
  399. }
  400. /**
  401. * Override Zend_Feed_Element to include <rss> root node
  402. *
  403. * @return string
  404. */
  405. public function saveXML()
  406. {
  407. // Return a complete document including XML prologue.
  408. $doc = new DOMDocument($this->_element->ownerDocument->version,
  409. $this->_element->ownerDocument->actualEncoding);
  410. $root = $doc->createElement('rss');
  411. // Use rss version 2.0
  412. $root->setAttribute('version', '2.0');
  413. // Content namespace
  414. $root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:content', 'http://purl.org/rss/1.0/modules/content/');
  415. $root->appendChild($doc->importNode($this->_element, true));
  416. // Append root node
  417. $doc->appendChild($root);
  418. // Format output
  419. $doc->formatOutput = true;
  420. return $doc->saveXML();
  421. }
  422. /**
  423. * Send feed to a http client with the correct header
  424. *
  425. * @throws Zend_Feed_Exception if headers have already been sent
  426. * @return void
  427. */
  428. public function send()
  429. {
  430. if (headers_sent()) {
  431. throw new Zend_Feed_Exception('Cannot send RSS because headers have already been sent.');
  432. }
  433. header('Content-type: application/rss+xml; charset: ' . $this->_element->ownerDocument->actualEncoding);
  434. echo $this->saveXML();
  435. }
  436. }