]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/omb.php
Merge remote branch 'laconica/0.8.x' 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     $omb_notice = notice_to_omb_notice($notice);
71
72     /* Get remote users subscribed to this profile. */
73     $rp = new Remote_profile();
74
75     $rp->query('SELECT postnoticeurl, token, secret ' .
76                'FROM subscription JOIN remote_profile ' .
77                'ON subscription.subscriber = remote_profile.id ' .
78                'WHERE subscription.subscribed = ' . $notice->profile_id . ' ');
79
80     $posted = array();
81
82     while ($rp->fetch()) {
83         if (!array_key_exists($rp->postnoticeurl, $posted)) {
84             common_log(LOG_DEBUG, 'Posting to ' . $rp->postnoticeurl);
85             if (omb_post_notice_keys($notice, $rp->postnoticeurl, $rp->token, $rp->secret)) {
86                 common_log(LOG_DEBUG, 'Finished to ' . $rp->postnoticeurl);
87                 $posted[$rp->postnoticeurl] = true;
88             } else {
89                 common_log(LOG_DEBUG, 'Failed posting to ' . $rp->postnoticeurl);
90             }
91         }
92         common_debug('Posting to ' . $rp->postnoticeurl, __FILE__);
93
94         /* Post notice. */
95         $service = new Laconica_OMB_Service_Consumer(
96                      array(OMB_ENDPOINT_POSTNOTICE => $rp->postnoticeurl));
97         try {
98             $service->setToken($rp->token, $rp->secret);
99             $service->postNotice($omb_notice);
100         } catch (Exception $e) {
101             common_log(LOG_ERR, 'Failed posting to ' . $rp->postnoticeurl);
102             common_log(LOG_ERR, 'Error status '.$e);
103             continue;
104         }
105         $posted[$rp->postnoticeurl] = true;
106
107         common_debug('Finished to ' . $rp->postnoticeurl, __FILE__);
108     }
109
110     return;
111 }
112
113 function omb_broadcast_profile($profile)
114 {
115     $user = User::staticGet('id', $profile->id);
116
117     if (!$user) {
118         return false;
119     }
120
121     $profile = $user->getProfile();
122
123     $omb_profile = profile_to_omb_profile($user->uri, $profile, true);
124
125     /* Get remote users subscribed to this profile. */
126     $rp = new Remote_profile();
127
128     $rp->query('SELECT updateprofileurl, token, secret ' .
129                'FROM subscription JOIN remote_profile ' .
130                'ON subscription.subscriber = remote_profile.id ' .
131                'WHERE subscription.subscribed = ' . $profile->id . ' ');
132
133     $posted = array();
134
135     while ($rp->fetch()) {
136         if (isset($posted[$rp->updateprofileurl])) {
137             /* We already posted to this url. */
138             continue;
139         }
140         common_debug('Posting to ' . $rp->updateprofileurl, __FILE__);
141
142         /* Update profile. */
143         $service = new StatusNet_OMB_Service_Consumer(
144                      array(OMB_ENDPOINT_UPDATEPROFILE => $rp->updateprofileurl));
145         try {
146             $service->setToken($rp->token, $rp->secret);
147             $service->updateProfile($omb_profile);
148         } catch (Exception $e) {
149             common_log(LOG_ERR, 'Failed posting to ' . $rp->updateprofileurl);
150             common_log(LOG_ERR, 'Error status '.$e);
151             continue;
152         }
153         $posted[$rp->updateprofileurl] = true;
154
155         common_debug('Finished to ' . $rp->updateprofileurl, __FILE__);
156     }
157
158     return;
159 }
160
161 class StatusNet_OMB_Service_Consumer extends OMB_Service_Consumer {
162     public function __construct($urls)
163     {
164         $this->services       = $urls;
165         $this->datastore      = omb_oauth_datastore();
166         $this->oauth_consumer = omb_oauth_consumer();
167         $this->fetcher        = Auth_Yadis_Yadis::getHTTPFetcher();
168     }
169
170 }
171
172 function profile_to_omb_profile($uri, $profile, $force = false)
173 {
174     $omb_profile = new OMB_Profile($uri);
175     $omb_profile->setNickname($profile->nickname);
176     $omb_profile->setLicenseURL(common_config('license', 'url'));
177     if (!is_null($profile->fullname)) {
178         $omb_profile->setFullname($profile->fullname);
179     } elseif ($force) {
180         $omb_profile->setFullname('');
181     }
182     if (!is_null($profile->homepage)) {
183         $omb_profile->setHomepage($profile->homepage);
184     } elseif ($force) {
185         $omb_profile->setHomepage('');
186     }
187     if (!is_null($profile->bio)) {
188         $omb_profile->setBio($profile->bio);
189     } elseif ($force) {
190         $omb_profile->setBio('');
191     }
192     if (!is_null($profile->location)) {
193         $omb_profile->setLocation($profile->location);
194     } elseif ($force) {
195         $omb_profile->setLocation('');
196     }
197     if (!is_null($profile->profileurl)) {
198         $omb_profile->setProfileURL($profile->profileurl);
199     } elseif ($force) {
200         $omb_profile->setProfileURL('');
201     }
202
203     $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
204     if ($avatar) {
205         $omb_profile->setAvatarURL($avatar->url);
206     } elseif ($force) {
207         $omb_profile->setAvatarURL('');
208     }
209     return $omb_profile;
210 }
211
212 function notice_to_omb_notice($notice)
213 {
214     /* Create an OMB_Notice for $notice. */
215     $user = User::staticGet('id', $notice->profile_id);
216
217     if (!$user) {
218         return null;
219     }
220
221     $profile = $user->getProfile();
222
223     $omb_notice = new OMB_Notice(profile_to_omb_profile($user->uri, $profile),
224                                  $notice->uri,
225                                  $notice->content);
226     $omb_notice->setURL(common_local_url('shownotice', array('notice' =>
227                                                                  $notice->id)));
228     $omb_notice->setLicenseURL(common_config('license', 'url'));
229
230     return $omb_notice;
231 }
232 ?>