2024-04-22 11:14:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2024-05-13 11:19:50 +00:00
|
|
|
use App\Models\DomainSslCertificate;
|
2024-05-14 13:23:26 +00:00
|
|
|
use App\VirtualHosts\DTO\ApacheVirtualHostSettings;
|
2024-05-13 11:19:50 +00:00
|
|
|
|
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'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-14 10:15:26 +00:00
|
|
|
public function configureVirtualHost($fixPermissions = false)
|
2024-04-22 11:14:05 +00:00
|
|
|
{
|
2024-04-30 09:18:52 +00:00
|
|
|
// check is valid domain
|
|
|
|
if (!filter_var($this->domain, FILTER_VALIDATE_DOMAIN)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-14 13:23:26 +00:00
|
|
|
$apacheVirtualHost = new ApacheVirtualHostSettings();
|
|
|
|
$apacheVirtualHost->setDomain($this->domain);
|
|
|
|
$apacheVirtualHost->setDomainPublic($this->domainPublic);
|
|
|
|
$apacheVirtualHost->setDomainRoot($this->domainRoot);
|
|
|
|
$apacheVirtualHost->setHomeRoot($this->domainRoot);
|
|
|
|
$apacheVirtualHost->setServerAdmin($this->email);
|
|
|
|
$apacheVirtualHost->setDomainAlias('*.'.$this->domain);
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-05-14 13:23:26 +00:00
|
|
|
$virtualHostSettings = $apacheVirtualHost->getSettings();
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-05-14 10:15:26 +00:00
|
|
|
if ($fixPermissions) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-14 13:23:26 +00:00
|
|
|
$virtualHostSettingsWithSSL = null;
|
2024-05-14 10:15:26 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-05-14 13:23:26 +00:00
|
|
|
$apacheVirtualHost->setPort(443);
|
|
|
|
$apacheVirtualHost->setSSLCertificateFile($sslCertificateFile);
|
|
|
|
$apacheVirtualHost->setSSLCertificateKeyFile($sslCertificateKeyFile);
|
|
|
|
$apacheVirtualHost->setSSLCertificateChainFile($sslCertificateChainFile);
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-05-14 13:23:26 +00:00
|
|
|
$virtualHostSettingsWithSSL = $apacheVirtualHost->getSettings();
|
2024-04-22 11:14:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
// End install SSL
|
|
|
|
|
2024-05-14 10:15:26 +00:00
|
|
|
if ($fixPermissions) {
|
2024-05-14 13:23:26 +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-30 09:33:36 +00:00
|
|
|
}
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-05-14 13:23:26 +00:00
|
|
|
shell_exec('chown -R www-data:www-data ' . $this->domainPublic);
|
|
|
|
shell_exec('chmod -R 755 ' . $this->domainPublic);
|
2024-05-14 10:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2024-05-14 13:23:26 +00:00
|
|
|
'virtualHostSettings' => $virtualHostSettings,
|
|
|
|
'virtualHostSettingsWithSSL' => $virtualHostSettingsWithSSL ?? null
|
2024-05-14 10:15:26 +00:00
|
|
|
];
|
|
|
|
|
2024-04-22 11:14:05 +00:00
|
|
|
}
|
|
|
|
}
|