Skip to content

Commit df55bd3

Browse files
authored
Update backup.php
switch to scandir to avoid errors if open_basedir restricts access to certain directories
1 parent 804e264 commit df55bd3

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

Diff for: backup.php

+33-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//########################################
66

7-
$registerNew = true; //Turn this to false if you dont want to register a new password
7+
$registerNew = false; //Turn this to false if you dont want to register a new password
88
$dir_path = "./"; //This is for the directory the backup.php lies in.
99
$correct_password = "827ccb0eea8a706c4c34a16891f84e7b"; // pass=12345, Change this to your actual password as an MD5 hash
1010

@@ -91,7 +91,7 @@ function unzipFile($zipFile)
9191
preventUnAuthorized();
9292
$zip = new ZipArchive;
9393
if ($zip->open($zipFile) === TRUE) {
94-
$zip->extractTo($dir_path);
94+
$zip->extractTo($dir_path);
9595
$zip->close();
9696
echo '<div class="alert alert-success" role="alert">File unzipped successfully!</div>';
9797
} else {
@@ -154,34 +154,37 @@ function unzipFile($zipFile)
154154
$_SESSION['set_dbname'] = $db_name;
155155

156156
$rootPath = realpath($dir_path);
157-
158157
$sqlfile = doSqlBackup($db_host, $db_user, $db_pass, $db_name);
159-
$archive_file_name = "backup-" . time() . ".zip";
158+
$archive_file_name = "./backup-" . time() . ".zip";
160159

161-
// Initialize archive object
162160
$zip = new ZipArchive();
163-
$zip->open($archive_file_name, ZipArchive::CREATE | ZipArchive::OVERWRITE);
164-
165-
// Create recursive directory iterator
166-
/** @var SplFileInfo[] $files */
167-
$files = new RecursiveIteratorIterator(
168-
new RecursiveDirectoryIterator($rootPath),
169-
RecursiveIteratorIterator::LEAVES_ONLY
170-
);
171-
172-
foreach ($files as $name => $file) {
173-
// Skip directories (they would be added automatically)
174-
if (!$file->isDir()) {
175-
// Get real and relative path for current file
176-
$filePath = $file->getRealPath();
161+
if ($zip->open($archive_file_name, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
162+
throw new RuntimeException("Cannot open <$archive_file_name>");
163+
}
164+
165+
function addFilesToZip($path, $zip, $rootPath)
166+
{
167+
$files = scandir($path);
168+
foreach ($files as $file) {
169+
if ($file === '.' || $file === '..')
170+
continue;
171+
172+
$filePath = $path . DIRECTORY_SEPARATOR . $file;
177173
$relativePath = substr($filePath, strlen($rootPath) + 1);
178174

179-
// Add current file to archive
180-
$zip->addFile($filePath, $relativePath);
175+
if (is_dir($filePath)) {
176+
addFilesToZip($filePath, $zip, $rootPath);
177+
} else {
178+
$zip->addFile($filePath, $relativePath);
179+
}
181180
}
182181
}
182+
183+
addFilesToZip($rootPath, $zip, $rootPath);
184+
183185
$zip->close();
184186

187+
185188
header("Content-type: application/zip");
186189
header("Content-Disposition: attachment; filename=$archive_file_name");
187190
header("Content-length: " . filesize($archive_file_name));
@@ -360,13 +363,16 @@ function doSqlBackup($host, $user, $pass, $dbname)
360363
body {
361364
height: 100%;
362365
}
366+
363367
.content-wrap {
364368
min-height: 100%;
365369
margin-bottom: -100px;
366370
}
371+
367372
.footer {
368373
height: 100px;
369374
}
375+
370376
.alert {
371377
position: absolute;
372378
top: 30px;
@@ -398,7 +404,7 @@ function doSqlBackup($host, $user, $pass, $dbname)
398404
<div class="card">
399405
<div class="card-header"><i class="fas fa-sign-in-alt"></i> Login</div>
400406
<div class="card-body">
401-
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
407+
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
402408
<div class="form-group">
403409
<label for="password">Password:</label>
404410
<input type="password" name="password" id="password" class="form-control" required>
@@ -414,7 +420,7 @@ function doSqlBackup($host, $user, $pass, $dbname)
414420
<div class="card">
415421
<div class="card-header"><i class="fas fa-user-plus"></i> Register</div>
416422
<div class="card-body">
417-
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
423+
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
418424
<div class="form-group">
419425
<label for="password">New Password:</label>
420426
<input type="password" name="newpassword" id="password" class="form-control" required>
@@ -436,7 +442,7 @@ function doSqlBackup($host, $user, $pass, $dbname)
436442
<div class="card mt-3">
437443
<div class="card-header"><i class="fas fa-cloud-upload-alt"></i> Backup</div>
438444
<div class="card-body">
439-
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
445+
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
440446
<!-- Database Credentials -->
441447
<div class="form-group">
442448
<label for="db_host">Database Host:</label>
@@ -478,7 +484,7 @@ class="btn mb-1 btn-success"><i class="fas fa-cloud-upload-alt"></i> Backup all
478484
<div class="card mt-3">
479485
<div class="card-header"><i class="fas fa-file-archive"></i> Unzip</div>
480486
<div class="card-body">
481-
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
487+
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
482488
<?php
483489

484490
$zip_files = glob('*.zip');
@@ -508,7 +514,7 @@ class="btn mb-1 btn-success"><i class="fas fa-cloud-upload-alt"></i> Backup all
508514
<div class="card mt-3">
509515
<div class="card-header"><i class="fas fa-database"></i> SQL Import</div>
510516
<div class="card-body">
511-
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
517+
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
512518
<?php
513519

514520
$sql_files = glob('*.sql');
@@ -547,7 +553,7 @@ class="btn mb-1 btn-success"><i class="fas fa-cloud-upload-alt"></i> Backup all
547553

548554
<div class="card-header"><i class="fas fa-upload"></i> Upload</div>
549555
<div class="card-body">
550-
<form action="<?= $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
556+
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
551557
<div class="form-group">
552558
<label for="file">Choose File:</label>
553559
<input type="file" name="file" id="file" class="form-control-file">

0 commit comments

Comments
 (0)