]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OMB/OMBPlugin.php
The overloaded DB_DataObject function staticGet is now called getKV
[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      * Load related modules when needed
59      *
60      * @param string $cls Name of the class to be loaded
61      *
62      * @return boolean hook value; true means continue processing, false means stop.
63      */
64     function onAutoload($cls)
65     {
66         $dir = dirname(__FILE__);
67
68         switch ($cls)
69         {
70         case 'Requesttokenaction':
71         case 'Accesstokenaction':
72         case 'Userauthorizationaction':
73         case 'Postnoticeaction':
74         case 'Updateprofileaction':
75         case 'Finishremotesubscribeaction':
76         case 'Remotesubscribeaction':
77         case 'XrdsAction':
78             include_once $dir . '/action/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
79             return false;
80             break;
81         case 'OmbQueueHandler':
82         case 'ProfileQueueHandler':
83             include_once $dir . '/lib/' . strtolower($cls) . '.php';
84             return false;
85         case 'OMBOAuthDataStore':
86             include_once $dir . '/lib/omboauthstore.php';
87         default:
88             return true;
89         }
90     }
91
92     /**
93      * Map URLs to actions
94      *
95      * @param Net_URL_Mapper $m path-to-action mapper
96      *
97      * @return boolean hook value; true means continue processing, false means stop.
98      */
99     function onRouterInitialized($m)
100     {
101         $bare = array(
102             'requesttoken',
103             'accesstoken',
104             'userauthorization',
105             'postnotice',
106             'updateprofile',
107             'finishremotesubscribe'
108         );
109
110         foreach ($bare as $action) {
111             $m->connect(
112                 'index.php?action=' . $action, array('action' => $action)
113             );
114         }
115
116         // exceptional
117
118         $m->connect('main/remote', array('action' => 'remotesubscribe'));
119         $m->connect(
120             'main/remote?nickname=:nickname',
121             array('action' => 'remotesubscribe'),
122             array('nickname' => '[A-Za-z0-9_-]+')
123         );
124
125         $m->connect(
126             'xrds',
127             array('action' => 'xrds', 'nickname' => $nickname)
128         );
129
130         return true;
131     }
132
133     /**
134      * Put saved notices into the queue for OMB distribution
135      *
136      * @param Notice $notice     the notice to broadcast
137      * @param array  $transports queuehandler's list of transports
138      * @return boolean true if queing was successful
139      */
140     function onStartEnqueueNotice($notice, &$transports)
141     {
142         if ($notice->isLocal()) {
143             if ($notice->inScope(null)) {
144                 array_unshift($transports, 'omb');
145                 common_log(
146                     LOG_INFO, "Notice {$notice->id} queued for OMB processing"
147                 );
148             } else {
149                 // Note: We don't do privacy-controlled OMB updates.
150                 common_log(
151                     LOG_NOTICE,
152                     "Not queueing notice {$notice->id} for OMB because of "
153                     . "privacy; scope = {$notice->scope}",
154                     __FILE__
155                 );
156             }
157         } else {
158             common_log(
159                 LOG_NOTICE,
160                 "Not queueing notice {$notice->id} for OMB because it's not "
161                 . "local.",
162                 __FILE__
163             );
164         }
165
166         return true;
167     }
168
169     /**
170      * Set up queue handlers for outgoing OMB pushes
171      *
172      * @param QueueManager $qm
173      * @return boolean hook return
174      */
175     function onEndInitializeQueueManager(QueueManager $qm)
176     {
177         // Prepare outgoing distributions after notice save.
178         $qm->connect('omb', 'OmbQueueHandler');
179         $qm->connect('profile', 'ProfileQueueHandler');
180
181         return true;
182     }
183
184     /**
185      * Return OMB remote profile, if any
186      *
187      * @param Profile $profile
188      * @param string  $uri
189      * @return boolen false if there's a remote profile
190      */
191     function onStartGetProfileUri($profile, &$uri)
192     {
193         $remote = Remote_profile::getKV('id', $this->id);
194         if (!empty($remote)) {
195             $uri = $remote->uri;
196             return false;
197         }
198         return true;
199     }
200
201     /**
202      * We can't initiate subscriptions for a remote OMB profile; don't show
203      * subscribe button
204      *
205      * @param type $action
206      */
207     function onStartShowProfileListSubscribeButton($action)
208     {
209         $remote = Remote_profile::getKV('id', $action->profile->id);
210         if (empty($remote)) {
211             false;
212         }
213         return true;
214     }
215
216     /**
217      * Check for illegal subscription attempts
218      *
219      * @param User    $user     subscriber
220      * @param Profile $other    subscribee
221      * @return hook return value
222      */
223     function onStartSubscribe($profile, $other)
224     {
225         // OMB 0.1 doesn't have a mechanism for local-server-
226         // originated subscription.
227
228         $omb01 = Remote_profile::getKV('id', $other_id);
229
230         if (!empty($omb01)) {
231             throw new ClientException(
232                 // TRANS: Client error displayed trying to subscribe to an OMB 0.1 remote profile.
233                 _m('You cannot subscribe to an OMB 0.1 '
234                     . 'remote profile with this action.'
235                 )
236             );
237             return false;
238         }
239     }
240
241     /**
242      * Throw an error if someone tries to tag a remote profile
243      *
244      * @param Profile $tagger_profile   profile of the tagger
245      * @param Profile $tagged_profile   profile of the taggee
246      * @param string $tag
247      *
248      * @return true
249      */
250     function onStartTagProfile($tagger_profile, $tagged_profile, $tag)
251     {
252         // OMB 0.1 doesn't have a mechanism for local-server-
253         // originated tag.
254
255         $omb01 = Remote_profile::getKV('id', $tagged_profile->id);
256
257         if (!empty($omb01)) {
258             $this->clientError(
259                 // TRANS: Client error displayed when trying to add an OMB 0.1 remote profile to a list.
260                 _m('You cannot list an OMB 0.1 '
261                    .'remote profile with this action.')
262             );
263         }
264         return false;
265     }
266
267     /**
268      * Check to make sure we're not tryng to untag an OMB profile
269      *
270      * // XXX: Should this ever happen?
271      *
272      * @param Profile_tag $ptag the profile tag
273      */
274     function onUntagProfile($ptag)
275     {
276         // OMB 0.1 doesn't have a mechanism for local-server-
277         // originated tag.
278
279         $omb01 = Remote_profile::getKV('id', $ptag->tagged);
280
281         if (!empty($omb01)) {
282             $this->clientError(
283                 // TRANS: Client error displayed when trying to (un)list an OMB 0.1 remote profile.
284                 _m('You cannot (un)list an OMB 0.1 '
285                     . 'remote profile with this action.')
286             );
287             return false;
288         }
289     }
290
291     /**
292      * Remove old OMB subscription tokens
293      *
294      * @param User    $user     subscriber
295      * @param Profile $other    subscribee
296      * @return hook return value
297      */
298     function onEndUnsubscribe($profile, $other)
299     {
300         $sub = Subscription::pkeyGet(
301             array('subscriber' => $subscriber->id, 'subscribed' => $other->id)
302         );
303
304         if (!empty($sub->token)) {
305
306             $token = new Token();
307
308             $token->tok    = $sub->token;
309
310             if ($token->find(true)) {
311
312                 $result = $token->delete();
313
314                 if (!$result) {
315                     common_log_db_error($token, 'DELETE', __FILE__);
316                     throw new Exception(
317                         // TRANS: Exception thrown when the OMB token for a subscription could not deleted on the server.
318                         _m('Could not delete subscription OMB token.')
319                     );
320                 }
321             } else {
322                 common_log(
323                     LOG_ERR,
324                     "Couldn't find credentials with token {$token->tok}",
325                     __FILE__
326                 );
327             }
328         }
329
330         return true;
331     }
332
333     /**
334      * Search for an OMB remote profile by URI
335      *
336      * @param string  $uri      remote profile URI
337      * @param Profile $profile  the profile to set
338      * @return boolean hook value
339      */
340     function onStartGetProfileFromURI($uri, &$profile)
341     {
342         $remote_profile = Remote_profile::getKV('uri', $uri);
343         if (!empty($remote_profile)) {
344             $profile = Profile::getKV('id', $remote_profile->profile_id);
345             return false;
346         }
347
348         return true;
349     }
350
351     /**
352      * Return OMB remote profiles as well as regular profiles
353      * in helper
354      *
355      * @param type $profile
356      * @param type $uri
357      */
358     function onStartCommonProfileURI($profile, &$uri)
359     {
360         $remote = Remote_profile::getKV($profile->id);
361         if ($remote) {
362             $uri = $remote->uri;
363             return false;
364         }
365         return true;
366     }
367
368     /**
369      * Broadcast a profile over OMB
370      *
371      * @param Profile $profile to broadcast
372      * @return false
373      */
374     function onBroadcastProfile($profile) {
375         $qm = QueueManager::get();
376         $qm->enqueue($profile, "profile");
377         return true;
378     }
379
380     function onStartProfileRemoteSubscribe($out, $profile)
381     {
382         $out->elementStart('li', 'entity_subscribe');
383         $url = common_local_url('remotesubscribe',
384                                 array('nickname' => $this->profile->nickname));
385         $out->element('a', array('href' => $url,
386                                   'class' => 'entity_remote_subscribe'),
387                        // TRANS: Link text for link that will subscribe to a remote profile.
388                        _m('BUTTON','Subscribe'));
389         $out->elementEnd('li');
390
391         return false;
392     }
393
394     /**
395      * Plugin version info
396      *
397      * @param array $versions
398      * @return boolean hook value
399      */
400     function onPluginVersion(&$versions)
401     {
402         $versions[] = array(
403             'name'           => 'OpenMicroBlogging',
404             'version'        => STATUSNET_VERSION,
405             'author'         => 'Zach Copley',
406             'homepage'       => 'http://status.net/wiki/Plugin:Sample',
407             // TRANS: Plugin description.
408             'rawdescription' => _m('A sample plugin to show basics of development for new hackers.')
409         );
410
411         return true;
412     }
413 }