forked from willfarrell/php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.db.php
More file actions
397 lines (358 loc) · 10.3 KB
/
class.db.php
File metadata and controls
397 lines (358 loc) · 10.3 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* MySQL Database Class
*
* PHP Version 5
*
* @category N/A
* @package N/A
* @author will Farrell <will.farrell@gmail.com>
* @copyright 2001 - 2012 willFarrell.ca
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version GIT: <git_id>
* @link http://willFarrell.ca
*/
/*
Additional Notes:
- Make sure the DB_USER only has permission for command your using
*/
// Set server default time, it's a life saver. seriously
date_default_timezone_set('UTC');
if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1' || $_SERVER['HTTP_HOST'] == 'localhost:8888') {
define('DB_SERVER','localhost');
define('DB_NAME','db');
define('DB_USER','root');
define('DB_PASS','localhost');
} else {
define('DB_SERVER','localhost');
define('DB_NAME','');
define('DB_USER','');
define('DB_PASS','');
}
/**
* Database
*
* @category N/A
* @package N/A
* @author Original Author <author@example.com>
* @author Another Author <another@example.com>
* @copyright 2001 - 2011 willFarrell.ca
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Release: <package_version>
* @link http://willFarrell.ca
*/
class MySQL
{
var $connection_mysql; //The MySQL database connection
/**
* Constructor
*/
function __construct()
{
$this->_connect();
}
/**
* Destructor
*/
function __destruct()
{
$this->_close();
}
/**
* create db connection
*
* @return null
* @access private
*/
private function _connect()
{
$this->connection_mysql = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $this->connection_mysql) or die(mysql_error());
}
/**
* close db connection
*
* @return null
* @access private
*/
private function _close()
{
mysql_close($this->connection_mysql);
}
/**
* Connection check
* pings mysql service to see if still running,
* if not, connects again
*
* @return true
* @aceess puiblic
*/
function ping()
{
if (!mysql_ping($this->connection_mysql)) {
$this->_connect();
$this->ping();
} else {
return true;
}
}
/**
* MySQL Query
* runs the query through the MyQSL service
*
* @param string $query - MySQL Query
*
* @return MySQL Object
* @aceess puiblic
*/
private function _run($query)
{
$return = mysql_query($query, $this->connection_mysql);
if (mysql_error()) {
echo $query."<br>";
echo "<b>".mysql_error()."</b><br>";
}
return $return;
}
/**
* checks the output of a MySQL query
*
* @param object $result MySQL Object
*
* @return MySQL Object
* @aceess puiblic
*/
function resultCheck($result)
{
if (!$result || (mysql_num_rows($result) < 1)) {
return null;
}
return $result;
}
//** Functions **//
/**
* cleans all values of SQL injections
*
* @param array $array array of values to interact with the DB
*
* @return array of cleaned values
* @aceess puiblic
*/
function cleanArray($array)
{
$array = array_map('trim', $array);
$array = array_map('stripslashes', $array);
$array = array_map('mysql_real_escape_string', $array);
return $array;
}
/**
* cleans a value of SQL injections
*
* @param string $value value to interact with the DB
*
* @return cleaned value
* @aceess puiblic
*/
function cleanValue($value)
{
if (is_string($value)) {
$value = trim($value);
$value = stripslashes($value);
$value = mysql_real_escape_string($value);
}
return $value;
}
/**
* FUTURE: cleans all values of SQL injections
*
* @param string $query - MySQL query
*
* @return cleaned query
* @aceess puiblic
*/
function cleanQuery($query)
{
//echo $q;
//preg_match_all("/[;]?(DROP TABLE|TRUNCATE)[\s]+/", $q, $matches);
//print_r($matches);
//if (count($matches) > 2) return null;
return $query;
}
/**
* Custom Query
* runs a templates written query
*l
* @param string $query MySQL Query
* @param array $value_array replace {{$key}} with $value in custom query
*
* @return object $object MySQL Object
* @aceess puiblic
*/
function query($query, $value_array = NULL)
{
if ($value_array && is_array($value_array)) {
$value_array = $this->cleanArray($value_array);
$query = preg_replace("/{{([\w]+)}}/e", "\$value_array['\\1']", $query);
/*foreach ($value_array as $key => $value) {
$query = preg_replace("/{{".$key."}}/i", $value, $query);
}*/
} else {
//$q = $this->cleanQuery($q);
}
$check = (substr($query, 0, 6) == 'SELECT')?true:false;
$result = $this->_run($query);
$result = ($check)?$this->resultCheck($result):$result;
return $result;
}
/**
* Select Query
*
* @param string $table table where rows will be deleted
* @param array $where_array `$key` = $value WHERE parameters
* @param array $select_array `$key` = $value SELECT parameters
* @param array $order_array `$key` = $value ORDER BY parameters
*
* @return ID of affected row
* @access public
*/
function select($table, $where_array = null, $select_array = null, $order_array = null)
{
$where = '';
if ($where_array && is_array($where_array)) {
$where_array = $this->cleanArray($where_array);
$i = 0;
foreach ($where_array as $key => $value) {
$where .= ($i)?"AND ":'';
$where .= "`$key` = '$value' ";
$i++;
}
$where = ($where)?"WHERE ".$where:'';
}
$select = '';
if ($select_array && is_array($select_array)) {
$select_array = $this->cleanArray($select_array);
foreach ($select_array as $value) {
$select .= ($select)?", ":'';
$select .= "$value";
}
$select = ($select) ? $select : "*";
} else {
$select = "*";
}
$order = '';
if ($order_array && is_array($order_array)) {
$order_array = $this->cleanArray($order_array);
foreach ($order_array as $value) {
$order .= ($order)?", ":'';
$order .= "$value";
}
$order = ($order)?"ORDER BY ".$order:'';
}
$query = "SELECT $select FROM `$table` $where $order";
$result = $this->_run($query);
$result = $this->resultCheck($result);
return $result;
}
/**
* Insert Query
*
* @param string $table table where rows will be deleted
* @param array $set_array `$key` = $value SET parameters
* @param array $update_array `$key` = $value ON DUPLICATE UPDATE parameters
*
* @return ID of affected row
* @access public
*/
function insert($table, $set_array, $update_array = null)
{
if ($set_array && is_array($set_array)) {
$set_array = $this->cleanArray($set_array);
$set = '';
foreach ($set_array as $key => $value) {
$set .= ($set)?", ":'';
$set .= "`$key` = '$value' ";
}
if ($update_array && is_array($update_array)) {
$update_array = $this->cleanArray($update_array);
$update = '';
foreach ($update_array as $key => $value) {
$update .= ($update)?", ":'';
$update .= "`$key` = '$value' ";
}
} else {
$update = $set;
}
$set = ($set)?"SET ".$set:'';
$update = ($update)?"ON DUPLICATE KEY UPDATE ".$update:'';
}
$query = "INSERT INTO `$table` $set $update";
$this->_run($query);
return mysql_insert_id();
}
/**
* Update Query
*
* @param string $table table where rows will be deleted
* @param array $set_array `$key` = $value SET parameters
* @param array $where_array `$key` = $value WHERE parameters
*
* @return number of affected rows
* @access public
*/
function update($table, $set_array, $where_array)
{
if ($set_array && is_array($set_array)) {
$set_array = $this->cleanArray($set_array);
$i = 0;
$set = '';
foreach ($set_array as $key => $value) {
$set .= ($i)?", ":'';
$set .= "`$key` = '$value' ";
$i++;
}
$set = ($set)?"SET ".$set:'';
}
if ($where_array && is_array($where_array)) {
$where_array = $this->cleanArray($where_array);
$i = 0;
$where = '';
foreach ($where_array as $key => $value) {
$where .= ($i)?"AND ":'';
$where .= "`$key` = '$value' ";
$i++;
}
$where = ($where)?"WHERE ".$where:'';
}
$query = "UPDATE `$table` $set $where";
$this->_run($query);
return mysql_affected_rows();
}
/**
* Delete Query
*
* @param string $table table where rows will be deleted
* @param array $where_array array of `$key` = $value parameters
*
* @return number of affected rows
* @access public
*/
function delete($table, $where_array)
{
if ($where_array && is_array($where_array)) {
$where_array = $this->cleanArray($where_array);
$i = 0;
$where = '';
foreach ($where_array as $key => $value) {
$where .= ($i)?"AND ":'';
$where .= "`$key` = '$value' ";
$i++;
}
$where = ($where)?"WHERE ".$where:'';
}
$query = "DELETE FROM `$table` $where";
$this->_run($query);
return mysql_affected_rows();
}
};
$database = new MySQL;
?>