3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once('OAuth.php');
23 require_once(INSTALLDIR.'/lib/oauthstore.php');
25 require_once(INSTALLDIR.'/classes/Consumer.php');
26 require_once(INSTALLDIR.'/classes/Nonce.php');
27 require_once(INSTALLDIR.'/classes/Token.php');
29 require_once('Auth/Yadis/Yadis.php');
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');
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');
46 function omb_oauth_consumer()
50 $con = new OAuthConsumer(common_root_url(), '');
55 function omb_oauth_server()
57 static $server = null;
59 $server = new OAuthServer(omb_oauth_datastore());
60 $server->add_signature_method(omb_hmac_sha1());
65 function omb_oauth_datastore()
69 $store = new LaconicaOAuthDataStore();
74 function omb_hmac_sha1()
76 static $hmac_method = null;
78 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
83 function omb_get_services($xrd, $type)
85 return $xrd->services(array(omb_service_filter($type)));
88 function omb_service_filter($type)
90 return create_function('$s',
91 'return omb_match_service($s, \''.$type.'\');');
94 function omb_match_service($service, $type)
96 return in_array($type, $service->getTypes());
99 function omb_service_uri($service)
104 $uris = $service->getURIs();
111 function omb_local_id($service)
116 $els = $service->getElements('xrd:LocalID');
121 return $service->parser->content($el);
124 function omb_broadcast_remote_subscribers($notice)
127 # First, get remote users subscribed to this profile
128 $rp = new Remote_profile();
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 . ' ');
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;
144 common_log(LOG_DEBUG, 'Failed posting to ' . $rp->postnoticeurl);
155 function omb_post_notice($notice, $remote_profile, $subscription)
157 return omb_post_notice_keys($notice, $remote_profile->postnoticeurl, $subscription->token, $subscription->secret);
160 function omb_post_notice_keys($notice, $postnoticeurl, $tk, $secret)
163 common_debug('Posting notice ' . $notice->id . ' to ' . $postnoticeurl, __FILE__);
165 $user = User::staticGet('id', $notice->profile_id);
168 common_debug('Failed to get user for notice ' . $notice->id . ', profile = ' . $notice->profile_id, __FILE__);
172 $con = omb_oauth_consumer();
174 $token = new OAuthToken($tk, $secret);
176 $url = $postnoticeurl;
177 $parsed = parse_url($url);
179 parse_str($parsed['query'], $params);
181 $req = OAuthRequest::from_consumer_and_token($con, $token,
182 'POST', $url, $params);
184 $req->set_parameter('omb_version', OMB_VERSION_01);
185 $req->set_parameter('omb_listenee', $user->uri);
186 $req->set_parameter('omb_notice', $notice->uri);
187 $req->set_parameter('omb_notice_content', $notice->content);
188 $req->set_parameter('omb_notice_url', common_local_url('shownotice',
191 $req->set_parameter('omb_notice_license', common_config('license', 'url'));
196 $req->sign_request(omb_hmac_sha1(), $con, $token);
198 # We re-use this tool's fetcher, since it's pretty good
200 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
203 common_log(LOG_WARNING, 'Failed to initialize Yadis fetcher.', __FILE__);
207 $result = $fetcher->post($req->get_normalized_http_url(),
209 array('User-Agent' => 'Laconica/' . LACONICA_VERSION));
211 common_debug('Got HTTP result "'.print_r($result,true).'"', __FILE__);
213 if ($result->status == 403) { # not authorized, don't send again
214 common_debug('403 result, deleting subscription', __FILE__);
215 # FIXME: figure out how to delete this
216 # $subscription->delete();
218 } else if ($result->status != 200) {
219 common_debug('Error status '.$result->status, __FILE__);
222 parse_str($result->body, $return);
223 if ($return['omb_version'] == OMB_VERSION_01) {
231 function omb_broadcast_profile($profile)
233 # First, get remote users subscribed to this profile
234 # XXX: use a join here rather than looping through results
235 $sub = new Subscription();
236 $sub->subscribed = $profile->id;
239 while ($sub->fetch()) {
240 $rp = Remote_profile::staticGet('id', $sub->subscriber);
242 if (!$updated[$rp->updateprofileurl]) {
243 if (omb_update_profile($profile, $rp, $sub)) {
244 $updated[$rp->updateprofileurl] = true;
252 function omb_update_profile($profile, $remote_profile, $subscription)
254 global $config; # for license URL
255 $user = User::staticGet($profile->id);
256 $con = omb_oauth_consumer();
257 $token = new OAuthToken($subscription->token, $subscription->secret);
258 $url = $remote_profile->updateprofileurl;
259 $parsed = parse_url($url);
261 parse_str($parsed['query'], $params);
262 $req = OAuthRequest::from_consumer_and_token($con, $token,
263 "POST", $url, $params);
264 $req->set_parameter('omb_version', OMB_VERSION_01);
265 $req->set_parameter('omb_listenee', $user->uri);
266 $req->set_parameter('omb_listenee_profile', common_profile_url($profile->nickname));
267 $req->set_parameter('omb_listenee_nickname', $profile->nickname);
269 # We use blanks to force emptying any existing values in these optional fields
271 $req->set_parameter('omb_listenee_fullname',
272 ($profile->fullname) ? $profile->fullname : '');
273 $req->set_parameter('omb_listenee_homepage',
274 ($profile->homepage) ? $profile->homepage : '');
275 $req->set_parameter('omb_listenee_bio',
276 ($profile->bio) ? $profile->bio : '');
277 $req->set_parameter('omb_listenee_location',
278 ($profile->location) ? $profile->location : '');
280 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
281 $req->set_parameter('omb_listenee_avatar',
282 ($avatar) ? $avatar->url : '');
284 $req->sign_request(omb_hmac_sha1(), $con, $token);
286 # We re-use this tool's fetcher, since it's pretty good
288 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
290 common_debug('request URL = '.$req->get_normalized_http_url(), __FILE__);
291 common_debug('postdata = '.$req->to_postdata(), __FILE__);
292 $result = $fetcher->post($req->get_normalized_http_url(),
294 array('User-Agent' => 'Laconica/' . LACONICA_VERSION));
296 common_debug('Got HTTP result "'.print_r($result,true).'"', __FILE__);
298 if ($result->status == 403) { # not authorized, don't send again
299 common_debug('403 result, deleting subscription', __FILE__);
300 $subscription->delete();
302 } else if ($result->status != 200) {
303 common_debug('Error status '.$result->status, __FILE__);
306 parse_str($result->body, $return);
307 if ($return['omb_version'] == OMB_VERSION_01) {