BaseSetup.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Hestia\WebApp\Installers;
  3. use Hestia\System\Util;
  4. use Hestia\System\HestiaApp;
  5. use Hestia\WebApp\InstallerInterface;
  6. use Hestia\Models\WebDomain;
  7. use Hestia\WebApp\Installers\Resources\ComposerResource;
  8. abstract class BaseSetup implements InstallerInterface {
  9. protected $domain;
  10. protected $extractsubdir;
  11. public function info(){
  12. return $this -> appInfo;
  13. }
  14. public function __construct($domain, HestiaApp $appcontext)
  15. {
  16. if(filter_var($domain, FILTER_VALIDATE_DOMAIN) === false) {
  17. throw new \Exception("Invalid domain name");
  18. }
  19. $this->domain = $domain;
  20. $this->appcontext = $appcontext;
  21. }
  22. public function getConfig($section=null)
  23. {
  24. return (!empty($section))? $this->config[$section] : $this->config;
  25. }
  26. public function getOptions()
  27. {
  28. return $this->getConfig('form');
  29. }
  30. public function withDatabase() : bool
  31. {
  32. return ($this->getConfig('database') === true);
  33. }
  34. public function getDocRoot($append_relative_path=null) : string
  35. {
  36. $domain_path = $this->appcontext->getWebDomainPath($this->domain);
  37. if(empty($domain_path) || ! is_dir($domain_path)) {
  38. throw new \Exception("Error finding domain folder ($domain_path)");
  39. }
  40. return Util::join_paths($domain_path, "public_html", $append_relative_path);
  41. }
  42. public function retrieveResources($options)
  43. {
  44. foreach ($this->getConfig('resources') as $res_type => $res_data) {
  45. if (!empty($res_data['dst']) && is_string($res_data['dst'])) {
  46. $resource_destination = $this->getDocRoot($res_data['dst']);
  47. } else {
  48. $resource_destination = $this->getDocRoot($this->extractsubdir);
  49. }
  50. if ($res_type === 'composer') {
  51. new ComposerResource($this->appcontext, $res_data, $resource_destination);
  52. } else {
  53. $this->appcontext->archiveExtract($res_data['src'], $resource_destination, 1);
  54. }
  55. }
  56. return true;
  57. }
  58. public function install(array $options=null)
  59. {
  60. $this->appcontext->runUser('v-delete-fs-file', [$this->getDocRoot('robots.txt')]);
  61. $this->appcontext->runUser('v-delete-fs-file', [$this->getDocRoot('index.html')]);
  62. return $this->retrieveResources($options);
  63. }
  64. public function cleanup()
  65. {
  66. // Remove temporary folder
  67. if(!empty($this->extractsubdir)) {
  68. $this->appcontext->runUser('v-delete-fs-directory',[$this->getDocRoot($this->extractsubdir)], $result);
  69. }
  70. }
  71. public function saveTempFile(string $data)
  72. {
  73. $tmp_file = tempnam("/tmp", "hst.");
  74. if(empty($tmp_file)) {
  75. throw new \Exception("Error creating temp file");
  76. }
  77. if (file_put_contents($tmp_file, $data) > 0) {
  78. chmod($tmp_file, 0644);
  79. $user_tmp_file = Util::join_paths($this->appcontext->getUserHomeDir(), $tmp_file );
  80. $this->appcontext->runUser('v-copy-fs-file',[$tmp_file, $user_tmp_file], $result);
  81. unlink($tmp_file);
  82. return $user_tmp_file;
  83. }
  84. if(file_exists($tmp_file)) {
  85. unlink($tmp_file);
  86. }
  87. return false;
  88. }
  89. }