forked from andreshg112/phpMyBackupPro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.php
executable file
·332 lines (279 loc) · 11.7 KB
/
import.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/*
+--------------------------------------------------------------------------+
| phpMyBackupPro |
+--------------------------------------------------------------------------+
| Copyright (c) 2004-2015 by Dirk Randhahn |
| http://www.phpMyBackupPro.net |
| version information can be found in definitions.php. |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.|
+--------------------------------------------------------------------------+
*/
require_once "login.php";
PMBP_print_header(preg_replace("#.*/#", "", $_SERVER['SCRIPT_NAME']));
// used variables
if (!isset($_GET['import'])) {
$_GET['import'] = false;
}
if (!isset($_GET['imp_ALL'])) {
$_GET['imp_ALL'] = false;
}
if (!isset($_GET['del'])) {
$_GET['del'] = false;
}
if (!isset($_GET['del_ALL'])) {
$_GET['del_ALL'] = false;
}
if (!isset($_GET['empty_ALL'])) {
$_GET['empty_ALL'] = false;
}
if (!isset($_GET['del_all'])) {
$_GET['del_all'] = false;
}
if (!isset($_GET['empty_all'])) {
$_GET['empty_all'] = false;
}
// if first use or no db-connection possible
if (!$con = PMBP_mysql_connect()) {
echo PMBP_addOutput(I_SQL_ERROR, "red");
}
// if importing sql
if ($_GET['import'] || $_GET['imp_ALL']) {
// get start time to calculate duration
if (function_exists("microtime")) {
$microtime = explode(" ", microtime());
$starttime = ($microtime[0] + $microtime[1]);
} else {
$starttime = time();
}
if ($_GET['import']) {
$import_files[] = $_GET['import'];
} else {
$all_files = PMBP_get_backup_files();
foreach ($all_files as $file) {
$db = PMBP_file_info("db", PMBP_EXPORT_DIR . $file);
$time = PMBP_file_info("time", PMBP_EXPORT_DIR . $file);
// define variable and set time
if (!isset($last_files[$db])) {
$last_files[$db][1] = -1;
}
// update time
if ($time > $last_files[$db][1]) {
$last_files[$db][0] = $file;
$last_files[$db][1] = $time;
}
}
if (isset($last_files)) {
foreach ($last_files as $last_file) {
$import_files[] = $last_file[0];
}
}
}
if (isset($import_files)) {
// set php timelimit
@set_time_limit($CONF['timelimit']);
@ignore_user_abort(true);
// import each file
foreach ($import_files as $import_file) {
// check if a db with the name of the backup file still exists and select it
$db = @mysqli_select_db($dbname = PMBP_file_info("db", PMBP_EXPORT_DIR . $import_file));
if (!$db) {
echo PMBP_addOutput(sprintf(PMBP_EX_NO_AVAILABLE, $dbname), "red");
} else {
$error = "";
// extract zip file
if (PMBP_file_info("comp", PMBP_EXPORT_DIR . $import_file) == "zip") {
include_once "pclzip.lib.php";
$pclzip = new PclZip(PMBP_EXPORT_DIR . $import_file);
$extracted_file = $pclzip->extractByIndex(0, PMBP_EXPORT_DIR, "");
if ($pclzip->error_code != 0) {
$error = "plczip: " . $pclzip->error_string . "<br>" . BI_BROKEN_ZIP . "!";
}
$import_file = substr($import_file, 0, strlen($import_file) - 4);
unset($pclzip);
}
// execute sql queries
if (!$error) {
if ($file = @gzopen(PMBP_EXPORT_DIR . $import_file, "r")) {
extract(PMBP_exec_sql($file, $con), EXTR_OVERWRITE);
} else {
$error = F_MAIL_3;
}
if ($file) {
@gzclose($file);
}
if (file_exists(PMBP_EXPORT_DIR . $import_file . ".zip")) {
unlink(PMBP_EXPORT_DIR . $import_file);
}
$import_file .= ".zip";
}
// echo success
if ($error) {
echo PMBP_addOutput($import_file . ": " . $error, "red_left");
} else {
echo PMBP_addOutput(IM_SUCCESS . " " . $insertQueries . " " . IM_TABLES . " " . $tableQueries . " " . IM_ROWS . " (" . $import_file . ")", "green");
}
}
}
}
// show execution duration
if (function_exists("microtime")) {
$microtime = explode(" ", microtime());
$endtime = ($microtime[0] + $microtime[1]);
} else {
$endtime = time();
}
echo "<div class=\"bold\">" . F_DURATION . ": " . number_format($endtime - $starttime, 3) . " " . F_SECONDS . "</div><br>\n";
}
// remove old backup files and get list of backup files
$all_files = PMBP_get_backup_files();
// delete ALL backup files if the link was clicked
if ($_GET['del_ALL']) {
$result = false;
if (is_array($all_files)) {
$result = PMBP_delete_backup_files($all_files);
}
if (!$result) {
echo PMBP_addOutput(B_DELETED_ALL, "green");
} else {
echo PMBP_addOutput($result, "red");
}
}
// empty ALL db if the link was clicked
if ($_GET['empty_ALL']) {
foreach (PMBP_get_db_list() as $dbname) {
empty_single_db($_GET['empty_all']);
}
}
// empty db if the link was clicked
if ($_GET['empty_all']) {
empty_single_db($_GET['empty_all']);
}
// delete all backup files of selected db if the link was clicked
if ($_GET['del_all']) {
if (is_array($all_files)) {
foreach ($all_files as $filename) {
if ($_GET['del_all'] == PMBP_file_info("db", PMBP_EXPORT_DIR . $filename)) {
$del_files[] = $filename;
}
}
}
$result = PMBP_delete_backup_files($del_files);
if (!$result) {
echo PMBP_addOutput(B_DELETED_ALL, "green");
} else {
echo PMBP_addOutput($result, "red");
}
}
// delete selected backup file if the link was clicked
if ($_GET['del']) {
$out = PMBP_delete_backup_files($_GET['del']);
if ($out) {
echo $out;
} else {
echo PMBP_addOutput(B_DELETED, "green");
}
}
// get new list of backup files
$all_files = PMBP_get_backup_files();
echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"0\" width=\"100%\">\n";
// list all backup files
if (is_array($all_files)) {
$last_backup = 0;
$size_sum = 0;
// print html table
foreach ($all_files as $filename) {
$file = PMBP_EXPORT_DIR . $filename;
// generate one row for the db name
if (!isset($printed_title[$db = PMBP_file_info("db", $file)])) {
$printed_title[$db] = true;
echo "<tr><th colspan=\"9\" class=\"active\">" . $db . " <span class=\"standard\">" . PMBP_confirm(B_CONF_EMPTY_DB, "import.php?empty_all=" . $db, "[" . B_EMPTY_DB . "]");
echo " " . PMBP_confirm(B_CONF_DEL_ALL, "import.php?del_all=" . $db, "[" . B_DELETE_ALL . "]") . "</span></th></tr>\n";
}
echo "<tr " . PMBP_change_color("#FFFFFF", "#000000") . ">\n<td class=\"list\">\n" . $filename . "</td>\n";
echo "<td class=\"list\">" . strftime($CONF['date'], $time = PMBP_file_info("time", $file)) . "</td>\n";
if ($time > $last_backup) {
$last_backup = $time;
}
$size_sum += $size = PMBP_file_info("size", $file);
$size = PMBP_size_type($size);
echo "<td class=\"list\">" . $size['value'] . " " . $size['type'] . "</td>\n";
echo "<td class=\"list\">" . PMBP_pop_up("file_info.php?file=" . $filename, B_INFO, "info") . "</td>\n";
echo "<td class=\"list\">" . PMBP_pop_up("get_file.php?view=" . $file, B_VIEW, "view") . "</td>\n";
echo "<td class=\"list\"><a href=\"get_file.php?download=true&view=" . $file . "\">" . B_DOWNLOAD . "</a></td>\n";
if ($con) {
echo "<td class=\"list\">" . PMBP_confirm(B_CONF_IMP, "import.php?import=" . $filename, B_IMPORT) . " \n";
} else {
echo "<td class=\"list\">" . B_IMPORT . " \n";
}
if ($con) {
echo "(" . PMBP_confirm(B_CONF_IMP, "big_import.php?fn=" . PMBP_EXPORT_DIR . $filename . "&dbn=" . $db, B_IMPORT_FRAG, "info") . ")</td>\n";
} else {
echo B_IMPORT_FRAG . "</td>\n";
}
echo "<td class=\"list\">" . PMBP_confirm(B_CONF_DEL, "import.php?del=" . $filename, B_DELETE) . "</td>\n</tr>\n";
}
// print delete ALL backups and empty ALL dbs links if backup files are available
$size_sum = PMBP_size_type($size_sum);
echo "<tr><td colspan=\"7\"><br><div class=\"bold\">" . B_SIZE_SUM . ": " . $size_sum['value'] . " " . $size_sum['type'] . " - " . B_LAST_BACKUP . ": " . strftime($CONF['date'], $last_backup) . "</div></td></tr>\n";
echo "<tr><td colspan=\"7\"><div class=\"bold\">" . PMBP_confirm(B_CONF_EMPT_ALL, "import.php?empty_ALL=TRUE", "[" . B_EMPTY_ALL . "]");
echo " " . PMBP_confirm(B_CONF_IMP_ALL, "import.php?imp_ALL=TRUE", "[" . B_IMPORT_ALL . "]") . "\n";
echo " " . PMBP_confirm(B_CONF_DEL_ALL_2, "import.php?del_ALL=TRUE", "[" . B_DELETE_ALL_2 . "]") . "<br> </div></td>\n</tr>\n";
} else {
// if there are no backup files
echo "<tr>\n<td><div class=\"bold\">" . B_NO_FILES . ".</div>\n</td>\n</tr>\n";
}
echo "</table>\n";
PMBP_print_footer();
function startTransaction()
{
mysqli_query($con, "SET AUTOCOMMIT=0");
mysqli_query($con, "START TRANSACTION");
mysqli_query($con, "SET FOREIGN_KEY_CHECKS=0");
}
function endTransaction()
{
mysqli_query($con, "SET FOREIGN_KEY_CHECKS=1");
mysqli_query($con, "COMMIT");
}
function empty_single_db($db_name)
{
$con = PMBP_mysql_connect();
$db = mysqli_select_db($con, $db_name);
if (!$db) {
echo PMBP_addOutput(sprintf(PMBP_EX_NO_AVAILABLE, $db_name), "red");
} else {
// $query = "SHOW TABLES";
// $res = mysqli_query($con, $query); // This is not doing anything
$tables_and_views = PMBP_list_tables_and_view($con);
startTransaction();
foreach ($tables_and_views['views'] as $view) {
mysqli_query($con, "drop view `" . $view['Name'] . "`");
echo mysqli_error($con);
}
foreach ($tables_and_views['tables'] as $table) {
mysqli_query($con, "drop table `" . $table['Name'] . "`");
echo mysqli_error($con);
}
endTransaction();
$error = mysqli_error($con);
if ($error) {
echo $error;
} else {
echo PMBP_addOutput(B_EMPTIED, "green");
}
}
}