[ Index ]

PHP Cross Reference of MyBB 1.8.38

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      $ext = get_extension($file);
 104  
 105      if(!trim($mybb->input['file']) || !file_exists(MYBB_ADMIN_DIR.'backups/'.$file) || filetype(MYBB_ADMIN_DIR.'backups/'.$file) != 'file' || ($ext != 'gz' && $ext != 'sql'))
 106      {
 107          flash_message($lang->error_backup_doesnt_exist, 'error');
 108          admin_redirect("index.php?module=tools-backupdb");
 109      }
 110  
 111      $plugins->run_hooks("admin_tools_backupdb_delete");
 112  
 113      if($mybb->request_method == "post")
 114      {
 115          $delete = @unlink(MYBB_ADMIN_DIR.'backups/'.$file);
 116  
 117          if($delete)
 118          {
 119              $plugins->run_hooks("admin_tools_backupdb_delete_commit");
 120  
 121              // Log admin action
 122              log_admin_action($file);
 123  
 124              flash_message($lang->success_backup_deleted, 'success');
 125              admin_redirect("index.php?module=tools-backupdb");
 126          }
 127          else
 128          {
 129              flash_message($lang->error_backup_not_deleted, 'error');
 130              admin_redirect("index.php?module=tools-backupdb");
 131          }
 132      }
 133      else
 134      {
 135          $page->output_confirm_action("index.php?module=tools-backupdb&amp;action=delete&amp;file={$mybb->input['file']}", $lang->confirm_backup_deletion);
 136      }
 137  }
 138  
 139  if($mybb->input['action'] == "backup")
 140  {
 141      $plugins->run_hooks("admin_tools_backupdb_backup");
 142  
 143      if($mybb->request_method == "post")
 144      {
 145          if(empty($mybb->input['tables']) || !is_array($mybb->input['tables']))
 146          {
 147              flash_message($lang->error_tables_not_selected, 'error');
 148              admin_redirect("index.php?module=tools-backupdb&action=backup");
 149          }
 150  
 151          @set_time_limit(0);
 152  
 153          // create an array with table prefix appended for checks, as full table names are accepted
 154          $binary_fields_prefixed = array();
 155          foreach($mybb->binary_fields as $table => $fields)
 156          {
 157              $binary_fields_prefixed[TABLE_PREFIX.$table] = $fields;
 158          }
 159  
 160          if($mybb->input['method'] == 'disk')
 161          {
 162              $file = MYBB_ADMIN_DIR.'backups/backup_'.date("_Ymd_His_").random_str(16);
 163  
 164              if($mybb->input['filetype'] == 'gzip')
 165              {
 166                  if(!function_exists('gzopen')) // check zlib-ness
 167                  {
 168                      flash_message($lang->error_no_zlib, 'error');
 169                      admin_redirect("index.php?module=tools-backupdb&action=backup");
 170                  }
 171  
 172                  $fp = gzopen($file.'.incomplete.sql.gz', 'w9');
 173              }
 174              else
 175              {
 176                  $fp = fopen($file.'.incomplete.sql', 'w');
 177              }
 178          }
 179          else
 180          {
 181              $file = 'backup_'.substr(md5($mybb->user['uid'].TIME_NOW), 0, 10).random_str(54);
 182              if($mybb->input['filetype'] == 'gzip')
 183              {
 184                  if(!function_exists('gzopen')) // check zlib-ness
 185                  {
 186                      flash_message($lang->error_no_zlib, 'error');
 187                      admin_redirect("index.php?module=tools-backupdb&action=backup");
 188                  }
 189  
 190                  // Send headers for gzip file
 191                  header('Content-Type: application/x-gzip');
 192                  header('Content-Disposition: attachment; filename="'.$file.'.sql.gz"');
 193              }
 194              else
 195              {
 196                  // Send standard headers for .sql
 197                  header('Content-Type: text/x-sql');
 198                  header('Content-Disposition: attachment; filename="'.$file.'.sql"');
 199              }
 200          }
 201          $db->set_table_prefix('');
 202  
 203          $time = date('dS F Y \a\t H:i', TIME_NOW);
 204          $header = "-- MyBB Database Backup\n-- Generated: {$time}\n-- -------------------------------------\n\n";
 205          $contents = $header;
 206          foreach($mybb->input['tables'] as $table)
 207          {
 208              if(!$db->table_exists($db->escape_string($table)))
 209              {
 210                  continue;
 211              }
 212              if($mybb->input['analyzeoptimize'] == 1)
 213              {
 214                  $db->optimize_table($table);
 215                  $db->analyze_table($table);
 216              }
 217  
 218              $field_list = array();
 219              $fields_array = $db->show_fields_from($table);
 220              foreach($fields_array as $field)
 221              {
 222                  $field_list[] = $field['Field'];
 223              }
 224  
 225              $fields = "`".implode("`,`", $field_list)."`";
 226              if($mybb->input['contents'] != 'data')
 227              {
 228                  $structure = $db->show_create_table($table).";\n";
 229                  $contents .= $structure;
 230  
 231                  if(isset($fp))
 232                  {
 233                      clear_overflow($fp, $contents);
 234                  }
 235              }
 236  
 237              if($mybb->input['contents'] != 'structure')
 238              {
 239                  if($db->engine == 'mysqli')
 240                  {
 241                      $query = mysqli_query($db->read_link, "SELECT * FROM {$db->table_prefix}{$table}", MYSQLI_USE_RESULT);
 242                  }
 243                  else
 244                  {
 245                      $query = $db->simple_select($table);
 246                  }
 247  
 248                  while($row = $db->fetch_array($query))
 249                  {
 250                      $insert = "INSERT INTO {$table} ($fields) VALUES (";
 251                      $comma = '';
 252                      foreach($field_list as $field)
 253                      {
 254                          if(!isset($row[$field]) || is_null($row[$field]))
 255                          {
 256                              $insert .= $comma."NULL";
 257                          }
 258                          else
 259                          {
 260                              if($db->engine == 'mysqli')
 261                              {
 262                                  if(!empty($binary_fields_prefixed[$table][$field]))
 263                                  {
 264                                      $insert .= $comma."X'".mysqli_real_escape_string($db->read_link, bin2hex($row[$field]))."'";
 265                                  }
 266                                  else
 267                                  {
 268                                      $insert .= $comma."'".mysqli_real_escape_string($db->read_link, $row[$field])."'";
 269                                  }
 270                              }
 271                              else
 272                              {
 273                                  if(!empty($binary_fields_prefixed[$table][$field]))
 274                                  {
 275                                      $insert .= $comma.$db->escape_binary($db->unescape_binary($row[$field]));
 276                                  }
 277                                  else
 278                                  {
 279                                      $insert .= $comma."'".$db->escape_string($row[$field])."'";
 280                                  }
 281                              }
 282                          }
 283                          $comma = ',';
 284                      }
 285                      $insert .= ");\n";
 286                      $contents .= $insert;
 287  
 288                      if(isset($fp))
 289                      {
 290                          clear_overflow($fp, $contents);
 291                      }
 292                  }
 293                  $db->free_result($query);
 294              }
 295          }
 296  
 297          $db->set_table_prefix(TABLE_PREFIX);
 298  
 299          if($mybb->input['method'] == 'disk')
 300          {
 301              if($mybb->input['filetype'] == 'gzip')
 302              {
 303                  gzwrite($fp, $contents);
 304                  gzclose($fp);
 305                  rename($file.'.incomplete.sql.gz', $file.'.sql.gz');
 306              }
 307              else
 308              {
 309                  fwrite($fp, $contents);
 310                  fclose($fp);
 311                  rename($file.'.incomplete.sql', $file.'.sql');
 312              }
 313  
 314              if($mybb->input['filetype'] == 'gzip')
 315              {
 316                  $ext = '.sql.gz';
 317              }
 318              else
 319              {
 320                  $ext = '.sql';
 321              }
 322  
 323              $plugins->run_hooks("admin_tools_backupdb_backup_disk_commit");
 324  
 325              // Log admin action
 326              log_admin_action("disk", $file.$ext);
 327  
 328              $file_from_admindir = 'index.php?module=tools-backupdb&amp;action=dlbackup&amp;file='.basename($file).$ext;
 329              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');
 330              admin_redirect("index.php?module=tools-backupdb");
 331          }
 332          else
 333          {
 334              $plugins->run_hooks("admin_tools_backupdb_backup_download_commit");
 335  
 336              // Log admin action
 337              log_admin_action("download");
 338  
 339              if($mybb->input['filetype'] == 'gzip')
 340              {
 341                  echo gzencode($contents);
 342              }
 343              else
 344              {
 345                  echo $contents;
 346              }
 347          }
 348  
 349          exit;
 350      }
 351  
 352      $page->extra_header = "    <script type=\"text/javascript\">
 353  	function changeSelection(action, prefix)
 354      {
 355          var select_box = document.getElementById('table_select');
 356  
 357          for(var i = 0; i < select_box.length; i++)
 358          {
 359              if(action == 'select')
 360              {
 361                  select_box[i].selected = true;
 362              }
 363              else if(action == 'deselect')
 364              {
 365                  select_box[i].selected = false;
 366              }
 367              else if(action == 'forum' && prefix != 0)
 368              {
 369                  select_box[i].selected = false;
 370                  var row = select_box[i].value;
 371                  var subString = row.substring(prefix.length, 0);
 372                  if(subString == prefix)
 373                  {
 374                      select_box[i].selected = true;
 375                  }
 376              }
 377          }
 378      }
 379      </script>\n";
 380  
 381      $page->add_breadcrumb_item($lang->new_database_backup);
 382      $page->output_header($lang->new_database_backup);
 383  
 384      $sub_tabs['database_backup'] = array(
 385          'title' => $lang->database_backups,
 386          'link' => "index.php?module=tools-backupdb"
 387      );
 388  
 389      $sub_tabs['new_backup'] = array(
 390          'title' => $lang->new_backup,
 391          'link' => "index.php?module=tools-backupdb&amp;action=backup",
 392          'description' => $lang->new_backup_desc
 393      );
 394  
 395      $page->output_nav_tabs($sub_tabs, 'new_backup');
 396  
 397      // Check if file is writable, before allowing submission
 398      if(!is_writable(MYBB_ADMIN_DIR."/backups"))
 399      {
 400          $lang->update_button = '';
 401          $page->output_alert($lang->alert_not_writable);
 402          $cannot_write = true;
 403      }
 404  
 405      $table = new Table;
 406      $table->construct_header($lang->table_selection);
 407      $table->construct_header($lang->backup_options);
 408  
 409      $table_selects = array();
 410      $table_list = $db->list_tables($config['database']['database']);
 411      foreach($table_list as $id => $table_name)
 412      {
 413          $table_selects[$table_name] = $table_name;
 414      }
 415  
 416      $form = new Form("index.php?module=tools-backupdb&amp;action=backup", "post", "table_selection", 0, "table_selection");
 417  
 418      $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'));
 419      $table->construct_row();
 420  
 421      $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%'));
 422      $table->construct_row();
 423      $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%'));
 424      $table->construct_row();
 425      $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%'));
 426      $table->construct_row();
 427      $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%'));
 428      $table->construct_row();
 429  
 430      $table->output($lang->new_database_backup);
 431  
 432      $buttons[] = $form->generate_submit_button($lang->perform_backup);
 433      $form->output_submit_wrapper($buttons);
 434  
 435      $form->end();
 436  
 437      $page->output_footer();
 438  }
 439  
 440  if(!$mybb->input['action'])
 441  {
 442      $page->add_breadcrumb_item($lang->backups);
 443      $page->output_header($lang->database_backups);
 444  
 445      $sub_tabs['database_backup'] = array(
 446          'title' => $lang->database_backups,
 447          'link' => "index.php?module=tools-backupdb",
 448          'description' => $lang->database_backups_desc
 449      );
 450  
 451      $sub_tabs['new_backup'] = array(
 452          'title' => $lang->new_backup,
 453          'link' => "index.php?module=tools-backupdb&amp;action=backup",
 454      );
 455  
 456      $plugins->run_hooks("admin_tools_backupdb_start");
 457  
 458      $page->output_nav_tabs($sub_tabs, 'database_backup');
 459  
 460      $backups = array();
 461      $dir = MYBB_ADMIN_DIR.'backups/';
 462      $handle = opendir($dir);
 463  
 464      if($handle !== false)
 465      {
 466          while(($file = readdir($handle)) !== false)
 467          {
 468              if(filetype(MYBB_ADMIN_DIR.'backups/'.$file) == 'file')
 469              {
 470                  $ext = get_extension($file);
 471                  if($ext == 'gz' || $ext == 'sql')
 472                  {
 473                      $backups[@filemtime(MYBB_ADMIN_DIR.'backups/'.$file)] = array(
 474                          "file" => $file,
 475                          "time" => @filemtime(MYBB_ADMIN_DIR.'backups/'.$file),
 476                          "type" => $ext
 477                      );
 478                  }
 479              }
 480          }
 481          closedir($handle);
 482      }
 483  
 484      $count = count($backups);
 485      krsort($backups);
 486  
 487      $table = new Table;
 488      $table->construct_header($lang->backup_filename);
 489      $table->construct_header($lang->file_size, array("class" => "align_center"));
 490      $table->construct_header($lang->creation_date);
 491      $table->construct_header($lang->controls, array("class" => "align_center"));
 492  
 493      foreach($backups as $backup)
 494      {
 495          $time = "-";
 496          if($backup['time'])
 497          {
 498              $time = my_date('relative', $backup['time']);
 499          }
 500  
 501          $table->construct_cell("<a href=\"index.php?module=tools-backupdb&amp;action=dlbackup&amp;file={$backup['file']}\">{$backup['file']}</a>");
 502          $table->construct_cell(get_friendly_size(filesize(MYBB_ADMIN_DIR.'backups/'.$backup['file'])), array("class" => "align_center"));
 503          $table->construct_cell($time);
 504          $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"));
 505          $table->construct_row();
 506      }
 507  
 508      if($count == 0)
 509      {
 510          $table->construct_cell($lang->no_backups, array('colspan' => 4));
 511          $table->construct_row();
 512      }
 513  
 514      $table->output($lang->existing_database_backups);
 515      $page->output_footer();
 516  }


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