[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/inc/tasks/ -> 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  function task_backupdb($task)
  12  {
  13      global $db, $config, $lang, $plugins;
  14      static $contents;
  15  
  16      @set_time_limit(0);
  17  
  18      if(!defined('MYBB_ADMIN_DIR'))
  19      {
  20          if(!isset($config['admin_dir']))
  21          {
  22              $config['admin_dir'] = "admin";
  23          }
  24  
  25          define('MYBB_ADMIN_DIR', MYBB_ROOT.$config['admin_dir'].'/');
  26      }
  27  
  28      // Check if folder is writable, before allowing submission
  29      if(!is_writable(MYBB_ADMIN_DIR."/backups"))
  30      {
  31          add_task_log($task, $lang->task_backup_cannot_write_backup);
  32      }
  33      else
  34      {
  35          $db->set_table_prefix('');
  36  
  37          $file = MYBB_ADMIN_DIR.'backups/backup_'.date("_Ymd_His_").random_str(16);
  38  
  39          if(function_exists('gzopen'))
  40          {
  41              $fp = gzopen($file.'.incomplete.sql.gz', 'w9');
  42          }
  43          else
  44          {
  45              $fp = fopen($file.'.incomplete.sql', 'w');
  46          }
  47  
  48          $tables = $db->list_tables($config['database']['database'], $config['database']['table_prefix']);
  49  
  50          $time = date('dS F Y \a\t H:i', TIME_NOW);
  51          $contents = "-- MyBB Database Backup\n-- Generated: {$time}\n-- -------------------------------------\n\n";
  52  
  53          if(is_object($plugins))
  54          {
  55              $args = array(
  56                  'task' =>  &$task,
  57                  'tables' =>  &$tables,
  58              );
  59              $plugins->run_hooks('task_backupdb', $args);
  60          }
  61  
  62          foreach($tables as $table)
  63          {
  64              $field_list = array();
  65              $fields_array = $db->show_fields_from($table);
  66              foreach($fields_array as $field)
  67              {
  68                  $field_list[] = $field['Field'];
  69              }
  70  
  71              $fields = "`".implode("`,`", $field_list)."`";
  72  
  73              $structure = $db->show_create_table($table).";\n";
  74              $contents .= $structure;
  75              clear_overflow($fp, $contents);
  76  
  77              if($db->engine == 'mysqli')
  78              {
  79                  $query = mysqli_query($db->read_link, "SELECT * FROM {$db->table_prefix}{$table}", MYSQLI_USE_RESULT);
  80              }
  81              else
  82              {
  83                  $query = $db->simple_select($table);
  84              }
  85  
  86              while($row = $db->fetch_array($query))
  87              {
  88                  $insert = "INSERT INTO {$table} ($fields) VALUES (";
  89                  $comma = '';
  90                  foreach($field_list as $field)
  91                  {
  92                      if(!isset($row[$field]) || is_null($row[$field]))
  93                      {
  94                          $insert .= $comma."NULL";
  95                      }
  96                      else if($db->engine == 'mysqli')
  97                      {
  98                          $insert .= $comma."'".mysqli_real_escape_string($db->read_link, $row[$field])."'";
  99                      }
 100                      else
 101                      {
 102                          $insert .= $comma."'".$db->escape_string($row[$field])."'";
 103                      }
 104                      $comma = ',';
 105                  }
 106                  $insert .= ");\n";
 107                  $contents .= $insert;
 108                  clear_overflow($fp, $contents);
 109              }
 110              $db->free_result($query);
 111          }
 112  
 113          $db->set_table_prefix(TABLE_PREFIX);
 114  
 115          if(function_exists('gzopen'))
 116          {
 117              gzwrite($fp, $contents);
 118              gzclose($fp);
 119              rename($file.'.incomplete.sql.gz', $file.'.sql.gz');
 120          }
 121          else
 122          {
 123              fwrite($fp, $contents);
 124              fclose($fp);
 125              rename($file.'.incomplete.sql', $file.'.sql');
 126          }
 127  
 128          add_task_log($task, $lang->task_backup_ran);
 129      }
 130  }
 131  
 132  // Allows us to refresh cache to prevent over flowing
 133  function clear_overflow($fp, &$contents)
 134  {
 135      global $mybb;
 136  
 137      if(function_exists('gzopen'))
 138      {
 139          gzwrite($fp, $contents);
 140      }
 141      else
 142      {
 143          fwrite($fp, $contents);
 144      }
 145  
 146      $contents = '';
 147  }


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