]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/omb.php
Merge branch '0.9.x' of git@gitorious.org:laconica/mainline into 0.9.x
[quix0rs-gnu-social.git] / lib / omb.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, 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('LACONICA')) {
21     exit(1);
22 }
23
24 require_once INSTALLDIR.'/lib/oauthstore.php';
25 require_once 'OAuth.php';
26 require_once 'libomb/constants.php';
27 require_once 'libomb/service_consumer.php';
28 require_once 'libomb/notice.php';
29 require_once 'libomb/profile.php';
30 require_once 'Auth/Yadis/Yadis.php';
31
32 function omb_oauth_consumer()
33 {
34     static $con = null;
35     if (is_null($con)) {
36         $con = new OAuthConsumer(common_root_url(), '');
37     }
38     return $con;
39 }
40
41 function omb_oauth_server()
42 {
43     static $server = null;
44     if (is_null($server)) {
45         $server = new OAuthServer(omb_oauth_datastore());
46         $server->add_signature_method(omb_hmac_sha1());
47     }
48     return $server;
49 }
50
51 function omb_oauth_datastore()
52 {
53     static $store = null;
54     if (is_null($store)) {
55         $store = new LaconicaDataStore();
56     }
57     return $store;
58 }
59
60 function omb_hmac_sha1()
61 {
62     static $hmac_method = null;
63     if (is_null($hmac_method)) {
64         $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
65     }
66     return $hmac_method;
67 }
68
69 function omb_broadcast_notice($notice)
70 {
71
72     $omb_notice = notice_to_omb_notice($notice);
73
74     /* Get remote users subscribed to this profile. */
75     $rp = new Remote_profile();
76
77     $rp->query('SELECT postnoticeurl, token, secret ' .
78                'FROM subscription JOIN remote_profile ' .
79                'ON subscription.subscriber = remote_profile.id ' .
80                'WHERE subscription.subscribed = ' . $notice->profile_id . ' ');
81
82     $posted = array();
83
84     while ($rp->fetch()) {
85         if (isset($posted[$rp->postnoticeurl])) {
86             /* We already posted to this url. */
87             continue;
88         }
89         common_debug('Posting to ' . $rp->postnoticeurl, __FILE__);
90
91         /* Post notice. */
92         $service = new Laconica_OMB_Service_Consumer(
93                      array(OMB_ENDPOINT_POSTNOTICE => $rp->postnoticeurl));
94         try {
95             $service->setToken($rp->token, $rp->secret);
96             $service->postNotice($omb_notice);
97         } catch (Exception $e) {
98             common_log(LOG_ERR, 'Failed posting to ' . $rp->postnoticeurl);
99             common_log(LOG_ERR, 'Error status '.$e);
100             continue;
101         }
102         $posted[$rp->postnoticeurl] = true;
103
104         common_debug('Finished to ' . $rp->postnoticeurl, __FILE__);
105     }
106
107     return;
108 }
109
110 function omb_broadcast_profile($profile)
111 {
112     $user = User::staticGet('id', $profile->id);
113
114     if (!$user) {
115         return false;
116     }
117
118     $profile = $user->getProfile();
119
120     $omb_profile = profile_to_omb_profile($user->uri, $profile, true);
121
122     /* Get remote users subscribed to this profile. */
123     $rp = new Remote_profile();
124
125     $rp->query('SELECT updateprofileurl, token, secret ' .
126                'FROM subscription JOIN remote_profile ' .
127                'ON subscription.subscriber = remote_profile.id ' .
128                'WHERE subscription.subscribed = ' . $profile->id . ' ');
129
130     $posted = array();
131
132     while ($rp->fetch()) {
133         if (isset($posted[$rp->updateprofileurl])) {
134             /* We already posted to this url. */
135             continue;
136         }
137         common_debug('Posting to ' . $rp->updateprofileurl, __FILE__);
138
139         /* Update profile. */
140         $service = new Laconica_OMB_Service_Consumer(
141                      array(OMB_ENDPOINT_UPDATEPROFILE => $rp->updateprofileurl));
142         try {
143             $service->setToken($rp->token, $rp->secret);
144             $service->updateProfile($omb_profile);
145         } catch (Exception $e) {
146             common_log(LOG_ERR, 'Failed posting to ' . $rp->updateprofileurl);
147             common_log(LOG_ERR, 'Error status '.$e);
148             continue;
149         }
150         $posted[$rp->updateprofileurl] = true;
151
152         common_debug('Finished to ' . $rp->updateprofileurl, __FILE__);
153     }
154
155     return;
156 }
157
158 class Laconica_OMB_Service_Consumer extends OMB_Service_Consumer {
159     public function __construct($urls)
160     {
161         $this->services       = $urls;
162         $this->datastore      = omb_oauth_datastore();
163         $this->oauth_consumer = omb_oauth_consumer();
164         $this->fetcher        = Auth_Yadis_Yadis::getHTTPFetcher();
165     }
166
167 }
168
169 function profile_to_omb_profile($uri, $profile, $force = false)
170 {
171     $omb_profile = new OMB_Profile($uri);
172     $omb_profile->setNickname($profile->nickname);
173     $omb_profile->setLicenseURL(common_config('license', 'url'));
174     if (!is_null($profile->fullname)) {
175         $omb_profile->setFullname($profile->fullname);
176     } elseif ($force) {
177         $omb_profile->setFullname('');
178     }
179     if (!is_null($profile->homepage)) {
180         $omb_profile->setHomepage($profile->homepage);
181     } elseif ($force) {
182         $omb_profile->setHomepage('');
183     }
184     if (!is_null($profile->bio)) {
185         $omb_profile->setBio($profile->bio);
186     } elseif ($force) {
187         $omb_profile->setBio('');
188     }
189     if (!is_null($profile->location)) {
190         $omb_profile->setLocation($profile->location);
191     } elseif ($force) {
192         $omb_profile->setLocation('');
193     }
194     if (!is_null($profile->profileurl)) {
195         $omb_profile->setProfileURL($profile->profileurl);
196     } elseif ($force) {
197         $omb_profile->setProfileURL('');
198     }
199
200     $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
201     if ($avatar) {
202         $omb_profile->setAvatarURL($avatar->url);
203     } elseif ($force) {
204         $omb_profile->setAvatarURL('');
205     }
206     return $omb_profile;
207 }
208
209 function notice_to_omb_notice($notice)
210 {
211     /* Create an OMB_Notice for $notice. */
212     $user = User::staticGet('id', $notice->profile_id);
213
214     if (!$user) {
215         return null;
216     }
217
218     $profile = $user->getProfile();
219
220     $omb_notice = new OMB_Notice(profile_to_omb_profile($user->uri, $profile),
221                                  $notice->uri,
222                                  $notice->content);
223     $omb_notice->setURL(common_local_url('shownotice', array('notice' =>
224                                                                  $notice->id)));
225     $omb_notice->setLicenseURL(common_config('license', 'url'));
226
227     return $omb_notice;
228 }
229 ?>