-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.trimMysqli.php
84 lines (76 loc) · 2.49 KB
/
class.trimMysqli.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
<?php
/**
* stripMysqli - Extend PHP's mysqli_query function to remove unnecessary formatting from
* the query in order to help reduce space used in logs as well as allow for
* simpler log parsing.
*
* @author Joseph Thayne <[email protected]>
*/
class stripMysqli extends mysqli
{
function query($query)
{
/**
* Initialize variables
*/
$skipTillResetSingle = false;
$skipTillResetDouble = false;
$reachedFirst = false;
/**
* Remove any beginning or ending whitespace before continuing
*/
$query = trim($query);
/**
* Split the string into an array with a single key for each character
* The array will be looped through to find the extra formatting characters
*/
$charList = str_split($query);
$queryLength = count($charList);
$spCount = 0;
for ($loopCount = 0; $loopCount < $queryLength; $loopCount++) {
switch ($charList[$loopCount]) {
case PHP_EOL:
case "\r":
case "\n":
if ($skipTillResetSingle == false && $skipTillResetDouble == false)
unset($charList[$loopCount]);
break;
case ' ':
if ($skipTillResetSingle == false && $skipTillResetDouble == false) {
if ($reachedFirst)
unset($charList[$loopCount]);
else
$reachedFirst = true;
}
break;
case '\\':
$reachedFirst = false;
$loopCount++;
break;
case '\'':
$reachedFirst = false;
if ($skipTillResetSingle)
$skipTillResetSingle = false;
else
$skipTillResetSingle = true;
break;
case '"':
$reachedFirst = false;
if ($skipTillResetDouble)
$skipTillResetDouble = false;
else
$skipTillResetDouble = true;
break;
default:
$reachedFirst = false;
break;
}
}
$query = implode('', $charList);
$result = parent::query($query);
if (mysqli_error($this)) {
throw new exception(mysqli_error($this), mysqli_errno($this));
}
return $result;
}
}