]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OMB/lib/omb.php
0804b65c369ea33c73e50bf919d11626842881d4
[quix0rs-gnu-social.git] / plugins / OMB / lib / omb.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2011, 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 $dir = dirname(__FILE__);
23
24 require_once $dir . '/omboauthstore.php';
25 require_once $dir . '/../extlib/libomb/constants.php';
26 require_once $dir . '/../extlib/libomb/service_consumer.php';
27 require_once $dir . '/../extlib/libomb/notice.php';
28 require_once $dir . '/../extlib/libomb/profile.php';
29 require_once INSTALLDIR . '/extlib/Auth/Yadis/Yadis.php';
30
31 function omb_oauth_consumer()
32 {
33     // Don't try to make this static. Leads to issues in
34     // multi-site setups - Z
35     return new OAuthConsumer(common_root_url(), '');
36 }
37
38 function omb_oauth_server()
39 {
40     static $server = null;
41     if (is_null($server)) {
42         $server = new OAuthServer(omb_oauth_datastore());
43         $server->add_signature_method(omb_hmac_sha1());
44     }
45     return $server;
46 }
47
48 function omb_oauth_datastore()
49 {
50     static $store = null;
51     if (is_null($store)) {
52         $store = new OMBOAuthDataStore();
53     }
54     return $store;
55 }
56
57 function omb_hmac_sha1()
58 {
59     static $hmac_method = null;
60     if (is_null($hmac_method)) {
61         $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
62     }
63     return $hmac_method;
64 }
65
66 function omb_broadcast_notice($notice)
67 {
68     try {
69         $omb_notice = notice_to_omb_notice($notice);
70     } catch (Exception $e) {
71         // @fixme we should clean up or highlight the problem item
72         common_log(LOG_ERR, 'Invalid OMB outgoing notice for notice ' . $notice->id);
73         common_log(LOG_ERR, 'Error status '.$e);
74         return true;
75     }
76
77     /* Get remote users subscribed to this profile. */
78     $rp = new Remote_profile();
79
80     $rp->query('SELECT remote_profile.*, secret, token ' .
81                'FROM subscription JOIN remote_profile ' .
82                'ON subscription.subscriber = remote_profile.id ' .
83                'WHERE subscription.subscribed = ' . $notice->profile_id . ' ');
84
85     $posted = array();
86
87     while ($rp->fetch()) {
88         if (isset($posted[$rp->postnoticeurl])) {
89             /* We already posted to this url. */
90             continue;
91         }
92         common_debug('Posting to ' . $rp->postnoticeurl, __FILE__);
93
94         /* Post notice. */
95         $service = new StatusNet_OMB_Service_Consumer(
96                      array(OMB_ENDPOINT_POSTNOTICE => $rp->postnoticeurl),
97                                                       $rp->uri);
98         try {
99             $service->setToken($rp->token, $rp->secret);
100             $service->postNotice($omb_notice);
101         } catch (Exception $e) {
102             common_log(LOG_ERR, 'Failed posting to ' . $rp->postnoticeurl);
103             common_log(LOG_ERR, 'Error status '.$e);
104             continue;
105         }
106         $posted[$rp->postnoticeurl] = true;
107
108         common_debug('Finished to ' . $rp->postnoticeurl, __FILE__);
109     }
110
111     return true;
112 }
113
114 function omb_broadcast_profile($profile)
115 {
116     $user = User::staticGet('id', $profile->id);
117
118     if (!$user) {
119         return false;
120     }
121
122     $profile = $user->getProfile();
123
124     $omb_profile = profile_to_omb_profile($user->uri, $profile, true);
125
126     /* Get remote users subscribed to this profile. */
127     $rp = new Remote_profile();
128
129     $rp->query('SELECT remote_profile.*, secret, token ' .
130                'FROM subscription JOIN remote_profile ' .
131                'ON subscription.subscriber = remote_profile.id ' .
132                'WHERE subscription.subscribed = ' . $profile->id . ' ');
133
134     $posted = array();
135
136     while ($rp->fetch()) {
137         if (isset($posted[$rp->updateprofileurl])) {
138             /* We already posted to this url. */
139             continue;
140         }
141         common_debug('Posting to ' . $rp->updateprofileurl, __FILE__);
142
143         /* Update profile. */
144         $service = new StatusNet_OMB_Service_Consumer(
145                      array(OMB_ENDPOINT_UPDATEPROFILE => $rp->updateprofileurl),
146                                                       $rp->uri);
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, $listener_uri=null)
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         $this->listener_uri   = $listener_uri;
172     }
173
174 }
175
176 function profile_to_omb_profile($uri, $profile, $force = false)
177 {
178     $omb_profile = new OMB_Profile($uri);
179     $omb_profile->setNickname($profile->nickname);
180     $omb_profile->setLicenseURL(common_config('license', 'url'));
181     if (!is_null($profile->fullname)) {
182         $omb_profile->setFullname($profile->fullname);
183     } elseif ($force) {
184         $omb_profile->setFullname('');
185     }
186     if (!is_null($profile->homepage)) {
187         $omb_profile->setHomepage($profile->homepage);
188     } elseif ($force) {
189         $omb_profile->setHomepage('');
190     }
191     if (!is_null($profile->bio)) {
192         $omb_profile->setBio($profile->bio);
193     } elseif ($force) {
194         $omb_profile->setBio('');
195     }
196     if (!is_null($profile->location)) {
197         $omb_profile->setLocation($profile->location);
198     } elseif ($force) {
199         $omb_profile->setLocation('');
200     }
201     if (!is_null($profile->profileurl)) {
202         $omb_profile->setProfileURL($profile->profileurl);
203     } elseif ($force) {
204         $omb_profile->setProfileURL('');
205     }
206
207     $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
208     if ($avatar) {
209         $omb_profile->setAvatarURL($avatar->url);
210     } elseif ($force) {
211         $omb_profile->setAvatarURL('');
212     }
213     return $omb_profile;
214 }
215
216 function notice_to_omb_notice($notice)
217 {
218     /* Create an OMB_Notice for $notice. */
219     $user = User::staticGet('id', $notice->profile_id);
220
221     if (!$user) {
222         return null;
223     }
224
225     $profile = $user->getProfile();
226
227     $omb_notice = new OMB_Notice(profile_to_omb_profile($user->uri, $profile),
228                                  $notice->uri,
229                                  $notice->content);
230     $omb_notice->setURL(common_local_url('shownotice', array('notice' =>
231                                                                  $notice->id)));
232     $omb_notice->setLicenseURL(common_config('license', 'url'));
233
234     return $omb_notice;
235 }
236