-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL.class.php
350 lines (287 loc) · 13 KB
/
MySQL.class.php
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
require "iCRUD.interface.php";
require "Log.class.php";
class MySQL implements iCRUD
{
private $conn;
private $servername;
private $dbname;
private $username;
private $password;
public static $instances = 0;
public static $connections = 0;
function __construct($servername, $dbname, $username, $password)
{
++self::$instances;
$filename = "log.txt";
system('attrib +H ' . escapeshellarg($filename));
$this->servername = $servername;
$this->dbname = $dbname;
$this->username = $username;
$this->password = $password;
}
function __destruct()
{
--self::$instances;
}
static function connection_status()
{
return "\nConnection status : \nYou have " . self::$instances . " active instances!\nYou have " . self::$connections . " active connections!";
}
function get_info($action)
{
switch ($action) {
case "ERRMODE" :
$value = $this->conn->getAttribute(constant("PDO::ATTR_$action"));
if ($value == 0)
return "ERROR_MODE_SILENT";
else if ($value == 1)
return "ERROR_MODE_WARNING";
else if ($value == 2)
return "ERROR_MODE_EXCEPTION";
break;
case "ERRMODE_ALL" :
return (
"ERROR_MODE_SILENT setting error codes\n
ERROR_MODE_WARNING raises warnings\n
ERROR_MODE_EXCEPTION throws exceptions\n
To check which one is active, use getInfo('ERRMODE')"
);
break;
case "CASE" :
$value = $this->conn->getAttribute(constant("PDO::ATTR_$action"));
if ($value == 0)
return "CASE_NATURAL";
else if ($value == 1)
return "CASE_UPPER";
else if ($value == 2)
return "CASE_LOWER";
break;
case "CASE_ALL" :
return ("
CASE_LOWER forces column names to lower case\n
CASE_NATURAL leaves column names as returned by the database driver\n
CASE_UPPER forces column names to upper case\n
To check which one is active, use getInfo('CASE')");
break;
case "CLIENT_VERSION" :
return $this->conn->getAttribute(constant("PDO::ATTR_$action")) . "\n";
break;
case "CONNECTION_STATUS";
return $this->conn->getAttribute(constant("PDO::ATTR_$action")) . "\n";
break;
case "SERVER_INFO" :
return $this->conn->getAttribute(constant("PDO::ATTR_$action")) . "\n";
break;
case "SERVER_VERSION" :
return $this->conn->getAttribute(constant("PDO::ATTR_$action")) . "\n";
break;
case "ACCESS_INFO" :
return("
SERVER NAME : $this->servername\n
USER NAME : $this->username\n
PASSWORD : $this->password\n
DATABASE : $this->dbname
");
break;
case "AUTHOR" :
return("
Author : Nemanja Kovacevic\n
Facebook : https://www.facebook.com/nemanja.kovacevic.923\n
Licence : Open source\n
Created on : jan 2017
");
break;
default :
return("
Wrong action\n
Actions you can check :
- ERRMODE
- CASE
- CLIENT_VERSION
- CONNECTION_STATUS
- SERVER_INFO
- SERVER_VERSION
");
}
}
function set_info($key, $value)
{
$file_handler = new Log();
switch ($key) {
case "ERRMODE" :
if ($value == "ERROR_MODE_SILENT") {
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$file_handler->write("!SUCCESS : ERROR MODE CHANGED TO SILENT MODE");
} else if ($value == "ERROR_MODE_WARNING") {
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$file_handler->write("!SUCCESS : ERROR MODE CHANGED TO WARNING MODE");
} else if ($value == "ERROR_MODE_EXCEPTION") {
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$file_handler->write("!SUCCESS : ERROR MODE CHANGED TO EXCEPTION MODE");
} else
print_r("!ERROR : Error mode you want doesn't exist, please check getInfo('ERRMODE_ALL') for available error modes");
break;
case "CASE" :
if ($value == "CASE_LOWER") {
$this->conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$file_handler->write("!SUCCESS : CASE MODE CHANGED TO LOWER MODE");
} else if ($value == "CASE_NATURAL") {
$this->conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
$file_handler->write("!SUCCESS : CASE MODE CHANGED TO NATURAL MODE");
} else if ($value == "CASE_UPPER") {
$this->conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
$file_handler->write("!SUCCESS : CASE MODE CHANGED TO UPPER MODE");
} else
return "Case mode you want doens't exist, please check getInfo('CASE_ALL') for available case modes";
}
}
function database_close()
{
$file_handler = new Log();
if ($this->conn != null) {
$file_handler->write("!SUCCESS : You have closed database connection with " . $this->dbname . "");
$this->conn = null;
--self::$connections;
} else if ($this->conn == null) {
$file_handler->write("!ERROR : You can't close database connection that is already closed!");
}
}
function database_connection()
{
if ($this->conn == null) {
$file_handler = new Log();
try {
$this->conn = new PDO("mysql:host=$this->servername;dbname=$this->dbname", $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$file_handler->write("!SUCCESS : You have connected to database " . $this->dbname . " successfully");
++self::$connections;
} catch (PDOException $e) {
$file_handler->write("!ERROR : Connection failed to database " . $this->dbname . " " . $e->getMessage());
}
} else if ($this->conn != null) {
$file_handler = new Log();
$file_handler->write("!ERROR : You can't connect, you are already connected with this instance!");
}
}
//SQL PREVENT ( helping hands along with PDO prepare statement )
function is_safe($values)
{
$val = explode(" ", $values);
$lower_val = array();
foreach ($val as $toLower)
{
$lower_val[] = strtolower($toLower);
}
var_dump($lower_val);
if(in_array("or", $lower_val) || in_array("delete", $lower_val) || in_array("drop", $lower_val)
|| in_array("truncate", $lower_val) || in_array("select", $lower_val) || in_array("insert", $lower_val)
|| in_array("where", $lower_val) || in_array("and", $lower_val) || in_array("between", $lower_val)
|| in_array("update", $lower_val) || in_array("create", $lower_val) || in_array("alter", $lower_val))
return true;
else
return false;
}
function db_select($array_columns, $array_tables, $array_where)
{
$file_handler = new Log();
if($this->conn != null)
{
$array_col = implode(",", $array_columns);
$array_tab = implode(",", $array_tables);
if(isset($array_where))
$array_wh = implode(" AND ", $array_where);
if(isset($array_where))
$sql = $this->conn->prepare("SELECT $array_col FROM $array_tab WHERE $array_wh");
else
$sql = $this->conn->prepare("SELECT $array_col FROM $array_tab");
try{
$sql->execute();
$rows = $sql->fetchAll();
$file_handler->write("!SUCCESS : SELECT OK - rows returned : " . count($rows));
return $rows;
} catch (PDOException $e)
{
$file_handler->write("!ERROR : SELECT FAIL - ERROR : " .$e->getMessage());
}
} else
$file_handler->write("!ERROR : Your SELECT query didn't pass because connection with database doesn't exist!");
}
function db_insert($array_values, $array_columns, $array_tables)
{
$file_handler = new Log();
if($this->conn != null)
{
$array_val = implode(",", $array_values);
$array_tab = implode(",", $array_tables);
$array_col = implode(",", $array_columns);
try{
$sql = $this->conn->prepare("INSERT INTO $array_tab($array_col) VALUES ($array_val)");
$sql->execute();
$file_handler->write("!SUCCESS : INSERT SUCCESS");
} catch (PDOException $e)
{
$file_handler->write("!ERROR : INSERT FAIL - ERROR : " .$e->getMessage());
}
} else
$file_handler->write("!ERROR : Your INSERT query didn't pass because connection with database doesn't exist!");
}
function db_update($array_tables, $array_columns, $array_values, $array_where)
{
$file_handler = new Log();
if($this->conn != null)
{
$array_tab = implode(",", $array_tables);
$array_wh = implode(" AND ", $array_where);
$newArraySet = array();
for($i = 0; $i < count($array_columns); $i++)
$newArraySet[] = $array_columns[$i]."=".$array_values[$i];
$makeString = implode(", ", $newArraySet);
try{
$sql = $this->conn->prepare("UPDATE $array_tab SET $makeString WHERE $array_wh");
$sql->execute();
$file_handler->write("!SUCCESS : UPDATE SUCCESS");
} catch (PDOException $e)
{
$file_handler->write("!ERROR : UPDATE FAIL - ERROR : " .$e->getMessage());
}
} else
$file_handler->write("!ERROR : Your UPDATE query didn't pass because connection with database doesn't exist!");
}
function db_delete($array_tables, $array_where)
{
$file_handler = new Log();
if($this->conn != null)
{
$array_tab = implode(",", $array_tables);
$array_wh = implode(" AND ", $array_where);
try{
$sql = $this->conn->prepare("DELETE FROM $array_tab WHERE $array_wh");
$sql->execute();
$file_handler->write("!SUCCESS : DELETE SUCCESS");
}catch (PDOException $e)
{
$file_handler->write("!ERROR : DELETE FAIL - ERROR : " .$e->getMessage());
}
} else
$file_handler->write("!ERROR : Your DELETE query didn't pass because connection with database doesn't exist!");
}
function db_truncate($array_tables)
{
$file_handler = new Log();
if($this->conn != null)
{
$array_tab = implode(",", $array_tables);
try{
$sql = $this->conn->prepare("TRUNCATE TABLE $array_tab");
$sql->execute();
$file_handler->write("!SUCCESS : TRUNCATE SUCCESS");
} catch (PDOException $e)
{
$file_handler->write("!ERROR : TRUNCATE FAIL - ERROR : " .$e->getMessage());
}
} else
$file_handler->write("!ERROR : Your TRUNCATE query didn't pass because connection with database doesn't exist!");
}
}
?>