2016-02-02 23:53:08 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @Author: printempw
|
|
|
|
* @Date: 2016-02-02 21:59:06
|
2016-03-06 14:12:12 +08:00
|
|
|
* @Last Modified by: printempw
|
2016-03-27 15:02:16 +08:00
|
|
|
* @Last Modified time: 2016-03-27 14:50:39
|
2016-02-02 23:53:08 +08:00
|
|
|
*/
|
|
|
|
|
2016-03-18 17:49:52 +08:00
|
|
|
namespace Database;
|
|
|
|
|
|
|
|
use Database\EncryptInterface;
|
|
|
|
use Database\SyncInterface;
|
|
|
|
use Utils;
|
|
|
|
use Mysqli;
|
2016-03-27 11:38:27 +08:00
|
|
|
use E;
|
2016-03-18 17:49:52 +08:00
|
|
|
|
2016-03-13 15:38:34 +08:00
|
|
|
class Database implements EncryptInterface, SyncInterface
|
2016-02-02 23:53:08 +08:00
|
|
|
{
|
|
|
|
private $connection = null;
|
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
$this->connection = self::checkConfig();
|
|
|
|
}
|
|
|
|
|
2016-03-26 19:05:09 +08:00
|
|
|
function __destruct() {
|
|
|
|
$this->connection->close();
|
|
|
|
}
|
2016-03-18 17:54:19 +08:00
|
|
|
|
2016-03-26 19:05:09 +08:00
|
|
|
public static function checkConfig() {
|
|
|
|
// use error control to hide shitty connect warnings
|
|
|
|
error_reporting(0);
|
2016-03-12 16:46:26 +08:00
|
|
|
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
|
2016-03-26 19:05:09 +08:00
|
|
|
error_reporting(E_ALL ^ E_NOTICE);
|
2016-03-18 17:54:19 +08:00
|
|
|
|
|
|
|
if ($conn->connect_error)
|
2016-03-27 11:38:27 +08:00
|
|
|
throw new E("无法连接至 MySQL 服务器。请确认 config.php 中的配置是否正确:".$conn->connect_error, $conn->connect_errno, true);
|
2016-03-18 18:48:30 +08:00
|
|
|
if (!self::checkTableExist($conn))
|
2016-03-27 15:02:16 +08:00
|
|
|
throw new E("数据库中不存在 ".DB_PREFIX."users 或 ".DB_PREFIX."options 表。请先访问 <a href='./setup'>/setup</a> 进行安装。", -1, true);
|
2016-03-18 17:54:19 +08:00
|
|
|
if (!is_dir(BASE_DIR."/textures/"))
|
2016-03-27 15:02:16 +08:00
|
|
|
throw new E("textures 文件夹不存在。请先访问 <a href='./setup'>/setup</a> 进行安装,或者手动放置一个。", -1, true);
|
2016-03-26 19:05:09 +08:00
|
|
|
|
2016-03-19 11:31:44 +08:00
|
|
|
$conn->query("SET names 'utf8'");
|
2016-02-02 23:53:08 +08:00
|
|
|
return $conn;
|
|
|
|
}
|
|
|
|
|
2016-02-03 00:11:43 +08:00
|
|
|
public static function checkTableExist($conn) {
|
|
|
|
$sql = "SELECT table_name FROM
|
2016-03-18 16:47:25 +08:00
|
|
|
`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)
|
2016-03-18 18:44:17 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
2016-02-03 00:11:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-02 23:53:08 +08:00
|
|
|
public function query($sql) {
|
|
|
|
$result = $this->connection->query($sql);
|
|
|
|
if (!$this->connection->error) {
|
|
|
|
return $result;
|
|
|
|
}
|
2016-03-27 11:38:27 +08:00
|
|
|
throw new E("Database query error: ".$this->connection->error, -1);
|
2016-02-02 23:53:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchArray($sql) {
|
|
|
|
return $this->query($sql)->fetch_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function select($key, $value) {
|
2016-03-13 11:31:46 +08:00
|
|
|
return $this->fetchArray("SELECT * FROM ".DB_PREFIX."users WHERE $key='$value'");
|
2016-02-02 23:53:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getNumRows($key, $value) {
|
2016-03-13 11:31:46 +08:00
|
|
|
$sql = "SELECT * FROM ".DB_PREFIX."users WHERE $key='$value'";
|
2016-02-02 23:53:08 +08:00
|
|
|
return $this->query($sql)->num_rows;
|
|
|
|
}
|
|
|
|
|
2016-03-06 14:12:12 +08:00
|
|
|
public function getRecordNum() {
|
2016-03-13 11:31:46 +08:00
|
|
|
$sql = "SELECT * FROM ".DB_PREFIX."users WHERE 1";
|
2016-03-06 14:12:12 +08:00
|
|
|
return $this->query($sql)->num_rows;
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:53:08 +08:00
|
|
|
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'];
|
2016-03-13 11:31:46 +08:00
|
|
|
$sql = "INSERT INTO ".DB_PREFIX."users (username, password, ip, preference)
|
|
|
|
VALUES ('$uname', '$passwd', '$ip', 'default')";
|
2016-02-02 23:53:08 +08:00
|
|
|
return $this->query($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($uname, $key, $value) {
|
2016-03-13 11:31:46 +08:00
|
|
|
return $this->query("UPDATE ".DB_PREFIX."users SET `$key`='$value' WHERE username='$uname'");
|
2016-02-02 23:53:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($uname) {
|
2016-03-13 11:31:46 +08:00
|
|
|
return $this->query("DELETE FROM ".DB_PREFIX."users WHERE username='$uname'");
|
2016-02-02 23:53:08 +08:00
|
|
|
}
|
|
|
|
|
2016-03-13 15:38:34 +08:00
|
|
|
public function encryptPassword($raw_passwd, $username="") {
|
|
|
|
$encrypt = md5($raw_passwd);
|
|
|
|
return $encrypt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sync($username) {
|
|
|
|
return ($this->checkRecordExist('username', $username)) ? true : false;
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:53:08 +08:00
|
|
|
}
|