[ Index ]

PHP Cross Reference of MyBB 1.6.5

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: backupdb.php 5409 2011-03-20 02:15:47Z jammerx2 $
  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($mybb->input['analyzeoptimize'] == 1)
 192              {
 193                  $db->optimize_table($table);
 194                  $db->analyze_table($table);
 195              }
 196              
 197              $field_list = array();
 198              $fields_array = $db->show_fields_from($table);
 199              foreach($fields_array as $field)
 200              {
 201                  $field_list[] = $field['Field'];
 202              }
 203              
 204              $fields = "`".implode("`,`", $field_list)."`";
 205              if($mybb->input['contents'] != 'data')
 206              {
 207                  $structure = $db->show_create_table($table).";\n";
 208                  $contents .= $structure;
 209                  clear_overflow($fp, $contents);
 210              }
 211              
 212              if($mybb->input['contents'] != 'structure')
 213              {
 214                  $query = $db->simple_select($table);
 215                  while($row = $db->fetch_array($query))
 216                  {
 217                      $insert = "INSERT INTO {$table} ($fields) VALUES (";
 218                      $comma = '';
 219                      foreach($field_list as $field)
 220                      {
 221                          if(!isset($row[$field]) || is_null($row[$field]))
 222                          {
 223                              $insert .= $comma."NULL";
 224                          }
 225                          else
 226                          {
 227                              $insert .= $comma."'".$db->escape_string($row[$field])."'";
 228                          }
 229                          $comma = ',';
 230                      }
 231                      $insert .= ");\n";
 232                      $contents .= $insert;
 233                      clear_overflow($fp, $contents);
 234                  }
 235              }
 236          }
 237          
 238          $db->set_table_prefix(TABLE_PREFIX);
 239  
 240          if($mybb->input['method'] == 'disk')
 241          {
 242              if($mybb->input['filetype'] == 'gzip')
 243              {
 244                  gzwrite($fp, $contents);
 245                  gzclose($fp);
 246              }
 247              else
 248              {
 249                  fwrite($fp, $contents);
 250                  fclose($fp);
 251              }
 252              
 253              if($mybb->input['filetype'] == 'gzip')
 254              {
 255                  $ext = '.sql.gz';
 256              }
 257              else
 258              {
 259                  $ext = '.sql';
 260              }
 261              
 262              $plugins->run_hooks("admin_tools_backupdb_backup_disk_commit");
 263              
 264              // Log admin action
 265              log_admin_action("disk", $file.$ext);
 266  
 267              $file_from_admindir = 'index.php?module=tools-backupdb&amp;action=dlbackup&amp;file='.basename($file).$ext;
 268              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');
 269              admin_redirect("index.php?module=tools-backupdb");
 270          }
 271          else
 272          {
 273              $plugins->run_hooks("admin_tools_backupdb_backup_download_commit");
 274              
 275              // Log admin action
 276              log_admin_action("download");
 277  
 278              if($mybb->input['filetype'] == 'gzip')
 279              {
 280                  echo gzencode($contents);
 281              }
 282              else
 283              {
 284                  echo $contents;
 285              }
 286          }
 287          
 288          exit;
 289      }
 290      
 291      $page->extra_header = "    <script type=\"text/javascript\">
 292  	function changeSelection(action, prefix)
 293      {
 294          var select_box = document.getElementById('table_select');
 295          
 296          for(var i = 0; i < select_box.length; i++)
 297          {
 298              if(action == 'select')
 299              {
 300                  select_box[i].selected = true;
 301              }
 302              else if(action == 'deselect')
 303              {
 304                  select_box[i].selected = false;
 305              }
 306              else if(action == 'forum' && prefix != 0)
 307              {
 308                  select_box[i].selected = false;
 309                  var row = select_box[i].value;
 310                  var subString = row.substring(prefix.length, 0);
 311                  if(subString == prefix)
 312                  {
 313                      select_box[i].selected = true;
 314                  }
 315              }
 316          }
 317      }
 318      </script>\n";
 319      
 320      $page->add_breadcrumb_item($lang->new_database_backup);
 321      $page->output_header($lang->new_database_backup);
 322      
 323      $sub_tabs['database_backup'] = array(
 324          'title' => $lang->database_backups,
 325          'link' => "index.php?module=tools-backupdb"
 326      );
 327      
 328      $sub_tabs['new_backup'] = array(
 329          'title' => $lang->new_backup,
 330          'link' => "index.php?module=tools-backupdb&amp;action=backup",
 331          'description' => $lang->new_backup_desc
 332      );
 333      
 334      $page->output_nav_tabs($sub_tabs, 'new_backup');
 335      
 336      // Check if file is writable, before allowing submission
 337      if(!is_writable(MYBB_ADMIN_DIR."/backups"))
 338      {
 339          $lang->update_button = '';
 340          $page->output_alert($lang->alert_not_writable);
 341          $cannot_write = true;
 342      }
 343      
 344      $table = new Table;
 345      $table->construct_header($lang->table_selection);
 346      $table->construct_header($lang->backup_options);
 347      
 348      $table_selects = array();
 349      $table_list = $db->list_tables($config['database']['database']);
 350      foreach($table_list as $id => $table_name)
 351      {
 352          $table_selects[$table_name] = $table_name;
 353      }
 354      
 355      $form = new Form("index.php?module=tools-backupdb&amp;action=backup", "post", "table_selection", 0, "table_selection");
 356      
 357      $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%'));
 358      $table->construct_row();
 359      
 360      $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%'));
 361      $table->construct_row();
 362      $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%'));
 363      $table->construct_row();
 364      $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%'));
 365      $table->construct_row();
 366      $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%'));
 367      $table->construct_row();
 368          
 369      $table->output($lang->new_database_backup);
 370      
 371      $buttons[] = $form->generate_submit_button($lang->perform_backup);
 372      $form->output_submit_wrapper($buttons);
 373      
 374      $form->end();
 375          
 376      $page->output_footer();
 377  }
 378  
 379  if(!$mybb->input['action'])
 380  {
 381      $plugins->run_hooks("admin_tools_backupdb_start");
 382      
 383      $page->add_breadcrumb_item($lang->backups);
 384      $page->output_header($lang->database_backups);
 385      
 386      $sub_tabs['database_backup'] = array(
 387          'title' => $lang->database_backups,
 388          'link' => "index.php?module=tools-backupdb",
 389          'description' => $lang->database_backups_desc
 390      );
 391      
 392      $sub_tabs['new_backup'] = array(
 393          'title' => $lang->new_backup,
 394          'link' => "index.php?module=tools-backupdb&amp;action=backup",
 395      );
 396      
 397      $page->output_nav_tabs($sub_tabs, 'database_backup');
 398      
 399      $backups = array();
 400      $dir = MYBB_ADMIN_DIR.'backups/';
 401      $handle = opendir($dir);
 402      while(($file = readdir($handle)) !== false)
 403      {
 404          if(filetype(MYBB_ADMIN_DIR.'backups/'.$file) == 'file')
 405          {
 406              $ext = get_extension($file);
 407              if($ext == 'gz' || $ext == 'sql')
 408              {
 409                  $backups[@filemtime(MYBB_ADMIN_DIR.'backups/'.$file)] = array(
 410                      "file" => $file,
 411                      "time" => @filemtime(MYBB_ADMIN_DIR.'backups/'.$file),
 412                      "type" => $ext
 413                  );
 414              }
 415          }
 416      }
 417      
 418      $count = count($backups);
 419      krsort($backups);
 420      
 421      $table = new Table;
 422      $table->construct_header($lang->backup_filename);
 423      $table->construct_header($lang->file_size, array("class" => "align_center"));
 424      $table->construct_header($lang->creation_date);
 425      $table->construct_header($lang->controls, array("class" => "align_center"));
 426      
 427      foreach($backups as $backup)
 428      {
 429          if($backup['time'])
 430          {
 431              $time = my_date($mybb->settings['dateformat'].", ".$mybb->settings['timeformat'], $backup['time']);
 432          }
 433          else
 434          {
 435              $time = "-";
 436          }
 437          
 438          $table->construct_cell("<a href=\"index.php?module=tools-backupdb&amp;action=dlbackup&amp;file={$backup['file']}\">{$backup['file']}</a>");
 439          $table->construct_cell(get_friendly_size(filesize(MYBB_ADMIN_DIR.'backups/'.$backup['file'])), array("class" => "align_center"));
 440          $table->construct_cell($time);
 441          $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"));
 442          $table->construct_row();
 443      }
 444      
 445      if($count == 0)
 446      {
 447          $table->construct_cell($lang->no_backups, array('colspan' => 4));
 448          $table->construct_row();
 449      }
 450      
 451      
 452      $table->output($lang->existing_database_backups);
 453          
 454      $page->output_footer();
 455  }
 456  
 457  ?>


Generated: Sun Dec 11 14:16:27 2011 Cross-referenced by PHPXref 0.7.1