3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A remote, atompub-receiving service
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 2010 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 * A remote service that supports AtomPub
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2010 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
49 protected $svcDocUrl = null;
50 protected $username = null;
51 protected $password = null;
52 protected $collections = array();
54 function __construct($svcDocUrl, $username, $password)
56 $this->svcDocUrl = $svcDocUrl;
57 $this->username = $username;
58 $this->password = $password;
60 $this->_parseSvcDoc();
63 private function _parseSvcDoc()
65 $client = new HTTPClient();
66 $response = $client->get($this->svcDocUrl);
68 if ($response->getStatus() != 200) {
69 throw new Exception("Can't get {$this->svcDocUrl}; response status " . $response->getStatus());
72 $xml = $response->getBody();
74 $dom = new DOMDocument();
76 // We don't want to bother with white spaces
77 $dom->preserveWhiteSpace = false;
79 // Don't spew XML warnings to output
80 $old = error_reporting();
81 error_reporting($old & ~E_WARNING);
82 $ok = $dom->loadXML($xml);
83 error_reporting($old);
85 $path = new DOMXPath($dom);
87 $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
88 $path->registerNamespace('app', 'http://www.w3.org/2007/app');
89 $path->registerNamespace('activity', 'http://activitystrea.ms/spec/1.0/');
91 $collections = $path->query('//app:collection');
93 for ($i = 0; $i < $collections->length; $i++) {
94 $collection = $collections->item($i);
95 $url = $collection->getAttribute('href');
96 $takesEntries = false;
97 $accepts = $path->query('app:accept', $collection);
98 for ($j = 0; $j < $accepts->length; $j++) {
99 $accept = $accepts->item($j);
100 $acceptValue = $accept->nodeValue;
101 if (preg_match('#application/atom\+xml(;\s*type=entry)?#', $acceptValue)) {
102 $takesEntries = true;
107 if (!$takesEntries) {
110 $verbs = $path->query('activity:verb', $collection);
111 if ($verbs->length == 0) {
112 $this->_addCollection(ActivityVerb::POST, $url);
114 for ($k = 0; $k < $verbs->length; $k++) {
115 $verb = $verbs->item($k);
116 $this->_addCollection($verb->nodeValue, $url);
122 private function _addCollection($verb, $url)
124 if (array_key_exists($verb, $this->collections)) {
125 $this->collections[$verb][] = $url;
127 $this->collections[$verb] = array($url);
132 function postActivity($activity)
134 if (!array_key_exists($activity->verb, $this->collections)) {
135 throw new Exception("No collection for verb {$activity->verb}");
137 if (count($this->collections[$activity->verb]) > 1) {
138 common_log(LOG_NOTICE, "More than one collection for verb {$activity->verb}");
140 $this->postToCollection($this->collections[$activity->verb][0], $activity);
144 function postToCollection($url, $activity)
146 $client = new HTTPClient($url);
148 $client->setMethod('POST');
149 $client->setAuth($this->username, $this->password);
150 $client->setHeader('Content-Type', 'application/atom+xml;type=entry');
151 $client->setBody($activity->asString(true, true, true));
153 $response = $client->send();
155 $status = $response->getStatus();
156 $reason = $response->getReasonPhrase();
158 if ($status >= 200 && $status < 300) {
160 } else if ($status >= 400 && $status < 500) {
161 // TRANS: Client exception thrown when post to collection fails with a 400 status.
162 // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason.
163 throw new ClientException(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));
164 } else if ($status >= 500 && $status < 600) {
165 // TRANS: Server exception thrown when post to collection fails with a 500 status.
166 // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason.
167 throw new ServerException(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));
169 // That's unexpected.
170 // TRANS: Exception thrown when post to collection fails with a status that is not handled.
171 // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason.
172 throw new Exception(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));