WordpressSetup.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Hestia\WebApp\Installers\Wordpress;
  3. use Hestia\System\Util;
  4. use Hestia\WebApp\Installers\BaseSetup as BaseSetup;
  5. use function Hestiacp\quoteshellarg\quoteshellarg;
  6. class WordpressSetup extends BaseSetup
  7. {
  8. protected $appInfo = [
  9. 'name' => 'Wordpress',
  10. 'group' => 'cms',
  11. 'enabled' => true,
  12. 'version' => 'latest',
  13. 'thumbnail' => 'wp-thumb.png'
  14. ];
  15. protected $appname = 'wordpress';
  16. protected $config = [
  17. 'form' => [
  18. //'protocol' => [
  19. // 'type' => 'select',
  20. // 'options' => ['http','https'],
  21. //],
  22. 'site_name' => ['type'=>'text', 'value'=>'WordPress Blog'],
  23. 'wordpress_account_username' => ['value'=>'wpadmin'],
  24. 'wordpress_account_email' => 'text',
  25. 'wordpress_account_password' => 'password',
  26. 'install_directory' => ['type'=>'text', 'value'=>'', 'placeholder'=>'/'],
  27. 'language' => [
  28. 'type' => 'select',
  29. 'value' => 'en_US',
  30. 'options' => [
  31. 'cs_CZ' => 'Czech',
  32. 'de_DE' => 'German',
  33. 'es_ES' => 'Spanish',
  34. 'en_US' => 'English',
  35. 'fr_FR' => 'French',
  36. 'hu_HU' => 'Hungarian',
  37. 'it_IT' => 'Italian',
  38. 'nl_NL' => 'Dutch',
  39. 'pt_PT' => 'Portuguese',
  40. 'sk_SK' => 'Slovak',
  41. 'sr_RS' => 'Serbian',
  42. 'tr_TR' => 'Turkish',
  43. 'ru_RU' => 'Russian',
  44. 'uk' => 'Ukrainian',
  45. 'zh-CN' => 'Simplified Chinese (China)',
  46. 'zh_TW' => 'Traditional Chinese',
  47. ]
  48. ],
  49. ],
  50. 'database' => true,
  51. 'resources' => [
  52. 'wp' => [ 'src' => 'https://wordpress.org/latest.tar.gz' ],
  53. ],
  54. 'server' => [
  55. 'nginx' => [
  56. 'template' => 'wordpress',
  57. ],
  58. 'php' => [
  59. 'supported' => [ '7.4','8.0','8.1' ],
  60. ]
  61. ],
  62. ];
  63. public function install(array $options = null)
  64. {
  65. parent::setAppDirInstall($options['install_directory']);
  66. parent::install($options);
  67. parent::setup($options);
  68. $this->appcontext->runUser('v-open-fs-file', [$this->getDocRoot("wp-config-sample.php")], $result);
  69. $distconfig = preg_replace(
  70. [
  71. '/database_name_here/', '/username_here/', '/password_here/', '/utf8/', '/wp_/'
  72. ],
  73. [
  74. $this->appcontext->user() . '_' . $options['database_name'],
  75. $this->appcontext->user() . '_' . $options['database_user'],
  76. $options['database_password'],
  77. 'utf8mb4',
  78. Util::generate_string(3, false).'_'
  79. ],
  80. $result->text
  81. );
  82. while (strpos($distconfig, 'put your unique phrase here') !== false) {
  83. $distconfig = preg_replace('/put your unique phrase here/', Util::generate_string(64), $distconfig, 1);
  84. }
  85. $tmp_configpath = $this->saveTempFile($distconfig);
  86. if (!$this->appcontext->runUser('v-move-fs-file', [$tmp_configpath, $this->getDocRoot("wp-config.php")], $result)) {
  87. throw new \Exception("Error installing config file in: " . $tmp_configpath . " to:" . $this->getDocRoot("wp-config.php") . $result->text);
  88. }
  89. $this->appcontext->run('v-list-web-domain', [$this->appcontext->user(), $this->domain, 'json'], $status);
  90. $sslEnabled = ($status->json[$this->domain]['SSL'] == 'no' ? 0 : 1);
  91. $webDomain = ($sslEnabled ? "https://" : "http://") . $this->domain . "/";
  92. $webPort= ($sslEnabled ? "443" : "80");
  93. if (substr($options['install_directory'], 0, 1) == '/') {
  94. $options['install_directory'] = substr($options['install_directory'], 1);
  95. }
  96. if (substr($options['install_directory'], -1, 1) == '/') {
  97. $options['install_directory'] = substr($options['install_directory'], 0, strlen($options['install_directory']) - 1);
  98. }
  99. exec("/usr/bin/curl --location --post301 --insecure --resolve ".$this->domain.":$webPort:".$this->appcontext->getWebDomainIp($this->domain)." "
  100. . quoteshellarg($webDomain.$options['install_directory']."/wp-admin/install.php?step=2")
  101. . " -d " . quoteshellarg(
  102. "weblog_title=" . rawurlencode($options['site_name'])
  103. . "&user_name=" . rawurlencode($options['wordpress_account_username'])
  104. . "&admin_password=" . rawurlencode($options['wordpress_account_password'])
  105. . "&admin_password2=". rawurlencode($options['wordpress_account_password'])
  106. . "&admin_email=" . rawurlencode($options['wordpress_account_email'])
  107. ), $output, $return_var);
  108. if ( strpos(implode(PHP_EOL,$output),'Error establishing a database connection') !== false){
  109. throw new \Exception('Error establishing a database connection');
  110. }
  111. if ($return_var > 0) {
  112. throw new \Exception(implode(PHP_EOL, $output));
  113. }
  114. return ($return_var === 0);
  115. }
  116. }