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