3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/lib/oauthstore.php';
23 require_once 'OAuth.php';
24 require_once 'libomb/constants.php';
25 require_once 'libomb/service_consumer.php';
26 require_once 'libomb/notice.php';
27 require_once 'libomb/profile.php';
28 require_once 'Auth/Yadis/Yadis.php';
30 function omb_oauth_consumer()
34 $con = new OAuthConsumer(common_root_url(), '');
39 function omb_oauth_server()
41 static $server = null;
42 if (is_null($server)) {
43 $server = new OAuthServer(omb_oauth_datastore());
44 $server->add_signature_method(omb_hmac_sha1());
49 function omb_oauth_datastore()
52 if (is_null($store)) {
53 $store = new StatusNetOAuthDataStore();
58 function omb_hmac_sha1()
60 static $hmac_method = null;
61 if (is_null($hmac_method)) {
62 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
67 function omb_broadcast_notice($notice)
71 $omb_notice = notice_to_omb_notice($notice);
72 } catch (Exception $e) {
73 // @fixme we should clean up or highlight the problem item
74 common_log(LOG_ERR, 'Invalid OMB outgoing notice for notice ' . $notice->id);
75 common_log(LOG_ERR, 'Error status '.$e);
79 /* Get remote users subscribed to this profile. */
80 $rp = new Remote_profile();
82 $rp->query('SELECT postnoticeurl, token, secret ' .
83 'FROM subscription JOIN remote_profile ' .
84 'ON subscription.subscriber = remote_profile.id ' .
85 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ');
89 while ($rp->fetch()) {
90 if (isset($posted[$rp->postnoticeurl])) {
91 /* We already posted to this url. */
94 common_debug('Posting to ' . $rp->postnoticeurl, __FILE__);
97 $service = new StatusNet_OMB_Service_Consumer(
98 array(OMB_ENDPOINT_POSTNOTICE => $rp->postnoticeurl));
100 $service->setToken($rp->token, $rp->secret);
101 $service->postNotice($omb_notice);
102 } catch (Exception $e) {
103 common_log(LOG_ERR, 'Failed posting to ' . $rp->postnoticeurl);
104 common_log(LOG_ERR, 'Error status '.$e);
107 $posted[$rp->postnoticeurl] = true;
109 common_debug('Finished to ' . $rp->postnoticeurl, __FILE__);
115 function omb_broadcast_profile($profile)
117 $user = User::staticGet('id', $profile->id);
123 $profile = $user->getProfile();
125 $omb_profile = profile_to_omb_profile($user->uri, $profile, true);
127 /* Get remote users subscribed to this profile. */
128 $rp = new Remote_profile();
130 $rp->query('SELECT updateprofileurl, token, secret ' .
131 'FROM subscription JOIN remote_profile ' .
132 'ON subscription.subscriber = remote_profile.id ' .
133 'WHERE subscription.subscribed = ' . $profile->id . ' ');
137 while ($rp->fetch()) {
138 if (isset($posted[$rp->updateprofileurl])) {
139 /* We already posted to this url. */
142 common_debug('Posting to ' . $rp->updateprofileurl, __FILE__);
144 /* Update profile. */
145 $service = new StatusNet_OMB_Service_Consumer(
146 array(OMB_ENDPOINT_UPDATEPROFILE => $rp->updateprofileurl));
148 $service->setToken($rp->token, $rp->secret);
149 $service->updateProfile($omb_profile);
150 } catch (Exception $e) {
151 common_log(LOG_ERR, 'Failed posting to ' . $rp->updateprofileurl);
152 common_log(LOG_ERR, 'Error status '.$e);
155 $posted[$rp->updateprofileurl] = true;
157 common_debug('Finished to ' . $rp->updateprofileurl, __FILE__);
163 class StatusNet_OMB_Service_Consumer extends OMB_Service_Consumer {
164 public function __construct($urls)
166 $this->services = $urls;
167 $this->datastore = omb_oauth_datastore();
168 $this->oauth_consumer = omb_oauth_consumer();
169 $this->fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
174 function profile_to_omb_profile($uri, $profile, $force = false)
176 $omb_profile = new OMB_Profile($uri);
177 $omb_profile->setNickname($profile->nickname);
178 $omb_profile->setLicenseURL(common_config('license', 'url'));
179 if (!is_null($profile->fullname)) {
180 $omb_profile->setFullname($profile->fullname);
182 $omb_profile->setFullname('');
184 if (!is_null($profile->homepage)) {
185 $omb_profile->setHomepage($profile->homepage);
187 $omb_profile->setHomepage('');
189 if (!is_null($profile->bio)) {
190 $omb_profile->setBio($profile->bio);
192 $omb_profile->setBio('');
194 if (!is_null($profile->location)) {
195 $omb_profile->setLocation($profile->location);
197 $omb_profile->setLocation('');
199 if (!is_null($profile->profileurl)) {
200 $omb_profile->setProfileURL($profile->profileurl);
202 $omb_profile->setProfileURL('');
205 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
207 $omb_profile->setAvatarURL($avatar->url);
209 $omb_profile->setAvatarURL('');
214 function notice_to_omb_notice($notice)
216 /* Create an OMB_Notice for $notice. */
217 $user = User::staticGet('id', $notice->profile_id);
223 $profile = $user->getProfile();
225 $omb_notice = new OMB_Notice(profile_to_omb_profile($user->uri, $profile),
228 $omb_notice->setURL(common_local_url('shownotice', array('notice' =>
230 $omb_notice->setLicenseURL(common_config('license', 'url'));