connection = self::checkConfig();
}
function __destruct() {
$this->connection->close();
}
public static function checkConfig() {
// use error control to hide shitty connect warnings
error_reporting(0);
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
error_reporting(E_ALL ^ E_NOTICE);
if ($conn->connect_error)
throw new E("无法连接至 MySQL 服务器。请确认 config.php 中的配置是否正确:".$conn->connect_error, $conn->connect_errno, true);
if (!self::checkTableExist($conn))
throw new E("数据库中不存在 ".DB_PREFIX."users 或 ".DB_PREFIX."options 表。请先访问 /setup 进行安装。", -1, true);
if (!is_dir(BASE_DIR."/textures/"))
throw new E("textures 文件夹不存在。请先访问 /setup 进行安装,或者手动放置一个。", -1, true);
$conn->query("SET names 'utf8'");
return $conn;
}
public static function checkTableExist($conn) {
$sql = "SELECT table_name FROM
`INFORMATION_SCHEMA`.`TABLES` WHERE (table_name ='".DB_PREFIX."users'
OR table_name ='".DB_PREFIX."options') AND TABLE_SCHEMA='".DB_NAME."'";
if ($conn->query($sql)->num_rows != 2)
return false;
return true;
}
public function query($sql) {
$result = $this->connection->query($sql);
if (!$this->connection->error) {
return $result;
}
throw new E("Database query error: ".$this->connection->error, -1);
}
public function fetchArray($sql) {
return $this->query($sql)->fetch_array();
}
public function select($key, $value) {
return $this->fetchArray("SELECT * FROM ".DB_PREFIX."users WHERE $key='$value'");
}
public function getNumRows($key, $value) {
$sql = "SELECT * FROM ".DB_PREFIX."users WHERE $key='$value'";
return $this->query($sql)->num_rows;
}
public function getRecordNum() {
$sql = "SELECT * FROM ".DB_PREFIX."users WHERE 1";
return $this->query($sql)->num_rows;
}
public function checkRecordExist($key, $value) {
return ($this->getNumRows($key, $value) != 0) ? true : false;
}
public function insert($array) {
$uname = $array['uname'];
$passwd = $array['passwd'];
$ip = $array['ip'];
$sql = "INSERT INTO ".DB_PREFIX."users (username, password, ip, preference)
VALUES ('$uname', '$passwd', '$ip', 'default')";
return $this->query($sql);
}
public function update($uname, $key, $value) {
return $this->query("UPDATE ".DB_PREFIX."users SET `$key`='$value' WHERE username='$uname'");
}
public function delete($uname) {
return $this->query("DELETE FROM ".DB_PREFIX."users WHERE username='$uname'");
}
public function encryptPassword($raw_passwd, $username="") {
$encrypt = md5($raw_passwd);
return $encrypt;
}
public function sync($username) {
return ($this->checkRecordExist('username', $username)) ? true : false;
}
}