messing around and fixing throwing of exceptions in DBLayer

This commit is contained in:
Quitta 2013-06-28 06:18:23 +02:00
parent f6511b256f
commit 197457c356
3 changed files with 105 additions and 80 deletions

View file

@ -5,28 +5,40 @@ class DBLayer{
function __construct($db) function __construct($db)
{ {
$dsn = "mysql:"; try{
$dsn .= "host=". $db['host'].";"; $dsn = "mysql:";
$dsn .= "dbname=". $db['name'].";"; $dsn .= "host=". $db['host'].";";
$dsn .= "port=". $db['port'].";"; $dsn .= "dbname=". $db['name'].";";
$dsn .= "port=". $db['port'].";";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, $opt = array(
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
); PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
$this->PDO = new PDO($dsn,$db['user'],$db['pass'], $opt); );
$this->PDO = new PDO($dsn,$db['user'],$db['pass'], $opt);
}catch (PDOException $e) {
throw $e;
}
} }
public function executeWithoutParams($query){ public function executeWithoutParams($query){
$statement = $this->PDO->prepare($query); try{
$statement->execute(); $statement = $this->PDO->prepare($query);
return $statement; $statement->execute();
return $statement;
}catch (PDOException $e) {
throw $e;
}
} }
public function execute($query,$params){ public function execute($query,$params){
$statement = $this->PDO->prepare($query); try{
$statement->execute($params); $statement = $this->PDO->prepare($query);
return $statement; $statement->execute($params);
return $statement;
}catch (PDOException $e) {
throw $e;
}
} }
} }

View file

@ -1,19 +1,19 @@
<?php <?php
class Users{ class Users{
public function add_user(){ /**
* Function check_register
helpers :: loadtemplate( 'register', $pageElements ); *
* @takes $array with username,password and email
} * @return string Info: Returns a string, if input data is valid then "success" is returned, else an array with errors
*/
public function check_Register(){ public function check_Register($values){
// check values // check values
if ( isset( $_POST["Username"] ) and isset( $_POST["Password"] ) and isset( $_POST["Email"] ) ){ if ( isset( $values["Username"] ) and isset( $values["Password"] ) and isset( $values["Email"] ) ){
$user = Users :: checkUser( $_POST["Username"] ); $user = Users :: checkUser( $values["Username"] );
$pass = Users :: checkPassword( $_POST["Password"] ); $pass = Users :: checkPassword( $values["Password"] );
$cpass = Users :: confirmPassword($pass); $cpass = Users :: confirmPassword($pass);
$email = Users :: checkEmail( $_POST["Email"] ); $email = Users :: checkEmail( $values["Email"] );
}else{ }else{
$user = ""; $user = "";
$pass = ""; $pass = "";
@ -70,29 +70,26 @@ class Users{
* @return string Info: Returns a string based on if the username is valid, if valid then "success" is returned * @return string Info: Returns a string based on if the username is valid, if valid then "success" is returned
*/ */
public function checkUser( $username ) public function checkUser( $username )
{ {
if ( isset( $username ) ){ if ( isset( $username ) ){
if ( strlen( $username ) > 12 ){ if ( strlen( $username ) > 12 ){
return "Username must be no more than 12 characters."; return "Username must be no more than 12 characters.";
}elseif ( strlen( $username ) < 5 ){ }else if ( strlen( $username ) < 5 ){
return "Username must be 5 or more characters."; return "Username must be 5 or more characters.";
}elseif ( !preg_match( '/^[a-z0-9\.]*$/', $username ) ){ }else if ( !preg_match( '/^[a-z0-9\.]*$/', $username ) ){
return "Username can only contain numbers and letters."; return "Username can only contain numbers and letters.";
}elseif ( $username == "" ){ }else if ( $username == "" ){
return "You have to fill in a username"; return "You have to fill in a username";
/*}elseif ($this->dbs->execute("SELECT * FROM user WHERE Login = :name",array('name' => $username))->rowCount()){
/*}elseif ( sql :: db_query( "SELECT COUNT(*) FROM {users} WHERE name = :name", array( return "Username " . $username . " is in use.";*/
':name' => $username }else{
) ) -> fetchField() ){ return "success";
return "Username " . $username . " is in use.";*/ }
}else{ }
return "success"; return "fail";
} }
}else{
return "success";
}
return "fail";
}
/** /**
* Function checkPassword * Function checkPassword
* *
@ -114,6 +111,8 @@ class Users{
} }
return "fail"; return "fail";
} }
/** /**
* Function confirmPassword * Function confirmPassword
* *
@ -133,6 +132,8 @@ class Users{
} }
return "fail"; return "fail";
} }
/** /**
* Function checkEmail * Function checkEmail
* *
@ -146,20 +147,23 @@ class Users{
return "Email address is not valid."; return "Email address is not valid.";
}else if($email == ""){ }else if($email == ""){
return "You have to fill in an email address"; return "You have to fill in an email address";
/*}elseif ( $this->dbs->execute("SELECT * FROM user WHERE Email = :email",array('email' => $email))->rowCount()){
return "Email is in use.";*/}
else{
return "success";
} }
/*}elseif ( db_query( "SELECT COUNT(*) FROM {users} WHERE mail = :mail", array( }
':mail' => $email return "fail";
) ) -> fetchField() ){ }
return "Email is in use.";}*/
else{
return "success";
}
}else{
return "success";
}
return "fail";
}
/**
* Function validEmail
*
* @takes $email
* @return true or false depending on if its a valid email format.
*/
public function validEmail( $email ){ public function validEmail( $email ){
$isValid = true; $isValid = true;
$atIndex = strrpos( $email, "@" ); $atIndex = strrpos( $email, "@" );
@ -203,6 +207,14 @@ class Users{
return $isValid; return $isValid;
} }
/**
* Function generateSALT
*
* @takes $length, which is by default 2
* @return a random salt of 2 chars
*/
public function generateSALT( $length = 2 ) public function generateSALT( $length = 2 )
{ {
// start with a blank salt // start with a blank salt
@ -237,21 +249,25 @@ class Users{
} }
function createUser($values){
/**
$libdb = $values['db']['lib']; * Function create
$sharddb = $values['db']['shard']; *
* @takes $array with name,pass and mail
* @return ok if it's get correctly added to the shard, else return lib offline and put in libDB, if libDB is also offline return liboffline.
*/
function createUser($values){
try { try {
//make connection with and put into shard db //make connection with and put into shard db
$dbs = new DBLayer($sharddb); global $cfg;
$dbs = new DBLayer($cfg['db']['shard']);
$dbs->execute("INSERT INTO user (Login, Password, Email) VALUES (:name, :pass, :mail)",$values["params"]); $dbs->execute("INSERT INTO user (Login, Password, Email) VALUES (:name, :pass, :mail)",$values["params"]);
return "ok"; return "ok";
} }
catch (PDOException $e) { catch (PDOException $e) {
//oh noooz, the shard is offline! Put in query queue at ams_lib db! //oh noooz, the shard is offline! Put in query queue at ams_lib db!
try { try {
$dbl = new DBLayer($libdb); $dbl = new DBLayer($cfg['db']['lib']);
$dbl->execute("INSERT INTO ams_querycache (type, query) VALUES (:type, :query)",array("type" => "createUser", $dbl->execute("INSERT INTO ams_querycache (type, query) VALUES (:type, :query)",array("type" => "createUser",
"query" => json_encode(array($values["params"]["name"],$values["params"]["pass"],$values["params"]["mail"])))); "query" => json_encode(array($values["params"]["name"],$values["params"]["pass"],$values["params"]["mail"]))));
return "shardoffline"; return "shardoffline";

View file

@ -1,8 +1,10 @@
<?php <?php
function add_user(){ function add_user(){
$result = Users :: check_Register();
//print_r($result); $params = Array('Username' => $_POST["Username"], 'Password' => $_POST["Password"], 'Email' => $_POST["Email"]);
$result = Users::check_Register($params);
// if all are good then create user // if all are good then create user
if ( $result == "success"){ if ( $result == "success"){
$edit = array( $edit = array(
@ -14,10 +16,8 @@ function add_user(){
'status' => 1, 'status' => 1,
'access' => $_SERVER['REQUEST_TIME'] 'access' => $_SERVER['REQUEST_TIME']
); );
//header( 'Location: email_sent.php' );
$status = write_user( $edit ); $status = write_user( $edit );
$pageElements['status'] = $status; $pageElements['status'] = $status;
//TODO: perhaps send email!
$pageElements['no_visible_elements'] = 'TRUE'; $pageElements['no_visible_elements'] = 'TRUE';
helpers :: loadtemplate( 'register_feedback', $pageElements); helpers :: loadtemplate( 'register_feedback', $pageElements);
exit; exit;
@ -36,9 +36,6 @@ function add_user(){
function write_user($newUser){ function write_user($newUser){
//get the db specifics out of the config file
global $cfg;
//create salt here, because we want it to be the same on the web/server //create salt here, because we want it to be the same on the web/server
$hashpass = crypt($newUser["pass"], Users::generateSALT()); $hashpass = crypt($newUser["pass"], Users::generateSALT());
@ -51,14 +48,14 @@ function write_user($newUser){
//print_r($params); //print_r($params);
//make a $values array for passing all data to the Users::createUser() function. //make a $values array for passing all data to the Users::createUser() function.
$values["params"] = $params; $values["params"] = $params;
$values["db"] = $cfg['db'];
//Create the user on the shard + in case shard is offline put copy of query in query db //Create the user on the shard + in case shard is offline put copy of query in query db
//returns: ok, shardoffline or liboffline //returns: ok, shardoffline or liboffline
$result = Users :: createUser($values); $result = Users::createUser($values);
try{ try{
//make connection with web db and put it in there //make connection with web db and put it in there
global $cfg;
$dbw = new DBLayer($cfg['db']['web']); $dbw = new DBLayer($cfg['db']['web']);
$dbw->execute("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)",$params); $dbw->execute("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)",$params);