]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OMB/OMBPlugin.php
Squashed commit of the following:
[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::staticGet('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::staticGet('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::staticGet('id', $other_id);
229
230         if (!empty($omb01)) {
231             // TRANS: Client error displayed trying to subscribe to an OMB 0.1 remote profile.
232             throw new ClientException(
233                 _m(
234                     'You cannot subscribe to an OMB 0.1 '
235                     . 'remote profile with this action.'
236                 )
237             );
238             return false;
239         }
240
241     }
242
243     /**
244      * Throw an error if someone tries to tag a remote profile
245      *
246      * @param Profile $tagger_profile   profile of the tagger
247      * @param Profile $tagged_profile   profile of the taggee
248      * @param string $tag
249      *
250      * @return true
251      */
252     function onStartTagProfile($tagger_profile, $tagged_profile, $tag)
253     {
254         // OMB 0.1 doesn't have a mechanism for local-server-
255         // originated tag.
256
257         $omb01 = Remote_profile::staticGet('id', $tagged_profile->id);
258
259         if (!empty($omb01)) {
260             // TRANS: Client error displayed when trying to add an OMB 0.1 remote profile to a list.
261             $this->clientError(
262                 _m(
263                     'You cannot list an OMB 0.1 '
264                    .'remote profile with this action.')
265             );
266         }
267         return false;
268     }
269
270     /**
271      * Check to make sure we're not tryng to untag an OMB profile
272      *
273      * // XXX: Should this ever happen?
274      *
275      * @param Profile_tag $ptag the profile tag
276      */
277     function onUntagProfile($ptag)
278     {
279         // OMB 0.1 doesn't have a mechanism for local-server-
280         // originated tag.
281
282         $omb01 = Remote_profile::staticGet('id', $ptag->tagged);
283
284         if (!empty($omb01)) {
285             // TRANS: Client error displayed when trying to (un)list an OMB 0.1 remote profile.
286             $this->clientError(
287                 _m(
288                     'You cannot (un)list an OMB 0.1 '
289                     . 'remote profile with this action.')
290             );
291             return false;
292         }
293     }
294
295     /**
296      * Remove old OMB subscription tokens
297      *
298      * @param User    $user     subscriber
299      * @param Profile $other    subscribee
300      * @return hook return value
301      */
302     function onEndUnsubscribe($profile, $other)
303     {
304         $sub = Subscription::pkeyGet(
305             array('subscriber' => $subscriber->id, 'subscribed' => $other->id)
306         );
307
308         if (!empty($sub->token)) {
309
310             $token = new Token();
311
312             $token->tok    = $sub->token;
313
314             if ($token->find(true)) {
315
316                 $result = $token->delete();
317
318                 if (!$result) {
319                     common_log_db_error($token, 'DELETE', __FILE__);
320                     // TRANS: Exception thrown when the OMB token for a subscription could not deleted on the server.
321                     throw new Exception(
322                         _m('Could not delete subscription OMB token.')
323                     );
324                 }
325             } else {
326                 common_log(
327                     LOG_ERR,
328                     "Couldn't find credentials with token {$token->tok}",
329                     __FILE__
330                 );
331             }
332         }
333
334         return true;
335     }
336
337     /**
338      * Search for an OMB remote profile by URI
339      *
340      * @param string  $uri      remote profile URI
341      * @param Profile $profile  the profile to set
342      * @return boolean hook value
343      */
344     function onStartGetProfileFromURI($uri, &$profile)
345     {
346         $remote_profile = Remote_profile::staticGet('uri', $uri);
347         if (!empty($remote_profile)) {
348             $profile = Profile::staticGet('id', $remote_profile->profile_id);
349             return false;
350         }
351
352         return true;
353     }
354
355     /**
356      * Return OMB remote profiles as well as regular profiles
357      * in helper
358      *
359      * @param type $profile
360      * @param type $uri
361      */
362     function onStartCommonProfileURI($profile, &$uri)
363     {
364         $remote = Remote_profile::staticGet($profile->id);
365         if ($remote) {
366             $uri = $remote->uri;
367             return false;
368         }
369         return true;
370     }
371
372     /**
373      * Plugin version info
374      *
375      * @param array $versions
376      * @return boolean hook value
377      */
378     function onPluginVersion(&$versions)
379     {
380         $versions[] = array(
381             'name'           => 'OpenMicroBlogging',
382             'version'        => STATUSNET_VERSION,
383             'author'         => 'Zach Copley',
384             'homepage'       => 'http://status.net/wiki/Plugin:Sample',
385             'rawdescription' =>
386             // TRANS: Plugin description.
387             _m('A sample plugin to show basics of development for new hackers.')
388         );
389
390         return true;
391     }
392 }
393