Example #1
0
 public function register()
 {
     $Token = new Token();
     if (!$Token->check($_POST['token'])) {
         $_SESSION['alert'] = 'Error, please try again.';
     } else {
         $Verify = new Verify();
         $username = trim(strip_tags($_POST['username']));
         $password = trim(strip_tags($_POST['password']));
         $repassword = trim(strip_tags($_POST['repassword']));
         $email = trim(strip_tags($_POST['email']));
         $email = explode('@', $email);
         if (!isset($username) && !isset($password) && !isset($repassword) && !isset($email)) {
             $_SESSION['alert'] = 'Not all fields have been completed.';
         } elseif (!$Verify->length($username, 255)) {
             $_SESSION['alert'] = 'The username is too long.';
         } elseif (!$Verify->same($password, $repassword)) {
             $_SESSION['alert'] = 'The passwords entered are not the same.';
         } elseif (!$Verify->length($email[0], 255)) {
             $_SESSION['alert'] = 'The email entered is too long.';
         } elseif (!$Verify->length($email[1], 255)) {
             $_SESSION['alert'] = 'The email entered is too long.';
         } else {
             $Db = new Db();
             $query = $Db->query('user', array(array('username', '=', $username, '')));
             $numrows = mysqli_num_rows($query);
             if ($numrows > 0) {
                 $_SESSION['alert'] = 'Error, please try again.';
             } else {
                 $salt = base64_encode(mcrypt_create_iv(128, MCRYPT_DEV_URANDOM));
                 $crypt = hash('sha512', $username . $salt . $password);
                 $datetime = date('Y-m-d H:i:s');
                 $rank = 0;
                 $insert = $Db->insert('user', array('', $username, $crypt, $email[0], $email[1], $salt, $datetime, $rank));
                 if (!$insert) {
                     $_SESSION['alert'] = 'User could not be registered.';
                 } else {
                     $_SESSION['alert'] = 'Successfully registered, you can now login with your credentials.';
                     header('Location: login.php');
                 }
             }
         }
     }
 }