From 60f3d1c6e3c19801e86ba5d72cb67e1a5ee999d8 Mon Sep 17 00:00:00 2001 From: Quitta Date: Thu, 27 Jun 2013 18:06:09 +0200 Subject: [PATCH] dblayer + refactored the db parts --- .../ryzom_ams/ams_lib/autoload/dblayer.php | 32 +++++++++++++++ .../ryzom_ams/ams_lib/autoload/sync.php | 38 +++++------------- .../ryzom_ams/ams_lib/autoload/users.php | 27 ++++--------- .../tools/server/ryzom_ams/www/config.php | 33 +++++++--------- .../ryzom_ams/www/html/inc/add_user.php | 39 +++---------------- .../server/ryzom_ams/www/html/inc/login.php | 13 ++----- 6 files changed, 71 insertions(+), 111 deletions(-) create mode 100644 code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php new file mode 100644 index 000000000..d323dc4e4 --- /dev/null +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php @@ -0,0 +1,32 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC + ); + $this->PDO = new PDO($dsn,$db['user'],$db['pass'], $opt); + } + + public function executeWithoutParams($query){ + $statement = $this->PDO->prepare($query); + $statement->execute(); + return $statement; + } + + public function execute($query,$params){ + $statement = $this->PDO->prepare($query); + $statement->execute($params); + return $statement; + } + +} \ No newline at end of file diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/sync.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/sync.php index 302d4dc14..170a1c4bf 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/sync.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/sync.php @@ -14,44 +14,24 @@ class Sync{ */ static public function syncdata () { - global $LIBDBHOST; - global $LIBDBPORT; - global $LIBDBNAME; - global $LIBDBUSERNAME; - global $LIBDBPASSWORD; - - global $SHARDDBHOST; - global $SHARDDBPORT; - global $SHARDDBNAME; - global $SHARDDBUSERNAME; - global $SHARDDBPASSWORD; + global $cfg; try { - $dbl = new PDO("mysql:host=$LIBDBHOST;port=$LIBDBPORT;dbname=$LIBDBNAME", $LIBDBUSERNAME, $LIBDBPASSWORD); - $dbl->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $statement = $dbl->prepare("SELECT * FROM ams_querycache"); - $statement->execute(); + $dbl = new DBLayer($cfg['db']['lib']); + $statement = $dbl->executeWithoutParams("SELECT * FROM ams_querycache"); $rows = $statement->fetchAll(); - - $dbs = new PDO("mysql:host=$SHARDDBHOST;port=$SHARDDBPORT;dbname=$SHARDDBNAME", $SHARDDBUSERNAME, $SHARDDBPASSWORD); - $dbs->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - + $dbs = new DBLayer($cfg['db']['shard']); foreach ($rows as $record) { switch($record['type']) { case 'createPermissions': case 'user_edit': case 'createUser': - $query = json_decode($record['query']); - //make connection with and put into shard db - $statement = $dbs->prepare("INSERT INTO user (Login, Password, Email) VALUES (?, ?, ?)"); - $statement->execute($query); - - $statement = $dbl->prepare("DELETE FROM ams_querycache WHERE SID=:SID"); - $query = array('SID' => $record['SID']); - $statement->execute($query); - - + $decode = json_decode($record['query']); + $query = array('login' => $decode[0], 'pass' => $decode[1], 'mail' => $decode[2] ); + //make connection with and put into shard db & delete from the lib + $dbs->execute("INSERT INTO user (Login, Password, Email) VALUES (:login, :pass, :mail)",$query); + $dbl->execute("DELETE FROM ams_querycache WHERE SID=:SID",array('SID' => $record['SID'])); } } print('Syncing completed'); diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/users.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/users.php index 1d6c62177..fab797681 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/users.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/users.php @@ -258,34 +258,21 @@ class Users{ function createUser($values){ - $libhost = $values["libhost"]; - $libport = $values["libport"]; - $libdbname = $values["libdbname"]; - $libusername = $values["libusername"]; - $libpassword = $values["libpassword"]; - - $shardhost = $values["shardhost"]; - $shardport = $values["shardport"]; - $sharddbname = $values["sharddbname"]; - $shardusername = $values["shardusername"]; - $shardpassword = $values["shardpassword"]; + $libdb = $values['db']['lib']; + $sharddb = $values['db']['shard']; try { //make connection with and put into shard db - $dbs = new PDO("mysql:host=$shardhost;port=$shardport;dbname=$sharddbname", $shardusername, $shardpassword); - $dbs->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $statement = $dbs->prepare("INSERT INTO user (Login, Password, Email) VALUES (:name, :pass, :mail)"); - $statement->execute($values["params"]); + $dbs = new DBLayer($sharddb); + $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 PDO("mysql:host=$libhost;port=$libport;dbname=$libdbname", $libusername, $libpassword); - $dbl->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $params = array("type" => "createUser","query" => json_encode(array($values["params"]["name"],$values["params"]["pass"],$values["params"]["mail"]))); - $statement = $dbl->prepare("INSERT INTO ams_querycache (type, query) VALUES (:type, :query)"); - $statement->execute($params); + $dbl = new DBLayer($libdb); + $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"; }catch (PDOException $e) { print_r($e); diff --git a/code/ryzom/tools/server/ryzom_ams/www/config.php b/code/ryzom/tools/server/ryzom_ams/www/config.php index 747ae8414..b896879a9 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/config.php +++ b/code/ryzom/tools/server/ryzom_ams/www/config.php @@ -7,26 +7,23 @@ // where we can find the mysql database //----------------------------------------------------------------------------------------- -//the www db -$WEBDBHOST = 'localhost'; -$WEBDBPORT = '3306'; -$WEBDBNAME = 'ryzom_ams'; -$WEBDBUSERNAME = 'root'; -$WEBDBPASSWORD = 'lol123' ; +$cfg['db']['web']['host'] = 'localhost'; +$cfg['db']['web']['port'] = '3306'; +$cfg['db']['web']['name'] = 'ryzom_ams'; +$cfg['db']['web']['user'] = 'root'; +$cfg['db']['web']['pass'] = 'lol123'; -//the ams_lib db -$LIBDBHOST = 'localhost'; -$LIBDBPORT = '3306'; -$LIBDBNAME = 'ryzom_ams_lib'; -$LIBDBUSERNAME = 'root'; -$LIBDBPASSWORD = 'lol123' ; +$cfg['db']['lib']['host'] = 'localhost'; +$cfg['db']['lib']['port'] = '3306'; +$cfg['db']['lib']['name'] = 'ryzom_ams_lib'; +$cfg['db']['lib']['user'] = 'root'; +$cfg['db']['lib']['pass'] = 'lol123'; -//the shard db -$SHARDDBHOST = 'localhost' ; -$SHARDDBPORT = '3306'; -$SHARDDBNAME = 'nel' ; -$SHARDDBUSERNAME = 'shard' ; -$SHARDDBPASSWORD = '' ; +$cfg['db']['shard']['host'] = 'localhost'; +$cfg['db']['shard']['port'] = '3306'; +$cfg['db']['shard']['name'] = 'nel'; +$cfg['db']['shard']['user'] = 'shard'; +$cfg['db']['shard']['pass'] = ''; //----------------------------------------------------------------------------------------- // If true= the server will add automatically unknown user in the database diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/inc/add_user.php b/code/ryzom/tools/server/ryzom_ams/www/html/inc/add_user.php index 64733ddb2..8f3a25d27 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/inc/add_user.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/inc/add_user.php @@ -37,23 +37,7 @@ function add_user(){ function write_user($newUser){ //get the db specifics out of the config file - global $WEBDBHOST; - global $WEBDBPORT; - global $WEBDBNAME; - global $WEBDBUSERNAME; - global $WEBDBPASSWORD; - - global $LIBDBHOST; - global $LIBDBPORT; - global $LIBDBNAME; - global $LIBDBUSERNAME; - global $LIBDBPASSWORD; - - global $SHARDDBHOST; - global $SHARDDBPORT; - global $SHARDDBNAME; - global $SHARDDBUSERNAME; - global $SHARDDBPASSWORD; + global $cfg; //create salt here, because we want it to be the same on the web/server $hashpass = crypt($newUser["pass"], Users::generateSALT()); @@ -67,29 +51,16 @@ function write_user($newUser){ //print_r($params); //make a $values array for passing all data to the Users::createUser() function. $values["params"] = $params; - $values["libhost"] = $LIBDBHOST; - $values["libport"] = $LIBDBPORT; - $values["libdbname"] = $LIBDBNAME; - $values["libusername"] = $LIBDBUSERNAME; - $values["libpassword"] = $LIBDBPASSWORD ; - - $values["shardhost"] = $SHARDDBHOST; - $values["shardport"] = $SHARDDBPORT; - $values["sharddbname"] = $SHARDDBNAME; - $values["shardusername"] = $SHARDDBUSERNAME; - $values["shardpassword"] = $SHARDDBPASSWORD; - + $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 + //returns: ok, shardoffline or liboffline $result = Users :: createUser($values); try{ //make connection with web db and put it in there - $dbw = new PDO("mysql:host=$WEBDBHOST;port=$WEBDBPORT;dbname=$WEBDBNAME", $WEBDBUSERNAME, $WEBDBPASSWORD); - $dbw->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $statement = $dbw->prepare("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)"); - $statement->execute($params); + $dbw = new DBLayer($cfg['db']['web']); + $dbw->execute("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)",$params); }catch (PDOException $e) { //go to error page or something, because can't access website db diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/inc/login.php b/code/ryzom/tools/server/ryzom_ams/www/html/inc/login.php index c2368747d..055ea442a 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/inc/login.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/inc/login.php @@ -2,18 +2,11 @@ function login(){ - global $WEBDBHOST; - global $WEBDBPORT; - global $WEBDBNAME; - global $WEBDBUSERNAME; - global $WEBDBPASSWORD; + global $cfg; try{ - $dbw = new PDO("mysql:host=$WEBDBHOST;port=$WEBDBPORT;dbname=$WEBDBNAME", $WEBDBUSERNAME, $WEBDBPASSWORD); - $dbw->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - - $statement = $dbw->prepare("SELECT * FROM ams_user WHERE Login=:user"); - $statement->execute(array('user' => $_POST['Username'])); + $dbw = new DBLayer($cfg['db']['web']); + $statement = $dbw->execute("SELECT * FROM ams_user WHERE Login=:user", array('user' => $_POST['Username'])); $row = $statement->fetch(); $salt = substr($row['Password'],0,2);