From 78dedd874e07faee752593f2e2c044b49cea0c45 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 18 Mar 2016 16:59:20 +0800 Subject: [PATCH] added AdaptedDatabase class includes reusable code of data adapting --- includes/AdaptedDatabase.class.php | 69 ++++++++++++++++++++++++++++++ includes/AuthmeDatabase.class.php | 62 +-------------------------- includes/CrazyDatabase.class.php | 64 +-------------------------- includes/DiscuzDatabase.class.php | 63 +-------------------------- 4 files changed, 75 insertions(+), 183 deletions(-) create mode 100644 includes/AdaptedDatabase.class.php diff --git a/includes/AdaptedDatabase.class.php b/includes/AdaptedDatabase.class.php new file mode 100644 index 00000000..ad99d6ed --- /dev/null +++ b/includes/AdaptedDatabase.class.php @@ -0,0 +1,69 @@ +table_name = Config::get('data_table_name'); + $this->column_uname = Config::get('data_column_uname'); + $this->column_passwd = Config::get('data_column_passwd'); + $this->column_ip = Config::get('data_column_ip'); + } + + public function createRecord($username, $password, $ip) { + $sql = "INSERT INTO ".$this->table_name." (".$this->column_uname.", ".$this->column_passwd.", ".$this->column_ip.") + VALUES ('$username', '$password', '$ip')"; + return $this->query($sql); + } + + public function sync($username) { + $exist_in_bs_table = $this->checkRecordExist('username', $username); + $exist_in_data_table = ($this->query("SELECT * FROM ".$this->table_name." + WHERE ".$this->column_uname."='$username'")->num_rows) ? true : false; + + if ($exist_in_bs_table && !$exist_in_data_table) { + $result = $this->select('username', $username); + $this->createRecord($username, $result['password'], $result['ip']); + return $this->sync($username); + } + + if (!$exist_in_bs_table && $exist_in_data_table) { + $result = $this->query("SELECT * FROM ".$this->table_name." + WHERE ".$this->column_uname."='$username'")->fetch_array(); + $this->insert(array( + "uname" => $username, + "passwd" => $result[$this->column_passwd], + "ip" => $result[$this->column_ip] + )); + return $this->sync($username); + } + + if (!($exist_in_bs_table || $exist_in_data_table)) + return false; + + if ($exist_in_bs_table && $exist_in_data_table) { + $passwd1 = $this->select('username', $username)['password']; + $passwd2 = $this->query("SELECT * FROM ".$this->table_name." + WHERE ".$this->column_uname."='$username'")->fetch_array()[$this->column_passwd]; + if ($passwd1 == $passwd2) { + return true; + } else { + // sync password + $this->update($username, 'password', $passwd2); + return $this->sync($username); + } + } + + } +} diff --git a/includes/AuthmeDatabase.class.php b/includes/AuthmeDatabase.class.php index 766c1267..67e70525 100644 --- a/includes/AuthmeDatabase.class.php +++ b/includes/AuthmeDatabase.class.php @@ -3,24 +3,11 @@ * @Author: printempw * @Date: 2016-03-13 11:59:32 * @Last Modified by: printempw - * @Last Modified time: 2016-03-18 16:40:24 + * @Last Modified time: 2016-03-18 16:55:57 */ -class AuthmeDatabase extends Database implements EncryptInterface, SyncInterface +class AuthmeDatabase extends AdaptedDatabase { - protected $table_name; - protected $column_uname; - protected $column_passwd; - protected $column_ip; - - function __construct() { - parent::__construct(); - $this->table_name = Config::get('data_table_name'); - $this->column_uname = Config::get('data_column_uname'); - $this->column_passwd = Config::get('data_column_passwd'); - $this->column_ip = Config::get('data_column_ip'); - } - /** * Default SHA256 encryption method for Authme * @@ -32,49 +19,4 @@ class AuthmeDatabase extends Database implements EncryptInterface, SyncInterface return $encrypt; } - public function createRecord($username, $password, $ip) { - $sql = "INSERT INTO ".$this->table_name." (".$this->column_uname.", ".$this->column_passwd.", ".$this->column_ip.") - VALUES ('$username', '$password', '$ip')"; - return $this->query($sql); - } - - public function sync($username) { - $exist_in_bs_table = $this->checkRecordExist('username', $username); - $exist_in_authme_table = ($this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->num_rows) ? true : false; - - if ($exist_in_bs_table && !$exist_in_authme_table) { - $result = $this->select('username', $username); - $this->createRecord($username, $result['password'], $result['ip']); - return $this->sync($username); - } - - if (!$exist_in_bs_table && $exist_in_authme_table) { - $result = $this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->fetch_array(); - $this->insert(array( - "uname" => $username, - "passwd" => $result[$this->column_passwd], - "ip" => $result[$this->column_ip] - )); - return $this->sync($username); - } - - if (!($exist_in_bs_table || $exist_in_authme_table)) - return false; - - if ($exist_in_bs_table && $exist_in_authme_table) { - $passwd1 = $this->select('username', $username)['password']; - $passwd2 = $this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->fetch_array()[$this->column_passwd]; - if ($passwd1 == $passwd2) { - return true; - } else { - // sync password - $this->update($username, 'password', $passwd2); - return $this->sync($username); - } - } - - } } diff --git a/includes/CrazyDatabase.class.php b/includes/CrazyDatabase.class.php index 821b340b..86c2037c 100644 --- a/includes/CrazyDatabase.class.php +++ b/includes/CrazyDatabase.class.php @@ -3,24 +3,11 @@ * @Author: printempw * @Date: 2016-03-13 12:15:08 * @Last Modified by: printempw - * @Last Modified time: 2016-03-18 16:41:05 + * @Last Modified time: 2016-03-18 16:56:29 */ -class CrazyDatabase extends Database implements EncryptInterface, SyncInterface +class CrazyDatabase extends AdaptedDatabase { - protected $table_name; - protected $column_uname; - protected $column_passwd; - protected $column_ip; - - function __construct() { - parent::__construct(); - $this->table_name = Config::get('data_table_name'); - $this->column_uname = Config::get('data_column_uname'); - $this->column_passwd = Config::get('data_column_passwd'); - $this->column_ip = Config::get('data_column_ip'); - } - /** * Fucking CrazyCrypt1 * @@ -35,51 +22,4 @@ class CrazyDatabase extends Database implements EncryptInterface, SyncInterface return $encrypt; } - public function createRecord($username, $password, $ip) { - $sql = "INSERT INTO ".$this->table_name." (".$this->column_uname.", ".$this->column_passwd.", ".$this->column_ip.") - VALUES ('$username', '$password', '$ip')"; - return $this->query($sql); - } - - public function sync($username) { - $exist_in_bs_table = $this->checkRecordExist('username', $username); - $exist_in_crazy_table = ($this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->num_rows) ? true : false; - - if ($exist_in_bs_table && !$exist_in_crazy_table) { - $result = $this->select('username', $username); - $this->createRecord($username, $result['password'], $result['ip']); - return $this->sync($username); - } - - if (!$exist_in_bs_table && $exist_in_crazy_table) { - $result = $this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->fetch_array(); - $this->insert(array( - "uname" => $username, - "passwd" => $result[$this->column_passwd], - "ip" => $result[$this->column_ip] - )); - return $this->sync($username); - } - - if (!($exist_in_bs_table || $exist_in_crazy_table)) - return false; - - if ($exist_in_bs_table && $exist_in_crazy_table) { - $passwd1 = $this->select('username', $username)['password']; - $passwd2 = $this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->fetch_array()[$this->column_passwd]; - if ($passwd1 == $passwd2) { - return true; - } else { - // sync password - $this->update($username, 'password', $passwd2); - return $this->sync($username); - } - } - - } - - } diff --git a/includes/DiscuzDatabase.class.php b/includes/DiscuzDatabase.class.php index c6435f9c..b76d87eb 100644 --- a/includes/DiscuzDatabase.class.php +++ b/includes/DiscuzDatabase.class.php @@ -3,24 +3,11 @@ * @Author: printempw * @Date: 2016-03-13 14:59:32 * @Last Modified by: printempw - * @Last Modified time: 2016-03-18 16:42:37 + * @Last Modified time: 2016-03-18 16:56:46 */ -class DiscuzDatabase extends Database implements EncryptInterface, SyncInterface +class DiscuzDatabase extends AdaptedDatabase { - protected $table_name; - protected $column_uname; - protected $column_passwd; - protected $column_ip; - - function __construct() { - parent::__construct(); - $this->table_name = Config::get('data_table_name'); - $this->column_uname = Config::get('data_column_uname'); - $this->column_passwd = Config::get('data_column_passwd'); - $this->column_ip = Config::get('data_column_ip'); - } - /** * Discuz's Fucking dynamic salt */ @@ -31,50 +18,4 @@ class DiscuzDatabase extends Database implements EncryptInterface, SyncInterface return $encrypt; } - public function createRecord($username, $password, $ip) { - $sql = "INSERT INTO ".$this->table_name." (".$this->column_uname.", ".$this->column_passwd.", ".$this->column_ip.") - VALUES ('$username', '$password', '$ip')"; - return $this->query($sql); - } - - public function sync($username) { - $exist_in_bs_table = $this->checkRecordExist('username', $username); - $exist_in_discuz_table = ($this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->num_rows) ? true : false; - - if ($exist_in_bs_table && !$exist_in_discuz_table) { - $result = $this->select('username', $username); - $this->createRecord($username, $result['password'], $result['ip']); - return $this->sync($username); - } - - if (!$exist_in_bs_table && $exist_in_discuz_table) { - $result = $this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->fetch_array(); - $this->insert(array( - "uname" => $username, - "passwd" => $result[$this->column_passwd], - "ip" => $result[$this->column_ip] - )); - return $this->sync($username); - } - - if (!($exist_in_bs_table || $exist_in_discuz_table)) - return false; - - if ($exist_in_bs_table && $exist_in_discuz_table) { - $passwd1 = $this->select('username', $username)['password']; - $passwd2 = $this->query("SELECT * FROM ".$this->table_name." - WHERE ".$this->column_uname."='$username'")->fetch_array()[$this->column_passwd]; - if ($passwd1 == $passwd2) { - return true; - } else { - // sync password - $this->update($username, 'password', $passwd2); - return $this->sync($username); - } - } - - } - }