3
//////////////////////////////////////////////////////////////
4
//===========================================================
6
//===========================================================
7
// AEF : Advanced Electron Forum
9
// Inspired by Pulkit and taken over by Electron
10
// ----------------------------------------------------------
11
// Started by: Electron, Ronak Gupta, Pulkit Gupta
12
// Date: 23rd Jan 2006
14
// Site: http://www.anelectron.com/ (Anelectron)
15
// ----------------------------------------------------------
16
// Please Read the Terms of use at http://www.anelectron.com
17
// ----------------------------------------------------------
18
//===========================================================
19
// (C)AEF Group All Rights Reserved.
20
//===========================================================
21
//////////////////////////////////////////////////////////////
23
if (!defined('AEF')) {
25
die('Hacking Attempt');
30
global $user, $conn, $dbtables, $logged_in, $globals, $l, $AEF_SESS, $theme;
32
if (!load_lang('admin/backup')) {
37
//The name of the file
38
$theme['init_theme'] = 'admin/backup';
40
//The name of the Page
41
$theme['init_theme_name'] = 'Admin Center - Backup';
43
//Array of functions to initialize
44
$theme['init_theme_func'] = array('fileback_theme', 'dbback_theme');
47
$globals['last_activity'] = 'aback';
50
//If a second Admin act is set then go by that
51
if (isset($_GET['seadact']) && trim($_GET['seadact']) !== "") {
53
$seadact = inputsec(htmlizer(trim($_GET['seadact'])));
63
//The form for backing up files
69
//The form for backing up the database
76
//Function to backup files and folders
79
global $user, $conn, $dbtables, $logged_in, $globals, $l, $AEF_SESS, $theme;
83
/////////////////////////////
84
// Define the necessary VARS
85
/////////////////////////////
90
$folderpath = ''; //The folder to backup
92
$compression = ''; //The compression method
94
$localpath = 0; //The path if we have to write it on the server itself
96
if (isset($_POST['startfileback'])) {
99
if (!(isset($_POST['folderpath'])) || (trim($_POST['folderpath']) == "")) {
101
$error[] = $l['no_path'];
104
$folderpath = inputsec(htmlizer(trim($_POST['folderpath'])));
106
$folderpath = rtrim($folderpath, '/\\');
108
if (!is_readable($folderpath)) {
110
$error[] = $l['no_readable_path'];
114
//on error call the form
115
if (!empty($error)) {
116
$theme['call_theme_func'] = 'fileback_theme';
121
//The compression method
122
if (!(isset($_POST['compression'])) || (trim($_POST['compression']) == "")) {
124
$error[] = $l['no_compression'];
127
$compression = inputsec(htmlizer(trim($_POST['compression'])));
129
if (!in_array($compression, array('zip', 'tar', 'tgz', 'tbz'))) {
131
$error[] = $l['compression_invalid'];
135
//on error call the form
136
if (!empty($error)) {
137
$theme['call_theme_func'] = 'fileback_theme';
141
//Are we to store locally
142
if (isset($_POST['localpath']) && (trim($_POST['localpath']) != "")) {
144
$localpath = inputsec(htmlizer(trim($_POST['localpath'])));
146
$localpath = rtrim($localpath, '/\\');
148
if (!is_writable($localpath)) {
150
$error[] = $l['unaccessible_local_path'];
154
//on error call the form
155
if (!empty($error)) {
156
$theme['call_theme_func'] = 'fileback_theme';
160
//Try to give some more time
164
ini_set('memory_limit', '128M');
167
$filename = basename($folderpath) . '(' . date('Y-m-d') . ')';
169
$data = compress($folderpath, $filename, $compression);
174
$error[] = $l['errors_compressing_data'];
176
//on error call the form
177
if (!empty($error)) {
178
$theme['call_theme_func'] = 'fileback_theme';
184
//What to do ? Output the file
185
if (empty($localpath)) {
187
$globals['stop_buffer_process'] = true;
190
switch ($compression) {
192
header("Content-Type: application/zip");
195
header("Content-Type: application/x-bzip2");
198
header("Content-Type: application/x-gzip");
201
header("Content-Type: application/x-tar");
205
//There are so many headers/headaches
206
header('Content-Disposition: attachment; filename="' . $filename . '.' . $compression . '"');
207
header('Accept-Ranges: bytes');
208
header('Content-Encoding: none');
209
header("Content-Length: " . aefstrlen($data));
210
header("Content-Transfer-Encoding: binary");
211
header('Content-Type: application/octetstream');
212
header("Cache-Control: no-cache, must-revalidate, private");
213
header('Connection: close');
219
if (writefile($localpath . '/' . $filename . '.' . $compression, $data, 0)) {
222
reportmessage($l['backup_ok'], $l['backup_created_ok'], '', $l['backup_created_ok_exp']);
227
$error[] = $l['errors_writing'];
229
//on error call the form
230
if (!empty($error)) {
231
$theme['call_theme_func'] = 'fileback_theme';
240
$theme['call_theme_func'] = 'fileback_theme';
244
//Function to manage Avatar settings
247
global $user, $conn, $dbtables, $logged_in, $globals, $l, $AEF_SESS, $theme;
251
/////////////////////////////
252
// Define the necessary VARS
253
/////////////////////////////
257
$tables = array(); //The name of the tables
259
$localpath = ''; //Store Locally
261
$crlf = "\n"; //MySQL loves \n on all platforms
265
if (isset($_POST['dbback'])) {
268
//Check the Avatar Directory
269
if (!(isset($_POST['tables'])) || !is_array($_POST['tables'])) {
271
$error[] = $l['no_tables_specified'];
274
$tables = $_POST['tables'];
276
$keys = array_keys($dbtables);
278
foreach ($tables as $k => $v) {
280
$tables[$k] = $v = trim($v);
282
//Is it a valid table
283
if (!in_array($v, $keys)) {
285
$error[] = $l['tables_invalid'];
291
//on error call the form
292
if (!empty($error)) {
293
$theme['call_theme_func'] = 'dbback_theme';
298
//The compression method
299
if (!(isset($_POST['compression'])) || (trim($_POST['compression']) == "")) {
301
$error[] = $l['no_compression'];
304
$compression = inputsec(htmlizer(trim($_POST['compression'])));
306
if (!in_array($compression, array('none', 'zip', 'gzip', 'bzip'))) {
308
$error[] = $l['compression_invalid'];
313
//Are we to store locally
314
if (isset($_POST['localpath']) && (trim($_POST['localpath']) != "")) {
316
$localpath = inputsec(htmlizer(trim($_POST['localpath'])));
318
$localpath = rtrim($localpath, '/\\');
320
if (!is_writable($localpath)) {
322
$error[] = $l['unaccessible_local_path'];
326
//on error call the form
327
if (!empty($error)) {
328
$theme['call_theme_func'] = 'dbback_theme';
332
//Select atlest the Data or structure
333
if (!isset($_POST['data']) && !isset($_POST['structure'])) {
335
$error[] = $l['select_structure_data'];
337
//on error call the form
338
if (!empty($error)) {
339
$theme['call_theme_func'] = 'dbback_theme';
345
//Try to give some more time
349
ini_set('memory_limit', '128M');
352
//////////////////////////////
353
// Lets Start making the data
354
//////////////////////////////
356
$data = '-- ////////////////////////////////////////////////////////////' . $crlf .
357
'-- ===========================================================' . $crlf .
358
'-- AEF MySQL Export' . $crlf .
359
'-- ===========================================================' . $crlf .
360
'-- AEF : Advanced Electron Forum ' . $crlf .
361
'-- Version : 1.1' . $crlf .
362
'-- ----------------------------------------------------------' . $crlf .
363
'-- Date: ' . date('jS F, Y') . '' . $crlf .
364
'-- Time: ' . date('g:i a') . '' . $crlf .
365
'-- PHP Version: ' . phpversion() . '' . $crlf .
366
'-- MySQL Version: ' . mysql_get_server_info() . '' . $crlf .
367
'-- ----------------------------------------------------------' . $crlf .
368
'-- ===========================================================' . $crlf .
369
'-- (C)AEF Group All Rights Reserved.' . $crlf .
370
'-- ===========================================================' . $crlf .
371
'-- ////////////////////////////////////////////////////////////' . $crlf .
373
'-- ----------------------------------------------------------' . $crlf .
377
foreach ($tables as $k => $v) {
379
if (isset($_POST['structure'])) {
381
$data .= tablestructure($dbtables[$v], $crlf);
384
if (isset($_POST['data'])) {
386
$data .= tabledata($dbtables[$v], $crlf);
391
'-- ----------------------------------------------------------' . $crlf .
396
if ($compression == 'zip') {
398
} elseif ($compression == 'gzip') {
400
} elseif ($compression == 'bzip') {
404
$filename = $globals['database'] . '(' . date('Y-m-d') . ').sql' . $ext;
408
if ($compression == 'zip') {
410
if (function_exists('gzcompress')) {
414
$zip->add_file($data, substr($filename, 0, -4));
416
$data = $zip->file();
419
$error[] = $l['no_zip'];
421
} elseif ($compression == 'gzip') {
423
if (function_exists('gzencode')) {
425
$data = gzencode($data);
428
$error[] = $l['no_gzip'];
430
} elseif ($compression == 'bzip') {
432
if (function_exists('bzcompress')) {
434
$data = bzcompress($data);
438
$error[] = $l['no_bzip'];
442
$error[] = $l['no_bzip'];
447
//on error call the form
448
if (!empty($error)) {
449
$theme['call_theme_func'] = 'dbback_theme';
454
//What to do ? Output the file
455
if (empty($localpath)) {
457
$globals['stop_buffer_process'] = true;
460
switch ($compression) {
462
header("Content-Type: application/zip");
465
header("Content-Type: application/x-bzip2");
468
header("Content-Type: application/x-gzip");
474
//There are so many headers/headaches
475
header('Content-Disposition: attachment; filename="' . $filename . '"');
476
header('Accept-Ranges: bytes');
477
header('Content-Encoding: none');
478
header("Content-Length: " . aefstrlen($data));
479
header("Content-Transfer-Encoding: binary");
480
header('Content-Type: application/octetstream');
481
header("Cache-Control: no-cache, must-revalidate, private");
482
header('Connection: close');
488
if (writefile($localpath . '/' . $filename, $data, 0)) {
491
reportmessage($l['backup_ok'], $l['backup_created_ok'], '', $l['backup_created_ok_exp']);
496
$error[] = $l['errors_writing'];
498
//on error call the form
499
if (!empty($error)) {
500
$theme['call_theme_func'] = 'dbback_theme';
509
$theme['call_theme_func'] = 'dbback_theme';
513
//Creates the tables structure
514
function tablestructure($table, $crlf) {
520
'-- Table structure for table ' . backquotes($table) . $crlf .
523
//Put a drop table statement
524
if (isset($_POST['droptable'])) {
526
$str .= $crlf . 'DROP TABLE IF EXISTS ' . backquotes($table) . ';' . $crlf;
530
//SET backquotes or not
531
if (isset($_POST['backquotes'])) {
533
$qresult = makequery("SET SQL_QUOTE_SHOW_CREATE = 1", false);
536
$qresult = makequery("SET SQL_QUOTE_SHOW_CREATE = 0", false);
539
//MySQL Can create its own tables
540
$qresult = makequery("SHOW CREATE TABLE " . $table);
542
if (mysql_num_rows($qresult) > 0) {
544
$row = mysql_fetch_row($qresult);
546
$create_query = $row[1];
551
//Convert end of line chars to one that we want
552
if (strpos($create_query, "(\r\n ")) {
554
$create_query = str_replace("\r\n", $crlf, $create_query);
555
} elseif (strpos($create_query, "(\n ")) {
557
$create_query = str_replace("\n", $crlf, $create_query);
558
} elseif (strpos($create_query, "(\r ")) {
560
$create_query = str_replace("\r", $crlf, $create_query);
564
//Should we use IF NOT EXISTS?
565
if (isset($_POST['ifnotexists'])) {
567
$create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);
571
mysql_free_result($qresult);
573
$str .= $create_query;
576
//Autoincrement value
577
$qresult = makequery("SHOW TABLE STATUS LIKE '" . strtr($table, array('_' => '\\_', '%' => '\\%')) . "'", false);
579
$row = mysql_fetch_assoc($qresult);
581
//remove a possible "AUTO_INCREMENT = value" clause
582
//that could be there starting with MySQL 5.0.24
583
$str = preg_replace('/AUTO_INCREMENT\s*=\s*([0-9])+/', '', $str);
585
if (isset($_POST['autoincrement']) && !empty($row['Auto_increment'])) {
587
$auto_increment = ' AUTO_INCREMENT=' . $row['Auto_increment'] . ' ';
589
$str .= $auto_increment;
592
$str .= ';' . $crlf . $crlf;
597
//Gives the Tables Data of a table
598
function tabledata($table, $crlf) {
606
if (isset($_POST['delayed'])) {
608
$delayed = " DELAYED";
611
if (isset($_POST['ignore'])) {
616
//Get everything from the table
617
$qresult = makequery("SELECT /*!40001 SQL_NO_CACHE */ * FROM " . $table);
620
for ($i = 1; $i <= mysql_num_rows($qresult); $i++) {
622
$row = mysql_fetch_assoc($qresult);
624
foreach ($row as $k => $v) {
626
if (is_numeric($v)) {
631
$row[$k] = "'" . mysql_real_escape_string($v) . "'";
635
$data .= "INSERT " . $delayed . $ignore . "INTO " . backquotes($table) . " VALUES (" . implode(', ', $row) . ");" . $crlf;
640
mysql_free_result($result);
642
//Is the data string empty
650
'-- Data for table ' . backquotes($table) . $crlf .
657
function backquotes($string) {
659
if (isset($_POST['backquotes'])) {
661
$string = '`' . $string . '`';