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