2016-03-13 15:38:34 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @Author: printempw
|
|
|
|
* @Date: 2016-03-13 12:15:08
|
|
|
|
* @Last Modified by: printempw
|
2016-03-18 16:47:25 +08:00
|
|
|
* @Last Modified time: 2016-03-18 16:41:05
|
2016-03-13 15:38:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
class CrazyDatabase extends Database implements EncryptInterface, SyncInterface
|
|
|
|
{
|
2016-03-18 16:47:25 +08:00
|
|
|
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');
|
|
|
|
}
|
2016-03-13 15:38:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fucking CrazyCrypt1
|
|
|
|
*
|
|
|
|
* https://github.com/ST-DDT/CrazyLogin/blob/master/php/Encryptors/CrazyCrypt1.php
|
|
|
|
*/
|
|
|
|
public function encryptPassword($raw_passwd, $username="") {
|
|
|
|
$text = "ÜÄaeut//&/=I " . $raw_passwd . "7421€547" . $username . "__+IÄIH§%NK " . $raw_passwd;
|
|
|
|
$t1 = unpack("H*", $text);
|
|
|
|
$t2 = substr($t1[1], 0, mb_strlen($text, 'UTF-8')*2);
|
|
|
|
$t3 = pack("H*", $t2);
|
|
|
|
$encrypt = hash("sha512", $t3);
|
|
|
|
return $encrypt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createRecord($username, $password, $ip) {
|
2016-03-18 16:47:25 +08:00
|
|
|
$sql = "INSERT INTO ".$this->table_name." (".$this->column_uname.", ".$this->column_passwd.", ".$this->column_ip.")
|
2016-03-13 15:38:34 +08:00
|
|
|
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."
|
2016-03-18 16:47:25 +08:00
|
|
|
WHERE ".$this->column_uname."='$username'")->num_rows) ? true : false;
|
2016-03-13 15:38:34 +08:00
|
|
|
|
|
|
|
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."
|
2016-03-18 16:47:25 +08:00
|
|
|
WHERE ".$this->column_uname."='$username'")->fetch_array();
|
2016-03-13 15:38:34 +08:00
|
|
|
$this->insert(array(
|
|
|
|
"uname" => $username,
|
2016-03-18 16:47:25 +08:00
|
|
|
"passwd" => $result[$this->column_passwd],
|
|
|
|
"ip" => $result[$this->column_ip]
|
2016-03-13 15:38:34 +08:00
|
|
|
));
|
|
|
|
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."
|
2016-03-18 16:47:25 +08:00
|
|
|
WHERE ".$this->column_uname."='$username'")->fetch_array()[$this->column_passwd];
|
2016-03-13 15:38:34 +08:00
|
|
|
if ($passwd1 == $passwd2) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// sync password
|
|
|
|
$this->update($username, 'password', $passwd2);
|
|
|
|
return $this->sync($username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-18 16:47:25 +08:00
|
|
|
|
2016-03-13 15:38:34 +08:00
|
|
|
}
|