register refactoring + small bug fixes that went unnoticed before

This commit is contained in:
Quitta 2013-06-19 06:50:08 +02:00
parent b60ef2194e
commit c95fd0aefd
3 changed files with 107 additions and 79 deletions

View file

@ -7,6 +7,62 @@ class Users{
} }
public function check_Register(){
// check values
if ( isset( $_POST["Username"] ) and isset( $_POST["Password"] ) and isset( $_POST["Email"] ) ){
$user = Users :: checkUser( $_POST["Username"] );
$pass = Users :: checkPassword( $_POST["Password"] );
$cpass = Users :: confirmPassword($pass);
$email = Users :: checkEmail( $_POST["Email"] );
}else{
$user = "";
$pass = "";
$cpass = "";
$email = "";
}
if ( ( $user == "success" ) and ( $pass == "success" ) and ( $cpass == "success" ) and ( $email == "success" ) and ( isset( $_POST["TaC"] ) ) ){
return "success";
}else{
$pageElements = array(
//'GAME_NAME' => $GAME_NAME,
// 'WELCOME_MESSAGE' => $WELCOME_MESSAGE,
'USERNAME' => $user,
'PASSWORD' => $pass,
'CPASSWORD' => $cpass,
'EMAIL' => $email
);
if ( $user != "success" ){
$pageElements['USERNAME_ERROR'] = 'TRUE';
}else{
$pageElements['USERNAME_ERROR'] = 'FALSE';
}
if ( $pass != "success" ){
$pageElements['PASSWORD_ERROR'] = 'TRUE';
}else{
$pageElements['PASSWORD_ERROR'] = 'FALSE';
}
if ( $cpass != "success" ){
$pageElements['CPASSWORD_ERROR'] = 'TRUE';
}else{
$pageElements['CPASSWORD_ERROR'] = 'FALSE';
}
if ( $email != "success" ){
$pageElements['EMAIL_ERROR'] = 'TRUE';
}else{
$pageElements['EMAIL_ERROR'] = 'FALSE';
}
if ( isset( $_POST["TaC"] ) ){
$pageElements['TAC_ERROR'] = 'FALSE';
}else{
$pageElements['TAC_ERROR'] = 'TRUE';
}
return $pageElements;
}
}
/** /**
* Function checkUser * Function checkUser
* *
@ -22,6 +78,9 @@ class Users{
return "Username must be 5 or more characters."; return "Username must be 5 or more characters.";
}elseif ( !preg_match( '/^[a-z0-9\.]*$/', $username ) ){ }elseif ( !preg_match( '/^[a-z0-9\.]*$/', $username ) ){
return "Username can only contain numbers and letters."; return "Username can only contain numbers and letters.";
}elseif ( $username == "" ){
return "You have to fill in a username";
/*}elseif ( sql :: db_query( "SELECT COUNT(*) FROM {users} WHERE name = :name", array( /*}elseif ( sql :: db_query( "SELECT COUNT(*) FROM {users} WHERE name = :name", array(
':name' => $username ':name' => $username
) ) -> fetchField() ){ ) ) -> fetchField() ){
@ -47,6 +106,8 @@ class Users{
return "Password must be no more than 20 characters."; return "Password must be no more than 20 characters.";
}elseif ( strlen( $pass ) < 5 ){ }elseif ( strlen( $pass ) < 5 ){
return "Password must be more than 5 characters."; return "Password must be more than 5 characters.";
}elseif ( $pass == ""){
return "You have to fill in a password";
}else{ }else{
return "success"; return "success";
} }
@ -59,10 +120,14 @@ class Users{
* @takes $pass * @takes $pass
* @return string Info: Verify's $_POST["Password"] is the same as $_POST["ConfirmPass"] * @return string Info: Verify's $_POST["Password"] is the same as $_POST["ConfirmPass"]
*/ */
public function confirmPassword() public function confirmPassword($pass_result)
{ {
if ( ( $_POST["Password"] ) != ( $_POST["ConfirmPass"] ) ){ if ( ( $_POST["Password"] ) != ( $_POST["ConfirmPass"] ) ){
return "Passwords do not match."; return "Passwords do not match.";
}else if ($_POST["ConfirmPass"]==""){
return "You have to fill in the confirmation password.";
}else if($pass_result != "success"){
return;
}else{ }else{
return "success"; return "success";
} }
@ -79,11 +144,14 @@ class Users{
if ( isset( $email ) ){ if ( isset( $email ) ){
if ( !Users::validEmail( $email ) ){ if ( !Users::validEmail( $email ) ){
return "Email address is not valid."; return "Email address is not valid.";
}else if($email == ""){
return "You have to fill in an email address";
}
/*}elseif ( db_query( "SELECT COUNT(*) FROM {users} WHERE mail = :mail", array( /*}elseif ( db_query( "SELECT COUNT(*) FROM {users} WHERE mail = :mail", array(
':mail' => $email ':mail' => $email
) ) -> fetchField() ){ ) ) -> fetchField() ){
return "Email is in use.";*/ return "Email is in use.";}*/
}else{ else{
return "success"; return "success";
} }
}else{ }else{

View file

@ -1,22 +1,10 @@
<?php <?php
function add_user(){ function add_user(){
$result = Users :: check_Register();
if ( isset( $_POST["Username"] ) and isset( $_POST["Password"] ) and isset( $_POST["Email"] ) ) //print_r($result);
{
// check values
$user = users :: checkUser( $_POST["Username"] );
$pass = users :: checkPassword( $_POST["Password"] );
$cpass = users :: confirmPassword();
$email = users :: checkEmail( $_POST["Email"] );
}else{
$user = "";
$pass = "";
$cpass = "";
$email = "";
}
// if all are good then create user // if all are good then create user
if ( ( $user == "success" ) and ( $pass == "success" ) and ( $cpass == "success" ) and ( $email == "success" ) and ( isset( $_POST["TaC"] ) ) ){ if ( $result == "success"){
$edit = array( $edit = array(
'name' => $_POST["Username"], 'name' => $_POST["Username"],
'pass' => $_POST["Password"], 'pass' => $_POST["Password"],
@ -26,45 +14,17 @@ if ( ( $user == "success" ) and ( $pass == "success" ) and ( $cpass == "success"
'status' => 1, 'status' => 1,
'access' => REQUEST_TIME 'access' => REQUEST_TIME
); );
header( 'Location: email_sent.php' ); //header( 'Location: email_sent.php' );
write_user( $edit ); write_user( $edit );
exit; exit;
}else{ }else{
$pageElements = array( // pass error
//'GAME_NAME' => $GAME_NAME, $result['prevUsername'] = $_POST["Username"];
// 'WELCOME_MESSAGE' => $WELCOME_MESSAGE, $result['prevPassword'] = $_POST["Password"];
// 'USERNAME' => $user, $result['prevConfirmPass'] = $_POST["ConfirmPass"];
// 'PASSWORD' => $pass, $result['prevEmail'] = $_POST["Email"];
// 'CPASSWORD' => $cpass, $result['no_visible_elements'] = 'TRUE';
// 'EMAIL' => $email helpers :: loadtemplate( 'register', $result);
);
if ( $user != "success" ){
$pageElements['USERNAME_ERROR'] = 'TRUE';
}else{
$pageElements['USERNAME_ERROR'] = 'FALSE';
}
if ( $pass != "success" ){
$pageElements['PASSWORD_ERROR'] = 'TRUE';
}else{
$pageElements['PASSWORD_ERROR'] = 'FALSE';
}
if ( $cpass != "success" ){
$pageElements['CPASSWORD_ERROR'] = 'TRUE';
}else{
$pageElements['CPASSWORD_ERROR'] = 'FALSE';
}
if ( $email != "success" ){
$pageElements['EMAIL_ERROR'] = 'TRUE';
}else{
$pageElements['EMAIL_ERROR'] = 'FALSE';
}
if ( isset( $_POST["TaC"] ) ){
$pageElements['TAC_ERROR'] = 'FALSE';
}else{
$pageElements['TAC_ERROR'] = 'TRUE';
}
helpers :: loadtemplate( 'register', $pageElements );
} }
} }
@ -73,6 +33,6 @@ function write_user(){
// add user locally here // add user locally here
users :: add_user(); print('Awesome');
} }

View file

@ -21,7 +21,7 @@
<div class="controls"> <div class="controls">
<div class="input-prepend"> <div class="input-prepend">
<span class="add-on"><i class="icon-user"></i></span> <span class="add-on"><i class="icon-user"></i></span>
<input type="text" class="input-xlarge" id="Username" name="Username" placeholder="{$username_default}" {if isset($Username)}value="{$Username}"{/if} rel="popover" data-content="{$username_tooltip}" data-original-title="{$username_default}"> <input type="text" class="input-xlarge" id="Username" name="Username" placeholder="{$username_default}" {if isset($prevUsername)}value="{$prevUsername}"{/if} rel="popover" data-content="{$username_tooltip}" data-original-title="{$username_default}">
</div> </div>
</div> </div>
</div> </div>
@ -33,7 +33,7 @@
<div class="controls"> <div class="controls">
<div class="input-prepend"> <div class="input-prepend">
<span class="add-on"><i class="icon-lock"></i></span> <span class="add-on"><i class="icon-lock"></i></span>
<input type="Password" id="Password" class="input-xlarge" name="Password" placeholder="{$password_default}" {if isset($Password)}value="{$Password}"{/if} rel="popover" data-content="{$password_tooltip}" data-original-title="{$password_default}"> <input type="Password" id="Password" class="input-xlarge" name="Password" placeholder="{$password_default}" {if isset($prevPassword)}value="{$prevPassword}"{/if} rel="popover" data-content="{$password_tooltip}" data-original-title="{$password_default}">
</div> </div>
</div> </div>
</div> </div>
@ -44,7 +44,7 @@
<div class="controls"> <div class="controls">
<div class="input-prepend"> <div class="input-prepend">
<span class="add-on"><i class="icon-lock"></i></span> <span class="add-on"><i class="icon-lock"></i></span>
<input type="Password" id="ConfirmPass" class="input-xlarge" name="ConfirmPass" placeholder="{$cpassword_default}" {if isset($ConfirmPass)}value="{$ConfirmPass}"{/if} rel="popover" data-content="{$cpassword_tooltip}" data-original-title="{$cpassword_default}"> <input type="Password" id="ConfirmPass" class="input-xlarge" name="ConfirmPass" placeholder="{$cpassword_default}" {if isset($prevConfirmPass)}value="{$prevConfirmPass}"{/if} rel="popover" data-content="{$cpassword_tooltip}" data-original-title="{$cpassword_default}">
</div> </div>
</div> </div>
</div> </div>
@ -55,7 +55,7 @@
<div class="controls"> <div class="controls">
<div class="input-prepend"> <div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span> <span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" class="input-xlarge" id="Email" name="Email" placeholder="{$email_default}" {if isset($Email)}value="{$Email}"{/if} rel="popover" data-content="{$email_tooltip}" data-original-title="{$email_default}"> <input type="text" class="input-xlarge" id="Email" name="Email" placeholder="{$email_default}" {if isset($prevEmail)}value="{$prevEmail}"{/if} rel="popover" data-content="{$email_tooltip}" data-original-title="{$email_default}">
</div> </div>
</div> </div>
</div> </div>
@ -84,7 +84,7 @@
</div> </div>
{/if} {/if}
{if isset($CPASSWORD_ERROR) and $CPASSWORD_ERROR eq "TRUE"} {if isset($CPASSWORD_ERROR) and $CPASSWORD_ERROR eq "TRUE" and $CPASSWORD != ""}
<div class="alert alert-error"> <div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button> <button type="button" class="close" data-dismiss="alert">×</button>
<strong>Confirmation Password Error</strong> {$CPASSWORD} <strong>Confirmation Password Error</strong> {$CPASSWORD}