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