~cruzjbishop/anelectron/trunk

« back to all changes in this revision

Viewing changes to main/admin/backup.php

  • Committer: Cruz Julian Bishop
  • Date: 2012-03-18 22:16:46 UTC
  • Revision ID: cruzjbishop@gmail.com-20120318221646-se8p54bd2g7i936n
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
//////////////////////////////////////////////////////////////
 
4
//===========================================================
 
5
// backup.php(Admin)
 
6
//===========================================================
 
7
// AEF : Advanced Electron Forum
 
8
// Version : 1.0.9
 
9
// Inspired by Pulkit and taken over by Electron
 
10
// ----------------------------------------------------------
 
11
// Started by: Electron, Ronak Gupta, Pulkit Gupta
 
12
// Date:       23rd Jan 2006
 
13
// Time:       15:00 hrs
 
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
//////////////////////////////////////////////////////////////
 
22
 
 
23
if (!defined('AEF')) {
 
24
 
 
25
    die('Hacking Attempt');
 
26
}
 
27
 
 
28
function backup() {
 
29
 
 
30
    global $user, $conn, $dbtables, $logged_in, $globals, $l, $AEF_SESS, $theme;
 
31
 
 
32
    if (!load_lang('admin/backup')) {
 
33
 
 
34
        return false;
 
35
    }
 
36
 
 
37
    //The name of the file
 
38
    $theme['init_theme'] = 'admin/backup';
 
39
 
 
40
    //The name of the Page
 
41
    $theme['init_theme_name'] = 'Admin Center - Backup';
 
42
 
 
43
    //Array of functions to initialize
 
44
    $theme['init_theme_func'] = array('fileback_theme', 'dbback_theme');
 
45
 
 
46
    //My activity
 
47
    $globals['last_activity'] = 'aback';
 
48
 
 
49
 
 
50
    //If a second Admin act is set then go by that
 
51
    if (isset($_GET['seadact']) && trim($_GET['seadact']) !== "") {
 
52
 
 
53
        $seadact = inputsec(htmlizer(trim($_GET['seadact'])));
 
54
    } else {
 
55
 
 
56
        $seadact = "";
 
57
    }
 
58
 
 
59
 
 
60
    //The switch handler
 
61
    switch ($seadact) {
 
62
 
 
63
        //The form for backing up files
 
64
        default:
 
65
        case 'fileback':
 
66
            fileback();
 
67
            break;
 
68
 
 
69
        //The form for backing up the database
 
70
        case 'dbback':
 
71
            dbback();
 
72
            break;
 
73
    }
 
74
}
 
75
 
 
76
//Function to backup files and folders
 
77
function fileback() {
 
78
 
 
79
    global $user, $conn, $dbtables, $logged_in, $globals, $l, $AEF_SESS, $theme;
 
80
    global $error;
 
81
 
 
82
 
 
83
    /////////////////////////////
 
84
    // Define the necessary VARS
 
85
    /////////////////////////////
 
86
 
 
87
    $error = array();
 
88
 
 
89
    //Profile Settings
 
90
    $folderpath = ''; //The folder to backup
 
91
 
 
92
    $compression = ''; //The compression method
 
93
 
 
94
    $localpath = 0; //The path if we have to write it on the server itself
 
95
 
 
96
    if (isset($_POST['startfileback'])) {
 
97
 
 
98
        //The folderpath
 
99
        if (!(isset($_POST['folderpath'])) || (trim($_POST['folderpath']) == "")) {
 
100
 
 
101
            $error[] = $l['no_path'];
 
102
        } else {
 
103
 
 
104
            $folderpath = inputsec(htmlizer(trim($_POST['folderpath'])));
 
105
 
 
106
            $folderpath = rtrim($folderpath, '/\\');
 
107
 
 
108
            if (!is_readable($folderpath)) {
 
109
 
 
110
                $error[] = $l['no_readable_path'];
 
111
            }
 
112
        }
 
113
 
 
114
        //on error call the form
 
115
        if (!empty($error)) {
 
116
            $theme['call_theme_func'] = 'fileback_theme';
 
117
            return false;
 
118
        }
 
119
 
 
120
 
 
121
        //The compression method
 
122
        if (!(isset($_POST['compression'])) || (trim($_POST['compression']) == "")) {
 
123
 
 
124
            $error[] = $l['no_compression'];
 
125
        } else {
 
126
 
 
127
            $compression = inputsec(htmlizer(trim($_POST['compression'])));
 
128
 
 
129
            if (!in_array($compression, array('zip', 'tar', 'tgz', 'tbz'))) {
 
130
 
 
131
                $error[] = $l['compression_invalid'];
 
132
            }
 
133
        }
 
134
 
 
135
        //on error call the form
 
136
        if (!empty($error)) {
 
137
            $theme['call_theme_func'] = 'fileback_theme';
 
138
            return false;
 
139
        }
 
140
 
 
141
        //Are we to store locally
 
142
        if (isset($_POST['localpath']) && (trim($_POST['localpath']) != "")) {
 
143
 
 
144
            $localpath = inputsec(htmlizer(trim($_POST['localpath'])));
 
145
 
 
146
            $localpath = rtrim($localpath, '/\\');
 
147
 
 
148
            if (!is_writable($localpath)) {
 
149
 
 
150
                $error[] = $l['unaccessible_local_path'];
 
151
            }
 
152
        }
 
153
 
 
154
        //on error call the form
 
155
        if (!empty($error)) {
 
156
            $theme['call_theme_func'] = 'fileback_theme';
 
157
            return false;
 
158
        }
 
159
 
 
160
        //Try to give some more time
 
161
        set_time_limit(300);
 
162
 
 
163
        //Memory limit
 
164
        ini_set('memory_limit', '128M');
 
165
 
 
166
 
 
167
        $filename = basename($folderpath) . '(' . date('Y-m-d') . ')';
 
168
 
 
169
        $data = compress($folderpath, $filename, $compression);
 
170
 
 
171
        //Did it compress
 
172
        if (empty($data)) {
 
173
 
 
174
            $error[] = $l['errors_compressing_data'];
 
175
 
 
176
            //on error call the form
 
177
            if (!empty($error)) {
 
178
                $theme['call_theme_func'] = 'fileback_theme';
 
179
                return false;
 
180
            }
 
181
        }
 
182
 
 
183
 
 
184
        //What to do ? Output the file
 
185
        if (empty($localpath)) {
 
186
 
 
187
            $globals['stop_buffer_process'] = true;
 
188
 
 
189
            //Give the headers
 
190
            switch ($compression) {
 
191
                case "zip":
 
192
                    header("Content-Type: application/zip");
 
193
                    break;
 
194
                case "tbz":
 
195
                    header("Content-Type: application/x-bzip2");
 
196
                    break;
 
197
                case "tbz":
 
198
                    header("Content-Type: application/x-gzip");
 
199
                    break;
 
200
                case "tar":
 
201
                    header("Content-Type: application/x-tar");
 
202
                    break;
 
203
            }
 
204
 
 
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');
 
214
            echo $data;
 
215
 
 
216
            //No - Store it
 
217
        } else {
 
218
 
 
219
            if (writefile($localpath . '/' . $filename . '.' . $compression, $data, 0)) {
 
220
 
 
221
                //Give a message
 
222
                reportmessage($l['backup_ok'], $l['backup_created_ok'], '', $l['backup_created_ok_exp']);
 
223
 
 
224
                return true;
 
225
            } else {
 
226
 
 
227
                $error[] = $l['errors_writing'];
 
228
 
 
229
                //on error call the form
 
230
                if (!empty($error)) {
 
231
                    $theme['call_theme_func'] = 'fileback_theme';
 
232
                    return false;
 
233
                }
 
234
            }
 
235
        }
 
236
 
 
237
        return true;
 
238
    } else {
 
239
 
 
240
        $theme['call_theme_func'] = 'fileback_theme';
 
241
    }
 
242
}
 
243
 
 
244
//Function to manage Avatar settings
 
245
function dbback() {
 
246
 
 
247
    global $user, $conn, $dbtables, $logged_in, $globals, $l, $AEF_SESS, $theme;
 
248
    global $error;
 
249
 
 
250
 
 
251
    /////////////////////////////
 
252
    // Define the necessary VARS
 
253
    /////////////////////////////
 
254
 
 
255
    $error = array();
 
256
 
 
257
    $tables = array(); //The name of the tables
 
258
 
 
259
    $localpath = ''; //Store Locally
 
260
 
 
261
    $crlf = "\n"; //MySQL loves \n on all platforms
 
262
 
 
263
    $data = '';
 
264
 
 
265
    if (isset($_POST['dbback'])) {
 
266
 
 
267
        //r_print($_POST);
 
268
        //Check the Avatar Directory
 
269
        if (!(isset($_POST['tables'])) || !is_array($_POST['tables'])) {
 
270
 
 
271
            $error[] = $l['no_tables_specified'];
 
272
        } else {
 
273
 
 
274
            $tables = $_POST['tables'];
 
275
 
 
276
            $keys = array_keys($dbtables);
 
277
 
 
278
            foreach ($tables as $k => $v) {
 
279
 
 
280
                $tables[$k] = $v = trim($v);
 
281
 
 
282
                //Is it a valid table
 
283
                if (!in_array($v, $keys)) {
 
284
 
 
285
                    $error[] = $l['tables_invalid'];
 
286
                    break;
 
287
                }
 
288
            }
 
289
        }
 
290
 
 
291
        //on error call the form
 
292
        if (!empty($error)) {
 
293
            $theme['call_theme_func'] = 'dbback_theme';
 
294
            return false;
 
295
        }
 
296
 
 
297
 
 
298
        //The compression method
 
299
        if (!(isset($_POST['compression'])) || (trim($_POST['compression']) == "")) {
 
300
 
 
301
            $error[] = $l['no_compression'];
 
302
        } else {
 
303
 
 
304
            $compression = inputsec(htmlizer(trim($_POST['compression'])));
 
305
 
 
306
            if (!in_array($compression, array('none', 'zip', 'gzip', 'bzip'))) {
 
307
 
 
308
                $error[] = $l['compression_invalid'];
 
309
            }
 
310
        }
 
311
 
 
312
 
 
313
        //Are we to store locally
 
314
        if (isset($_POST['localpath']) && (trim($_POST['localpath']) != "")) {
 
315
 
 
316
            $localpath = inputsec(htmlizer(trim($_POST['localpath'])));
 
317
 
 
318
            $localpath = rtrim($localpath, '/\\');
 
319
 
 
320
            if (!is_writable($localpath)) {
 
321
 
 
322
                $error[] = $l['unaccessible_local_path'];
 
323
            }
 
324
        }
 
325
 
 
326
        //on error call the form
 
327
        if (!empty($error)) {
 
328
            $theme['call_theme_func'] = 'dbback_theme';
 
329
            return false;
 
330
        }
 
331
 
 
332
        //Select atlest the Data or structure
 
333
        if (!isset($_POST['data']) && !isset($_POST['structure'])) {
 
334
 
 
335
            $error[] = $l['select_structure_data'];
 
336
 
 
337
            //on error call the form
 
338
            if (!empty($error)) {
 
339
                $theme['call_theme_func'] = 'dbback_theme';
 
340
                return false;
 
341
            }
 
342
        }
 
343
 
 
344
 
 
345
        //Try to give some more time
 
346
        set_time_limit(450);
 
347
 
 
348
        //Memory limit
 
349
        ini_set('memory_limit', '128M');
 
350
 
 
351
 
 
352
        //////////////////////////////
 
353
        // Lets Start making the data
 
354
        //////////////////////////////
 
355
        //AEF Headers
 
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 .
 
372
                $crlf .
 
373
                '-- ----------------------------------------------------------' . $crlf .
 
374
                $crlf .
 
375
                $crlf;
 
376
 
 
377
        foreach ($tables as $k => $v) {
 
378
 
 
379
            if (isset($_POST['structure'])) {
 
380
 
 
381
                $data .= tablestructure($dbtables[$v], $crlf);
 
382
            }
 
383
 
 
384
            if (isset($_POST['data'])) {
 
385
 
 
386
                $data .= tabledata($dbtables[$v], $crlf);
 
387
            }
 
388
 
 
389
            //Ending this table
 
390
            $data .= $crlf .
 
391
                    '-- ----------------------------------------------------------' . $crlf .
 
392
                    $crlf;
 
393
        }
 
394
 
 
395
        $ext = '';
 
396
        if ($compression == 'zip') {
 
397
            $ext = '.zip';
 
398
        } elseif ($compression == 'gzip') {
 
399
            $ext = '.gz';
 
400
        } elseif ($compression == 'bzip') {
 
401
            $ext = '.bz';
 
402
        }
 
403
 
 
404
        $filename = $globals['database'] . '(' . date('Y-m-d') . ').sql' . $ext;
 
405
 
 
406
 
 
407
        //Compress
 
408
        if ($compression == 'zip') {
 
409
 
 
410
            if (function_exists('gzcompress')) {
 
411
 
 
412
                $zip = zipclass();
 
413
 
 
414
                $zip->add_file($data, substr($filename, 0, -4));
 
415
 
 
416
                $data = $zip->file();
 
417
            } else {
 
418
 
 
419
                $error[] = $l['no_zip'];
 
420
            }
 
421
        } elseif ($compression == 'gzip') {
 
422
 
 
423
            if (function_exists('gzencode')) {
 
424
 
 
425
                $data = gzencode($data);
 
426
            } else {
 
427
 
 
428
                $error[] = $l['no_gzip'];
 
429
            }
 
430
        } elseif ($compression == 'bzip') {
 
431
 
 
432
            if (function_exists('bzcompress')) {
 
433
 
 
434
                $data = bzcompress($data);
 
435
 
 
436
                if ($data === -8) {
 
437
 
 
438
                    $error[] = $l['no_bzip'];
 
439
                }
 
440
            } else {
 
441
 
 
442
                $error[] = $l['no_bzip'];
 
443
            }
 
444
        }
 
445
 
 
446
 
 
447
        //on error call the form
 
448
        if (!empty($error)) {
 
449
            $theme['call_theme_func'] = 'dbback_theme';
 
450
            return false;
 
451
        }
 
452
 
 
453
 
 
454
        //What to do ? Output the file
 
455
        if (empty($localpath)) {
 
456
 
 
457
            $globals['stop_buffer_process'] = true;
 
458
 
 
459
            //Give the headers
 
460
            switch ($compression) {
 
461
                case "zip":
 
462
                    header("Content-Type: application/zip");
 
463
                    break;
 
464
                case "bzip":
 
465
                    header("Content-Type: application/x-bzip2");
 
466
                    break;
 
467
                case "gzip":
 
468
                    header("Content-Type: application/x-gzip");
 
469
                    break;
 
470
                case "none":
 
471
                    break;
 
472
            }
 
473
 
 
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');
 
483
            echo $data;
 
484
 
 
485
            //No - Store it
 
486
        } else {
 
487
 
 
488
            if (writefile($localpath . '/' . $filename, $data, 0)) {
 
489
 
 
490
                //Give a message
 
491
                reportmessage($l['backup_ok'], $l['backup_created_ok'], '', $l['backup_created_ok_exp']);
 
492
 
 
493
                return true;
 
494
            } else {
 
495
 
 
496
                $error[] = $l['errors_writing'];
 
497
 
 
498
                //on error call the form
 
499
                if (!empty($error)) {
 
500
                    $theme['call_theme_func'] = 'dbback_theme';
 
501
                    return false;
 
502
                }
 
503
            }
 
504
        }
 
505
 
 
506
        return true;
 
507
    } else {
 
508
 
 
509
        $theme['call_theme_func'] = 'dbback_theme';
 
510
    }
 
511
}
 
512
 
 
513
//Creates the tables structure
 
514
function tablestructure($table, $crlf) {
 
515
 
 
516
    $create_query = '';
 
517
 
 
518
    $str = $crlf .
 
519
            '-- ' . $crlf .
 
520
            '-- Table structure for table ' . backquotes($table) . $crlf .
 
521
            '-- ' . $crlf;
 
522
 
 
523
    //Put a drop table statement
 
524
    if (isset($_POST['droptable'])) {
 
525
 
 
526
        $str .= $crlf . 'DROP TABLE IF EXISTS ' . backquotes($table) . ';' . $crlf;
 
527
    }
 
528
 
 
529
   
 
530
    //SET backquotes or not
 
531
    if (isset($_POST['backquotes'])) {
 
532
 
 
533
        $qresult = makequery("SET SQL_QUOTE_SHOW_CREATE = 1", false);
 
534
    } else {
 
535
 
 
536
        $qresult = makequery("SET SQL_QUOTE_SHOW_CREATE = 0", false);
 
537
    }
 
538
 
 
539
    //MySQL Can create its own tables
 
540
    $qresult = makequery("SHOW CREATE TABLE " . $table);
 
541
 
 
542
    if (mysql_num_rows($qresult) > 0) {
 
543
 
 
544
        $row = mysql_fetch_row($qresult);
 
545
 
 
546
        $create_query = $row[1];
 
547
 
 
548
        unset($row);
 
549
 
 
550
 
 
551
        //Convert end of line chars to one that we want
 
552
        if (strpos($create_query, "(\r\n ")) {
 
553
 
 
554
            $create_query = str_replace("\r\n", $crlf, $create_query);
 
555
        } elseif (strpos($create_query, "(\n ")) {
 
556
 
 
557
            $create_query = str_replace("\n", $crlf, $create_query);
 
558
        } elseif (strpos($create_query, "(\r ")) {
 
559
 
 
560
            $create_query = str_replace("\r", $crlf, $create_query);
 
561
        }
 
562
 
 
563
 
 
564
        //Should we use IF NOT EXISTS?
 
565
        if (isset($_POST['ifnotexists'])) {
 
566
 
 
567
            $create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);
 
568
        }
 
569
    }
 
570
 
 
571
    mysql_free_result($qresult);
 
572
 
 
573
    $str .= $create_query;
 
574
 
 
575
 
 
576
    //Autoincrement value
 
577
    $qresult = makequery("SHOW TABLE STATUS    LIKE '" . strtr($table, array('_' => '\\_', '%' => '\\%')) . "'", false);
 
578
 
 
579
    $row = mysql_fetch_assoc($qresult);
 
580
 
 
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);
 
584
 
 
585
    if (isset($_POST['autoincrement']) && !empty($row['Auto_increment'])) {
 
586
 
 
587
        $auto_increment = ' AUTO_INCREMENT=' . $row['Auto_increment'] . ' ';
 
588
 
 
589
        $str .= $auto_increment;
 
590
    }
 
591
 
 
592
    $str .= ';' . $crlf . $crlf;
 
593
 
 
594
    return $str;
 
595
}
 
596
 
 
597
//Gives the Tables Data of a table
 
598
function tabledata($table, $crlf) {
 
599
 
 
600
    $data = '';
 
601
 
 
602
    $delayed = '';
 
603
 
 
604
    $ignore = '';
 
605
 
 
606
    if (isset($_POST['delayed'])) {
 
607
 
 
608
        $delayed = " DELAYED";
 
609
    }
 
610
 
 
611
    if (isset($_POST['ignore'])) {
 
612
 
 
613
        $ignore = " IGNORE";
 
614
    }
 
615
 
 
616
    //Get everything from the table
 
617
    $qresult = makequery("SELECT /*!40001 SQL_NO_CACHE */ * FROM " . $table);
 
618
 
 
619
    //Loop
 
620
    for ($i = 1; $i <= mysql_num_rows($qresult); $i++) {
 
621
 
 
622
        $row = mysql_fetch_assoc($qresult);
 
623
 
 
624
        foreach ($row as $k => $v) {
 
625
 
 
626
            if (is_numeric($v)) {
 
627
 
 
628
                $row[$k] = $v;
 
629
            } else {
 
630
 
 
631
                $row[$k] = "'" . mysql_real_escape_string($v) . "'";
 
632
            }
 
633
        }
 
634
 
 
635
        $data .= "INSERT " . $delayed . $ignore . "INTO " . backquotes($table) . " VALUES (" . implode(', ', $row) . ");" . $crlf;
 
636
 
 
637
        unset($row);
 
638
    }
 
639
 
 
640
    mysql_free_result($result);
 
641
 
 
642
    //Is the data string empty
 
643
    if (empty($data)) {
 
644
 
 
645
        return '';
 
646
    }
 
647
 
 
648
    return $crlf .
 
649
    '-- ' . $crlf .
 
650
    '-- Data for table ' . backquotes($table) . $crlf .
 
651
    '-- ' . $crlf .
 
652
    $crlf .
 
653
    $data .
 
654
    $crlf;
 
655
}
 
656
 
 
657
function backquotes($string) {
 
658
 
 
659
    if (isset($_POST['backquotes'])) {
 
660
 
 
661
        $string = '`' . $string . '`';
 
662
    }
 
663
 
 
664
    return $string;
 
665
}
 
666
 
 
667
?>