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