[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

/admin/modules/tools/ -> backupdb.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  // Disallow direct access to this file for security reasons
  12  if(!defined("IN_MYBB"))
  13  {
  14      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  15  }
  16  
  17  /**
  18   * Allows us to refresh cache to prevent over flowing
  19   *
  20   * @param resource $fp
  21   * @param string $contents
  22   */
  23  function clear_overflow($fp, &$contents)
  24  {
  25      global $mybb;
  26  
  27      if($mybb->input['method'] == 'disk')
  28      {
  29          if($mybb->input['filetype'] == 'gzip')
  30          {
  31              gzwrite($fp, $contents);
  32          }
  33          else
  34          {
  35              fwrite($fp, $contents);
  36          }
  37      }
  38      else
  39      {
  40          if($mybb->input['filetype'] == "gzip")
  41          {
  42              echo gzencode($contents);
  43          }
  44          else
  45          {
  46              echo $contents;
  47          }
  48      }
  49  
  50      $contents = '';
  51  }
  52  
  53  $page->add_breadcrumb_item($lang->database_backups, "index.php?module=tools-backupdb");
  54  
  55  $plugins->run_hooks("admin_tools_backupdb_begin");
  56  
  57  if($mybb->input['action'] == "dlbackup")
  58  {
  59      if(empty($mybb->input['file']))
  60      {
  61          flash_message($lang->error_file_not_specified, 'error');
  62          admin_redirect("index.php?module=tools-backupdb");
  63      }
  64  
  65      $plugins->run_hooks("admin_tools_backupdb_dlbackup");
  66  
  67      $file = basename($mybb->input['file']);
  68      $ext = get_extension($file);
  69  
  70      if(file_exists(MYBB_ADMIN_DIR.'backups/'.$file) && filetype(MYBB_ADMIN_DIR.'backups/'.$file) == 'file' && ($ext == 'gz' || $ext == 'sql'))
  71      {
  72          $plugins->run_hooks("admin_tools_backupdb_dlbackup_commit");
  73  
  74          // Log admin action
  75          log_admin_action($file);
  76  
  77          header('Content-disposition: attachment; filename='.$file);
  78          header("Content-type: ".$ext);
  79          header("Content-length: ".filesize(MYBB_ADMIN_DIR.'backups/'.$file));
  80  
  81          $handle = fopen(MYBB_ADMIN_DIR.'backups/'.$file, 'rb');
  82          while(!feof($handle))
  83          {
  84              echo fread($handle, 8192);
  85          }
  86          fclose($handle);
  87      }
  88      else
  89      {
  90          flash_message($lang->error_invalid_backup, 'error');
  91          admin_redirect("index.php?module=tools-backupdb");
  92      }
  93  }
  94  
  95  if($mybb->input['action'] == "delete")
  96  {
  97      if($mybb->get_input('no'))
  98      {
  99          admin_redirect("index.php?module=tools-backupdb");
 100      }
 101  
 102      $file = basename($mybb->input['file']);
 103  
 104      if(!trim($mybb->input['file']) || !file_exists(MYBB_ADMIN_DIR.'backups/'.$file))
 105      {
 106          flash_message($lang->error_backup_doesnt_exist, 'error');
 107          admin_redirect("index.php?module=tools-backupdb");
 108      }
 109  
 110      $plugins->run_hooks("admin_tools_backupdb_delete");
 111  
 112      if($mybb->request_method == "post")
 113      {
 114          $delete = @unlink(MYBB_ADMIN_DIR.'backups/'.$file);
 115  
 116          if($delete)
 117          {
 118              $plugins->run_hooks("admin_tools_backupdb_delete_commit");
 119  
 120              // Log admin action
 121              log_admin_action($file);
 122  
 123              flash_message($lang->success_backup_deleted, 'success');
 124              admin_redirect("index.php?module=tools-backupdb");
 125          }
 126          else
 127          {
 128              flash_message($lang->error_backup_not_deleted, 'error');
 129              admin_redirect("index.php?module=tools-backupdb");
 130          }
 131      }
 132      else
 133      {
 134          $page->output_confirm_action("index.php?module=tools-backupdb&amp;action=delete&amp;file={$mybb->input['file']}", $lang->confirm_backup_deletion);
 135      }
 136  }
 137  
 138  if($mybb->input['action'] == "backup")
 139  {
 140      $plugins->run_hooks("admin_tools_backupdb_backup");
 141  
 142      if($mybb->request_method == "post")
 143      {
 144          if(empty($mybb->input['tables']) || !is_array($mybb->input['tables']))
 145          {
 146              flash_message($lang->error_tables_not_selected, 'error');
 147              admin_redirect("index.php?module=tools-backupdb&action=backup");
 148          }
 149  
 150          @set_time_limit(0);
 151  
 152          // create an array with table prefix appended for checks, as full table names are accepted
 153          $binary_fields_prefixed = array();
 154          foreach($mybb->binary_fields as $table => $fields)
 155          {
 156              $binary_fields_prefixed[TABLE_PREFIX.$table] = $fields;
 157          }
 158  
 159          if($mybb->input['method'] == 'disk')
 160          {
 161              $file = MYBB_ADMIN_DIR.'backups/backup_'.date("_Ymd_His_").random_str(16);
 162  
 163              if($mybb->input['filetype'] == 'gzip')
 164              {
 165                  if(!function_exists('gzopen')) // check zlib-ness
 166                  {
 167                      flash_message($lang->error_no_zlib, 'error');
 168                      admin_redirect("index.php?module=tools-backupdb&action=backup");
 169                  }
 170  
 171                  $fp = gzopen($file.'.incomplete.sql.gz', 'w9');
 172              }
 173              else
 174              {
 175                  $fp = fopen($file.'.incomplete.sql', 'w');
 176              }
 177          }
 178          else
 179          {
 180              $file = 'backup_'.substr(md5($mybb->user['uid'].TIME_NOW), 0, 10).random_str(54);
 181              if($mybb->input['filetype'] == 'gzip')
 182              {
 183                  if(!function_exists('gzopen')) // check zlib-ness
 184                  {
 185                      flash_message($lang->error_no_zlib, 'error');
 186                      admin_redirect("index.php?module=tools-backupdb&action=backup");
 187                  }
 188  
 189                  // Send headers for gzip file
 190                  header('Content-Type: application/x-gzip');
 191                  header('Content-Disposition: attachment; filename="'.$file.'.sql.gz"');
 192              }
 193              else
 194              {
 195                  // Send standard headers for .sql
 196                  header('Content-Type: text/x-sql');
 197                  header('Content-Disposition: attachment; filename="'.$file.'.sql"');
 198              }
 199          }
 200          $db->set_table_prefix('');
 201  
 202          $time = date('dS F Y \a\t H:i', TIME_NOW);
 203          $header = "-- MyBB Database Backup\n-- Generated: {$time}\n-- -------------------------------------\n\n";
 204          $contents = $header;
 205          foreach($mybb->input['tables'] as $table)
 206          {
 207              if(!$db->table_exists($db->escape_string($table)))
 208              {
 209                  continue;
 210              }
 211              if($mybb->input['analyzeoptimize'] == 1)
 212              {
 213                  $db->optimize_table($table);
 214                  $db->analyze_table($table);
 215              }
 216  
 217              $field_list = array();
 218              $fields_array = $db->show_fields_from($table);
 219              foreach($fields_array as $field)
 220              {
 221                  $field_list[] = $field['Field'];
 222              }
 223  
 224              $fields = "`".implode("`,`", $field_list)."`";
 225              if($mybb->input['contents'] != 'data')
 226              {
 227                  $structure = $db->show_create_table($table).";\n";
 228                  $contents .= $structure;
 229  
 230                  if(isset($fp))
 231                  {
 232                      clear_overflow($fp, $contents);
 233                  }
 234              }
 235  
 236              if($mybb->input['contents'] != 'structure')
 237              {
 238                  if($db->engine == 'mysqli')
 239                  {
 240                      $query = mysqli_query($db->read_link, "SELECT * FROM {$db->table_prefix}{$table}", MYSQLI_USE_RESULT);
 241                  }
 242                  else
 243                  {
 244                      $query = $db->simple_select($table);
 245                  }
 246  
 247                  while($row = $db->fetch_array($query))
 248                  {
 249                      $insert = "INSERT INTO {$table} ($fields) VALUES (";
 250                      $comma = '';
 251                      foreach($field_list as $field)
 252                      {
 253                          if(!isset($row[$field]) || is_null($row[$field]))
 254                          {
 255                              $insert .= $comma."NULL";
 256                          }
 257                          else
 258                          {
 259                              if($db->engine == 'mysqli')
 260                              {
 261                                  if(!empty($binary_fields_prefixed[$table][$field]))
 262                                  {
 263                                      $insert .= $comma."X'".mysqli_real_escape_string($db->read_link, bin2hex($row[$field]))."'";
 264                                  }
 265                                  else
 266                                  {
 267                                      $insert .= $comma."'".mysqli_real_escape_string($db->read_link, $row[$field])."'";
 268                                  }
 269                              }
 270                              else
 271                              {
 272                                  if(!empty($binary_fields_prefixed[$table][$field]))
 273                                  {
 274                                      $insert .= $comma.$db->escape_binary($db->unescape_binary($row[$field]));
 275                                  }
 276                                  else
 277                                  {
 278                                      $insert .= $comma."'".$db->escape_string($row[$field])."'";
 279                                  }
 280                              }
 281                          }
 282                          $comma = ',';
 283                      }
 284                      $insert .= ");\n";
 285                      $contents .= $insert;
 286  
 287                      if(isset($fp))
 288                      {
 289                          clear_overflow($fp, $contents);
 290                      }
 291                  }
 292                  $db->free_result($query);
 293              }
 294          }
 295  
 296          $db->set_table_prefix(TABLE_PREFIX);
 297  
 298          if($mybb->input['method'] == 'disk')
 299          {
 300              if($mybb->input['filetype'] == 'gzip')
 301              {
 302                  gzwrite($fp, $contents);
 303                  gzclose($fp);
 304                  rename($file.'.incomplete.sql.gz', $file.'.sql.gz');
 305              }
 306              else
 307              {
 308                  fwrite($fp, $contents);
 309                  fclose($fp);
 310                  rename($file.'.incomplete.sql', $file.'.sql');
 311              }
 312  
 313              if($mybb->input['filetype'] == 'gzip')
 314              {
 315                  $ext = '.sql.gz';
 316              }
 317              else
 318              {
 319                  $ext = '.sql';
 320              }
 321  
 322              $plugins->run_hooks("admin_tools_backupdb_backup_disk_commit");
 323  
 324              // Log admin action
 325              log_admin_action("disk", $file.$ext);
 326  
 327              $file_from_admindir = 'index.php?module=tools-backupdb&amp;action=dlbackup&amp;file='.basename($file).$ext;
 328              flash_message("<span><em>{$lang->success_backup_created}</em></span><p>{$lang->backup_saved_to}<br />{$file}{$ext} (<a href=\"{$file_from_admindir}\">{$lang->download}</a>)</p>", 'success');
 329              admin_redirect("index.php?module=tools-backupdb");
 330          }
 331          else
 332          {
 333              $plugins->run_hooks("admin_tools_backupdb_backup_download_commit");
 334  
 335              // Log admin action
 336              log_admin_action("download");
 337  
 338              if($mybb->input['filetype'] == 'gzip')
 339              {
 340                  echo gzencode($contents);
 341              }
 342              else
 343              {
 344                  echo $contents;
 345              }
 346          }
 347  
 348          exit;
 349      }
 350  
 351      $page->extra_header = "    <script type=\"text/javascript\">
 352  	function changeSelection(action, prefix)
 353      {
 354          var select_box = document.getElementById('table_select');
 355  
 356          for(var i = 0; i < select_box.length; i++)
 357          {
 358              if(action == 'select')
 359              {
 360                  select_box[i].selected = true;
 361              }
 362              else if(action == 'deselect')
 363              {
 364                  select_box[i].selected = false;
 365              }
 366              else if(action == 'forum' && prefix != 0)
 367              {
 368                  select_box[i].selected = false;
 369                  var row = select_box[i].value;
 370                  var subString = row.substring(prefix.length, 0);
 371                  if(subString == prefix)
 372                  {
 373                      select_box[i].selected = true;
 374                  }
 375              }
 376          }
 377      }
 378      </script>\n";
 379  
 380      $page->add_breadcrumb_item($lang->new_database_backup);
 381      $page->output_header($lang->new_database_backup);
 382  
 383      $sub_tabs['database_backup'] = array(
 384          'title' => $lang->database_backups,
 385          'link' => "index.php?module=tools-backupdb"
 386      );
 387  
 388      $sub_tabs['new_backup'] = array(
 389          'title' => $lang->new_backup,
 390          'link' => "index.php?module=tools-backupdb&amp;action=backup",
 391          'description' => $lang->new_backup_desc
 392      );
 393  
 394      $page->output_nav_tabs($sub_tabs, 'new_backup');
 395  
 396      // Check if file is writable, before allowing submission
 397      if(!is_writable(MYBB_ADMIN_DIR."/backups"))
 398      {
 399          $lang->update_button = '';
 400          $page->output_alert($lang->alert_not_writable);
 401          $cannot_write = true;
 402      }
 403  
 404      $table = new Table;
 405      $table->construct_header($lang->table_selection);
 406      $table->construct_header($lang->backup_options);
 407  
 408      $table_selects = array();
 409      $table_list = $db->list_tables($config['database']['database']);
 410      foreach($table_list as $id => $table_name)
 411      {
 412          $table_selects[$table_name] = $table_name;
 413      }
 414  
 415      $form = new Form("index.php?module=tools-backupdb&amp;action=backup", "post", "table_selection", 0, "table_selection");
 416  
 417      $table->construct_cell("{$lang->table_select_desc}\n<br /><br />\n<a href=\"javascript:changeSelection('select', 0);\">{$lang->select_all}</a><br />\n<a href=\"javascript:changeSelection('deselect', 0);\">{$lang->deselect_all}</a><br />\n<a href=\"javascript:changeSelection('forum', '".TABLE_PREFIX."');\">{$lang->select_forum_tables}</a>\n<br /><br />\n<div class=\"form_row\">".$form->generate_select_box("tables[]", $table_selects, false, array('multiple' => true, 'id' => 'table_select', 'size' => 20))."</div>", array('rowspan' => 5, 'width' => '50%', 'style' => 'border-bottom: 0px'));
 418      $table->construct_row();
 419  
 420      $table->construct_cell("<strong>{$lang->file_type}</strong><br />\n{$lang->file_type_desc}<br />\n<div class=\"form_row\">".$form->generate_radio_button("filetype", "gzip", $lang->gzip_compressed, array('checked' => 1))."<br />\n".$form->generate_radio_button("filetype", "plain", $lang->plain_text)."</div>", array('width' => '50%'));
 421      $table->construct_row();
 422      $table->construct_cell("<strong>{$lang->save_method}</strong><br />\n{$lang->save_method_desc}<br /><div class=\"form_row\">".$form->generate_radio_button("method", "disk", $lang->backup_directory)."<br />\n".$form->generate_radio_button("method", "download", $lang->download, array('checked' => 1))."</div>", array('width' => '50%'));
 423      $table->construct_row();
 424      $table->construct_cell("<strong>{$lang->backup_contents}</strong><br />\n{$lang->backup_contents_desc}<br /><div class=\"form_row\">".$form->generate_radio_button("contents", "both", $lang->structure_and_data, array('checked' => 1))."<br />\n".$form->generate_radio_button("contents", "structure", $lang->structure_only)."<br />\n".$form->generate_radio_button("contents", "data", $lang->data_only)."</div>", array('width' => '50%'));
 425      $table->construct_row();
 426      $table->construct_cell("<strong>{$lang->analyze_and_optimize}</strong><br />\n{$lang->analyze_and_optimize_desc}<br /><div class=\"form_row\">".$form->generate_yes_no_radio("analyzeoptimize")."</div>", array('width' => '50%'));
 427      $table->construct_row();
 428  
 429      $table->output($lang->new_database_backup);
 430  
 431      $buttons[] = $form->generate_submit_button($lang->perform_backup);
 432      $form->output_submit_wrapper($buttons);
 433  
 434      $form->end();
 435  
 436      $page->output_footer();
 437  }
 438  
 439  if(!$mybb->input['action'])
 440  {
 441      $page->add_breadcrumb_item($lang->backups);
 442      $page->output_header($lang->database_backups);
 443  
 444      $sub_tabs['database_backup'] = array(
 445          'title' => $lang->database_backups,
 446          'link' => "index.php?module=tools-backupdb",
 447          'description' => $lang->database_backups_desc
 448      );
 449  
 450      $sub_tabs['new_backup'] = array(
 451          'title' => $lang->new_backup,
 452          'link' => "index.php?module=tools-backupdb&amp;action=backup",
 453      );
 454  
 455      $plugins->run_hooks("admin_tools_backupdb_start");
 456  
 457      $page->output_nav_tabs($sub_tabs, 'database_backup');
 458  
 459      $backups = array();
 460      $dir = MYBB_ADMIN_DIR.'backups/';
 461      $handle = opendir($dir);
 462  
 463      if($handle !== false)
 464      {
 465          while(($file = readdir($handle)) !== false)
 466          {
 467              if(filetype(MYBB_ADMIN_DIR.'backups/'.$file) == 'file')
 468              {
 469                  $ext = get_extension($file);
 470                  if($ext == 'gz' || $ext == 'sql')
 471                  {
 472                      $backups[@filemtime(MYBB_ADMIN_DIR.'backups/'.$file)] = array(
 473                          "file" => $file,
 474                          "time" => @filemtime(MYBB_ADMIN_DIR.'backups/'.$file),
 475                          "type" => $ext
 476                      );
 477                  }
 478              }
 479          }
 480          closedir($handle);
 481      }
 482  
 483      $count = count($backups);
 484      krsort($backups);
 485  
 486      $table = new Table;
 487      $table->construct_header($lang->backup_filename);
 488      $table->construct_header($lang->file_size, array("class" => "align_center"));
 489      $table->construct_header($lang->creation_date);
 490      $table->construct_header($lang->controls, array("class" => "align_center"));
 491  
 492      foreach($backups as $backup)
 493      {
 494          $time = "-";
 495          if($backup['time'])
 496          {
 497              $time = my_date('relative', $backup['time']);
 498          }
 499  
 500          $table->construct_cell("<a href=\"index.php?module=tools-backupdb&amp;action=dlbackup&amp;file={$backup['file']}\">{$backup['file']}</a>");
 501          $table->construct_cell(get_friendly_size(filesize(MYBB_ADMIN_DIR.'backups/'.$backup['file'])), array("class" => "align_center"));
 502          $table->construct_cell($time);
 503          $table->construct_cell("<a href=\"index.php?module=tools-backupdb&amp;action=backup&amp;action=delete&amp;file={$backup['file']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->confirm_backup_deletion}')\">{$lang->delete}</a>", array("class" => "align_center"));
 504          $table->construct_row();
 505      }
 506  
 507      if($count == 0)
 508      {
 509          $table->construct_cell($lang->no_backups, array('colspan' => 4));
 510          $table->construct_row();
 511      }
 512  
 513      $table->output($lang->existing_database_backups);
 514      $page->output_footer();
 515  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref