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/
48 class TwitterBridgePlugin extends Plugin
50 const VERSION = STATUSNET_VERSION;
51 public $adminImportControl = false; // Should the 'import' checkbox be exposed in the admin panel?
54 * Initializer for the plugin.
58 // Allow the key and secret to be passed in
59 // Control panel will override
61 if (isset($this->consumer_key)) {
62 $key = common_config('twitter', 'consumer_key');
64 Config::save('twitter', 'consumer_key', $this->consumer_key);
68 if (isset($this->consumer_secret)) {
69 $secret = common_config('twitter', 'consumer_secret');
74 $this->consumer_secret
81 * Check to see if there is a consumer key and secret defined
82 * for Twitter integration.
84 * @return boolean result
86 static function hasKeys()
88 $ckey = common_config('twitter', 'consumer_key');
89 $csecret = common_config('twitter', 'consumer_secret');
91 if (empty($ckey) && empty($csecret)) {
92 $ckey = common_config('twitter', 'global_consumer_key');
93 $csecret = common_config('twitter', 'global_consumer_secret');
96 if (!empty($ckey) && !empty($csecret)) {
104 * Add Twitter-related paths to the router table
106 * Hook for RouterInitialized event.
108 * @param Net_URL_Mapper $m path-to-action mapper
110 * @return boolean hook return
112 function onRouterInitialized($m)
114 $m->connect('admin/twitter', array('action' => 'twitteradminpanel'));
116 if (self::hasKeys()) {
118 'twitter/authorization',
119 array('action' => 'twitterauthorization')
122 'settings/twitter', array(
123 'action' => 'twittersettings'
126 if (common_config('twitter', 'signin')) {
129 array('action' => 'twitterlogin')
138 * Add a login tab for 'Sign in with Twitter'
140 * @param Action &action the current action
144 function onEndLoginGroupNav(&$action)
146 $action_name = $action->trimmed('action');
148 if (self::hasKeys() && common_config('twitter', 'signin')) {
150 common_local_url('twitterlogin'),
152 _m('Login or register using Twitter'),
153 'twitterlogin' === $action_name
161 * Add the Twitter Settings page to the Connect Settings menu
163 * @param Action &$action The calling page
165 * @return boolean hook return
167 function onEndConnectSettingsNav(&$action)
169 if (self::hasKeys()) {
170 $action_name = $action->trimmed('action');
173 common_local_url('twittersettings'),
175 _m('Twitter integration options'),
176 $action_name === 'twittersettings'
183 * Automatically load the actions and libraries used by the Twitter bridge
185 * @param Class $cls the class
187 * @return boolean hook return
190 function onAutoload($cls)
192 $dir = dirname(__FILE__);
195 case 'TwittersettingsAction':
196 case 'TwitterauthorizationAction':
197 case 'TwitterloginAction':
198 case 'TwitteradminpanelAction':
199 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
201 case 'TwitterOAuthClient':
202 case 'TwitterQueueHandler':
203 case 'TwitterImport':
204 case 'JsonStreamReader':
205 case 'TwitterStreamReader':
206 include_once $dir . '/' . strtolower($cls) . '.php';
208 case 'TwitterSiteStream':
209 case 'TwitterUserStream':
210 include_once $dir . '/twitterstreamreader.php';
212 case 'Notice_to_status':
213 case 'Twitter_synch_status':
214 include_once $dir . '/' . $cls . '.php';
222 * Add a Twitter queue item for each notice
224 * @param Notice $notice the notice
225 * @param array &$transports the list of transports (queues)
227 * @return boolean hook return
229 function onStartEnqueueNotice($notice, &$transports)
231 if (self::hasKeys() && $notice->isLocal()) {
232 // Avoid a possible loop
233 if ($notice->source != 'twitter') {
234 array_push($transports, 'twitter');
241 * Add Twitter bridge daemons to the list of daemons to start
243 * @param array $daemons the list fo daemons to run
245 * @return boolean hook return
247 function onGetValidDaemons($daemons)
249 if (self::hasKeys()) {
253 . '/plugins/TwitterBridge/daemons/synctwitterfriends.php'
255 if (common_config('twitterimport', 'enabled')) {
259 . '/plugins/TwitterBridge/daemons/twitterstatusfetcher.php'
268 * Register Twitter notice queue handler
270 * @param QueueManager $manager
272 * @return boolean hook return
274 function onEndInitializeQueueManager($manager)
276 if (self::hasKeys()) {
277 // Outgoing notices -> twitter
278 $manager->connect('twitter', 'TwitterQueueHandler');
280 // Incoming statuses <- twitter
281 $manager->connect('tweetin', 'TweetInQueueHandler');
283 // Control messages from our web interface to the import daemon
284 $manager->connect('tweetctl', 'TweetCtlQueueHandler', 'twitter');
290 * Add a Twitter tab to the admin panel
292 * @param Widget $nav Admin panel nav
294 * @return boolean hook value
297 function onEndAdminPanelNav($nav)
299 if (AdminPanelAction::canAdmin('twitter')) {
301 $action_name = $nav->action->trimmed('action');
304 common_local_url('twitteradminpanel'),
306 _m('Twitter bridge configuration'),
307 $action_name == 'twitteradminpanel',
308 'nav_twitter_admin_panel'
316 * Plugin version data
318 * @param array &$versions array of version blocks
320 * @return boolean hook value
322 function onPluginVersion(&$versions)
325 'name' => 'TwitterBridge',
326 'version' => self::VERSION,
327 'author' => 'Zach Copley, Julien C',
328 'homepage' => 'http://status.net/wiki/Plugin:TwitterBridge',
329 'rawdescription' => _m(
330 'The Twitter "bridge" plugin allows integration ' .
331 'of a StatusNet instance with ' .
332 '<a href="http://twitter.com/">Twitter</a>.'
339 * Expose the adminImportControl setting to the administration panel code.
340 * This allows us to disable the import bridge enabling checkbox for administrators,
341 * since on a bulk farm site we can't yet automate the import daemon setup.
343 * @return boolean hook value;
345 function onTwitterBridgeAdminImportControl()
347 return (bool)$this->adminImportControl;
351 * When the site is set to ssl=sometimes mode, we should make sure our
352 * various auth-related pages are on SSL to keep things looking happy.
353 * Although we're not submitting passwords directly, we do link out to
354 * an authentication source and it's a lot happier if we've got some
355 * protection against MitM.
357 * @param string $action name
358 * @param boolean $ssl outval to force SSL
359 * @return mixed hook return value
361 function onSensitiveAction($action, &$ssl)
363 $sensitive = array('twitteradminpanel',
365 'twitterauthorization',
367 if (in_array($action, $sensitive)) {
376 * Database schema setup
378 * We maintain a table mapping StatusNet notices to Twitter statuses
383 * @return boolean hook value; true means continue processing, false means stop.
385 function onCheckSchema()
387 $schema = Schema::get();
389 // For saving the last-synched status of various timelines
390 // home_timeline, messages (in), messages (out), ...
392 $schema->ensureTable('twitter_synch_status',
393 array(new ColumnDef('foreign_id', 'bigint', null,
395 new ColumnDef('timeline', 'varchar', 255,
397 new ColumnDef('last_id', 'bigint', null, // XXX: check for PostgreSQL
399 new ColumnDef('created', 'datetime', null,
401 new ColumnDef('modified', 'datetime', null,
404 // For storing user-submitted flags on profiles
406 $schema->ensureTable('notice_to_status',
407 array(new ColumnDef('notice_id', 'integer', null,
409 new ColumnDef('status_id', 'bigint', null, // XXX: check for PostgreSQL
411 new ColumnDef('created', 'datetime', null,
418 * If a notice gets deleted, remove the Notice_to_status mapping and
419 * delete the status on Twitter.
421 * @param User $user The user doing the deleting
422 * @param Notice $notice The notice getting deleted
424 * @return boolean hook value
426 function onStartDeleteOwnNotice(User $user, Notice $notice)
428 $n2s = Notice_to_status::staticGet('notice_id', $notice->id);
432 $flink = Foreign_link::getByUserID($notice->profile_id,
433 TWITTER_SERVICE); // twitter service
439 if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
440 $this->log(LOG_INFO, "Skipping deleting notice for {$notice->id} since link is not OAuth.");
444 $token = TwitterOAuthClient::unpackToken($flink->credentials);
445 $client = new TwitterOAuthClient($token->key, $token->secret);
447 $client->statusesDestroy($n2s->status_id);
455 * Notify remote users when their notices get favorited.
457 * @param Profile or User $profile of local user doing the faving
458 * @param Notice $notice being favored
459 * @return hook return value
461 function onEndFavorNotice(Profile $profile, Notice $notice)
463 $flink = Foreign_link::getByUserID($profile->id,
464 TWITTER_SERVICE); // twitter service
470 if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
471 $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth.");
475 $status_id = twitter_status_id($notice);
477 if (empty($status_id)) {
481 $token = TwitterOAuthClient::unpackToken($flink->credentials);
482 $client = new TwitterOAuthClient($token->key, $token->secret);
484 $client->favoritesCreate($status_id);
490 * Notify remote users when their notices get de-favorited.
492 * @param Profile $profile Profile person doing the de-faving
493 * @param Notice $notice Notice being favored
495 * @return hook return value
497 function onEndDisfavorNotice(Profile $profile, Notice $notice)
499 $flink = Foreign_link::getByUserID($profile->id,
500 TWITTER_SERVICE); // twitter service
506 if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
507 $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth.");
511 $status_id = twitter_status_id($notice);
513 if (empty($status_id)) {
517 $token = TwitterOAuthClient::unpackToken($flink->credentials);
518 $client = new TwitterOAuthClient($token->key, $token->secret);
520 $client->favoritesDestroy($status_id);