]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitterauthorization.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterauthorization.php
index 8ae725374f3c95e553fb7d4d014be06ce1d027f7..b2657ff61fb0bbd220e71105139b8cc1c51fd96d 100644 (file)
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * @category  TwitterauthorizationAction
+ * @category  Plugin
  * @package   StatusNet
  * @author    Zach Copley <zach@status.net>
- * @copyright 2009 StatusNet, Inc.
+ * @author    Julien C <chaumond@gmail.com>
+ * @copyright 2009-2010 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link      http://status.net/
  */
@@ -41,19 +42,21 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
  * (Foreign_link) between the StatusNet user and Twitter user and stores the
  * access token and secret in the link.
  *
- * @category Twitter
+ * @category Plugin
  * @package  StatusNet
  * @author   Zach Copley <zach@status.net>
+ * @author   Julien C <chaumond@gmail.com>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://laconi.ca/
  *
  */
 class TwitterauthorizationAction extends Action
 {
-    var $twuid = null;
-    var $tw_fields  = null;
+    var $twuid        = null;
+    var $tw_fields    = null;
     var $access_token = null;
-    
+    var $signin       = null;
+
     /**
      * Initialize class members. Looks for 'oauth_token' parameter.
      *
@@ -65,6 +68,7 @@ class TwitterauthorizationAction extends Action
     {
         parent::prepare($args);
 
+        $this->signin      = $this->boolean('signin');
         $this->oauth_token = $this->arg('oauth_token');
 
         return true;
@@ -80,31 +84,37 @@ class TwitterauthorizationAction extends Action
     function handle($args)
     {
         parent::handle($args);
-        
+
         if (common_logged_in()) {
             $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 (isset($flink)) {
                 common_redirect(common_local_url('twittersettings'));
             }
         }
-        
-        
+
         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+
             // User was not logged in to StatusNet before
+
             $this->twuid = $this->trimmed('twuid');
-            $this->tw_fields = array("name" => $this->trimmed('name'), "fullname" => $this->trimmed('fullname'));
+
+            $this->tw_fields = array('screen_name' => $this->trimmed('tw_fields_screen_name'),
+                                     'fullname' => $this->trimmed('tw_fields_fullname'));
+
             $this->access_token = new OAuthToken($this->trimmed('access_token_key'), $this->trimmed('access_token_secret'));
 
             $token = $this->trimmed('token');
+
             if (!$token || $token != common_session_token()) {
                 $this->showForm(_('There was a problem with your session token. Try again, please.'));
                 return;
             }
+
             if ($this->arg('create')) {
                 if (!$this->boolean('license')) {
                     $this->showForm(_('You can\'t register if you don\'t agree to the license.'),
@@ -124,7 +134,7 @@ class TwitterauthorizationAction extends Action
             // $this->oauth_token is only populated once Twitter authorizes our
             // request token. If it's empty we're at the beginning of the auth
             // process
-    
+
             if (empty($this->oauth_token)) {
                 $this->authorizeRequestToken();
             } else {
@@ -154,7 +164,7 @@ class TwitterauthorizationAction extends Action
             $_SESSION['twitter_request_token']        = $req_tok->key;
             $_SESSION['twitter_request_token_secret'] = $req_tok->secret;
 
-            $auth_link = $client->getAuthorizeLink($req_tok);
+            $auth_link = $client->getAuthorizeLink($req_tok, $this->signin);
 
         } catch (OAuthClientException $e) {
             $msg = sprintf('OAuth client cURL error - code: %1s, msg: %2s',
@@ -181,6 +191,8 @@ class TwitterauthorizationAction extends Action
             $this->serverError(_m('Couldn\'t link your Twitter account.'));
         }
 
+        $twitter_user = null;
+
         try {
 
             $client = new TwitterOAuthClient($_SESSION['twitter_request_token'],
@@ -196,18 +208,24 @@ class TwitterauthorizationAction extends Action
             $twitter_user = $client->verifyCredentials();
 
         } catch (OAuthClientException $e) {
-            $msg = sprintf('OAuth client cURL error - code: %1$s, msg: %2$s',
+            $msg = sprintf('OAuth client error - code: %1$s, msg: %2$s',
                            $e->getCode(), $e->getMessage());
             $this->serverError(_m('Couldn\'t link your Twitter account.'));
         }
 
         if (common_logged_in()) {
+
             // Save the access token and Twitter user info
-            $this->saveForeignLink($atok, $twitter_user);
-        }
-        else{
+
+            $user = common_current_user();
+            $this->saveForeignLink($user->id, $twitter_user->id, $atok);
+            save_twitter_user($twitter_user->id, $twitter_user->name);
+
+        } else {
+
             $this->twuid = $twitter_user->id;
-            $this->tw_fields = array("name" => $twitter_user->screen_name, "fullname" => $twitter_user->name);
+            $this->tw_fields = array("screen_name" => $twitter_user->screen_name,
+                                     "name" => $twitter_user->name);
             $this->access_token = $atok;
             $this->tryLogin();
         }
@@ -216,7 +234,7 @@ class TwitterauthorizationAction extends Action
 
         unset($_SESSION['twitter_request_token']);
         unset($_SESSION['twitter_request_token_secret']);
-        
+
         if (common_logged_in()) {
             common_redirect(common_local_url('twittersettings'));
         }
@@ -226,19 +244,18 @@ class TwitterauthorizationAction extends Action
      * Saves a Foreign_link between Twitter user and local user,
      * which includes the access token and secret.
      *
-     * @param OAuthToken $access_token the access token to save
-     * @param mixed      $twitter_user twitter API user object
+     * @param int        $user_id StatusNet user ID
+     * @param int        $twuid   Twitter user ID
+     * @param OAuthToken $token   the access token to save
      *
      * @return nothing
      */
-    function saveForeignLink($access_token, $twitter_user)
+    function saveForeignLink($user_id, $twuid, $access_token)
     {
-        $user = common_current_user();
-
         $flink = new Foreign_link();
 
-        $flink->user_id     = $user->id;
-        $flink->foreign_id  = $twitter_user->id;
+        $flink->user_id     = $user_id;
+        $flink->foreign_id  = $twuid;
         $flink->service     = TWITTER_SERVICE;
 
         $creds = TwitterOAuthClient::packToken($access_token);
@@ -254,15 +271,12 @@ class TwitterauthorizationAction extends Action
 
         if (empty($flink_id)) {
             common_log_db_error($flink, 'INSERT', __FILE__);
-                $this->serverError(_m('Couldn\'t link your Twitter account.'));
+                $this->serverError(_('Couldn\'t link your Twitter account.'));
         }
 
-        save_twitter_user($twitter_user->id, $twitter_user->screen_name);
+        return $flink_id;
     }
 
-
-
-
     function showPageNotice()
     {
         if ($this->error) {
@@ -322,8 +336,8 @@ class TwitterauthorizationAction extends Action
         $this->hidden('access_token_key', $this->access_token->key);
         $this->hidden('access_token_secret', $this->access_token->secret);
         $this->hidden('twuid', $this->twuid);
+        $this->hidden('tw_fields_screen_name', $this->tw_fields['screen_name']);
         $this->hidden('tw_fields_name', $this->tw_fields['name']);
-        $this->hidden('tw_fields_fullname', $this->tw_fields['fullname']);
 
         $this->elementStart('fieldset');
         $this->hidden('token', common_session_token());
@@ -410,7 +424,7 @@ class TwitterauthorizationAction extends Action
             return;
         }
 
-        $fullname = trim($this->tw_fields['fullname']);
+        $fullname = trim($this->tw_fields['name']);
 
         $args = array('nickname' => $nickname, 'fullname' => $fullname);
 
@@ -420,7 +434,11 @@ class TwitterauthorizationAction extends Action
 
         $user = User::register($args);
 
-        $result = $this->flinkUser($user->id, $this->twuid);
+        $result = $this->saveForeignLink($user->id,
+                                         $this->twuid,
+                                         $this->access_token);
+
+        save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
 
         if (!$result) {
             $this->serverError(_('Error connecting user to Twitter.'));
@@ -430,8 +448,8 @@ class TwitterauthorizationAction extends Action
         common_set_user($user);
         common_real_login(true);
 
-        common_debug('Twitter Connect Plugin - ' .
-                     "Registered new user $user->id from Twitter user $this->fbuid");
+        common_debug('TwitterBridge Plugin - ' .
+                     "Registered new user $user->id from Twitter user $this->twuid");
 
         common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)),
                         303);
@@ -450,19 +468,23 @@ class TwitterauthorizationAction extends Action
         $user = User::staticGet('nickname', $nickname);
 
         if (!empty($user)) {
-            common_debug('Twitter Connect Plugin - ' .
+            common_debug('TwitterBridge Plugin - ' .
                          "Legit user to connect to Twitter: $nickname");
         }
 
-        $result = $this->flinkUser($user->id, $this->twuid);
+        $result = $this->saveForeignLink($user->id,
+                                         $this->twuid,
+                                         $this->access_token);
+
+        save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
 
         if (!$result) {
             $this->serverError(_('Error connecting user to Twitter.'));
             return;
         }
 
-        common_debug('Twitter Connnect Plugin - ' .
-                     "Connected Twitter user $this->fbuid to local user $user->id");
+        common_debug('TwitterBridge Plugin - ' .
+                     "Connected Twitter user $this->twuid to local user $user->id");
 
         common_set_user($user);
         common_real_login(true);
@@ -481,26 +503,27 @@ class TwitterauthorizationAction extends Action
             return;
         }
 
-        common_debug('Twitter Connect Plugin - ' .
-                     "Connected Twitter user $this->fbuid to local user $user->id");
+        common_debug('TwitterBridge Plugin - ' .
+                     "Connected Twitter user $this->twuid to local user $user->id");
 
         // Return to Twitter connection settings tab
         common_redirect(common_local_url('twittersettings'), 303);
     }
-    
+
     function tryLogin()
     {
-        common_debug('Twitter Connect Plugin - ' .
-                     "Trying login for Twitter user $this->fbuid.");
+        common_debug('TwitterBridge Plugin - ' .
+                     "Trying login for Twitter user $this->twuid.");
 
-        $flink = Foreign_link::getByForeignID($this->twuid, TWITTER_SERVICE);
+        $flink = Foreign_link::getByForeignID($this->twuid,
+                                              TWITTER_SERVICE);
 
         if (!empty($flink)) {
             $user = $flink->getUser();
 
             if (!empty($user)) {
 
-                common_debug('Twitter Connect Plugin - ' .
+                common_debug('TwitterBridge Plugin - ' .
                              "Logged in Twitter user $flink->foreign_id as user $user->id ($user->nickname)");
 
                 common_set_user($user);
@@ -510,7 +533,7 @@ class TwitterauthorizationAction extends Action
 
         } else {
 
-            common_debug('Twitter Connect Plugin - ' .
+            common_debug('TwitterBridge Plugin - ' .
                          "No flink found for twuid: $this->twuid - new user");
 
             $this->showForm(null, $this->bestNewNickname());
@@ -531,37 +554,7 @@ class TwitterauthorizationAction extends Action
 
         common_redirect($url, 303);
     }
-    
-    function flinkUser($user_id, $twuid)
-    {
-        $flink = new Foreign_link();
-
-        $flink->user_id     = $user_id;
-        $flink->foreign_id  = $twuid;
-        $flink->service     = TWITTER_SERVICE;
-        
-        $creds = TwitterOAuthClient::packToken($this->access_token);
-
-        $flink->credentials = $creds;
-        $flink->created     = common_sql_now();
-
-        // Defaults: noticesync on, everything else off
-
-        $flink->set_flags(true, false, false, false);
-
-        $flink_id = $flink->insert();
 
-        if (empty($flink_id)) {
-            common_log_db_error($flink, 'INSERT', __FILE__);
-                $this->serverError(_('Couldn\'t link your Twitter account.'));
-        }
-
-        save_twitter_user($twuid, $this->tw_fields['name']);
-        
-        return $flink_id;
-    }
-    
-    
     function bestNewNickname()
     {
         if (!empty($this->tw_fields['name'])) {