diff options
| author | Horus3 | 2014-09-18 14:19:56 +0200 |
|---|---|---|
| committer | Horus3 | 2014-09-18 14:19:56 +0200 |
| commit | e2e66e5ebb845f96703fbdacabfd0b75b29b705c (patch) | |
| tree | 08a6224457eaa328243a0f71e8a11fa8eaccc36a /public_html | |
| parent | 548e11e863e906bb80695fe7b2789fe4af3f0f39 (diff) | |
| download | vfs-e2e66e5ebb845f96703fbdacabfd0b75b29b705c.tar.gz | |
class vfsdb
Diffstat (limited to 'public_html')
| -rw-r--r-- | public_html/class/mysql.php | 111 | ||||
| -rw-r--r-- | public_html/functions.php | 22 | ||||
| -rw-r--r-- | public_html/vfs_config.php | 1 |
3 files changed, 119 insertions, 15 deletions
diff --git a/public_html/class/mysql.php b/public_html/class/mysql.php index b2d2983..d38fc2c 100644 --- a/public_html/class/mysql.php +++ b/public_html/class/mysql.php @@ -1,28 +1,109 @@ <?php -class database { +class vfsdb { public $db; - private $dbhost; - private $dbuser; - private $dbpassword; - private $dbname; - private $dbprefix; - public function __construct($dbhost, $dbuser, $dbname, $dbpassword, $dbprefix){ - $this->dbhost=$dbhost; - $this->dbuser=$dbuser; - $this->dbname=$dbname; - $this->dbpassword=$dbpassword; - $this->dbprefix=$dbprefix; + public function __construct(){ + return true; } public function open(){ try { - $this->db = new mysqli($this->dbhost, $this->dbuser, $this->dbpassword, $this->dbname); + $this->db = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBNAME); } catch (Exception $e){ - echo $e->getMessage(); - exit; + failure($e->getMessage(), '<p>500 Server Failure</p>', false, '<h1>Failed to open database.</h1>'); } + + if ( $this->db->connect_errno() ){ + failure("<p>Can't connect to the database. MySQL gave this error code: ".$this->db->connect_errno . "</p>", '500 Server Failure', false, '<h1>Connection to MySQL server failed.</h1>'); + } + + if ( ! $this->db->ping() ){ + failure("<p>Can't reach MySQL server. Server says: " . $this->db->error . "</p>", '500 Server Failure', false, "<h1>Can't reach MySQL server!</h1>"); + } + + if ( ! $this->db->set_charset(DBCHARSET) ){ + failure("<p>Can't set UTF-8 as a charset on your MySQL server." , '500 Server Failure', false, "<h1>Setting Charset failed!</h1>"); + } + + } + + 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("<p>Can't reach MySQL server. Server says: ". $this->db->error . "</p>", '500 Server Failure', false, "<h1>Can't reach MySQL server!</h1>") + + $sql = _prepare($string); + if ( ! $sql ) + return false; + return $this->db->query($sql); + } + + public function setUp(){ + $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("<p>Setting up the database failed.</p>", '500 Server Failure', false, "<h1>CREATE TABLE FAILED"); + } + + public function __destruct(){ + $this->close(); } } diff --git a/public_html/functions.php b/public_html/functions.php new file mode 100644 index 0000000..a5dbd83 --- /dev/null +++ b/public_html/functions.php @@ -0,0 +1,22 @@ +<?php + +function failure($reason, $httpcode, $ajax = true, $heading = NULL){ + + # send header with $httpcode + header($_SERVER['SERVER_PROTOCOL'] . " " . $httpcode) + + if($ajax){ + # just echo the reason to the ajax response + echo $reason; + exit + } + + // TODO: Put pretty HTML here, please + if($heading != NULL) + echo $heading; + + echo $reason; + + # exit the script here + exit; +} diff --git a/public_html/vfs_config.php b/public_html/vfs_config.php index 141345c..da0085a 100644 --- a/public_html/vfs_config.php +++ b/public_html/vfs_config.php @@ -5,6 +5,7 @@ define('DBHOST', 'localhost'); define('DBUSER', 'vfs-user'); define('DBNAME', 'vfs'); define('DBPASSWORD', 'secretpassword'); +define('DBCHARSET', 'utf8'); define('DBPREFIX', 'vfs_'); # absolute path |
