]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OMB/OMBPlugin.php
Tidying up getUser calls to profiles and some events
[quix0rs-gnu-social.git] / plugins / OMB / OMBPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * OpenMicroBlogging plugin - add OpenMicroBloggin 0.1 support to
7  * StatusNet.
8  *
9  * Note: the OpenMicroBlogging protocol has been deprecated in favor of OStatus.
10  * This plugin is provided for backwards compatibility and experimentation.
11  *
12  * Please see the README and the OStatus plugin.
13  *
14  * PHP version 5
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  * @category  Sample
30  * @package   StatusNet
31  * @author    Zach Copley
32  * @copyright 2011 StatusNet, Inc.
33  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
34  * @link      http://status.net/
35  */
36
37 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/');
38
39 if (!defined('STATUSNET')) {
40     // This check helps protect against security problems;
41     // your code file can't be executed directly from the web.
42     exit(1);
43 }
44
45 /**
46  * OMB plugin main class
47  *
48  * @category  Integration
49  * @package   StatusNet
50  * @author    Zach Copley <zach@status.net>
51  * @copyright 2011 StatusNet, Inc.
52  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
53  * @link      http://status.net/
54  */
55 class OMBPlugin extends Plugin
56 {
57
58     /**
59      * Map URLs to actions
60      *
61      * @param Net_URL_Mapper $m path-to-action mapper
62      *
63      * @return boolean hook value; true means continue processing, false means stop.
64      */
65     function onRouterInitialized($m)
66     {
67         $bare = array(
68             'requesttoken',
69             'accesstoken',
70             'userauthorization',
71             'postnotice',
72             'updateprofile',
73             'finishremotesubscribe'
74         );
75
76         foreach ($bare as $action) {
77             $m->connect(
78                 'index.php?action=' . $action, array('action' => $action)
79             );
80         }
81
82         // exceptional
83
84         $m->connect('main/remote', array('action' => 'remotesubscribe'));
85         $m->connect(
86             'main/remote?nickname=:nickname',
87             array('action' => 'remotesubscribe'),
88             array('nickname' => '[A-Za-z0-9_-]+')
89         );
90
91         $m->connect(
92             'xrds',
93             array('action' => 'xrds', 'nickname' => $nickname)
94         );
95
96         return true;
97     }
98
99     /**
100      * Put saved notices into the queue for OMB distribution
101      *
102      * @param Notice $notice     the notice to broadcast
103      * @param array  $transports queuehandler's list of transports
104      * @return boolean true if queing was successful
105      */
106     function onStartEnqueueNotice($notice, &$transports)
107     {
108         if ($notice->isLocal()) {
109             if ($notice->inScope(null)) {
110                 array_unshift($transports, 'omb');
111                 common_log(
112                     LOG_INFO, "Notice {$notice->id} queued for OMB processing"
113                 );
114             } else {
115                 // Note: We don't do privacy-controlled OMB updates.
116                 common_log(
117                     LOG_NOTICE,
118                     "Not queueing notice {$notice->id} for OMB because of "
119                     . "privacy; scope = {$notice->scope}",
120                     __FILE__
121                 );
122             }
123         } else {
124             common_log(
125                 LOG_NOTICE,
126                 "Not queueing notice {$notice->id} for OMB because it's not "
127                 . "local.",
128                 __FILE__
129             );
130         }
131
132         return true;
133     }
134
135     /**
136      * Set up queue handlers for outgoing OMB pushes
137      *
138      * @param QueueManager $qm
139      * @return boolean hook return
140      */
141     function onEndInitializeQueueManager(QueueManager $qm)
142     {
143         // Prepare outgoing distributions after notice save.
144         $qm->connect('omb', 'OmbQueueHandler');
145         $qm->connect('profile', 'ProfileQueueHandler');
146
147         return true;
148     }
149
150     /**
151      * Return OMB remote profile, if any
152      *
153      * @param Profile $profile
154      * @param string  $uri
155      * @return boolen false if there's a remote profile
156      */
157     function onStartGetProfileUri($profile, &$uri)
158     {
159         $remote = Remote_profile::getKV('id', $this->id);
160         if (!empty($remote)) {
161             $uri = $remote->uri;
162             return false;
163         }
164         return true;
165     }
166
167     /**
168      * We can't initiate subscriptions for a remote OMB profile; don't show
169      * subscribe button
170      *
171      * @param type $action
172      */
173     function onStartShowProfileListSubscribeButton($action)
174     {
175         $remote = Remote_profile::getKV('id', $action->profile->id);
176         if (empty($remote)) {
177             false;
178         }
179         return true;
180     }
181
182     /**
183      * Check for illegal subscription attempts
184      *
185      * @param Profile $profile  subscriber
186      * @param Profile $other    subscribee
187      * @return hook return value
188      */
189     function onStartSubscribe(Profile $profile, Profile $other)
190     {
191         // OMB 0.1 doesn't have a mechanism for local-server-
192         // originated subscription.
193
194         $omb01 = Remote_profile::getKV('id', $other->id);
195
196         if (!empty($omb01)) {
197             throw new ClientException(
198                 // TRANS: Client error displayed trying to subscribe to an OMB 0.1 remote profile.
199                 _m('You cannot subscribe to an OMB 0.1 '
200                     . 'remote profile with this action.'
201                 )
202             );
203             return false;
204         }
205     }
206
207     /**
208      * Throw an error if someone tries to tag a remote profile
209      *
210      * @param Profile $tagger_profile   profile of the tagger
211      * @param Profile $tagged_profile   profile of the taggee
212      * @param string $tag
213      *
214      * @return true
215      */
216     function onStartTagProfile($tagger_profile, $tagged_profile, $tag)
217     {
218         // OMB 0.1 doesn't have a mechanism for local-server-
219         // originated tag.
220
221         $omb01 = Remote_profile::getKV('id', $tagged_profile->id);
222
223         if (!empty($omb01)) {
224             $this->clientError(
225                 // TRANS: Client error displayed when trying to add an OMB 0.1 remote profile to a list.
226                 _m('You cannot list an OMB 0.1 '
227                    .'remote profile with this action.')
228             );
229         }
230         return false;
231     }
232
233     /**
234      * Check to make sure we're not tryng to untag an OMB profile
235      *
236      * // XXX: Should this ever happen?
237      *
238      * @param Profile_tag $ptag the profile tag
239      */
240     function onUntagProfile($ptag)
241     {
242         // OMB 0.1 doesn't have a mechanism for local-server-
243         // originated tag.
244
245         $omb01 = Remote_profile::getKV('id', $ptag->tagged);
246
247         if (!empty($omb01)) {
248             $this->clientError(
249                 // TRANS: Client error displayed when trying to (un)list an OMB 0.1 remote profile.
250                 _m('You cannot (un)list an OMB 0.1 '
251                     . 'remote profile with this action.')
252             );
253             return false;
254         }
255     }
256
257     /**
258      * Remove old OMB subscription tokens
259      *
260      * @param Profile $profile  subscriber
261      * @param Profile $other    subscribee
262      * @return hook return value
263      */
264     function onEndUnsubscribe(Profile $profile, Profile $other)
265     {
266         $sub = Subscription::pkeyGet(
267             array('subscriber' => $profile->id, 'subscribed' => $other->id)
268         );
269
270         if (!empty($sub->token)) {
271
272             $token = new Token();
273
274             $token->tok    = $sub->token;
275
276             if ($token->find(true)) {
277
278                 $result = $token->delete();
279
280                 if (!$result) {
281                     common_log_db_error($token, 'DELETE', __FILE__);
282                     throw new Exception(
283                         // TRANS: Exception thrown when the OMB token for a subscription could not deleted on the server.
284                         _m('Could not delete subscription OMB token.')
285                     );
286                 }
287             } else {
288                 common_log(
289                     LOG_ERR,
290                     "Couldn't find credentials with token {$token->tok}",
291                     __FILE__
292                 );
293             }
294         }
295
296         return true;
297     }
298
299     /**
300      * Search for an OMB remote profile by URI
301      *
302      * @param string  $uri      remote profile URI
303      * @param Profile $profile  the profile to set
304      * @return boolean hook value
305      */
306     function onStartGetProfileFromURI($uri, &$profile)
307     {
308         $remote_profile = Remote_profile::getKV('uri', $uri);
309         if (!empty($remote_profile)) {
310             $profile = Profile::getKV('id', $remote_profile->profile_id);
311             return false;
312         }
313
314         return true;
315     }
316
317     /**
318      * Return OMB remote profiles as well as regular profiles
319      * in helper
320      *
321      * @param type $profile
322      * @param type $uri
323      */
324     function onStartCommonProfileURI($profile, &$uri)
325     {
326         $remote = Remote_profile::getKV($profile->id);
327         if ($remote) {
328             $uri = $remote->uri;
329             return false;
330         }
331         return true;
332     }
333
334     /**
335      * Broadcast a profile over OMB
336      *
337      * @param Profile $profile to broadcast
338      * @return false
339      */
340     function onBroadcastProfile($profile) {
341         $qm = QueueManager::get();
342         $qm->enqueue($profile, "profile");
343         return true;
344     }
345
346     function onStartProfileRemoteSubscribe($out, $profile)
347     {
348         $out->elementStart('li', 'entity_subscribe');
349         $url = common_local_url('remotesubscribe',
350                                 array('nickname' => $this->profile->nickname));
351         $out->element('a', array('href' => $url,
352                                   'class' => 'entity_remote_subscribe'),
353                        // TRANS: Link text for link that will subscribe to a remote profile.
354                        _m('BUTTON','Subscribe'));
355         $out->elementEnd('li');
356
357         return false;
358     }
359
360     /**
361      * Plugin version info
362      *
363      * @param array $versions
364      * @return boolean hook value
365      */
366     function onPluginVersion(&$versions)
367     {
368         $versions[] = array(
369             'name'           => 'OpenMicroBlogging',
370             'version'        => STATUSNET_VERSION,
371             'author'         => 'Zach Copley',
372             'homepage'       => 'http://status.net/wiki/Plugin:Sample',
373             // TRANS: Plugin description.
374             'rawdescription' => _m('A sample plugin to show basics of development for new hackers.')
375         );
376
377         return true;
378     }
379 }