[ Index ]

PHP Cross Reference of phpBB-3.2.11-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              @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
  75  
  76              // enforce strict mode on databases that support it
  77              if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
  78              {
  79                  $result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
  80                  if ($result)
  81                  {
  82                      $row = mysqli_fetch_assoc($result);
  83                      mysqli_free_result($result);
  84  
  85                      $modes = array_map('trim', explode(',', $row['sql_mode']));
  86                  }
  87                  else
  88                  {
  89                      $modes = array();
  90                  }
  91  
  92                  // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
  93                  if (!in_array('TRADITIONAL', $modes))
  94                  {
  95                      if (!in_array('STRICT_ALL_TABLES', $modes))
  96                      {
  97                          $modes[] = 'STRICT_ALL_TABLES';
  98                      }
  99  
 100                      if (!in_array('STRICT_TRANS_TABLES', $modes))
 101                      {
 102                          $modes[] = 'STRICT_TRANS_TABLES';
 103                      }
 104                  }
 105  
 106                  $mode = implode(',', $modes);
 107                  @mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
 108              }
 109              return $this->db_connect_id;
 110          }
 111  
 112          return $this->sql_error('');
 113      }
 114  
 115      /**
 116      * {@inheritDoc}
 117      */
 118  	function sql_server_info($raw = false, $use_cache = true)
 119      {
 120          global $cache;
 121  
 122          if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false)
 123          {
 124              $result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
 125              if ($result)
 126              {
 127                  $row = mysqli_fetch_assoc($result);
 128                  mysqli_free_result($result);
 129  
 130                  $this->sql_server_version = $row['version'];
 131  
 132                  if (!empty($cache) && $use_cache)
 133                  {
 134                      $cache->put('mysqli_version', $this->sql_server_version);
 135                  }
 136              }
 137          }
 138  
 139          return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
 140      }
 141  
 142      /**
 143      * SQL Transaction
 144      * @access private
 145      */
 146  	function _sql_transaction($status = 'begin')
 147      {
 148          switch ($status)
 149          {
 150              case 'begin':
 151                  return @mysqli_autocommit($this->db_connect_id, false);
 152              break;
 153  
 154              case 'commit':
 155                  $result = @mysqli_commit($this->db_connect_id);
 156                  @mysqli_autocommit($this->db_connect_id, true);
 157                  return $result;
 158              break;
 159  
 160              case 'rollback':
 161                  $result = @mysqli_rollback($this->db_connect_id);
 162                  @mysqli_autocommit($this->db_connect_id, true);
 163                  return $result;
 164              break;
 165          }
 166  
 167          return true;
 168      }
 169  
 170      /**
 171      * {@inheritDoc}
 172      */
 173  	function sql_query($query = '', $cache_ttl = 0)
 174      {
 175          if ($query != '')
 176          {
 177              global $cache;
 178  
 179              // EXPLAIN only in extra debug mode
 180              if (defined('DEBUG'))
 181              {
 182                  $this->sql_report('start', $query);
 183              }
 184              else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 185              {
 186                  $this->curtime = microtime(true);
 187              }
 188  
 189              $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
 190              $this->sql_add_num_queries($this->query_result);
 191  
 192              if ($this->query_result === false)
 193              {
 194                  if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
 195                  {
 196                      $this->sql_error($query);
 197                  }
 198  
 199                  if (defined('DEBUG'))
 200                  {
 201                      $this->sql_report('stop', $query);
 202                  }
 203                  else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 204                  {
 205                      $this->sql_time += microtime(true) - $this->curtime;
 206                  }
 207  
 208                  if (!$this->query_result)
 209                  {
 210                      return false;
 211                  }
 212  
 213                  if ($cache && $cache_ttl)
 214                  {
 215                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 216                  }
 217              }
 218              else if (defined('DEBUG'))
 219              {
 220                  $this->sql_report('fromcache', $query);
 221              }
 222          }
 223          else
 224          {
 225              return false;
 226          }
 227  
 228          return $this->query_result;
 229      }
 230  
 231      /**
 232      * {@inheritDoc}
 233      */
 234  	function sql_affectedrows()
 235      {
 236          return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
 237      }
 238  
 239      /**
 240      * {@inheritDoc}
 241      */
 242  	function sql_fetchrow($query_id = false)
 243      {
 244          global $cache;
 245  
 246          if ($query_id === false)
 247          {
 248              $query_id = $this->query_result;
 249          }
 250  
 251          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 252          {
 253              return $cache->sql_fetchrow($query_id);
 254          }
 255  
 256          if ($query_id)
 257          {
 258              $result = mysqli_fetch_assoc($query_id);
 259              return $result !== null ? $result : false;
 260          }
 261  
 262          return false;
 263      }
 264  
 265      /**
 266      * {@inheritDoc}
 267      */
 268  	function sql_rowseek($rownum, &$query_id)
 269      {
 270          global $cache;
 271  
 272          if ($query_id === false)
 273          {
 274              $query_id = $this->query_result;
 275          }
 276  
 277          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 278          {
 279              return $cache->sql_rowseek($rownum, $query_id);
 280          }
 281  
 282          return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false;
 283      }
 284  
 285      /**
 286      * {@inheritDoc}
 287      */
 288  	function sql_nextid()
 289      {
 290          return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
 291      }
 292  
 293      /**
 294      * {@inheritDoc}
 295      */
 296  	function sql_freeresult($query_id = false)
 297      {
 298          global $cache;
 299  
 300          if ($query_id === false)
 301          {
 302              $query_id = $this->query_result;
 303          }
 304  
 305          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 306          {
 307              return $cache->sql_freeresult($query_id);
 308          }
 309  
 310          if (!$query_id)
 311          {
 312              return false;
 313          }
 314  
 315          if ($query_id === true)
 316          {
 317              return true;
 318          }
 319  
 320          return mysqli_free_result($query_id);
 321      }
 322  
 323      /**
 324      * {@inheritDoc}
 325      */
 326  	function sql_escape($msg)
 327      {
 328          return @mysqli_real_escape_string($this->db_connect_id, $msg);
 329      }
 330  
 331      /**
 332      * return sql error array
 333      * @access private
 334      */
 335  	function _sql_error()
 336      {
 337          if ($this->db_connect_id)
 338          {
 339              $error = array(
 340                  'message'    => @mysqli_error($this->db_connect_id),
 341                  'code'        => @mysqli_errno($this->db_connect_id)
 342              );
 343          }
 344          else if (function_exists('mysqli_connect_error'))
 345          {
 346              $error = array(
 347                  'message'    => @mysqli_connect_error(),
 348                  'code'        => @mysqli_connect_errno(),
 349              );
 350          }
 351          else
 352          {
 353              $error = array(
 354                  'message'    => $this->connect_error,
 355                  'code'        => '',
 356              );
 357          }
 358  
 359          return $error;
 360      }
 361  
 362      /**
 363      * Close sql connection
 364      * @access private
 365      */
 366  	function _sql_close()
 367      {
 368          return @mysqli_close($this->db_connect_id);
 369      }
 370  
 371      /**
 372      * Build db-specific report
 373      * @access private
 374      */
 375  	function _sql_report($mode, $query = '')
 376      {
 377          static $test_prof;
 378  
 379          // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
 380          if ($test_prof === null)
 381          {
 382              $test_prof = false;
 383              if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
 384              {
 385                  $ver = mysqli_get_server_version($this->db_connect_id);
 386                  if ($ver >= 50037 && $ver < 50100)
 387                  {
 388                      $test_prof = true;
 389                  }
 390              }
 391          }
 392  
 393          switch ($mode)
 394          {
 395              case 'start':
 396  
 397                  $explain_query = $query;
 398                  if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 399                  {
 400                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 401                  }
 402                  else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 403                  {
 404                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 405                  }
 406  
 407                  if (preg_match('/^SELECT/', $explain_query))
 408                  {
 409                      $html_table = false;
 410  
 411                      // begin profiling
 412                      if ($test_prof)
 413                      {
 414                          @mysqli_query($this->db_connect_id, 'SET profiling = 1;');
 415                      }
 416  
 417                      if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
 418                      {
 419                          while ($row = mysqli_fetch_assoc($result))
 420                          {
 421                              $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 422                          }
 423                          mysqli_free_result($result);
 424                      }
 425  
 426                      if ($html_table)
 427                      {
 428                          $this->html_hold .= '</table>';
 429                      }
 430  
 431                      if ($test_prof)
 432                      {
 433                          $html_table = false;
 434  
 435                          // get the last profile
 436                          if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
 437                          {
 438                              $this->html_hold .= '<br />';
 439                              while ($row = mysqli_fetch_assoc($result))
 440                              {
 441                                  // make <unknown> HTML safe
 442                                  if (!empty($row['Source_function']))
 443                                  {
 444                                      $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
 445                                  }
 446  
 447                                  // remove unsupported features
 448                                  foreach ($row as $key => $val)
 449                                  {
 450                                      if ($val === null)
 451                                      {
 452                                          unset($row[$key]);
 453                                      }
 454                                  }
 455                                  $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 456                              }
 457                              mysqli_free_result($result);
 458                          }
 459  
 460                          if ($html_table)
 461                          {
 462                              $this->html_hold .= '</table>';
 463                          }
 464  
 465                          @mysqli_query($this->db_connect_id, 'SET profiling = 0;');
 466                      }
 467                  }
 468  
 469              break;
 470  
 471              case 'fromcache':
 472                  $endtime = explode(' ', microtime());
 473                  $endtime = $endtime[0] + $endtime[1];
 474  
 475                  $result = @mysqli_query($this->db_connect_id, $query);
 476                  if ($result)
 477                  {
 478                      while ($void = mysqli_fetch_assoc($result))
 479                      {
 480                          // Take the time spent on parsing rows into account
 481                      }
 482                      mysqli_free_result($result);
 483                  }
 484  
 485                  $splittime = explode(' ', microtime());
 486                  $splittime = $splittime[0] + $splittime[1];
 487  
 488                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 489  
 490              break;
 491          }
 492      }
 493  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1