PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Feed/Entry/Atom.php

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