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