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
|
65 |
|
if ($new !== false) { |
44
|
1 |
|
trigger_error('Argument $new is no longer supported in PHP > 7', E_USER_WARNING); |
45
|
|
|
} |
46
|
|
|
|
47
|
64 |
|
if (null === $hostname) { |
48
|
1 |
|
$hostname = ini_get('mysqli.default_host') ?: null; |
49
|
|
|
} |
50
|
64 |
|
if (null === $username) { |
51
|
1 |
|
$username = ini_get('mysqli.default_user') ?: null; |
52
|
|
|
} |
53
|
64 |
|
if (null === $password) { |
54
|
61 |
|
$password = ini_get('mysqli.default_pw') ?: null; |
55
|
|
|
} |
56
|
|
|
|
57
|
64 |
|
$hash = sha1($hostname . $username . $flags); |
58
|
|
|
/* persistent connections start with p: */ |
59
|
|
|
/* don't use a cached link for those */ |
60
|
64 |
|
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
|
55 |
|
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
|
55 |
|
$port = null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/* No flags, means we can use mysqli_connect() */ |
75
|
55 |
|
if ($flags === 0) { |
76
|
53 |
|
$conn = mysqli_connect($hostname, $username, $password, '', $port); |
77
|
52 |
|
if (!$conn instanceof mysqli) { |
78
|
1 |
|
return false; |
79
|
|
|
} |
80
|
51 |
|
\Dshafik\MySQL::$last_connection = $conn; |
81
|
51 |
|
$conn->hash = $hash; |
82
|
51 |
|
\Dshafik\MySQL::$connections[$hash] = array('refcount' => 1, 'conn' => $conn); |
83
|
|
|
|
84
|
51 |
|
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
|
88 |
|
$isDefault = ($link === null); |
135
|
|
|
|
136
|
88 |
|
$link = \Dshafik\MySQL::getConnection($link, __FUNCTION__); |
137
|
88 |
|
if ($link === null) { |
138
|
|
|
// @codeCoverageIgnoreStart |
139
|
|
|
// PHPUnit Warning -> Exception |
140
|
|
|
return false; |
141
|
|
|
// @codeCoverageIgnoreEnd |
142
|
|
|
} |
143
|
|
|
|
144
|
88 |
|
if (isset(\Dshafik\MySQL::$connections[$link->hash])) { |
145
|
61 |
|
\Dshafik\MySQL::$connections[$link->hash]['refcount'] -= 1; |
146
|
|
|
} |
147
|
|
|
|
148
|
88 |
|
$return = true; |
149
|
88 |
|
if (\Dshafik\MySQL::$connections[$link->hash]['refcount'] === 0) { |
150
|
52 |
|
$return = mysqli_close($link); |
151
|
52 |
|
unset(\Dshafik\MySQL::$connections[$link->hash]); |
152
|
|
|
} |
153
|
|
|
|
154
|
88 |
|
if ($isDefault) { |
155
|
88 |
|
Dshafik\MySQL::$last_connection = null; |
156
|
|
|
} |
157
|
|
|
|
158
|
88 |
|
return $return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
function mysql_select_db($databaseName, \mysqli $link = null) |
162
|
|
|
{ |
163
|
54 |
|
$link = \Dshafik\MySQL::getConnection($link); |
164
|
|
|
|
165
|
54 |
|
return mysqli_query( |
166
|
54 |
|
$link, |
167
|
54 |
|
'USE `' . mysqli_real_escape_string($link, $databaseName) . '`' |
168
|
54 |
|
) !== false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
function mysql_query($query, \mysqli $link = null) |
172
|
|
|
{ |
173
|
57 |
|
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
|
4 |
|
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
410
|
|
|
// @codeCoverageIgnoreStart |
411
|
|
|
return false; |
412
|
|
|
// @codeCoverageIgnoreEnd |
413
|
|
|
} |
414
|
2 |
|
$res = mysqli_fetch_field($result); |
415
|
2 |
|
if ($res instanceof \stdClass) { |
416
|
2 |
|
$res->not_null = ($res->flags & MYSQLI_NOT_NULL_FLAG) ? 1 : 0; |
417
|
2 |
|
$res->primary_key = ($res->flags & MYSQLI_PRI_KEY_FLAG ) ? 1 : 0; |
418
|
2 |
|
$res->unique_key = ($res->flags & MYSQLI_UNIQUE_KEY_FLAG ) ? 1 : 0; |
419
|
2 |
|
$res->multiple_key = ($res->flags & MYSQLI_MULTIPLE_KEY_FLAG ) ? 1 : 0; |
420
|
2 |
|
$res->numeric = ($res->flags & MYSQLI_NUM_FLAG ) ? 1 : 0; |
421
|
2 |
|
$res->blob = ($res->flags & MYSQLI_BLOB_FLAG ) ? 1 : 0; |
422
|
2 |
|
$res->unsigned = ($res->flags & MYSQLI_UNSIGNED_FLAG ) ? 1 : 0; |
423
|
2 |
|
$res->zerofill = ($res->flags & MYSQLI_ZEROFILL_FLAG ) ? 1 : 0; |
424
|
|
|
} |
425
|
2 |
|
return $res; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
function mysql_field_seek($result, $field) |
429
|
|
|
{ |
430
|
1 |
|
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
431
|
|
|
// @codeCoverageIgnoreStart |
432
|
|
|
return false; |
433
|
|
|
// @codeCoverageIgnoreEnd |
434
|
|
|
} |
435
|
|
|
return mysqli_field_seek($result, $field); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
function mysql_free_result($result) |
439
|
|
|
{ |
440
|
2 |
|
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
441
|
|
|
// @codeCoverageIgnoreStart |
442
|
|
|
return false; |
443
|
|
|
// @codeCoverageIgnoreEnd |
444
|
|
|
} |
445
|
1 |
|
return mysqli_free_result($result); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
View Code Duplication |
function mysql_field_name($result, $field) |
449
|
|
|
{ |
450
|
4 |
|
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
451
|
|
|
// @codeCoverageIgnoreStart |
452
|
|
|
return false; |
453
|
|
|
// @codeCoverageIgnoreEnd |
454
|
|
|
} |
455
|
3 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'name'); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
function mysql_field_table($result, $field) |
459
|
|
|
{ |
460
|
4 |
|
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
461
|
|
|
// @codeCoverageIgnoreStart |
462
|
|
|
return false; |
463
|
|
|
// @codeCoverageIgnoreEnd |
464
|
|
|
} |
465
|
3 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'table'); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
function mysql_field_len($result, $field) |
469
|
|
|
{ |
470
|
4 |
|
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
471
|
|
|
// @codeCoverageIgnoreStart |
472
|
|
|
return false; |
473
|
|
|
// @codeCoverageIgnoreEnd |
474
|
|
|
} |
475
|
3 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'length'); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
View Code Duplication |
function mysql_field_type($result, $field) |
479
|
|
|
{ |
480
|
4 |
|
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
481
|
|
|
// @codeCoverageIgnoreStart |
482
|
|
|
return false; |
483
|
|
|
// @codeCoverageIgnoreEnd |
484
|
|
|
} |
485
|
3 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'type'); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
function mysql_field_flags($result, $field) |
489
|
|
|
{ |
490
|
4 |
|
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) { |
491
|
|
|
// @codeCoverageIgnoreStart |
492
|
|
|
return false; |
493
|
|
|
// @codeCoverageIgnoreEnd |
494
|
|
|
} |
495
|
3 |
|
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'flags'); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
function mysql_escape_string($unescapedString) |
499
|
|
|
{ |
500
|
2 |
|
if (\Dshafik\MySQL::$last_connection === null) { |
501
|
2 |
|
trigger_error( |
502
|
2 |
|
sprintf( |
503
|
2 |
|
'%s() is insecure; use mysql_real_escape_string() instead!', |
504
|
2 |
|
__FUNCTION__ |
505
|
|
|
), |
506
|
2 |
|
E_USER_NOTICE |
507
|
|
|
); |
508
|
|
|
|
509
|
1 |
|
return \Dshafik\MySQL::escapeString($unescapedString); |
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
|
88 |
|
public static function getConnection($link = null, $func = null) |
675
|
|
|
{ |
676
|
88 |
|
if ($link !== null) { |
677
|
9 |
|
return $link; |
678
|
|
|
} |
679
|
|
|
|
680
|
88 |
|
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
|
27 |
|
return false; |
687
|
|
|
} |
688
|
|
|
|
689
|
62 |
|
return static::$last_connection; |
690
|
|
|
} |
691
|
|
|
|
692
|
7 |
|
public static function mysqlFieldInfo(\mysqli_result $result, $field, $what) |
693
|
|
|
{ |
694
|
|
|
try { |
695
|
7 |
|
$field = mysqli_fetch_field_direct($result, $field); |
696
|
5 |
|
} catch (\Exception $e) { |
697
|
5 |
|
trigger_error( |
698
|
5 |
|
sprintf( |
699
|
5 |
|
'mysql_field_%s(): Field %d is invalid for MySQL result index %s', |
700
|
5 |
|
($what !== 'length') ? $what : 'len', |
701
|
|
|
$field, |
702
|
5 |
|
spl_object_hash($result) |
703
|
|
|
), |
704
|
5 |
|
E_USER_WARNING |
705
|
|
|
); |
706
|
|
|
// @codeCoverageIgnoreStart |
707
|
|
|
// PHPUnit turns the warning into an exception, so this never runs |
708
|
|
|
return false; |
709
|
|
|
// @codeCoverageIgnoreEnd |
710
|
|
|
} |
711
|
|
|
|
712
|
2 |
|
if ($what === 'type') { |
713
|
2 |
|
return static::getFieldType($field->type); |
714
|
|
|
} |
715
|
|
|
|
716
|
2 |
|
if ($what === 'flags') { |
717
|
2 |
|
return static::getFieldFlags($field->flags); |
718
|
|
|
} |
719
|
|
|
|
720
|
2 |
|
if (isset($field->{$what})) { |
721
|
2 |
|
return $field->{$what}; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
return false; |
725
|
|
|
} |
726
|
|
|
|
727
|
59 |
|
public static function checkValidResult($result, $function) |
728
|
|
|
{ |
729
|
59 |
|
if (!($result instanceof \mysqli_result)) { |
730
|
21 |
|
if ($function !== 'mysql_fetch_object') { |
731
|
20 |
|
trigger_error( |
732
|
20 |
|
$function . '() expects parameter 1 to be resource, ' . strtolower(gettype($result)) . ' given', |
733
|
20 |
|
E_USER_WARNING |
734
|
|
|
); |
735
|
|
|
} |
736
|
|
|
|
737
|
1 |
|
if ($function === 'mysql_fetch_object') { |
738
|
1 |
|
trigger_error( |
739
|
1 |
|
$function . '(): supplied argument is not a valid MySQL result resource', |
740
|
1 |
|
E_USER_WARNING |
741
|
|
|
); |
742
|
|
|
} |
743
|
|
|
return false; |
744
|
|
|
} |
745
|
|
|
|
746
|
38 |
|
return true; |
747
|
|
|
} |
748
|
|
|
|
749
|
1 |
|
public static function escapeString($unescapedString) |
750
|
|
|
{ |
751
|
1 |
|
$escapedString = ''; |
752
|
1 |
|
for ($i = 0, $max = strlen($unescapedString); $i < $max; $i++) { |
753
|
1 |
|
$escapedString .= self::escapeChar($unescapedString[$i]); |
754
|
|
|
} |
755
|
|
|
|
756
|
1 |
|
return $escapedString; |
757
|
|
|
} |
758
|
|
|
|
759
|
2 |
|
protected static function getFieldFlags($what) |
760
|
|
|
{ |
761
|
|
|
// Order of flags taken from http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#2507 |
762
|
|
|
$flags = array( |
763
|
2 |
|
MYSQLI_NOT_NULL_FLAG => 'not_null', |
764
|
2 |
|
MYSQLI_PRI_KEY_FLAG => 'primary_key', |
765
|
2 |
|
MYSQLI_UNIQUE_KEY_FLAG => 'unique_key', |
766
|
2 |
|
MYSQLI_MULTIPLE_KEY_FLAG => 'multiple_key', |
767
|
2 |
|
MYSQLI_BLOB_FLAG => 'blob', |
768
|
2 |
|
MYSQLI_UNSIGNED_FLAG => 'unsigned', |
769
|
2 |
|
MYSQLI_ZEROFILL_FLAG => 'zerofill', |
770
|
2 |
|
MYSQLI_BINARY_FLAG => 'binary', |
771
|
2 |
|
MYSQLI_ENUM_FLAG => 'enum', |
772
|
2 |
|
MYSQLI_SET_FLAG => 'set', |
773
|
2 |
|
MYSQLI_AUTO_INCREMENT_FLAG => 'auto_increment', |
774
|
2 |
|
MYSQLI_TIMESTAMP_FLAG => 'timestamp', |
775
|
|
|
); |
776
|
|
|
|
777
|
2 |
|
$fieldFlags = array(); |
778
|
2 |
|
foreach ($flags as $flag => $value) { |
779
|
2 |
|
if ($what & $flag) { |
780
|
2 |
|
$fieldFlags[] = $value; |
781
|
|
|
} |
782
|
|
|
} |
783
|
|
|
|
784
|
2 |
|
return implode(' ', $fieldFlags); |
785
|
|
|
} |
786
|
|
|
|
787
|
2 |
|
protected static function getFieldType($what) |
788
|
|
|
{ |
789
|
|
|
$types = array( |
790
|
2 |
|
MYSQLI_TYPE_STRING => 'string', |
791
|
2 |
|
MYSQLI_TYPE_VAR_STRING => 'string', |
792
|
2 |
|
MYSQLI_TYPE_ENUM => 'string', |
793
|
2 |
|
MYSQLI_TYPE_SET => 'string', |
794
|
|
|
|
795
|
2 |
|
MYSQLI_TYPE_LONG => 'int', |
796
|
2 |
|
MYSQLI_TYPE_TINY => 'int', |
797
|
2 |
|
MYSQLI_TYPE_SHORT => 'int', |
798
|
2 |
|
MYSQLI_TYPE_INT24 => 'int', |
799
|
2 |
|
MYSQLI_TYPE_CHAR => 'int', |
800
|
2 |
|
MYSQLI_TYPE_LONGLONG => 'int', |
801
|
|
|
|
802
|
2 |
|
MYSQLI_TYPE_DECIMAL => 'real', |
803
|
2 |
|
MYSQLI_TYPE_FLOAT => 'real', |
804
|
2 |
|
MYSQLI_TYPE_DOUBLE => 'real', |
805
|
2 |
|
MYSQLI_TYPE_NEWDECIMAL => 'real', |
806
|
|
|
|
807
|
2 |
|
MYSQLI_TYPE_TINY_BLOB => 'blob', |
808
|
2 |
|
MYSQLI_TYPE_MEDIUM_BLOB => 'blob', |
809
|
2 |
|
MYSQLI_TYPE_LONG_BLOB => 'blob', |
810
|
2 |
|
MYSQLI_TYPE_BLOB => 'blob', |
811
|
|
|
|
812
|
2 |
|
MYSQLI_TYPE_NEWDATE => 'date', |
813
|
2 |
|
MYSQLI_TYPE_DATE => 'date', |
814
|
2 |
|
MYSQLI_TYPE_TIME => 'time', |
815
|
2 |
|
MYSQLI_TYPE_YEAR => 'year', |
816
|
2 |
|
MYSQLI_TYPE_DATETIME => 'datetime', |
817
|
2 |
|
MYSQLI_TYPE_TIMESTAMP => 'timestamp', |
818
|
|
|
|
819
|
2 |
|
MYSQLI_TYPE_NULL => 'null', |
820
|
|
|
|
821
|
2 |
|
MYSQLI_TYPE_GEOMETRY => 'geometry', |
822
|
|
|
); |
823
|
|
|
|
824
|
2 |
|
return isset($types[$what]) ? $types[$what] : 'unknown'; |
825
|
|
|
} |
826
|
|
|
|
827
|
1 |
|
protected static function escapeChar($char) |
828
|
|
|
{ |
829
|
1 |
|
switch ($char) { |
830
|
1 |
|
case "\0": |
831
|
1 |
|
$esc = "\\0"; |
832
|
1 |
|
break; |
833
|
1 |
|
case "\n": |
834
|
1 |
|
$esc = "\\n"; |
835
|
1 |
|
break; |
836
|
1 |
|
case "\r": |
837
|
1 |
|
$esc = "\\r"; |
838
|
1 |
|
break; |
839
|
1 |
|
case '\\': |
840
|
1 |
|
case '\'': |
841
|
1 |
|
case '"': |
842
|
1 |
|
$esc = "\\{$char}"; |
843
|
1 |
|
break; |
844
|
1 |
|
case "\032": |
845
|
1 |
|
$esc = "\\Z"; |
846
|
1 |
|
break; |
847
|
|
|
default: |
848
|
1 |
|
$esc = $char; |
849
|
1 |
|
break; |
850
|
|
|
} |
851
|
|
|
|
852
|
1 |
|
return $esc; |
853
|
|
|
} |
854
|
|
|
} |
855
|
|
|
} |
856
|
|
|
|