[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/phpbb/db/driver/ -> mysqli.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\db\driver;
  15  
  16  /**
  17  * MySQLi Database Abstraction Layer
  18  * mysqli-extension has to be compiled with:
  19  * MySQL 4.1+ or MySQL 5.0+
  20  */
  21  class mysqli extends \phpbb\db\driver\mysql_base
  22  {
  23      var $multi_insert = true;
  24      var $connect_error = '';
  25  
  26      /**
  27      * {@inheritDoc}
  28      */
  29  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  30      {
  31          if (!function_exists('mysqli_connect'))
  32          {
  33              $this->connect_error = 'mysqli_connect function does not exist, is mysqli extension installed?';
  34              return $this->sql_error('');
  35          }
  36  
  37          $this->persistency = $persistency;
  38          $this->user = $sqluser;
  39  
  40          // If persistent connection, set dbhost to localhost when empty and prepend it with 'p:' prefix
  41          $this->server = ($this->persistency) ? 'p:' . (($sqlserver) ? $sqlserver : 'localhost') : $sqlserver;
  42  
  43          $this->dbname = $database;
  44          $port = (!$port) ? null : $port;
  45  
  46          // If port is set and it is not numeric, most likely mysqli socket is set.
  47          // Try to map it to the $socket parameter.
  48          $socket = null;
  49          if ($port)
  50          {
  51              if (is_numeric($port))
  52              {
  53                  $port = (int) $port;
  54              }
  55              else
  56              {
  57                  $socket = $port;
  58                  $port = null;
  59              }
  60          }
  61  
  62          $this->db_connect_id = mysqli_init();
  63  
  64          if (!@mysqli_real_connect($this->db_connect_id, $this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket, MYSQLI_CLIENT_FOUND_ROWS))
  65          {
  66              $this->db_connect_id = '';
  67          }
  68  
  69          if ($this->db_connect_id && $this->dbname != '')
  70          {
  71              // Disable loading local files on client side
  72              @mysqli_options($this->db_connect_id, MYSQLI_OPT_LOCAL_INFILE, false);
  73  
  74              /*
  75               * As of PHP 8.1 MySQLi default error mode is set to MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT
  76               * See https://wiki.php.net/rfc/mysqli_default_errmode
  77               * Since phpBB implements own SQL errors handling, explicitly set it back to MYSQLI_REPORT_OFF
  78               */
  79              mysqli_report(MYSQLI_REPORT_OFF);
  80  
  81              @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
  82  
  83              // enforce strict mode on databases that support it
  84              if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
  85              {
  86                  $result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
  87                  if ($result)
  88                  {
  89                      $row = mysqli_fetch_assoc($result);
  90                      mysqli_free_result($result);
  91  
  92                      $modes = array_map('trim', explode(',', $row['sql_mode']));
  93                  }
  94                  else
  95                  {
  96                      $modes = array();
  97                  }
  98  
  99                  // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
 100                  if (!in_array('TRADITIONAL', $modes))
 101                  {
 102                      if (!in_array('STRICT_ALL_TABLES', $modes))
 103                      {
 104                          $modes[] = 'STRICT_ALL_TABLES';
 105                      }
 106  
 107                      if (!in_array('STRICT_TRANS_TABLES', $modes))
 108                      {
 109                          $modes[] = 'STRICT_TRANS_TABLES';
 110                      }
 111                  }
 112  
 113                  $mode = implode(',', $modes);
 114                  @mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
 115              }
 116              return $this->db_connect_id;
 117          }
 118  
 119          return $this->sql_error('');
 120      }
 121  
 122      /**
 123      * {@inheritDoc}
 124      */
 125  	function sql_server_info($raw = false, $use_cache = true)
 126      {
 127          global $cache;
 128  
 129          if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false)
 130          {
 131              $result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
 132              if ($result)
 133              {
 134                  $row = mysqli_fetch_assoc($result);
 135                  mysqli_free_result($result);
 136  
 137                  $this->sql_server_version = $row['version'];
 138  
 139                  if (!empty($cache) && $use_cache)
 140                  {
 141                      $cache->put('mysqli_version', $this->sql_server_version);
 142                  }
 143              }
 144          }
 145  
 146          return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
 147      }
 148  
 149      /**
 150      * SQL Transaction
 151      * @access private
 152      */
 153  	function _sql_transaction($status = 'begin')
 154      {
 155          switch ($status)
 156          {
 157              case 'begin':
 158                  return @mysqli_autocommit($this->db_connect_id, false);
 159              break;
 160  
 161              case 'commit':
 162                  $result = @mysqli_commit($this->db_connect_id);
 163                  @mysqli_autocommit($this->db_connect_id, true);
 164                  return $result;
 165              break;
 166  
 167              case 'rollback':
 168                  $result = @mysqli_rollback($this->db_connect_id);
 169                  @mysqli_autocommit($this->db_connect_id, true);
 170                  return $result;
 171              break;
 172          }
 173  
 174          return true;
 175      }
 176  
 177      /**
 178      * {@inheritDoc}
 179      */
 180  	function sql_query($query = '', $cache_ttl = 0)
 181      {
 182          if ($query != '')
 183          {
 184              global $cache;
 185  
 186              if ($this->debug_sql_explain)
 187              {
 188                  $this->sql_report('start', $query);
 189              }
 190              else if ($this->debug_load_time)
 191              {
 192                  $this->curtime = microtime(true);
 193              }
 194  
 195              $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
 196              $this->sql_add_num_queries($this->query_result);
 197  
 198              if ($this->query_result === false)
 199              {
 200                  if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
 201                  {
 202                      $this->sql_error($query);
 203                  }
 204  
 205                  if ($this->debug_sql_explain)
 206                  {
 207                      $this->sql_report('stop', $query);
 208                  }
 209                  else if ($this->debug_load_time)
 210                  {
 211                      $this->sql_time += microtime(true) - $this->curtime;
 212                  }
 213  
 214                  if (!$this->query_result)
 215                  {
 216                      return false;
 217                  }
 218  
 219                  if ($cache && $cache_ttl)
 220                  {
 221                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 222                  }
 223              }
 224              else if ($this->debug_sql_explain)
 225              {
 226                  $this->sql_report('fromcache', $query);
 227              }
 228          }
 229          else
 230          {
 231              return false;
 232          }
 233  
 234          return $this->query_result;
 235      }
 236  
 237      /**
 238      * {@inheritDoc}
 239      */
 240  	function sql_affectedrows()
 241      {
 242          return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
 243      }
 244  
 245      /**
 246      * {@inheritDoc}
 247      */
 248  	function sql_fetchrow($query_id = false)
 249      {
 250          global $cache;
 251  
 252          if ($query_id === false)
 253          {
 254              $query_id = $this->query_result;
 255          }
 256  
 257          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 258          {
 259              return $cache->sql_fetchrow($query_id);
 260          }
 261  
 262          if ($query_id)
 263          {
 264              $result = mysqli_fetch_assoc($query_id);
 265              return $result !== null ? $result : false;
 266          }
 267  
 268          return false;
 269      }
 270  
 271      /**
 272      * {@inheritDoc}
 273      */
 274  	function sql_rowseek($rownum, &$query_id)
 275      {
 276          global $cache;
 277  
 278          if ($query_id === false)
 279          {
 280              $query_id = $this->query_result;
 281          }
 282  
 283          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 284          {
 285              return $cache->sql_rowseek($rownum, $query_id);
 286          }
 287  
 288          return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false;
 289      }
 290  
 291      /**
 292      * {@inheritDoc}
 293      */
 294  	function sql_nextid()
 295      {
 296          return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
 297      }
 298  
 299      /**
 300      * {@inheritDoc}
 301      */
 302  	function sql_freeresult($query_id = false)
 303      {
 304          global $cache;
 305  
 306          if ($query_id === false)
 307          {
 308              $query_id = $this->query_result;
 309          }
 310  
 311          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 312          {
 313              return $cache->sql_freeresult($query_id);
 314          }
 315  
 316          if (!$query_id)
 317          {
 318              return false;
 319          }
 320  
 321          if ($query_id === true)
 322          {
 323              return true;
 324          }
 325  
 326          return mysqli_free_result($query_id);
 327      }
 328  
 329      /**
 330      * {@inheritDoc}
 331      */
 332  	function sql_escape($msg)
 333      {
 334          return @mysqli_real_escape_string($this->db_connect_id, $msg);
 335      }
 336  
 337      /**
 338      * return sql error array
 339      * @access private
 340      */
 341  	function _sql_error()
 342      {
 343          if ($this->db_connect_id)
 344          {
 345              $error = array(
 346                  'message'    => @mysqli_error($this->db_connect_id),
 347                  'code'        => @mysqli_errno($this->db_connect_id)
 348              );
 349          }
 350          else if (function_exists('mysqli_connect_error'))
 351          {
 352              $error = array(
 353                  'message'    => @mysqli_connect_error(),
 354                  'code'        => @mysqli_connect_errno(),
 355              );
 356          }
 357          else
 358          {
 359              $error = array(
 360                  'message'    => $this->connect_error,
 361                  'code'        => '',
 362              );
 363          }
 364  
 365          return $error;
 366      }
 367  
 368      /**
 369      * Close sql connection
 370      * @access private
 371      */
 372  	function _sql_close()
 373      {
 374          return @mysqli_close($this->db_connect_id);
 375      }
 376  
 377      /**
 378      * Build db-specific report
 379      * @access private
 380      */
 381  	function _sql_report($mode, $query = '')
 382      {
 383          static $test_prof;
 384  
 385          // current detection method, might just switch to see the existence of INFORMATION_SCHEMA.PROFILING
 386          if ($test_prof === null)
 387          {
 388              $test_prof = false;
 389              if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
 390              {
 391                  $ver = mysqli_get_server_version($this->db_connect_id);
 392                  if ($ver >= 50037 && $ver < 50100)
 393                  {
 394                      $test_prof = true;
 395                  }
 396              }
 397          }
 398  
 399          switch ($mode)
 400          {
 401              case 'start':
 402  
 403                  $explain_query = $query;
 404                  if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 405                  {
 406                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 407                  }
 408                  else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 409                  {
 410                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 411                  }
 412  
 413                  if (preg_match('/^SELECT/', $explain_query))
 414                  {
 415                      $html_table = false;
 416  
 417                      // begin profiling
 418                      if ($test_prof)
 419                      {
 420                          @mysqli_query($this->db_connect_id, 'SET profiling = 1;');
 421                      }
 422  
 423                      if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
 424                      {
 425                          while ($row = mysqli_fetch_assoc($result))
 426                          {
 427                              $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 428                          }
 429                          mysqli_free_result($result);
 430                      }
 431  
 432                      if ($html_table)
 433                      {
 434                          $this->html_hold .= '</table>';
 435                      }
 436  
 437                      if ($test_prof)
 438                      {
 439                          $html_table = false;
 440  
 441                          // get the last profile
 442                          if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
 443                          {
 444                              $this->html_hold .= '<br />';
 445                              while ($row = mysqli_fetch_assoc($result))
 446                              {
 447                                  // make <unknown> HTML safe
 448                                  if (!empty($row['Source_function']))
 449                                  {
 450                                      $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
 451                                  }
 452  
 453                                  // remove unsupported features
 454                                  foreach ($row as $key => $val)
 455                                  {
 456                                      if ($val === null)
 457                                      {
 458                                          unset($row[$key]);
 459                                      }
 460                                  }
 461                                  $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 462                              }
 463                              mysqli_free_result($result);
 464                          }
 465  
 466                          if ($html_table)
 467                          {
 468                              $this->html_hold .= '</table>';
 469                          }
 470  
 471                          @mysqli_query($this->db_connect_id, 'SET profiling = 0;');
 472                      }
 473                  }
 474  
 475              break;
 476  
 477              case 'fromcache':
 478                  $endtime = explode(' ', microtime());
 479                  $endtime = $endtime[0] + $endtime[1];
 480  
 481                  $result = @mysqli_query($this->db_connect_id, $query);
 482                  if ($result)
 483                  {
 484                      while ($void = mysqli_fetch_assoc($result))
 485                      {
 486                          // Take the time spent on parsing rows into account
 487                      }
 488                      mysqli_free_result($result);
 489                  }
 490  
 491                  $splittime = explode(' ', microtime());
 492                  $splittime = $splittime[0] + $splittime[1];
 493  
 494                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 495  
 496              break;
 497          }
 498      }
 499  
 500      /**
 501      * {@inheritDoc}
 502      */
 503  	function sql_quote($msg)
 504      {
 505          return '`' . $msg . '`';
 506      }
 507  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1