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