Passed
Push — master ( e95921...ddbd1b )
by Dimas
13:28 queued 05:15
created

MySQL::getFieldFlags()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 26
rs 9.6666
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;
0 ignored issues
show
Bug introduced by
The property hash does not seem to exist on mysqli.
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_connect() has been deprecated: 5.5 Open a connection to a MySQL Server ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

127
      return /** @scrutinizer ignore-deprecated */ mysql_connect($hostname, $username, $password, false, $flags);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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])) {
0 ignored issues
show
Bug introduced by
The property hash does not seem to exist on mysqli.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $link can also be of type false; however, parameter $link of mysqli_close() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

148
        $return = mysqli_close(/** @scrutinizer ignore-type */ $link);
Loading history...
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,
0 ignored issues
show
Bug introduced by
It seems like $link can also be of type false; however, parameter $link of mysqli_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
        /** @scrutinizer ignore-type */ $link,
Loading history...
165
        'USE `' . mysqli_real_escape_string($link, $databaseName) . '`'
0 ignored issues
show
Bug introduced by
It seems like $link can also be of type false; however, parameter $link of mysqli_real_escape_string() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
        'USE `' . mysqli_real_escape_string(/** @scrutinizer ignore-type */ $link, $databaseName) . '`'
Loading history...
166
      ) !== false;
167
    }
168
169
    function mysql_query($query, \mysqli $link = null)
170
    {
171
      return mysqli_query(\UniversalFramework\MySQL::getConnection($link), $query);
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

171
      return mysqli_query(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link), $query);
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
It seems like $link can also be of type false; however, parameter $link of mysqli_real_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

177
      if (mysqli_real_query(/** @scrutinizer ignore-type */ $link, $query)) {
Loading history...
178
        return mysqli_use_result($link);
0 ignored issues
show
Bug introduced by
It seems like $link can also be of type false; however, parameter $link of mysqli_use_result() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
        return mysqli_use_result(/** @scrutinizer ignore-type */ $link);
Loading history...
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)) {
0 ignored issues
show
Deprecated Code introduced by
The function mysql_select_db() has been deprecated: 5.5 Select a MySQL database ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

186
      if (/** @scrutinizer ignore-deprecated */ mysql_select_db($databaseName, $link)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
It seems like $link can also be of type mysqli; however, parameter $link_identifier of mysql_select_db() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
      if (mysql_select_db($databaseName, /** @scrutinizer ignore-type */ $link)) {
Loading history...
187
        return mysql_query($query, $link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_query() has been deprecated: 5.5 Send a MySQL query ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

187
        return /** @scrutinizer ignore-deprecated */ mysql_query($query, $link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
It seems like $link can also be of type mysqli; however, parameter $link_identifier of mysql_query() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

187
        return mysql_query($query, /** @scrutinizer ignore-type */ $link);
Loading history...
188
      }
189
      return false;
190
    }
191
192
    function mysql_list_dbs(\mysqli $link = null)
193
    {
194
      return mysql_query('SHOW DATABASES', $link);
0 ignored issues
show
Bug introduced by
It seems like $link can also be of type mysqli; however, parameter $link_identifier of mysql_query() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
      return mysql_query('SHOW DATABASES', /** @scrutinizer ignore-type */ $link);
Loading history...
Deprecated Code introduced by
The function mysql_query() has been deprecated: 5.5 Send a MySQL query ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

194
      return /** @scrutinizer ignore-deprecated */ mysql_query('SHOW DATABASES', $link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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)
0 ignored issues
show
Deprecated Code introduced by
The function mysql_real_escape_string() has been deprecated: 5.5 Escapes special characters in a string for use in an SQL statement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

202
        /** @scrutinizer ignore-deprecated */ mysql_real_escape_string($databaseName, $link)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
203
      );
204
      return mysql_query($query, $link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_query() has been deprecated: 5.5 Send a MySQL query ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

204
      return /** @scrutinizer ignore-deprecated */ mysql_query($query, $link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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),
0 ignored issues
show
Bug introduced by
It seems like $link can also be of type false; however, parameter $link of mysqli_real_escape_string() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
        mysqli_real_escape_string(/** @scrutinizer ignore-type */ $link, $databaseName),
Loading history...
214
        mysqli_real_escape_string($link, $tableName)
215
      );
216
217
      $result = mysql_query($query, $link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_query() has been deprecated: 5.5 Send a MySQL query ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

217
      $result = /** @scrutinizer ignore-deprecated */ mysql_query($query, $link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
218
219
      if ($result instanceof \mysqli_result) {
0 ignored issues
show
introduced by
$result is never a sub-type of mysqli_result.
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_query() has been deprecated: 5.5 Send a MySQL query ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

232
      return /** @scrutinizer ignore-deprecated */ mysql_query('SHOW PROCESSLIST', $link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
It seems like $link can also be of type mysqli; however, parameter $link_identifier of mysql_query() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

232
      return mysql_query('SHOW PROCESSLIST', /** @scrutinizer ignore-type */ $link);
Loading history...
233
    }
234
235
    function mysql_error(\mysqli $link = null)
236
    {
237
      return mysqli_error(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_error() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
      return mysqli_error(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
238
    }
239
240
    function mysql_errno(\mysqli $link = null)
241
    {
242
      return mysqli_errno(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_errno() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

242
      return mysqli_errno(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
243
    }
244
245
    function mysql_affected_rows(\mysqli $link = null)
246
    {
247
      return mysqli_affected_rows(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_affected_rows() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

247
      return mysqli_affected_rows(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
248
    }
249
250
    function mysql_insert_id($link = null) /*|*/
251
    {
252
      return mysqli_insert_id(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_insert_id() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
      return mysqli_insert_id(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_fetch_array() has been deprecated: 5.5 Fetch a result row as an associative array, a numeric array, or both ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

293
      $row = /** @scrutinizer ignore-deprecated */ mysql_fetch_array($result);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
350
     * @param [type] $resultType
351
     * @return array|null
352
     */
353
    function mysql_fetch_array($result, $resultType = MYSQL_BOTH)
0 ignored issues
show
introduced by
The constant MYSQL_BOTH has been deprecated: 5.5 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

353
    function mysql_fetch_array($result, $resultType = /** @scrutinizer ignore-deprecated */ MYSQL_BOTH)
Loading history...
354
    {
355
      if (!\UniversalFramework\MySQL::checkValidResult($result, __FUNCTION__)) {
356
        // @codeCoverageIgnoreStart
357
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array|null.
Loading history...
358
        // @codeCoverageIgnoreEnd
359
      }
360
      return mysqli_fetch_array($result, $resultType) ?: false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return mysqli_fetch_arra..., $resultType) ?: false could also return false which is incompatible with the documented return type array|null. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
361
    }
362
363
    /**
364
     * Fetch a result row as assoc array
365
     * @inheritDoc mysqli_fetch_assoc
366
     * @param [type] $result
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array|null.
Loading history...
374
        // @codeCoverageIgnoreEnd
375
      }
376
377
      return mysqli_fetch_assoc($result) ?: false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return mysqli_fetch_assoc($result) ?: false could also return false which is incompatible with the documented return type array|null. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the usage of mysqli_free_result($result) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_real_escape_string() has been deprecated: 5.5 Escapes special characters in a string for use in an SQL statement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

510
      return /** @scrutinizer ignore-deprecated */ mysql_real_escape_string($unescapedString, null);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
511
    }
512
513
    function mysql_real_escape_string($unescapedString, \mysqli $link = null)
514
    {
515
      return mysqli_escape_string(\UniversalFramework\MySQL::getConnection($link), $unescapedString);
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_escape_string() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

515
      return mysqli_escape_string(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link), $unescapedString);
Loading history...
516
    }
517
518
    function mysql_stat(\mysqli $link = null)
519
    {
520
      return mysqli_stat(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_stat() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

520
      return mysqli_stat(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
521
    }
522
523
    function mysql_thread_id(\mysqli $link = null)
524
    {
525
      return mysqli_thread_id(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_thread_id() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

525
      return mysqli_thread_id(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
526
    }
527
528
    function mysql_client_encoding(\mysqli $link = null)
529
    {
530
      return mysqli_character_set_name(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_character_set_name() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

530
      return mysqli_character_set_name(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
531
    }
532
533
    function mysql_ping(\mysqli $link = null)
534
    {
535
      return mysqli_ping(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_ping() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

535
      return mysqli_ping(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
536
    }
537
538
    function mysql_get_client_info(\mysqli $link = null)
539
    {
540
      return mysqli_get_client_info(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Unused Code introduced by
The call to mysqli_get_client_info() has too many arguments starting with UniversalFramework\MySQL::getConnection($link). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

540
      return /** @scrutinizer ignore-call */ mysqli_get_client_info(\UniversalFramework\MySQL::getConnection($link));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
541
    }
542
543
    function mysql_get_host_info(\mysqli $link = null)
544
    {
545
      return mysqli_get_host_info(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_get_host_info() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

545
      return mysqli_get_host_info(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
546
    }
547
548
    function mysql_get_proto_info(\mysqli $link = null)
549
    {
550
      return mysqli_get_proto_info(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_get_proto_info() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

550
      return mysqli_get_proto_info(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
551
    }
552
553
    function mysql_get_server_info(\mysqli $link = null)
554
    {
555
      return mysqli_get_server_info(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_get_server_info() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

555
      return mysqli_get_server_info(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
556
    }
557
558
    function mysql_info(\mysqli $link = null)
559
    {
560
      return mysqli_info(\UniversalFramework\MySQL::getConnection($link));
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_info() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

560
      return mysqli_info(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link));
Loading history...
561
    }
562
563
    function mysql_set_charset($charset, \mysqli $link = null)
564
    {
565
      return mysqli_set_charset(\UniversalFramework\MySQL::getConnection($link), $charset);
0 ignored issues
show
Bug introduced by
It seems like UniversalFramework\MySQL::getConnection($link) can also be of type false; however, parameter $link of mysqli_set_charset() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

565
      return mysqli_set_charset(/** @scrutinizer ignore-type */ \UniversalFramework\MySQL::getConnection($link), $charset);
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_result() has been deprecated: 5.5 Get result data ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

577
      return /** @scrutinizer ignore-deprecated */ mysql_result($result, $row, $field);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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');
0 ignored issues
show
Deprecated Code introduced by
The function mysql_result() has been deprecated: 5.5 Get result data ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

589
      return /** @scrutinizer ignore-deprecated */ mysql_result($result, $row, 'Table');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
590
    }
591
592
    /* Aliases */
593
594
    function mysql_fieldname($result, $field)
595
    {
596
      return mysql_field_name($result, $field);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_field_name() has been deprecated: 5.5 Get the name of the specified field in a result ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

596
      return /** @scrutinizer ignore-deprecated */ mysql_field_name($result, $field);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
597
    }
598
599
    function mysql_fieldtable($result, $field)
600
    {
601
      return mysql_field_table($result, $field);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_field_table() has been deprecated: 5.5 Get name of the table the specified field is in ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

601
      return /** @scrutinizer ignore-deprecated */ mysql_field_table($result, $field);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
602
    }
603
604
    function mysql_fieldlen($result, $field)
605
    {
606
      return mysql_field_len($result, $field);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_field_len() has been deprecated: 5.5 Returns the length of the specified field ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

606
      return /** @scrutinizer ignore-deprecated */ mysql_field_len($result, $field);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
607
    }
608
609
    function mysql_fieldtype($result, $field)
610
    {
611
      return mysql_field_type($result, $field);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_field_type() has been deprecated: 5.5 Get the type of the specified field in a result ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

611
      return /** @scrutinizer ignore-deprecated */ mysql_field_type($result, $field);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
612
    }
613
614
    function mysql_fieldflags($result, $field)
615
    {
616
      return mysql_field_flags($result, $field);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_field_flags() has been deprecated: 5.5 Get the flags associated with the specified field in a result ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

616
      return /** @scrutinizer ignore-deprecated */ mysql_field_flags($result, $field);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
617
    }
618
619
    function mysql_selectdb($databaseName, $link = null)
620
    {
621
      return mysql_select_db($databaseName, $link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_select_db() has been deprecated: 5.5 Select a MySQL database ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

621
      return /** @scrutinizer ignore-deprecated */ mysql_select_db($databaseName, $link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
622
    }
623
624
    function mysql_freeresult($result)
625
    {
626
      return mysql_free_result($result);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_free_result() has been deprecated: 5.5 Free result memory ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

626
      return /** @scrutinizer ignore-deprecated */ mysql_free_result($result);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
627
    }
628
629
    function mysql_numfields($result)
630
    {
631
      return mysql_num_fields($result);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_num_fields() has been deprecated: 5.5 Get number of fields in result ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

631
      return /** @scrutinizer ignore-deprecated */ mysql_num_fields($result);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
632
    }
633
634
    function mysql_numrows($result)
635
    {
636
      return mysql_num_rows($result);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_num_rows() has been deprecated: 5.5 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

636
      return /** @scrutinizer ignore-deprecated */ mysql_num_rows($result);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
637
    }
638
639
    function mysql_listdbs($link)
640
    {
641
      return mysql_list_dbs($link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_list_dbs() has been deprecated: 5.4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

641
      return /** @scrutinizer ignore-deprecated */ mysql_list_dbs($link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
642
    }
643
644
    function mysql_listtables($databaseName, $link = null)
645
    {
646
      return mysql_list_tables($databaseName, $link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_list_tables() has been deprecated: 5.2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

646
      return /** @scrutinizer ignore-deprecated */ mysql_list_tables($databaseName, $link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
647
    }
648
649
    function mysql_listfields($databaseName, $tableName, $link = null)
650
    {
651
      return mysql_list_fields($databaseName, $tableName, $link);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_list_fields() has been deprecated: 5.5 List MySQL table fields ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

651
      return /** @scrutinizer ignore-deprecated */ mysql_list_fields($databaseName, $tableName, $link);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
652
    }
653
654
    function mysql_dbname($result, $row, $field = 0)
655
    {
656
      return mysql_db_name($result, $row, $field);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_db_name() has been deprecated: 5.5 Retrieves database name from the call to <b>mysql_list_dbs</b> ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

656
      return /** @scrutinizer ignore-deprecated */ mysql_db_name($result, $row, $field);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
657
    }
658
659
    function mysql_table_name($result, $row)
660
    {
661
      return mysql_tablename($result, $row);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_tablename() has been deprecated: 5.5 Get table name of field ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

661
      return /** @scrutinizer ignore-deprecated */ mysql_tablename($result, $row);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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,
0 ignored issues
show
Bug introduced by
$field of type false|object is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

700
            /** @scrutinizer ignore-type */ $field,
Loading history...
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