register refactoring + small bug fixes that went unnoticed before

--HG--
branch : quitta-gsoc-2013
This commit is contained in:
Quitta 2013-06-19 06:50:08 +02:00
parent bae15e3558
commit fb622a9eb7
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,15 +120,19 @@ 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{ }else if ($_POST["ConfirmPass"]==""){
return "You have to fill in the confirmation password.";
}else if($pass_result != "success"){
return;
}else{
return "success"; return "success";
} }
return "fail"; return "fail";
} }
/** /**
* Function checkEmail * Function checkEmail
* *
@ -77,13 +142,16 @@ class Users{
public function checkEmail( $email ) public function checkEmail( $email )
{ {
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,70 +1,30 @@
<?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);
{ // if all are good then create user
// check values if ( $result == "success"){
$user = users :: checkUser( $_POST["Username"] ); $edit = array(
$pass = users :: checkPassword( $_POST["Password"] ); 'name' => $_POST["Username"],
$cpass = users :: confirmPassword(); 'pass' => $_POST["Password"],
$email = users :: checkEmail( $_POST["Email"] ); 'mail' => $_POST["Email"],
'init' => $_POST["Email"],
'unhashpass' => $_POST["Password"],
'status' => 1,
'access' => REQUEST_TIME
);
//header( 'Location: email_sent.php' );
write_user( $edit );
exit;
}else{ }else{
$user = ""; // pass error
$pass = ""; $result['prevUsername'] = $_POST["Username"];
$cpass = ""; $result['prevPassword'] = $_POST["Password"];
$email = ""; $result['prevConfirmPass'] = $_POST["ConfirmPass"];
} $result['prevEmail'] = $_POST["Email"];
// if all are good then create user $result['no_visible_elements'] = 'TRUE';
if ( ( $user == "success" ) and ( $pass == "success" ) and ( $cpass == "success" ) and ( $email == "success" ) and ( isset( $_POST["TaC"] ) ) ){ helpers :: loadtemplate( 'register', $result);
$edit = array(
'name' => $_POST["Username"],
'pass' => $_POST["Password"],
'mail' => $_POST["Email"],
'init' => $_POST["Email"],
'unhashpass' => $_POST["Password"],
'status' => 1,
'access' => REQUEST_TIME
);
header( 'Location: email_sent.php' );
write_user( $edit );
exit;
}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';
}
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}