]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitterauthorization.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterauthorization.php
index b2657ff61fb0bbd220e71105139b8cc1c51fd96d..cabf69d7a8b47a0e03dc0fad1cf2eb3c877a5ca9 100644 (file)
@@ -56,6 +56,7 @@ class TwitterauthorizationAction extends Action
     var $tw_fields    = null;
     var $access_token = null;
     var $signin       = null;
+    var $verifier     = null;
 
     /**
      * Initialize class members. Looks for 'oauth_token' parameter.
@@ -70,6 +71,7 @@ class TwitterauthorizationAction extends Action
 
         $this->signin      = $this->boolean('signin');
         $this->oauth_token = $this->arg('oauth_token');
+        $this->verifier    = $this->arg('oauth_verifier');
 
         return true;
     }
@@ -89,11 +91,15 @@ class TwitterauthorizationAction extends Action
             $user  = common_current_user();
             $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
 
-            // If there's already a foreign link record, it means we already
-            // have an access token, and this is unecessary. So go back.
+            // If there's already a foreign link record and a foreign user
+            // it means the accounts are already linked, and this is unecessary.
+            // So go back.
 
             if (isset($flink)) {
-                common_redirect(common_local_url('twittersettings'));
+                $fuser = $flink->getForeignUser();
+                if (!empty($fuser)) {
+                    common_redirect(common_local_url('twittersettings'));
+                }
             }
         }
 
@@ -125,8 +131,7 @@ class TwitterauthorizationAction extends Action
             } else if ($this->arg('connect')) {
                 $this->connectNewUser();
             } else {
-                common_debug('Twitter Connect Plugin - ' .
-                             print_r($this->args, true));
+                common_debug('Twitter bridge - ' . print_r($this->args, true));
                 $this->showForm(_('Something weird happened.'),
                                 $this->trimmed('newname'));
             }
@@ -156,8 +161,7 @@ class TwitterauthorizationAction extends Action
             // Get a new request token and authorize it
 
             $client  = new TwitterOAuthClient();
-            $req_tok =
-              $client->getRequestToken(TwitterOAuthClient::$requestTokenURL);
+            $req_tok = $client->getRequestToken();
 
             // Sock the request token away in the session temporarily
 
@@ -167,9 +171,15 @@ class TwitterauthorizationAction extends Action
             $auth_link = $client->getAuthorizeLink($req_tok, $this->signin);
 
         } catch (OAuthClientException $e) {
-            $msg = sprintf('OAuth client cURL error - code: %1s, msg: %2s',
-                           $e->getCode(), $e->getMessage());
-            $this->serverError(_m('Couldn\'t link your Twitter account.'));
+            $msg = sprintf(
+                'OAuth client error - code: %1s, msg: %2s',
+                $e->getCode(),
+                $e->getMessage()
+            );
+            common_log(LOG_INFO, 'Twitter bridge - ' . $msg);
+            $this->serverError(
+                _m('Couldn\'t link your Twitter account.')
+            );
         }
 
         common_redirect($auth_link);
@@ -183,12 +193,13 @@ class TwitterauthorizationAction extends Action
      */
     function saveAccessToken()
     {
-
         // Check to make sure Twitter returned the same request
         // token we sent them
 
         if ($_SESSION['twitter_request_token'] != $this->oauth_token) {
-            $this->serverError(_m('Couldn\'t link your Twitter account.'));
+            $this->serverError(
+                _m('Couldn\'t link your Twitter account: oauth_token mismatch.')
+            );
         }
 
         $twitter_user = null;
@@ -200,7 +211,7 @@ class TwitterauthorizationAction extends Action
 
             // Exchange the request token for an access token
 
-            $atok = $client->getAccessToken(TwitterOAuthClient::$accessTokenURL);
+            $atok = $client->getAccessToken($this->verifier);
 
             // Test the access token and get the user's Twitter info
 
@@ -208,9 +219,15 @@ class TwitterauthorizationAction extends Action
             $twitter_user = $client->verifyCredentials();
 
         } catch (OAuthClientException $e) {
-            $msg = sprintf('OAuth client error - code: %1$s, msg: %2$s',
-                           $e->getCode(), $e->getMessage());
-            $this->serverError(_m('Couldn\'t link your Twitter account.'));
+            $msg = sprintf(
+                'OAuth client error - code: %1$s, msg: %2$s',
+                $e->getCode(),
+                $e->getMessage()
+            );
+            common_log(LOG_INFO, 'Twitter bridge - ' . $msg);
+            $this->serverError(
+                _m('Couldn\'t link your Twitter account.')
+            );
         }
 
         if (common_logged_in()) {
@@ -219,7 +236,7 @@ class TwitterauthorizationAction extends Action
 
             $user = common_current_user();
             $this->saveForeignLink($user->id, $twitter_user->id, $atok);
-            save_twitter_user($twitter_user->id, $twitter_user->name);
+            save_twitter_user($twitter_user->id, $twitter_user->screen_name);
 
         } else {
 
@@ -254,6 +271,10 @@ class TwitterauthorizationAction extends Action
     {
         $flink = new Foreign_link();
 
+        $flink->user_id = $user_id;
+        $flink->service = TWITTER_SERVICE;
+        $flink->delete(); // delete stale flink, if any
+
         $flink->user_id     = $user_id;
         $flink->foreign_id  = $twuid;
         $flink->service     = TWITTER_SERVICE;
@@ -271,7 +292,7 @@ class TwitterauthorizationAction extends Action
 
         if (empty($flink_id)) {
             common_log_db_error($flink, 'INSERT', __FILE__);
-                $this->serverError(_('Couldn\'t link your Twitter account.'));
+            $this->serverError(_('Couldn\'t link your Twitter account.'));
         }
 
         return $flink_id;