backup-functions.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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->writeLog('success', 'Backup Manager Function - Deleted Backup [' . pathinfo($filename, PATHINFO_BASENAME) . ']', $this->user['username']);
  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. $orgFiles = array(
  62. 'orgLog' => $this->organizrLog,
  63. 'loginLog' => $this->organizrLoginLog,
  64. 'config' => $this->userConfigPath,
  65. 'database' => $this->config['dbLocation'] . $this->config['dbName']
  66. );
  67. $files = $this->fileArray($orgFiles);
  68. if (!empty($files)) {
  69. $this->writeLog('success', 'BACKUP: backup process started', 'SYSTEM');
  70. $zipname = $directory . 'backup[' . date('Y-m-d_H-i') . '][' . $this->version . '].zip';
  71. $zip = new ZipArchive;
  72. $zip->open($zipname, ZipArchive::CREATE);
  73. foreach ($files as $file) {
  74. $zip->addFile($file);
  75. }
  76. $zip->close();
  77. $this->writeLog('success', 'BACKUP: backup process finished', 'SYSTEM');
  78. $this->setAPIResponse('success', 'Backup has been created', 200);
  79. return true;
  80. } else {
  81. $this->setAPIResponse('error', 'Backup creation failed', 409);
  82. return false;
  83. }
  84. }
  85. public function getBackups()
  86. {
  87. $path = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  88. @mkdir($path, 0770, true);
  89. $files = array_diff(scandir($path), array('.', '..'));
  90. $fileList = [];
  91. $totalFiles = 0;
  92. $totalFileSize = 0;
  93. foreach ($files as $file) {
  94. if (file_exists($path . $file)) {
  95. $size = filesize($path . $file);
  96. $totalFileSize = $totalFileSize + $size;
  97. $totalFiles = $totalFiles + 1;
  98. try {
  99. $fileList['files'][] = [
  100. 'name' => $file,
  101. 'size' => $this->human_filesize($size, 0),
  102. 'date' => gmdate("Y-m-d\TH:i:s\Z", (filemtime($path . $file)))
  103. ];
  104. } catch (Exception $e) {
  105. $this->setAPIResponse('error', 'Backup list failed', 409, $e->getMessage());
  106. return false;
  107. }
  108. }
  109. }
  110. $fileList['total_files'] = $totalFiles;
  111. $fileList['total_size'] = $this->human_filesize($totalFileSize, 2);
  112. $this->setAPIResponse('success', null, 200, array_reverse($fileList));
  113. return array_reverse($fileList);
  114. }
  115. }