[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

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

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


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1