Example #1
0
 /**
  * Register a new user in the system
  * @param username string Username
  * @param password1 string Password
  * @param password2 string Password verification
  * @param pin int 4 digit PIN code
  * @param email1 string Email address
  * @param email2 string Email confirmation
  * @return bool
  **/
 public function register($username, $password1, $password2, $pin, $email1 = '', $email2 = '', $tac = '', $strToken = '')
 {
     $this->debug->append("STA " . __METHOD__, 4);
     if ($tac != 1) {
         $this->setErrorMessage('You need to accept our <a href="' . $_SERVER['SCRIPT_NAME'] . '?page=tac" target="_blank">Terms and Conditions</a>');
         return false;
     }
     if (strlen($username) > 40) {
         $this->setErrorMessage('Username exceeding character limit');
         return false;
     }
     if (preg_match('/[^a-z_\\-0-9]/i', $username)) {
         $this->setErrorMessage('Username may only contain alphanumeric characters');
         return false;
     }
     if ($this->getEmail($email1)) {
         $this->setErrorMessage('This e-mail address is already taken');
         return false;
     }
     if (strlen($password1) < 8) {
         $this->setErrorMessage('Password is too short, minimum of 8 characters required');
         return false;
     }
     if ($password1 !== $password2) {
         $this->setErrorMessage('Password do not match');
         return false;
     }
     if (empty($email1) || !filter_var($email1, FILTER_VALIDATE_EMAIL)) {
         $this->setErrorMessage('Invalid e-mail address');
         return false;
     }
     if ($email1 !== $email2) {
         $this->setErrorMessage('E-mail do not match');
         return false;
     }
     if (!is_numeric($pin) || strlen($pin) > 4 || strlen($pin) < 4) {
         $this->setErrorMessage('Invalid PIN');
         return false;
     }
     if (isset($strToken) && !empty($strToken)) {
         if (!($aToken = $this->token->getToken($strToken, 'invitation'))) {
             $this->setErrorMessage('Unable to find token');
             return false;
         }
         // Circle dependency, so we create our own object here
         $invitation = new Invitation();
         $invitation->setMysql($this->mysqli);
         $invitation->setDebug($this->debug);
         $invitation->setLog($this->log);
         $invitation->setUser($this);
         $invitation->setConfig($this->config);
         if (!$invitation->setActivated($aToken['id'])) {
             $this->setErrorMessage('Unable to activate your invitation');
             return false;
         }
         if (!$this->token->deleteToken($strToken)) {
             $this->setErrorMessage('Unable to remove used token');
             $this->log->log("warn", "{$username} tried to register but failed to delete the invitation token");
             return false;
         }
     }
     if ($this->mysqli->query("SELECT id FROM {$this->table} LIMIT 1")->num_rows > 0) {
         !$this->setting->getValue('accounts_confirm_email_disabled') ? $is_locked = 1 : ($is_locked = 0);
         $is_admin = 0;
         $stmt = $this->mysqli->prepare("\n        INSERT INTO {$this->table} (username, pass, email, signup_timestamp, pin, api_key, is_locked)\n        VALUES (?, ?, ?, ?, ?, ?, ?)\n        ");
     } else {
         $is_locked = 0;
         $is_admin = 1;
         $stmt = $this->mysqli->prepare("\n        INSERT INTO {$this->table} (username, pass, email, signup_timestamp, pin, api_key, is_admin, is_locked)\n        VALUES (?, ?, ?, ?, ?, ?, 1, ?)\n        ");
     }
     // Create hashed strings using original string and salt
     $password_hash = $this->getHash($password1);
     $pin_hash = $this->getHash($pin);
     $apikey_hash = $this->getHash($username);
     $username_clean = strip_tags($username);
     $signup_time = time();
     if ($this->checkStmt($stmt) && $stmt->bind_param('sssissi', $username_clean, $password_hash, $email1, $signup_time, $pin_hash, $apikey_hash, $is_locked) && $stmt->execute()) {
         if (!$this->setting->getValue('accounts_confirm_email_disabled') && $is_admin != 1) {
             if ($token = $this->token->createToken('confirm_email', $stmt->insert_id)) {
                 $aData['username'] = $username_clean;
                 $aData['token'] = $token;
                 $aData['email'] = $email1;
                 $aData['subject'] = 'E-Mail verification';
                 if (!$this->mail->sendMail('register/confirm_email', $aData)) {
                     $this->setErrorMessage('Unable to request email confirmation: ' . $this->mail->getError());
                     return false;
                 }
                 return true;
             } else {
                 $this->setErrorMessage('Failed to create confirmation token');
                 $this->debug->append('Unable to create confirm_email token: ' . $this->token->getError());
                 return false;
             }
         } else {
             return true;
         }
     } else {
         $this->setErrorMessage('Unable to register');
         $this->debug->append('Failed to insert user into DB: ' . $this->mysqli->error);
         if ($stmt->sqlstate == '23000') {
             $this->setErrorMessage('Username or email already registered');
         }
         return false;
     }
     return false;
 }