backup-functions.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. trait BackupFunctions
  3. {
  4. public function fileArray($files)
  5. {
  6. foreach ($files as $file) {
  7. if (file_exists($file)) {
  8. $list[] = $file;
  9. }
  10. }
  11. if (!empty($list)) {
  12. return $list;
  13. }
  14. }
  15. public function deleteBackup($filename)
  16. {
  17. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  18. $path = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  19. $filename = $path . $filename;
  20. if ($ext == 'zip') {
  21. if (file_exists($filename)) {
  22. $this->setLoggerChannel('Backup')->info('Deleted Backup [' . pathinfo($filename, PATHINFO_BASENAME) . ']');
  23. $this->setAPIResponse(null, pathinfo($filename, PATHINFO_BASENAME) . ' has been deleted', null);
  24. return (unlink($filename));
  25. } else {
  26. $this->setAPIResponse('error', 'File does not exist', 404);
  27. return false;
  28. }
  29. } else {
  30. $this->setAPIResponse('error', pathinfo($filename, PATHINFO_BASENAME) . ' is not approved to be deleted', 409);
  31. return false;
  32. }
  33. }
  34. public function downloadBackup($filename)
  35. {
  36. $path = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  37. $filename = $path . $filename;
  38. if (file_exists($filename)) {
  39. header('Content-Type: application/zip');
  40. header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
  41. header('Content-Length: ' . filesize($filename));
  42. flush();
  43. readfile($filename);
  44. exit();
  45. } else {
  46. $this->setAPIResponse('error', 'File does not exist', 404);
  47. return false;
  48. }
  49. }
  50. public function backupOrganizr($type = 'config')
  51. {
  52. $directory = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  53. @mkdir($directory, 0770, true);
  54. switch ($type) {
  55. case 'config':
  56. break;
  57. case 'full':
  58. break;
  59. default:
  60. }
  61. $this->setLoggerChannel('Backup')->notice('Backing up Organizr');
  62. $zipname = $directory . 'backup[' . date('Y-m-d_H-i') . ' - ' . $this->random_ascii_string(2) . '][' . $this->version . '].zip';
  63. $zip = new ZipArchive;
  64. $zip->open($zipname, ZipArchive::CREATE);
  65. if ($this->config['driver'] == 'sqlite3') {
  66. $zip->addFile($this->config['dbLocation'] . $this->config['dbName'], basename($this->config['dbLocation'] . $this->config['dbName']));
  67. }
  68. $rootPath = $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR;
  69. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
  70. foreach ($files as $name => $file) {
  71. // Skip directories (they would be added automatically)
  72. if (!$file->isDir()) {
  73. if (stripos($name, 'data' . DIRECTORY_SEPARATOR . 'cache') == false) {
  74. // Get real and relative path for current file
  75. $filePath = $file->getRealPath();
  76. $relativePath = substr($filePath, strlen($rootPath));
  77. // Add current file to archive
  78. $zip->addFile($filePath, $relativePath);
  79. }
  80. }
  81. }
  82. $zip->close();
  83. $this->setLoggerChannel('Backup')->notice('Backup process finished');
  84. $this->setAPIResponse('success', 'Backup has been created', 200);
  85. return true;
  86. }
  87. public function getBackups()
  88. {
  89. $path = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  90. @mkdir($path, 0770, true);
  91. $files = array_diff(scandir($path), array('.', '..'));
  92. $fileList = [];
  93. $totalFiles = 0;
  94. $totalFileSize = 0;
  95. foreach ($files as $file) {
  96. $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  97. if (file_exists($path . $file) && $ext == 'zip') {
  98. $size = filesize($path . $file);
  99. $totalFileSize = $totalFileSize + $size;
  100. $totalFiles = $totalFiles + 1;
  101. try {
  102. $fileList['files'][] = [
  103. 'name' => $file,
  104. 'size' => $this->human_filesize($size, 0),
  105. 'date' => gmdate("Y-m-d\TH:i:s\Z", (filemtime($path . $file)))
  106. ];
  107. } catch (Exception $e) {
  108. $this->setAPIResponse('error', 'Backup list failed', 409, $e->getMessage());
  109. return false;
  110. }
  111. }
  112. }
  113. $fileList['total_files'] = $totalFiles;
  114. $fileList['total_size'] = $this->human_filesize($totalFileSize, 2);
  115. $fileList['files'] = $totalFiles > 0 ? array_reverse($fileList['files']) : null;
  116. $this->setAPIResponse('success', null, 200, array_reverse($fileList));
  117. return array_reverse($fileList);
  118. }
  119. }