| 109 |
rodolico |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* php7-mysql-shim
|
|
|
4 |
*
|
|
|
5 |
* @author Davey Shafik <me@daveyshafik.com>
|
|
|
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 |
if (null === $hostname) {
|
|
|
44 |
$hostname = ini_get('mysqli.default_host') ?: null;
|
|
|
45 |
}
|
|
|
46 |
if (null === $username) {
|
|
|
47 |
$username = ini_get('mysqli.default_user') ?: null;
|
|
|
48 |
}
|
|
|
49 |
if (null === $password) {
|
|
|
50 |
$password = ini_get('mysqli.default_pw') ?: null;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$hash = sha1($hostname . $username . $flags);
|
|
|
54 |
/* persistent connections start with p: */
|
|
|
55 |
/* don't use a cached link for those */
|
|
|
56 |
if (!$new && $hostname[1] !== ':' && isset(\Dshafik\MySQL::$connections[$hash])) {
|
|
|
57 |
\Dshafik\MySQL::$last_connection = \Dshafik\MySQL::$connections[$hash]['conn'];
|
|
|
58 |
\Dshafik\MySQL::$connections[$hash]['refcount'] += 1;
|
|
|
59 |
return \Dshafik\MySQL::$connections[$hash]['conn'];
|
|
|
60 |
}
|
|
|
61 |
|
|
|
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 |
$port = (int) $port_matches[2];
|
|
|
66 |
} else {
|
|
|
67 |
$port = null;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/* No flags, means we can use mysqli_connect() */
|
|
|
71 |
if ($flags === 0) {
|
|
|
72 |
$conn = mysqli_connect($hostname, $username, $password, '', $port);
|
|
|
73 |
if (!$conn instanceof mysqli) {
|
|
|
74 |
return false;
|
|
|
75 |
}
|
|
|
76 |
\Dshafik\MySQL::$last_connection = $conn;
|
|
|
77 |
$conn->hash = $hash;
|
|
|
78 |
\Dshafik\MySQL::$connections[$hash] = array('refcount' => 1, 'conn' => $conn);
|
|
|
79 |
|
|
|
80 |
return $conn;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/* Flags means we need to use mysqli_real_connect() instead, and handle exceptions */
|
|
|
84 |
try {
|
|
|
85 |
\Dshafik\MySQL::$last_connection = $conn = mysqli_init();
|
|
|
86 |
|
|
|
87 |
mysqli_real_connect(
|
|
|
88 |
$conn,
|
|
|
89 |
$hostname,
|
|
|
90 |
$username,
|
|
|
91 |
$password,
|
|
|
92 |
'',
|
|
|
93 |
$port,
|
|
|
94 |
'',
|
|
|
95 |
$flags
|
|
|
96 |
);
|
|
|
97 |
|
|
|
98 |
// @codeCoverageIgnoreStart
|
|
|
99 |
// PHPUnit turns the warning from mysqli_real_connect into an exception, so this never runs
|
|
|
100 |
if ($conn === false) {
|
|
|
101 |
return false;
|
|
|
102 |
}
|
|
|
103 |
// @codeCoverageIgnoreEnd
|
|
|
104 |
|
|
|
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 |
function mysql_pconnect(
|
|
|
119 |
$hostname = null,
|
|
|
120 |
$username = null,
|
|
|
121 |
$password = null,
|
|
|
122 |
$flags = 0
|
|
|
123 |
) {
|
|
|
124 |
$hostname = 'p:' . $hostname;
|
|
|
125 |
return mysql_connect($hostname, $username, $password, false, $flags);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
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 |
// @codeCoverageIgnoreStart
|
|
|
135 |
// PHPUnit Warning -> Exception
|
|
|
136 |
return false;
|
|
|
137 |
// @codeCoverageIgnoreEnd
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
if (isset(\Dshafik\MySQL::$connections[$link->hash])) {
|
|
|
141 |
\Dshafik\MySQL::$connections[$link->hash]['refcount'] -= 1;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
$return = true;
|
|
|
145 |
if (\Dshafik\MySQL::$connections[$link->hash]['refcount'] === 0) {
|
|
|
146 |
$return = mysqli_close($link);
|
|
|
147 |
unset(\Dshafik\MySQL::$connections[$link->hash]);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if ($isDefault) {
|
|
|
151 |
Dshafik\MySQL::$last_connection = null;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
return $return;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
function mysql_select_db($databaseName, \mysqli $link = null)
|
|
|
158 |
{
|
|
|
159 |
$link = \Dshafik\MySQL::getConnection($link);
|
|
|
160 |
|
|
|
161 |
return mysqli_query(
|
|
|
162 |
$link,
|
|
|
163 |
'USE `' . mysqli_real_escape_string($link, $databaseName) . '`'
|
|
|
164 |
) !== false;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
function mysql_query($query, \mysqli $link = null)
|
|
|
168 |
{
|
|
|
169 |
return mysqli_query(\Dshafik\MySQL::getConnection($link), $query);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
function mysql_unbuffered_query($query, \mysqli $link = null)
|
|
|
173 |
{
|
|
|
174 |
$link = \Dshafik\MySQL::getConnection($link);
|
|
|
175 |
if (mysqli_real_query($link, $query)) {
|
|
|
176 |
return mysqli_use_result($link);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
return false;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
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 |
}
|
|
|
187 |
return false;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
function mysql_list_dbs(\mysqli $link = null)
|
|
|
191 |
{
|
|
|
192 |
return mysql_query('SHOW DATABASES', $link);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
function mysql_list_tables($databaseName, \mysqli $link = null)
|
|
|
196 |
{
|
|
|
197 |
$link = \Dshafik\MySQL::getConnection($link);
|
|
|
198 |
$query = sprintf(
|
|
|
199 |
'SHOW TABLES FROM `%s`',
|
|
|
200 |
mysql_real_escape_string($databaseName, $link)
|
|
|
201 |
);
|
|
|
202 |
return mysql_query($query, $link);
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
function mysql_list_fields($databaseName, $tableName, \mysqli $link = null)
|
|
|
206 |
{
|
|
|
207 |
$link = \Dshafik\MySQL::getConnection($link);
|
|
|
208 |
|
|
|
209 |
$query = sprintf(
|
|
|
210 |
'SHOW COLUMNS FROM `%s`.`%s`',
|
|
|
211 |
mysqli_real_escape_string($link, $databaseName),
|
|
|
212 |
mysqli_real_escape_string($link, $tableName)
|
|
|
213 |
);
|
|
|
214 |
|
|
|
215 |
$result = mysql_query($query, $link);
|
|
|
216 |
|
|
|
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 |
{
|
|
|
230 |
return mysql_query('SHOW PROCESSLIST', $link);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
function mysql_error(\mysqli $link = null)
|
|
|
234 |
{
|
|
|
235 |
return mysqli_error(\Dshafik\MySQL::getConnection($link));
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
function mysql_errno(\mysqli $link = null)
|
|
|
239 |
{
|
|
|
240 |
return mysqli_errno(\Dshafik\MySQL::getConnection($link));
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
function mysql_affected_rows(\mysqli $link = null)
|
|
|
244 |
{
|
|
|
245 |
return mysqli_affected_rows(\Dshafik\MySQL::getConnection($link));
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
function mysql_insert_id($link = null) /*|*/
|
|
|
249 |
{
|
|
|
250 |
return mysqli_insert_id(\Dshafik\MySQL::getConnection($link));
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
function mysql_result($result, $row, $field = 0)
|
|
|
254 |
{
|
|
|
255 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
256 |
// @codeCoverageIgnoreStart
|
|
|
257 |
return false;
|
|
|
258 |
// @codeCoverageIgnoreEnd
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
if (!mysqli_data_seek($result, $row)) {
|
|
|
262 |
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 |
);
|
|
|
270 |
// @codeCoverageIgnoreStart
|
|
|
271 |
return false;
|
|
|
272 |
// @codeCoverageIgnoreEnd
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
$found = true;
|
|
|
276 |
if (strpos($field, '.') !== false) {
|
|
|
277 |
list($table, $name) = explode('.', $field);
|
|
|
278 |
$i = 0;
|
|
|
279 |
$found = false;
|
|
|
280 |
mysqli_field_seek($result, 0);
|
|
|
281 |
while ($column = mysqli_fetch_field($result)) {
|
|
|
282 |
if ($column->table === $table && $column->name === $name) {
|
|
|
283 |
$field = $i;
|
|
|
284 |
$found = true;
|
|
|
285 |
break;
|
|
|
286 |
}
|
|
|
287 |
$i++;
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
$row = mysql_fetch_array($result);
|
|
|
292 |
if ($found && array_key_exists($field, $row)) {
|
|
|
293 |
return $row[$field];
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
trigger_error(
|
|
|
297 |
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 |
return false;
|
|
|
307 |
// @codeCoverageIgnoreEnd
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
function mysql_num_rows($result)
|
|
|
311 |
{
|
|
|
312 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
313 |
// @codeCoverageIgnoreStart
|
|
|
314 |
return false;
|
|
|
315 |
// @codeCoverageIgnoreEnd
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
$previous = error_reporting(0);
|
|
|
319 |
$rows = mysqli_num_rows($result);
|
|
|
320 |
error_reporting($previous);
|
|
|
321 |
|
|
|
322 |
return $rows;
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
function mysql_num_fields($result)
|
|
|
326 |
{
|
|
|
327 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
328 |
// @codeCoverageIgnoreStart
|
|
|
329 |
return false;
|
|
|
330 |
// @codeCoverageIgnoreEnd
|
|
|
331 |
}
|
|
|
332 |
return mysqli_num_fields($result);
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
function mysql_fetch_row($result)
|
|
|
336 |
{
|
|
|
337 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
338 |
// @codeCoverageIgnoreStart
|
|
|
339 |
return false;
|
|
|
340 |
// @codeCoverageIgnoreEnd
|
|
|
341 |
}
|
|
|
342 |
return mysqli_fetch_row($result) ?: false;
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
function mysql_fetch_array($result, $resultType = MYSQL_BOTH)
|
|
|
346 |
{
|
|
|
347 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
348 |
// @codeCoverageIgnoreStart
|
|
|
349 |
return false;
|
|
|
350 |
// @codeCoverageIgnoreEnd
|
|
|
351 |
}
|
|
|
352 |
return mysqli_fetch_array($result, $resultType) ?: false;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
function mysql_fetch_assoc($result) /* : array|null */
|
|
|
356 |
{
|
|
|
357 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
358 |
// @codeCoverageIgnoreStart
|
|
|
359 |
return false;
|
|
|
360 |
// @codeCoverageIgnoreEnd
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
return mysqli_fetch_assoc($result) ?: false;
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
function mysql_fetch_object($result, $class = null, array $params = array()) /* : object|null */
|
|
|
367 |
{
|
|
|
368 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
369 |
// @codeCoverageIgnoreStart
|
|
|
370 |
return false;
|
|
|
371 |
// @codeCoverageIgnoreEnd
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
if ($class === null) {
|
|
|
375 |
$object = mysqli_fetch_object($result);
|
|
|
376 |
} else {
|
|
|
377 |
$object = mysqli_fetch_object($result, $class, $params);
|
|
|
378 |
}
|
|
|
379 |
|
|
|
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 |
}
|
|
|
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 |
}
|
|
|
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 |
}
|
|
|
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 |
}
|
|
|
420 |
return mysqli_field_seek($result, $field);
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
function mysql_free_result($result)
|
|
|
424 |
{
|
|
|
425 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
426 |
// @codeCoverageIgnoreStart
|
|
|
427 |
return false;
|
|
|
428 |
// @codeCoverageIgnoreEnd
|
|
|
429 |
}
|
|
|
430 |
return mysqli_free_result($result);
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
function mysql_field_name($result, $field)
|
|
|
434 |
{
|
|
|
435 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
436 |
// @codeCoverageIgnoreStart
|
|
|
437 |
return false;
|
|
|
438 |
// @codeCoverageIgnoreEnd
|
|
|
439 |
}
|
|
|
440 |
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'name');
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
function mysql_field_table($result, $field)
|
|
|
444 |
{
|
|
|
445 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
446 |
// @codeCoverageIgnoreStart
|
|
|
447 |
return false;
|
|
|
448 |
// @codeCoverageIgnoreEnd
|
|
|
449 |
}
|
|
|
450 |
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'table');
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
function mysql_field_len($result, $field)
|
|
|
454 |
{
|
|
|
455 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
456 |
// @codeCoverageIgnoreStart
|
|
|
457 |
return false;
|
|
|
458 |
// @codeCoverageIgnoreEnd
|
|
|
459 |
}
|
|
|
460 |
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'length');
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
function mysql_field_type($result, $field)
|
|
|
464 |
{
|
|
|
465 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
466 |
// @codeCoverageIgnoreStart
|
|
|
467 |
return false;
|
|
|
468 |
// @codeCoverageIgnoreEnd
|
|
|
469 |
}
|
|
|
470 |
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'type');
|
|
|
471 |
}
|
|
|
472 |
|
|
|
473 |
function mysql_field_flags($result, $field)
|
|
|
474 |
{
|
|
|
475 |
if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
|
|
|
476 |
// @codeCoverageIgnoreStart
|
|
|
477 |
return false;
|
|
|
478 |
// @codeCoverageIgnoreEnd
|
|
|
479 |
}
|
|
|
480 |
return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'flags');
|
|
|
481 |
}
|
|
|
482 |
|
|
|
483 |
function mysql_escape_string($unescapedString)
|
|
|
484 |
{
|
|
|
485 |
if (\Dshafik\MySQL::$last_connection === null) {
|
|
|
486 |
trigger_error(
|
|
|
487 |
sprintf(
|
|
|
488 |
'%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 |
}
|
|
|
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 |
{
|
|
|
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 |
// @codeCoverageIgnoreStart
|
|
|
558 |
return false;
|
|
|
559 |
// @codeCoverageIgnoreEnd
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
// 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 |
|
|
|
654 |
class MySQL
|
|
|
655 |
{
|
|
|
656 |
public static $last_connection = null;
|
|
|
657 |
public static $connections = array();
|
|
|
658 |
|
|
|
659 |
public static function getConnection($link = null, $func = null)
|
|
|
660 |
{
|
|
|
661 |
if ($link !== null) {
|
|
|
662 |
return $link;
|
|
|
663 |
}
|
|
|
664 |
|
|
|
665 |
if (static::$last_connection === null) {
|
|
|
666 |
$err = 'A link to the server could not be established';
|
|
|
667 |
if ($func !== null) {
|
|
|
668 |
$err = $func . '(): no MySQL-Link resource supplied';
|
|
|
669 |
}
|
|
|
670 |
trigger_error($err, E_USER_WARNING);
|
|
|
671 |
return false;
|
|
|
672 |
}
|
|
|
673 |
|
|
|
674 |
return static::$last_connection;
|
|
|
675 |
}
|
|
|
676 |
|
|
|
677 |
public static function mysqlFieldInfo(\mysqli_result $result, $field, $what)
|
|
|
678 |
{
|
|
|
679 |
try {
|
|
|
680 |
$field = mysqli_fetch_field_direct($result, $field);
|
|
|
681 |
} catch (\Exception $e) {
|
|
|
682 |
trigger_error(
|
|
|
683 |
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 |
// @codeCoverageIgnoreStart
|
|
|
692 |
// PHPUnit turns the warning into an exception, so this never runs
|
|
|
693 |
return false;
|
|
|
694 |
// @codeCoverageIgnoreEnd
|
|
|
695 |
}
|
|
|
696 |
|
|
|
697 |
if ($what === 'type') {
|
|
|
698 |
return static::getFieldType($field->type);
|
|
|
699 |
}
|
|
|
700 |
|
|
|
701 |
if ($what === 'flags') {
|
|
|
702 |
return static::getFieldFlags($field->flags);
|
|
|
703 |
}
|
|
|
704 |
|
|
|
705 |
if (isset($field->{$what})) {
|
|
|
706 |
return $field->{$what};
|
|
|
707 |
}
|
|
|
708 |
|
|
|
709 |
return false;
|
|
|
710 |
}
|
|
|
711 |
|
|
|
712 |
public static function checkValidResult($result, $function)
|
|
|
713 |
{
|
|
|
714 |
if (!($result instanceof \mysqli_result)) {
|
|
|
715 |
if ($function !== 'mysql_fetch_object') {
|
|
|
716 |
trigger_error(
|
|
|
717 |
$function . '() expects parameter 1 to be resource, ' . strtolower(gettype($result)) . ' given',
|
|
|
718 |
E_USER_WARNING
|
|
|
719 |
);
|
|
|
720 |
}
|
|
|
721 |
|
|
|
722 |
if ($function === 'mysql_fetch_object') {
|
|
|
723 |
trigger_error(
|
|
|
724 |
$function . '(): supplied argument is not a valid MySQL result resource',
|
|
|
725 |
E_USER_WARNING
|
|
|
726 |
);
|
|
|
727 |
}
|
|
|
728 |
return false;
|
|
|
729 |
}
|
|
|
730 |
|
|
|
731 |
return true;
|
|
|
732 |
}
|
|
|
733 |
|
|
|
734 |
public static function escapeString($unescapedString)
|
|
|
735 |
{
|
|
|
736 |
$escapedString = '';
|
|
|
737 |
for ($i = 0, $max = strlen($unescapedString); $i < $max; $i++) {
|
|
|
738 |
$escapedString .= self::escapeChar($unescapedString[$i]);
|
|
|
739 |
}
|
|
|
740 |
|
|
|
741 |
return $escapedString;
|
|
|
742 |
}
|
|
|
743 |
|
|
|
744 |
protected static function getFieldFlags($what)
|
|
|
745 |
{
|
|
|
746 |
// Order of flags taken from http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#2507
|
|
|
747 |
$flags = array(
|
|
|
748 |
MYSQLI_NOT_NULL_FLAG => 'not_null',
|
|
|
749 |
MYSQLI_PRI_KEY_FLAG => 'primary_key',
|
|
|
750 |
MYSQLI_UNIQUE_KEY_FLAG => 'unique_key',
|
|
|
751 |
MYSQLI_MULTIPLE_KEY_FLAG => 'multiple_key',
|
|
|
752 |
MYSQLI_BLOB_FLAG => 'blob',
|
|
|
753 |
MYSQLI_UNSIGNED_FLAG => 'unsigned',
|
|
|
754 |
MYSQLI_ZEROFILL_FLAG => 'zerofill',
|
|
|
755 |
MYSQLI_BINARY_FLAG => 'binary',
|
|
|
756 |
MYSQLI_ENUM_FLAG => 'enum',
|
|
|
757 |
MYSQLI_SET_FLAG => 'set',
|
|
|
758 |
MYSQLI_AUTO_INCREMENT_FLAG => 'auto_increment',
|
|
|
759 |
MYSQLI_TIMESTAMP_FLAG => 'timestamp',
|
|
|
760 |
);
|
|
|
761 |
|
|
|
762 |
$fieldFlags = array();
|
|
|
763 |
foreach ($flags as $flag => $value) {
|
|
|
764 |
if ($what & $flag) {
|
|
|
765 |
$fieldFlags[] = $value;
|
|
|
766 |
}
|
|
|
767 |
}
|
|
|
768 |
|
|
|
769 |
return implode(' ', $fieldFlags);
|
|
|
770 |
}
|
|
|
771 |
|
|
|
772 |
protected static function getFieldType($what)
|
|
|
773 |
{
|
|
|
774 |
$types = array(
|
|
|
775 |
MYSQLI_TYPE_STRING => 'string',
|
|
|
776 |
MYSQLI_TYPE_VAR_STRING => 'string',
|
|
|
777 |
MYSQLI_TYPE_ENUM => 'string',
|
|
|
778 |
MYSQLI_TYPE_SET => 'string',
|
|
|
779 |
|
|
|
780 |
MYSQLI_TYPE_LONG => 'int',
|
|
|
781 |
MYSQLI_TYPE_TINY => 'int',
|
|
|
782 |
MYSQLI_TYPE_SHORT => 'int',
|
|
|
783 |
MYSQLI_TYPE_INT24 => 'int',
|
|
|
784 |
MYSQLI_TYPE_CHAR => 'int',
|
|
|
785 |
MYSQLI_TYPE_LONGLONG => 'int',
|
|
|
786 |
|
|
|
787 |
MYSQLI_TYPE_DECIMAL => 'real',
|
|
|
788 |
MYSQLI_TYPE_FLOAT => 'real',
|
|
|
789 |
MYSQLI_TYPE_DOUBLE => 'real',
|
|
|
790 |
MYSQLI_TYPE_NEWDECIMAL => 'real',
|
|
|
791 |
|
|
|
792 |
MYSQLI_TYPE_TINY_BLOB => 'blob',
|
|
|
793 |
MYSQLI_TYPE_MEDIUM_BLOB => 'blob',
|
|
|
794 |
MYSQLI_TYPE_LONG_BLOB => 'blob',
|
|
|
795 |
MYSQLI_TYPE_BLOB => 'blob',
|
|
|
796 |
|
|
|
797 |
MYSQLI_TYPE_NEWDATE => 'date',
|
|
|
798 |
MYSQLI_TYPE_DATE => 'date',
|
|
|
799 |
MYSQLI_TYPE_TIME => 'time',
|
|
|
800 |
MYSQLI_TYPE_YEAR => 'year',
|
|
|
801 |
MYSQLI_TYPE_DATETIME => 'datetime',
|
|
|
802 |
MYSQLI_TYPE_TIMESTAMP => 'timestamp',
|
|
|
803 |
|
|
|
804 |
MYSQLI_TYPE_NULL => 'null',
|
|
|
805 |
|
|
|
806 |
MYSQLI_TYPE_GEOMETRY => 'geometry',
|
|
|
807 |
);
|
|
|
808 |
|
|
|
809 |
return isset($types[$what]) ? $types[$what] : 'unknown';
|
|
|
810 |
}
|
|
|
811 |
|
|
|
812 |
protected static function escapeChar($char)
|
|
|
813 |
{
|
|
|
814 |
switch ($char) {
|
|
|
815 |
case "\0":
|
|
|
816 |
$esc = "\\0";
|
|
|
817 |
break;
|
|
|
818 |
case "\n":
|
|
|
819 |
$esc = "\\n";
|
|
|
820 |
break;
|
|
|
821 |
case "\r":
|
|
|
822 |
$esc = "\\r";
|
|
|
823 |
break;
|
|
|
824 |
case '\\':
|
|
|
825 |
case '\'':
|
|
|
826 |
case '"':
|
|
|
827 |
$esc = "\\{$char}";
|
|
|
828 |
break;
|
|
|
829 |
case "\032":
|
|
|
830 |
$esc = "\\Z";
|
|
|
831 |
break;
|
|
|
832 |
default:
|
|
|
833 |
$esc = $char;
|
|
|
834 |
break;
|
|
|
835 |
}
|
|
|
836 |
|
|
|
837 |
return $esc;
|
|
|
838 |
}
|
|
|
839 |
}
|
|
|
840 |
}
|