update copyright year
[GitHub/Stricted/Domain-Control-Panel.git] / lib / system / DB.class.php
CommitLineData
2aa91ff2
S
1<?php
2namespace dns\system;
3
4/**
5 * @author Jan Altensen (Stricted)
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
d4779364 7 * @copyright 2013-2016 Jan Altensen (Stricted)
2aa91ff2
S
8 */
9class DB {
10 /**
11 * PDO object
12 * @var object
13 */
14 private $conn = null;
15
2aa91ff2
S
16 /**
17 * Connects to SQL Server
18 *
19 * @param string $driver
20 * @param string $host
21 * @param string $username
22 * @param string $password
23 * @param string $database
24 * @param integer $port
25 * @param array $options
26 * @return boolean
27 */
28 public function __construct($driver, $host, $username, $password, $database, $port = 0, $options = array()) {
29 if (!extension_loaded("pdo")) {
30 // check if extension loaded
19018596 31 throw new SystemException("Missing <a href=\"http://www.php.net/manual/en/book.pdo.php\">PDO</a> PHP extension.");
2aa91ff2
S
32 }
33
34 $driver = strtolower($driver);
19018596
S
35 if ($driver == "mysql") {
36 if (!extension_loaded("pdo_mysql")) {
37 // check if extension loaded
38 throw new SystemException("Missing <a href=\"http://php.net/manual/de/ref.pdo-mysql.php\">pdo_mysql</a> PHP extension.");
2aa91ff2 39 }
19018596
S
40
41 if (empty($port)) {
42 $port=3306;
2aa91ff2 43 }
19018596
S
44
45 $this->conn = new \PDO("mysql:host=".$host.";port=".$port.";dbname=".$database, $username, $password, $options);
46 }
47 else if ($driver == "pgsql") {
48 if (!extension_loaded("pdo_pgsql")) {
49 // check if extension loaded
50 throw new SystemException("Missing <a href=\"http://php.net/manual/de/ref.pdo-pgsql.php\">pdo_pgsql</a> PHP extension.");
2aa91ff2 51 }
19018596
S
52
53 if (empty($port)) {
54 $port=5432;
2aa91ff2
S
55 }
56
19018596
S
57 $this->conn = new \PDO("pgsql:host=".$host.";port=".$port.";dbname=".$database, $username, $password, $options);
58 }
59 /*
60 else if ($driver == "sqlite") {
61 if (!extension_loaded("pdo_sqlite")) {
62 // check if extension loaded
63 throw new SystemException("Missing <a href=\"http://php.net/manual/de/ref.pdo-sqlite.php\">pdo_sqlite</a> PHP extension.");
64 }
65
66 if (!file_exists($database)) {
67 @touch($database);
68 }
2aa91ff2 69
19018596
S
70 if (file_exists($database) && is_readable($database) && is_writable($database)) {
71 $this->conn = new \PDO("sqlite:".$database, $username, $password, $options);
72 }
73 else {
74 throw new SystemException("cant crate/connect the/to sqlite database");
75 }
2aa91ff2 76 }
19018596
S
77 */
78 else{
79 throw new SystemException("no supported database type found");
2aa91ff2 80 }
19018596
S
81
82 return true;
2aa91ff2
S
83 }
84
85 /*
86 * close the database connection
87 */
88 public function close () {
89 $this->conn = null;
90 }
91
92 /**
93 * Sends a database query to SQL server.
94 *
95 * @param string $res
96 * @param array $bind
97 * @return integer
98 */
99 public function query ($res, $bind = array()) {
19018596
S
100 $query = null;
101 $query = $this->conn->prepare($res);
102
103 if (is_array($bind) && !empty($bind)) {
104 $query->execute($bind);
2aa91ff2 105 }
19018596
S
106 else {
107 $query->execute();
2aa91ff2 108 }
19018596
S
109
110 return $query;
111
2aa91ff2
S
112 }
113
114 /**
115 * Gets a row from SQL database query result.
116 *
117 * @param string $res
118 * @return array
119 */
120 public function fetch_array ($res) {
19018596 121 return $res->fetch(\PDO::FETCH_ASSOC);
2aa91ff2
S
122 }
123
124 /**
125 * return the last insert id
126 *
127 * @return integer
128 */
129 public function last_id () {
130 return $this->conn->lastInsertId();
131 }
132
2aa91ff2
S
133 /**
134 * call PDO methods
135 *
136 * @param string $name
137 * @param array $arguments
138 * @return mixed
139 */
140 public function __call($name, $arguments) {
141 if (!method_exists($this->conn, $name)) {
142 throw new \Exception("unknown method '".$name."'");
143 }
144
145 return call_user_func_array(array($this->conn, $name), $arguments);
146 }
147}