[ Index ]

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1