Sindbad~EG File Manager
<?php
namespace App\Parser;
/**
* Tools to parse the httpd.conf and return useful data in an array
* Class HttpdParser
* @package O2switch\Parser
*/
class ApacheHttpdParser extends AbstractHttpdParser
{
// Regex that matches multiples lines for each VirtualHost
private const VIRTUALHOST_REGEX = '#\<VirtualHost ([0-9a-f\.\:\[\]]+)\:([0-9]+)\>(.+?)\<\/VirtualHost\>#s';
private const CONFIG_TYPE = 'apache';
public function __construct(int $httpsPort, int $httpPort)
{
parent::__construct($httpsPort, $httpPort);
}
/**
* Parse the apache httpd.conf and return a boolean
* @param string $filepath
* @param string|null $domainToUpdate If provided, just update one domain
* @return bool
*/
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, $matches, PREG_PATTERN_ORDER);
//dd(count($matches[3]));
foreach($matches[3] as $k => $vhost){
if(strpos($vhost, 'aforem-edu.net')){
continue;
}
preg_match("#ServerName (.+?)\n#", $vhost, $rawServerName);
$serverName = $rawServerName[1];
$ip = $matches[1][$k];
$port = $matches[2][$k];
preg_match("#ServerAlias (.+?)\n#", $vhost, $aliases);
$rawAliases = $aliases[1] ?? "www.$serverName";
if($domainToUpdate !== null && ($serverName !== $domainToUpdate && !in_array($domainToUpdate, explode(' ', $rawAliases)))){
continue;
}
preg_match("#DocumentRoot (.+?)\n#", $vhost, $docRoot);
$rawDocRoot = $docRoot[1];
if (strpos($rawDocRoot, '/home') === 0) {
// In the case of wildcard, we switch up. We wan't the "serverName' to be '*.xxxx.xyz' because it's
// used as the redis keys and Openresty will match this (avoid another loopup with Redis)
if (strpos($serverName, '_wildcard_') === 0) {
$tmp = $serverName;
$serverName = $rawAliases;
$rawAliases = $tmp;
}
$id = $serverName;
$docRoot = explode('/', $rawDocRoot); // !!! tester /home
$user = $docRoot[2]; // recup homedir aussi
$homedir = '/' . $docRoot[1] . '/' . $docRoot[2];
$this->result[$id]['cpUser'] = $user;
$this->result[$id]['docRoot'] = $rawDocRoot;
$this->result[$id]['homeDir'] = $homedir;
} else {
$id = 'default'; // Special keys for the main/default vhosts
}
$this->result[$id]['serverName'] = $serverName;
$this->result[$id]['serverAlias'] = explode(' ', $rawAliases);
$this->result[$id]['rawServerAlias'] = $rawAliases;
$this->result[$id]['ip'] = $ip;
$this->result[$id]['configType'] = self::CONFIG_TYPE;
if($port == $this->httpsPort){
preg_match("#SSLCertificateFile (.+?)\n#s", $vhost, $sslCertFilepath);
$sslCertFilepath = $sslCertFilepath[1] ?? null;
$this->result[$id]['httpsPort'] = $port;
$this->result[$id]['sslCertificateFilepath'] = $sslCertFilepath;
} else {
$this->result[$id]['httpPort'] = $port;
}
//$this->transform();
}
$this->transform();
return true;
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists