]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/omb.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into 0.9.x
[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('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
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';
29
30 function omb_oauth_consumer()
31 {
32     static $con = null;
33     if (is_null($con)) {
34         $con = new OAuthConsumer(common_root_url(), '');
35     }
36     return $con;
37 }
38
39 function omb_oauth_server()
40 {
41     static $server = null;
42     if (is_null($server)) {
43         $server = new OAuthServer(omb_oauth_datastore());
44         $server->add_signature_method(omb_hmac_sha1());
45     }
46     return $server;
47 }
48
49 function omb_oauth_datastore()
50 {
51     static $store = null;
52     if (is_null($store)) {
53         $store = new StatusNetOAuthDataStore();
54     }
55     return $store;
56 }
57
58 function omb_hmac_sha1()
59 {
60     static $hmac_method = null;
61     if (is_null($hmac_method)) {
62         $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
63     }
64     return $hmac_method;
65 }
66
67 function omb_broadcast_notice($notice)
68 {
69
70     try {
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);
76         return true;
77     }
78
79     /* Get remote users subscribed to this profile. */
80     $rp = new Remote_profile();
81
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 . ' ');
86
87     $posted = array();
88
89     while ($rp->fetch()) {
90         if (isset($posted[$rp->postnoticeurl])) {
91             /* We already posted to this url. */
92             continue;
93         }
94         common_debug('Posting to ' . $rp->postnoticeurl, __FILE__);
95
96         /* Post notice. */
97         $service = new StatusNet_OMB_Service_Consumer(
98                      array(OMB_ENDPOINT_POSTNOTICE => $rp->postnoticeurl));
99         try {
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);
105             continue;
106         }
107         $posted[$rp->postnoticeurl] = true;
108
109         common_debug('Finished to ' . $rp->postnoticeurl, __FILE__);
110     }
111
112     return true;
113 }
114
115 function omb_broadcast_profile($profile)
116 {
117     $user = User::staticGet('id', $profile->id);
118
119     if (!$user) {
120         return false;
121     }
122
123     $profile = $user->getProfile();
124
125     $omb_profile = profile_to_omb_profile($user->uri, $profile, true);
126
127     /* Get remote users subscribed to this profile. */
128     $rp = new Remote_profile();
129
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 . ' ');
134
135     $posted = array();
136
137     while ($rp->fetch()) {
138         if (isset($posted[$rp->updateprofileurl])) {
139             /* We already posted to this url. */
140             continue;
141         }
142         common_debug('Posting to ' . $rp->updateprofileurl, __FILE__);
143
144         /* Update profile. */
145         $service = new StatusNet_OMB_Service_Consumer(
146                      array(OMB_ENDPOINT_UPDATEPROFILE => $rp->updateprofileurl));
147         try {
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);
153             continue;
154         }
155         $posted[$rp->updateprofileurl] = true;
156
157         common_debug('Finished to ' . $rp->updateprofileurl, __FILE__);
158     }
159
160     return;
161 }
162
163 class StatusNet_OMB_Service_Consumer extends OMB_Service_Consumer {
164     public function __construct($urls)
165     {
166         $this->services       = $urls;
167         $this->datastore      = omb_oauth_datastore();
168         $this->oauth_consumer = omb_oauth_consumer();
169         $this->fetcher        = Auth_Yadis_Yadis::getHTTPFetcher();
170         $this->fetcher->timeout = intval(common_config('omb', 'timeout'));
171     }
172
173 }
174
175 function profile_to_omb_profile($uri, $profile, $force = false)
176 {
177     $omb_profile = new OMB_Profile($uri);
178     $omb_profile->setNickname($profile->nickname);
179     $omb_profile->setLicenseURL(common_config('license', 'url'));
180     if (!is_null($profile->fullname)) {
181         $omb_profile->setFullname($profile->fullname);
182     } elseif ($force) {
183         $omb_profile->setFullname('');
184     }
185     if (!is_null($profile->homepage)) {
186         $omb_profile->setHomepage($profile->homepage);
187     } elseif ($force) {
188         $omb_profile->setHomepage('');
189     }
190     if (!is_null($profile->bio)) {
191         $omb_profile->setBio($profile->bio);
192     } elseif ($force) {
193         $omb_profile->setBio('');
194     }
195     if (!is_null($profile->location)) {
196         $omb_profile->setLocation($profile->location);
197     } elseif ($force) {
198         $omb_profile->setLocation('');
199     }
200     if (!is_null($profile->profileurl)) {
201         $omb_profile->setProfileURL($profile->profileurl);
202     } elseif ($force) {
203         $omb_profile->setProfileURL('');
204     }
205
206     $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
207     if ($avatar) {
208         $omb_profile->setAvatarURL($avatar->url);
209     } elseif ($force) {
210         $omb_profile->setAvatarURL('');
211     }
212     return $omb_profile;
213 }
214
215 function notice_to_omb_notice($notice)
216 {
217     /* Create an OMB_Notice for $notice. */
218     $user = User::staticGet('id', $notice->profile_id);
219
220     if (!$user) {
221         return null;
222     }
223
224     $profile = $user->getProfile();
225
226     $omb_notice = new OMB_Notice(profile_to_omb_profile($user->uri, $profile),
227                                  $notice->uri,
228                                  $notice->content);
229     $omb_notice->setURL(common_local_url('shownotice', array('notice' =>
230                                                                  $notice->id)));
231     $omb_notice->setLicenseURL(common_config('license', 'url'));
232
233     return $omb_notice;
234 }
235 ?>