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