Files.php 3.4 KB

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