| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
<span class="code-comment">* admin_db_utilities.php |
|---|
| 4 |
* ------------------- |
|---|
| 5 |
* begin : Thu May 31, 2001 |
|---|
| 6 |
* copyright : (C) 2001 The phpBB Group |
|---|
| 7 |
* email : support@phpbb.com |
|---|
| 8 |
* |
|---|
| 9 |
* $Id: admin_db_utilities.php,v 1.42.2.11 2005/02/21 18:36:49 acydburn Exp $ |
|---|
| 10 |
* |
|---|
| 11 |
****************************************************************************/ |
|---|
| 12 |
|
|---|
| 13 |
/*************************************************************************** |
|---|
| 14 |
* |
|---|
| 15 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 16 |
* it under the terms of the GNU General Public License as published by |
|---|
| 17 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 18 |
* (at your option) any later version. |
|---|
| 19 |
* |
|---|
| 20 |
***************************************************************************/ |
|---|
| 21 |
|
|---|
| 22 |
/*************************************************************************** |
|---|
| 23 |
* We will attempt to create a file based backup of all of the data in the |
|---|
| 24 |
* users phpBB database. The resulting file should be able to be imported by |
|---|
| 25 |
* the db_restore.php function, or by using the mysql command_line |
|---|
| 26 |
* |
|---|
| 27 |
* Some functions are adapted from the upgrade_20.php script and others |
|---|
| 28 |
* adapted from the unoficial phpMyAdmin 2.2.0. |
|---|
| 29 |
***************************************************************************/ |
|---|
| 30 |
|
|---|
| 31 |
if( !empty($setmodules) )</span> |
|---|
| 32 |
<span class="code-keyword">{ |
|---|
| 33 |
$filename = basename(__FILE__); |
|---|
| 34 |
$module['general']['backup_db'] = $filename . "?perform=backup"; |
|---|
| 35 |
|
|---|
| 36 |
$file_uploads = (@phpversion() >= '4.0.0') ? @ini_get('file_uploads') : @get_cfg_var('file_uploads'); |
|---|
| 37 |
|
|---|
| 38 |
if( (empty($file_uploads) || $file_uploads != 0) && (strtolower($file_uploads) != 'off') && (@phpversion() != '4.0.4pl1') ) |
|---|
| 39 |
{ |
|---|
| 40 |
$module['general']['restore_db'] = $filename . "?perform=restore"; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
return; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
define('IN_PHPBB', 1); |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
<span class="code-comment">// Load default header |
|---|
| 50 |
// |
|---|
| 51 |
$no_page_header = TRUE; |
|---|
| 52 |
$root_path = './../';</span> |
|---|
| 53 |
<span class="code-lang">require($root_path . 'extension.inc'); |
|---|
| 54 |
require('./pagestart.' . $phpEx); |
|---|
| 55 |
include($root_path . 'phpBB2/includes/sql_parse.'.$phpEx); |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
<span class="code-comment">// Increase maximum execution time, but don't complain about it if it isn't |
|---|
| 59 |
// allowed. |
|---|
| 60 |
// |
|---|
| 61 |
@set_time_limit(0); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
<span class="code-comment">// The following functions are adapted from phpMyAdmin and upgrade_20.php |
|---|
| 65 |
// |
|---|
| 66 |
function gzip_PrintFourChars($Val)</span> |
|---|
| 67 |
<span class="code-keyword">{ |
|---|
| 68 |
$return = ''; |
|---|
| 69 |
for ($i = 0; $i < 4; $i ++) |
|---|
| 70 |
{ |
|---|
| 71 |
$return .= chr($Val % 256); |
|---|
| 72 |
$Val = floor($Val / 256); |
|---|
| 73 |
} |
|---|
| 74 |
return $return; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
<span class="code-comment">* @package acp |
|---|
| 81 |
*/ |
|---|
| 82 |
class base_extractor |
|---|
| 83 |
{</span> |
|---|
| 84 |
<span class="code-keyword"> var $fh; |
|---|
| 85 |
var $fp; |
|---|
| 86 |
var $write; |
|---|
| 87 |
var $close; |
|---|
| 88 |
var $store; |
|---|
| 89 |
var $download; |
|---|
| 90 |
var $time; |
|---|
| 91 |
var $format; |
|---|
| 92 |
var $run_comp = false; |
|---|
| 93 |
|
|---|
| 94 |
function base_extractor($download = false, $store = false, $format, $filename, $time) |
|---|
| 95 |
{ |
|---|
| 96 |
$this->download = $download; |
|---|
| 97 |
$this->store = $store; |
|---|
| 98 |
$this->time = $time; |
|---|
| 99 |
$this->format = $format; |
|---|
| 100 |
|
|---|
| 101 |
switch ($format) |
|---|
| 102 |
{ |
|---|
| 103 |
case 'text': |
|---|
| 104 |
$ext = '.sql'; |
|---|
| 105 |
$open = 'fopen'; |
|---|
| 106 |
$this->write = 'fwrite'; |
|---|
| 107 |
$this->close = 'fclose'; |
|---|
| 108 |
$mimetype = 'text/x-sql'; |
|---|
| 109 |
break; |
|---|
| 110 |
case 'bzip2': |
|---|
| 111 |
$ext = '.sql.bz2'; |
|---|
| 112 |
$open = 'bzopen'; |
|---|
| 113 |
$this->write = 'bzwrite'; |
|---|
| 114 |
$this->close = 'bzclose'; |
|---|
| 115 |
$mimetype = 'application/x-bzip2'; |
|---|
| 116 |
break; |
|---|
| 117 |
case 'gzip': |
|---|
| 118 |
$ext = '.sql.gz'; |
|---|
| 119 |
$open = 'gzopen'; |
|---|
| 120 |
$this->write = 'gzwrite'; |
|---|
| 121 |
$this->close = 'gzclose'; |
|---|
| 122 |
$mimetype = 'application/x-gzip'; |
|---|
| 123 |
break; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
if ($download == true) |
|---|
| 127 |
{ |
|---|
| 128 |
$name = $filename . $ext; |
|---|
| 129 |
header('Pragma: no-cache'); |
|---|
| 130 |
header("Content-Type: $mimetype; name=\"$name\""); |
|---|
| 131 |
header("Content-disposition: attachment; filename=$name"); |
|---|
| 132 |
|
|---|
| 133 |
switch ($format) |
|---|
| 134 |
{ |
|---|
| 135 |
case 'bzip2': |
|---|
| 136 |
ob_start(); |
|---|
| 137 |
break; |
|---|
| 138 |
|
|---|
| 139 |
case 'gzip': |
|---|
| 140 |
if ((isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) && strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'msie') === false) |
|---|
| 141 |
{ |
|---|
| 142 |
ob_start('ob_gzhandler'); |
|---|
| 143 |
} |
|---|
| 144 |
else |
|---|
| 145 |
{ |
|---|
| 146 |
$this->run_comp = true; |
|---|
| 147 |
} |
|---|
| 148 |
break; |
|---|
| 149 |
} |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
if ($store == true) |
|---|
| 153 |
{ |
|---|
| 154 |
global $root_path; |
|---|
| 155 |
$file = $root_path . 'cache/' . $filename . $ext; |
|---|
| 156 |
|
|---|
| 157 |
$this->fp = $open($file, 'w'); |
|---|
| 158 |
|
|---|
| 159 |
if (!$this->fp) |
|---|
| 160 |
{ |
|---|
| 161 |
trigger_error('Unable to write temporary file to storage folder', E_USER_ERROR); |
|---|
| 162 |
} |
|---|
| 163 |
} |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
function write_end() |
|---|
| 167 |
{ |
|---|
| 168 |
static $close; |
|---|
| 169 |
if ($this->store) |
|---|
| 170 |
{ |
|---|
| 171 |
if ($close === null) |
|---|
| 172 |
{ |
|---|
| 173 |
$close = $this->close; |
|---|
| 174 |
} |
|---|
| 175 |
$close($this->fp); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
if ($this->download && $this->format === 'bzip2') |
|---|
| 180 |
{ |
|---|
| 181 |
$c = ob_get_clean(); |
|---|
| 182 |
echo bzcompress($c); |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
function flush($data) |
|---|
| 187 |
{ |
|---|
| 188 |
static $write; |
|---|
| 189 |
if ($this->store === true) |
|---|
| 190 |
{ |
|---|
| 191 |
if ($write === null) |
|---|
| 192 |
{ |
|---|
| 193 |
$write = $this->write; |
|---|
| 194 |
} |
|---|
| 195 |
$write($this->fp, $data); |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
if ($this->download === true) |
|---|
| 199 |
{ |
|---|
| 200 |
if ($this->format === 'bzip2' || $this->format === 'text' || ($this->format === 'gzip' && !$this->run_comp)) |
|---|
| 201 |
{ |
|---|
| 202 |
echo $data; |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
if ($this->format === 'gzip') |
|---|
| 207 |
{ |
|---|
| 208 |
if ($this->run_comp) |
|---|
| 209 |
{ |
|---|
| 210 |
echo gzencode($data); |
|---|
| 211 |
} |
|---|
| 212 |
else |
|---|
| 213 |
{ |
|---|
| 214 |
ob_flush(); |
|---|
| 215 |
flush(); |
|---|
| 216 |
} |
|---|
| 217 |
} |
|---|
| 218 |
} |
|---|
| 219 |
} |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
<span class="code-comment">* @package acp |
|---|
| 224 |
*/ |
|---|
| 225 |
class mysql_extractor extends base_extractor |
|---|
| 226 |
{</span> |
|---|
| 227 |
<span class="code-keyword"> function write_start($table_prefix) |
|---|
| 228 |
{ |
|---|
| 229 |
$sql_data = "#\n"; |
|---|
| 230 |
$sql_data .= "# TBDevSZ Backup Script\n"; |
|---|
| 231 |
$sql_data .= "# Dump of tables for $table_prefix\n"; |
|---|
| 232 |
$sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n"; |
|---|
| 233 |
$sql_data .= "#\n"; |
|---|
| 234 |
$this->flush($sql_data); |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
function write_table($table_name) |
|---|
| 238 |
{ |
|---|
| 239 |
global $db; |
|---|
| 240 |
static $new_extract; |
|---|
| 241 |
|
|---|
| 242 |
if ($new_extract === null) |
|---|
| 243 |
{ |
|---|
| 244 |
if ($db->sql_layer === 'mysqli' || version_compare($db->mysql_version, '3.23.20', '>=')) |
|---|
| 245 |
{ |
|---|
| 246 |
$new_extract = true; |
|---|
| 247 |
} |
|---|
| 248 |
else |
|---|
| 249 |
{ |
|---|
| 250 |
$new_extract = false; |
|---|
| 251 |
} |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
if ($new_extract) |
|---|
| 255 |
{ |
|---|
| 256 |
$this->new_write_table($table_name); |
|---|
| 257 |
} |
|---|
| 258 |
else |
|---|
| 259 |
{ |
|---|
| 260 |
$this->old_write_table($table_name); |
|---|
| 261 |
} |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
function write_data($table_name) |
|---|
| 265 |
{ |
|---|
| 266 |
global $db; |
|---|
| 267 |
if ($db->sql_layer === 'mysqli') |
|---|
| 268 |
{ |
|---|
| 269 |
$this->write_data_mysqli($table_name); |
|---|
| 270 |
} |
|---|
| 271 |
else |
|---|
| 272 |
{ |
|---|
| 273 |
$this->write_data_mysql($table_name); |
|---|
| 274 |
} |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
function write_data_mysqli($table_name) |
|---|
| 278 |
{ |
|---|
| 279 |
global $db; |
|---|
| 280 |
$sql = "SELECT * |
|---|
| 281 |
FROM $table_name"; |
|---|
| 282 |
$result = mysqli_query($db->db_connect_id, $sql, MYSQLI_USE_RESULT); |
|---|
| 283 |
if ($result != false) |
|---|
| 284 |
{ |
|---|
| 285 |
$fields_cnt = mysqli_num_fields($result); |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
$field = mysqli_fetch_fields($result); |
|---|
| 289 |
$field_set = array(); |
|---|
| 290 |
|
|---|
| 291 |
for ($j = 0; $j < $fields_cnt; $j++) |
|---|
| 292 |
{ |
|---|
| 293 |
$field_set[] = $field[$j]->name; |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
$search = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"'); |
|---|
| 297 |
$replace = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"'); |
|---|
| 298 |
$fields = implode(', ', $field_set); |
|---|
| 299 |
$sql_data = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES '; |
|---|
| 300 |
$first_set = true; |
|---|
| 301 |
$query_len = 0; |
|---|
| 302 |
$max_len = get_usable_memory(); |
|---|
| 303 |
|
|---|
| 304 |
while ($row = mysqli_fetch_row($result)) |
|---|
| 305 |
{ |
|---|
| 306 |
$values = array(); |
|---|
| 307 |
if ($first_set) |
|---|
| 308 |
{ |
|---|
| 309 |
$query = $sql_data . '('; |
|---|
| 310 |
} |
|---|
| 311 |
else |
|---|
| 312 |
{ |
|---|
| 313 |
$query .= ',('; |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
for ($j = 0; $j < $fields_cnt; $j++) |
|---|
| 317 |
{ |
|---|
| 318 |
if (!isset($row[$j]) || is_null($row[$j])) |
|---|
| 319 |
{ |
|---|
| 320 |
$values[$j] = 'NULL'; |
|---|
| 321 |
} |
|---|
| 322 |
else if (($field[$j]->flags & 32768) && !($field[$j]->flags & 1024)) |
|---|
| 323 |
{ |
|---|
| 324 |
$values[$j] = $row[$j]; |
|---|
| 325 |
} |
|---|
| 326 |
else |
|---|
| 327 |
{ |
|---|
| 328 |
$values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'"; |
|---|
| 329 |
} |
|---|
| 330 |
} |
|---|
| 331 |
$query .= implode(', ', $values) . ')'; |
|---|
| 332 |
|
|---|
| 333 |
$query_len += strlen($query); |
|---|
| 334 |
if ($query_len > $max_len) |
|---|
| 335 |
{ |
|---|
| 336 |
$this->flush($query . ";\n\n"); |
|---|
| 337 |
$query = ''; |
|---|
| 338 |
$query_len = 0; |
|---|
| 339 |
$first_set = true; |
|---|
| 340 |
} |
|---|
| 341 |
else |
|---|
| 342 |
{ |
|---|
| 343 |
$first_set = false; |
|---|
| 344 |
} |
|---|
| 345 |
} |
|---|
| 346 |
mysqli_free_result($result); |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
if (!$first_set && $query) |
|---|
| 350 |
{ |
|---|
| 351 |
$this->flush($query . ";\n\n"); |
|---|
| 352 |
} |
|---|
| 353 |
} |
|---|
| 354 |
} |
|---|
| 355 |
|
|---|
| 356 |
function write_data_mysql($table_name) |
|---|
| 357 |
{ |
|---|
| 358 |
global $db; |
|---|
| 359 |
$sql = "SELECT * |
|---|
| 360 |
FROM $table_name"; |
|---|
| 361 |
$result = mysql_unbuffered_query($sql, $db->db_connect_id); |
|---|
| 362 |
|
|---|
| 363 |
if ($result != false) |
|---|
| 364 |
{ |
|---|
| 365 |
$fields_cnt = mysql_num_fields($result); |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
$field = array(); |
|---|
| 369 |
for ($i = 0; $i < $fields_cnt; $i++) |
|---|
| 370 |
{ |
|---|
| 371 |
$field[] = mysql_fetch_field($result, $i); |
|---|
| 372 |
} |
|---|
| 373 |
$field_set = array(); |
|---|
| 374 |
|
|---|
| 375 |
for ($j = 0; $j < $fields_cnt; $j++) |
|---|
| 376 |
{ |
|---|
| 377 |
$field_set[] = $field[$j]->name; |
|---|
| 378 |
} |
|---|
| 379 |
|
|---|
| 380 |
$search = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"'); |
|---|
| 381 |
$replace = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"'); |
|---|
| 382 |
$fields = implode(', ', $field_set); |
|---|
| 383 |
$sql_data = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES '; |
|---|
| 384 |
$first_set = true; |
|---|
| 385 |
$query_len = 0; |
|---|
| 386 |
$max_len = get_usable_memory(); |
|---|
| 387 |
|
|---|
| 388 |
while ($row = mysql_fetch_row($result)) |
|---|
| 389 |
{ |
|---|
| 390 |
$values = array(); |
|---|
| 391 |
if ($first_set) |
|---|
| 392 |
{ |
|---|
| 393 |
$query = $sql_data . '('; |
|---|
| 394 |
} |
|---|
| 395 |
else |
|---|
| 396 |
{ |
|---|
| 397 |
$query .= ',('; |
|---|
| 398 |
} |
|---|
| 399 |
|
|---|
| 400 |
for ($j = 0; $j < $fields_cnt; $j++) |
|---|
| 401 |
{ |
|---|
| 402 |
if (!isset($row[$j]) || is_null($row[$j])) |
|---|
| 403 |
{ |
|---|
| 404 |
$values[$j] = 'NULL'; |
|---|
| 405 |
} |
|---|
| 406 |
else if ($field[$j]->numeric && ($field[$j]->type !== 'timestamp')) |
|---|
| 407 |
{ |
|---|
| 408 |
$values[$j] = $row[$j]; |
|---|
| 409 |
} |
|---|
| 410 |
else |
|---|
| 411 |
{ |
|---|
| 412 |
$values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'"; |
|---|
| 413 |
} |
|---|
| 414 |
} |
|---|
| 415 |
$query .= implode(', ', $values) . ')'; |
|---|
| 416 |
|
|---|
| 417 |
$query_len += strlen($query); |
|---|
| 418 |
if ($query_len > $max_len) |
|---|
| 419 |
{ |
|---|
| 420 |
$this->flush($query . ";\n\n"); |
|---|
| 421 |
$query = ''; |
|---|
| 422 |
$query_len = 0; |
|---|
| 423 |
$first_set = true; |
|---|
| 424 |
} |
|---|
| 425 |
else |
|---|
| 426 |
{ |
|---|
| 427 |
$first_set = false; |
|---|
| 428 |
} |
|---|
| 429 |
} |
|---|
| 430 |
mysql_free_result($result); |
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
if (!$first_set && $query) |
|---|
| 434 |
{ |
|---|
| 435 |
$this->flush($query . ";\n\n"); |
|---|
| 436 |
} |
|---|
| 437 |
} |
|---|
| 438 |
} |
|---|
| 439 |
|
|---|
| 440 |
function new_write_table($table_name) |
|---|
| 441 |
{ |
|---|
| 442 |
global $db; |
|---|
| 443 |
|
|---|
| 444 |
$sql = 'SHOW CREATE TABLE ' . $table_name; |
|---|
| 445 |
$result = $db->sql_query($sql); |
|---|
| 446 |
$row = $db->sql_fetchrow($result); |
|---|
| 447 |
|
|---|
| 448 |
$sql_data = '# Table: ' . $table_name . "\n"; |
|---|
| 449 |
$sql_data .= "DROP TABLE IF EXISTS $table_name;\n"; |
|---|
| 450 |
$this->flush($sql_data . $row['Create Table'] . ";\n\n"); |
|---|
| 451 |
|
|---|
| 452 |
$db->sql_freeresult($result); |
|---|
| 453 |
} |
|---|
| 454 |
|
|---|
| 455 |
function old_write_table($table_name) |
|---|
| 456 |
{ |
|---|
| 457 |
global $db; |
|---|
| 458 |
|
|---|
| 459 |
$sql_data = '# Table: ' . $table_name . "\n"; |
|---|
| 460 |
$sql_data .= "DROP TABLE IF EXISTS $table_name;\n"; |
|---|
| 461 |
$sql_data .= "CREATE TABLE $table_name(\n"; |
|---|
| 462 |
$rows = array(); |
|---|
| 463 |
|
|---|
| 464 |
$sql = "SHOW FIELDS |
|---|
| 465 |
FROM $table_name"; |
|---|
| 466 |
$result = $db->sql_query($sql); |
|---|
| 467 |
|
|---|
| 468 |
while ($row = $db->sql_fetchrow($result)) |
|---|
| 469 |
{ |
|---|
| 470 |
$line = ' ' . $row['Field'] . ' ' . $row['Type']; |
|---|
| 471 |
|
|---|
| 472 |
if (!is_null($row['Default'])) |
|---|
| 473 |
{ |
|---|
| 474 |
$line .= " DEFAULT '{$row['Default']}'"; |
|---|
| 475 |
} |
|---|
| 476 |
|
|---|
| 477 |
if ($row['Null'] != 'YES') |
|---|
| 478 |
{ |
|---|
| 479 |
$line .= ' NOT NULL'; |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
if ($row['Extra'] != '') |
|---|
| 483 |
{ |
|---|
| 484 |
$line .= ' ' . $row['Extra']; |
|---|
| 485 |
} |
|---|
| 486 |
|
|---|
| 487 |
$rows[] = $line; |
|---|
| 488 |
} |
|---|
| 489 |
$db->sql_freeresult($result); |
|---|
| 490 |
|
|---|
| 491 |
$sql = "SHOW KEYS |
|---|
| 492 |
FROM $table_name"; |
|---|
| 493 |
|
|---|
| 494 |
$result = $db->sql_query($sql); |
|---|
| 495 |
|
|---|
| 496 |
$index = array(); |
|---|
| 497 |
while ($row = $db->sql_fetchrow($result)) |
|---|
| 498 |
{ |
|---|
| 499 |
$kname = $row['Key_name']; |
|---|
| 500 |
|
|---|
| 501 |
if ($kname != 'PRIMARY') |
|---|
| 502 |
{ |
|---|
| 503 |
if ($row['Non_unique'] == 0) |
|---|
| 504 |
{ |
|---|
| 505 |
$kname = "UNIQUE|$kname"; |
|---|
| 506 |
} |
|---|
| 507 |
} |
|---|
| 508 |
|
|---|
| 509 |
if ($row['Sub_part']) |
|---|
| 510 |
{ |
|---|
| 511 |
$row['Column_name'] .= '(' . $row['Sub_part'] . ')'; |
|---|
| 512 |
} |
|---|
| 513 |
$index[$kname][] = $row['Column_name']; |
|---|
| 514 |
} |
|---|
| 515 |
$db->sql_freeresult($result); |
|---|
| 516 |
|
|---|
| 517 |
foreach ($index as $key => $columns) |
|---|
| 518 |
{ |
|---|
| 519 |
$line = ' '; |
|---|
| 520 |
|
|---|
| 521 |
if ($key == 'PRIMARY') |
|---|
| 522 |
{ |
|---|
| 523 |
$line .= 'PRIMARY KEY (' . implode(', ', $columns) . ')'; |
|---|
| 524 |
} |
|---|
| 525 |
else if (strpos($key, 'UNIQUE') === 0) |
|---|
| 526 |
{ |
|---|
| 527 |
$line .= 'UNIQUE ' . substr($key, 7) . ' (' . implode(', ', $columns) . ')'; |
|---|
| 528 |
} |
|---|
| 529 |
else if (strpos($key, 'FULLTEXT') === 0) |
|---|
| 530 |
{ |
|---|
| 531 |
$line .= 'FULLTEXT ' . substr($key, 9) . ' (' . implode(', ', $columns) . ')'; |
|---|
| 532 |
} |
|---|
| 533 |
else |
|---|
| 534 |
{ |
|---|
| 535 |
$line .= "KEY $key (" . implode(', ', $columns) . ')'; |
|---|
| 536 |
} |
|---|
| 537 |
|
|---|
| 538 |
$rows[] = $line; |
|---|
| 539 |
} |
|---|
| 540 |
|
|---|
| 541 |
$sql_data .= implode(",\n", $rows); |
|---|
| 542 |
$sql_data .= "\n);\n\n"; |
|---|
| 543 |
|
|---|
| 544 |
$this->flush($sql_data); |
|---|
| 545 |
} |
|---|
| 546 |
} |
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
function get_usable_memory()</span> |
|---|
| 550 |
<span class="code-keyword">{ |
|---|
| 551 |
$val = trim(@ini_get('memory_limit')); |
|---|
| 552 |
|
|---|
| 553 |
if (preg_match('/(\\d+)([mkg]?)/i', $val, $regs)) |
|---|
| 554 |
{ |
|---|
| 555 |
$memory_limit = (int) $regs[1]; |
|---|
| 556 |
switch ($regs[2]) |
|---|
| 557 |
{ |
|---|
| 558 |
|
|---|
| 559 |
case 'k': |
|---|
| 560 |
case 'K': |
|---|
| 561 |
$memory_limit *= 1024; |
|---|
| 562 |
break; |
|---|
| 563 |
|
|---|
| 564 |
case 'm': |
|---|
| 565 |
case 'M': |
|---|
| 566 |
$memory_limit *= 1048576; |
|---|
| 567 |
break; |
|---|
| 568 |
|
|---|
| 569 |
case 'g': |
|---|
| 570 |
case 'G': |
|---|
| 571 |
$memory_limit *= 1073741824; |
|---|
| 572 |
break; |
|---|
| 573 |
} |
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
if ($memory_limit > 6100000) |
|---|
| 577 |
{ |
|---|
| 578 |
$memory_limit -= 6100000; |
|---|
| 579 |
} |
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
$memory_limit /= 2; |
|---|
| 583 |
} |
|---|
| 584 |
else |
|---|
| 585 |
{ |
|---|
| 586 |
|
|---|
| 587 |
$memory_limit = 1048576; |
|---|
| 588 |
} |
|---|
| 589 |
|
|---|
| 590 |
return $memory_limit; |
|---|
| 591 |
} |
|---|
| 592 |
|
|---|
| 593 |
<span class="code-comment">// End Functions |
|---|
| 594 |
// ------------- |
|---|
| 595 |
|
|---|
| 596 |
$perform = request_var('perform', ''); |
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
<span class="code-comment">// Begin program proper |
|---|
| 601 |
// |
|---|
| 602 |
if( $perform )</span> |
|---|
| 603 |
<span class="code-keyword">{ |
|---|
| 604 |
|
|---|
| 605 |
switch($perform) |
|---|
| 606 |
{ |
|---|
| 607 |
case 'backup': |
|---|
| 608 |
|
|---|
| 609 |
$error = false; |
|---|
| 610 |
switch($db_type) |
|---|
| 611 |
{ |
|---|
| 612 |
case 'oracle': |
|---|
| 613 |
$error = true; |
|---|
| 614 |
break; |
|---|
| 615 |
case 'db2': |
|---|
| 616 |
$error = true; |
|---|
| 617 |
break; |
|---|
| 618 |
case 'msaccess': |
|---|
| 619 |
$error = true; |
|---|
| 620 |
break; |
|---|
| 621 |
case 'mssql': |
|---|
| 622 |
case 'mssql-odbc': |
|---|
| 623 |
$error = true; |
|---|
| 624 |
break; |
|---|
| 625 |
} |
|---|
| 626 |
|
|---|
| 627 |
if ($error) |
|---|
| 628 |
{ |
|---|
| 629 |
include('./page_header_admin.'.$phpEx); |
|---|
| 630 |
|
|---|
| 631 |
$template->set_filenames(array( |
|---|
| 632 |
"body" => "../admin/admin_message_body.tpl") |
|---|
| 633 |
); |
|---|
| 634 |
|
|---|
| 635 |
$template->assign_vars(array( |
|---|
| 636 |
"MESSAGE_TITLE" => $lang['information'], |
|---|
| 637 |
"MESSAGE_TEXT" => $lang['backups_not_supported']) |
|---|
| 638 |
); |
|---|
| 639 |
|
|---|
| 640 |
$template->display("body"); |
|---|
| 641 |
|
|---|
| 642 |
include('./page_footer_admin.'.$phpEx); |
|---|
| 643 |
} |
|---|
| 644 |
|
|---|
| 645 |
$tables = array(BANLIST_TABLE, BOOKMARKS_TABLE, CATEGORIES_TABLE, CHEATERS_TABLE, COMMENTS_NOTIFY_TABLE, COMMENTS_TABLE, CONFIG_TABLE, CONFIRM_TABLE, COUNTRIES_TABLE, DISALLOW_TABLE, FILES_TABLE, AUTH_ACCESS_TABLE, FORUM_CATEGORIES_TABLE, FORUMS_TABLE, GROUPS_TABLE, POSTS_TABLE, POSTS_TEXT_TABLE, PRUNE_TABLE, SEARCH_TABLE, SEARCH_WORD_TABLE, SEARCH_MATCH_TABLE, TOPICS_TABLE, USER_GROUP_TABLE, FRIENDS_TABLE, HELPDESK_TABLE, INDEXRELEASES_TABLE, INVITES_TABLE, PRIVATE_MESSAGES_TABLE, NEWS_TABLE, OFFERS_TABLE, OFFERS_VOTES_TABLE, PEERS_TABLE, RATINGS_TABLE, RELEASE_GROUPS_TABLE, REPORTS_TABLE, REQUESTS_TABLE, REQUESTS_VOTES_TABLE, SESSIONS_TABLE, SESSIONS_KEYS_TABLE, SIMPATY_TABLE, SITELOG_TABLE, SMILIES_TABLE, SNATCHED_TABLE, SOS_TABLE, THANKS_TABLE, THEMES_TABLE, TORRENTS_TABLE, UPLOADAPP_TABLE, USERS_TABLE, WORDS_TABLE, VOTE_DESC_TABLE, VOTE_RESULTS_TABLE, VOTE_USERS_TABLE); |
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
$additional_tables = request_var('additional_tables', ''); |
|---|
| 649 |
|
|---|
| 650 |
$backup_type = request_var('backup_type', ''); |
|---|
| 651 |
|
|---|
| 652 |
$gzipcompress = request_var('gzipcompress', 0); |
|---|
| 653 |
|
|---|
| 654 |
$drop = request_var('drop', 0); |
|---|
| 655 |
|
|---|
| 656 |
if(!empty($additional_tables)) |
|---|
| 657 |
{ |
|---|
| 658 |
if(strpos(",", $additional_tables)) |
|---|
| 659 |
{ |
|---|
| 660 |
$additional_tables = implode(",", $additional_tables); |
|---|
| 661 |
|
|---|
| 662 |
for($i = 0; $i < sizeof($additional_tables); $i++) |
|---|
| 663 |
{ |
|---|
| 664 |
$tables[] = trim($additional_tables[$i]); |
|---|
| 665 |
} |
|---|
| 666 |
|
|---|
| 667 |
} |
|---|
| 668 |
else |
|---|
| 669 |
{ |
|---|
| 670 |
$tables[] = trim($additional_tables); |
|---|
| 671 |
} |
|---|
| 672 |
} |
|---|
| 673 |
|
|---|
| 674 |
if( !isset($_POST['backupstart']) && !isset($_GET['backupstart'])) |
|---|
| 675 |
{ |
|---|
| 676 |
include('./page_header_admin.'.$phpEx); |
|---|
| 677 |
|
|---|
| 678 |
$template->set_filenames(array( |
|---|
| 679 |
"body" => "../admin/db_utils_backup_body.tpl") |
|---|
| 680 |
); |
|---|
| 681 |
$s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"backup\" /><input type=\"hidden\" name=\"drop\" value=\"1\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />"; |
|---|
| 682 |
|
|---|
| 683 |
$template->assign_vars(array( |
|---|
| 684 |
"L_DATABASE_BACKUP" => $lang['database_utilities'] . " : " . $lang['backup'], |
|---|
| 685 |
|
|---|
| 686 |
"S_HIDDEN_FIELDS" => $s_hidden_fields, |
|---|
| 687 |
"S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx")) |
|---|
| 688 |
); |
|---|
| 689 |
$template->display("body"); |
|---|
| 690 |
|
|---|
| 691 |
break; |
|---|
| 692 |
|
|---|
| 693 |
} |
|---|
| 694 |
else if( !isset($_POST['startdownload']) && !isset($_GET['startdownload']) ) |
|---|
| 695 |
{ |
|---|
| 696 |
if(is_array($additional_tables)) |
|---|
| 697 |
{ |
|---|
| 698 |
$additional_tables = implode(',', $additional_tables); |
|---|
| 699 |
} |
|---|
| 700 |
$template->set_filenames(array( |
|---|
| 701 |
"body" => "../admin/admin_message_body.tpl") |
|---|
| 702 |
); |
|---|
| 703 |
|
|---|
| 704 |
$template->assign_vars(array( |
|---|
| 705 |
"META" => '<meta http-equiv="refresh" content="2;url=' . append_sid("admin_db_utilities.$phpEx?perform=backup&additional_tables=" . quotemeta($additional_tables) . "&backup_type=$backup_type&drop=1&backupstart=1&gzipcompress=$gzipcompress&startdownload=1") . '">', |
|---|
| 706 |
|
|---|
| 707 |
"MESSAGE_TITLE" => $lang['database_utilities'] . " : " . $lang['backup'], |
|---|
| 708 |
"MESSAGE_TEXT" => $lang['backup_download']) |
|---|
| 709 |
); |
|---|
| 710 |
|
|---|
| 711 |
include('./page_header_admin.'.$phpEx); |
|---|
| 712 |
|
|---|
| 713 |
$template->display("body"); |
|---|
| 714 |
|
|---|
| 715 |
include('./page_footer_admin.'.$phpEx); |
|---|
| 716 |
|
|---|
| 717 |
} |
|---|
| 718 |
header("Pragma: no-cache"); |
|---|
| 719 |
|
|---|
| 720 |
$structure = $schema_data = false; |
|---|
| 721 |
$store = $download = true; |
|---|
| 722 |
|
|---|
| 723 |
if ($backup_type == 'full' || $backup_type == 'structure') |
|---|
| 724 |
{ |
|---|
| 725 |
$structure = true; |
|---|
| 726 |
} |
|---|
| 727 |
|
|---|
| 728 |
if ($backup_type == 'full' || $backup_type == 'data') |
|---|
| 729 |
{ |
|---|
| 730 |
$schema_data = true; |
|---|
| 731 |
} |
|---|
| 732 |
|
|---|
| 733 |
$format = ( $gzipcompress ? 'gzip' : 'text' ); |
|---|
| 734 |
|
|---|
| 735 |
@set_time_limit(1200); |
|---|
| 736 |
|
|---|
| 737 |
$time = time(); |
|---|
| 738 |
|
|---|
| 739 |
$filename = 'tb_dev_sz_db_backup_' . $time; |
|---|
| 740 |
switch ($db->sql_layer) |
|---|
| 741 |
{ |
|---|
| 742 |
case 'mysqli': |
|---|
| 743 |
case 'mysql4': |
|---|
| 744 |
case 'mysql': |
|---|
| 745 |
$extractor = new mysql_extractor($download, $store, $format, $filename, $time); |
|---|
| 746 |
break; |
|---|
| 747 |
} |
|---|
| 748 |
|
|---|
| 749 |
$extractor->write_start($table_prefix); |
|---|
| 750 |
|
|---|
| 751 |
foreach ($tables as $table_name) |
|---|
| 752 |
{ |
|---|
| 753 |
|
|---|
| 754 |
if ($structure) |
|---|
| 755 |
{ |
|---|
| 756 |
$extractor->write_table($table_name); |
|---|
| 757 |
} |
|---|
| 758 |
else |
|---|
| 759 |
{ |
|---|
| 760 |
|
|---|
| 761 |
switch ($db->sql_layer) |
|---|
| 762 |
{ |
|---|
| 763 |
default: |
|---|
| 764 |
$extractor->flush('TRUNCATE TABLE ' . $table_name . ";\n"); |
|---|
| 765 |
break; |
|---|
| 766 |
} |
|---|
| 767 |
} |
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
if ($schema_data) |
|---|
| 771 |
{ |
|---|
| 772 |
$extractor->write_data($table_name); |
|---|
| 773 |
} |
|---|
| 774 |
} |
|---|
| 775 |
|
|---|
| 776 |
$extractor->write_end(); |
|---|
| 777 |
|
|---|
| 778 |
if ($download == true) |
|---|
| 779 |
{ |
|---|
| 780 |
gc(); |
|---|
| 781 |
} |
|---|
| 782 |
|
|---|
| 783 |
break; |
|---|
| 784 |
|
|---|
| 785 |
case 'restore': |
|---|
| 786 |
if(!isset($_POST['restore_start'])) |
|---|
| 787 |
{ |
|---|
| 788 |
|
|---|
| 789 |
// Define Template files... |
|---|
| 790 |
// |
|---|
| 791 |
include('./page_header_admin.'.$phpEx); |
|---|
| 792 |
|
|---|
| 793 |
$template->set_filenames(array( |
|---|
| 794 |
"body" => "../admin/db_utils_restore_body.tpl") |
|---|
| 795 |
); |
|---|
| 796 |
|
|---|
| 797 |
$s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"restore\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />"; |
|---|
| 798 |
|
|---|
| 799 |
$template->assign_vars(array( |
|---|
| 800 |
"L_DATABASE_RESTORE" => $lang['database_utilities'] . " : " . $lang['restore'], |
|---|
| 801 |
|
|---|
| 802 |
"S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"), |
|---|
| 803 |
"S_HIDDEN_FIELDS" => $s_hidden_fields) |
|---|
| 804 |
); |
|---|
| 805 |
$template->display("body"); |
|---|
| 806 |
|
|---|
| 807 |
break; |
|---|
| 808 |
|
|---|
| 809 |
} |
|---|
| 810 |
else |
|---|
| 811 |
{ |
|---|
| 812 |
|
|---|
| 813 |
// Handle the file upload .... |
|---|
| 814 |
// If no file was uploaded report an error... |
|---|
| 815 |
// |
|---|
| 816 |
$backup_file_name = (!empty($_FILES['backup_file']['name'])) ? $_FILES['backup_file']['name'] : ""; |
|---|
| 817 |
$backup_file_tmpname = ($_FILES['backup_file']['tmp_name'] != "none") ? $_FILES['backup_file']['tmp_name'] : ""; |
|---|
| 818 |
$backup_file_type = (!empty($_FILES['backup_file']['type'])) ? $_FILES['backup_file']['type'] : ""; |
|---|
| 819 |
|
|---|
| 820 |
if($backup_file_tmpname == "" || $backup_file_name == "") |
|---|
| 821 |
{ |
|---|
| 822 |
trigger_error($lang['restore_error_no_file']); |
|---|
| 823 |
return; |
|---|
| 824 |
} |
|---|
| 825 |
|
|---|
| 826 |
// If I file was actually uploaded, check to make sure that we |
|---|
| 827 |
// are actually passed the name of an uploaded file, and not |
|---|
| 828 |
// a hackers attempt at getting us to process a local system |
|---|
| 829 |
// file. |
|---|
| 830 |
// |
|---|
| 831 |
if( file_exists(phpbb_realpath($backup_file_tmpname)) ) |
|---|
| 832 |
{ |
|---|
| 833 |
if( preg_match("/^(text\/[a-zA-Z]+)|(application\/(x\-)?gzip(\-compressed)?)|(application\/octet-stream)$/is", $backup_file_type) ) |
|---|
| 834 |
{ |
|---|
| 835 |
if( preg_match("/\.gz$/is",$backup_file_name) ) |
|---|
| 836 |
{ |
|---|
| 837 |
$do_gzip_compress = FALSE; |
|---|
| 838 |
$phpver = phpversion(); |
|---|
| 839 |
if($phpver >= "4.0") |
|---|
| 840 |
{ |
|---|
| 841 |
if(extension_loaded("zlib")) |
|---|
| 842 |
{ |
|---|
| 843 |
$do_gzip_compress = TRUE; |
|---|
| 844 |
} |
|---|
| 845 |
} |
|---|
| 846 |
|
|---|
| 847 |
if($do_gzip_compress) |
|---|
| 848 |
{ |
|---|
| 849 |
$gz_ptr = gzopen($backup_file_tmpname, 'rb'); |
|---|
| 850 |
$sql_query = ""; |
|---|
| 851 |
while( !gzeof($gz_ptr) ) |
|---|
| 852 |
{ |
|---|
| 853 |
$sql_query .= gzgets($gz_ptr, 100000); |
|---|
| 854 |
} |
|---|
| 855 |
} |
|---|
| 856 |
else |
|---|
| 857 |
{ |
|---|
| 858 |
trigger_error($lang['restore_error_decompress']); |
|---|
| 859 |
return; |
|---|
| 860 |
} |
|---|
| 861 |
} |
|---|
| 862 |
else |
|---|
| 863 |
{ |
|---|
| 864 |
$sql_query = fread(fopen($backup_file_tmpname, 'r'), filesize($backup_file_tmpname)); |
|---|
| 865 |
} |
|---|
| 866 |
|
|---|
| 867 |
// Comment this line out to see if this fixes the stuff... |
|---|
| 868 |
// |
|---|
| 869 |
//$sql_query = stripslashes($sql_query); |
|---|
| 870 |
} |
|---|
| 871 |
else |
|---|
| 872 |
{ |
|---|
| 873 |
trigger_error($lang['restore_error_filename'] ." $backup_file_type $backup_file_name"); |
|---|
| 874 |
return; |
|---|
| 875 |
} |
|---|
| 876 |
} |
|---|
| 877 |
else |
|---|
| 878 |
{ |
|---|
| 879 |
trigger_error($lang['restore_error_uploading']); |
|---|
| 880 |
return; |
|---|
| 881 |
} |
|---|
| 882 |
|
|---|
| 883 |
if($sql_query != "") |
|---|
| 884 |
{ |
|---|
| 885 |
|
|---|
| 886 |
$sql_query = remove_remarks($sql_query); |
|---|
| 887 |
$pieces = split_sql_file($sql_query, ";"); |
|---|
| 888 |
|
|---|
| 889 |
$sql_count = sizeof($pieces); |
|---|
| 890 |
for($i = 0; $i < $sql_count; $i++) |
|---|
| 891 |
{ |
|---|
| 892 |
$sql = trim($pieces[$i]); |
|---|
| 893 |
|
|---|
| 894 |
if(!empty($sql) and $sql[0] != "#") |
|---|
| 895 |
{ |
|---|
| 896 |
if( defined('DEBUG_EXTRA') ) |
|---|
| 897 |
{ |
|---|
| 898 |
echo "Executing: $sql\n<br>"; |
|---|
| 899 |
flush(); |
|---|
| 900 |
} |
|---|
| 901 |
|
|---|
| 902 |
$result = $db->sql_query($sql); |
|---|
| 903 |
|
|---|
| 904 |
if(!$result && ( !($db_type == 'postgresql' && eregi("drop table", $sql) ) ) ) |
|---|
| 905 |
{ |
|---|
| 906 |
trigger_error("Error importing backup file"); |
|---|
| 907 |
return; |
|---|
| 908 |
} |
|---|
| 909 |
} |
|---|
| 910 |
} |
|---|
| 911 |
} |
|---|
| 912 |
|
|---|
| 913 |
include('./page_header_admin.'.$phpEx); |
|---|
| 914 |
|
|---|
| 915 |
$template->set_filenames(array( |
|---|
| 916 |
"body" => "../admin/admin_message_body.tpl") |
|---|
| 917 |
); |
|---|
| 918 |
|
|---|
| 919 |
$message = $lang['restore_success']; |
|---|
| 920 |
|
|---|
| 921 |
$template->assign_vars(array( |
|---|
| 922 |
"MESSAGE_TITLE" => $lang['database_utilities'] . " : " . $lang['restore'], |
|---|
| 923 |
"MESSAGE_TEXT" => $message) |
|---|
| 924 |
); |
|---|
| 925 |
|
|---|
| 926 |
$template->display("body"); |
|---|
| 927 |
break; |
|---|
| 928 |
} |
|---|
| 929 |
break; |
|---|
| 930 |
} |
|---|
| 931 |
} |
|---|
| 932 |
|
|---|
| 933 |
include('./page_footer_admin.'.$phpEx); |
|---|
| 934 |
|
|---|
| 935 |
?> |
|---|