|
1
|
|
|
<?php |
|
2
|
|
|
namespace { |
|
3
|
|
|
|
|
4
|
|
|
if (!function_exists('\mysql_connect')) { |
|
5
|
|
|
define ('MYSQL_ASSOC', 1); |
|
6
|
|
|
define ('MYSQL_NUM', 2); |
|
7
|
|
|
define ('MYSQL_BOTH', 3); |
|
8
|
|
|
define ('MYSQL_CLIENT_COMPRESS', 32); |
|
9
|
|
|
define ('MYSQL_CLIENT_SSL', 2048); |
|
10
|
|
|
define ('MYSQL_CLIENT_INTERACTIVE', 1024); |
|
11
|
|
|
define ('MYSQL_CLIENT_IGNORE_SPACE', 256); |
|
12
|
|
|
|
|
13
|
|
|
function mysql_connect( |
|
14
|
|
|
$hostname = null, |
|
15
|
|
|
$username = null, |
|
16
|
|
|
$password = null, |
|
17
|
|
|
$new = false, |
|
18
|
|
|
$flags = 0 |
|
19
|
|
|
) { |
|
20
|
51 |
|
if ($new !== false) { |
|
21
|
1 |
|
trigger_error('Argument $new is no longer supported in PHP > 7', E_USER_WARNING); |
|
22
|
|
|
} |
|
23
|
50 |
|
if (null === $hostname) { |
|
24
|
1 |
|
$hostname = ini_get('mysqli.default_host') ?: null; |
|
25
|
|
|
} |
|
26
|
50 |
|
if (null === $username) { |
|
27
|
1 |
|
$username = ini_get('mysqli.default_user') ?: null; |
|
28
|
|
|
} |
|
29
|
50 |
|
if (null === $password) { |
|
30
|
48 |
|
$password = ini_get('mysqli.default_pw') ?: null; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
50 |
|
$hash = sha1($hostname . $username . $flags); |
|
34
|
50 |
|
if ($hostname{1} != ':' && isset(\Dshafik\MySQL::$connections[$hash])) { |
|
35
|
10 |
|
\Dshafik\MySQL::$last_connection = \Dshafik\MySQL::$connections[$hash]['conn']; |
|
36
|
10 |
|
\Dshafik\MySQL::$connections[$hash]['refcount'] += 1; |
|
37
|
10 |
|
return \Dshafik\MySQL::$connections[$hash]['conn']; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
41 |
|
if ($flags === 0) { |
|
41
|
39 |
|
\Dshafik\MySQL::$last_connection = $conn = mysqli_connect($hostname, $username, $password); |
|
42
|
38 |
|
$conn->hash = $hash; |
|
43
|
38 |
|
\Dshafik\MySQL::$connections[$hash] = ['refcount' => 1, 'conn' => $conn]; |
|
44
|
|
|
|
|
45
|
38 |
|
return $conn; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
2 |
|
\Dshafik\MySQL::$last_connection = $conn = mysqli_init(); |
|
50
|
|
|
|
|
51
|
2 |
|
mysqli_real_connect( |
|
52
|
|
|
$conn, |
|
53
|
|
|
$hostname, |
|
54
|
|
|
$username, |
|
55
|
|
|
$password, |
|
56
|
2 |
|
'', |
|
57
|
2 |
|
null, |
|
58
|
2 |
|
'', |
|
59
|
|
|
$flags |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
// @codeCoverageIgnoreStart |
|
63
|
|
|
// PHPUnit turns the warning from mysqli_real_connect into an exception, so this never runs |
|
64
|
|
|
if ($conn === false) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
// @codeCoverageIgnoreEnd |
|
68
|
|
|
|
|
69
|
1 |
|
$conn->hash = $hash; |
|
70
|
1 |
|
\Dshafik\MySQL::$connections[$hash] = ['refcount' => 1, 'conn' => $conn]; |
|
71
|
|
|
|
|
72
|
1 |
|
return $conn; |
|
73
|
1 |
|
} catch (\Throwable $e) { |
|
74
|
1 |
|
trigger_error($e->getMessage(), E_USER_WARNING); |
|
75
|
|
|
// @codeCoverageIgnoreStart |
|
76
|
|
|
// PHPUnit turns the warning into an exception, so this never runs |
|
77
|
|
|
return false; |
|
78
|
|
|
// @codeCoverageIgnoreEnd |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
function mysql_pconnect( |
|
83
|
|
|
$hostname = null, |
|
84
|
|
|
$username = null, |
|
85
|
|
|
$password = null, |
|
86
|
|
|
$flags = 0 |
|
87
|
|
|
) { |
|
88
|
1 |
|
$hostname = 'p:' . $hostname; |
|
89
|
1 |
|
return mysql_connect($hostname, $username, $password, false, $flags); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
function mysql_close(\mysqli $link = null) |
|
93
|
|
|
{ |
|
94
|
74 |
|
$isDefault = ($link === null); |
|
95
|
|
|
|
|
96
|
74 |
|
$link = \Dshafik\MySQL::getConnection($link, __FUNCTION__); |
|
97
|
74 |
|
if ($link === null) { |
|
98
|
|
|
// @codeCoverageIgnoreStart |
|
99
|
|
|
// PHPUnit Warning -> Exception |
|
100
|
|
|
return false; |
|
101
|
|
|
// @codeCoverageIgnoreEnd |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
74 |
|
if (isset(\Dshafik\MySQL::$connections[$link->hash])) { |
|
105
|
48 |
|
\Dshafik\MySQL::$connections[$link->hash]['refcount'] -= 1; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
74 |
|
$return = true; |
|
109
|
74 |
|
if (\Dshafik\MySQL::$connections[$link->hash]['refcount'] == 0) { |
|
110
|
65 |
|
$return = mysqli_close($link); |
|
111
|
65 |
|
unset(\Dshafik\MySQL::$connections[$link->hash]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
74 |
|
if ($isDefault) { |
|
115
|
74 |
|
Dshafik\MySQL::$last_connection = null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
74 |
|
return $return; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
function mysql_select_db($databaseName, \mysqli $link = null) |
|
122
|
|
|
{ |
|
123
|
41 |
|
$link = \Dshafik\MySQL::getConnection($link); |
|
124
|
|
|
|
|
125
|
41 |
|
return mysqli_query( |
|
126
|
|
|
$link, |
|
127
|
41 |
|
"USE " . mysqli_real_escape_string($link, $databaseName) |
|
128
|
41 |
|
) !== false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
function mysql_query($query, \mysqli $link = null) |
|
132
|
|
|
{ |
|
133
|
44 |
|
return mysqli_query(\Dshafik\MySQL::getConnection($link), $query); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
function mysql_unbuffered_query($query, \mysqli $link = null) |
|
137
|
|
|
{ |
|
138
|
4 |
|
$link = \Dshafik\MySQL::getConnection($link); |
|
139
|
4 |
|
if (mysqli_real_query($link, $query)) { |
|
140
|
3 |
|
return mysqli_use_result($link); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
function mysql_db_query($databaseName, $query, \mysqli $link = null) |
|
147
|
|
|
{ |
|
148
|
2 |
|
if (mysql_select_db($databaseName, $link)) { |
|
149
|
1 |
|
return mysql_query($query, $link); |
|
150
|
|
|
} |
|
151
|
1 |
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
function mysql_list_dbs(\mysqli $link = null) |
|
155
|
|
|
{ |
|
156
|
1 |
|
return mysql_query("SHOW DATABASES", $link); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
function mysql_list_tables($databaseName, \mysqli $link = null) |
|
160
|
|
|
{ |
|
161
|
2 |
|
$link = \Dshafik\MySQL::getConnection($link); |
|
162
|
2 |
|
return mysql_query("SHOW TABLES FROM " . mysql_real_escape_string($databaseName, $link), $link); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
function mysql_list_fields($databaseName, $tableName, \mysqli $link = null) |
|
166
|
|
|
{ |
|
167
|
2 |
|
$link = \Dshafik\MySQL::getConnection($link); |
|
168
|
2 |
|
$result = mysql_query( |
|
169
|
|
|
"SHOW COLUMNS FROM " . |
|
170
|
2 |
|
mysqli_real_escape_string($link, $databaseName) . "." . |
|
171
|
2 |
|
mysqli_real_escape_string($link, $tableName), |
|
172
|
|
|
$link |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
2 |
|
if ($result instanceof \mysqli_result) { |
|
176
|
1 |
|
$result->table = $tableName; |
|
177
|
1 |
|
return $result; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
trigger_error("mysql_list_fields(): Unable to save MySQL query result", E_USER_WARNING); |
|
181
|
|
|
// @codeCoverageIgnoreStart |
|
182
|
|
|
return false; |
|
183
|
|
|
// @codeCoverageIgnoreEnd |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
function mysql_list_processes(\mysqli $link = null) |
|
187
|
|
|
{ |
|
188
|
|
|
return mysql_query("SHOW PROCESSLIST", $link); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
function mysql_error(\mysqli $link = null) |
|
192
|
|
|
{ |
|
193
|
27 |
|
return mysqli_error(\Dshafik\MySQL::getConnection($link)); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
function mysql_errno(\mysqli $link = null) |
|
197
|
|
|
{ |
|
198
|
1 |
|
return mysqli_errno(\Dshafik\MySQL::getConnection($link)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
function mysql_affected_rows(\mysqli $link = null) |
|
202
|
|
|
{ |
|
203
|
1 |
|
return mysqli_affected_rows(\Dshafik\MySQL::getConnection($link)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
function mysql_insert_id($link = null) /*|*/ |
|
207
|
|
|
{ |
|
208
|
1 |
|
return mysqli_insert_id(\Dshafik\MySQL::getConnection($link)); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
function mysql_result($result, $row, $field = 0) |
|
212
|
|
|
{ |
|
213
|
6 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
214
|
|
|
// @codeCoverageIgnoreStart |
|
215
|
|
|
return false; |
|
216
|
|
|
// @codeCoverageIgnoreEnd |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
5 |
|
if (!mysqli_data_seek($result, $row)) { |
|
220
|
1 |
|
trigger_error( |
|
221
|
|
|
sprintf( |
|
222
|
1 |
|
"mysql_result(): Unable to jump to row %d on MySQL result index %s", |
|
223
|
|
|
$row, |
|
224
|
|
|
spl_object_hash($result) |
|
225
|
|
|
), |
|
226
|
1 |
|
E_USER_WARNING |
|
227
|
|
|
); |
|
228
|
|
|
// @codeCoverageIgnoreStart |
|
229
|
|
|
return false; |
|
230
|
|
|
// @codeCoverageIgnoreEnd |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
4 |
|
$found = true; |
|
234
|
4 |
|
if (strpos($field, ".") !== false) { |
|
235
|
2 |
|
list($table, $name) = explode(".", $field); |
|
236
|
2 |
|
$i = 0; |
|
237
|
2 |
|
$found = false; |
|
238
|
2 |
|
while ($column = mysqli_fetch_field($result)) { |
|
239
|
2 |
|
if ($column->table == $table && $column->name == $name) { |
|
240
|
1 |
|
$field = $i; |
|
241
|
1 |
|
$found = true; |
|
242
|
1 |
|
break; |
|
243
|
|
|
} |
|
244
|
1 |
|
$i++; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
4 |
|
$row = mysql_fetch_array($result); |
|
249
|
4 |
|
if ($found && isset($row[$field])) { |
|
250
|
2 |
|
return $row[$field]; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
2 |
|
trigger_error( |
|
254
|
|
|
sprintf( |
|
255
|
2 |
|
"%s(): %s not found in MySQL result index %s", |
|
256
|
2 |
|
__FUNCTION__, |
|
257
|
|
|
$field, |
|
258
|
|
|
spl_object_hash($result) |
|
259
|
|
|
), |
|
260
|
2 |
|
E_USER_WARNING |
|
261
|
|
|
); |
|
262
|
|
|
// @codeCoverageIgnoreStart |
|
263
|
|
|
return false; |
|
264
|
|
|
// @codeCoverageIgnoreEnd |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
function mysql_num_rows($result) |
|
268
|
|
|
{ |
|
269
|
12 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
270
|
|
|
// @codeCoverageIgnoreStart |
|
271
|
|
|
return false; |
|
272
|
|
|
// @codeCoverageIgnoreEnd |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
11 |
|
$previous = error_reporting(0); |
|
276
|
11 |
|
$rows = mysqli_num_rows($result); |
|
277
|
11 |
|
error_reporting($previous); |
|
278
|
|
|
|
|
279
|
11 |
|
return $rows; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
function mysql_num_fields($result) |
|
283
|
|
|
{ |
|
284
|
3 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
285
|
|
|
// @codeCoverageIgnoreStart |
|
286
|
|
|
return false; |
|
287
|
|
|
// @codeCoverageIgnoreEnd |
|
288
|
|
|
} |
|
289
|
1 |
|
return mysqli_num_fields($result); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
function mysql_fetch_row($result) |
|
293
|
|
|
{ |
|
294
|
6 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
295
|
|
|
// @codeCoverageIgnoreStart |
|
296
|
|
|
return false; |
|
297
|
|
|
// @codeCoverageIgnoreEnd |
|
298
|
|
|
} |
|
299
|
5 |
|
return mysqli_fetch_row($result) ?: false; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
function mysql_fetch_array($result) |
|
303
|
|
|
{ |
|
304
|
7 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
305
|
|
|
// @codeCoverageIgnoreStart |
|
306
|
|
|
return false; |
|
307
|
|
|
// @codeCoverageIgnoreEnd |
|
308
|
|
|
} |
|
309
|
6 |
|
return mysqli_fetch_array($result) ?: false; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
function mysql_fetch_assoc($result) /* : array|null */ |
|
313
|
|
|
{ |
|
314
|
7 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
315
|
|
|
// @codeCoverageIgnoreStart |
|
316
|
|
|
return false; |
|
317
|
|
|
// @codeCoverageIgnoreEnd |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
6 |
|
return mysqli_fetch_assoc($result) ?: false; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
function mysql_fetch_object($result, $class = null, array $params = []) /* : object|null */ |
|
324
|
|
|
{ |
|
325
|
3 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
326
|
|
|
// @codeCoverageIgnoreStart |
|
327
|
|
|
return false; |
|
328
|
|
|
// @codeCoverageIgnoreEnd |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
2 |
|
if ($class == null) { |
|
332
|
2 |
|
$object = mysqli_fetch_object($result); |
|
333
|
|
|
} else { |
|
334
|
|
|
$object = mysqli_fetch_object($result, $class, $params); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
2 |
|
return $object ?: false; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
function mysql_data_seek($result, $offset) |
|
341
|
|
|
{ |
|
342
|
1 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
343
|
|
|
// @codeCoverageIgnoreStart |
|
344
|
|
|
return false; |
|
345
|
|
|
// @codeCoverageIgnoreEnd |
|
346
|
|
|
} |
|
347
|
|
|
return mysqli_data_seek($result, $offset); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
function mysql_fetch_lengths($result) /* : array|*/ |
|
351
|
|
|
{ |
|
352
|
1 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
353
|
|
|
// @codeCoverageIgnoreStart |
|
354
|
|
|
return false; |
|
355
|
|
|
// @codeCoverageIgnoreEnd |
|
356
|
|
|
} |
|
357
|
|
|
return mysqli_fetch_lengths($result); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
function mysql_fetch_field($result) /* : object|*/ |
|
361
|
|
|
{ |
|
362
|
1 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
363
|
|
|
// @codeCoverageIgnoreStart |
|
364
|
|
|
return false; |
|
365
|
|
|
// @codeCoverageIgnoreEnd |
|
366
|
|
|
} |
|
367
|
|
|
return mysqli_fetch_field($result); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
function mysql_field_seek($result, $field) |
|
371
|
|
|
{ |
|
372
|
1 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
373
|
|
|
// @codeCoverageIgnoreStart |
|
374
|
|
|
return false; |
|
375
|
|
|
// @codeCoverageIgnoreEnd |
|
376
|
|
|
} |
|
377
|
|
|
return mysqli_field_seek($result, $field); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
function mysql_free_result($result) |
|
381
|
|
|
{ |
|
382
|
2 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
383
|
|
|
// @codeCoverageIgnoreStart |
|
384
|
|
|
return false; |
|
385
|
|
|
// @codeCoverageIgnoreEnd |
|
386
|
|
|
} |
|
387
|
1 |
|
return mysqli_free_result($result); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
function mysql_field_name($result, $field) |
|
391
|
|
|
{ |
|
392
|
3 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
393
|
|
|
// @codeCoverageIgnoreStart |
|
394
|
|
|
return false; |
|
395
|
|
|
// @codeCoverageIgnoreEnd |
|
396
|
|
|
} |
|
397
|
2 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'name'); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
function mysql_field_table($result, $field) |
|
401
|
|
|
{ |
|
402
|
3 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
403
|
|
|
// @codeCoverageIgnoreStart |
|
404
|
|
|
return false; |
|
405
|
|
|
// @codeCoverageIgnoreEnd |
|
406
|
|
|
} |
|
407
|
2 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'table'); |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
function mysql_field_len($result, $field) |
|
411
|
|
|
{ |
|
412
|
3 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
413
|
|
|
// @codeCoverageIgnoreStart |
|
414
|
|
|
return false; |
|
415
|
|
|
// @codeCoverageIgnoreEnd |
|
416
|
|
|
} |
|
417
|
2 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'len'); |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
function mysql_field_type($result, $field) |
|
421
|
|
|
{ |
|
422
|
3 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
423
|
|
|
// @codeCoverageIgnoreStart |
|
424
|
|
|
return false; |
|
425
|
|
|
// @codeCoverageIgnoreEnd |
|
426
|
|
|
} |
|
427
|
2 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'type'); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
function mysql_field_flags($result, $field) |
|
431
|
|
|
{ |
|
432
|
3 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
433
|
|
|
// @codeCoverageIgnoreStart |
|
434
|
|
|
return false; |
|
435
|
|
|
// @codeCoverageIgnoreEnd |
|
436
|
|
|
} |
|
437
|
2 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'flags'); |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
function mysql_escape_string($unescapedString) |
|
441
|
|
|
{ |
|
442
|
2 |
|
if (\Dshafik\MySQL::$last_connection == null) { |
|
443
|
2 |
|
trigger_error( |
|
444
|
|
|
sprintf( |
|
445
|
2 |
|
"%s() is insecure; use mysql_real_escape_string() instead!", |
|
446
|
2 |
|
__FUNCTION__ |
|
447
|
|
|
), |
|
448
|
2 |
|
E_USER_NOTICE |
|
449
|
|
|
); |
|
450
|
|
|
|
|
451
|
1 |
|
return \Dshafik\MySQL::escapeString($unescapedString); |
|
452
|
|
|
} |
|
453
|
|
|
return mysql_real_escape_string($unescapedString, null); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
function mysql_real_escape_string($unescapedString, \mysqli $link = null) |
|
457
|
|
|
{ |
|
458
|
2 |
|
return mysqli_escape_string(\Dshafik\MySQL::getConnection($link), $unescapedString); |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
function mysql_stat(\mysqli $link = null) |
|
462
|
|
|
{ |
|
463
|
|
|
return mysqli_stat(\Dshafik\MySQL::getConnection($link)); |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
function mysql_thread_id(\mysqli $link = null) |
|
467
|
|
|
{ |
|
468
|
|
|
return mysqli_thread_id(\Dshafik\MySQL::getConnection($link)); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
function mysql_client_encoding(\mysqli $link = null) |
|
472
|
|
|
{ |
|
473
|
|
|
return mysqli_character_set_name(\Dshafik\MySQL::getConnection($link)); |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
function mysql_ping(\mysqli $link = null) |
|
477
|
|
|
{ |
|
478
|
|
|
return mysqli_ping($link); |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
function mysql_get_client_info(\mysqli $link = null) |
|
482
|
|
|
{ |
|
483
|
|
|
return mysqli_get_client_info(\Dshafik\MySQL::getConnection($link)); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
function mysql_get_host_info(\mysqli $link = null) |
|
487
|
|
|
{ |
|
488
|
|
|
return mysqli_get_host_info(\Dshafik\MySQL::getConnection($link)); |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
function mysql_get_proto_info(\mysqli $link = null) |
|
492
|
|
|
{ |
|
493
|
|
|
return mysqli_get_proto_info(\Dshafik\MySQL::getConnection($link)); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
function mysql_get_server_info(\mysqli $link = null) |
|
497
|
|
|
{ |
|
498
|
|
|
return mysqli_get_server_info(\Dshafik\MySQL::getConnection($link)); |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
function mysql_info(\mysqli $link = null) |
|
502
|
|
|
{ |
|
503
|
|
|
return mysqli_info(\Dshafik\MySQL::getConnection($link)); |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
function mysql_set_charset($charset, \mysqli $link = null) |
|
507
|
|
|
{ |
|
508
|
|
|
return mysqli_set_charset(\Dshafik\MySQL::getConnection($link), $charset); |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
function mysql_db_name($result, $row) |
|
512
|
|
|
{ |
|
513
|
1 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
514
|
|
|
// @codeCoverageIgnoreStart |
|
515
|
|
|
return false; |
|
516
|
|
|
// @codeCoverageIgnoreEnd |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
// Alias as per http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#319 |
|
520
|
|
|
return mysql_result($result, $row, 'Database'); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
function mysql_tablename($result, $row) |
|
524
|
|
|
{ |
|
525
|
1 |
|
if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
|
526
|
|
|
// @codeCoverageIgnoreStart |
|
527
|
|
|
return false; |
|
528
|
|
|
// @codeCoverageIgnoreEnd |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
// Alias as per http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#321 |
|
532
|
|
|
return mysql_result($result, $row, 'Table'); |
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
|
|
/* Aliases */ |
|
536
|
|
|
|
|
537
|
|
|
function mysql_fieldname(... $args) |
|
538
|
|
|
{ |
|
539
|
|
|
return mysql_field_name(... $args); |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
function mysql_fieldtable(... $args) |
|
543
|
|
|
{ |
|
544
|
|
|
return mysql_field_table(... $args); |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
function mysql_fieldlen(... $args) |
|
548
|
|
|
{ |
|
549
|
|
|
return mysql_field_len(... $args); |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
function mysql_fieldtype(... $args) |
|
553
|
|
|
{ |
|
554
|
|
|
return mysql_field_type(... $args); |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
function mysql_fieldflags(... $args) |
|
558
|
|
|
{ |
|
559
|
|
|
return mysql_field_flags(... $args); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
function mysql_selectdb(... $args) |
|
563
|
|
|
{ |
|
564
|
|
|
return mysql_select_db(... $args); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
function mysql_freeresult(... $args) |
|
568
|
|
|
{ |
|
569
|
|
|
return mysql_free_result(... $args); |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
function mysql_numfields(... $args) |
|
573
|
|
|
{ |
|
574
|
|
|
return mysql_num_fields(... $args); |
|
575
|
|
|
} |
|
576
|
|
|
|
|
577
|
|
|
function mysql_numrows(... $args) |
|
578
|
|
|
{ |
|
579
|
|
|
return mysql_num_rows(... $args); |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
function mysql_listdbs(... $args) |
|
583
|
|
|
{ |
|
584
|
|
|
return mysql_list_dbs(... $args); |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
function mysql_listtables(... $args) |
|
588
|
|
|
{ |
|
589
|
|
|
return mysql_list_tables(... $args); |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
function mysql_listfields(... $args) |
|
593
|
|
|
{ |
|
594
|
|
|
return mysql_list_fields(... $args); |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
function mysql_dbname(... $args) |
|
598
|
|
|
{ |
|
599
|
|
|
return mysql_db_name(... $args); |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
function mysql_table_name(... $args) |
|
603
|
|
|
{ |
|
604
|
|
|
return mysql_tablename(... $args); |
|
605
|
|
|
} |
|
606
|
|
|
} |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
namespace Dshafik { |
|
610
|
|
|
|
|
611
|
|
|
class MySQL |
|
612
|
|
|
{ |
|
613
|
|
|
public static $last_connection = null; |
|
614
|
|
|
public static $connections = []; |
|
615
|
|
|
|
|
616
|
74 |
|
public static function getConnection($link = null, $func = null) |
|
617
|
|
|
{ |
|
618
|
74 |
|
if ($link !== null) { |
|
619
|
7 |
|
return $link; |
|
620
|
|
|
} |
|
621
|
|
|
|
|
622
|
74 |
|
if (static::$last_connection === null) { |
|
623
|
26 |
|
$err = "A link to the server could not be established"; |
|
624
|
26 |
|
if ($func !== null) { |
|
625
|
26 |
|
$err = $func . "(): no MySQL-Link resource supplied"; |
|
626
|
|
|
} |
|
627
|
26 |
|
trigger_error($err, E_USER_WARNING); |
|
628
|
26 |
|
return false; |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
49 |
|
return static::$last_connection; |
|
632
|
|
|
} |
|
633
|
|
|
|
|
634
|
6 |
|
public static function mysqlFieldInfo(\mysqli_result $result, $field, $what) |
|
635
|
|
|
{ |
|
636
|
|
|
try { |
|
637
|
6 |
|
$field = mysqli_fetch_field_direct($result, $field); |
|
638
|
5 |
|
} catch (\Exception $e) { |
|
639
|
5 |
|
trigger_error( |
|
640
|
|
|
sprintf( |
|
641
|
5 |
|
"mysql_field_%s(): Field %d is invalid for MySQL result index %s", |
|
642
|
|
|
$what, |
|
643
|
|
|
$field, |
|
644
|
|
|
spl_object_hash($result) |
|
645
|
|
|
), |
|
646
|
5 |
|
E_USER_WARNING |
|
647
|
|
|
); |
|
648
|
|
|
// @codeCoverageIgnoreStart |
|
649
|
|
|
// PHPUnit turns the warning into an exception, so this never runs |
|
650
|
|
|
return false; |
|
651
|
|
|
// @codeCoverageIgnoreEnd |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
1 |
|
if ($what == 'name' || $what == 'table') { |
|
655
|
1 |
|
return $field->{$what}; |
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
1 |
|
if ($what == 'len') { |
|
659
|
1 |
|
return $field->length; |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
1 |
|
if ($what == 'type') { |
|
663
|
1 |
|
return static::getFieldType($field->type); |
|
664
|
|
|
} |
|
665
|
|
|
|
|
666
|
1 |
|
if ($what == 'flags') { |
|
667
|
1 |
|
return static::getFieldFlags($field->flags); |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
return false; |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
49 |
|
public static function checkValidResult($result, $function) |
|
674
|
|
|
{ |
|
675
|
49 |
|
if (!($result instanceof \mysqli_result)) { |
|
676
|
20 |
|
if ($function != "mysql_fetch_object") { |
|
677
|
19 |
|
trigger_error( |
|
678
|
19 |
|
$function . "() expects parameter 1 to be resource, " . strtolower(gettype($result)) . " given", |
|
679
|
19 |
|
E_USER_WARNING |
|
680
|
|
|
); |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
1 |
|
if ($function == "mysql_fetch_object") { |
|
684
|
1 |
|
trigger_error( |
|
685
|
1 |
|
$function . "(): supplied argument is not a valid MySQL result resource", |
|
686
|
1 |
|
E_USER_WARNING |
|
687
|
|
|
); |
|
688
|
|
|
} |
|
689
|
|
|
return false; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
29 |
|
} |
|
693
|
|
|
|
|
694
|
1 |
|
public static function escapeString($unescapedString) |
|
695
|
|
|
{ |
|
696
|
1 |
|
$escapedString = ""; |
|
697
|
1 |
|
for ($i = 0; $i < strlen($unescapedString); $i++) { |
|
698
|
1 |
|
$escapedString .= self::escapeChar($unescapedString{$i}); |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
1 |
|
return $escapedString; |
|
702
|
|
|
} |
|
703
|
|
|
|
|
704
|
1 |
|
protected static function getFieldFlags($what) |
|
705
|
|
|
{ |
|
706
|
|
|
// Order of flags taken from http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#2507 |
|
707
|
|
|
$flags = [ |
|
708
|
1 |
|
MYSQLI_NOT_NULL_FLAG => "not_null", |
|
709
|
1 |
|
MYSQLI_PRI_KEY_FLAG => "primary_key", |
|
710
|
1 |
|
MYSQLI_UNIQUE_KEY_FLAG => "unique_key", |
|
711
|
1 |
|
MYSQLI_MULTIPLE_KEY_FLAG => "multiple_key", |
|
712
|
1 |
|
MYSQLI_BLOB_FLAG => "blob", |
|
713
|
1 |
|
MYSQLI_UNSIGNED_FLAG => "unsigned", |
|
714
|
1 |
|
MYSQLI_ZEROFILL_FLAG => "zerofill", |
|
715
|
1 |
|
MYSQLI_BINARY_FLAG => "binary", |
|
716
|
1 |
|
MYSQLI_ENUM_FLAG => "enum", |
|
717
|
1 |
|
MYSQLI_SET_FLAG => "set", |
|
718
|
1 |
|
MYSQLI_AUTO_INCREMENT_FLAG => "auto_increment", |
|
719
|
1 |
|
MYSQLI_TIMESTAMP_FLAG => "timestamp", |
|
720
|
|
|
]; |
|
721
|
|
|
|
|
722
|
1 |
|
$fieldFlags = []; |
|
723
|
1 |
|
foreach ($flags as $flag => $value) { |
|
724
|
1 |
|
if ($what & $flag) { |
|
725
|
1 |
|
$fieldFlags[] = $value; |
|
726
|
|
|
} |
|
727
|
|
|
} |
|
728
|
|
|
|
|
729
|
1 |
|
return implode(" ", $fieldFlags); |
|
730
|
|
|
} |
|
731
|
|
|
|
|
732
|
1 |
|
protected static function getFieldType($what) |
|
733
|
|
|
{ |
|
734
|
|
|
$types = [ |
|
735
|
1 |
|
MYSQLI_TYPE_STRING => 'string', |
|
736
|
1 |
|
MYSQLI_TYPE_VAR_STRING => 'string', |
|
737
|
1 |
|
MYSQLI_TYPE_ENUM => 'string', |
|
738
|
1 |
|
MYSQLI_TYPE_SET => 'string', |
|
739
|
|
|
|
|
740
|
1 |
|
MYSQLI_TYPE_LONG => 'int', |
|
741
|
1 |
|
MYSQLI_TYPE_TINY => 'int', |
|
742
|
1 |
|
MYSQLI_TYPE_SHORT => 'int', |
|
743
|
1 |
|
MYSQLI_TYPE_INT24 => 'int', |
|
744
|
1 |
|
MYSQLI_TYPE_CHAR => 'int', |
|
745
|
1 |
|
MYSQLI_TYPE_LONGLONG => 'int', |
|
746
|
|
|
|
|
747
|
1 |
|
MYSQLI_TYPE_DECIMAL => 'real', |
|
748
|
1 |
|
MYSQLI_TYPE_FLOAT => 'real', |
|
749
|
1 |
|
MYSQLI_TYPE_DOUBLE => 'real', |
|
750
|
1 |
|
MYSQLI_TYPE_NEWDECIMAL => 'real', |
|
751
|
|
|
|
|
752
|
1 |
|
MYSQLI_TYPE_TINY_BLOB => 'blob', |
|
753
|
1 |
|
MYSQLI_TYPE_MEDIUM_BLOB => 'blob', |
|
754
|
1 |
|
MYSQLI_TYPE_LONG_BLOB => 'blob', |
|
755
|
1 |
|
MYSQLI_TYPE_BLOB => 'blob', |
|
756
|
|
|
|
|
757
|
1 |
|
MYSQLI_TYPE_NEWDATE => 'date', |
|
758
|
1 |
|
MYSQLI_TYPE_DATE => 'date', |
|
759
|
1 |
|
MYSQLI_TYPE_TIME => 'time', |
|
760
|
1 |
|
MYSQLI_TYPE_YEAR => 'year', |
|
761
|
1 |
|
MYSQLI_TYPE_DATETIME => 'datetime', |
|
762
|
1 |
|
MYSQLI_TYPE_TIMESTAMP => 'timestamp', |
|
763
|
|
|
|
|
764
|
1 |
|
MYSQLI_TYPE_NULL => 'null', |
|
765
|
|
|
|
|
766
|
1 |
|
MYSQLI_TYPE_GEOMETRY => 'geometry', |
|
767
|
|
|
]; |
|
768
|
|
|
|
|
769
|
1 |
|
return isset($types[$what]) ? $types[$what] : "unknown"; |
|
770
|
|
|
} |
|
771
|
|
|
|
|
772
|
1 |
|
protected static function escapeChar($char) |
|
773
|
|
|
{ |
|
774
|
|
|
switch ($char) { |
|
775
|
1 |
|
case "\0": |
|
776
|
1 |
|
$esc = "\\0"; |
|
777
|
1 |
|
break; |
|
778
|
|
|
case "\n": |
|
779
|
1 |
|
$esc = "\\n"; |
|
780
|
1 |
|
break; |
|
781
|
|
|
case "\r": |
|
782
|
1 |
|
$esc = "\\r"; |
|
783
|
1 |
|
break; |
|
784
|
|
|
case '\\': |
|
785
|
|
|
case '\'': |
|
786
|
|
|
case '"': |
|
787
|
1 |
|
$esc = "\\{$char}"; |
|
788
|
1 |
|
break; |
|
789
|
1 |
|
case "\032": |
|
790
|
1 |
|
$esc = "\\Z"; |
|
791
|
1 |
|
break; |
|
792
|
|
|
default: |
|
793
|
1 |
|
$esc = $char; |
|
794
|
1 |
|
break; |
|
795
|
|
|
} |
|
796
|
|
|
|
|
797
|
1 |
|
return $esc; |
|
798
|
|
|
} |
|
799
|
|
|
} |
|
800
|
|
|
} |
|
801
|
|
|
|