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