/
backup.export.php
129 lines (116 loc) · 4.13 KB
/
backup.export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* The export function to import the phpMyFAQ backups
*
* PHP Version 5.3
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* @category phpMyFAQ
* @package Administration
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2009-2012 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link http://www.phpmyfaq.de
* @since 2009-08-18
*/
define('PMF_ROOT_DIR', dirname(__DIR__));
//
// Define the named constant used as a check by any included PHP file
//
define('IS_VALID_PHPMYFAQ', null);
//
// Autoload classes, prepend and start the PHP session
//
require_once PMF_ROOT_DIR.'/inc/Bootstrap.php';
PMF_Init::cleanRequest();
session_name(PMF_Session::PMF_COOKIE_NAME_AUTH);
session_start();
$action = PMF_Filter::filterInput(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
$auth = false;
$user = PMF_User_CurrentUser::getFromSession($faqConfig);
if ($user) {
$auth = true;
} else {
$user = null;
unset($user);
}
//
// Get current user rights
//
$permission = array();
if ($auth === true) {
// read all rights, set them FALSE
$allRights = $user->perm->getAllRightsData();
foreach ($allRights as $right) {
$permission[$right['name']] = false;
}
// check user rights, set them TRUE
$allUserRights = $user->perm->getAllUserRights($user->getUserId());
foreach ($allRights as $right) {
if (in_array($right['right_id'], $allUserRights))
$permission[$right['name']] = true;
}
}
header('Content-Type: application/octet-stream');
header('Pragma: no-cache');
if ($permission['backup']) {
$faqConfig->getDb()->getTableNames(PMF_Db::getTablePrefix());
$tables = $tableNames = $this->config->getDb()->getTableNames(PMF_Db::getTablePrefix());
$tablePrefix = (PMF_Db::getTablePrefix() !== '') ? PMF_Db::getTablePrefix() . '.phpmyfaq' : 'phpmyfaq';
$tableNames = '';
$majorVersion = substr($faqConfig->get('main.currentVersion'), 0, 3);
$dbHelper = new PMF_DB_Helper($faqConfig);
switch ($action) {
case 'backup_content' :
foreach ($tables as $table) {
if ((PMF_Db::getTablePrefix() . 'faqadminlog' == trim($table)) || (PMF_Db::getTablePrefix() . 'faqsessions' == trim($table))) {
continue;
}
$tableNames .= $table . ' ';
}
break;
case 'backup_logs' :
foreach ($tables as $table) {
if ((PMF_Db::getTablePrefix() . 'faqadminlog' == trim($table)) || (PMF_Db::getTablePrefix() . 'faqsessions' == trim($table))) {
$tableNames .= $table . ' ';
}
}
break;
}
$text[] = "-- pmf" . $majorVersion . ": " . $tableNames;
$text[] = "-- DO NOT REMOVE THE FIRST LINE!";
$text[] = "-- pmftableprefix: " . PMF_Db::getTablePrefix();
$text[] = "-- DO NOT REMOVE THE LINES ABOVE!";
$text[] = "-- Otherwise this backup will be broken.";
switch ($action) {
case 'backup_content' :
$header = sprintf(
'Content-Disposition: attachment; filename="%s-data.%s.sql',
$tablePrefix,
date("Y-m-d-H-i-s")
);
header($header);
foreach (explode(' ', $tableNames) as $table) {
print implode("\r\n", $text);
$text = $dbHelper->buildInsertQueries("SELECT * FROM " . $table, $table);
}
break;
case 'backup_logs' :
$header = sprintf(
'Content-Disposition: attachment; filename="%s-logs.%s.sql',
$tablePrefix,
date("Y-m-d-H-i-s")
);
header($header);
foreach (explode(' ', $tableNames) as $table) {
print implode("\r\n", $text);
$text = $dbHelper->buildInsertQueries("SELECT * FROM " . $table, $table);
}
break;
}
} else {
print $PMF_LANG['err_NotAuth'];
}