[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/includes/acp/ -> acp_database.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  /**
  15  * @ignore
  16  */
  17  if (!defined('IN_PHPBB'))
  18  {
  19      exit;
  20  }
  21  
  22  class acp_database
  23  {
  24      var $db_tools;
  25      var $u_action;
  26      public $page_title;
  27  
  28  	function main($id, $mode)
  29      {
  30          global $cache, $db, $user, $template, $table_prefix, $request;
  31          global $phpbb_root_path, $phpbb_container, $phpbb_log;
  32  
  33          $this->db_tools = $phpbb_container->get('dbal.tools');
  34  
  35          $user->add_lang('acp/database');
  36  
  37          $this->tpl_name = 'acp_database';
  38          $this->page_title = 'ACP_DATABASE';
  39  
  40          $action    = $request->variable('action', '');
  41  
  42          $form_key = 'acp_database';
  43          add_form_key($form_key);
  44  
  45          $template->assign_vars(array(
  46              'MODE'    => $mode
  47          ));
  48  
  49          switch ($mode)
  50          {
  51              case 'backup':
  52  
  53                  $this->page_title = 'ACP_BACKUP';
  54  
  55                  switch ($action)
  56                  {
  57                      case 'download':
  58                          $type    = $request->variable('type', '');
  59                          $table    = array_intersect($this->db_tools->sql_list_tables(), $request->variable('table', array('')));
  60                          $format    = $request->variable('method', '');
  61  
  62                          if (!count($table))
  63                          {
  64                              trigger_error($user->lang['TABLE_SELECT_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
  65                          }
  66  
  67                          if (!check_form_key($form_key))
  68                          {
  69                              trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
  70                          }
  71  
  72                          $store = true;
  73                          $structure = false;
  74                          $schema_data = false;
  75  
  76                          if ($type == 'full' || $type == 'structure')
  77                          {
  78                              $structure = true;
  79                          }
  80  
  81                          if ($type == 'full' || $type == 'data')
  82                          {
  83                              $schema_data = true;
  84                          }
  85  
  86                          @set_time_limit(1200);
  87                          @set_time_limit(0);
  88  
  89                          $time = time();
  90  
  91                          $filename = 'backup_' . $time . '_' . unique_id();
  92  
  93                          /** @var phpbb\db\extractor\extractor_interface $extractor Database extractor */
  94                          $extractor = $phpbb_container->get('dbal.extractor');
  95                          $extractor->init_extractor($format, $filename, $time, false, $store);
  96  
  97                          $extractor->write_start($table_prefix);
  98  
  99                          foreach ($table as $table_name)
 100                          {
 101                              // Get the table structure
 102                              if ($structure)
 103                              {
 104                                  $extractor->write_table($table_name);
 105                              }
 106                              else
 107                              {
 108                                  // We might wanna empty out all that junk :D
 109                                  switch ($db->get_sql_layer())
 110                                  {
 111                                      case 'sqlite3':
 112                                          $extractor->flush('DELETE FROM ' . $table_name . ";\n");
 113                                      break;
 114  
 115                                      case 'mssql_odbc':
 116                                      case 'mssqlnative':
 117                                          $extractor->flush('TRUNCATE TABLE ' . $table_name . "GO\n");
 118                                      break;
 119  
 120                                      case 'oracle':
 121                                          $extractor->flush('TRUNCATE TABLE ' . $table_name . "/\n");
 122                                      break;
 123  
 124                                      default:
 125                                          $extractor->flush('TRUNCATE TABLE ' . $table_name . ";\n");
 126                                      break;
 127                                  }
 128                              }
 129  
 130                              // Data
 131                              if ($schema_data)
 132                              {
 133                                  $extractor->write_data($table_name);
 134                              }
 135                          }
 136  
 137                          $extractor->write_end();
 138  
 139                          $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_DB_BACKUP');
 140  
 141                          trigger_error($user->lang['BACKUP_SUCCESS'] . adm_back_link($this->u_action));
 142                      break;
 143  
 144                      default:
 145                          $tables = $this->db_tools->sql_list_tables();
 146                          asort($tables);
 147                          foreach ($tables as $table_name)
 148                          {
 149                              if (strlen($table_prefix) === 0 || stripos($table_name, $table_prefix) === 0)
 150                              {
 151                                  $template->assign_block_vars('tables', array(
 152                                      'TABLE'    => $table_name
 153                                  ));
 154                              }
 155                          }
 156                          unset($tables);
 157  
 158                          $template->assign_vars(array(
 159                              'U_ACTION'    => $this->u_action . '&amp;action=download'
 160                          ));
 161  
 162                          $available_methods = array('gzip' => 'zlib', 'bzip2' => 'bz2');
 163  
 164                          foreach ($available_methods as $type => $module)
 165                          {
 166                              if (!@extension_loaded($module))
 167                              {
 168                                  continue;
 169                              }
 170  
 171                              $template->assign_block_vars('methods', array(
 172                                  'TYPE'    => $type
 173                              ));
 174                          }
 175  
 176                          $template->assign_block_vars('methods', array(
 177                              'TYPE'    => 'text'
 178                          ));
 179                      break;
 180                  }
 181              break;
 182  
 183              case 'restore':
 184  
 185                  $this->page_title = 'ACP_RESTORE';
 186  
 187                  switch ($action)
 188                  {
 189                      case 'submit':
 190                          $delete = $request->variable('delete', '');
 191                          $file = $request->variable('file', '');
 192  
 193                          $backup_info = $this->get_backup_file($phpbb_root_path . 'store/', $file);
 194  
 195                          if (empty($backup_info) || !is_readable($backup_info['file_name']))
 196                          {
 197                              trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
 198                          }
 199  
 200                          if ($delete)
 201                          {
 202                              if (confirm_box(true))
 203                              {
 204                                  unlink($backup_info['file_name']);
 205                                  $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_DB_DELETE');
 206                                  trigger_error($user->lang['BACKUP_DELETE'] . adm_back_link($this->u_action));
 207                              }
 208                              else
 209                              {
 210                                  confirm_box(false, $user->lang['DELETE_SELECTED_BACKUP'], build_hidden_fields(array('delete' => $delete, 'file' => $file)));
 211                              }
 212                          }
 213                          else if (confirm_box(true))
 214                          {
 215                              switch ($backup_info['extension'])
 216                              {
 217                                  case 'sql':
 218                                      $fp = fopen($backup_info['file_name'], 'rb');
 219                                      $read = 'fread';
 220                                      $seek = 'fseek';
 221                                      $eof = 'feof';
 222                                      $close = 'fclose';
 223                                      $fgetd = 'fgetd';
 224                                  break;
 225  
 226                                  case 'sql.bz2':
 227                                      $fp = bzopen($backup_info['file_name'], 'r');
 228                                      $read = 'bzread';
 229                                      $seek = '';
 230                                      $eof = 'feof';
 231                                      $close = 'bzclose';
 232                                      $fgetd = 'fgetd_seekless';
 233                                  break;
 234  
 235                                  case 'sql.gz':
 236                                      $fp = gzopen($backup_info['file_name'], 'rb');
 237                                      $read = 'gzread';
 238                                      $seek = 'gzseek';
 239                                      $eof = 'gzeof';
 240                                      $close = 'gzclose';
 241                                      $fgetd = 'fgetd';
 242                                  break;
 243  
 244                                  default:
 245                                      trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
 246                                      return;
 247                              }
 248  
 249                              switch ($db->get_sql_layer())
 250                              {
 251                                  case 'mysqli':
 252                                  case 'sqlite3':
 253                                      while (($sql = $fgetd($fp, ";\n", $read, $seek, $eof)) !== false)
 254                                      {
 255                                          $db->sql_query($sql);
 256                                      }
 257                                  break;
 258  
 259                                  case 'postgres':
 260                                      $delim = ";\n";
 261                                      while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
 262                                      {
 263                                          $query = trim($sql);
 264  
 265                                          if (substr($query, 0, 13) == 'CREATE DOMAIN')
 266                                          {
 267                                              list(, , $domain) = explode(' ', $query);
 268                                              $sql = "SELECT domain_name
 269                                                  FROM information_schema.domains
 270                                                  WHERE domain_name = '$domain';";
 271                                              $result = $db->sql_query($sql);
 272                                              if (!$db->sql_fetchrow($result))
 273                                              {
 274                                                  $db->sql_query($query);
 275                                              }
 276                                              $db->sql_freeresult($result);
 277                                          }
 278                                          else
 279                                          {
 280                                              $db->sql_query($query);
 281                                          }
 282  
 283                                          if (substr($query, 0, 4) == 'COPY')
 284                                          {
 285                                              while (($sub = $fgetd($fp, "\n", $read, $seek, $eof)) !== '\.')
 286                                              {
 287                                                  if ($sub === false)
 288                                                  {
 289                                                      trigger_error($user->lang['RESTORE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
 290                                                  }
 291                                                  pg_put_line($db->get_db_connect_id(), $sub . "\n");
 292                                              }
 293                                              pg_put_line($db->get_db_connect_id(), "\\.\n");
 294                                              pg_end_copy($db->get_db_connect_id());
 295                                          }
 296                                      }
 297                                  break;
 298  
 299                                  case 'oracle':
 300                                      while (($sql = $fgetd($fp, "/\n", $read, $seek, $eof)) !== false)
 301                                      {
 302                                          $db->sql_query($sql);
 303                                      }
 304                                  break;
 305  
 306                                  case 'mssql_odbc':
 307                                  case 'mssqlnative':
 308                                      while (($sql = $fgetd($fp, "GO\n", $read, $seek, $eof)) !== false)
 309                                      {
 310                                          $db->sql_query($sql);
 311                                      }
 312                                  break;
 313                              }
 314  
 315                              $close($fp);
 316  
 317                              // Purge the cache due to updated data
 318                              $cache->purge();
 319  
 320                              $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_DB_RESTORE');
 321                              trigger_error($user->lang['RESTORE_SUCCESS'] . adm_back_link($this->u_action));
 322                              break;
 323                          }
 324                          else
 325                          {
 326                              confirm_box(false, $user->lang['RESTORE_SELECTED_BACKUP'], build_hidden_fields(array('file' => $file)));
 327                          }
 328  
 329                      default:
 330                          $backup_files = $this->get_file_list($phpbb_root_path . 'store/');
 331  
 332                          if (!empty($backup_files))
 333                          {
 334                              krsort($backup_files);
 335  
 336                              foreach ($backup_files as $name => $file)
 337                              {
 338                                  $template->assign_block_vars('files', array(
 339                                      'FILE'        => sha1($file),
 340                                      'NAME'        => $user->format_date($name, 'd-m-Y H:i', true),
 341                                      'SUPPORTED'    => true,
 342                                  ));
 343                              }
 344                          }
 345  
 346                          $template->assign_vars(array(
 347                              'U_ACTION'    => $this->u_action . '&amp;action=submit'
 348                          ));
 349                      break;
 350                  }
 351              break;
 352          }
 353      }
 354  
 355      /**
 356       * Get backup file from file hash
 357       *
 358       * @param string $directory Relative path to directory
 359       * @param string $file_hash Hash of selected file
 360       *
 361       * @return array Backup file data or empty array if unable to find file
 362       */
 363  	protected function get_backup_file($directory, $file_hash)
 364      {
 365          $backup_data = [];
 366  
 367          $file_list = $this->get_file_list($directory);
 368          $supported_extensions = $this->get_supported_extensions();
 369  
 370          foreach ($file_list as $file)
 371          {
 372              preg_match('#^backup_(\d{10,})_(?:[a-z\d]{16}|[a-z\d]{32})\.(sql(?:\.(?:gz|bz2))?)$#i', $file, $matches);
 373              if (sha1($file) === $file_hash && in_array($matches[2], $supported_extensions))
 374              {
 375                  $backup_data = [
 376                      'file_name' => $directory . $file,
 377                      'extension' => $matches[2],
 378                  ];
 379                  break;
 380              }
 381          }
 382  
 383          return $backup_data;
 384      }
 385  
 386      /**
 387       * Get backup file list for directory
 388       *
 389       * @param string $directory Relative path to backup directory
 390       *
 391       * @return array List of backup files in specified directory
 392       */
 393  	protected function get_file_list($directory)
 394      {
 395          $supported_extensions = $this->get_supported_extensions();
 396  
 397          $dh = @opendir($directory);
 398  
 399          $backup_files = [];
 400  
 401          if ($dh)
 402          {
 403              while (($file = readdir($dh)) !== false)
 404              {
 405                  if (preg_match('#^backup_(\d{10,})_(?:[a-z\d]{16}|[a-z\d]{32})\.(sql(?:\.(?:gz|bz2))?)$#i', $file, $matches))
 406                  {
 407                      if (in_array($matches[2], $supported_extensions))
 408                      {
 409                          $backup_files[(int) $matches[1]] = $file;
 410                      }
 411                  }
 412              }
 413              closedir($dh);
 414          }
 415  
 416          return $backup_files;
 417      }
 418  
 419      /**
 420       * Get supported extensions for backup
 421       *
 422       * @return array List of supported extensions
 423       */
 424  	protected function get_supported_extensions()
 425      {
 426          $extensions = ['sql'];
 427          $available_methods = ['sql.gz' => 'zlib', 'sql.bz2' => 'bz2'];
 428  
 429          foreach ($available_methods as $type => $module)
 430          {
 431              if (!@extension_loaded($module))
 432              {
 433                  continue;
 434              }
 435              $extensions[] = $type;
 436          }
 437  
 438          return $extensions;
 439      }
 440  }
 441  
 442  // get how much space we allow for a chunk of data, very similar to phpMyAdmin's way of doing things ;-) (hey, we only do this for MySQL anyway :P)
 443  function get_usable_memory()
 444  {
 445      $val = trim(@ini_get('memory_limit'));
 446  
 447      if (preg_match('/(\\d+)([mkg]?)/i', $val, $regs))
 448      {
 449          $memory_limit = (int) $regs[1];
 450          switch ($regs[2])
 451          {
 452  
 453              case 'k':
 454              case 'K':
 455                  $memory_limit *= 1024;
 456              break;
 457  
 458              case 'm':
 459              case 'M':
 460                  $memory_limit *= 1048576;
 461              break;
 462  
 463              case 'g':
 464              case 'G':
 465                  $memory_limit *= 1073741824;
 466              break;
 467          }
 468  
 469          // how much memory PHP requires at the start of export (it is really a little less)
 470          if ($memory_limit > 6100000)
 471          {
 472              $memory_limit -= 6100000;
 473          }
 474  
 475          // allow us to consume half of the total memory available
 476          $memory_limit /= 2;
 477      }
 478      else
 479      {
 480          // set the buffer to 1M if we have no clue how much memory PHP will give us :P
 481          $memory_limit = 1048576;
 482      }
 483  
 484      return $memory_limit;
 485  }
 486  
 487  function sanitize_data_mssql($text)
 488  {
 489      $data = preg_split('/[\n\t\r\b\f]/', $text);
 490      preg_match_all('/[\n\t\r\b\f]/', $text, $matches);
 491  
 492      $val = array();
 493  
 494      foreach ($data as $value)
 495      {
 496          if (strlen($value))
 497          {
 498              $val[] = "'" . $value . "'";
 499          }
 500          if (count($matches[0]))
 501          {
 502              $val[] = 'char(' . ord(array_shift($matches[0])) . ')';
 503          }
 504      }
 505  
 506      return implode('+', $val);
 507  }
 508  
 509  function sanitize_data_oracle($text)
 510  {
 511  //    $data = preg_split('/[\0\n\t\r\b\f\'"\/\\\]/', $text);
 512  //    preg_match_all('/[\0\n\t\r\b\f\'"\/\\\]/', $text, $matches);
 513      $data = preg_split('/[\0\b\f\'\/]/', $text);
 514      preg_match_all('/[\0\r\b\f\'\/]/', $text, $matches);
 515  
 516      $val = array();
 517  
 518      foreach ($data as $value)
 519      {
 520          if (strlen($value))
 521          {
 522              $val[] = "'" . $value . "'";
 523          }
 524          if (count($matches[0]))
 525          {
 526              $val[] = 'chr(' . ord(array_shift($matches[0])) . ')';
 527          }
 528      }
 529  
 530      return implode('||', $val);
 531  }
 532  
 533  function sanitize_data_generic($text)
 534  {
 535      $data = preg_split('/[\n\t\r\b\f]/', $text);
 536      preg_match_all('/[\n\t\r\b\f]/', $text, $matches);
 537  
 538      $val = array();
 539  
 540      foreach ($data as $value)
 541      {
 542          if (strlen($value))
 543          {
 544              $val[] = "'" . $value . "'";
 545          }
 546          if (count($matches[0]))
 547          {
 548              $val[] = "'" . array_shift($matches[0]) . "'";
 549          }
 550      }
 551  
 552      return implode('||', $val);
 553  }
 554  
 555  // modified from PHP.net
 556  function fgetd(&$fp, $delim, $read, $seek, $eof, $buffer = 8192)
 557  {
 558      $record = '';
 559      $delim_len = strlen($delim);
 560  
 561      while (!$eof($fp))
 562      {
 563          $pos = strpos($record, $delim);
 564          if ($pos === false)
 565          {
 566              $record .= $read($fp, $buffer);
 567              if ($eof($fp) && ($pos = strpos($record, $delim)) !== false)
 568              {
 569                  $seek($fp, $pos + $delim_len - strlen($record), SEEK_CUR);
 570                  return substr($record, 0, $pos);
 571              }
 572          }
 573          else
 574          {
 575              $seek($fp, $pos + $delim_len - strlen($record), SEEK_CUR);
 576              return substr($record, 0, $pos);
 577          }
 578      }
 579  
 580      return false;
 581  }
 582  
 583  function fgetd_seekless(&$fp, $delim, $read, $seek, $eof, $buffer = 8192)
 584  {
 585      static $array = array();
 586      static $record = '';
 587  
 588      if (!count($array))
 589      {
 590          while (!$eof($fp))
 591          {
 592              if (strpos($record, $delim) !== false)
 593              {
 594                  $array = explode($delim, $record);
 595                  $record = array_pop($array);
 596                  break;
 597              }
 598              else
 599              {
 600                  $record .= $read($fp, $buffer);
 601              }
 602          }
 603          if ($eof($fp) && strpos($record, $delim) !== false)
 604          {
 605              $array = explode($delim, $record);
 606              $record = array_pop($array);
 607          }
 608      }
 609  
 610      if (count($array))
 611      {
 612          return array_shift($array);
 613      }
 614  
 615      return false;
 616  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1