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