]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/omb.php
a distributed -> the distributed
[quix0rs-gnu-social.git] / lib / omb.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once('OAuth.php');
23 require_once(INSTALLDIR.'/lib/oauthstore.php');
24
25 require_once(INSTALLDIR.'/classes/Consumer.php');
26 require_once(INSTALLDIR.'/classes/Nonce.php');
27 require_once(INSTALLDIR.'/classes/Token.php');
28
29 require_once('Auth/Yadis/Yadis.php');
30
31 define('OAUTH_NAMESPACE', 'http://oauth.net/core/1.0/');
32 define('OMB_NAMESPACE', 'http://openmicroblogging.org/protocol/0.1');
33 define('OMB_VERSION_01', 'http://openmicroblogging.org/protocol/0.1');
34 define('OAUTH_DISCOVERY', 'http://oauth.net/discovery/1.0');
35
36 define('OMB_ENDPOINT_UPDATEPROFILE', OMB_NAMESPACE.'/updateProfile');
37 define('OMB_ENDPOINT_POSTNOTICE', OMB_NAMESPACE.'/postNotice');
38 define('OAUTH_ENDPOINT_REQUEST', OAUTH_NAMESPACE.'endpoint/request');
39 define('OAUTH_ENDPOINT_AUTHORIZE', OAUTH_NAMESPACE.'endpoint/authorize');
40 define('OAUTH_ENDPOINT_ACCESS', OAUTH_NAMESPACE.'endpoint/access');
41 define('OAUTH_ENDPOINT_RESOURCE', OAUTH_NAMESPACE.'endpoint/resource');
42 define('OAUTH_AUTH_HEADER', OAUTH_NAMESPACE.'parameters/auth-header');
43 define('OAUTH_POST_BODY', OAUTH_NAMESPACE.'parameters/post-body');
44 define('OAUTH_HMAC_SHA1', OAUTH_NAMESPACE.'signature/HMAC-SHA1');
45
46 function omb_oauth_consumer()
47 {
48     static $con = null;
49     if (!$con) {
50         $con = new OAuthConsumer(common_root_url(), '');
51     }
52     return $con;
53 }
54
55 function omb_oauth_server()
56 {
57     static $server = null;
58     if (!$server) {
59         $server = new OAuthServer(omb_oauth_datastore());
60         $server->add_signature_method(omb_hmac_sha1());
61     }
62     return $server;
63 }
64
65 function omb_oauth_datastore()
66 {
67     static $store = null;
68     if (!$store) {
69         $store = new StatusNetOAuthDataStore();
70     }
71     return $store;
72 }
73
74 function omb_hmac_sha1()
75 {
76     static $hmac_method = null;
77     if (!$hmac_method) {
78         $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
79     }
80     return $hmac_method;
81 }
82
83 function omb_get_services($xrd, $type)
84 {
85     return $xrd->services(array(omb_service_filter($type)));
86 }
87
88 function omb_service_filter($type)
89 {
90     return create_function('$s',
91                            'return omb_match_service($s, \''.$type.'\');');
92 }
93
94 function omb_match_service($service, $type)
95 {
96     return in_array($type, $service->getTypes());
97 }
98
99 function omb_service_uri($service)
100 {
101     if (!$service) {
102         return null;
103     }
104     $uris = $service->getURIs();
105     if (!$uris) {
106         return null;
107     }
108     return $uris[0];
109 }
110
111 function omb_local_id($service)
112 {
113     if (!$service) {
114         return null;
115     }
116     $els = $service->getElements('xrd:LocalID');
117     if (!$els) {
118         return null;
119     }
120     $el = $els[0];
121     return $service->parser->content($el);
122 }
123
124 function omb_broadcast_remote_subscribers($notice)
125 {
126
127     # First, get remote users subscribed to this profile
128     $rp = new Remote_profile();
129
130     $rp->query('SELECT postnoticeurl, token, secret ' .
131                'FROM subscription JOIN remote_profile ' .
132                'ON subscription.subscriber = remote_profile.id ' .
133                'WHERE subscription.subscribed = ' . $notice->profile_id . ' ');
134
135     $posted = array();
136
137     while ($rp->fetch()) {
138         if (!$posted[$rp->postnoticeurl]) {
139             common_log(LOG_DEBUG, 'Posting to ' . $rp->postnoticeurl);
140             if (omb_post_notice_keys($notice, $rp->postnoticeurl, $rp->token, $rp->secret)) {
141                 common_log(LOG_DEBUG, 'Finished to ' . $rp->postnoticeurl);
142                 $posted[$rp->postnoticeurl] = true;
143             } else {
144                 common_log(LOG_DEBUG, 'Failed posting to ' . $rp->postnoticeurl);
145             }
146         }
147     }
148
149     $rp->free();
150     unset($rp);
151
152     return true;
153 }
154
155 function omb_post_notice($notice, $remote_profile, $subscription)
156 {
157     return omb_post_notice_keys($notice, $remote_profile->postnoticeurl, $subscription->token, $subscription->secret);
158 }
159
160 function omb_post_notice_keys($notice, $postnoticeurl, $tk, $secret)
161 {
162     $user = User::staticGet('id', $notice->profile_id);
163
164     if (!$user) {
165         return false;
166     }
167
168     $con = omb_oauth_consumer();
169
170     $token = new OAuthToken($tk, $secret);
171
172     $url = $postnoticeurl;
173     $parsed = parse_url($url);
174     $params = array();
175     parse_str($parsed['query'], $params);
176
177     $req = OAuthRequest::from_consumer_and_token($con, $token,
178                                                  'POST', $url, $params);
179
180     $req->set_parameter('omb_version', OMB_VERSION_01);
181     $req->set_parameter('omb_listenee', $user->uri);
182     $req->set_parameter('omb_notice', $notice->uri);
183     $req->set_parameter('omb_notice_content', $notice->content);
184     $req->set_parameter('omb_notice_url', common_local_url('shownotice',
185                                                            array('notice' =>
186                                                                  $notice->id)));
187     $req->set_parameter('omb_notice_license', common_config('license', 'url'));
188
189     $user->free();
190     unset($user);
191
192     $req->sign_request(omb_hmac_sha1(), $con, $token);
193
194     # We re-use this tool's fetcher, since it's pretty good
195
196     $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
197
198     if (!$fetcher) {
199         common_log(LOG_WARNING, 'Failed to initialize Yadis fetcher.', __FILE__);
200         return false;
201     }
202
203     $result = $fetcher->post($req->get_normalized_http_url(),
204                              $req->to_postdata(),
205                              array('User-Agent: StatusNet/' . LACONICA_VERSION));
206
207     if ($result->status == 403) { # not authorized, don't send again
208         common_debug('403 result, deleting subscription', __FILE__);
209         # FIXME: figure out how to delete this
210         # $subscription->delete();
211         return false;
212     } else if ($result->status != 200) {
213         common_debug('Error status '.$result->status, __FILE__);
214         return false;
215     } else { # success!
216         parse_str($result->body, $return);
217         if ($return['omb_version'] == OMB_VERSION_01) {
218             return true;
219         } else {
220             return false;
221         }
222     }
223 }
224
225 function omb_broadcast_profile($profile)
226 {
227     # First, get remote users subscribed to this profile
228     # XXX: use a join here rather than looping through results
229     $sub = new Subscription();
230     $sub->subscribed = $profile->id;
231     if ($sub->find()) {
232         $updated = array();
233         while ($sub->fetch()) {
234             $rp = Remote_profile::staticGet('id', $sub->subscriber);
235             if ($rp) {
236                 if (!array_key_exists($rp->updateprofileurl, $updated)) {
237                     if (omb_update_profile($profile, $rp, $sub)) {
238                         $updated[$rp->updateprofileurl] = true;
239                     }
240                 }
241             }
242         }
243     }
244 }
245
246 function omb_update_profile($profile, $remote_profile, $subscription)
247 {
248     $user = User::staticGet($profile->id);
249     $con = omb_oauth_consumer();
250     $token = new OAuthToken($subscription->token, $subscription->secret);
251     $url = $remote_profile->updateprofileurl;
252     $parsed = parse_url($url);
253     $params = array();
254     parse_str($parsed['query'], $params);
255     $req = OAuthRequest::from_consumer_and_token($con, $token,
256                                                  "POST", $url, $params);
257     $req->set_parameter('omb_version', OMB_VERSION_01);
258     $req->set_parameter('omb_listenee', $user->uri);
259     $req->set_parameter('omb_listenee_profile', common_profile_url($profile->nickname));
260     $req->set_parameter('omb_listenee_nickname', $profile->nickname);
261
262     # We use blanks to force emptying any existing values in these optional fields
263
264     $req->set_parameter('omb_listenee_fullname',
265                         ($profile->fullname) ? $profile->fullname : '');
266     $req->set_parameter('omb_listenee_homepage',
267                         ($profile->homepage) ? $profile->homepage : '');
268     $req->set_parameter('omb_listenee_bio',
269                         ($profile->bio) ? $profile->bio : '');
270     $req->set_parameter('omb_listenee_location',
271                         ($profile->location) ? $profile->location : '');
272
273     $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
274     $req->set_parameter('omb_listenee_avatar',
275                         ($avatar) ? $avatar->url : '');
276
277     $req->sign_request(omb_hmac_sha1(), $con, $token);
278
279     # We re-use this tool's fetcher, since it's pretty good
280
281     $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
282
283     $result = $fetcher->post($req->get_normalized_http_url(),
284                              $req->to_postdata(),
285                              array('User-Agent: StatusNet/' . LACONICA_VERSION));
286
287     if (empty($result) || !$result) {
288         common_debug("Unable to contact " . $req->get_normalized_http_url());
289     } else if ($result->status == 403) { # not authorized, don't send again
290         common_debug('403 result, deleting subscription', __FILE__);
291         $subscription->delete();
292         return false;
293     } else if ($result->status != 200) {
294         common_debug('Error status '.$result->status, __FILE__);
295         return false;
296     } else { # success!
297         parse_str($result->body, $return);
298         if (isset($return['omb_version']) && $return['omb_version'] === OMB_VERSION_01) {
299             return true;
300         } else {
301             return false;
302         }
303     }
304 }