]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/omb.php
use a join for omb broadcast
[quix0rs-gnu-social.git] / lib / omb.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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         static $con = NULL;
48         if (!$con) {
49                 $con = new OAuthConsumer(common_root_url(), '');
50         }
51         return $con;
52 }
53
54 function omb_oauth_server() {
55         static $server = null;
56         if (!$server) {
57                 $server = new OAuthServer(omb_oauth_datastore());
58                 $server->add_signature_method(omb_hmac_sha1());
59         }
60         return $server;
61 }
62
63 function omb_oauth_datastore() {
64         static $store = NULL;
65         if (!$store) {
66                 $store = new LaconicaOAuthDataStore();
67         }
68         return $store;
69 }
70
71 function omb_hmac_sha1() {
72         static $hmac_method = NULL;
73         if (!$hmac_method) {
74                 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
75         }
76         return $hmac_method;
77 }
78
79 function omb_get_services($xrd, $type) {
80         return $xrd->services(array(omb_service_filter($type)));
81 }
82
83 function omb_service_filter($type) {
84         return create_function('$s',
85                                                    'return omb_match_service($s, \''.$type.'\');');
86 }
87
88 function omb_match_service($service, $type) {
89         return in_array($type, $service->getTypes());
90 }
91
92 function omb_service_uri($service) {
93         if (!$service) {
94                 return NULL;
95         }
96         $uris = $service->getURIs();
97         if (!$uris) {
98                 return NULL;
99         }
100         return $uris[0];
101 }
102
103 function omb_local_id($service) {
104         if (!$service) {
105                 return NULL;
106         }
107         $els = $service->getElements('xrd:LocalID');
108         if (!$els) {
109                 return NULL;
110         }
111         $el = $els[0];
112         return $service->parser->content($el);
113 }
114
115 function omb_broadcast_remote_subscribers($notice) {
116
117         # First, get remote users subscribed to this profile
118         $rp = new Remote_profile();
119
120         $rp->query('SELECT postnoticeurl, token, secret ' .
121                            'FROM subscription JOIN remote_profile ' .
122                            'ON subscription.subscriber = remote_profile.id ' .
123                            'WHERE subscription.subscribed = ' . $notice->profile_id . ' ');
124
125         $posted = array();
126
127         while ($rp->fetch()) {
128                 if (!$posted[$rp->postnoticeurl]) {
129                         if (omb_post_notice_keys($notice, $rp->postnoticeurl, $rp->token, $rp->secret)) {
130                                 $posted[$rp->postnoticeurl] = TRUE;
131                         }
132                 }
133         }
134
135         return true;
136 }
137
138 function omb_post_notice($notice, $remote_profile, $subscription) {
139         return omb_post_notice_keys($notice, $remote_profile->postnoticeurl, $subscription->token, $subscription->secret);
140 }
141
142 function omb_post_notice_keys($notice, $postnoticeurl, $tk, $secret) {
143
144         $user = User::staticGet('id', $notice->profile_id);
145
146         $con = omb_oauth_consumer();
147         $token = new OAuthToken($tk, $secret);
148         $url = $postnoticeurl;
149         $parsed = parse_url($url);
150         $params = array();
151         parse_str($parsed['query'], $params);
152         $req = OAuthRequest::from_consumer_and_token($con, $token,
153                                                                                                  "POST", $url, $params);
154         $req->set_parameter('omb_version', OMB_VERSION_01);
155         $req->set_parameter('omb_listenee', $user->uri);
156         $req->set_parameter('omb_notice', $notice->uri);
157         $req->set_parameter('omb_notice_content', $notice->content);
158         $req->set_parameter('omb_notice_url', common_local_url('shownotice',
159                                                                                                                    array('notice' =>
160                                                                                                                                  $notice->id)));
161         $req->set_parameter('omb_notice_license', common_config('license', 'url'));
162
163         $req->sign_request(omb_hmac_sha1(), $con, $token);
164
165         # We re-use this tool's fetcher, since it's pretty good
166
167         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
168
169         $result = $fetcher->post($req->get_normalized_http_url(),
170                                                          $req->to_postdata());
171
172         common_debug('Got HTTP result "'.print_r($result,TRUE).'"', __FILE__);
173
174         if ($result->status == 403) { # not authorized, don't send again
175                 common_debug('403 result, deleting subscription', __FILE__);
176                 # FIXME: figure out how to delete this
177                 # $subscription->delete();
178                 return false;
179         } else if ($result->status != 200) {
180                 common_debug('Error status '.$result->status, __FILE__);
181                 return false;
182         } else { # success!
183                 parse_str($result->body, $return);
184                 if ($return['omb_version'] == OMB_VERSION_01) {
185                         return true;
186                 } else {
187                         return false;
188                 }
189         }
190 }
191
192 function omb_broadcast_profile($profile) {
193         # First, get remote users subscribed to this profile
194         # XXX: use a join here rather than looping through results
195         $sub = new Subscription();
196         $sub->subscribed = $profile->id;
197         if ($sub->find()) {
198                 $updated = array();
199                 while ($sub->fetch()) {
200                         $rp = Remote_profile::staticGet('id', $sub->subscriber);
201                         if ($rp) {
202                                 if (!$updated[$rp->updateprofileurl]) {
203                                         if (omb_update_profile($profile, $rp, $sub)) {
204                                                 $updated[$rp->updateprofileurl] = TRUE;
205                                         }
206                                 }
207                         }
208                 }
209         }
210 }
211
212 function omb_update_profile($profile, $remote_profile, $subscription) {
213         global $config; # for license URL
214         $user = User::staticGet($profile->id);
215         $con = omb_oauth_consumer();
216         $token = new OAuthToken($subscription->token, $subscription->secret);
217         $url = $remote_profile->updateprofileurl;
218         $parsed = parse_url($url);
219         $params = array();
220         parse_str($parsed['query'], $params);
221         $req = OAuthRequest::from_consumer_and_token($con, $token,
222                                                                                                  "POST", $url, $params);
223         $req->set_parameter('omb_version', OMB_VERSION_01);
224         $req->set_parameter('omb_listenee', $user->uri);
225         $req->set_parameter('omb_listenee_profile', common_profile_url($profile->nickname));
226         $req->set_parameter('omb_listenee_nickname', $profile->nickname);
227
228         # We use blanks to force emptying any existing values in these optional fields
229
230         $req->set_parameter('omb_listenee_fullname',
231                                                 ($profile->fullname) ? $profile->fullname : '');
232         $req->set_parameter('omb_listenee_homepage',
233                                                 ($profile->homepage) ? $profile->homepage : '');
234         $req->set_parameter('omb_listenee_bio',
235                                                 ($profile->bio) ? $profile->bio : '');
236         $req->set_parameter('omb_listenee_location',
237                                                 ($profile->location) ? $profile->location : '');
238
239         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
240         $req->set_parameter('omb_listenee_avatar',
241                                                 ($avatar) ? $avatar->url : '');
242
243         $req->sign_request(omb_hmac_sha1(), $con, $token);
244
245         # We re-use this tool's fetcher, since it's pretty good
246
247         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
248
249         common_debug('request URL = '.$req->get_normalized_http_url(), __FILE__);
250         common_debug('postdata = '.$req->to_postdata(), __FILE__);
251         $result = $fetcher->post($req->get_normalized_http_url(),
252                                                          $req->to_postdata());
253
254         common_debug('Got HTTP result "'.print_r($result,TRUE).'"', __FILE__);
255
256         if ($result->status == 403) { # not authorized, don't send again
257                 common_debug('403 result, deleting subscription', __FILE__);
258                 $subscription->delete();
259                 return false;
260         } else if ($result->status != 200) {
261                 common_debug('Error status '.$result->status, __FILE__);
262                 return false;
263         } else { # success!
264                 parse_str($result->body, $return);
265                 if ($return['omb_version'] == OMB_VERSION_01) {
266                         return true;
267                 } else {
268                         return false;
269                 }
270         }
271 }