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