Sindbad~EG File Manager

Current Path : /opt/nginxhttpd_/src/Parser/
Upload File :
Current File : //opt/nginxhttpd_/src/Parser/CpanelUserdataParser.php

<?php

namespace App\Parser;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;

/**
 * This class is used to explore/parse cpanel userdata file. It's will be used to enrich httpd.conf data. The main problem
 * is that cPanel will put the subdomain in the serverName. So the serverName is not the "real" domain. The real domain
 * will appear in the ServerAlias part of the Vhost. We need to map the cPanel "subdomain" to the "real" domain.
 * Class CpanelUserdataParser
 * @package O2switch\Parser
 */
class CpanelUserdataParser
{

    protected $cpUserdataPath;
    protected $parsed = [];

    public function __construct(string $cpUserdataPath)
    {
        $this->cpUserdataPath = $cpUserdataPath;
    }

    public function parse() : bool{
        $this->parsed = [];
        $finder = new Finder();
        $finder->files()->in($this->cpUserdataPath)->name('main')->depth(1);
        foreach ($finder as $file) {
            $path = $file->getRealPath();
            $user = explode('/', $path);
            $user = $user[4] ?? null;

            if(!$user || empty($user)){
                continue;
            }

            $value = Yaml::parseFile($path);
            $mainDomain = $value['main_domain'];

            // Map the real domain to the subdomain created by cPanel and that'll appear in the ServerName
            if(isset($value['addon_domains']) && is_array($value['addon_domains'])){
                foreach($value['addon_domains'] as $domain => $subdomain){
                    $this->parsed[$user][$subdomain] = $domain;
                }
            }

            // Map the parked domain to the main domain (same vhost)
            if(isset($value['parked_domains']) && is_array($value['parked_domains'])){
                foreach($value['parked_domains'] as $domain){
                    // $this->parsed[$user][$mainDomain] = $domain;
                    $this->parsed[$user][$domain] = $mainDomain;
                }
            }
        }
        return true;
    }

    public function getData(){
        return $this->parsed;
    }
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists