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)
{
$dsn = "mysql:";
$dsn .= "host=". $db['host'].";";
$dsn .= "dbname=". $db['name'].";";
$dsn .= "port=". $db['port'].";";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$this->PDO = new PDO($dsn,$db['user'],$db['pass'], $opt);
try{
$dsn = "mysql:";
$dsn .= "host=". $db['host'].";";
$dsn .= "dbname=". $db['name'].";";
$dsn .= "port=". $db['port'].";";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
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){
$statement = $this->PDO->prepare($query);
$statement->execute();
return $statement;
public function executeWithoutParams($query){
try{
$statement = $this->PDO->prepare($query);
$statement->execute();
return $statement;
}catch (PDOException $e) {
throw $e;
}
}
public function execute($query,$params){
$statement = $this->PDO->prepare($query);
$statement->execute($params);
return $statement;
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 = "";
@ -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
*/
public function checkUser( $username )
{
if ( isset( $username ) ){
if ( strlen( $username ) > 12 ){
return "Username must be no more than 12 characters.";
}elseif ( strlen( $username ) < 5 ){
return "Username must be 5 or more characters.";
}elseif ( !preg_match( '/^[a-z0-9\.]*$/', $username ) ){
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(
':name' => $username
) ) -> fetchField() ){
return "Username " . $username . " is in use.";*/
}else{
return "success";
}
}else{
return "success";
}
return "fail";
}
{
if ( isset( $username ) ){
if ( strlen( $username ) > 12 ){
return "Username must be no more than 12 characters.";
}else if ( strlen( $username ) < 5 ){
return "Username must be 5 or more characters.";
}else if ( !preg_match( '/^[a-z0-9\.]*$/', $username ) ){
return "Username can only contain numbers and letters.";
}else if ( $username == "" ){
return "You have to fill in a username";
/*}elseif ($this->dbs->execute("SELECT * FROM user WHERE Login = :name",array('name' => $username))->rowCount()){
return "Username " . $username . " is in use.";*/
}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 ( $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
) ) -> fetchField() ){
return "Email is in use.";}*/
else{
return "success";
}
}else{
return "success";
}
return "fail";
}
}
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 createUser($values){
$libdb = $values['db']['lib'];
$sharddb = $values['db']['shard'];
/**
* 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){
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,14 +48,14 @@ 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
$result = Users :: createUser($values);
$result = Users::createUser($values);
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);