BackupProvider.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\Servers\ProxmoxVps\App\UI\Backup\Providers;
  20. use MGProvision\Proxmox\v2\models\File;
  21. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RestoreVm;
  22. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  24. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  26. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  27. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  28. use ModulesGarden\Servers\ProxmoxVps\App\Enum\ConfigurableOption;
  29. class BackupProvider extends BaseDataProvider implements ClientArea
  30. {
  31. use ProductService;
  32. use ApiService;
  33. public function read()
  34. {
  35. if ($this->actionElementId)
  36. {
  37. $this->data = json_decode(base64_decode($this->actionElementId), true);
  38. }
  39. }
  40. public function create()
  41. {
  42. $this->acl()->backup();
  43. $this->resourceGuard()->backupLimit();
  44. $storage = $this->configuration()->getBackupStorage();
  45. $routing = $this->configuration()->isBackupRouting() ? 1 : 0;
  46. $maxFiles = $this->configuration()->getBackupMaxFiles();
  47. if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
  48. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
  49. }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)=="-1"){
  50. $maxFiles = null;
  51. }
  52. $this->vm()->backup($storage, $routing, $maxFiles, $this->formData['compress'], $this->formData['mode']);
  53. return (new HtmlDataJsonResponse())
  54. ->setStatusSuccess()
  55. ->setMessageAndTranslate('The backup creation is in progress')
  56. ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
  57. ->setCallBackFunction('pmToggleButton');
  58. }
  59. public function restore()
  60. {
  61. queue(RestoreVm::class, ['volid' => $this->formData['volid']], null, 'hosting', $this->getWhmcsParamByKey('serviceid'));
  62. return (new HtmlDataJsonResponse())
  63. ->setStatusSuccess()
  64. ->setMessageAndTranslate('The virtual machine restore is in progress');
  65. }
  66. public function update()
  67. {
  68. }
  69. public function delete()
  70. {
  71. $this->acl()->backup();
  72. $storage = $this->configuration()->getBackupStorage();
  73. $volid = $this->formData['volid'];
  74. $backupFile = new File();
  75. $backupFile->setApi($this->api());
  76. $backupFile->setPath("/nodes/{$this->vm()->getNode()}/storage/{$storage}/content/{$volid}");
  77. $backupFile->delete();
  78. return (new HtmlDataJsonResponse())
  79. ->setStatusSuccess()
  80. ->setMessageAndTranslate('The backup has been deleted successfully')
  81. ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
  82. ->setCallBackFunction('pmToggleButton');
  83. }
  84. public function deleteMass()
  85. {
  86. $this->acl()->backup();
  87. $storage = $this->configuration()->getBackupStorage();
  88. $backupFile = new File();
  89. $backupFile->setApi($this->api());
  90. foreach ($this->request->get('massActions') as $id)
  91. {
  92. $data = json_decode(base64_decode($id), true);
  93. $volid = $data['volid'];
  94. $backupFile->setPath("/nodes/{$this->vm()->getNode()}/storage/{$storage}/content/{$volid}");
  95. $backupFile->delete();
  96. }
  97. return (new HtmlDataJsonResponse())
  98. ->setStatusSuccess()
  99. ->setMessageAndTranslate('The backups have been deleted successfully')
  100. ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
  101. ->setCallBackFunction('pmToggleButton');
  102. }
  103. }