]> 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 15408668fc7d4a8350b219e0288e1db60a63c484..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,9 +42,10 @@ 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/
  *
@@ -101,7 +103,7 @@ class TwitterauthorizationAction extends Action
 
             $this->twuid = $this->trimmed('twuid');
 
-            $this->tw_fields = array('name' => $this->trimmed('tw_fields_name'),
+            $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'));
@@ -215,12 +217,15 @@ class TwitterauthorizationAction extends Action
 
             // Save the access token and Twitter user info
 
-            $this->saveForeignLink($atok, $twitter_user);
+            $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();
         }
@@ -239,41 +244,13 @@ 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)
-    {
-        $user = common_current_user();
-
-        $flink = new Foreign_link();
-
-        $flink->user_id     = $user->id;
-        $flink->foreign_id  = $twitter_user->id;
-        $flink->service     = TWITTER_SERVICE;
-
-        $creds = TwitterOAuthClient::packToken($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(_m('Couldn\'t link your Twitter account.'));
-        }
-
-        save_twitter_user($twitter_user->id, $twitter_user->screen_name);
-    }
-
-    function flinkUser($user_id, $twuid)
+    function saveForeignLink($user_id, $twuid, $access_token)
     {
         $flink = new Foreign_link();
 
@@ -281,7 +258,7 @@ class TwitterauthorizationAction extends Action
         $flink->foreign_id  = $twuid;
         $flink->service     = TWITTER_SERVICE;
 
-        $creds = TwitterOAuthClient::packToken($this->access_token);
+        $creds = TwitterOAuthClient::packToken($access_token);
 
         $flink->credentials = $creds;
         $flink->created     = common_sql_now();
@@ -297,8 +274,6 @@ class TwitterauthorizationAction extends Action
                 $this->serverError(_('Couldn\'t link your Twitter account.'));
         }
 
-        save_twitter_user($twuid, $this->tw_fields['name']);
-
         return $flink_id;
     }
 
@@ -361,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());
@@ -449,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);
 
@@ -459,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.'));
@@ -470,7 +449,7 @@ class TwitterauthorizationAction extends Action
         common_real_login(true);
 
         common_debug('TwitterBridge Plugin - ' .
-                     "Registered new user $user->id from Twitter user $this->fbuid");
+                     "Registered new user $user->id from Twitter user $this->twuid");
 
         common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)),
                         303);
@@ -493,7 +472,11 @@ class TwitterauthorizationAction extends Action
                          "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.'));
@@ -501,7 +484,7 @@ class TwitterauthorizationAction extends Action
         }
 
         common_debug('TwitterBridge Plugin - ' .
-                     "Connected Twitter user $this->fbuid to local user $user->id");
+                     "Connected Twitter user $this->twuid to local user $user->id");
 
         common_set_user($user);
         common_real_login(true);
@@ -521,7 +504,7 @@ class TwitterauthorizationAction extends Action
         }
 
         common_debug('TwitterBridge Plugin - ' .
-                     "Connected Twitter user $this->fbuid to local user $user->id");
+                     "Connected Twitter user $this->twuid to local user $user->id");
 
         // Return to Twitter connection settings tab
         common_redirect(common_local_url('twittersettings'), 303);
@@ -532,7 +515,8 @@ class TwitterauthorizationAction extends Action
         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();