| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPSynchro\API; |
| 4 |
|
| 5 |
use WPSynchro\Utilities\PluginDirs; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class for handling service to download database backups |
| 9 |
* Call should already be verified by permissions callback |
| 10 |
*/ |
| 11 |
class DownloadLogDBBackup extends WPSynchroService |
| 12 |
{ |
| 13 |
public function service() |
| 14 |
{ |
| 15 |
if (!isset($_REQUEST['job_id']) || strlen($_REQUEST['job_id']) == 0) { |
| 16 |
$result = new \StdClass(); |
| 17 |
echo json_encode($result); |
| 18 |
http_response_code(400); |
| 19 |
return; |
| 20 |
} |
| 21 |
$job_id = sanitize_key($_REQUEST['job_id']); |
| 22 |
|
| 23 |
$filename = "database_backup_" . $job_id . ".sql"; |
| 24 |
$plugins_dirs = new PluginDirs(); |
| 25 |
$log_path = $plugins_dirs->getUploadsFilePath(); |
| 26 |
|
| 27 |
if (!file_exists($log_path . $filename)) { |
| 28 |
http_response_code(400); |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$log_contents = file_get_contents($log_path . $filename); |
| 33 |
|
| 34 |
$zipfilename = "wpsynchro_db_backup_" . $job_id . ".zip"; |
| 35 |
|
| 36 |
http_response_code(200); // IIS fails if this is not here |
| 37 |
header("Pragma: public"); |
| 38 |
header("Expires: 0"); |
| 39 |
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
| 40 |
header("Cache-Control: public"); |
| 41 |
header("Content-Description: File Transfer"); |
| 42 |
header("Content-Type: application/zip"); |
| 43 |
header("Content-Disposition: attachment; filename=" . $zipfilename); |
| 44 |
|
| 45 |
$zipfile = tempnam($log_path, "zip"); |
| 46 |
$zip = new \ZipArchive(); |
| 47 |
$zip->open($zipfile, \ZipArchive::OVERWRITE); |
| 48 |
$zip->addFromString($filename, $log_contents); |
| 49 |
$zip->close(); |
| 50 |
|
| 51 |
readfile($zipfile); |
| 52 |
unlink($zipfile); |
| 53 |
|
| 54 |
exit(); |
| 55 |
} |
| 56 |
} |
| 57 |
|