Sindbad~EG File Manager
<?php
namespace App\Parser;
class LswsHttpdParser extends AbstractHttpdParser
{
private const VIRTUALHOST_REGEX = '#virtualhost\s+([\w.-]+)\s+{([^}]*)}#s';
private const CONFIG_TYPE_LSWS = 'lsws';
private const CONFIG_TYPE_APACHE = 'lsws-apache';
protected $apacheHttpsPort;
protected $apacheHttpPort;
public function __construct(int $httpsPort, int $httpPort, int $apacheHttpsPort, int $apacheHttpPort)
{
parent::__construct($httpsPort, $httpPort);
$this->apacheHttpsPort = $apacheHttpsPort;
$this->apacheHttpPort = $apacheHttpPort;
}
public function parse(string $filepath, ?string $domainToUpdate = null): bool
{
$httpd = $this->getContent($filepath);
if ($httpd === false) {
return false;
}
$this->result = [];
preg_match_all(self::VIRTUALHOST_REGEX, $httpd, $vhost_matches, PREG_PATTERN_ORDER);
foreach ($vhost_matches[2] as $k => $rawVhost) {
if (strpos($rawVhost, 'aforem-edu.net')) {
continue;
}
$vhostName = $vhost_matches[1][$k];
$vhost = [];
foreach (explode("\n", trim($rawVhost)) as $line) {
$line = explode('#', $line, 2)[0];
if (trim($line) === '') {
continue;
}
if (preg_match('/(\S+)\s+(.+)$/', $line, $matches)) {
$vhost[$matches[1]] = $matches[2];
}
}
$required = ['configFile', 'user'];
if (count(array_intersect_key(array_flip($required), $vhost)) !== count($required)) {
continue;
}
$configFile = '/usr/local/lsws/' . $vhost['configFile'];
$user = $vhost['user'];
$configFileContent = $this->getContent($configFile);
if (!$configFileContent) {
continue;
}
$vhostConfig = [];
$categ = null;
$skip = ['rewrite'];
foreach (explode("\n", trim($configFileContent)) as $line) {
if (preg_match('#^}.*#s', trim($line))) {
$categ = null;
continue;
}
if (in_array($categ, $skip)) {
continue;
}
$line = explode('#', $line, 2)[0];
if (trim($line) === '') {
continue;
}
if (preg_match('/(.*){$/', $line, $matches)) {
$categ = preg_replace('/\s+/', '_', trim($matches[1]));
if (!in_array($categ, $skip)) {
$vhostConfig[$categ] = [];
}
continue;
}
if (preg_match('/(\S+)\s+(.+)$/', $line, $matches)) {
if (!preg_match('#^{.*#s', trim($matches[2]))) {
if ($categ) {
$vhostConfig[$categ][$matches[1]] = $matches[2];
} else {
$vhostConfig[$matches[1]] = $matches[2];
}
}
}
}
$required = ['docRoot', 'vhDomain', 'vhAliases'];
if (count(array_intersect_key(array_flip($required), $vhostConfig)) !== count($required)) {
continue;
}
$documentRoot = $vhostConfig['docRoot'];
$serverName = $vhostConfig['vhDomain'];
$rawAliases = str_replace(',', ' ', str_replace(' ', '', $vhostConfig['vhAliases']));
$serverAlias = explode(' ', $rawAliases);
if ($domainToUpdate !== null && ($serverName !== $domainToUpdate && !in_array($domainToUpdate, $serverAlias))) {
continue;
}
if (strpos($documentRoot, '/home') === 0) {
if (strpos($serverName, '_wildcard_') === 0) {
$tmp = $serverName;
$serverName = $rawAliases;
$rawAliases = $tmp;
}
$docRoot = explode('/', $documentRoot);
$homedir = '/' . $docRoot[1] . '/' . $docRoot[2];
}
$ip = '127.0.0.1';
$certFile = (array_key_exists('vhssl', $vhostConfig) && array_key_exists('certFile', $vhostConfig['vhssl'])) ? $vhostConfig['vhssl']['certFile'] : null;
$configType = preg_replace('/' . $serverName . '_*/', '', $vhostName);
$this->result[$vhostName] = [
'cpUser' => $user,
'docRoot' => $documentRoot,
'serverName' => $serverName,
'serverAlias' => $serverAlias,
'rawServerAlias' => $rawAliases,
'ip' => $ip,
'httpPort' => $configType === 'apache' ? $this->apacheHttpPort : $this->httpPort,
'httpsPort' => $configType === 'apache' ? $this->apacheHttpsPort : $this->httpsPort,
'sslCertificateFilepath' => $certFile,
'configType' => $configType === 'apache' ? self::CONFIG_TYPE_APACHE : self::CONFIG_TYPE_LSWS,
'homeDir' => $homedir ?? null
];
}
$this->transform();
return true;
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists