Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

mysql_extractor.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 8.75 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb\db\extractor;
 015   
 016  use phpbb\db\extractor\exception\extractor_not_initialized_exception;
 017   
 018  class mysql_extractor extends base_extractor
 019  {
 020      /**
 021      * {@inheritdoc}
 022      */
 023      public function write_start($table_prefix)
 024      {
 025          if (!$this->is_initialized)
 026          {
 027              throw new extractor_not_initialized_exception();
 028          }
 029   
 030          $sql_data = "#\n";
 031          $sql_data .= "# phpBB Backup Script\n";
 032          $sql_data .= "# Dump of tables for $table_prefix\n";
 033          $sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
 034          $sql_data .= "#\n";
 035          $this->flush($sql_data);
 036      }
 037   
 038      /**
 039      * {@inheritdoc}
 040      */
 041      public function write_table($table_name)
 042      {
 043          static $new_extract;
 044   
 045          if (!$this->is_initialized)
 046          {
 047              throw new extractor_not_initialized_exception();
 048          }
 049   
 050          if ($new_extract === null)
 051          {
 052              if ($this->db->get_sql_layer() === 'mysqli' || version_compare($this->db->sql_server_info(true), '3.23.20', '>='))
 053              {
 054                  $new_extract = true;
 055              }
 056              else
 057              {
 058                  $new_extract = false;
 059              }
 060          }
 061   
 062          if ($new_extract)
 063          {
 064              $this->new_write_table($table_name);
 065          }
 066          else
 067          {
 068              $this->old_write_table($table_name);
 069          }
 070      }
 071   
 072      /**
 073      * {@inheritdoc}
 074      */
 075      public function write_data($table_name)
 076      {
 077          if (!$this->is_initialized)
 078          {
 079              throw new extractor_not_initialized_exception();
 080          }
 081   
 082          if ($this->db->get_sql_layer() === 'mysqli')
 083          {
 084              $this->write_data_mysqli($table_name);
 085          }
 086          else
 087          {
 088              $this->write_data_mysql($table_name);
 089          }
 090      }
 091   
 092      /**
 093      * Extracts data from database table (for MySQLi driver)
 094      *
 095      * @param    string    $table_name    name of the database table
 096      * @return null
 097      * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
 098      */
 099      protected function write_data_mysqli($table_name)
 100      {
 101          if (!$this->is_initialized)
 102          {
 103              throw new extractor_not_initialized_exception();
 104          }
 105   
 106          $sql = "SELECT *
 107              FROM $table_name";
 108          $result = mysqli_query($this->db->get_db_connect_id(), $sql, MYSQLI_USE_RESULT);
 109          if ($result != false)
 110          {
 111              $fields_cnt = mysqli_num_fields($result);
 112   
 113              // Get field information
 114              $field = mysqli_fetch_fields($result);
 115              $field_set = array();
 116   
 117              for ($j = 0; $j < $fields_cnt; $j++)
 118              {
 119                  $field_set[] = $field[$j]->name;
 120              }
 121   
 122              $search            = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
 123              $replace        = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
 124              $fields            = implode(', ', $field_set);
 125              $sql_data        = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
 126              $first_set        = true;
 127              $query_len        = 0;
 128              $max_len        = get_usable_memory();
 129   
 130              while ($row = mysqli_fetch_row($result))
 131              {
 132                  $values    = array();
 133                  if ($first_set)
 134                  {
 135                      $query = $sql_data . '(';
 136                  }
 137                  else
 138                  {
 139                      $query  .= ',(';
 140                  }
 141   
 142                  for ($j = 0; $j < $fields_cnt; $j++)
 143                  {
 144                      if (!isset($row[$j]) || is_null($row[$j]))
 145                      {
 146                          $values[$j] = 'NULL';
 147                      }
 148                      else if (($field[$j]->flags & 32768) && !($field[$j]->flags & 1024))
 149                      {
 150                          $values[$j] = $row[$j];
 151                      }
 152                      else
 153                      {
 154                          $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
 155                      }
 156                  }
 157                  $query .= implode(', ', $values) . ')';
 158   
 159                  $query_len += strlen($query);
 160                  if ($query_len > $max_len)
 161                  {
 162                      $this->flush($query . ";\n\n");
 163                      $query = '';
 164                      $query_len = 0;
 165                      $first_set = true;
 166                  }
 167                  else
 168                  {
 169                      $first_set = false;
 170                  }
 171              }
 172              mysqli_free_result($result);
 173   
 174              // check to make sure we have nothing left to flush
 175              if (!$first_set && $query)
 176              {
 177                  $this->flush($query . ";\n\n");
 178              }
 179          }
 180      }
 181   
 182      /**
 183      * Extracts data from database table (for MySQL driver)
 184      *
 185      * @param    string    $table_name    name of the database table
 186      * @return null
 187      * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
 188      */
 189      protected function write_data_mysql($table_name)
 190      {
 191          if (!$this->is_initialized)
 192          {
 193              throw new extractor_not_initialized_exception();
 194          }
 195   
 196          $sql = "SELECT *
 197              FROM $table_name";
 198          $result = mysql_unbuffered_query($sql, $this->db->get_db_connect_id());
 199   
 200          if ($result != false)
 201          {
 202              $fields_cnt = mysql_num_fields($result);
 203   
 204              // Get field information
 205              $field = array();
 206              for ($i = 0; $i < $fields_cnt; $i++)
 207              {
 208                  $field[] = mysql_fetch_field($result, $i);
 209              }
 210              $field_set = array();
 211   
 212              for ($j = 0; $j < $fields_cnt; $j++)
 213              {
 214                  $field_set[] = $field[$j]->name;
 215              }
 216   
 217              $search            = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
 218              $replace        = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
 219              $fields            = implode(', ', $field_set);
 220              $sql_data        = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
 221              $first_set        = true;
 222              $query_len        = 0;
 223              $max_len        = get_usable_memory();
 224   
 225              while ($row = mysql_fetch_row($result))
 226              {
 227                  $values = array();
 228                  if ($first_set)
 229                  {
 230                      $query = $sql_data . '(';
 231                  }
 232                  else
 233                  {
 234                      $query  .= ',(';
 235                  }
 236   
 237                  for ($j = 0; $j < $fields_cnt; $j++)
 238                  {
 239                      if (!isset($row[$j]) || is_null($row[$j]))
 240                      {
 241                          $values[$j] = 'NULL';
 242                      }
 243                      else if ($field[$j]->numeric && ($field[$j]->type !== 'timestamp'))
 244                      {
 245                          $values[$j] = $row[$j];
 246                      }
 247                      else
 248                      {
 249                          $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
 250                      }
 251                  }
 252                  $query .= implode(', ', $values) . ')';
 253   
 254                  $query_len += strlen($query);
 255                  if ($query_len > $max_len)
 256                  {
 257                      $this->flush($query . ";\n\n");
 258                      $query = '';
 259                      $query_len = 0;
 260                      $first_set = true;
 261                  }
 262                  else
 263                  {
 264                      $first_set = false;
 265                  }
 266              }
 267              mysql_free_result($result);
 268   
 269              // check to make sure we have nothing left to flush
 270              if (!$first_set && $query)
 271              {
 272                  $this->flush($query . ";\n\n");
 273              }
 274          }
 275      }
 276   
 277      /**
 278      * Extracts database table structure (for MySQLi or MySQL 3.23.20+)
 279      *
 280      * @param    string    $table_name    name of the database table
 281      * @return null
 282      * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
 283      */
 284      protected function new_write_table($table_name)
 285      {
 286          if (!$this->is_initialized)
 287          {
 288              throw new extractor_not_initialized_exception();
 289          }
 290   
 291          $sql = 'SHOW CREATE TABLE ' . $table_name;
 292          $result = $this->db->sql_query($sql);
 293          $row = $this->db->sql_fetchrow($result);
 294   
 295          $sql_data = '# Table: ' . $table_name . "\n";
 296          $sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
 297          $this->flush($sql_data . $row['Create Table'] . ";\n\n");
 298   
 299          $this->db->sql_freeresult($result);
 300      }
 301   
 302      /**
 303      * Extracts database table structure (for MySQL verisons older than 3.23.20)
 304      *
 305      * @param    string    $table_name    name of the database table
 306      * @return null
 307      * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
 308      */
 309      protected function old_write_table($table_name)
 310      {
 311          if (!$this->is_initialized)
 312          {
 313              throw new extractor_not_initialized_exception();
 314          }
 315   
 316          $sql_data = '# Table: ' . $table_name . "\n";
 317          $sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
 318          $sql_data .= "CREATE TABLE $table_name(\n";
 319          $rows = array();
 320   
 321          $sql = "SHOW FIELDS
 322              FROM $table_name";
 323          $result = $this->db->sql_query($sql);
 324   
 325          while ($row = $this->db->sql_fetchrow($result))
 326          {
 327              $line = '   ' . $row['Field'] . ' ' . $row['Type'];
 328   
 329              if (!is_null($row['Default']))
 330              {
 331                  $line .= " DEFAULT '{$row['Default']}'";
 332              }
 333   
 334              if ($row['Null'] != 'YES')
 335              {
 336                  $line .= ' NOT NULL';
 337              }
 338   
 339              if ($row['Extra'] != '')
 340              {
 341                  $line .= ' ' . $row['Extra'];
 342              }
 343   
 344              $rows[] = $line;
 345          }
 346          $this->db->sql_freeresult($result);
 347   
 348          $sql = "SHOW KEYS
 349              FROM $table_name";
 350   
 351          $result = $this->db->sql_query($sql);
 352   
 353          $index = array();
 354          while ($row = $this->db->sql_fetchrow($result))
 355          {
 356              $kname = $row['Key_name'];
 357   
 358              if ($kname != 'PRIMARY')
 359              {
 360                  if ($row['Non_unique'] == 0)
 361                  {
 362                      $kname = "UNIQUE|$kname";
 363                  }
 364              }
 365   
 366              if ($row['Sub_part'])
 367              {
 368                  $row['Column_name'] .= '(' . $row['Sub_part'] . ')';
 369              }
 370              $index[$kname][] = $row['Column_name'];
 371          }
 372          $this->db->sql_freeresult($result);
 373   
 374          foreach ($index as $key => $columns)
 375          {
 376              $line = '   ';
 377   
 378              if ($key == 'PRIMARY')
 379              {
 380                  $line .= 'PRIMARY KEY (' . implode(', ', $columns) . ')';
 381              }
 382              else if (strpos($key, 'UNIQUE') === 0)
 383              {
 384                  $line .= 'UNIQUE ' . substr($key, 7) . ' (' . implode(', ', $columns) . ')';
 385              }
 386              else if (strpos($key, 'FULLTEXT') === 0)
 387              {
 388                  $line .= 'FULLTEXT ' . substr($key, 9) . ' (' . implode(', ', $columns) . ')';
 389              }
 390              else
 391              {
 392                  $line .= "KEY $key (" . implode(', ', $columns) . ')';
 393              }
 394   
 395              $rows[] = $line;
 396          }
 397   
 398          $sql_data .= implode(",\n", $rows);
 399          $sql_data .= "\n);\n\n";
 400   
 401          $this->flush($sql_data);
 402      }
 403  }
 404