dblayer + refactored the db parts
This commit is contained in:
parent
229f76c3e8
commit
278dbb23ff
6 changed files with 71 additions and 111 deletions
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -14,44 +14,24 @@ class Sync{
|
||||||
*/
|
*/
|
||||||
static public function syncdata () {
|
static public function syncdata () {
|
||||||
|
|
||||||
global $LIBDBHOST;
|
global $cfg;
|
||||||
global $LIBDBPORT;
|
|
||||||
global $LIBDBNAME;
|
|
||||||
global $LIBDBUSERNAME;
|
|
||||||
global $LIBDBPASSWORD;
|
|
||||||
|
|
||||||
global $SHARDDBHOST;
|
|
||||||
global $SHARDDBPORT;
|
|
||||||
global $SHARDDBNAME;
|
|
||||||
global $SHARDDBUSERNAME;
|
|
||||||
global $SHARDDBPASSWORD;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$dbl = new PDO("mysql:host=$LIBDBHOST;port=$LIBDBPORT;dbname=$LIBDBNAME", $LIBDBUSERNAME, $LIBDBPASSWORD);
|
$dbl = new DBLayer($cfg['db']['lib']);
|
||||||
$dbl->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$statement = $dbl->executeWithoutParams("SELECT * FROM ams_querycache");
|
||||||
$statement = $dbl->prepare("SELECT * FROM ams_querycache");
|
|
||||||
$statement->execute();
|
|
||||||
$rows = $statement->fetchAll();
|
$rows = $statement->fetchAll();
|
||||||
|
$dbs = new DBLayer($cfg['db']['shard']);
|
||||||
$dbs = new PDO("mysql:host=$SHARDDBHOST;port=$SHARDDBPORT;dbname=$SHARDDBNAME", $SHARDDBUSERNAME, $SHARDDBPASSWORD);
|
|
||||||
$dbs->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
||||||
|
|
||||||
foreach ($rows as $record) {
|
foreach ($rows as $record) {
|
||||||
|
|
||||||
switch($record['type']) {
|
switch($record['type']) {
|
||||||
case 'createPermissions':
|
case 'createPermissions':
|
||||||
case 'user_edit':
|
case 'user_edit':
|
||||||
case 'createUser':
|
case 'createUser':
|
||||||
$query = json_decode($record['query']);
|
$decode = json_decode($record['query']);
|
||||||
//make connection with and put into shard db
|
$query = array('login' => $decode[0], 'pass' => $decode[1], 'mail' => $decode[2] );
|
||||||
$statement = $dbs->prepare("INSERT INTO user (Login, Password, Email) VALUES (?, ?, ?)");
|
//make connection with and put into shard db & delete from the lib
|
||||||
$statement->execute($query);
|
$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']));
|
||||||
$statement = $dbl->prepare("DELETE FROM ams_querycache WHERE SID=:SID");
|
|
||||||
$query = array('SID' => $record['SID']);
|
|
||||||
$statement->execute($query);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print('Syncing completed');
|
print('Syncing completed');
|
||||||
|
|
|
@ -258,34 +258,21 @@ class Users{
|
||||||
|
|
||||||
function createUser($values){
|
function createUser($values){
|
||||||
|
|
||||||
$libhost = $values["libhost"];
|
$libdb = $values['db']['lib'];
|
||||||
$libport = $values["libport"];
|
$sharddb = $values['db']['shard'];
|
||||||
$libdbname = $values["libdbname"];
|
|
||||||
$libusername = $values["libusername"];
|
|
||||||
$libpassword = $values["libpassword"];
|
|
||||||
|
|
||||||
$shardhost = $values["shardhost"];
|
|
||||||
$shardport = $values["shardport"];
|
|
||||||
$sharddbname = $values["sharddbname"];
|
|
||||||
$shardusername = $values["shardusername"];
|
|
||||||
$shardpassword = $values["shardpassword"];
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//make connection with and put into shard db
|
//make connection with and put into shard db
|
||||||
$dbs = new PDO("mysql:host=$shardhost;port=$shardport;dbname=$sharddbname", $shardusername, $shardpassword);
|
$dbs = new DBLayer($sharddb);
|
||||||
$dbs->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$dbs->execute("INSERT INTO user (Login, Password, Email) VALUES (:name, :pass, :mail)",$values["params"]);
|
||||||
$statement = $dbs->prepare("INSERT INTO user (Login, Password, Email) VALUES (:name, :pass, :mail)");
|
|
||||||
$statement->execute($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 PDO("mysql:host=$libhost;port=$libport;dbname=$libdbname", $libusername, $libpassword);
|
$dbl = new DBLayer($libdb);
|
||||||
$dbl->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$dbl->execute("INSERT INTO ams_querycache (type, query) VALUES (:type, :query)",array("type" => "createUser",
|
||||||
$params = 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"]))));
|
||||||
$statement = $dbl->prepare("INSERT INTO ams_querycache (type, query) VALUES (:type, :query)");
|
|
||||||
$statement->execute($params);
|
|
||||||
return "shardoffline";
|
return "shardoffline";
|
||||||
}catch (PDOException $e) {
|
}catch (PDOException $e) {
|
||||||
print_r($e);
|
print_r($e);
|
||||||
|
|
|
@ -7,26 +7,23 @@
|
||||||
// where we can find the mysql database
|
// where we can find the mysql database
|
||||||
//-----------------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------------
|
||||||
|
|
||||||
//the www db
|
$cfg['db']['web']['host'] = 'localhost';
|
||||||
$WEBDBHOST = 'localhost';
|
$cfg['db']['web']['port'] = '3306';
|
||||||
$WEBDBPORT = '3306';
|
$cfg['db']['web']['name'] = 'ryzom_ams';
|
||||||
$WEBDBNAME = 'ryzom_ams';
|
$cfg['db']['web']['user'] = 'root';
|
||||||
$WEBDBUSERNAME = 'root';
|
$cfg['db']['web']['pass'] = 'lol123';
|
||||||
$WEBDBPASSWORD = 'lol123' ;
|
|
||||||
|
|
||||||
//the ams_lib db
|
$cfg['db']['lib']['host'] = 'localhost';
|
||||||
$LIBDBHOST = 'localhost';
|
$cfg['db']['lib']['port'] = '3306';
|
||||||
$LIBDBPORT = '3306';
|
$cfg['db']['lib']['name'] = 'ryzom_ams_lib';
|
||||||
$LIBDBNAME = 'ryzom_ams_lib';
|
$cfg['db']['lib']['user'] = 'root';
|
||||||
$LIBDBUSERNAME = 'root';
|
$cfg['db']['lib']['pass'] = 'lol123';
|
||||||
$LIBDBPASSWORD = 'lol123' ;
|
|
||||||
|
|
||||||
//the shard db
|
$cfg['db']['shard']['host'] = 'localhost';
|
||||||
$SHARDDBHOST = 'localhost' ;
|
$cfg['db']['shard']['port'] = '3306';
|
||||||
$SHARDDBPORT = '3306';
|
$cfg['db']['shard']['name'] = 'nel';
|
||||||
$SHARDDBNAME = 'nel' ;
|
$cfg['db']['shard']['user'] = 'shard';
|
||||||
$SHARDDBUSERNAME = 'shard' ;
|
$cfg['db']['shard']['pass'] = '';
|
||||||
$SHARDDBPASSWORD = '' ;
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------------
|
||||||
// If true= the server will add automatically unknown user in the database
|
// If true= the server will add automatically unknown user in the database
|
||||||
|
|
|
@ -37,23 +37,7 @@ function add_user(){
|
||||||
function write_user($newUser){
|
function write_user($newUser){
|
||||||
|
|
||||||
//get the db specifics out of the config file
|
//get the db specifics out of the config file
|
||||||
global $WEBDBHOST;
|
global $cfg;
|
||||||
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;
|
|
||||||
|
|
||||||
//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());
|
||||||
|
@ -67,29 +51,16 @@ 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["libhost"] = $LIBDBHOST;
|
$values["db"] = $cfg['db'];
|
||||||
$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;
|
|
||||||
|
|
||||||
|
|
||||||
//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
|
||||||
$dbw = new PDO("mysql:host=$WEBDBHOST;port=$WEBDBPORT;dbname=$WEBDBNAME", $WEBDBUSERNAME, $WEBDBPASSWORD);
|
$dbw = new DBLayer($cfg['db']['web']);
|
||||||
$dbw->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$dbw->execute("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)",$params);
|
||||||
$statement = $dbw->prepare("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)");
|
|
||||||
$statement->execute($params);
|
|
||||||
|
|
||||||
}catch (PDOException $e) {
|
}catch (PDOException $e) {
|
||||||
//go to error page or something, because can't access website db
|
//go to error page or something, because can't access website db
|
||||||
|
|
|
@ -2,18 +2,11 @@
|
||||||
|
|
||||||
function login(){
|
function login(){
|
||||||
|
|
||||||
global $WEBDBHOST;
|
global $cfg;
|
||||||
global $WEBDBPORT;
|
|
||||||
global $WEBDBNAME;
|
|
||||||
global $WEBDBUSERNAME;
|
|
||||||
global $WEBDBPASSWORD;
|
|
||||||
|
|
||||||
try{
|
try{
|
||||||
$dbw = new PDO("mysql:host=$WEBDBHOST;port=$WEBDBPORT;dbname=$WEBDBNAME", $WEBDBUSERNAME, $WEBDBPASSWORD);
|
$dbw = new DBLayer($cfg['db']['web']);
|
||||||
$dbw->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$statement = $dbw->execute("SELECT * FROM ams_user WHERE Login=:user", array('user' => $_POST['Username']));
|
||||||
|
|
||||||
$statement = $dbw->prepare("SELECT * FROM ams_user WHERE Login=:user");
|
|
||||||
$statement->execute(array('user' => $_POST['Username']));
|
|
||||||
|
|
||||||
$row = $statement->fetch();
|
$row = $statement->fetch();
|
||||||
$salt = substr($row['Password'],0,2);
|
$salt = substr($row['Password'],0,2);
|
||||||
|
|
Loading…
Reference in a new issue