123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- require_once __DIR__ . '/twitter.php';
- class TwitterBridgePlugin extends Plugin
- {
- const VERSION = GNUSOCIAL_VERSION;
- public $adminImportControl = false;
-
- function initialize()
- {
-
-
- if (isset($this->consumer_key)) {
- $key = common_config('twitter', 'consumer_key');
- if (empty($key)) {
- Config::save('twitter', 'consumer_key', $this->consumer_key);
- }
- }
- if (isset($this->consumer_secret)) {
- $secret = common_config('twitter', 'consumer_secret');
- if (empty($secret)) {
- Config::save(
- 'twitter',
- 'consumer_secret',
- $this->consumer_secret
- );
- }
- }
- }
-
- static function hasKeys()
- {
- $ckey = common_config('twitter', 'consumer_key');
- $csecret = common_config('twitter', 'consumer_secret');
- if (empty($ckey) && empty($csecret)) {
- $ckey = common_config('twitter', 'global_consumer_key');
- $csecret = common_config('twitter', 'global_consumer_secret');
- }
- if (!empty($ckey) && !empty($csecret)) {
- return true;
- }
- return false;
- }
-
- public function onRouterInitialized(URLMapper $m)
- {
- $m->connect('panel/twitter', array('action' => 'twitteradminpanel'));
- if (self::hasKeys()) {
- $m->connect(
- 'twitter/authorization',
- array('action' => 'twitterauthorization')
- );
- $m->connect(
- 'settings/twitter', array(
- 'action' => 'twittersettings'
- )
- );
- if (common_config('twitter', 'signin')) {
- $m->connect(
- 'main/twitterlogin',
- array('action' => 'twitterlogin')
- );
- }
- }
- return true;
- }
-
- function onEndLoginGroupNav($action)
- {
- $action_name = $action->trimmed('action');
- if (self::hasKeys() && common_config('twitter', 'signin')) {
- $action->menuItem(
- common_local_url('twitterlogin'),
-
- _m('MENU','Twitter'),
-
- _m('Login or register using Twitter.'),
- 'twitterlogin' === $action_name
- );
- }
- return true;
- }
-
- function onEndConnectSettingsNav($action)
- {
- if (self::hasKeys()) {
- $action_name = $action->trimmed('action');
- $action->menuItem(
- common_local_url('twittersettings'),
-
- _m('MENU','Twitter'),
-
- _m('Twitter integration options'),
- $action_name === 'twittersettings'
- );
- }
- return true;
- }
-
- function onStartEnqueueNotice($notice, &$transports)
- {
- if (self::hasKeys() && $notice->isLocal() && $notice->inScope(null)) {
-
- if ($notice->source != 'twitter') {
- array_push($transports, 'twitter');
- }
- }
- return true;
- }
-
- function onGetValidDaemons(&$daemons)
- {
- if (self::hasKeys()) {
- array_push(
- $daemons,
- INSTALLDIR
- . '/plugins/TwitterBridge/daemons/synctwitterfriends.php'
- );
- if (common_config('twitterimport', 'enabled')) {
- array_push(
- $daemons,
- INSTALLDIR
- . '/plugins/TwitterBridge/daemons/twitterstatusfetcher.php'
- );
- }
- }
- return true;
- }
-
- function onEndInitializeQueueManager($manager)
- {
- if (self::hasKeys()) {
-
- $manager->connect('twitter', 'TwitterQueueHandler');
-
- $manager->connect('tweetin', 'TweetInQueueHandler');
- }
- return true;
- }
-
- function onAdminPanelCheck($name, &$isOK)
- {
- if ($name == 'twitter') {
- $isOK = true;
- return false;
- }
- return true;
- }
-
- function onEndAdminPanelNav($nav)
- {
- if (AdminPanelAction::canAdmin('twitter')) {
- $action_name = $nav->action->trimmed('action');
- $nav->out->menuItem(
- common_local_url('twitteradminpanel'),
-
- _m('Twitter'),
-
- _m('Twitter bridge configuration page.'),
- $action_name == 'twitteradminpanel',
- 'nav_twitter_admin_panel'
- );
- }
- return true;
- }
-
- function onPluginVersion(&$versions)
- {
- $versions[] = array(
- 'name' => 'TwitterBridge',
- 'version' => self::VERSION,
- 'author' => 'Zach Copley, Julien C, Jean Baptiste Favre',
- 'homepage' => 'http://status.net/wiki/Plugin:TwitterBridge',
-
- 'rawdescription' => _m('The Twitter "bridge" plugin allows integration ' .
- 'of a StatusNet instance with ' .
- '<a href="http://twitter.com/">Twitter</a>.'
- )
- );
- return true;
- }
-
- function onTwitterBridgeAdminImportControl()
- {
- return (bool)$this->adminImportControl;
- }
-
- function onSensitiveAction($action, &$ssl)
- {
- $sensitive = array('twitteradminpanel',
- 'twittersettings',
- 'twitterauthorization',
- 'twitterlogin');
- if (in_array($action, $sensitive)) {
- $ssl = true;
- return false;
- } else {
- return true;
- }
- }
-
- function onCheckSchema()
- {
- $schema = Schema::get();
-
-
- $schema->ensureTable('twitter_synch_status', Twitter_synch_status::schemaDef());
-
- $schema->ensureTable('notice_to_status', Notice_to_status::schemaDef());
- return true;
- }
-
- function onStartDeleteOwnNotice(User $user, Notice $notice)
- {
- $n2s = Notice_to_status::getKV('notice_id', $notice->id);
- if (!empty($n2s)) {
- $flink = Foreign_link::getByUserID($notice->profile_id,
- TWITTER_SERVICE);
- if (empty($flink)) {
- return true;
- }
- if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
- $this->log(LOG_INFO, "Skipping deleting notice for {$notice->id} since link is not OAuth.");
- return true;
- }
- try {
- $token = TwitterOAuthClient::unpackToken($flink->credentials);
- $client = new TwitterOAuthClient($token->key, $token->secret);
- $client->statusesDestroy($n2s->status_id);
- } catch (Exception $e) {
- common_log(LOG_ERR, "Error attempting to delete bridged notice from Twitter: " . $e->getMessage());
- }
- $n2s->delete();
- }
- return true;
- }
-
- function onEndFavorNotice(Profile $profile, Notice $notice)
- {
- $flink = Foreign_link::getByUserID($profile->id,
- TWITTER_SERVICE);
- if (empty($flink)) {
- return true;
- }
- if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
- $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth.");
- return true;
- }
- $status_id = twitter_status_id($notice);
- if (empty($status_id)) {
- return true;
- }
- try {
- $token = TwitterOAuthClient::unpackToken($flink->credentials);
- $client = new TwitterOAuthClient($token->key, $token->secret);
- $client->favoritesCreate($status_id);
- } catch (Exception $e) {
- common_log(LOG_ERR, "Error attempting to favorite bridged notice on Twitter: " . $e->getMessage());
- }
- return true;
- }
-
- function onEndDisfavorNotice(Profile $profile, Notice $notice)
- {
- $flink = Foreign_link::getByUserID($profile->id,
- TWITTER_SERVICE);
- if (empty($flink)) {
- return true;
- }
- if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
- $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth.");
- return true;
- }
- $status_id = twitter_status_id($notice);
- if (empty($status_id)) {
- return true;
- }
- try {
- $token = TwitterOAuthClient::unpackToken($flink->credentials);
- $client = new TwitterOAuthClient($token->key, $token->secret);
- $client->favoritesDestroy($status_id);
- } catch (Exception $e) {
- common_log(LOG_ERR, "Error attempting to unfavorite bridged notice on Twitter: " . $e->getMessage());
- }
- return true;
- }
- function onStartGetProfileUri($profile, &$uri)
- {
- if (preg_match('!^https?://twitter.com/!', $profile->profileurl)) {
- $uri = $profile->profileurl;
- return false;
- }
- return true;
- }
-
- function onOtherAccountProfiles($profile, &$links)
- {
- $fuser = null;
- $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
- if (!empty($flink)) {
- $fuser = $flink->getForeignUser();
- if (!empty($fuser)) {
- $links[] = array("href" => $fuser->uri,
- "text" => sprintf(_("@%s on Twitter"), $fuser->nickname),
- "image" => $this->path("icons/twitter-bird-white-on-blue.png"));
- }
- }
- return true;
- }
- public function onEndShowHeadElements(Action $action)
- {
- if (!($action instanceof AttachmentAction)) {
- return true;
- }
-
-
- switch ($action->attachment->mimetype) {
- case 'image/pjpeg':
- case 'image/jpeg':
- case 'image/jpg':
- case 'image/png':
- case 'image/gif':
- $action->element('meta', array('name' => 'twitter:card',
- 'content' => 'photo'),
- null);
- $action->element('meta', array('name' => 'twitter:url',
- 'content' => common_local_url('attachment',
- array('attachment' => $action->attachment->id))),
- null );
- $action->element('meta', array('name' => 'twitter:image',
- 'content' => $action->attachment->url));
- $action->element('meta', array('name' => 'twitter:title',
- 'content' => $action->attachment->title));
- $ns = new AttachmentNoticeSection($action);
- $notices = $ns->getNotices();
- $noticeArray = $notices->fetchAll();
-
- if( count($noticeArray) != 1 ) { break; }
- $post = $noticeArray[0];
- $flink = Foreign_link::getByUserID($post->profile_id, TWITTER_SERVICE);
- if( $flink ) {
- $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE);
- if( $fuser ) {
- $action->element('meta', array('name' => 'twitter:creator',
- 'content' => '@'.$fuser->nickname));
- }
- }
- break;
- default: break;
- }
- return true;
- }
- }
|