PhyrePanel-mirror/web/app/MasterDomain.php

160 lines
6.8 KiB
PHP
Raw Normal View History

2024-04-22 11:14:05 +00:00
<?php
namespace App;
2024-05-13 11:19:50 +00:00
use App\Models\DomainSslCertificate;
2024-04-22 11:14:05 +00:00
class MasterDomain
{
public $domain;
public $email;
public $country = 'US';
public $locality = 'New York';
public $organization = 'PhyrePanel';
public $domainPublic = '/var/www/html';
public $domainRoot = '/var/www';
public function __construct()
{
$generalSettings = Settings::general();
$this->domain = $generalSettings['master_domain'];
$this->email = $generalSettings['master_email'];
$this->country = $generalSettings['master_country'];
$this->locality = $generalSettings['master_locality'];
$this->organization = $generalSettings['organization_name'];
}
public function configureVirtualHost()
{
2024-04-30 09:18:52 +00:00
// check is valid domain
if (!filter_var($this->domain, FILTER_VALIDATE_DOMAIN)) {
return false;
}
2024-04-22 11:14:05 +00:00
$apacheVirtualHostBuilder = new \App\VirtualHosts\ApacheVirtualHostBuilder();
$apacheVirtualHostBuilder->setDomain($this->domain);
$apacheVirtualHostBuilder->setDomainPublic($this->domainPublic);
$apacheVirtualHostBuilder->setDomainRoot($this->domainRoot);
$apacheVirtualHostBuilder->setHomeRoot($this->domainRoot);
$apacheVirtualHostBuilder->setServerAdmin($this->email);
2024-05-13 11:30:41 +00:00
$apacheVirtualHostBuilder->setDomainAlias('*.'.$this->domain);
2024-04-22 11:14:05 +00:00
$apacheBaseConfig = $apacheVirtualHostBuilder->buildConfig();
2024-05-02 18:11:57 +00:00
shell_exec('mkdir -p /var/www/logs/apache2');
shell_exec('touch /var/www/logs/apache2/bytes.log');
shell_exec('touch /var/www/logs/apache2/access.log');
shell_exec('touch /var/www/logs/apache2/error.log');
2024-04-22 11:14:05 +00:00
if (!empty($apacheBaseConfig)) {
2024-05-13 12:16:31 +00:00
file_put_contents('/etc/apache2/sites-available/zzz-'.$this->domain.'.conf', $apacheBaseConfig);
2024-05-13 12:22:02 +00:00
shell_exec('ln -s /etc/apache2/sites-available/zzz-'.$this->domain.'.conf /etc/apache2/sites-enabled/zzz-'.$this->domain.'.conf');
2024-04-22 11:14:05 +00:00
}
2024-05-13 11:19:50 +00:00
// Install SSL
2024-04-22 11:14:05 +00:00
$findDomainSSLCertificate = null;
2024-05-13 11:19:50 +00:00
$catchMainDomain = '';
$domainExp = explode('.', $this->domain);
if (count($domainExp) > 0) {
unset($domainExp[0]);
$catchMainDomain = implode('.', $domainExp);
}
2024-04-22 11:14:05 +00:00
// Try to find wildcard SSL certificate
$findDomainSSLCertificateWildcard = \App\Models\DomainSslCertificate::where('domain', '*.' . $this->domain)
->where('is_wildcard', 1)
->first();
if ($findDomainSSLCertificateWildcard) {
$findDomainSSLCertificate = $findDomainSSLCertificateWildcard;
} else {
$findDomainSSL = \App\Models\DomainSslCertificate::where('domain', $this->domain)->first();
2024-05-13 11:19:50 +00:00
if ($findDomainSSL) {
$findDomainSSLCertificate = $findDomainSSL;
} else {
$findMainDomainWildcardSSLCertificate = \App\Models\DomainSslCertificate::where('domain', '*.'.$catchMainDomain)
->first();
if ($findMainDomainWildcardSSLCertificate) {
$findDomainSSLCertificate = $findMainDomainWildcardSSLCertificate;
}
}
2024-04-22 11:14:05 +00:00
}
if ($findDomainSSLCertificate) {
$certsFolderName = $findDomainSSLCertificate->domain;
$certsFolderName = str_replace('*.', 'wildcard.', $certsFolderName);
$sslCertificateFile = $this->domainRoot . '/certs/' . $certsFolderName . '/public/cert.pem';
$sslCertificateKeyFile = $this->domainRoot . '/certs/' . $certsFolderName . '/private/key.private.pem';
$sslCertificateChainFile = $this->domainRoot . '/certs/' . $certsFolderName . '/public/fullchain.pem';
if (!empty($findDomainSSLCertificate->certificate)) {
if (!is_dir($this->domainRoot . '/certs/' . $certsFolderName . '/public')) {
mkdir($this->domainRoot . '/certs/' . $certsFolderName . '/public', 0755, true);
}
file_put_contents($sslCertificateFile, $findDomainSSLCertificate->certificate);
}
if (!empty($findDomainSSLCertificate->private_key)) {
if (!is_dir($this->domainRoot . '/certs/' . $certsFolderName . '/private')) {
mkdir($this->domainRoot . '/certs/' . $certsFolderName . '/private', 0755, true);
}
file_put_contents($sslCertificateKeyFile, $findDomainSSLCertificate->private_key);
}
if (!empty($findDomainSSLCertificate->certificate_chain)) {
if (!is_dir($this->domainRoot . '/certs/' . $certsFolderName . '/public')) {
mkdir($this->domainRoot . '/certs/' . $certsFolderName . '/public', 0755, true);
}
file_put_contents($sslCertificateChainFile, $findDomainSSLCertificate->certificate_chain);
}
$apacheVirtualHostBuilder->setPort(443);
$apacheVirtualHostBuilder->setSSLCertificateFile($sslCertificateFile);
$apacheVirtualHostBuilder->setSSLCertificateKeyFile($sslCertificateKeyFile);
$apacheVirtualHostBuilder->setSSLCertificateChainFile($sslCertificateChainFile);
$apacheBaseConfigWithSSL = $apacheVirtualHostBuilder->buildConfig();
if (!empty($apacheBaseConfigWithSSL)) {
// Add SSL options conf file
$apache2SSLOptionsSample = view('actions.samples.ubuntu.apache2-ssl-options-conf')->render();
$apache2SSLOptionsFilePath = '/etc/apache2/phyre/options-ssl-apache.conf';
if (!file_exists($apache2SSLOptionsFilePath)) {
if (!is_dir('/etc/apache2/phyre')) {
mkdir('/etc/apache2/phyre');
}
file_put_contents($apache2SSLOptionsFilePath, $apache2SSLOptionsSample);
}
2024-05-13 12:16:31 +00:00
file_put_contents('/etc/apache2/sites-available/zzz-'.$this->domain.'-ssl.conf', $apacheBaseConfigWithSSL);
2024-05-13 12:20:29 +00:00
shell_exec('ln -s /etc/apache2/sites-available/zzz-'.$this->domain.'-ssl.conf /etc/apache2/sites-enabled/zzz-'.$this->domain.'-ssl.conf');
2024-04-22 11:14:05 +00:00
}
}
// End install SSL
2024-04-30 09:33:36 +00:00
$domainIndexFile = $this->domainPublic . '/index.html';
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);
}
}
2024-04-22 11:14:05 +00:00
shell_exec('chown -R www-data:www-data ' . $this->domainPublic);
shell_exec('chmod -R 755 ' . $this->domainPublic);
shell_exec('systemctl restart apache2');
}
}