]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OMB/lib/omb.php
4731440a311ee6a51a2697752d75565e26aa02ff
[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 . '/../extlib/libomb/constants.php';
25 require_once $dir . '/../extlib/libomb/service_consumer.php';
26 require_once $dir . '/../extlib/libomb/notice.php';
27 require_once $dir . '/../extlib/libomb/profile.php';
28 require_once INSTALLDIR . '/extlib/Auth/Yadis/Yadis.php';
29
30 function omb_oauth_consumer()
31 {
32     // Don't try to make this static. Leads to issues in
33     // multi-site setups - Z
34     return new OAuthConsumer(common_root_url(), '');
35 }
36
37 function omb_oauth_server()
38 {
39     static $server = null;
40     if (is_null($server)) {
41         $server = new OAuthServer(omb_oauth_datastore());
42         $server->add_signature_method(omb_hmac_sha1());
43     }
44     return $server;
45 }
46
47 function omb_oauth_datastore()
48 {
49     static $store = null;
50     if (is_null($store)) {
51         $store = new OMBOAuthDataStore();
52     }
53     return $store;
54 }
55
56 function omb_hmac_sha1()
57 {
58     static $hmac_method = null;
59     if (is_null($hmac_method)) {
60         $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
61     }
62     return $hmac_method;
63 }
64
65 function omb_broadcast_notice($notice)
66 {
67     try {
68         $omb_notice = notice_to_omb_notice($notice);
69     } catch (Exception $e) {
70         // @fixme we should clean up or highlight the problem item
71         common_log(LOG_ERR, 'Invalid OMB outgoing notice for notice ' . $notice->id);
72         common_log(LOG_ERR, 'Error status '.$e);
73         return true;
74     }
75
76     /* Get remote users subscribed to this profile. */
77     $rp = new Remote_profile();
78
79     $rp->query('SELECT remote_profile.*, secret, token ' .
80                'FROM subscription JOIN remote_profile ' .
81                'ON subscription.subscriber = remote_profile.id ' .
82                'WHERE subscription.subscribed = ' . $notice->profile_id . ' ');
83
84     $posted = array();
85
86     while ($rp->fetch()) {
87         if (isset($posted[$rp->postnoticeurl])) {
88             /* We already posted to this url. */
89             continue;
90         }
91         common_debug('Posting to ' . $rp->postnoticeurl, __FILE__);
92
93         /* Post notice. */
94         $service = new StatusNet_OMB_Service_Consumer(
95                      array(OMB_ENDPOINT_POSTNOTICE => $rp->postnoticeurl),
96                                                       $rp->uri);
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 true;
111 }
112
113 function omb_broadcast_profile($profile)
114 {
115     $user = User::getKV('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 remote_profile.*, secret, token ' .
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                                                       $rp->uri);
146         try {
147             $service->setToken($rp->token, $rp->secret);
148             $service->updateProfile($omb_profile);
149         } catch (Exception $e) {
150             common_log(LOG_ERR, 'Failed posting to ' . $rp->updateprofileurl);
151             common_log(LOG_ERR, 'Error status '.$e);
152             continue;
153         }
154         $posted[$rp->updateprofileurl] = true;
155
156         common_debug('Finished to ' . $rp->updateprofileurl, __FILE__);
157     }
158
159     return;
160 }
161
162 class StatusNet_OMB_Service_Consumer extends OMB_Service_Consumer {
163     public function __construct($urls, $listener_uri=null)
164     {
165         $this->services       = $urls;
166         $this->datastore      = omb_oauth_datastore();
167         $this->oauth_consumer = omb_oauth_consumer();
168         $this->fetcher        = Auth_Yadis_Yadis::getHTTPFetcher();
169         $this->fetcher->timeout = intval(common_config('omb', 'timeout'));
170         $this->listener_uri   = $listener_uri;
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::getKV('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