messing around and fixing throwing of exceptions in DBLayer

This commit is contained in:
Quitta 2013-06-28 06:18:23 +02:00
parent 1d0c66e401
commit 467250c549
3 changed files with 105 additions and 80 deletions

View file

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

View file

@ -1,19 +1,19 @@
<?php
class Users{
public function add_user(){
helpers :: loadtemplate( 'register', $pageElements );
}
public function check_Register(){
/**
* Function check_register
*
* @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($values){
// check values
if ( isset( $_POST["Username"] ) and isset( $_POST["Password"] ) and isset( $_POST["Email"] ) ){
$user = Users :: checkUser( $_POST["Username"] );
$pass = Users :: checkPassword( $_POST["Password"] );
if ( isset( $values["Username"] ) and isset( $values["Password"] ) and isset( $values["Email"] ) ){
$user = Users :: checkUser( $values["Username"] );
$pass = Users :: checkPassword( $values["Password"] );
$cpass = Users :: confirmPassword($pass);
$email = Users :: checkEmail( $_POST["Email"] );
$email = Users :: checkEmail( $values["Email"] );
}else{
$user = "";
$pass = "";
@ -80,19 +80,16 @@ class Users{
return "Username can only contain numbers and letters.";
}else if ( $username == "" ){
return "You have to fill in a username";
/*}elseif ( sql :: db_query( "SELECT COUNT(*) FROM {users} WHERE name = :name", array(
':name' => $username
) ) -> fetchField() ){
/*}elseif ($this->dbs->execute("SELECT * FROM user WHERE Login = :name",array('name' => $username))->rowCount()){
return "Username " . $username . " is in use.";*/
}else{
return "success";
}
}else{
return "success";
}
return "fail";
}
/**
* Function checkPassword
*
@ -114,6 +111,8 @@ class Users{
}
return "fail";
}
/**
* Function confirmPassword
*
@ -133,6 +132,8 @@ class Users{
}
return "fail";
}
/**
* Function checkEmail
*
@ -146,20 +147,23 @@ class Users{
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(
':mail' => $email
) ) -> fetchField() ){
return "Email is in use.";}*/
/*}elseif ( $this->dbs->execute("SELECT * FROM user WHERE Email = :email",array('email' => $email))->rowCount()){
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 ){
$isValid = true;
$atIndex = strrpos( $email, "@" );
@ -203,6 +207,14 @@ class Users{
return $isValid;
}
/**
* Function generateSALT
*
* @takes $length, which is by default 2
* @return a random salt of 2 chars
*/
public function generateSALT( $length = 2 )
{
// start with a blank salt
@ -237,21 +249,25 @@ class Users{
}
/**
* Function create
*
* @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){
$libdb = $values['db']['lib'];
$sharddb = $values['db']['shard'];
try {
//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"]);
return "ok";
}
catch (PDOException $e) {
//oh noooz, the shard is offline! Put in query queue at ams_lib db!
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",
"query" => json_encode(array($values["params"]["name"],$values["params"]["pass"],$values["params"]["mail"]))));
return "shardoffline";

View file

@ -1,8 +1,10 @@
<?php
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 ( $result == "success"){
$edit = array(
@ -14,10 +16,8 @@ function add_user(){
'status' => 1,
'access' => $_SERVER['REQUEST_TIME']
);
//header( 'Location: email_sent.php' );
$status = write_user( $edit );
$pageElements['status'] = $status;
//TODO: perhaps send email!
$pageElements['no_visible_elements'] = 'TRUE';
helpers :: loadtemplate( 'register_feedback', $pageElements);
exit;
@ -36,9 +36,6 @@ function add_user(){
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
$hashpass = crypt($newUser["pass"], Users::generateSALT());
@ -51,7 +48,6 @@ function write_user($newUser){
//print_r($params);
//make a $values array for passing all data to the Users::createUser() function.
$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
//returns: ok, shardoffline or liboffline
@ -59,6 +55,7 @@ function write_user($newUser){
try{
//make connection with web db and put it in there
global $cfg;
$dbw = new DBLayer($cfg['db']['web']);
$dbw->execute("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)",$params);