summaryrefslogtreecommitdiff
path: root/public_html/class/mysql.php
blob: d0c69495becd4e276865a76f4793fd4595c40e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

class vfsdb {
	
	public $db;

	public function __construct(){
		$this->open();
	}

	public function open(){
		try {
			$this->db = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBNAME);	
		} catch (Exception $e){
			failure("<p>".$e->getMessage()."</p>", '500 Server Failure', false, '<h1>Failed to open database connection.</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 " . DBCHARSET . " as the charset on your MySQL server.</p>" , '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 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("<p>There was a problem during bootstrapping the database schema. " . $this->db->error . "</p>", '500 Server Failure', false, "<h1>CREATE TABLE FAILED</h1>");
	}
	
	public function __destruct(){
		$this->close();
	}
}