From 4a82572934e5f815f1b8b23dbb2aa68523c3e958 Mon Sep 17 00:00:00 2001 From: Bozhidar Slaveykov Date: Thu, 4 Apr 2024 19:30:22 +0300 Subject: [PATCH] add apache configs from modules --- .../Providers/LetsEncryptServiceProvider.php | 4 ++ .../LetsEncryptApacheVirtualHostConfig.php | 13 ++++++ .../Providers/MicroweberServiceProvider.php | 4 ++ .../MicroweberApacheVirtualHostConfig.php | 13 ++++++ web/Modules/Microweber/config/config.php | 3 +- web/app/Actions/ApacheWebsiteCreate.php | 17 ++++++++ .../Listeners/ModelWebsiteCreatedListener.php | 9 ++++ web/app/Models/HostingPackage.php | 11 ----- web/app/PhyreLaravelApplication.php | 14 +++++++ web/app/Providers/AppServiceProvider.php | 14 +++++-- .../ApacheVirtualHostConfigBase.php | 18 ++++++++ .../VirtualHosts/ApacheVirtualHostManager.php | 42 +++++++++++++++++++ web/bootstrap/app.php | 2 +- .../samples/ubuntu/apache2-conf.blade.php | 15 +++++++ 14 files changed, 162 insertions(+), 17 deletions(-) create mode 100644 web/Modules/LetsEncrypt/LetsEncryptApacheVirtualHostConfig.php create mode 100644 web/Modules/Microweber/MicroweberApacheVirtualHostConfig.php delete mode 100644 web/app/Models/HostingPackage.php create mode 100644 web/app/PhyreLaravelApplication.php create mode 100644 web/app/VirtualHosts/ApacheVirtualHostConfigBase.php create mode 100644 web/app/VirtualHosts/ApacheVirtualHostManager.php diff --git a/web/Modules/LetsEncrypt/App/Providers/LetsEncryptServiceProvider.php b/web/Modules/LetsEncrypt/App/Providers/LetsEncryptServiceProvider.php index c271a37..04a09e2 100644 --- a/web/Modules/LetsEncrypt/App/Providers/LetsEncryptServiceProvider.php +++ b/web/Modules/LetsEncrypt/App/Providers/LetsEncryptServiceProvider.php @@ -6,6 +6,7 @@ use App\Events\HostingAccountIsCreated; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; +use Modules\LetsEncrypt\LetsEncryptApacheVirtualHostConfig; use Modules\LetsEncrypt\Listeners\HostingAccountIsCreatedListener; use Modules\LetsEncrypt\Providers\Filament\AdminPanelProvider; @@ -37,6 +38,9 @@ class LetsEncryptServiceProvider extends ServiceProvider { $this->app->register(RouteServiceProvider::class); $this->app->register(AdminPanelProvider::class); + + + app()->virtualHostManager->registerConfig(LetsEncryptApacheVirtualHostConfig::class, $this->moduleNameLower); } /** diff --git a/web/Modules/LetsEncrypt/LetsEncryptApacheVirtualHostConfig.php b/web/Modules/LetsEncrypt/LetsEncryptApacheVirtualHostConfig.php new file mode 100644 index 0000000..5f7b3a5 --- /dev/null +++ b/web/Modules/LetsEncrypt/LetsEncryptApacheVirtualHostConfig.php @@ -0,0 +1,13 @@ +app->register(RouteServiceProvider::class); + + app()->virtualHostManager->registerConfig(MicroweberApacheVirtualHostConfig::class, $this->moduleNameLower); } /** diff --git a/web/Modules/Microweber/MicroweberApacheVirtualHostConfig.php b/web/Modules/Microweber/MicroweberApacheVirtualHostConfig.php new file mode 100644 index 0000000..03e30b3 --- /dev/null +++ b/web/Modules/Microweber/MicroweberApacheVirtualHostConfig.php @@ -0,0 +1,13 @@ +'/usr/share/microweber/latest', 'modules'=>'/usr/share/microweber/latest/userfiles/modules', 'templates'=>'/usr/share/microweber/latest/userfiles/templates' - ] + ], + ]; diff --git a/web/app/Actions/ApacheWebsiteCreate.php b/web/app/Actions/ApacheWebsiteCreate.php index f6da9d7..2190c4d 100644 --- a/web/app/Actions/ApacheWebsiteCreate.php +++ b/web/app/Actions/ApacheWebsiteCreate.php @@ -4,6 +4,7 @@ namespace App\Actions; use App\FileManagerApi; use App\ShellApi; +use App\VirtualHosts\ApacheVirtualHostManager; class ApacheWebsiteCreate { @@ -11,6 +12,8 @@ class ApacheWebsiteCreate public $user; public $email; public $password; + public $additionalServices = []; + public $features = []; public function setDomain($domain) { @@ -32,6 +35,16 @@ class ApacheWebsiteCreate $this->password = $password; } + public function setAdditionalServices($additionalServices) + { + $this->additionalServices = $additionalServices; + } + + public function setFeatures($features) + { + $this->features = $features; + } + public function handle() { @@ -54,6 +67,8 @@ class ApacheWebsiteCreate $domainPublic = $domainRoot . '/public_html'; $homeRoot = '/home/'.$this->user; + $apacheVirtualHostConfigs = app()->virtualHostManager->getConfigs($this->additionalServices); + $settings = [ 'port' => 80, 'domain' => $this->domain, @@ -64,6 +79,8 @@ class ApacheWebsiteCreate 'group' => 'www-data', 'enableRuid2' => true, ]; + + $settings = array_merge($settings, $apacheVirtualHostConfigs); $apache2Sample = view('actions.samples.ubuntu.apache2-conf', $settings)->render(); if (!is_dir($homeRoot)) { diff --git a/web/app/Listeners/ModelWebsiteCreatedListener.php b/web/app/Listeners/ModelWebsiteCreatedListener.php index 0a61a16..6408960 100644 --- a/web/app/Listeners/ModelWebsiteCreatedListener.php +++ b/web/app/Listeners/ModelWebsiteCreatedListener.php @@ -5,6 +5,8 @@ namespace App\Listeners; use App\Actions\ApacheWebsiteCreate; use App\Events\ModelWebsiteCreated; use App\Models\Customer; +use App\Models\HostingPackage; +use App\Models\HostingPlan; use Cassandra\Custom; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; @@ -35,11 +37,18 @@ class ModelWebsiteCreatedListener return; } + $findHostingPlan = HostingPlan::where('id', $findWebsite->hosting_plan_id)->first(); + if (!$findHostingPlan) { + return; + } + $newApacheWebsite = new ApacheWebsiteCreate(); $newApacheWebsite->setDomain($findWebsite->domain); $newApacheWebsite->setUser($findCustomer->username); $newApacheWebsite->setEmail($findCustomer->email); $newApacheWebsite->setPassword(Str::random(16)); + $newApacheWebsite->setAdditionalServices($findHostingPlan->additional_services); + $newApacheWebsite->setFeatures($findHostingPlan->features); $create = $newApacheWebsite->handle(); diff --git a/web/app/Models/HostingPackage.php b/web/app/Models/HostingPackage.php deleted file mode 100644 index c9956eb..0000000 --- a/web/app/Models/HostingPackage.php +++ /dev/null @@ -1,11 +0,0 @@ -phpAdminValueOpenBaseDirs; + + return $configValues; + } + +} diff --git a/web/app/VirtualHosts/ApacheVirtualHostManager.php b/web/app/VirtualHosts/ApacheVirtualHostManager.php new file mode 100644 index 0000000..943cc68 --- /dev/null +++ b/web/app/VirtualHosts/ApacheVirtualHostManager.php @@ -0,0 +1,42 @@ +registerConfigs[$module][] = $config; + } + + public function getConfigs($forModules = []) + { + $allConfigs = []; + foreach ($this->registerConfigs as $module => $configs) { + if (!in_array($module, $forModules)) { + continue; + } + foreach ($configs as $config) { + try { + $registerConfigInstance = app()->make($config); + $getConfig = $registerConfigInstance->getConfig(); + if (!empty($getConfig)) { + foreach ($getConfig as $key => $value) { + if (!isset($allConfigs[$key])) { + $allConfigs[$key] = []; + } + $allConfigs[$key] = array_merge($allConfigs[$key], $value); + } + } + } catch (\Exception $e) { + // can't create instance + } + } + } + + return $allConfigs; + } + +} diff --git a/web/bootstrap/app.php b/web/bootstrap/app.php index 037e17d..01c6cb4 100644 --- a/web/bootstrap/app.php +++ b/web/bootstrap/app.php @@ -11,7 +11,7 @@ | */ -$app = new Illuminate\Foundation\Application( +$app = new \App\PhyreLaravelApplication( $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__) ); diff --git a/web/resources/views/actions/samples/ubuntu/apache2-conf.blade.php b/web/resources/views/actions/samples/ubuntu/apache2-conf.blade.php index 31f2e39..a2bc86d 100644 --- a/web/resources/views/actions/samples/ubuntu/apache2-conf.blade.php +++ b/web/resources/views/actions/samples/ubuntu/apache2-conf.blade.php @@ -21,6 +21,21 @@ RMode config RUidGid {{$user}} {{$group}} @endif + + @php + $appendOpenBaseDirs = $homeRoot; + if (isset($phpAdminValueOpenBaseDirs) + && is_array($phpAdminValueOpenBaseDirs) + && !empty($phpAdminValueOpenBaseDirs)) { + $appendOpenBaseDirs .= ':' . implode(':', $phpAdminValueOpenBaseDirs); + } + @endphp + php_admin_value open_basedir {{$appendOpenBaseDirs}} + + php_admin_value upload_tmp_dir {{$homeRoot}}/tmp + php_admin_value session.save_path {{$homeRoot}}/tmp + php_admin_value sys_temp_dir {{$homeRoot}}/tmp +