]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/FacebookBridge/actions/facebookfinishlogin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / FacebookBridge / actions / facebookfinishlogin.php
index 2703ab85542a08fd8cf222ebedfd403e0cd776bc..4a9e09b53dfe1c27fe1f9a0ce2b7987c8d0e8a98 100644 (file)
@@ -37,7 +37,7 @@ class FacebookfinishloginAction extends Action
     private $fbuser      = null; // Facebook user object (JSON)
     private $accessToken = null; // Access token provided by Facebook JS API
 
-    function prepare($args) {
+    function prepare(array $args=array()) {
         parent::prepare($args);
 
         // Check cookie for a valid access_token
@@ -48,18 +48,12 @@ class FacebookfinishloginAction extends Action
 
         if (empty($this->accessToken)) {
             $this->clientError(_m("Unable to authenticate you with Facebook."));
-            return false;
         }
 
         $graphUrl = 'https://graph.facebook.com/me?access_token=' . urlencode($this->accessToken);
         $this->fbuser = json_decode(file_get_contents($graphUrl));
 
-        if (!empty($this->fbuser)) {
-            $this->fbuid  = $this->fbuser->id;
-            // OKAY, all is well... proceed to register
-            return true;
-        } else {
-
+        if (empty($this->fbuser)) {
             // log badness
 
             list($proxy, $ip) = common_client_ip();
@@ -80,10 +74,12 @@ class FacebookfinishloginAction extends Action
             );
         }
 
-        return false;
+        $this->fbuid  = $this->fbuser->id;
+        // OKAY, all is well... proceed to register
+        return true;
     }
 
-    function handle($args)
+    function handle(array $args=array())
     {
         parent::handle($args);
 
@@ -120,8 +116,6 @@ class FacebookfinishloginAction extends Action
                 // TRANS: in the same StatusNet site.
                 _m('There is already a local account linked with that Facebook account.')
             );
-
-            return;
        }
 
        $cur = common_current_user();
@@ -136,8 +130,6 @@ class FacebookfinishloginAction extends Action
                 // TRANS: in the same StatusNet site.
                 _m('There is already a local account linked with that Facebook account.')
             );
-
-            return;
         }
     }
 
@@ -332,7 +324,6 @@ class FacebookfinishloginAction extends Action
         if (common_config('site', 'closed')) {
             // TRANS: Client error trying to register with registrations not allowed.
             $this->clientError(_m('Registration not allowed.'));
-            return;
         }
 
         $invite = null;
@@ -342,37 +333,23 @@ class FacebookfinishloginAction extends Action
             if (empty($code)) {
                 // TRANS: Client error trying to register with registrations 'invite only'.
                 $this->clientError(_m('Registration not allowed.'));
-                return;
             }
 
-            $invite = Invitation::staticGet($code);
+            $invite = Invitation::getKV($code);
 
             if (empty($invite)) {
                 // TRANS: Client error trying to register with an invalid invitation code.
                 $this->clientError(_m('Not a valid invitation code.'));
-                return;
             }
         }
 
         try {
-            $nickname = Nickname::normalize($this->trimmed('newname'));
+            $nickname = Nickname::normalize($this->trimmed('newname'), true);
         } catch (NicknameException $e) {
             $this->showForm($e->getMessage());
             return;
         }
 
-        if (!User::allowed_nickname($nickname)) {
-            // TRANS: Form validation error displayed when picking a nickname that is not allowed.
-            $this->showForm(_m('Nickname not allowed.'));
-            return;
-        }
-
-        if (User::staticGet('nickname', $nickname)) {
-            // TRANS: Form validation error displayed when picking a nickname that is already in use.
-            $this->showForm(_m('Nickname already in use. Try another one.'));
-            return;
-        }
-
         $args = array(
             'nickname' => $nickname,
             'fullname' => $this->fbuser->name,
@@ -399,7 +376,6 @@ class FacebookfinishloginAction extends Action
         if (!$result) {
             // TRANS: Server error displayed when connecting to Facebook fails.
             $this->serverError(_m('Error connecting user to Facebook.'));
-            return;
         }
 
         // Add a Foreign_user record
@@ -448,7 +424,7 @@ class FacebookfinishloginAction extends Action
             if ($response->isOk()) {
 
                 // seems to always be jpeg, but not sure
-                $tmpname = "facebook-avatar-tmp-" . common_good_rand(4);
+                $tmpname = "facebook-avatar-tmp-" . common_random_hexstr(4);
 
                 $ok = file_put_contents(
                     Avatar::path($tmpname),
@@ -460,8 +436,14 @@ class FacebookfinishloginAction extends Action
                 } else {
                     // save it as an avatar
 
-                    $file = new ImageFile($user->id, Avatar::path($tmpname));
-                    $filename = $file->resize(180); // size of the biggest img we get from Facebook
+                    $imagefile = new ImageFile(null, Avatar::path($tmpname));
+                    $filename = Avatar::filename($user->id, image_type_to_extension($imagefile->preferredType()),
+                                                 180, common_timestamp());
+                    // Previous docs said 180 is the "biggest img we get from Facebook"
+                    $imagefile->resizeTo(Avatar::path($filename, array('width'=>180, 'height'=>180)));
+
+                    // No need to keep the temporary file around...
+                    @unlink(Avatar::path($tmpname));
 
                     $profile   = $user->getProfile();
 
@@ -481,7 +463,6 @@ class FacebookfinishloginAction extends Action
                         );
 
                         // clean up tmp file
-                        @unlink(Avatar::path($tmpname));
                     }
 
                 }
@@ -503,7 +484,7 @@ class FacebookfinishloginAction extends Action
             return;
         }
 
-        $user = User::staticGet('nickname', $nickname);
+        $user = User::getKV('nickname', $nickname);
 
         $this->tryLinkUser($user);
 
@@ -533,40 +514,35 @@ class FacebookfinishloginAction extends Action
         if (empty($result)) {
             // TRANS: Server error displayed when connecting to Facebook fails.
             $this->serverError(_m('Error connecting user to Facebook.'));
-            return;
         }
     }
 
     function tryLogin()
     {
-        $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
-
-        if (!empty($flink)) {
+        try {
+            $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
             $user = $flink->getUser();
 
-            if (!empty($user)) {
-
-                common_log(
-                    LOG_INFO,
-                    sprintf(
-                        'Logged in Facebook user %s as user %d (%s)',
-                        $this->fbuid,
-                        $user->nickname,
-                        $user->id
-                    ),
-                    __FILE__
-                );
+            common_log(
+                LOG_INFO,
+                sprintf(
+                    'Logged in Facebook user %s as user %d (%s)',
+                    $this->fbuid,
+                    $user->nickname,
+                    $user->id
+                ),
+                __FILE__
+            );
 
-                common_set_user($user);
-                common_real_login(true);
+            common_set_user($user);
+            common_real_login(true);
 
-                // clear out the stupid cookie
-                setcookie('fb_access_token', '', time() - 3600); // one hour ago
+            // clear out the stupid cookie
+            setcookie('fb_access_token', '', time() - 3600); // one hour ago
 
-                $this->goHome($user->nickname);
-            }
+            $this->goHome($user->nickname);
 
-        } else {
+        } catch (NoResultException $e) {
             $this->showForm(null, $this->bestNewNickname());
         }
     }
@@ -603,58 +579,23 @@ class FacebookfinishloginAction extends Action
 
     function bestNewNickname()
     {
-        if (!empty($this->fbuser->username)) {
-            $nickname = $this->nicknamize($this->fbuser->username);
-            if ($this->isNewNickname($nickname)) {
-                return $nickname;
-            }
+        try {
+            $nickname = Nickname::normalize($this->fbuser->username, true);
+            return $nickname;
+        } catch (NicknameException $e) {
+            // Failed to normalize nickname, but let's try the full name
         }
 
-        // Try the full name
-
-        $fullname = $this->fbuser->name;
-
-        if (!empty($fullname)) {
-            $fullname = $this->nicknamize($fullname);
-            if ($this->isNewNickname($fullname)) {
-                return $fullname;
-            }
+        try {
+            $nickname = Nickname::normalize($this->fbuser->name, true);
+            return $nickname;
+        } catch (NicknameException $e) {
+            // Any more ideas? Nope.
         }
 
         return null;
     }
 
-     /**
-      * Given a string, try to make it work as a nickname
-      */
-     function nicknamize($str)
-     {
-         $str = preg_replace('/\W/', '', $str);
-         return strtolower($str);
-     }
-
-     /*
-      * Is the desired nickname already taken?
-      *
-      * @return boolean result
-      */
-     function isNewNickname($str)
-     {
-        if (!Nickname::isValid($str)) {
-            return false;
-        }
-
-        if (!User::allowed_nickname($str)) {
-            return false;
-        }
-
-        if (User::staticGet('nickname', $str)) {
-            return false;
-        }
-
-        return true;
-    }
-
     /*
      * Do we already have a user record with this email?
      * (emails have to be unique but they can change)
@@ -666,7 +607,7 @@ class FacebookfinishloginAction extends Action
      function isNewEmail($email)
      {
          // we shouldn't have to validate the format
-         $result = User::staticGet('email', $email);
+         $result = User::getKV('email', $email);
 
          if (empty($result)) {
              return true;