3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
29 if (!defined('STATUSNET')) {
33 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
36 * Plugin for sending and importing Twitter statuses
38 * This class allows users to link their Twitter accounts
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/
49 class TwitterBridgePlugin extends Plugin
52 const VERSION = STATUSNET_VERSION;
53 public $adminImportControl = false; // Should the 'import' checkbox be exposed in the admin panel?
56 * Initializer for the plugin.
61 // Allow the key and secret to be passed in
62 // Control panel will override
64 if (isset($this->consumer_key)) {
65 $key = common_config('twitter', 'consumer_key');
67 Config::save('twitter', 'consumer_key', $this->consumer_key);
71 if (isset($this->consumer_secret)) {
72 $secret = common_config('twitter', 'consumer_secret');
77 $this->consumer_secret
84 * Check to see if there is a consumer key and secret defined
85 * for Twitter integration.
87 * @return boolean result
90 static function hasKeys()
92 $ckey = common_config('twitter', 'consumer_key');
93 $csecret = common_config('twitter', 'consumer_secret');
95 if (empty($ckey) && empty($csecret)) {
96 $ckey = common_config('twitter', 'global_consumer_key');
97 $csecret = common_config('twitter', 'global_consumer_secret');
100 if (!empty($ckey) && !empty($csecret)) {
108 * Add Twitter-related paths to the router table
110 * Hook for RouterInitialized event.
112 * @param Net_URL_Mapper $m path-to-action mapper
114 * @return boolean hook return
117 function onRouterInitialized($m)
119 $m->connect('admin/twitter', array('action' => 'twitteradminpanel'));
121 if (self::hasKeys()) {
123 'twitter/authorization',
124 array('action' => 'twitterauthorization')
127 'settings/twitter', array(
128 'action' => 'twittersettings'
131 if (common_config('twitter', 'signin')) {
134 array('action' => 'twitterlogin')
143 * Add a login tab for 'Sign in with Twitter'
145 * @param Action &action the current action
149 function onEndLoginGroupNav(&$action)
151 $action_name = $action->trimmed('action');
153 if (self::hasKeys() && common_config('twitter', 'signin')) {
155 common_local_url('twitterlogin'),
157 _m('Login or register using Twitter'),
158 'twitterlogin' === $action_name
166 * Add the Twitter Settings page to the Connect Settings menu
168 * @param Action &$action The calling page
170 * @return boolean hook return
172 function onEndConnectSettingsNav(&$action)
174 if (self::hasKeys()) {
175 $action_name = $action->trimmed('action');
178 common_local_url('twittersettings'),
180 _m('Twitter integration options'),
181 $action_name === 'twittersettings'
188 * Automatically load the actions and libraries used by the Twitter bridge
190 * @param Class $cls the class
192 * @return boolean hook return
195 function onAutoload($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';
205 case 'TwitterOAuthClient':
206 case 'TwitterQueueHandler':
207 include_once INSTALLDIR . '/plugins/TwitterBridge/' .
208 strtolower($cls) . '.php';
216 * Add a Twitter queue item for each notice
218 * @param Notice $notice the notice
219 * @param array &$transports the list of transports (queues)
221 * @return boolean hook return
223 function onStartEnqueueNotice($notice, &$transports)
225 if (self::hasKeys() && $notice->isLocal()) {
226 // Avoid a possible loop
227 if ($notice->source != 'twitter') {
228 array_push($transports, 'twitter');
235 * Add Twitter bridge daemons to the list of daemons to start
237 * @param array $daemons the list fo daemons to run
239 * @return boolean hook return
241 function onGetValidDaemons($daemons)
243 if (self::hasKeys()) {
247 . '/plugins/TwitterBridge/daemons/synctwitterfriends.php'
249 if (common_config('twitterimport', 'enabled')) {
253 . '/plugins/TwitterBridge/daemons/twitterstatusfetcher.php'
262 * Register Twitter notice queue handler
264 * @param QueueManager $manager
266 * @return boolean hook return
268 function onEndInitializeQueueManager($manager)
270 if (self::hasKeys()) {
271 $manager->connect('twitter', 'TwitterQueueHandler');
277 * Add a Twitter tab to the admin panel
279 * @param Widget $nav Admin panel nav
281 * @return boolean hook value
284 function onEndAdminPanelNav($nav)
286 if (AdminPanelAction::canAdmin('twitter')) {
288 $action_name = $nav->action->trimmed('action');
291 common_local_url('twitteradminpanel'),
293 _m('Twitter bridge configuration'),
294 $action_name == 'twitteradminpanel',
295 'nav_twitter_admin_panel'
303 * Plugin version data
305 * @param array &$versions array of version blocks
307 * @return boolean hook value
310 function onPluginVersion(&$versions)
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>.'
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.
331 * @return boolean hook value;
333 function onTwitterBridgeAdminImportControl()
335 return (bool)$this->adminImportControl;