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)
197 $dir = dirname(__FILE__);
200 case 'TwittersettingsAction':
201 case 'TwitterauthorizationAction':
202 case 'TwitterloginAction':
203 case 'TwitteradminpanelAction':
204 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
206 case 'TwitterOAuthClient':
207 case 'TwitterQueueHandler':
208 include_once $dir . '/' . strtolower($cls) . '.php';
210 case 'Notice_to_status':
211 include_once $dir . '/' . $cls . '.php';
219 * Add a Twitter queue item for each notice
221 * @param Notice $notice the notice
222 * @param array &$transports the list of transports (queues)
224 * @return boolean hook return
226 function onStartEnqueueNotice($notice, &$transports)
228 if (self::hasKeys() && $notice->isLocal()) {
229 // Avoid a possible loop
230 if ($notice->source != 'twitter') {
231 array_push($transports, 'twitter');
238 * Add Twitter bridge daemons to the list of daemons to start
240 * @param array $daemons the list fo daemons to run
242 * @return boolean hook return
244 function onGetValidDaemons($daemons)
246 if (self::hasKeys()) {
250 . '/plugins/TwitterBridge/daemons/synctwitterfriends.php'
252 if (common_config('twitterimport', 'enabled')) {
256 . '/plugins/TwitterBridge/daemons/twitterstatusfetcher.php'
265 * Register Twitter notice queue handler
267 * @param QueueManager $manager
269 * @return boolean hook return
271 function onEndInitializeQueueManager($manager)
273 if (self::hasKeys()) {
274 $manager->connect('twitter', 'TwitterQueueHandler');
280 * Add a Twitter tab to the admin panel
282 * @param Widget $nav Admin panel nav
284 * @return boolean hook value
287 function onEndAdminPanelNav($nav)
289 if (AdminPanelAction::canAdmin('twitter')) {
291 $action_name = $nav->action->trimmed('action');
294 common_local_url('twitteradminpanel'),
296 _m('Twitter bridge configuration'),
297 $action_name == 'twitteradminpanel',
298 'nav_twitter_admin_panel'
306 * Plugin version data
308 * @param array &$versions array of version blocks
310 * @return boolean hook value
313 function onPluginVersion(&$versions)
316 'name' => 'TwitterBridge',
317 'version' => self::VERSION,
318 'author' => 'Zach Copley, Julien C',
319 'homepage' => 'http://status.net/wiki/Plugin:TwitterBridge',
320 'rawdescription' => _m(
321 'The Twitter "bridge" plugin allows you to integrate ' .
322 'your StatusNet instance with ' .
323 '<a href="http://twitter.com/">Twitter</a>.'
330 * Expose the adminImportControl setting to the administration panel code.
331 * This allows us to disable the import bridge enabling checkbox for administrators,
332 * since on a bulk farm site we can't yet automate the import daemon setup.
334 * @return boolean hook value;
336 function onTwitterBridgeAdminImportControl()
338 return (bool)$this->adminImportControl;
342 * When the site is set to ssl=sometimes mode, we should make sure our
343 * various auth-related pages are on SSL to keep things looking happy.
344 * Although we're not submitting passwords directly, we do link out to
345 * an authentication source and it's a lot happier if we've got some
346 * protection against MitM.
348 * @param string $action name
349 * @param boolean $ssl outval to force SSL
350 * @return mixed hook return value
352 function onSensitiveAction($action, &$ssl)
354 $sensitive = array('twitteradminpanel',
356 'twitterauthorization',
358 if (in_array($action, $sensitive)) {
367 * Database schema setup
369 * We maintain a table mapping StatusNet notices to Twitter statuses
374 * @return boolean hook value; true means continue processing, false means stop.
377 function onCheckSchema()
379 $schema = Schema::get();
381 // For storing user-submitted flags on profiles
383 $schema->ensureTable('notice_to_status',
384 array(new ColumnDef('notice_id', 'integer', null,
386 new ColumnDef('status_id', 'bigint', null, // XXX: check for PostgreSQL
388 new ColumnDef('created', 'datetime', null,
391 // We update any notices that may have come in from
392 // Twitter that we don't have a status_id for. Note that
393 // this won't catch notices that originated at this StatusNet site.
397 $n->query('SELECT notice.id, notice.uri ' .
398 'FROM notice LEFT JOIN notice_to_status ' .
399 'ON notice.id = notice_to_status.notice_id ' .
400 'WHERE notice.source = "twitter"' .
401 'AND notice_to_status.status_id IS NULL');
403 while ($n->fetch()) {
404 if (preg_match('#^http://twitter.com/[\w_.]+/status/(\d+)$#', $n->uri, $match)) {
406 $status_id = $match[1];
408 Notice_to_status::saveNew($n->id, $status_id);
416 * If a notice gets deleted, remove the Notice_to_status mapping
418 * @param Notice $notice The notice getting deleted
420 * @return boolean hook value
423 function onNoticeDeleteRelated($notice)
425 $n2s = Notice_to_status::staticGet('notice_id', $notice->id);