]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/TwitterBridgePlugin.php
Revert "debugging replyToID"
[quix0rs-gnu-social.git] / plugins / TwitterBridge / TwitterBridgePlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * PHP version 5
6  *
7  * LICENCE: This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @category  Plugin
21  * @package   StatusNet
22  * @author    Zach Copley <zach@status.net>
23  * @author    Julien C <chaumond@gmail.com>
24  * @copyright 2009-2010 Control Yourself, Inc.
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link      http://status.net/
27  */
28
29 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
34
35 /**
36  * Plugin for sending and importing Twitter statuses
37  *
38  * This class allows users to link their Twitter accounts
39  *
40  * @category Plugin
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @author   Julien C <chaumond@gmail.com>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  * @link     http://twitter.com/
47  */
48
49 class TwitterBridgePlugin extends Plugin
50 {
51
52     const VERSION = STATUSNET_VERSION;
53     public $adminImportControl = false; // Should the 'import' checkbox be exposed in the admin panel?
54
55     /**
56      * Initializer for the plugin.
57      */
58
59     function initialize()
60     {
61         // Allow the key and secret to be passed in
62         // Control panel will override
63
64         if (isset($this->consumer_key)) {
65             $key = common_config('twitter', 'consumer_key');
66             if (empty($key)) {
67                 Config::save('twitter', 'consumer_key', $this->consumer_key);
68             }
69         }
70
71         if (isset($this->consumer_secret)) {
72             $secret = common_config('twitter', 'consumer_secret');
73             if (empty($secret)) {
74                 Config::save(
75                     'twitter',
76                     'consumer_secret',
77                     $this->consumer_secret
78                 );
79             }
80         }
81     }
82
83     /**
84      * Check to see if there is a consumer key and secret defined
85      * for Twitter integration.
86      *
87      * @return boolean result
88      */
89
90     static function hasKeys()
91     {
92         $ckey    = common_config('twitter', 'consumer_key');
93         $csecret = common_config('twitter', 'consumer_secret');
94
95         if (empty($ckey) && empty($csecret)) {
96             $ckey    = common_config('twitter', 'global_consumer_key');
97             $csecret = common_config('twitter', 'global_consumer_secret');
98         }
99
100         if (!empty($ckey) && !empty($csecret)) {
101             return true;
102         }
103
104         return false;
105     }
106
107     /**
108      * Add Twitter-related paths to the router table
109      *
110      * Hook for RouterInitialized event.
111      *
112      * @param Net_URL_Mapper $m path-to-action mapper
113      *
114      * @return boolean hook return
115      */
116
117     function onRouterInitialized($m)
118     {
119         $m->connect('admin/twitter', array('action' => 'twitteradminpanel'));
120
121         if (self::hasKeys()) {
122             $m->connect(
123                 'twitter/authorization',
124                 array('action' => 'twitterauthorization')
125             );
126             $m->connect(
127                 'settings/twitter', array(
128                     'action' => 'twittersettings'
129                     )
130                 );
131             if (common_config('twitter', 'signin')) {
132                 $m->connect(
133                     'main/twitterlogin',
134                     array('action' => 'twitterlogin')
135                 );
136             }
137         }
138
139         return true;
140     }
141
142     /*
143      * Add a login tab for 'Sign in with Twitter'
144      *
145      * @param Action &action the current action
146      *
147      * @return void
148      */
149     function onEndLoginGroupNav(&$action)
150     {
151         $action_name = $action->trimmed('action');
152
153         if (self::hasKeys() && common_config('twitter', 'signin')) {
154             $action->menuItem(
155                 common_local_url('twitterlogin'),
156                 _m('Twitter'),
157                 _m('Login or register using Twitter'),
158                 'twitterlogin' === $action_name
159             );
160         }
161
162         return true;
163     }
164
165     /**
166      * Add the Twitter Settings page to the Connect Settings menu
167      *
168      * @param Action &$action The calling page
169      *
170      * @return boolean hook return
171      */
172     function onEndConnectSettingsNav(&$action)
173     {
174         if (self::hasKeys()) {
175             $action_name = $action->trimmed('action');
176
177             $action->menuItem(
178                 common_local_url('twittersettings'),
179                 _m('Twitter'),
180                 _m('Twitter integration options'),
181                 $action_name === 'twittersettings'
182             );
183         }
184         return true;
185     }
186
187     /**
188      * Automatically load the actions and libraries used by the Twitter bridge
189      *
190      * @param Class $cls the class
191      *
192      * @return boolean hook return
193      *
194      */
195     function onAutoload($cls)
196     {
197         switch ($cls) {
198         case 'TwittersettingsAction':
199         case 'TwitterauthorizationAction':
200         case 'TwitterloginAction':
201         case 'TwitteradminpanelAction':
202             include_once INSTALLDIR . '/plugins/TwitterBridge/' .
203               strtolower(mb_substr($cls, 0, -6)) . '.php';
204             return false;
205         case 'TwitterOAuthClient':
206         case 'TwitterQueueHandler':
207             include_once INSTALLDIR . '/plugins/TwitterBridge/' .
208               strtolower($cls) . '.php';
209             return false;
210         default:
211             return true;
212         }
213     }
214
215     /**
216      * Add a Twitter queue item for each notice
217      *
218      * @param Notice $notice      the notice
219      * @param array  &$transports the list of transports (queues)
220      *
221      * @return boolean hook return
222      */
223     function onStartEnqueueNotice($notice, &$transports)
224     {
225         if (self::hasKeys() && $notice->isLocal()) {
226             // Avoid a possible loop
227             if ($notice->source != 'twitter') {
228                 array_push($transports, 'twitter');
229             }
230         }
231         return true;
232     }
233
234     /**
235      * Add Twitter bridge daemons to the list of daemons to start
236      *
237      * @param array $daemons the list fo daemons to run
238      *
239      * @return boolean hook return
240      */
241     function onGetValidDaemons($daemons)
242     {
243         if (self::hasKeys()) {
244             array_push(
245                 $daemons,
246                 INSTALLDIR
247                 . '/plugins/TwitterBridge/daemons/synctwitterfriends.php'
248             );
249             if (common_config('twitterimport', 'enabled')) {
250                 array_push(
251                     $daemons,
252                     INSTALLDIR
253                     . '/plugins/TwitterBridge/daemons/twitterstatusfetcher.php'
254                     );
255             }
256         }
257
258         return true;
259     }
260
261     /**
262      * Register Twitter notice queue handler
263      *
264      * @param QueueManager $manager
265      *
266      * @return boolean hook return
267      */
268     function onEndInitializeQueueManager($manager)
269     {
270         if (self::hasKeys()) {
271             $manager->connect('twitter', 'TwitterQueueHandler');
272         }
273         return true;
274     }
275
276     /**
277      * Add a Twitter tab to the admin panel
278      *
279      * @param Widget $nav Admin panel nav
280      *
281      * @return boolean hook value
282      */
283
284     function onEndAdminPanelNav($nav)
285     {
286         if (AdminPanelAction::canAdmin('twitter')) {
287
288             $action_name = $nav->action->trimmed('action');
289
290             $nav->out->menuItem(
291                 common_local_url('twitteradminpanel'),
292                 _m('Twitter'),
293                 _m('Twitter bridge configuration'),
294                 $action_name == 'twitteradminpanel',
295                 'nav_twitter_admin_panel'
296             );
297         }
298
299         return true;
300     }
301
302     /**
303      * Plugin version data
304      *
305      * @param array &$versions array of version blocks
306      *
307      * @return boolean hook value
308      */
309
310     function onPluginVersion(&$versions)
311     {
312         $versions[] = array(
313             'name' => 'TwitterBridge',
314             'version' => self::VERSION,
315             'author' => 'Zach Copley, Julien C',
316             'homepage' => 'http://status.net/wiki/Plugin:TwitterBridge',
317             'rawdescription' => _m(
318                 'The Twitter "bridge" plugin allows you to integrate ' .
319                 'your StatusNet instance with ' .
320                 '<a href="http://twitter.com/">Twitter</a>.'
321             )
322         );
323         return true;
324     }
325
326     /**
327      * Expose the adminImportControl setting to the administration panel code.
328      * This allows us to disable the import bridge enabling checkbox for administrators,
329      * since on a bulk farm site we can't yet automate the import daemon setup.
330      *
331      * @return boolean hook value;
332      */
333     function onTwitterBridgeAdminImportControl()
334     {
335         return (bool)$this->adminImportControl;
336     }
337
338 }
339