1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Generic SQL Driver Related Functionality |
4
|
|
|
* @author Joe Huss <[email protected]> |
5
|
|
|
* @copyright 2018 |
6
|
|
|
* @package MyAdmin |
7
|
|
|
* @category SQL |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MyDb; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Generic |
14
|
|
|
*/ |
15
|
|
|
abstract class Generic { |
16
|
|
|
/* public: connection parameters */ |
17
|
|
|
public $host = 'localhost'; |
18
|
|
|
public $database = ''; |
19
|
|
|
public $user = ''; |
20
|
|
|
public $password = ''; |
21
|
|
|
|
22
|
|
|
/* public: configuration parameters */ |
23
|
|
|
public $autoStripslashes = false; |
24
|
|
|
public $Debug = 0; // Set to 1 for debugging messages. |
25
|
|
|
public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
26
|
|
|
|
27
|
|
|
public $maxConnectErrors = 30; |
28
|
|
|
public $connectionAttempt = 0; |
29
|
|
|
public $maxMatches = 10000000; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
public $autoFree = 0; // Set to 1 for automatic mysql_free_result() |
35
|
|
|
|
36
|
|
|
/* public: result array and current row number */ |
37
|
|
|
public $Record = []; |
38
|
|
|
public $Row; |
39
|
|
|
|
40
|
|
|
/* public: current error number and error text */ |
41
|
|
|
public $Errno = 0; |
42
|
|
|
public $Error = ''; |
43
|
|
|
|
44
|
|
|
/* public: this is an api revision, not a CVS revision. */ |
45
|
|
|
public $type = 'generic'; |
46
|
|
|
|
47
|
|
|
/* private: link and query handles */ |
48
|
|
|
public $linkId = 0; |
49
|
|
|
public $queryId = 0; |
50
|
|
|
|
51
|
|
|
public $characterSet = 'utf8mb4'; |
52
|
|
|
public $collation = 'utf8mb4_unicode_ci'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructs the db handler, can optionally specify connection parameters |
56
|
|
|
* |
57
|
|
|
* @param string $database Optional The database name |
58
|
|
|
* @param string $user Optional The username to connect with |
59
|
|
|
* @param string $password Optional The password to use |
60
|
|
|
* @param string $host Optional The hostname where the server is, or default to localhost |
61
|
|
|
* @param string $query Optional query to perform immediately |
62
|
|
|
*/ |
63
|
|
|
public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '') { |
64
|
|
|
$this->database = $database; |
65
|
|
|
$this->user = $user; |
66
|
|
|
$this->password = $password; |
67
|
|
|
$this->host = $host; |
68
|
|
|
if ($query != '') |
69
|
|
|
$this->query($query); |
|
|
|
|
70
|
|
|
$this->connection_atttempt = 0; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $message |
75
|
|
|
* @param string $line |
76
|
|
|
* @param string $file |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function log($message, $line = '', $file = '', $level = 'info') { |
|
|
|
|
80
|
|
|
//if (function_exists('myadmin_log')) |
81
|
|
|
//myadmin_log('db', $level, $message, $line, $file, isset($GLOBALS['tf'])); |
82
|
|
|
//else |
83
|
|
|
error_log($message); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
1 |
|
public function linkId() { |
90
|
1 |
|
return $this->linkId; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
1 |
|
public function queryId() { |
97
|
1 |
|
return $this->queryId; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $string |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function real_escape($string) { |
105
|
|
|
if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) |
|
|
|
|
106
|
|
|
return mysqli_escape_string($string); |
|
|
|
|
107
|
|
|
return mysqli_real_escape_string($this->linkId, $string); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $string |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function escape($string) { |
115
|
|
|
return mysql_escape_string($string); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param mixed $str |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function dbAddslashes($str) { |
123
|
|
|
if (!isset($str) || $str == '') |
124
|
|
|
return ''; |
125
|
|
|
|
126
|
|
|
return addslashes($str); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Db::toTimestamp() |
131
|
|
|
* @param mixed $epoch |
132
|
|
|
* @return bool|string |
133
|
|
|
*/ |
134
|
1 |
|
public function toTimestamp($epoch) { |
135
|
1 |
|
return date('Y-m-d H:i:s', $epoch); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Db::fromTimestamp() |
140
|
|
|
* @param mixed $timestamp |
141
|
|
|
* @return bool|int|mixed |
142
|
|
|
*/ |
143
|
1 |
|
public function fromTimestamp($timestamp) { |
144
|
1 |
|
if (preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $timestamp, $parts)) |
145
|
1 |
|
$time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
146
|
|
|
elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) |
147
|
|
|
$time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
148
|
|
|
elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) |
149
|
|
|
$time = mktime(1, 1, 1, $parts[2], $parts[3], $parts[1]); |
150
|
|
|
elseif (is_numeric($timestamp) && $timestamp >= 943938000) |
151
|
|
|
$time = $timestamp; |
152
|
|
|
else { |
153
|
|
|
$this->log('Cannot Match Timestamp from '.$timestamp, __LINE__, __FILE__); |
154
|
|
|
$time = false; |
155
|
|
|
} |
156
|
1 |
|
return $time; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int $start |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function limit($start = 0) { |
164
|
|
|
echo '<b>Warning: limit() is no longer used, use limitQuery()</b>'; |
165
|
|
|
if ($start == 0) { |
166
|
|
|
$s = 'limit '.(int) $this->maxMatches; |
167
|
|
|
} else { |
168
|
|
|
$s = 'limit '.(int) $start.','.(int) $this->maxMatches; |
169
|
|
|
} |
170
|
|
|
return $s; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* perform a query with limited result set |
175
|
|
|
* |
176
|
|
|
* @param string $queryString |
177
|
|
|
* @param int $offset |
178
|
|
|
* @param string|int $numRows |
179
|
|
|
* @param string|int $line |
180
|
|
|
* @param string $file |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
1 |
|
public function limitQuery($queryString, $numRows = '', $offset = 0, $line = '', $file = '') { |
184
|
1 |
|
if (!$numRows) |
185
|
|
|
$numRows = $this->maxMatches; |
186
|
1 |
|
if ($offset == 0) { |
187
|
1 |
|
$queryString .= ' LIMIT '.(int) $numRows; |
188
|
|
|
} else { |
189
|
1 |
|
$queryString .= ' LIMIT '.(int) $offset.','.(int) $numRows; |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
if ($this->Debug) |
193
|
|
|
printf("Debug: limitQuery = %s<br>offset=%d, num_rows=%d<br>\n", $queryString, $offset, $numRows); |
194
|
|
|
|
195
|
1 |
|
return $this->query($queryString, $line, $file); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* db:qr() |
200
|
|
|
* |
201
|
|
|
* alias of queryReturn() |
202
|
|
|
* |
203
|
|
|
* @param mixed $query SQL Query to be used |
204
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
205
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
206
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
207
|
|
|
*/ |
208
|
|
|
public function qr($query, $line = '', $file = '') { |
209
|
|
|
return $this->queryReturn($query, $line, $file); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* gets a field |
214
|
|
|
* |
215
|
|
|
* @param mixed $name |
216
|
|
|
* @param string $stripSlashes |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
1 |
|
public function f($name, $stripSlashes = '') { |
220
|
1 |
|
if ($stripSlashes || ($this->autoStripslashes && !$stripSlashes)) { |
221
|
|
|
return stripslashes($this->Record[$name]); |
222
|
|
|
} else { |
223
|
1 |
|
return $this->Record[$name]; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* error handling |
229
|
|
|
* |
230
|
|
|
* @param mixed $msg |
231
|
|
|
* @param string $line |
232
|
|
|
* @param string $file |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
public function halt($msg, $line = '', $file = '') { |
236
|
|
|
$this->unlock(false); |
|
|
|
|
237
|
|
|
/* Just in case there is a table currently locked */ |
238
|
|
|
|
239
|
|
|
//$this->Error = @$this->linkId->error; |
240
|
|
|
//$this->Errno = @$this->linkId->errno; |
241
|
|
|
if ($this->haltOnError == 'no') |
242
|
|
|
return; |
243
|
|
|
if ($msg != '') |
244
|
|
|
$this->haltmsg($msg, $line, $file); |
245
|
|
|
if ($this->haltOnError != 'report') { |
246
|
|
|
echo '<p><b>Session halted.</b>'; |
247
|
|
|
// FIXME! Add check for error levels |
248
|
|
|
if (isset($GLOBALS['tf'])) |
249
|
|
|
$GLOBALS['tf']->terminate(); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param mixed $msg |
255
|
|
|
* @param string $line |
256
|
|
|
* @param string $file |
257
|
|
|
* @return mixed|void |
258
|
|
|
*/ |
259
|
|
|
public function logBackTrace($msg, $line = '', $file = '') { |
|
|
|
|
260
|
|
|
$backtrace = (function_exists('debug_backtrace') ? debug_backtrace() : []); |
261
|
|
|
$this->log( |
262
|
|
|
('' !== getenv('REQUEST_URI') ? ' '.getenv('REQUEST_URI') : ''). |
263
|
|
|
((isset($_POST) && count($_POST)) ? ' POST='.json_encode($_POST) : ''). |
264
|
|
|
((isset($_GET) && count($_GET)) ? ' GET='.json_encode($_GET) : ''). |
265
|
|
|
((isset($_FILES) && count($_FILES)) ? ' FILES='.json_encode($_FILES) : ''). |
266
|
|
|
('' !== getenv('HTTP_USER_AGENT') ? ' AGENT="'.getenv('HTTP_USER_AGENT').'"' : ''). |
267
|
|
|
(isset($_SERVER['REQUEST_METHOD']) ? ' METHOD="'.$_SERVER['REQUEST_METHOD'].'"'. |
268
|
|
|
($_SERVER['REQUEST_METHOD'] === 'POST' ? ' POST="'.json_encode($_POST).'"' : '') : ''), $line, $file, 'error'); |
269
|
|
|
for ($level = 1, $levelMax = count($backtrace); $level < $levelMax; $level++) { |
270
|
|
|
$message = (isset($backtrace[$level]['file']) ? 'File: '.$backtrace[$level]['file'] : ''). |
271
|
|
|
(isset($backtrace[$level]['line']) ? ' Line: '.$backtrace[$level]['line'] : ''). |
272
|
|
|
' Function: '.(isset($backtrace[$level] ['class']) ? '(class '.$backtrace[$level] ['class'].') ' : ''). |
273
|
|
|
(isset($backtrace[$level] ['type']) ? $backtrace[$level] ['type'].' ' : ''). |
274
|
|
|
$backtrace[$level] ['function'].'('; |
275
|
|
|
if (isset($backtrace[$level] ['args'])) |
276
|
|
|
for ($argument = 0, $argumentMax = count($backtrace[$level]['args']); $argument < $argumentMax; $argument++) |
277
|
|
|
$message .= ($argument > 0 ? ', ' : ''). |
278
|
|
|
(is_object($backtrace[$level]['args'][$argument]) ? 'class '.get_class($backtrace[$level]['args'][$argument]) : json_encode($backtrace[$level]['args'][$argument])); |
279
|
|
|
$message .= ')'; |
280
|
|
|
$this->log($message, $line, $file, 'error'); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param mixed $msg |
286
|
|
|
* @param string $line |
287
|
|
|
* @param string $file |
288
|
|
|
* @return mixed|void |
289
|
|
|
*/ |
290
|
|
|
public function haltmsg($msg, $line = '', $file = '') { |
291
|
|
|
$this->log("Database error: $msg", $line, $file, 'error'); |
292
|
|
|
if ($this->Errno != '0' || !in_array($this->Error, '', '()')) { |
|
|
|
|
293
|
|
|
$sqlstate = mysqli_sqlstate($this->linkId); |
|
|
|
|
294
|
|
|
$this->log("MySQLi SQLState: {$sqlstate}. Error: ".$this->Errno.' ('.$this->Error.')', $line, $file, 'error'); |
295
|
|
|
} |
296
|
|
|
$this->logBackTrace($msg, $line, $file); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return array |
301
|
|
|
*/ |
302
|
1 |
|
public function indexNames() { |
303
|
1 |
|
return []; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.