]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitterauthorization.php
Merge branch '1.0.x' into 1.1.x
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterauthorization.php
index 7a896e1687a86b6ce736563286f5ae545ef36d4a..1a7234214e132d5867d99f86988333cb1b8e9c7c 100644 (file)
@@ -117,13 +117,15 @@ class TwitterauthorizationAction extends Action
             $token = $this->trimmed('token');
 
             if (!$token || $token != common_session_token()) {
-                $this->showForm(_('There was a problem with your session token. Try again, please.'));
+                // TRANS: Client error displayed when the session token does not match or is not given.
+                $this->showForm(_m('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.'),
+                    // TRANS: Form validation error displayed when the checkbox to agree to the license has not been checked.
+                    $this->showForm(_m('You cannot register if you do not agree to the license.'),
                                     $this->trimmed('newname'));
                     return;
                 }
@@ -132,7 +134,8 @@ class TwitterauthorizationAction extends Action
                 $this->connectNewUser();
             } else {
                 common_debug('Twitter bridge - ' . print_r($this->args, true));
-                $this->showForm(_('Something weird happened.'),
+                // TRANS: Form validation error displayed when an unhandled error occurs.
+                $this->showForm(_m('Something weird happened.'),
                                 $this->trimmed('newname'));
             }
         } else {
@@ -157,7 +160,6 @@ class TwitterauthorizationAction extends Action
     function authorizeRequestToken()
     {
         try {
-
             // Get a new request token and authorize it
 
             $client  = new TwitterOAuthClient();
@@ -169,7 +171,6 @@ class TwitterauthorizationAction extends Action
             $_SESSION['twitter_request_token_secret'] = $req_tok->secret;
 
             $auth_link = $client->getAuthorizeLink($req_tok, $this->signin);
-
         } catch (OAuthClientException $e) {
             $msg = sprintf(
                 'OAuth client error - code: %1s, msg: %2s',
@@ -178,7 +179,8 @@ class TwitterauthorizationAction extends Action
             );
             common_log(LOG_INFO, 'Twitter bridge - ' . $msg);
             $this->serverError(
-                _m('Couldn\'t link your Twitter account.')
+                // TRANS: Server error displayed when linking to a Twitter account fails.
+                _m('Could not link your Twitter account.')
             );
         }
 
@@ -198,7 +200,8 @@ class TwitterauthorizationAction extends Action
 
         if ($_SESSION['twitter_request_token'] != $this->oauth_token) {
             $this->serverError(
-                _m('Couldn\'t link your Twitter account: oauth_token mismatch.')
+                // TRANS: Server error displayed when linking to a Twitter account fails because of an incorrect oauth_token.
+                _m('Could not link your Twitter account: oauth_token mismatch.')
             );
         }
 
@@ -226,12 +229,12 @@ class TwitterauthorizationAction extends Action
             );
             common_log(LOG_INFO, 'Twitter bridge - ' . $msg);
             $this->serverError(
-                _m('Couldn\'t link your Twitter account.')
+                // TRANS: Server error displayed when linking to a Twitter account fails.
+                _m('Could not link your Twitter account.')
             );
         }
 
         if (common_logged_in()) {
-
             // Save the access token and Twitter user info
 
             $user = common_current_user();
@@ -242,7 +245,7 @@ class TwitterauthorizationAction extends Action
 
             $this->twuid = $twitter_user->id;
             $this->tw_fields = array("screen_name" => $twitter_user->screen_name,
-                                     "name" => $twitter_user->name);
+                                     "fullname" => $twitter_user->name);
             $this->access_token = $atok;
             $this->tryLogin();
         }
@@ -298,7 +301,8 @@ class TwitterauthorizationAction extends Action
 
         if (empty($flink_id)) {
             common_log_db_error($flink, 'INSERT', __FILE__);
-            $this->serverError(_('Couldn\'t link your Twitter account.'));
+            // TRANS: Server error displayed when linking to a Twitter account fails.
+            $this->serverError(_m('Could not link your Twitter account.'));
         }
 
         return $flink_id;
@@ -310,13 +314,15 @@ class TwitterauthorizationAction extends Action
             $this->element('div', array('class' => 'error'), $this->error);
         } else {
             $this->element('div', 'instructions',
-                           sprintf(_('This is the first time you\'ve logged into %s so we must connect your Twitter account to a local account. You can either create a new account, or connect with your existing account, if you have one.'), common_config('site', 'name')));
+                           // TRANS: Page instruction. %s is the StatusNet sitename.
+                           sprintf(_m('This is the first time you have logged into %s so we must connect your Twitter account to a local account. You can either create a new account, or connect with your existing account, if you have one.'), common_config('site', 'name')));
         }
     }
 
     function title()
     {
-        return _('Twitter Account Setup');
+        // TRANS: Page title.
+        return _m('Twitter Account Setup');
     }
 
     function showForm($error=null, $username=null)
@@ -349,69 +355,132 @@ class TwitterauthorizationAction extends Action
                                           'class' => 'form_settings',
                                           'action' => common_local_url('twitterauthorization')));
         $this->elementStart('fieldset', array('id' => 'settings_twitter_connect_options'));
-        $this->element('legend', null, _('Connection options'));
-        $this->elementStart('ul', 'form_data');
-        $this->elementStart('li');
-        $this->element('input', array('type' => 'checkbox',
-                                      'id' => 'license',
-                                      'class' => 'checkbox',
-                                      'name' => 'license',
-                                      'value' => 'true'));
-        $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
-        $message = _('My text and files are available under %s ' .
-                     'except this private data: password, ' .
-                     'email address, IM address, and phone number.');
-        $link = '<a href="' .
-                htmlspecialchars(common_config('license', 'url')) .
-                '">' .
-                htmlspecialchars(common_config('license', 'title')) .
-                '</a>';
-        $this->raw(sprintf(htmlspecialchars($message), $link));
-        $this->elementEnd('label');
-        $this->elementEnd('li');
-        $this->elementEnd('ul');
+        // TRANS: Fieldset legend.
+        $this->element('legend', null, _m('Connection options'));
+
         $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_name', $this->tw_fields['fullname']);
+        $this->hidden('token', common_session_token());
+
+        // Don't allow new account creation if site is flagged as invite only
+       if (common_config('site', 'inviteonly') == false) {
+            $this->elementStart('fieldset');
+            $this->element('legend', null,
+                           // TRANS: Fieldset legend.
+                           _m('Create new account'));
+            $this->element('p', null,
+                           // TRANS: Sub form introduction text.
+                          _m('Create a new user with this nickname.'));
+            $this->elementStart('ul', 'form_data');
+
+            // Hook point for captcha etc
+            Event::handle('StartRegistrationFormData', array($this));
+
+            $this->elementStart('li');
+            // TRANS: Field label.
+            $this->input('newname', _m('New nickname'),
+                         ($this->username) ? $this->username : '',
+                         // TRANS: Field title for nickname field.
+                         _m('1-64 lowercase letters or numbers, no punctuation or spaces.'));
+            $this->elementEnd('li');
+            $this->elementStart('li');
+            // TRANS: Field label.
+            $this->input('email', _m('LABEL','Email'), $this->getEmail(),
+                         // TRANS: Field title for e-mail address field.
+                         _m('Used only for updates, announcements, '.
+                           'and password recovery'));
+            $this->elementEnd('li');
+
+            // Hook point for captcha etc
+            Event::handle('EndRegistrationFormData', array($this));
+
+            $this->elementEnd('ul');
+            // TRANS: Button text for creating a new StatusNet account in the Twitter connect page.
+            $this->submit('create', _m('BUTTON','Create'));
+            $this->elementEnd('fieldset');
+        }
 
         $this->elementStart('fieldset');
-        $this->hidden('token', common_session_token());
         $this->element('legend', null,
-                       _('Create new account'));
+                       // TRANS: Fieldset legend.
+                       _m('Connect existing account'));
         $this->element('p', null,
-                       _('Create a new user with this nickname.'));
+                       // TRANS: Sub form introduction text.
+                       _m('If you already have an account, login with your username and password to connect it to your Twitter account.'));
         $this->elementStart('ul', 'form_data');
         $this->elementStart('li');
-        $this->input('newname', _('New nickname'),
-                     ($this->username) ? $this->username : '',
-                     _('1-64 lowercase letters or numbers, no punctuation or spaces'));
+        // TRANS: Field label.
+        $this->input('nickname', _m('Existing nickname'));
+        $this->elementEnd('li');
+        $this->elementStart('li');
+        // TRANS: Field label.
+        $this->password('password', _m('Password'));
         $this->elementEnd('li');
         $this->elementEnd('ul');
-        $this->submit('create', _('Create'));
         $this->elementEnd('fieldset');
 
         $this->elementStart('fieldset');
         $this->element('legend', null,
-                       _('Connect existing account'));
-        $this->element('p', null,
-                       _('If you already have an account, login with your username and password to connect it to your Twitter account.'));
+                       // TRANS: Fieldset legend.
+                       _m('License'));
         $this->elementStart('ul', 'form_data');
         $this->elementStart('li');
-        $this->input('nickname', _('Existing nickname'));
-        $this->elementEnd('li');
-        $this->elementStart('li');
-        $this->password('password', _('Password'));
+        $this->element('input', array('type' => 'checkbox',
+                                      'id' => 'license',
+                                      'class' => 'checkbox',
+                                      'name' => 'license',
+                                      'value' => 'true'));
+        $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
+        // TRANS: Text for license agreement checkbox.
+        // TRANS: %s is the license as configured for the StatusNet site.
+        $message = _m('My text and files are available under %s ' .
+                     'except this private data: password, ' .
+                     'email address, IM address, and phone number.');
+        $link = '<a href="' .
+                htmlspecialchars(common_config('license', 'url')) .
+                '">' .
+                htmlspecialchars(common_config('license', 'title')) .
+                '</a>';
+        $this->raw(sprintf(htmlspecialchars($message), $link));
+        $this->elementEnd('label');
         $this->elementEnd('li');
         $this->elementEnd('ul');
-        $this->submit('connect', _('Connect'));
         $this->elementEnd('fieldset');
-
+        // TRANS: Button text for connecting an existing StatusNet account in the Twitter connect page..
+        $this->submit('connect', _m('BUTTON','Connect'));
         $this->elementEnd('fieldset');
         $this->elementEnd('form');
     }
 
+    /**
+     * Get specified e-mail from the form, or the invite code.
+     *
+     * @return string
+     */
+    function getEmail()
+    {
+        $email = $this->trimmed('email');
+        if (!empty($email)) {
+            return $email;
+        }
+
+        // Terrible hack for invites...
+        if (common_config('site', 'inviteonly')) {
+            $code = $_SESSION['invitecode'];
+            if ($code) {
+                $invite = Invitation::staticGet($code);
+
+                if ($invite && $invite->address_type == 'email') {
+                    return $invite->address;
+                }
+            }
+        }
+        return '';
+    }
+
     function message($msg)
     {
         $this->message_text = $msg;
@@ -420,8 +489,13 @@ class TwitterauthorizationAction extends Action
 
     function createNewUser()
     {
+        if (!Event::handle('StartRegistrationTry', array($this))) {
+            return;
+        }
+
         if (common_config('site', 'closed')) {
-            $this->clientError(_('Registration not allowed.'));
+            // TRANS: Client error displayed when trying to create a new user while creating new users is not allowed.
+            $this->clientError(_m('Registration not allowed.'));
             return;
         }
 
@@ -430,38 +504,40 @@ class TwitterauthorizationAction extends Action
         if (common_config('site', 'inviteonly')) {
             $code = $_SESSION['invitecode'];
             if (empty($code)) {
-                $this->clientError(_('Registration not allowed.'));
+                // TRANS: Client error displayed when trying to create a new user while creating new users is not allowed.
+                $this->clientError(_m('Registration not allowed.'));
                 return;
             }
 
             $invite = Invitation::staticGet($code);
 
             if (empty($invite)) {
-                $this->clientError(_('Not a valid invitation code.'));
+                // TRANS: Client error displayed when trying to create a new user with an invalid invitation code.
+                $this->clientError(_m('Not a valid invitation code.'));
                 return;
             }
         }
 
-        $nickname = $this->trimmed('newname');
-
-        if (!Validate::string($nickname, array('min_length' => 1,
-                                               'max_length' => 64,
-                                               'format' => NICKNAME_FMT))) {
-            $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
+        try {
+            $nickname = Nickname::normalize($this->trimmed('newname'));
+        } catch (NicknameException $e) {
+            $this->showForm($e->getMessage());
             return;
         }
 
         if (!User::allowed_nickname($nickname)) {
-            $this->showForm(_('Nickname not allowed.'));
+            // TRANS: Client error displayed when trying to create a new user with an invalid username.
+            $this->showForm(_m('Nickname not allowed.'));
             return;
         }
 
         if (User::staticGet('nickname', $nickname)) {
-            $this->showForm(_('Nickname already in use. Try another one.'));
+            // TRANS: Client error displayed when trying to create a new user with a username that is already in use.
+            $this->showForm(_m('Nickname already in use. Try another one.'));
             return;
         }
 
-        $fullname = trim($this->tw_fields['name']);
+        $fullname = trim($this->tw_fields['fullname']);
 
         $args = array('nickname' => $nickname, 'fullname' => $fullname);
 
@@ -469,10 +545,16 @@ class TwitterauthorizationAction extends Action
             $args['code'] = $invite->code;
         }
 
+        $email = $this->getEmail();
+        if (!empty($email)) {
+            $args['email'] = $email;
+        }
+
         $user = User::register($args);
 
         if (empty($user)) {
-            $this->serverError(_('Error registering user.'));
+            // TRANS: Server error displayed when creating a new user has failed.
+            $this->serverError(_m('Error registering user.'));
             return;
         }
 
@@ -483,7 +565,8 @@ class TwitterauthorizationAction extends Action
         save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
 
         if (!$result) {
-            $this->serverError(_('Error connecting user to Twitter.'));
+            // TRANS: Server error displayed when connecting a user to a Twitter user has failed.
+            $this->serverError(_m('Error connecting user to Twitter.'));
             return;
         }
 
@@ -493,6 +576,8 @@ class TwitterauthorizationAction extends Action
         common_debug('TwitterBridge Plugin - ' .
                      "Registered new user $user->id from Twitter user $this->twuid");
 
+        Event::handle('EndRegistrationTry', array($this));
+
         common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)),
                         303);
     }
@@ -503,7 +588,9 @@ class TwitterauthorizationAction extends Action
         $password = $this->trimmed('password');
 
         if (!common_check_user($nickname, $password)) {
-            $this->showForm(_('Invalid username or password.'));
+            // TRANS: Form validation error displayed when connecting an existing user to a Twitter user fails because
+            // TRANS: the provided username and/or password are incorrect.
+            $this->showForm(_m('Invalid username or password.'));
             return;
         }
 
@@ -521,7 +608,8 @@ class TwitterauthorizationAction extends Action
         save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
 
         if (!$result) {
-            $this->serverError(_('Error connecting user to Twitter.'));
+            // TRANS: Server error displayed connecting a user to a Twitter user has failed.
+            $this->serverError(_m('Error connecting user to Twitter.'));
             return;
         }
 
@@ -541,7 +629,8 @@ class TwitterauthorizationAction extends Action
         $result = $this->flinkUser($user->id, $this->twuid);
 
         if (empty($result)) {
-            $this->serverError(_('Error connecting user to Twitter.'));
+            // TRANS: Server error displayed connecting a user to a Twitter user has failed.
+            $this->serverError(_m('Error connecting user to Twitter.'));
             return;
         }
 
@@ -599,8 +688,8 @@ class TwitterauthorizationAction extends Action
 
     function bestNewNickname()
     {
-        if (!empty($this->tw_fields['name'])) {
-            $nickname = $this->nicknamize($this->tw_fields['name']);
+        if (!empty($this->tw_fields['fullname'])) {
+            $nickname = $this->nicknamize($this->tw_fields['fullname']);
             if ($this->isNewNickname($nickname)) {
                 return $nickname;
             }
@@ -620,9 +709,7 @@ class TwitterauthorizationAction extends Action
 
     function isNewNickname($str)
     {
-        if (!Validate::string($str, array('min_length' => 1,
-                                          'max_length' => 64,
-                                          'format' => NICKNAME_FMT))) {
+        if (!Nickname::isValid($str)) {
             return false;
         }
         if (!User::allowed_nickname($str)) {
@@ -633,6 +720,4 @@ class TwitterauthorizationAction extends Action
         }
         return true;
     }
-
 }
-