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