Atom.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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-2015 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_Entry_Abstract
  23. */
  24. require_once 'Zend/Feed/Entry/Abstract.php';
  25. /** @see Zend_Xml_Security */
  26. require_once 'Zend/Xml/Security.php';
  27. /**
  28. * Concrete class for working with Atom entries.
  29. *
  30. * @category Zend
  31. * @package Zend_Feed
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Feed_Entry_Atom extends Zend_Feed_Entry_Abstract
  36. {
  37. /**
  38. * Content-Type
  39. */
  40. const CONTENT_TYPE = 'application/atom+xml';
  41. /**
  42. * Root XML element for Atom entries.
  43. *
  44. * @var string
  45. */
  46. protected $_rootElement = 'entry';
  47. /**
  48. * Root namespace for Atom entries.
  49. *
  50. * @var string
  51. */
  52. protected $_rootNamespace = 'atom';
  53. /**
  54. * Delete an atom entry.
  55. *
  56. * Delete tries to delete this entry from its feed. If the entry
  57. * does not contain a link rel="edit", we throw an error (either
  58. * the entry does not yet exist or this is not an editable
  59. * feed). If we have a link rel="edit", we do the empty-body
  60. * HTTP DELETE to that URI and check for a response of 2xx.
  61. * Usually the response would be 204 No Content, but the Atom
  62. * Publishing Protocol permits it to be 200 OK.
  63. *
  64. * @return void
  65. * @throws Zend_Feed_Exception
  66. */
  67. public function delete()
  68. {
  69. // Look for link rel="edit" in the entry object.
  70. $deleteUri = $this->link('edit');
  71. if (!$deleteUri) {
  72. /**
  73. * @see Zend_Feed_Exception
  74. */
  75. require_once 'Zend/Feed/Exception.php';
  76. throw new Zend_Feed_Exception('Cannot delete entry; no link rel="edit" is present.');
  77. }
  78. // DELETE
  79. $client = Zend_Feed::getHttpClient();
  80. do {
  81. $client->setUri($deleteUri);
  82. if (Zend_Feed::getHttpMethodOverride()) {
  83. $client->setHeader('X-HTTP-Method-Override', 'DELETE');
  84. $response = $client->request('POST');
  85. } else {
  86. $response = $client->request('DELETE');
  87. }
  88. $httpStatus = $response->getStatus();
  89. switch ((int) $httpStatus / 100) {
  90. // Success
  91. case 2:
  92. return true;
  93. // Redirect
  94. case 3:
  95. $deleteUri = $response->getHeader('Location');
  96. continue;
  97. // Error
  98. default:
  99. /**
  100. * @see Zend_Feed_Exception
  101. */
  102. require_once 'Zend/Feed/Exception.php';
  103. throw new Zend_Feed_Exception("Expected response code 2xx, got $httpStatus");
  104. }
  105. } while (true);
  106. }
  107. /**
  108. * Save a new or updated Atom entry.
  109. *
  110. * Save is used to either create new entries or to save changes to
  111. * existing ones. If we have a link rel="edit", we are changing
  112. * an existing entry. In this case we re-serialize the entry and
  113. * PUT it to the edit URI, checking for a 200 OK result.
  114. *
  115. * For posting new entries, you must specify the $postUri
  116. * parameter to save() to tell the object where to post itself.
  117. * We use $postUri and POST the serialized entry there, checking
  118. * for a 201 Created response. If the insert is successful, we
  119. * then parse the response from the POST to get any values that
  120. * the server has generated: an id, an updated time, and its new
  121. * link rel="edit".
  122. *
  123. * @param string $postUri Location to POST for creating new entries.
  124. * @return void
  125. * @throws Zend_Feed_Exception
  126. */
  127. public function save($postUri = null)
  128. {
  129. if ($this->id()) {
  130. // If id is set, look for link rel="edit" in the
  131. // entry object and PUT.
  132. $editUri = $this->link('edit');
  133. if (!$editUri) {
  134. /**
  135. * @see Zend_Feed_Exception
  136. */
  137. require_once 'Zend/Feed/Exception.php';
  138. throw new Zend_Feed_Exception('Cannot edit entry; no link rel="edit" is present.');
  139. }
  140. $client = Zend_Feed::getHttpClient();
  141. $client->setUri($editUri);
  142. if (Zend_Feed::getHttpMethodOverride()) {
  143. $client->setHeaders(array('X-HTTP-Method-Override: PUT',
  144. 'Content-Type: ' . self::CONTENT_TYPE));
  145. $client->setRawData($this->saveXML());
  146. $response = $client->request('POST');
  147. } else {
  148. $client->setHeaders('Content-Type', self::CONTENT_TYPE);
  149. $client->setRawData($this->saveXML());
  150. $response = $client->request('PUT');
  151. }
  152. if ($response->getStatus() !== 200) {
  153. /**
  154. * @see Zend_Feed_Exception
  155. */
  156. require_once 'Zend/Feed/Exception.php';
  157. throw new Zend_Feed_Exception('Expected response code 200, got ' . $response->getStatus());
  158. }
  159. } else {
  160. if ($postUri === null) {
  161. /**
  162. * @see Zend_Feed_Exception
  163. */
  164. require_once 'Zend/Feed/Exception.php';
  165. throw new Zend_Feed_Exception('PostURI must be specified to save new entries.');
  166. }
  167. $client = Zend_Feed::getHttpClient();
  168. $client->setUri($postUri);
  169. $client->setHeaders('Content-Type', self::CONTENT_TYPE);
  170. $client->setRawData($this->saveXML());
  171. $response = $client->request('POST');
  172. if ($response->getStatus() !== 201) {
  173. /**
  174. * @see Zend_Feed_Exception
  175. */
  176. require_once 'Zend/Feed/Exception.php';
  177. throw new Zend_Feed_Exception('Expected response code 201, got '
  178. . $response->getStatus());
  179. }
  180. }
  181. // Update internal properties using $client->responseBody;
  182. @ini_set('track_errors', 1);
  183. $newEntry = new DOMDocument;
  184. $newEntry = @Zend_Xml_Security::scan($response->getBody(), $newEntry);
  185. @ini_restore('track_errors');
  186. if (!$newEntry) {
  187. // prevent the class to generate an undefined variable notice (ZF-2590)
  188. if (!isset($php_errormsg)) {
  189. if (function_exists('xdebug_is_enabled')) {
  190. $php_errormsg = '(error message not available, when XDebug is running)';
  191. } else {
  192. $php_errormsg = '(error message not available)';
  193. }
  194. }
  195. /**
  196. * @see Zend_Feed_Exception
  197. */
  198. require_once 'Zend/Feed/Exception.php';
  199. throw new Zend_Feed_Exception('XML cannot be parsed: ' . $php_errormsg);
  200. }
  201. $newEntry = $newEntry->getElementsByTagName($this->_rootElement)->item(0);
  202. if (!$newEntry) {
  203. /**
  204. * @see Zend_Feed_Exception
  205. */
  206. require_once 'Zend/Feed/Exception.php';
  207. throw new Zend_Feed_Exception('No root <feed> element found in server response:'
  208. . "\n\n" . $client->responseBody);
  209. }
  210. if ($this->_element->parentNode) {
  211. $oldElement = $this->_element;
  212. $this->_element = $oldElement->ownerDocument->importNode($newEntry, true);
  213. $oldElement->parentNode->replaceChild($this->_element, $oldElement);
  214. } else {
  215. $this->_element = $newEntry;
  216. }
  217. }
  218. /**
  219. * Easy access to <link> tags keyed by "rel" attributes.
  220. *
  221. * If $elt->link() is called with no arguments, we will attempt to
  222. * return the value of the <link> tag(s) like all other
  223. * method-syntax attribute access. If an argument is passed to
  224. * link(), however, then we will return the "href" value of the
  225. * first <link> tag that has a "rel" attribute matching $rel:
  226. *
  227. * $elt->link(): returns the value of the link tag.
  228. * $elt->link('self'): returns the href from the first <link rel="self"> in the entry.
  229. *
  230. * @param string $rel The "rel" attribute to look for.
  231. * @return mixed
  232. */
  233. public function link($rel = null)
  234. {
  235. if ($rel === null) {
  236. return parent::__call('link', null);
  237. }
  238. // index link tags by their "rel" attribute.
  239. $links = parent::__get('link');
  240. if (!is_array($links)) {
  241. if ($links instanceof Zend_Feed_Element) {
  242. $links = array($links);
  243. } else {
  244. return $links;
  245. }
  246. }
  247. foreach ($links as $link) {
  248. if (empty($link['rel'])) {
  249. $link['rel'] = 'alternate'; // see Atom 1.0 spec
  250. }
  251. if ($rel == $link['rel']) {
  252. return $link['href'];
  253. }
  254. }
  255. return null;
  256. }
  257. }