BaseSetup.php 2.6 KB

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