BaseSetup.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. function join_paths() {
  3. $paths = array();
  4. foreach (func_get_args() as $arg) {
  5. if ($arg !== '') { $paths[] = $arg; }
  6. }
  7. return preg_replace('#/+#','/',join('/', $paths));
  8. }
  9. function generate_string(int $length = 16) {
  10. $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@|#[]$%^&*() _-=+{}:;<>?,./';
  11. $random_string = '';
  12. for($i = 0; $i < $length; $i++) {
  13. $random_string .= $chars[random_int(0, strlen($chars) - 1)];
  14. }
  15. return $random_string;
  16. }
  17. abstract class BaseSetup {
  18. protected $domain;
  19. protected $extractsubdir;
  20. public function __construct($domain, HestiaApp $appcontext) {
  21. if(filter_var($domain, FILTER_VALIDATE_DOMAIN) === false) {
  22. throw new Exception("Invalid domain name");
  23. }
  24. $this->domain = $domain;
  25. $this->appcontext = $appcontext;
  26. }
  27. public function getConfig($section=null) {
  28. return (!empty($section))? $this->config[$section] : $this->config;
  29. }
  30. public function getOptions() {
  31. return $this->getConfig('form');
  32. }
  33. public function withDatabase() : bool {
  34. return ($this->getConfig('database') === true);
  35. }
  36. public function getDocRoot($docrelative=null) : string {
  37. $domain_path = $this->appcontext->getWebDomainPath($this->domain);
  38. if(empty($domain_path) || ! is_dir($domain_path)) {
  39. throw new Exception("Error finding domain folder ($domain_path)");
  40. }
  41. return join_paths($domain_path, "public_html", $docrelative);
  42. }
  43. public function retrieveResources() {
  44. return $this->appcontext->archiveExtract(
  45. $this->getConfig('url'),
  46. $this->getDocRoot($this->extractsubdir), 1);
  47. }
  48. public function install($options) {
  49. return $this->retrieveResources();
  50. }
  51. public function cleanup() {
  52. // Remove temporary folder
  53. if(!empty($this->extractsubdir)) {
  54. $this->appcontext->runUser('v-delete-fs-directory',[$this->getDocRoot($this->extractsubdir)], $result);
  55. }
  56. }
  57. }