Files.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\App\Requirements\Handlers;
  3. use \ModulesGarden\ProxmoxAddon\Core\App\Requirements\Instances\Files as FilesInstance;
  4. /**
  5. * Description of Files
  6. *
  7. * @author INBSX-37H
  8. */
  9. class Files implements \ModulesGarden\ProxmoxAddon\Core\App\Requirements\HandlerInterface
  10. {
  11. use \ModulesGarden\ProxmoxAddon\Core\Traits\Lang;
  12. protected $fileList = [];
  13. protected $unfulfilledRequirements = [];
  14. public function __construct(array $fileList = [])
  15. {
  16. $this->fileList = $fileList;
  17. $this->handleRequirements();
  18. }
  19. public function handleRequirements()
  20. {
  21. foreach ($this->fileList as $record)
  22. {
  23. if (!$this->isValidPath($record[FilesInstance::PATH]))
  24. {
  25. continue;
  26. }
  27. $this->handleRequirement($record);
  28. }
  29. }
  30. public function isValidPath($path)
  31. {
  32. if (stripos($path, FilesInstance::WHMCS_PATH) === 0 || stripos($path, FilesInstance::MODULE_PATH) === 0)
  33. {
  34. return true;
  35. }
  36. return false;
  37. }
  38. protected function handleRequirement($record)
  39. {
  40. $filePath = $this->getFullPath($record[FilesInstance::PATH]);
  41. switch ($record[FilesInstance::TYPE])
  42. {
  43. case FilesInstance::REMOVE:
  44. $this->removeFile($filePath);
  45. break;
  46. case FilesInstance::IS_WRITABLE:
  47. $this->checkIfWritable($filePath);
  48. break;
  49. }
  50. }
  51. public function getFullPath($recordPath = null)
  52. {
  53. if (stripos($recordPath, FilesInstance::WHMCS_PATH) === 0)
  54. {
  55. return str_replace(FilesInstance::WHMCS_PATH, \ModulesGarden\ProxmoxAddon\Core\ModuleConstants::getFullPathWhmcs(), str_replace('/', DIRECTORY_SEPARATOR, $recordPath));
  56. }
  57. if (stripos($recordPath, FilesInstance::MODULE_PATH) === 0)
  58. {
  59. return str_replace(FilesInstance::MODULE_PATH, \ModulesGarden\ProxmoxAddon\Core\ModuleConstants::getModuleRootDir(), str_replace('/', DIRECTORY_SEPARATOR, $recordPath));
  60. }
  61. return null;
  62. }
  63. protected function removeFile($filePath = null)
  64. {
  65. $fileValidator = new \ModulesGarden\ProxmoxAddon\Core\FileReader\PathValidator();
  66. if (!$fileValidator->pathExists($filePath))
  67. {
  68. return null;
  69. }
  70. unlink($filePath);
  71. if (!$fileValidator->pathExists($filePath))
  72. {
  73. return null;
  74. }
  75. $this->addUnfulfilledRequirement('In order for the module to work correctly, please remove the following file: :remove_file_requirement:', $filePath);
  76. }
  77. protected function checkIfWritable($filePath = null)
  78. {
  79. $fileValidator = new \ModulesGarden\ProxmoxAddon\Core\FileReader\PathValidator();
  80. if ($fileValidator->isPathWritable($filePath))
  81. {
  82. return null;
  83. }
  84. $this->addUnfulfilledRequirement('In order for the module to work correctly, please set up permissions to the :remove_file_requirement: directory as writable.',
  85. $filePath);
  86. }
  87. protected function addUnfulfilledRequirement($message = null, $path = null)
  88. {
  89. if ($message && $path)
  90. {
  91. $this->loadLang();
  92. $this->unfulfilledRequirements[] = str_replace(':remove_file_requirement:', $path, $this->lang->absoluteTranslate('unfulfilledRequirement', $message));
  93. }
  94. }
  95. public function getUnfulfilledRequirements()
  96. {
  97. return $this->unfulfilledRequirements;
  98. }
  99. }