123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- if (!defined('SMF'))
- die('Hacking attempt...');
- function DumpDatabase2()
- {
- global $db_name, $scripturl, $context, $modSettings, $crlf, $smcFunc, $db_prefix, $db_show_debug;
-
- if (!allowedTo('admin_forum'))
- fatal_lang_error('no_dump_database', 'critical');
-
- $modSettings['disableQueryCheck'] = true;
- $db_show_debug = false;
-
- if (!isset($_REQUEST['struct']) && !isset($_REQUEST['data']))
- $_REQUEST['data'] = true;
- checkSession('post');
-
- db_extend();
-
- @set_time_limit(600);
- $time_limit = ini_get('max_execution_time');
- $start_time = time();
-
- setMemoryLimit('256M');
- $memory_limit = memoryReturnBytes(ini_get('memory_limit')) / 4;
- $current_used_memory = 0;
- $db_backup = '';
- $output_function = 'un_compressed';
- @ob_end_clean();
-
- if (isset($_REQUEST['compress']) && function_exists('gzencode'))
- {
- $output_function = 'gzencode';
-
- header('Content-Type: application/x-gzip');
- header('Accept-Ranges: bytes');
- header('Content-Encoding: none');
-
- if (!isBrowser('gecko'))
- header('Content-Transfer-Encoding: binary');
-
- $extension = '.sql.gz';
- }
- else
- {
-
- if (!empty($modSettings['enableCompressedOutput']))
- @ob_end_clean();
-
- elseif (ob_get_length() != 0)
- ob_clean();
-
- header('Content-Type: ' . (isBrowser('ie') || isBrowser('opera') ? 'application/octetstream' : 'application/octet-stream'));
- header('Content-Encoding: none');
-
- $extension = '.sql';
- }
-
- $scripturl = '';
-
- if (!empty($smcFunc['db_get_backup']))
- {
- $smcFunc['db_get_backup']();
- exit;
- }
-
- header('Content-Disposition: attachment; filename="' . $db_name . '-' . (empty($_REQUEST['struct']) ? 'data' : (empty($_REQUEST['data']) ? 'structure' : 'complete')) . '_' . strftime('%Y-%m-%d') . $extension . '"');
- header('Cache-Control: private');
- header('Connection: close');
-
- $crlf = "\r\n";
-
- $db_chunks =
- '-- ==========================================================' . $crlf .
- '--' . $crlf .
- '-- Database dump of tables in `' . $db_name . '`' . $crlf .
- '-- ' . timeformat(time(), false) . $crlf .
- '--' . $crlf .
- '-- ==========================================================' . $crlf .
- $crlf;
-
- if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) != 0)
- {
- $db = strtr($match[1], array('`' => ''));
- $dbp = str_replace('_', '\_', $match[2]);
- }
- else
- {
- $db = false;
- $dbp = $db_prefix;
- }
-
- $tables = $smcFunc['db_list_tables'](false, $db_prefix . '%');
- foreach ($tables as $tableName)
- {
-
- if (isset($_REQUEST['struct']))
- {
- $db_chunks .=
- $crlf .
- '--' . $crlf .
- '-- Table structure for table `' . $tableName . '`' . $crlf .
- '--' . $crlf .
- $crlf .
- $smcFunc['db_table_sql']($tableName) . ';' . $crlf;
- }
- else
-
- $smcFunc['db_table_sql']($tableName);
-
- if (!isset($_REQUEST['data']) || substr($tableName, -10) == 'log_errors')
- continue;
- $first_round = true;
- $close_table = false;
-
- while ($get_rows = $smcFunc['db_insert_sql']($tableName, $first_round))
- {
- if (empty($get_rows))
- break;
-
- if (function_exists('apache_reset_timeout'))
- @apache_reset_timeout();
- elseif (!empty($time_limit) && ($start_time + $time_limit - 20 > time()))
- {
- $start_time = time();
- @set_time_limit(150);
- }
- if ($first_round)
- {
- $db_chunks .=
- $crlf .
- '--' . $crlf .
- '-- Dumping data in `' . $tableName . '`' . $crlf .
- '--' . $crlf .
- $crlf;
- $first_round = false;
- }
- $db_chunks .=
- $get_rows;
- $current_used_memory += $smcFunc['strlen']($db_chunks);
- $db_backup .= $db_chunks;
- unset($db_chunks);
- $db_chunks = '';
- if ($current_used_memory > $memory_limit)
- {
- echo $output_function($db_backup);
- $current_used_memory = 0;
-
- unset($db_backup);
- $db_backup = '';
- }
- $close_table = true;
- }
-
- if ($close_table)
- $db_backup .=
- '-- --------------------------------------------------------' . $crlf;
- }
- $db_backup .=
- $crlf .
- '-- Done' . $crlf;
- echo $output_function($db_backup);
- exit;
- }
- function un_compressed($string = '')
- {
- return $string;
- }
|