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
<?php
class db extends PDO {
	private $passwords = array(
		'user' => 'password',
		'user2' => 'another password'
	);
	private $aliases = array(
		'username' => 'user'
	);
	public function __construct($user, $local = false) {
		try {
			$host = DB_HOST;
			$username = USERNAME;
			if($local) {
				$host = 'localhost';
				$username = '';
			}
			$dsn = 'mysql:host=' . $host . ';' . 'dbname=' . $username . $name;
			if(isset($this->aliases[$user])) {
				$name = $this->aliases[$user];
			} else {
				$name = $user;
			}
			if(empty($this->passwords[$name])) throw new PDOException('User not found!');
			$dsn = 'mysql:host=' . $host . ';' . 'dbname=' . $username . $name;
			parent::__construct($dsn, $username . $user, $this->passwords[$name]);
			$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
			$this->exec("SET time_zone = '+0:00'");
			unset($this->passwords, $this->aliases);
		} catch(PDOException $e) {
			echo 'DB error: ' . $e->getMessage();
		}
	}
}