]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitterauthorization.php
More info for a proper, fancy-url lighttpd setup
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterauthorization.php
index 931a037230b2c26289fbd1cc1ed7fd3ed87647e4..6a86c81cb724ea01092b9503b1e532a1dd3d08f2 100644 (file)
@@ -117,13 +117,15 @@ class TwitterauthorizationAction extends Action
             $token = $this->trimmed('token');
 
             if (!$token || $token != common_session_token()) {
+                // 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(_m('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,6 +134,7 @@ class TwitterauthorizationAction extends Action
                 $this->connectNewUser();
             } else {
                 common_debug('Twitter bridge - ' . print_r($this->args, true));
+                // TRANS: Form validation error displayed when an unhandled error occurs.
                 $this->showForm(_m('Something weird happened.'),
                                 $this->trimmed('newname'));
             }
@@ -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,7 +229,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.')
             );
         }
 
@@ -241,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();
         }
@@ -297,7 +301,8 @@ class TwitterauthorizationAction extends Action
 
         if (empty($flink_id)) {
             common_log_db_error($flink, 'INSERT', __FILE__);
-            $this->serverError(_m('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;
@@ -309,12 +314,14 @@ class TwitterauthorizationAction extends Action
             $this->element('div', array('class' => 'error'), $this->error);
         } else {
             $this->element('div', 'instructions',
-                           sprintf(_m('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()
     {
+        // TRANS: Page title.
         return _m('Twitter Account Setup');
     }
 
@@ -348,69 +355,132 @@ class TwitterauthorizationAction extends Action
                                           'class' => 'form_settings',
                                           'action' => common_local_url('twitterauthorization')));
         $this->elementStart('fieldset', array('id' => 'settings_twitter_connect_options'));
+        // TRANS: Fieldset legend.
         $this->element('legend', null, _m('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 = _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->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->elementStart('fieldset');
+        $this->hidden('tw_fields_name', $this->tw_fields['fullname']);
         $this->hidden('token', common_session_token());
-        $this->element('legend', null,
-                       _m('Create new account'));
-        $this->element('p', null,
-                       _m('Create a new user with this nickname.'));
-        $this->elementStart('ul', 'form_data');
-        $this->elementStart('li');
-        $this->input('newname', _m('New nickname'),
-                     ($this->username) ? $this->username : '',
-                     _m('1-64 lowercase letters or numbers, no punctuation or spaces'));
-        $this->elementEnd('li');
-        $this->elementEnd('ul');
-        $this->submit('create', _m('Create'));
-        $this->elementEnd('fieldset');
+
+        // 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->element('legend', null,
+                       // TRANS: Fieldset legend.
                        _m('Connect existing account'));
         $this->element('p', null,
+                       // 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');
+        // 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('connect', _m('Connect'));
         $this->elementEnd('fieldset');
 
+        $this->elementStart('fieldset');
+        $this->element('legend', null,
+                       // TRANS: Fieldset legend.
+                       _m('License'));
+        $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'));
+        // 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->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::getKV($code);
+
+                if ($invite && $invite->address_type == 'email') {
+                    return $invite->address;
+                }
+            }
+        }
+        return '';
+    }
+
     function message($msg)
     {
         $this->message_text = $msg;
@@ -419,7 +489,12 @@ class TwitterauthorizationAction extends Action
 
     function createNewUser()
     {
+        if (!Event::handle('StartRegistrationTry', array($this))) {
+            return;
+        }
+
         if (common_config('site', 'closed')) {
+            // 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;
         }
@@ -429,38 +504,40 @@ class TwitterauthorizationAction extends Action
         if (common_config('site', 'inviteonly')) {
             $code = $_SESSION['invitecode'];
             if (empty($code)) {
+                // 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);
+            $invite = Invitation::getKV($code);
 
             if (empty($invite)) {
+                // 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(_m('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)) {
+            // 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)) {
+        if (User::getKV('nickname', $nickname)) {
+            // 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);
 
@@ -468,9 +545,15 @@ class TwitterauthorizationAction extends Action
             $args['code'] = $invite->code;
         }
 
+        $email = $this->getEmail();
+        if (!empty($email)) {
+            $args['email'] = $email;
+        }
+
         $user = User::register($args);
 
         if (empty($user)) {
+            // TRANS: Server error displayed when creating a new user has failed.
             $this->serverError(_m('Error registering user.'));
             return;
         }
@@ -482,6 +565,7 @@ class TwitterauthorizationAction extends Action
         save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
 
         if (!$result) {
+            // TRANS: Server error displayed when connecting a user to a Twitter user has failed.
             $this->serverError(_m('Error connecting user to Twitter.'));
             return;
         }
@@ -492,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);
     }
@@ -502,11 +588,13 @@ class TwitterauthorizationAction extends Action
         $password = $this->trimmed('password');
 
         if (!common_check_user($nickname, $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;
         }
 
-        $user = User::staticGet('nickname', $nickname);
+        $user = User::getKV('nickname', $nickname);
 
         if (!empty($user)) {
             common_debug('TwitterBridge Plugin - ' .
@@ -520,6 +608,7 @@ class TwitterauthorizationAction extends Action
         save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
 
         if (!$result) {
+            // TRANS: Server error displayed connecting a user to a Twitter user has failed.
             $this->serverError(_m('Error connecting user to Twitter.'));
             return;
         }
@@ -540,6 +629,7 @@ class TwitterauthorizationAction extends Action
         $result = $this->flinkUser($user->id, $this->twuid);
 
         if (empty($result)) {
+            // TRANS: Server error displayed connecting a user to a Twitter user has failed.
             $this->serverError(_m('Error connecting user to Twitter.'));
             return;
         }
@@ -598,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;
             }
@@ -619,19 +709,15 @@ 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)) {
             return false;
         }
-        if (User::staticGet('nickname', $str)) {
+        if (User::getKV('nickname', $str)) {
             return false;
         }
         return true;
     }
-
 }
-