Sindbad~EG File Manager

Current Path : /opt/nginxhttpd_/vendor/extensions-cpanel/cpanel-development-lib/src/Helper/
Upload File :
Current File : //opt/nginxhttpd_/vendor/extensions-cpanel/cpanel-development-lib/src/Helper/ConfigHelper.php

<?php


namespace O2switch\CpanelLib\Helper;

use O2switch\CpanelLib\Entity\Customization;

/**
 * Asset management : manage the Servers and IP address available
 */
class ConfigHelper
{

    const ASN_O2SWITCH = 'AS50474';

    /** @var array */
    private $servers;
    /** @var array */
    private $ips;
    /** @var array */
    private $conf;
    /** @var int */
    private $maxCustomIp;

    public function __construct(array $conf)
    {
        if(!isset($conf['nginxServers'], $conf['varnishServers'], $conf['lslbServers'], $conf['ipList'], $conf['max_custom_ip'])){
            throw new \InvalidArgumentException("The 'conf' is invalid, we expect the following keys : nginxServers, varnishServers, lslbServers, ipList, max_custom_ip.");
        }
        $this->conf = $conf;
        $this->servers = $conf['nginxServers'];
        $this->ips = $conf['ipList'];
        $this->maxCustomIp = $conf['max_custom_ip'];
    }

    public function getServersAndIpList(){
        return $this->ips;
    }

    /**
     * Return the servername that manage the IP passed in parameters
     * @param $ip
     * @param string $type
     * @return bool|int|string
     */
    public function getServerNameByIpAndType(string $ip, string $type = 'nginx')
    {
        if($type === 'nginx') {
            foreach ($this->conf['ipList'] as $servername => $serverdata) {
                foreach($serverdata as $k => $d){
                    if(is_array($d)){
                        if($k == $ip){
                            return $servername;
                        }
                    } else {
                        if($d == $ip){
                            return $servername;
                        }
                    }
                }
            }

            return false;
        } elseif($type === 'varnish'){
            foreach ($this->conf['varnishServers'] as $servername => $serverdata) {
                if (in_array($ip, $serverdata)) {
                    return $servername;
                }
            }
        } elseif($type === 'lslb') {
            foreach ($this->conf['lslbServers'] as $servername => $serverdata) {
                if (in_array($ip, $serverdata)) {
                    return $servername;
                }
            }
        } else {
            return false;
        }
    }

    /**
     * @param array $customerDomains
     * @return array List of IPs
     */
    public function getUsedIp(array $customerDomains){
        $usedIp = [];
        foreach ($customerDomains as $k => $d){
            if($d instanceof Customization && isset($d->getChains()[0])){
                $usedIp[] = $d->getChains()[0]->getServerIp();
                continue;
            }

            if(is_array($d) && isset($d['ip'], $d['originalIp']) && $d['ip'] == $d['originalIp']){
                continue;
            }

            if(is_array($d) && isset($d['customization']) && is_a($d['customization'], '\\App\\Entity\\Customization')){
                if(method_exists($d['customization'], 'getChains') && isset($d['ip'])) {
                    $usedIp[] = $d['ip'];
                }
                continue;
            }

            if(!is_array($d) || !isset($d['ip']) || !$this->checkIpAvailable($d['ip'])){
                continue;
            }

            $usedIp[] = $d['ip'];
        }
        return array_values(array_unique($usedIp));
    }

    /**
     * Return true if customer can customize domain
     * @param array $customizations
     * @return bool
     */
    public function canCustomizeDomain(array $customizations){
        return $this->maxCustomIp > count($customizations);
    }

    /**
     * Return the server nicename.
     *
     * @param string $serverName
     * @return string
     */
    public function getNiceName(string $serverName) : string
    {
        if (isset($this->servers[$serverName]) && isset($this->servers[$serverName]['niceName'])) {
            return $this->servers[$serverName]['niceName'];
        }

        return $serverName;
    }

    /**
     * Return an array (or false in case of error) of the description details (country, asn, etc...) of the IP address
     * @param $ip
     * @return array|bool
     */
    public function getDescriptionDetails(string $ip){
        foreach($this->ips as $serverName => $ipsOnServer){
            foreach($ipsOnServer as $k => $d){
                if(is_array($d)){
                    // A description/array is attached to the IP, $k is the IP
                    if($k == $ip){
                        // $d main not contains everything ...
                        $default = $this->servers[$serverName]['defaultDesc'] ?? [];
                        return [
                            'ipCountry' => $d['ipCountry'] ?? $default['ipCountry'],
                            'ipAsn' => $d['ipAsn'] ?? $default['ipAsn'],
                            'srvCountry' => $d['srvCountry'] ?? $default['srvCountry'],
                            'desc' => $d['desc'] ?? $default['desc'],
                        ];
                    }
                } else {
                    // No description so we get the default one from the server
                    if($d == $ip){
                        return $this->servers[$serverName]['defaultDesc'] ?? [];
                    }
                }
            }
        }
        return false;
    }

    /**
     * Return a nice string description of the IP address
     * @param string $ip
     * @return string
     */
    public function getNiceDescription(string $ip){
        $d = $this->getDescriptionDetails($ip);
        if(is_array($d)){
            // Description can use replacement placeholder
            // %ip - %type - %ipCountryIso2 - %srvCountryIso2 - %asn
            $r = str_replace('%type', $d['ipAsn'] == self::ASN_O2SWITCH ? 'Interne' : 'Externe', $d['desc']);
            //$r = str_replace('%ip', $ip, $r);
            $r = str_replace('%ipCountryIso2', $d['ipCountry'], $r);
            $r = str_replace('%srvCountryIso2', $d['srvCountry'], $r);
            $r = str_replace('%asn', $d['ipAsn'], $r);
            return $r;
        }
        return "";
    }

    /**
     * Return the list of IP available in an array.
     *
     * @param bool $withServerNicename
     *                                 if on : return $ip[] => [ip, description]
     *                                 if off : return $ip[] => ip
     *
     * @return array
     */
    public function getIpList(bool $withServerNicename = false)
    {
        $ipList = [];
        foreach ($this->ips as $servername => $ipsOnServer) {
            foreach ($ipsOnServer as $k => $ip) {
                // Array = IP with a descriptive array of infos, to the key is the IP in this case
                if(is_array($ip)){
                    $ip = $k;
                }

                if ($withServerNicename) {
                    $ipList[] = [
                        'ip' => $ip,
                        'nicename' => $this->getNiceDescription($ip),
                    ];
                } else {
                    $ipList[] = $ip;
                }
            }
        }

        return $ipList;
    }

    /**
     * Return true if $ip is available.
     *
     * @param string $ip
     *
     * @return bool
     */
    public function checkIpAvailable($ip)
    {
        return in_array($ip, $this->getIpList(false));
    }

    /**
     * Return the server main IP, used for the API.
     *
     * @param string $serverName
     *
     * @return string|bool
     */
    public function getServerMainIp(string $serverName)
    {
        return $this->getServerParam($serverName, 'mainIp');
    }

    public function getServerPort(string $serverName, string $appType){
        return $this->getServerParam($serverName, $appType !== 'forwarder' && $appType !== 'forwarder_port' ? 'port' : 'forwarder_port');
    }

    /**
     * Return the server token, used for the API.
     *
     * @param string $serverName
     * @return string|bool
     */
    public function getServerToken(string $serverName)
    {
        return $this->getServerParam($serverName, 'token');
    }

    /**
     * Return the server secret, used for the API.
     *
     * @param string $serverName
     *
     * @return string|bool
     */
    public function getServerSecret(string $serverName)
    {
        return $this->getServerParam($serverName, 'secretHash');
    }

    /**
     * Return the server algo, used for the API.
     *
     * @param string $serverName
     *
     * @return string|bool
     */
    public function getServerAlgo(string $serverName)
    {
        return $this->getServerParam($serverName, 'algo');
    }

    /**
     * Return an array with all the auth params : token, algo, secret hash.
     *
     * @param $serverName
     */
    public function getAuthParams(string $serverName)
    {
        return [
            'hash' => $this->getServerSecret($serverName),
            'algo' => $this->getServerAlgo($serverName),
            'token' => $this->getServerToken($serverName),
        ];
    }

    /**
     * Return the servername from an IP.
     *
     * @param string $ip
     *
     * @return string|bool
     */
    public function getServerNameByIp(string $ip, ?string $app = null)
    {
        if($app === 'varnish' || $app === 'xtremcache'){
            return 'varnish-1';
        }
        if($app === 'lslb' || $app === 'litespeed') {
            return 'lslb-1';
        }

        foreach ($this->ips as $servername => $serverdata) {
            if (in_array($ip, $serverdata)) {
                return $servername;
            }
            if(isset($serverdata[$ip])){
                return $servername;
            }
        }

        return false;
    }

    protected function getServerParam(string $serverName, string $paramName)
    {
        if (isset($this->servers[$serverName]) && isset($this->servers[$serverName][$paramName])) {
            return $this->servers[$serverName][$paramName];
        }
        if (isset($this->conf['lslbServers'][$serverName]) && isset($this->conf['lslbServers'][$serverName][$paramName])) {
            return $this->conf['lslbServers'][$serverName][$paramName];
        }
        if (isset($this->conf['varnishServers'][$serverName]) && isset($this->conf['varnishServers'][$serverName][$paramName])) {
            return $this->conf['varnishServers'][$serverName][$paramName];
        }

        return false;
    }
}

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