]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/EmailRegistration/emailregister.php
allow setting some initial tags on a new network
[quix0rs-gnu-social.git] / plugins / EmailRegistration / emailregister.php
index 22e8d59424f6f716be832fdb5c5109a9fcea4f29..1b0902587f78be6507149449ff684b53b4f5ccb6 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2011, StatusNet, Inc.
  *
  * Register a user by their email address
- * 
+ *
  * PHP version 5
  *
  * This program is free software: you can redistribute it and/or modify
@@ -39,10 +39,10 @@ if (!defined('STATUSNET')) {
  *
  * There are four cases where we're called:
  *
- * 1. GET, no arguments. Initial registration; ask for an email address.  
+ * 1. GET, no arguments. Initial registration; ask for an email address.
  * 2. POST, email address argument. Initial registration; send an email to confirm.
  * 3. GET, code argument. Confirming an invitation or a registration; look them up,
- *    create the relevant user if possible, login as that user, and 
+ *    create the relevant user if possible, login as that user, and
  *    show a password-entry form.
  * 4. POST, password argument. After confirmation, set the password for the new
  *    user, and redirect to a registration complete action with some instructions.
@@ -54,7 +54,6 @@ if (!defined('STATUSNET')) {
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  * @link      http://status.net/
  */
-
 class EmailregisterAction extends Action
 {
     const NEWEMAIL = 1;
@@ -80,6 +79,10 @@ class EmailregisterAction extends Action
     {
         parent::prepare($argarray);
 
+        if (common_config('site', 'closed')) {
+            throw new ClientException(_('Registration not allowed.'), 403);
+        }
+
         if ($this->isPost()) {
 
             $this->checkSessionToken();
@@ -87,6 +90,9 @@ class EmailregisterAction extends Action
             $this->email = $this->trimmed('email');
 
             if (!empty($this->email)) {
+                if (common_config('site', 'inviteonly')) {
+                    throw new ClientException(_('Sorry, only invited people can register.'), 403);
+                }
                 $this->email = common_canonical_email($this->email);
                 $this->state = self::NEWEMAIL;
             } else {
@@ -95,40 +101,53 @@ class EmailregisterAction extends Action
                 $this->code = $this->trimmed('code');
 
                 if (empty($this->code)) {
-                    throw new ClientException(_('No confirmation code.'));
+                    // TRANS: Client exception thrown when no confirmation code was provided.
+                    throw new ClientException(_m('No confirmation code.'));
                 }
 
                 $this->invitation = Invitation::staticGet('code', $this->code);
 
-                if (empty($this->invitation)) {
+                if (!empty($this->invitation)) {
+                    if (!empty($this->invitation->registered_user_id)) {
+                        throw new ClientException(_m('Invitation already used.'), 403);
+                    }
+                } else {
 
                     $this->confirmation = Confirm_address::staticGet('code', $this->code);
 
                     if (empty($this->confirmation)) {
-                        throw new ClientException(_('No such confirmation code.'), 403);
+                        // TRANS: Client exception thrown when given confirmation code was not issued.
+                        throw new ClientException(_m('No such confirmation code.'), 403);
                     }
                 }
 
                 $this->password1 = $this->trimmed('password1');
                 $this->password2 = $this->trimmed('password2');
-                
+
                 $this->tos = $this->boolean('tos');
             }
         } else { // GET
             $this->code = $this->trimmed('code');
 
             if (empty($this->code)) {
+                if (common_config('site', 'inviteonly')) {
+                    throw new ClientException(_('Sorry, only invited people can register.'), 403);
+                }
                 $this->state = self::NEWREGISTER;
             } else {
                 $this->invitation = Invitation::staticGet('code', $this->code);
                 if (!empty($this->invitation)) {
+                    if (!empty($this->invitation->registered_user_id)) {
+                        throw new ClientException(_m('Invitation already used.'), 403);
+                    }
                     $this->state = self::CONFIRMINVITE;
                 } else {
                     $this->state = self::CONFIRMREGISTER;
                     $this->confirmation = Confirm_address::staticGet('code', $this->code);
 
                     if (empty($this->confirmation)) {
-                        throw new ClientException(_('No such confirmation code.'), 405);
+                        // TRANS: Client exception thrown when given confirmation code was not issued.
+                        throw new ClientException(_m('No such confirmation code.'), 405);
                     }
                 }
             }
@@ -148,7 +167,7 @@ class EmailregisterAction extends Action
         case self::SETPASSWORD:
         case self::CONFIRMINVITE:
         case self::CONFIRMREGISTER:
-            // TRANS: Title for page where to change password.
+            // TRANS: Title for page where to register with a confirmation code.
             return _m('TITLE','Complete registration');
             break;
         }
@@ -164,6 +183,13 @@ class EmailregisterAction extends Action
 
     function handle($argarray=null)
     {
+        $cur = common_current_user();
+
+        if (!empty($cur)) {
+            common_redirect(common_local_url('all', array('nickname' => $cur->nickname)));
+            return;
+        }
+
         switch ($this->state) {
         case self::NEWREGISTER:
             $this->showRegistrationForm();
@@ -192,44 +218,23 @@ class EmailregisterAction extends Action
 
     function registerUser()
     {
-        $old = User::staticGet('email', $this->email);
-
-        if (!empty($old)) {
-            $this->error = sprintf(_('A user with that email address already exists. You can use the '.
-                                     '<a href="%s">password recovery</a> tool to recover a missing password.'),
-                                   common_local_url('recoverpassword'));
+        try {
+            $confirm = EmailRegistrationPlugin::registerEmail($this->email);
+        } catch (ClientException $ce) {
+            $this->error = $ce->getMessage();
             $this->showRegistrationForm();
             return;
         }
 
-        $valid = false;
-
-        if (Event::handle('StartValidateUserEmail', array(null, $this->email, &$valid))) {
-            $valid = Validate::email($this->email, common_config('email', 'check_domain'));
-            Event::handle('EndValidateUserEmail', array(null, $this->email, &$valid));
-        }
-
-        if (!$valid) {
-            $this->error = _('Not a valid email address.');
-            $this->showRegistrationForm();
-            return;
-        }
-
-        $confirm = Confirm_address::getAddress($this->email, self::CONFIRMTYPE);
-
-        if (empty($confirm)) {
-            $confirm = Confirm_address::saveNew(null, $this->email, 'register');
-            $prompt = sprintf(_('An email was sent to %s to confirm that address. Check your email inbox for instructions.'),
-                              $this->email);
-        } else {
-            $prompt = sprintf(_('The address %s was already registered but not confirmed. The confirmation code was resent.'),
-                              $this->email);
-        }
+        EmailRegistrationPlugin::sendConfirmEmail($confirm);
 
-        $this->sendConfirmEmail($confirm);
+        // TRANS: Confirmation text after initial registration.
+        // TRANS: %s an e-mail address.
+        $prompt = sprintf(_m('An email was sent to %s to confirm that address. Check your email inbox for instructions.'),
+                          $this->email);
 
         $this->complete = $prompt;
-        
+
         $this->showPage();
     }
 
@@ -240,7 +245,7 @@ class EmailregisterAction extends Action
         } else if (!empty($this->confirmation)) {
             $email = $this->confirmation->address;
         }
-        
+
         $nickname = $this->nicknameFromEmail($email);
 
         $this->form = new ConfirmRegistrationForm($this,
@@ -254,29 +259,56 @@ class EmailregisterAction extends Action
     {
         if (Event::handle('StartRegistrationTry', array($this))) {
             if (!empty($this->invitation)) {
-                $email = $this->invitation->address;
+                $email = trim($this->invitation->address);
             } else if (!empty($this->confirmation)) {
-                $email = $this->confirmation->address;
+                $email = trim($this->confirmation->address);
             } else {
                 throw new Exception('No confirmation thing.');
             }
 
             if (!$this->tos) {
-                $this->error = _('You must accept the terms of service and privacy policy to register.');
+                // TRANS: Error text when trying to register without agreeing to the terms.
+                $this->error = _m('You must accept the terms of service and privacy policy to register.');
+            } else if (empty($this->password1)) {
+                // TRANS: Error text when trying to register without a password.
+                $this->error = _m('You must set a password');
+            } else if (strlen($this->password1) < 6) {
+                // TRANS: Error text when trying to register with too short a password.
+                $this->error = _m('Password must be 6 or more characters.');
+            } else if ($this->password1 != $this->password2) {
+                // TRANS: Error text when trying to register without providing the same password twice.
+                $this->error = _m('Passwords do not match.');
+            }
+
+            if (!empty($this->error)) {
                 $nickname = $this->nicknameFromEmail($email);
-                $this->form = new ConfirmRegistrationForm($this, $nickname, $this->email, $this->code);
+                $this->form = new ConfirmRegistrationForm($this, $nickname, $email, $this->code);
                 $this->showPage();
                 return;
             }
 
             $nickname = $this->nicknameFromEmail($email);
 
-            $this->user = User::register(array('nickname' => $nickname,
-                                               'email' => $email,
-                                               'email_confirmed' => true));
+            try {
+                $fields = array('nickname' => $nickname,
+                                'email' => $email,
+                                'password' => $this->password1,
+                                'email_confirmed' => true);
+
+                if (!empty($this->invitation)) {
+                    $fields['code'] = $this->invitation->code;
+                }
+                $this->user = User::register($fields);
+            } catch (ClientException $e) {
+                $this->error = $e->getMessage();
+                $nickname = $this->nicknameFromEmail($email);
+                $this->form = new ConfirmRegistrationForm($this, $nickname, $email, $this->code);
+                $this->showPage();
+                return;
+            }
 
             if (empty($this->user)) {
-                throw new Exception("Failed to register user.");
+                throw new Exception('Failed to register user.');
             }
 
             common_set_user($this->user);
@@ -286,25 +318,18 @@ class EmailregisterAction extends Action
             // Re-init language env in case it changed (not yet, but soon)
             common_init_language();
 
-            if (!empty($this->invitation)) {
-                $inviter = User::staticGet('id', $this->invitation->user_id);
-                if (!empty($inviter)) {
-                    Subscription::start($inviter->getProfile(),
-                                        $user->getProfile());
-                }
-
-                $this->invitation->delete();
-            } else if (!empty($this->confirmation)) {
+            if (!empty($this->confirmation)) {
                 $this->confirmation->delete();
-            } else {
-                throw new Exception('No confirmation thing.');
             }
 
             Event::handle('EndRegistrationTry', array($this));
         }
 
-        common_redirect(common_local_url('doc', array('title' => 'welcome')),
-                        303);
+        if (Event::handle('StartRegisterSuccess', array($this))) {
+            common_redirect(common_local_url('doc', array('title' => 'welcome')),
+                            303);
+            Event::handle('EndRegisterSuccess', array($this));
+        }
     }
 
     function sendConfirmEmail($confirm)
@@ -315,11 +340,15 @@ class EmailregisterAction extends Action
 
         $headers['From'] = mail_notify_from();
         $headers['To'] = trim($confirm->address);
-        $headers['Subject'] = sprintf(_('Confirm your registration on %1$s'), $sitename);
+         // TRANS: Subject for confirmation e-mail.
+         // TRANS: %s is the StatusNet sitename.
+        $headers['Subject'] = sprintf(_m('Confirm your registration on %s'), $sitename);
 
         $confirmUrl = common_local_url('register', array('code' => $confirm->code));
 
-        $body = sprintf(_('Someone (probably you) has requested an account on %1$s using this email address.'.
+         // TRANS: Body for confirmation e-mail.
+         // TRANS: %1$s is the StatusNet sitename, %2$s is the confirmation URL.
+        $body = sprintf(_m('Someone (probably you) has requested an account on %1$s using this email address.'.
                           "\n".
                           'To confirm the address, click the following URL or copy it into the address bar of your browser.'.
                           "\n".
@@ -360,7 +389,6 @@ class EmailregisterAction extends Action
      *
      * @return boolean is read only action?
      */
-
     function isReadOnly($args)
     {
         return false;
@@ -368,24 +396,7 @@ class EmailregisterAction extends Action
 
     function nicknameFromEmail($email)
     {
-        $parts = explode('@', $email);
-        
-        $nickname = $parts[0];
-        
-        $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);
-
-        $nickname = Nickname::normalize($nickname);
-
-        $original = $nickname;
-
-        $n = 0;
-
-        while (User::staticGet('nickname', $nickname)) {
-            $n++;
-            $nickname = $original . $n;
-        }
-
-        return $nickname;
+        return EmailRegistrationPlugin::nicknameFromEmail($email);
     }
 
     /**
@@ -395,7 +406,6 @@ class EmailregisterAction extends Action
      *
      * @return void
      */
-
     function showLocalNav()
     {
         $nav = new LoginGroupNav($this);