atompubclient.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Client class for AtomPub
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Cache
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Client class for AtomPub
  37. *
  38. * @category General
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class AtomPubClient
  46. {
  47. public $url;
  48. private $user, $pass;
  49. /**
  50. *
  51. * @param string $url collection feed URL
  52. * @param string $user auth username
  53. * @param string $pass auth password
  54. */
  55. function __construct($url, $user, $pass)
  56. {
  57. $this->url = $url;
  58. $this->user = $user;
  59. $this->pass = $pass;
  60. }
  61. /**
  62. * Set up an HTTPClient with auth for our resource.
  63. *
  64. * @param string $method
  65. * @return HTTPClient
  66. */
  67. private function httpClient($method='GET')
  68. {
  69. $client = new HTTPClient($this->url);
  70. $client->setMethod($method);
  71. $client->setAuth($this->user, $this->pass);
  72. return $client;
  73. }
  74. function get()
  75. {
  76. $client = $this->httpClient('GET');
  77. $response = $client->send();
  78. if ($response->isOk()) {
  79. return $response->getBody();
  80. } else {
  81. throw new Exception("Bogus return code: " . $response->getStatus() . ': ' . $response->getBody());
  82. }
  83. }
  84. /**
  85. * Create a new resource by POSTing it to the collection.
  86. * If successful, will return the URL representing the
  87. * canonical location of the new resource. Neat!
  88. *
  89. * @param string $data
  90. * @param string $type defaults to Atom entry
  91. * @return string URL to the created resource
  92. *
  93. * @throws exceptions on failure
  94. */
  95. function post($data, $type='application/atom+xml;type=entry')
  96. {
  97. $client = $this->httpClient('POST');
  98. $client->setHeader('Content-Type', $type);
  99. // optional Slug header not used in this case
  100. $client->setBody($data);
  101. $response = $client->send();
  102. if ($response->getStatus() != '201') {
  103. throw new Exception("Expected HTTP 201 on POST, got " . $response->getStatus() . ': ' . $response->getBody());
  104. }
  105. $loc = $response->getHeader('Location');
  106. $contentLoc = $response->getHeader('Content-Location');
  107. if (empty($loc)) {
  108. throw new Exception("AtomPub POST response missing Location header.");
  109. }
  110. if (!empty($contentLoc)) {
  111. if ($loc != $contentLoc) {
  112. throw new Exception("AtomPub POST response Location and Content-Location headers do not match.");
  113. }
  114. // If Content-Location and Location match, that means the response
  115. // body is safe to interpret as the resource itself.
  116. if ($type == 'application/atom+xml;type=entry') {
  117. self::validateAtomEntry($response->getBody());
  118. }
  119. }
  120. return $loc;
  121. }
  122. /**
  123. * Note that StatusNet currently doesn't allow PUT editing on notices.
  124. *
  125. * @param string $data
  126. * @param string $type defaults to Atom entry
  127. * @return true on success
  128. *
  129. * @throws exceptions on failure
  130. */
  131. function put($data, $type='application/atom+xml;type=entry')
  132. {
  133. $client = $this->httpClient('PUT');
  134. $client->setHeader('Content-Type', $type);
  135. $client->setBody($data);
  136. $response = $client->send();
  137. if ($response->getStatus() != '200' && $response->getStatus() != '204') {
  138. throw new Exception("Expected HTTP 200 or 204 on PUT, got " . $response->getStatus() . ': ' . $response->getBody());
  139. }
  140. return true;
  141. }
  142. /**
  143. * Delete the resource.
  144. *
  145. * @return true on success
  146. *
  147. * @throws exceptions on failure
  148. */
  149. function delete()
  150. {
  151. $client = $this->httpClient('DELETE');
  152. $client->setBody($data);
  153. $response = $client->send();
  154. if ($response->getStatus() != '200' && $response->getStatus() != '204') {
  155. throw new Exception("Expected HTTP 200 or 204 on DELETE, got " . $response->getStatus() . ': ' . $response->getBody());
  156. }
  157. return true;
  158. }
  159. /**
  160. * Ensure that the given string is a parseable Atom entry.
  161. *
  162. * @param string $str
  163. * @return boolean
  164. * @throws Exception on invalid input
  165. */
  166. static function validateAtomEntry($str)
  167. {
  168. if (empty($str)) {
  169. throw new Exception('Bad Atom entry: empty');
  170. }
  171. $dom = new DOMDocument;
  172. if (!$dom->loadXML($str)) {
  173. throw new Exception('Bad Atom entry: XML is not well formed.');
  174. }
  175. $activity = new Activity($dom->documentRoot);
  176. return true;
  177. }
  178. static function entryEditURL($str) {
  179. $dom = new DOMDocument;
  180. $dom->loadXML($str);
  181. $path = new DOMXPath($dom);
  182. $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
  183. $links = $path->query('/atom:entry/atom:link[@rel="edit"]', $dom->documentRoot);
  184. if ($links && $links->length) {
  185. if ($links->length > 1) {
  186. throw new Exception('Bad Atom entry; has multiple rel=edit links.');
  187. }
  188. $link = $links->item(0);
  189. $url = $link->getAttribute('href');
  190. return $url;
  191. } else {
  192. throw new Exception('Atom entry lists no rel=edit link.');
  193. }
  194. }
  195. static function entryId($str) {
  196. $dom = new DOMDocument;
  197. $dom->loadXML($str);
  198. $path = new DOMXPath($dom);
  199. $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
  200. $links = $path->query('/atom:entry/atom:id', $dom->documentRoot);
  201. if ($links && $links->length) {
  202. if ($links->length > 1) {
  203. throw new Exception('Bad Atom entry; has multiple id entries.');
  204. }
  205. $link = $links->item(0);
  206. $url = $link->textContent;
  207. return $url;
  208. } else {
  209. throw new Exception('Atom entry lists no id.');
  210. }
  211. }
  212. static function getEntryInFeed($str, $id)
  213. {
  214. $dom = new DOMDocument;
  215. $dom->loadXML($str);
  216. $path = new DOMXPath($dom);
  217. $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
  218. $query = '/atom:feed/atom:entry[atom:id="'.$id.'"]';
  219. $items = $path->query($query, $dom->documentRoot);
  220. if ($items && $items->length) {
  221. return $items->item(0);
  222. } else {
  223. return null;
  224. }
  225. }
  226. }