]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitysink.php
Misses this file to merge. I like the comments.
[quix0rs-gnu-social.git] / lib / activitysink.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A remote, atompub-receiving service
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  AtomPub
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * A remote service that supports AtomPub
39  *
40  * @category  AtomPub
41  * @package   StatusNet
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/
46  */
47 class ActivitySink
48 {
49     protected $svcDocUrl   = null;
50     protected $username    = null;
51     protected $password    = null;
52     protected $collections = array();
53
54     function __construct($svcDocUrl, $username, $password)
55     {
56         $this->svcDocUrl = $svcDocUrl;
57         $this->username  = $username;
58         $this->password  = $password;
59
60         $this->_parseSvcDoc();
61     }
62
63     private function _parseSvcDoc()
64     {
65         $client   = new HTTPClient();
66         $response = $client->get($this->svcDocUrl);
67
68         if ($response->getStatus() != 200) {
69             throw new Exception("Can't get {$this->svcDocUrl}; response status " . $response->getStatus());
70         }
71
72         $xml = $response->getBody();
73
74         $dom = new DOMDocument();
75
76         // We don't want to bother with white spaces
77         $dom->preserveWhiteSpace = false;
78
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);
84
85         $path = new DOMXPath($dom);
86
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/');
90
91         $collections = $path->query('//app:collection');
92
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;
103                     break;
104                 }
105             }
106
107             if (!$takesEntries) {
108                 continue;
109             }
110             $verbs = $path->query('activity:verb', $collection);
111             if ($verbs->length == 0) {
112                 $this->_addCollection(ActivityVerb::POST, $url);
113             } else {
114                 for ($k = 0; $k < $verbs->length; $k++) {
115                     $verb = $verbs->item($k);
116                     $this->_addCollection($verb->nodeValue, $url);
117                 }
118             }
119         }
120     }
121
122     private function _addCollection($verb, $url)
123     {
124         if (array_key_exists($verb, $this->collections)) {
125             $this->collections[$verb][] = $url;
126         } else {
127             $this->collections[$verb] = array($url);
128         }
129         return;
130     }
131
132     function postActivity($activity)
133     {
134         if (!array_key_exists($activity->verb, $this->collections)) {
135             throw new Exception("No collection for verb {$activity->verb}");
136         } else {
137             if (count($this->collections[$activity->verb]) > 1) {
138                 common_log(LOG_NOTICE, "More than one collection for verb {$activity->verb}");
139             }
140             $this->postToCollection($this->collections[$activity->verb][0], $activity);
141         }
142     }
143
144     function postToCollection($url, $activity)
145     {
146         $client = new HTTPClient($url);
147
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));
152
153         $response = $client->send();
154
155         $status = $response->getStatus();
156         $reason = $response->getReasonPhrase();
157
158         if ($status >= 200 && $status < 300) {
159             return true;
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));
168         } else {
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));
173         }
174     }
175 }