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