3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Client class for AtomPub
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Client class for AtomPub
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2011 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
55 * @param string $url collection feed URL
56 * @param string $user auth username
57 * @param string $pass auth password
59 function __construct($url, $user, $pass)
67 * Set up an HTTPClient with auth for our resource.
69 * @param string $method
72 private function httpClient($method='GET')
74 $client = new HTTPClient($this->url);
75 $client->setMethod($method);
76 $client->setAuth($this->user, $this->pass);
82 $client = $this->httpClient('GET');
83 $response = $client->send();
84 if ($response->isOk()) {
85 return $response->getBody();
87 throw new Exception("Bogus return code: " . $response->getStatus() . ': ' . $response->getBody());
92 * Create a new resource by POSTing it to the collection.
93 * If successful, will return the URL representing the
94 * canonical location of the new resource. Neat!
97 * @param string $type defaults to Atom entry
98 * @return string URL to the created resource
100 * @throws exceptions on failure
102 function post($data, $type='application/atom+xml;type=entry')
104 $client = $this->httpClient('POST');
105 $client->setHeader('Content-Type', $type);
106 // optional Slug header not used in this case
107 $client->setBody($data);
108 $response = $client->send();
110 if ($response->getStatus() != '201') {
111 throw new Exception("Expected HTTP 201 on POST, got " . $response->getStatus() . ': ' . $response->getBody());
113 $loc = $response->getHeader('Location');
114 $contentLoc = $response->getHeader('Content-Location');
117 throw new Exception("AtomPub POST response missing Location header.");
119 if (!empty($contentLoc)) {
120 if ($loc != $contentLoc) {
121 throw new Exception("AtomPub POST response Location and Content-Location headers do not match.");
124 // If Content-Location and Location match, that means the response
125 // body is safe to interpret as the resource itself.
126 if ($type == 'application/atom+xml;type=entry') {
127 self::validateAtomEntry($response->getBody());
135 * Note that StatusNet currently doesn't allow PUT editing on notices.
137 * @param string $data
138 * @param string $type defaults to Atom entry
139 * @return true on success
141 * @throws exceptions on failure
143 function put($data, $type='application/atom+xml;type=entry')
145 $client = $this->httpClient('PUT');
146 $client->setHeader('Content-Type', $type);
147 $client->setBody($data);
148 $response = $client->send();
150 if ($response->getStatus() != '200' && $response->getStatus() != '204') {
151 throw new Exception("Expected HTTP 200 or 204 on PUT, got " . $response->getStatus() . ': ' . $response->getBody());
158 * Delete the resource.
160 * @return true on success
162 * @throws exceptions on failure
166 $client = $this->httpClient('DELETE');
167 $client->setBody($data);
168 $response = $client->send();
170 if ($response->getStatus() != '200' && $response->getStatus() != '204') {
171 throw new Exception("Expected HTTP 200 or 204 on DELETE, got " . $response->getStatus() . ': ' . $response->getBody());
178 * Ensure that the given string is a parseable Atom entry.
182 * @throws Exception on invalid input
184 static function validateAtomEntry($str)
187 throw new Exception('Bad Atom entry: empty');
189 $dom = new DOMDocument;
190 if (!$dom->loadXML($str)) {
191 throw new Exception('Bad Atom entry: XML is not well formed.');
194 $activity = new Activity($dom->documentRoot);
198 static function entryEditURL($str) {
199 $dom = new DOMDocument;
201 $path = new DOMXPath($dom);
202 $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
204 $links = $path->query('/atom:entry/atom:link[@rel="edit"]', $dom->documentRoot);
205 if ($links && $links->length) {
206 if ($links->length > 1) {
207 throw new Exception('Bad Atom entry; has multiple rel=edit links.');
209 $link = $links->item(0);
210 $url = $link->getAttribute('href');
213 throw new Exception('Atom entry lists no rel=edit link.');
217 static function entryId($str) {
218 $dom = new DOMDocument;
220 $path = new DOMXPath($dom);
221 $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
223 $links = $path->query('/atom:entry/atom:id', $dom->documentRoot);
224 if ($links && $links->length) {
225 if ($links->length > 1) {
226 throw new Exception('Bad Atom entry; has multiple id entries.');
228 $link = $links->item(0);
229 $url = $link->textContent;
232 throw new Exception('Atom entry lists no id.');
236 static function getEntryInFeed($str, $id)
238 $dom = new DOMDocument;
240 $path = new DOMXPath($dom);
241 $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
243 $query = '/atom:feed/atom:entry[atom:id="'.$id.'"]';
244 $items = $path->query($query, $dom->documentRoot);
245 if ($items && $items->length) {
246 return $items->item(0);