-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqa-backup.php
478 lines (407 loc) · 13.7 KB
/
qa-backup.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
/*
Question2Answer (c) 2011, Gideon Greenspan
http://www.question2answer.org/
File: qa-plugin/Kielce-backup/qa-backup.php
Version: (see qa-plugin.php)
Description: Module class for DB Backup plugin
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.
More about this license: http://www.question2answer.org/license.php
*/
class qa_backup {
var $urltoroot;
var $currentUrlDir;
var $savedpath = "";
var $error = "";
var $msg = "";
var $backupDirUrl ="";
var $fileCount = 0;
var $listedFiles = "";
var $backupDir = "";
function load_module($directory, $urltoroot)
{
$this->urltoroot=$urltoroot;
$this->currentUrlDir = $directory;
}
function option_default($option)
{
if ($option=='backup_file_max_size') {
return 1048576;
}
if ($option=='backup_only_qa_tables_enable') {
return true;
}
}
function admin_form(&$qa_content)
{
require_once QA_INCLUDE_DIR.'qa-app-blobs.php';
global $qa_root_url_relative, $QA_CONST_PATH_MAP;
$this->backupDirUrl = $qa_root_url_relative . "qa-content/backup/";
$this->backupDir = QA_BASE_DIR."qa-content/backup/";
if (!is_dir($this->backupDir))
mkdir($this->backupDir, 0755);
if (qa_clicked('backup_send_upload_file')) {
$this->saveChanges();
if (isset($_FILES["backup_upload_file"]))
$this->uploadFile();
}
if (qa_clicked('backup_export_button')) {
$this->saveChanges();
$this->doBackup();
$this->msg = 'Database Backup done.';
}
if (qa_clicked('backup_delete_files')) {
$this->saveChanges();
$this->doDelete();
}
$this->listedFiles = $this->listFiles();
if (qa_clicked('backup_import_button')) {
$this->saveChanges();
$this->doImport();
$this->msg = 'Database Import done';
}
return array(
'ok' => $this->error ? '<span style="color:#f00;">ERROR: '.$this->error.'</span>' : ($this->msg ? $this->msg : null),
'fields' => array(
array(
'label' => $this->listedFiles,
'type' => 'static',
'value' => '<input type="file" name="backup_upload_file" onmouseout="this.className=\'qa-form-tall-button qa-form-tall-button-0\';" onmouseover="this.className=\'qa-form-tall-hover qa-form-tall-hover-0\';" class="qa-form-tall-button qa-form-tall-button-0" /> '.
'<input type="submit" value="Upload to server" name="backup_send_upload_file" onmouseout="this.className=\'qa-form-tall-button qa-form-tall-button-0\';" onmouseover="this.className=\'qa-form-tall-hover qa-form-tall-hover-0\';" onclick="bck_t=\'\'" class="qa-form-tall-button qa-form-tall-button-0" /><br />'.
'<input type="submit" value="Delete all files" name="backup_delete_files" onmouseout="this.className=\'qa-form-tall-button qa-form-tall-button-0\';" onmouseover="this.className=\'qa-form-tall-hover qa-form-tall-hover-0\';" onclick="bck_t=\'delete all files from a backup folder\'" class="qa-form-tall-button qa-form-tall-button-0" />',
'tags' => 'NAME="backup_importFile"',
),
array(
'label' => 'Compress backups.',
'type' => 'checkbox',
'value' => qa_opt('backup_compress_enable'),
'tags' => 'NAME="backup_compress_enable"',
),
array(
'label' => 'Backup Q2A tables only - those with `'.QA_MYSQL_TABLE_PREFIX.'` prefix. <br />If you uncheck this option, you will backup whole batabase. <br />(Useful for more than one Q2A instance in one database).',
'type' => 'checkbox',
'value' => qa_opt('backup_only_qa_tables_enable'),
'tags' => 'NAME="backup_only_qa_tables_enable"',
),
// Not yet implemented !
// array(
// 'id' => 'wysiwyg_editor_upload_max_size_display',
// 'label' => 'Maximum single backup file size in MB:',
// 'type' => 'number',
// 'value' => $this->bytes_to_mega_html(qa_opt('backup_file_max_size')),
// 'tags' => 'NAME="backup_file_max_size_field"',
// ),
array(
'label' => '<span style="color:#f99; font-size:20px; text-align:center;">Caution! Use carefully. Risk of losing all your data.<br />'.
'<span style="font-size:15px;">(While restoring, it is always recommended to <a href="'.$this->urltoroot.'README.rst" target="backup_readme">restore in a safe way</a>).<br />In case of problems, see <a href="'.$this->urltoroot.'README.rst" target="backup_readme">TROUBLESHOOTING</a> section.</span></span>',
'type' => 'custom',
'tags' => 'NAME="backup_import_label"',
),
),
'buttons' => array(
array(
'label' => 'Do the backup !',
'tags' => 'NAME="backup_export_button" onmouseup="bck_t=\'\'"',
),
array(
'label' => 'Import selected file !',
'tags' => 'NAME="backup_import_button" onmouseup="bck_t=\'execute EVERYTHING that is in the selected file. If you import wrong file, you can have mess in your database or even data loss.\nIt is recommended to make a backup first.\n\nNOTE: Backup files done with this plugin delete previous data and then do the import\'"',
),
),
);
}
function saveChanges()
{
qa_opt('backup_compress_enable',(bool)qa_post_text('backup_compress_enable'));
qa_opt('backup_only_qa_tables_enable',(bool)qa_post_text('backup_only_qa_tables_enable'));
//qa_opt('backup_file_max_size', max(1048576*0.1, 1048576*(float)qa_post_text('backup_file_max_size_field')));
}
function doBackup()
{
$path = QA_BASE_DIR."qa-content/backup";
// create backup folder
if (!is_dir($path))
mkdir($path, 0755);
if (!is_dir($this->getDir($path)))
{
$this->error = "No such directory: ". $this->getDir($path);
return;
}
// create default index.php file to avoid that somebody see directory content.
if (!file_exists($path."/index.php"))
{
$indexFile = @fopen($path."/index.php", "w");
@fwrite($indexFile, '<?php header(\'Location: ../\'); ?>');
@fclose($indexFile);
}
$ext = qa_opt('backup_compress_enable') ? '.gz' : '.sql';
$path .= "/".$this->prefix()."-".date_format(new DateTime(), 'Y_m_d-H_i_s')."-".$this->suffix().$ext;
$oldMaintenance = qa_opt('site_maintenance');
qa_opt('site_maintenance', 1);
$this->getDumper($path)->doDump();
qa_opt('site_maintenance', $oldMaintenance);
}
function doImport()
{
if ($this->fileCount == 0)
{
$this->error = "No files found. Please, upload first.";
return;
}
$fileName = qa_post_text('file_name_selected');
if (!$fileName)
{
$this->error = "No files selected. Please, select a file.";
return;
}
$path = QA_BASE_DIR."qa-content/backup" . "/" . $fileName;
if (!file_exists($path))
{
$this->error = "File does not exist: ". $path;
return;
}
$oldMaintenance = qa_opt('site_maintenance');
qa_opt('site_maintenance', 1);
$this->launchFile($path, $this->error);
qa_opt('site_maintenance', $oldMaintenance);
}
function launchFile($path, &$error)
{
// read the file
$file = null;
$compressed = $this->endsWith(strtolower($path), ".gz");
if ($compressed)
$file = gzopen($path, "r");
else
$file = fopen($path, "r");
if(!$file)
{
$error = "Error opening data file: ". $path;
return;
}
$size = filesize($path);
if(!$size)
{
$error = "File is empty: ". $path;
return;
}
// debug
// $file2 = fopen($path."a", "w");
// fwrite($file2, $content);
// fclose($file2);
$lineNum = 0;
$lineseparator = "\n";
$lastIsComment = false;
$newLnPos = true;
$line = "";
$query = "";
while ( ! feof( $file ) )
{
$chunk = "";
if ($compressed)
$chunk = gzread($file, 1024);
else
$chunk = fgets( $file, 1024);
$chunk = str_replace("\r","",$chunk);
do
{
$newLnPos = strpos($chunk, "\n");
if ($newLnPos === 0 || $newLnPos > 0)
{
$line .= substr($chunk, 0, $newLnPos);
// omit first line (issue with byte order mark)
if ($lineNum>0 && $line && !$this->startsWith($line, "--"))
{
$query .= $line . " ";
if ($this->endsWith($line, ";"))
{
qa_db_query_raw($query); // execute query
$query = "";
}
}
$line = "";
$lineNum++;
$chunk = substr($chunk, $newLnPos+1);
}
else
{
$line .= $chunk;
}
} while($newLnPos === 0 || $newLnPos > 0);
}
if ($lineNum>0 && $line && !$this->startsWith($line, "--"))
{
$query .= $line;
}
if (strlen($query) > 0)
{
qa_db_query_raw($query); // execute query
$query = "";
}
$line = "";
if ($compressed)
gzclose($file);
else
fclose($file);
}
function uploadFile()
{
if ($_FILES["backup_upload_file"]["size"] > 2*1024*1024) {
$this->error = "File is too large (".$this->formatKB($_FILES["backup_upload_file"]["size"]).").";
return;
}
if (!$_FILES["backup_upload_file"]["name"]) {
$this->error = "No files selected. Please, select a file.<br />";
return;
}
if ($_FILES["backup_upload_file"]["type"] != "text/plain" && $_FILES["backup_upload_file"]["type"] != "application/octet-stream") {
$this->error = "Wrong file type: ".$_FILES["backup_upload_file"]["type"].". <br />You can only upload text or gz files!";
return;
}
if ($_FILES["backup_upload_file"]["error"]) {
$this->error = " code: ".$_FILES["backup_upload_file"]["error"]." - enexpected one...<br />";
return;
}
$dir = $this->backupDir;
if (file_exists($dir . $_FILES["backup_upload_file"]["name"]))
@unlink($dir . $_FILES["backup_upload_file"]["name"]);
if (file_exists($dir . $_FILES["backup_upload_file"]["name"]))
{
$this->error .= "Could not delete file ".$_FILES["backup_upload_file"]["name"] . ". ";
}
else
{
move_uploaded_file($_FILES["backup_upload_file"]["tmp_name"],
$dir . $_FILES["backup_upload_file"]["name"]);
$this->msg = "File uploaded to: <br />" . $dir . $_FILES["backup_upload_file"]["name"] . "<br />";
$this->msg .= "(type: " . $_FILES["backup_upload_file"]["type"] . ", ";
$this->msg .= "size: " . ceil(($_FILES["backup_upload_file"]["size"] / 1024)) . " KB)<br />";
}
}
function doDelete()
{
$fileArr = $this->getFiles($this->backupDir);
if (count($fileArr) == 0)
{
$this->msg = "There are no files to delete.";
return;
}
for ($i=0; $i < count($fileArr); $i++)
{
if (file_exists($this->backupDir.$fileArr[$i]))
@unlink($this->backupDir.$fileArr[$i]);
if (file_exists($this->backupDir.$fileArr[$i]))
$this->error .= $this->backupDir.$fileArr[$i] . "<br />";
}
if (strlen($this->error) > 0)
$this->error = "Can't delete files (are they locked by another process?): <br />".$this->error;
else
$this->msg = 'Files deleted';
}
function getFiles($dirpath)
{
$myDirectory = opendir($dirpath);
while($entryName = readdir($myDirectory))
$dirArray[] = $entryName;
closedir($myDirectory);
sort($dirArray);
$indexCount = count($dirArray);
$filesArr = array();
for ($index=0; $index < $indexCount; $index++)
{
if (substr("$dirArray[$index]", 0, 1) != "." // don't list hidden files
&& !$this->endsWith($dirArray[$index], "index.php") // ignore index.php
&& !is_dir($dirpath.$dirArray[$index]) // don't list directories
)
{
$filesArr[] = $dirArray[$index];
}
}
return $filesArr;
}
function getDumper($path)
{
require_once('mysqldump.php');
$tabPrefix = qa_opt('backup_only_qa_tables_enable') ? QA_MYSQL_TABLE_PREFIX : "";
return new MySQLDump(QA_FINAL_MYSQL_DATABASE, $path, qa_opt('backup_compress_enable'), false, $tabPrefix); // db, filepath, compress, hex, table_prefix, bufferSizeKB
// , qa_opt('backup_file_max_size')/1024.0
}
function listFiles()
{
$fileArr = $this->getFiles($this->backupDir);
$strFiles = "";
for ($i=0; $i < count($fileArr); $i++)
{
$strFiles .= "<input type=\"radio\" name=\"file_name_selected\" value=\"$fileArr[$i]\">".
"<a href=\"".$this->backupDirUrl.$fileArr[$i]."\" target=\"_blank\">$fileArr[$i]</a> (".$this->formatKB(@filesize($this->backupDir.$fileArr[$i])).")".
"</input><br />";
}
$this->fileCount = count($fileArr);
$res = "";
if ($this->fileCount > 0)
{
$res .= '<div style="color:#000; font-size:12px; text-align:left; width:500px;">';
$res .= 'Files in backup folder <span style="color:#f99">(download them and delete fast before someone sees your data!)</span>:<br />';
$res .= $strFiles;
$res .= "</div>";
}
return $res;
}
function formatKB($number)
{
if (!$number)
return 0;
$number = ceil($number/1024);
return number_format($number, 0, ",", " ")." KB";
}
function getDir($path)
{
$res = "";
$slash1 = strrpos($path, "\\");
$slash2 = strrpos($path, "/");
$maxSlash = -1;
if ($slash1) $maxSlash = $slash1;
if ($slash2 && $slash2>$maxSlash) $maxSlash = $slash2;
if ($maxSlash != -1)
$res = substr($path, 0, $maxSlash+1);
return $res;
}
function startsWith($string, $search)
{
return (strncmp($string, $search, strlen($search)) == 0);
}
function endsWith($string, $search)
{
$length = strlen($search);
$start = $length * -1; //negative
return (substr($string, $start) === $search);
}
function prefix()
{
$prefix = qa_opt('site_title');
$prefix = strtolower(@ereg_replace("[^A-Za-z0-9]", "", $prefix));
if (strlen($prefix) > 8)
$prefix = substr($prefix, 0, 8);
if (!$prefix)
$prefix = "q2a";
return $prefix;
}
function suffix()
{
return substr(md5(microtime()), 0, 5);
}
function bytes_to_mega_html($bytes)
{
return qa_html(number_format($bytes/1048576, 1));
}
};
/*
Omit PHP closing tag to help avoid accidental output
*/