From 9990d432728c4c5b1fd069c3e9d18140f775b8bc Mon Sep 17 00:00:00 2001 From: Peter Ivanov Date: Tue, 17 Dec 2024 14:38:17 +0200 Subject: [PATCH] update --- .../Api/HostingSubscriptionsController.php | 3 +- web/app/Jobs/ApacheBuild.php | 28 +++++++++++++++---- web/app/MasterDomain.php | 23 +++++++-------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/web/app/Http/Controllers/Api/HostingSubscriptionsController.php b/web/app/Http/Controllers/Api/HostingSubscriptionsController.php index 7267ac94..7d941240 100644 --- a/web/app/Http/Controllers/Api/HostingSubscriptionsController.php +++ b/web/app/Http/Controllers/Api/HostingSubscriptionsController.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Api\Request\HostingSubscriptionCreateRequest; use App\Http\Controllers\ApiController; +use App\Jobs\ApacheBuild; use App\Models\Domain; use App\Models\HostingSubscription; use Carbon\Carbon; @@ -61,7 +62,7 @@ public function store(HostingSubscriptionCreateRequest $request) $hostingSubscription->setup_date = Carbon::now(); $hostingSubscription->save(); - + ApacheBuild::dispatchSync(); return response()->json([ 'status' => 'ok', 'message' => 'Hosting subscription created', diff --git a/web/app/Jobs/ApacheBuild.php b/web/app/Jobs/ApacheBuild.php index 7c675a17..5278709d 100644 --- a/web/app/Jobs/ApacheBuild.php +++ b/web/app/Jobs/ApacheBuild.php @@ -29,12 +29,29 @@ public function __construct($fixPermissions = false) */ public function handle(): void { - $getAllDomains = Domain::whereNot('status','<=>', 'broken')->get(); + $getAllDomains = Domain::whereNot('status', '<=>', 'broken')->get(); $virtualHosts = []; - foreach ($getAllDomains as $domain) { + + file_put_contents(storage_path('logs/apache2.log'),1111); + + foreach ($getAllDomains as $domain) { + $isBroken = false; +// $findHostingSubscription = \App\Models\HostingSubscription::where('id', $domain->hosting_subscription_id) +// ->first(); +// // check is valid domain +// if (!$findHostingSubscription) { +// $isBroken = true; +// } + if ($domain->status === 'broken') { + continue; + } // check is valid domain if (!filter_var($domain->domain, FILTER_VALIDATE_DOMAIN)) { + $isBroken = true; + } + + if ($isBroken) { // mark domain as broken $domain->status = 'broken'; $domain->save(); @@ -42,9 +59,10 @@ public function handle(): void } + /* @var Domain $domain */ $virtualHostSettings = $domain->configureVirtualHost($this->fixPermissions); - if(!$virtualHostSettings){ + if (!$virtualHostSettings) { // mark domain as broken $domain->status = 'broken'; $domain->save(); @@ -54,7 +72,7 @@ public function handle(): void if (isset($virtualHostSettings['virtualHostSettings'])) { $virtualHosts[] = $virtualHostSettings['virtualHostSettings']; } - if (isset($virtualHostSettings['virtualHostSettingsWithSSL'])) { + if (isset($virtualHostSettings['virtualHostSettingsWithSSL']) and $virtualHostSettings['virtualHostSettingsWithSSL']) { $virtualHosts[] = $virtualHostSettings['virtualHostSettingsWithSSL']; } } @@ -67,7 +85,7 @@ public function handle(): void if (isset($domainVirtualHost['virtualHostSettings'])) { $virtualHosts[] = $domainVirtualHost['virtualHostSettings']; } - if (isset($domainVirtualHost['virtualHostSettingsWithSSL'])) { + if (isset($domainVirtualHost['virtualHostSettingsWithSSL']) and $domainVirtualHost['virtualHostSettingsWithSSL']) { $virtualHosts[] = $domainVirtualHost['virtualHostSettingsWithSSL']; } } diff --git a/web/app/MasterDomain.php b/web/app/MasterDomain.php index 619cfdfd..7784acc5 100644 --- a/web/app/MasterDomain.php +++ b/web/app/MasterDomain.php @@ -46,7 +46,7 @@ public function configureVirtualHost($fixPermissions = false) $apacheVirtualHost->setDomainRoot($this->domainRoot); $apacheVirtualHost->setHomeRoot($this->domainRoot); $apacheVirtualHost->setServerAdmin($this->email); - $apacheVirtualHost->setDomainAlias('*.'.$this->domain); + $apacheVirtualHost->setDomainAlias('*.' . $this->domain); $virtualHostSettings = $apacheVirtualHost->getSettings(); @@ -78,7 +78,7 @@ public function configureVirtualHost($fixPermissions = false) if ($findDomainSSL) { $findDomainSSLCertificate = $findDomainSSL; } else { - $findMainDomainWildcardSSLCertificate = \App\Models\DomainSslCertificate::where('domain', '*.'.$catchMainDomain) + $findMainDomainWildcardSSLCertificate = \App\Models\DomainSslCertificate::where('domain', '*.' . $catchMainDomain) ->first(); if ($findMainDomainWildcardSSLCertificate) { $findDomainSSLCertificate = $findMainDomainWildcardSSLCertificate; @@ -115,13 +115,14 @@ public function configureVirtualHost($fixPermissions = false) } file_put_contents($sslCertificateChainFile, $findDomainSSLCertificate->certificate_chain); } + if (is_file($sslCertificateFile)) { + $apacheVirtualHost->setPort(443); + $apacheVirtualHost->setSSLCertificateFile($sslCertificateFile); + $apacheVirtualHost->setSSLCertificateKeyFile($sslCertificateKeyFile); + $apacheVirtualHost->setSSLCertificateChainFile($sslCertificateChainFile); - $apacheVirtualHost->setPort(443); - $apacheVirtualHost->setSSLCertificateFile($sslCertificateFile); - $apacheVirtualHost->setSSLCertificateKeyFile($sslCertificateKeyFile); - $apacheVirtualHost->setSSLCertificateChainFile($sslCertificateChainFile); - - $virtualHostSettingsWithSSL = $apacheVirtualHost->getSettings(); + $virtualHostSettingsWithSSL = $apacheVirtualHost->getSettings(); + } } // End install SSL @@ -131,15 +132,15 @@ public function configureVirtualHost($fixPermissions = false) if (file_exists($domainIndexFile)) { $domainIndexFileContent = file_get_contents($domainIndexFile); if (str_contains($domainIndexFileContent, 'Apache2 Debian Default Page')) { - $indexContent = file_get_contents(base_path('resources/views/actions/samples/apache/html/app-index.html')); - file_put_contents($this->domainPublic . '/index.html', $indexContent); + $indexContent = file_get_contents(base_path('resources/views/actions/samples/apache/html/app-index.html')); + file_put_contents($this->domainPublic . '/index.html', $indexContent); } } shell_exec('chown -R www-data:www-data ' . $this->domainPublic); shell_exec('chmod -R 755 ' . $this->domainPublic); } - if(!is_dir($this->domainPublic)){ + if (!is_dir($this->domainPublic)) { return false; }