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