dblayer + refactored the db parts

This commit is contained in:
Quitta 2013-06-27 18:06:09 +02:00
parent 229f76c3e8
commit 278dbb23ff
6 changed files with 71 additions and 111 deletions

View file

@ -0,0 +1,32 @@
<?php
class DBLayer{
private $PDO;
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);
}
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;
}
}

View file

@ -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');

View file

@ -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);

View file

@ -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

View file

@ -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

View file

@ -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);