open();
}
public function open(){
try {
$this->db = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBNAME);
} catch (Exception $e){
failure("
".$e->getMessage()."
", '500 Server Failure', false, 'Failed to open database connection.
');
}
if ( $this->db->connect_errno() ){
failure("Can't connect to the database. MySQL gave this error code: ".$this->db->connect_errno . "
", '500 Server Failure', false, 'Connection to MySQL server failed.
');
}
if ( ! $this->db->ping() ){
failure("Can't reach MySQL server. Server says: " . $this->db->error . "
", '500 Server Failure', false, "Can't reach MySQL server!
");
}
if ( ! $this->db->set_charset(DBCHARSET) ){
failure("Can't set " . DBCHARSET . " as the charset on your MySQL server.
" , '500 Server Failure', false, "Setting Charset failed!
");
}
}
public function close(){
$this->db->close();
}
public function check(){
if ( ! $this->db->ping() ){
return false;
}
return true;
}
private function _prepare($sql){
if ( is_null($sql) || $sql == "")
return false;
return $this->db->real_escape_string($sql);
}
public function doQuery($string){
if ( ! $this->check() )
failure("Can't reach MySQL server. Server says: ". $this->db->error . "
", '500 Server Failure', false, "Can't reach MySQL server!
")
$sql = _prepare($string);
if ( ! $sql )
return false;
return $this->db->query($sql);
}
public function createTables(){
$user_table =
'CREATE TABLE IF NOT EXISTS ' . DBPREFIX . 'user
( id INTEGER AUTO_INCREMENT NOT NULL, PRIMARY KEY(id),
name VARCHAR(70), UNIQUE(name),
password VARCHAR(70), UNIQUE(password),
email VARCHAR(70), UNIQUE(email),
invites INTEGER,
inviter INTEGER,
invitekey VARCHAR(70), UNIQUE(invitekey),
status INTEGER,
color_folder VARCHAR(70),
color_file VARCHAR(70))
ENGINE=InnoDB;';
$files_table =
'CREATE TABLE IF NOT EXISTS ' . DBPREFIX . 'files
( files_id INTEGER AUTO_INCREMENT NOT NULL, PRIMARY KEY(files_id),
parent INTEGER,
owner INTEGER,
name VARCHAR(70),
type VARCHAR(70),
mime VARCHAR(70),
size INTEGER,
visibility VARCHAR(70),
hash VARCHAR(70),
download_link VARCHAR(70),
upload_time INTEGER,
last_access INTEGER,
FOREIGN KEY(files_id) REFERENCES user(id) ON DELETE CASCADE
)
ENGINE=InnoDB;';
$banned_user_table =
'CREATE TABLE IF NOT EXISTS ' . DBPREFIX . 'banned_user
( banned_id INTEGER AUTO_INCREMENT NOT NULL, PRIMARY KEY(banned_id),
login_attempts INTEGER,
ip TEXT,
session_id TEXT,
time INTEGER,
user INTEGER
)
ENGINE=InnoDB;';
if ( ! $this->db->query($user_table . ' ' . $files_table . ' ' . $banned_user_table) )
failure("There was a problem during bootstrapping the database schema. " . $this->db->error . "
", '500 Server Failure', false, "CREATE TABLE FAILED
");
}
public function __destruct(){
$this->close();
}
}