]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/TwitterBridgePlugin.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into 0.9.x
[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
54     /**
55      * Initializer for the plugin.
56      */
57
58     function initialize()
59     {
60         // Allow the key and secret to be passed in
61         // Control panel will override
62
63         if (isset($this->consumer_key)) {
64             $key = common_config('twitter', 'consumer_key');
65             if (empty($key)) {
66                 Config::save('twitter', 'consumer_key', $this->consumer_key);
67             }
68         }
69
70         if (isset($this->consumer_secret)) {
71             $secret = common_config('twitter', 'consumer_secret');
72             if (empty($secret)) {
73                 Config::save(
74                     'twitter',
75                     'consumer_secret',
76                     $this->consumer_secret
77                 );
78             }
79         }
80     }
81
82     /**
83      * Check to see if there is a consumer key and secret defined
84      * for Twitter integration.
85      *
86      * @return boolean result
87      */
88
89     static function hasKeys()
90     {
91         $ckey    = common_config('twitter', 'consumer_key');
92         $csecret = common_config('twitter', 'consumer_secret');
93
94         if (empty($ckey) && empty($csecret)) {
95             $ckey    = common_config('twitter', 'global_consumer_key');
96             $csecret = common_config('twitter', 'global_consumer_secret');
97         }
98
99         if (!empty($ckey) && !empty($csecret)) {
100             return true;
101         }
102
103         return false;
104     }
105
106     /**
107      * Add Twitter-related paths to the router table
108      *
109      * Hook for RouterInitialized event.
110      *
111      * @param Net_URL_Mapper $m path-to-action mapper
112      *
113      * @return boolean hook return
114      */
115
116     function onRouterInitialized($m)
117     {
118         $m->connect('admin/twitter', array('action' => 'twitteradminpanel'));
119
120         if (self::hasKeys()) {
121             $m->connect(
122                 'twitter/authorization',
123                 array('action' => 'twitterauthorization')
124             );
125             $m->connect(
126                 'settings/twitter', array(
127                     'action' => 'twittersettings'
128                     )
129                 );
130             if (common_config('twitter', 'signin')) {
131                 $m->connect(
132                     'main/twitterlogin',
133                     array('action' => 'twitterlogin')
134                 );
135             }
136         }
137
138         return true;
139     }
140
141     /*
142      * Add a login tab for 'Sign in with Twitter'
143      *
144      * @param Action &action the current action
145      *
146      * @return void
147      */
148     function onEndLoginGroupNav(&$action)
149     {
150         $action_name = $action->trimmed('action');
151
152         if (self::hasKeys() && common_config('twitter', 'signin')) {
153             $action->menuItem(
154                 common_local_url('twitterlogin'),
155                 _m('Twitter'),
156                 _m('Login or register using Twitter'),
157                 'twitterlogin' === $action_name
158             );
159         }
160
161         return true;
162     }
163
164     /**
165      * Add the Twitter Settings page to the Connect Settings menu
166      *
167      * @param Action &$action The calling page
168      *
169      * @return boolean hook return
170      */
171     function onEndConnectSettingsNav(&$action)
172     {
173         if (self::hasKeys()) {
174             $action_name = $action->trimmed('action');
175
176             $action->menuItem(
177                 common_local_url('twittersettings'),
178                 _m('Twitter'),
179                 _m('Twitter integration options'),
180                 $action_name === 'twittersettings'
181             );
182         }
183         return true;
184     }
185
186     /**
187      * Automatically load the actions and libraries used by the Twitter bridge
188      *
189      * @param Class $cls the class
190      *
191      * @return boolean hook return
192      *
193      */
194     function onAutoload($cls)
195     {
196         switch ($cls) {
197         case 'TwittersettingsAction':
198         case 'TwitterauthorizationAction':
199         case 'TwitterloginAction':
200         case 'TwitteradminpanelAction':
201             include_once INSTALLDIR . '/plugins/TwitterBridge/' .
202               strtolower(mb_substr($cls, 0, -6)) . '.php';
203             return false;
204         case 'TwitterOAuthClient':
205         case 'TwitterQueueHandler':
206             include_once INSTALLDIR . '/plugins/TwitterBridge/' .
207               strtolower($cls) . '.php';
208             return false;
209         default:
210             return true;
211         }
212     }
213
214     /**
215      * Add a Twitter queue item for each notice
216      *
217      * @param Notice $notice      the notice
218      * @param array  &$transports the list of transports (queues)
219      *
220      * @return boolean hook return
221      */
222     function onStartEnqueueNotice($notice, &$transports)
223     {
224         if (self::hasKeys()) {
225             // Avoid a possible loop
226             if ($notice->source != 'twitter') {
227                 array_push($transports, 'twitter');
228             }
229         }
230         return true;
231     }
232
233     /**
234      * Add Twitter bridge daemons to the list of daemons to start
235      *
236      * @param array $daemons the list fo daemons to run
237      *
238      * @return boolean hook return
239      */
240     function onGetValidDaemons($daemons)
241     {
242         if (self::hasKeys()) {
243             array_push(
244                 $daemons,
245                 INSTALLDIR
246                 . '/plugins/TwitterBridge/daemons/synctwitterfriends.php'
247             );
248             if (common_config('twitterimport', 'enabled')) {
249                 array_push(
250                     $daemons,
251                     INSTALLDIR
252                     . '/plugins/TwitterBridge/daemons/twitterstatusfetcher.php'
253                     );
254             }
255         }
256
257         return true;
258     }
259
260     /**
261      * Register Twitter notice queue handler
262      *
263      * @param QueueManager $manager
264      *
265      * @return boolean hook return
266      */
267     function onEndInitializeQueueManager($manager)
268     {
269         if (self::hasKeys()) {
270             $manager->connect('twitter', 'TwitterQueueHandler');
271         }
272         return true;
273     }
274
275     /**
276      * Add a Twitter tab to the admin panel
277      *
278      * @param Widget $nav Admin panel nav
279      *
280      * @return boolean hook value
281      */
282
283     function onEndAdminPanelNav($nav)
284     {
285         if (AdminPanelAction::canAdmin('twitter')) {
286
287             $action_name = $nav->action->trimmed('action');
288
289             $nav->out->menuItem(
290                 common_local_url('twitteradminpanel'),
291                 _m('Twitter'),
292                 _m('Twitter bridge configuration'),
293                 $action_name == 'twitteradminpanel',
294                 'nav_twitter_admin_panel'
295             );
296         }
297
298         return true;
299     }
300
301     /**
302      * Plugin version data
303      *
304      * @param array &$versions array of version blocks
305      *
306      * @return boolean hook value
307      */
308
309     function onPluginVersion(&$versions)
310     {
311         $versions[] = array(
312             'name' => 'TwitterBridge',
313             'version' => self::VERSION,
314             'author' => 'Zach Copley, Julien C',
315             'homepage' => 'http://status.net/wiki/Plugin:TwitterBridge',
316             'rawdescription' => _m(
317                 'The Twitter "bridge" plugin allows you to integrate ' .
318                 'your StatusNet instance with ' .
319                 '<a href="http://twitter.com/">Twitter</a>.'
320             )
321         );
322         return true;
323     }
324
325 }
326