2016-02-02 23:53:08 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @Author: printempw
|
|
|
|
* @Date: 2016-02-02 21:59:06
|
|
|
|
* @Last Modified by: prpr
|
2016-02-06 23:18:07 +08:00
|
|
|
* @Last Modified time: 2016-02-06 23:06:30
|
2016-02-02 23:53:08 +08:00
|
|
|
*/
|
|
|
|
|
2016-02-06 23:18:07 +08:00
|
|
|
class Database
|
2016-02-02 23:53:08 +08:00
|
|
|
{
|
|
|
|
private $connection = null;
|
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
$this->connection = self::checkConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function checkConfig() {
|
|
|
|
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);
|
|
|
|
if ($conn->connect_error) {
|
2016-02-06 23:18:07 +08:00
|
|
|
Utils::raise(-1, "Can not connect to mysql, check if database info correct in config.php. ".
|
2016-02-02 23:53:08 +08:00
|
|
|
$conn->connect_error);
|
|
|
|
}
|
2016-02-03 00:11:43 +08:00
|
|
|
if (!self::checkTableExist($conn)) {
|
2016-02-06 23:18:07 +08:00
|
|
|
Utils::raise(-1, "Looks like that there is no `users` table in your database. ".
|
2016-02-03 00:11:43 +08:00
|
|
|
"Please run `/admin/install.php` first.");
|
|
|
|
}
|
2016-02-03 15:55:52 +08:00
|
|
|
$dir = dirname(dirname(__FILE__));
|
|
|
|
if (!is_dir("$dir/textures/")) {
|
2016-02-06 23:18:07 +08:00
|
|
|
Utils::raise(-1, "No `textures` directory exists. Please run `/admin/install.php` ".
|
2016-02-03 15:47:06 +08:00
|
|
|
"or put one manually.");
|
|
|
|
}
|
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
|
|
|
|
`INFORMATION_SCHEMA`.`TABLES` WHERE table_name ='users'
|
|
|
|
AND TABLE_SCHEMA='".DB_NAME."'";
|
|
|
|
return ($conn->query($sql)->num_rows != 0) ? true : false;
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:53:08 +08:00
|
|
|
public function query($sql) {
|
|
|
|
$result = $this->connection->query($sql);
|
|
|
|
if (!$this->connection->error) {
|
|
|
|
return $result;
|
|
|
|
}
|
2016-02-06 23:18:07 +08:00
|
|
|
Utils::raise(-1, "Database query error: ".$this->connection->error);
|
2016-02-02 23:53:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchArray($sql) {
|
|
|
|
return $this->query($sql)->fetch_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function select($key, $value) {
|
|
|
|
return $this->fetchArray("SELECT * FROM users WHERE $key='$value'");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNumRows($key, $value) {
|
|
|
|
$sql = "SELECT * FROM users WHERE $key='$value'";
|
|
|
|
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 users (username, password, ip, preference)
|
|
|
|
VALUES ('$uname', '$passwd', '$ip', 'default')";
|
|
|
|
return $this->query($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($uname, $key, $value) {
|
2016-02-03 13:27:13 +08:00
|
|
|
return $this->query("UPDATE users SET `$key`='$value' WHERE username='$uname'");
|
2016-02-02 23:53:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($uname) {
|
|
|
|
return $this->query("DELETE from users WHERE username='$uname'");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|