PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Feed/Atom.php

https://github.com/sluther/portsensor
PHP | 381 lines | 201 code | 52 blank | 128 comment | 27 complexity | 07332b70116d7e6b0be2c3c890f10d09 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  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: Atom.php 4733 2007-05-06 19:41:25Z thomas $
  20. */
  21. /**
  22. * @see Zend_Feed_Abstract
  23. */
  24. require_once 'Zend/Feed/Abstract.php';
  25. /**
  26. * @see Zend_Feed_Entry_Atom
  27. */
  28. require_once 'Zend/Feed/Entry/Atom.php';
  29. /**
  30. * Atom feed class
  31. *
  32. * The Zend_Feed_Atom class is a concrete subclass of the general
  33. * Zend_Feed_Abstract class, tailored for representing an Atom
  34. * feed. It shares all of the same methods with its abstract
  35. * parent. The distinction is made in the format of data that
  36. * Zend_Feed_Atom expects, and as a further pointer for users as to
  37. * what kind of feed object they have been passed.
  38. *
  39. * @category Zend
  40. * @package Zend_Feed
  41. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Feed_Atom extends Zend_Feed_Abstract
  45. {
  46. /**
  47. * The classname for individual feed elements.
  48. *
  49. * @var string
  50. */
  51. protected $_entryClassName = 'Zend_Feed_Entry_Atom';
  52. /**
  53. * The element name for individual feed elements (Atom <entry>
  54. * elements).
  55. *
  56. * @var string
  57. */
  58. protected $_entryElementName = 'entry';
  59. /**
  60. * The default namespace for Atom feeds.
  61. *
  62. * @var string
  63. */
  64. protected $_defaultNamespace = 'atom';
  65. /**
  66. * Override Zend_Feed_Abstract to set up the $_element and $_entries aliases.
  67. *
  68. * @return void
  69. */
  70. public function __wakeup()
  71. {
  72. parent::__wakeup();
  73. // Find the base feed element and create an alias to it.
  74. $element = $this->_element->getElementsByTagName('feed')->item(0);
  75. if (!$element) {
  76. // Try to find a single <entry> instead.
  77. $element = $this->_element->getElementsByTagName($this->_entryElementName)->item(0);
  78. if (!$element) {
  79. throw new Zend_Feed_Exception('No root <feed> or <' . $this->_entryElementName
  80. . '> element found, cannot parse feed.');
  81. }
  82. $doc = new DOMDocument($this->_element->version,
  83. $this->_element->actualEncoding);
  84. $feed = $doc->appendChild($doc->createElement('feed'));
  85. $feed->appendChild($doc->importNode($element, true));
  86. $element = $feed;
  87. }
  88. $this->_element = $element;
  89. // Find the entries and save a pointer to them for speed and
  90. // simplicity.
  91. $this->_buildEntryCache();
  92. }
  93. /**
  94. * Easy access to <link> tags keyed by "rel" attributes.
  95. *
  96. * If $elt->link() is called with no arguments, we will attempt to
  97. * return the value of the <link> tag(s) like all other
  98. * method-syntax attribute access. If an argument is passed to
  99. * link(), however, then we will return the "href" value of the
  100. * first <link> tag that has a "rel" attribute matching $rel:
  101. *
  102. * $elt->link(): returns the value of the link tag.
  103. * $elt->link('self'): returns the href from the first <link rel="self"> in the entry.
  104. *
  105. * @param string $rel The "rel" attribute to look for.
  106. * @return mixed
  107. */
  108. public function link($rel = null)
  109. {
  110. if ($rel === null) {
  111. return parent::__call('link', null);
  112. }
  113. // index link tags by their "rel" attribute.
  114. $links = parent::__get('link');
  115. if (!is_array($links)) {
  116. if ($links instanceof Zend_Feed_Element) {
  117. $links = array($links);
  118. } else {
  119. return $links;
  120. }
  121. }
  122. foreach ($links as $link) {
  123. if (empty($link['rel'])) {
  124. continue;
  125. }
  126. if ($rel == $link['rel']) {
  127. return $link['href'];
  128. }
  129. }
  130. return null;
  131. }
  132. /**
  133. * Make accessing some individual elements of the feed easier.
  134. *
  135. * Special accessors 'entry' and 'entries' are provided so that if
  136. * you wish to iterate over an Atom feed's entries, you can do so
  137. * using foreach ($feed->entries as $entry) or foreach
  138. * ($feed->entry as $entry).
  139. *
  140. * @param string $var The property to access.
  141. * @return mixed
  142. */
  143. public function __get($var)
  144. {
  145. switch ($var) {
  146. case 'entry':
  147. // fall through to the next case
  148. case 'entries':
  149. return $this;
  150. default:
  151. return parent::__get($var);
  152. }
  153. }
  154. /**
  155. * Generate the header of the feed when working in write mode
  156. *
  157. * @param array $array the data to use
  158. * @return DOMElement root node
  159. */
  160. protected function _mapFeedHeaders($array)
  161. {
  162. $feed = $this->_element->createElement('feed');
  163. $feed->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
  164. $id = $this->_element->createElement('id', $array->link);
  165. $feed->appendChild($id);
  166. $title = $this->_element->createElement('title');
  167. $title->appendChild($this->_element->createCDATASection($array->title));
  168. $feed->appendChild($title);
  169. if (isset($array->author)) {
  170. $author = $this->_element->createElement('author');
  171. $name = $this->_element->createElement('name', $array->author);
  172. $author->appendChild($name);
  173. if (isset($array->email)) {
  174. $email = $this->_element->createElement('email', $array->email);
  175. $author->appendChild($email);
  176. }
  177. $feed->appendChild($author);
  178. }
  179. $updated = isset($array->lastUpdate) ? $array->lastUpdate : time();
  180. $updated = $this->_element->createElement('updated', date(DATE_ATOM, $updated));
  181. $feed->appendChild($updated);
  182. if (isset($array->published)) {
  183. $published = $this->_element->createElement('published', date(DATE_ATOM, $array->published));
  184. $feed->appendChild($published);
  185. }
  186. $link = $this->_element->createElement('link');
  187. $link->setAttribute('rel', 'self');
  188. $link->setAttribute('href', $array->link);
  189. if (isset($array->language)) {
  190. $link->setAttribute('hreflang', $array->language);
  191. }
  192. $feed->appendChild($link);
  193. if (isset($array->description)) {
  194. $subtitle = $this->_element->createElement('subtitle');
  195. $subtitle->appendChild($this->_element->createCDATASection($array->description));
  196. $feed->appendChild($subtitle);
  197. }
  198. if (isset($array->copyright)) {
  199. $copyright = $this->_element->createElement('rights', $array->copyright);
  200. $feed->appendChild($copyright);
  201. }
  202. if (isset($array->image)) {
  203. $image = $this->_element->createElement('logo', $array->image);
  204. $feed->appendChild($image);
  205. }
  206. $generator = !empty($array->generator) ? $array->generator : 'Zend_Feed';
  207. $generator = $this->_element->createElement('generator', $generator);
  208. $feed->appendChild($generator);
  209. return $feed;
  210. }
  211. /**
  212. * Generate the entries of the feed when working in write mode
  213. *
  214. * The following nodes are constructed for each feed entry
  215. * <entry>
  216. * <id>url to feed entry</id>
  217. * <title>entry title</title>
  218. * <updated>last update</updated>
  219. * <link rel="alternate" href="url to feed entry" />
  220. * <summary>short text</summary>
  221. * <content>long version, can contain html</content>
  222. * </entry>
  223. *
  224. * @param array $array the data to use
  225. * @param DOMElement $root the root node to use
  226. * @return void
  227. */
  228. protected function _mapFeedEntries(DOMElement $root, $array)
  229. {
  230. foreach ($array as $dataentry) {
  231. $entry = $this->_element->createElement('entry');
  232. $id = $this->_element->createElement('id', isset($dataentry->guid) ? $dataentry->guid : $dataentry->link);
  233. $entry->appendChild($id);
  234. $title = $this->_element->createElement('title');
  235. $title->appendChild($this->_element->createCDATASection($dataentry->title));
  236. $entry->appendChild($title);
  237. $updated = isset($dataentry->lastUpdate) ? $dataentry->lastUpdate : time();
  238. $updated = $this->_element->createElement('updated', date(DATE_ATOM, $updated));
  239. $entry->appendChild($updated);
  240. $link = $this->_element->createElement('link');
  241. $link->setAttribute('rel', 'alternate');
  242. $link->setAttribute('href', $dataentry->link);
  243. $entry->appendChild($link);
  244. $summary = $this->_element->createElement('summary');
  245. $summary->appendChild($this->_element->createCDATASection($dataentry->description));
  246. $entry->appendChild($summary);
  247. if (isset($dataentry->content)) {
  248. $content = $this->_element->createElement('content');
  249. $content->setAttribute('type', 'html');
  250. $content->appendChild($this->_element->createCDATASection($dataentry->content));
  251. $entry->appendChild($content);
  252. }
  253. if (isset($dataentry->category)) {
  254. foreach ($dataentry->category as $category) {
  255. $node = $this->_element->createElement('category');
  256. $node->setAttribute('term', $category['term']);
  257. if (isset($category['scheme'])) {
  258. $node->setAttribute('scheme', $category['scheme']);
  259. }
  260. $entry->appendChild($node);
  261. }
  262. }
  263. if (isset($dataentry->source)) {
  264. $source = $this->_element->createElement('source');
  265. $title = $this->_element->createElement('title', $dataentry->source['title']);
  266. $source->appendChild($title);
  267. $link = $this->_element->createElement('link', $dataentry->source['title']);
  268. $link->setAttribute('rel', 'alternate');
  269. $link->setAttribute('href', $dataentry->source['url']);
  270. $source->appendChild($link);
  271. }
  272. if (isset($dataentry->enclosure)) {
  273. foreach ($dataentry->enclosure as $enclosure) {
  274. $node = $this->_element->createElement('link');
  275. $node->setAttribute('rel', 'enclosure');
  276. $node->setAttribute('href', $enclosure['url']);
  277. if (isset($enclosure['type'])) {
  278. $node->setAttribute('type', $enclosure['type']);
  279. }
  280. if (isset($enclosure['length'])) {
  281. $node->setAttribute('length', $enclosure['length']);
  282. }
  283. $entry->appendChild($node);
  284. }
  285. }
  286. if (isset($dataentry->comments)) {
  287. $comments = $this->_element->createElementNS('http://wellformedweb.org/CommentAPI/',
  288. 'wfw:comment',
  289. $dataentry->comments);
  290. $entry->appendChild($comments);
  291. }
  292. if (isset($dataentry->commentRss)) {
  293. $comments = $this->_element->createElementNS('http://wellformedweb.org/CommentAPI/',
  294. 'wfw:commentRss',
  295. $dataentry->commentRss);
  296. $entry->appendChild($comments);
  297. }
  298. $root->appendChild($entry);
  299. }
  300. }
  301. /**
  302. * Override Zend_Feed_Element to allow formated feeds
  303. *
  304. * @return string
  305. */
  306. public function saveXML()
  307. {
  308. // Return a complete document including XML prologue.
  309. $doc = new DOMDocument($this->_element->ownerDocument->version,
  310. $this->_element->ownerDocument->actualEncoding);
  311. $doc->appendChild($doc->importNode($this->_element, true));
  312. $doc->formatOutput = true;
  313. return $doc->saveXML();
  314. }
  315. /**
  316. * Send feed to a http client with the correct header
  317. *
  318. * @throws Zend_Feed_Exception if headers have already been sent
  319. * @return void
  320. */
  321. public function send()
  322. {
  323. if (headers_sent()) {
  324. throw new Zend_Feed_Exception('Cannot send ATOM because headers have already been sent.');
  325. }
  326. header('Content-type: application/atom+xml; charset: ' . $this->_element->ownerDocument->actualEncoding);
  327. echo $this->saveXML();
  328. }
  329. }