Sindbad~EG File Manager

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

<?php


namespace O2switch\CpanelLib\Client;

use O2switch\CpanelLib\Helper\Truncator;
use Psr\Log\LoggerInterface;

/**
 * This class provide a small wrapper for the cPanel live API.
 * Class Cpanel.
 */
class CpanelApi
{

    use CpanelApiShortcutTrait;

    /**
     * @var \CPANEL
     */
    private $cpanel;

    /**
     * @var LoggerInterface
     */
    private $log;

    public function __construct(\CPANEL $cpanel, LoggerInterface $log)
    {
        $this->cpanel = $cpanel;
        $this->log = $log;
    }

    public function getcPanel()
    {
        return $this->cpanel;
    }

    public function header($data)
    {
        return $this->cpanel->header($data);
    }

    public function footer($data="")
    {
        return $this->cpanel->footer($data);
    }

    public function getVar($varname){
        return $this->cpanel->cpanelprint('$'.$varname);
    }

    /**
     * @param $module
     * @param $func
     * @param array $params
     *
     * @return array
     */
    public function api2(string $module, string $func, $params = [])
    {
        if (count($params) === false) {
            try {
                $context = [
                    'params' => Truncator::truncate($params),
                    'cpuser' => isset($_SERVER['USER']) ? $_SERVER['USER'] : 'none',
                    'user_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['USER'] : 'none',
                ];
                $this->log->info("API2 [OK] $module $func", $context);
                $result = $this->parseReturnApi2($this->cpanel->api2($module, $func));
            } catch (\Exception $e) {
                $context = [
                    'params' => Truncator::truncate($params),
                    'cpuser' => isset($_SERVER['USER']) ? $_SERVER['USER'] : 'none',
                    'user_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['USER'] : 'none',
                    'error' => $e->getMessage(),
                ];
                $this->log->warning("API2 [KO] $module $func", $context);

                return [
                    'errors' => true,
                    'exception' => $e->getMessage(),
                ];
            }

            return $result;
        }

        try {
            $result = $this->parseReturnApi2($this->cpanel->api2($module, $func, $params));
        } catch (\Exception $e) {
            $context = [
                'params' => Truncator::truncate($params),
                'cpuser' => isset($_SERVER['USER']) ? $_SERVER['USER'] : 'none',
                'user_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['USER'] : 'none',
                'error' => $e->getMessage(),
            ];
            $this->log->warning("API2 [KO] $module $func", $context);

            return [
                'errors' => true,
                'exception' => $e->getMessage(),
            ];
        }
        $context = [
            'params' => Truncator::truncate($params),
            'cpuser' => isset($_SERVER['USER']) ? $_SERVER['USER'] : 'none',
            'user_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['USER'] : 'none',
        ];
        $this->log->info("API2 [OK] $module $func", $context);

        return $result;
    }

    /**
     * @param $data
     *
     * @return bool
     */
    public function parseReturnApi2($data)
    {
        if (isset($data['cpanelresult'])) {
            return $data['cpanelresult']['data'];
        }

        return false;
    }

    /**
     * Call the UAPI of cPanel.
     *
     * @param $module
     * @param $func
     * @param array $params
     *
     * @return array
     */
    public function uapi(string $module, string $func, $params = [])
    {
        if (empty($params)) {
            try {
                $context = [
                    'params' => Truncator::truncate($params),
                    'cpuser' => isset($_SERVER['USER']) ? $_SERVER['USER'] : 'none',
                    'user_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['USER'] : 'none',
                ];
                $this->log->info("UAPI [OK] $module $func", $context);
                return $this->parseReturnUapi($this->cpanel->uapi($module, $func));
            } catch (\Exception $e) {
                $context = [
                    'params' => Truncator::truncate($params),
                    'cpuser' => isset($_SERVER['USER']) ? $_SERVER['USER'] : 'none',
                    'user_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['USER'] : 'none',
                    'error' => $e->getMessage(),
                ];
                $this->log->warning("UAPI [KO] $module $func", $context);

                return [
                    'errors' => true,
                    'exception' => $e->getMessage(),
                ];
            }
        }

        try {
            $context = [
                'params' => Truncator::truncate($params),
                'cpuser' => isset($_SERVER['USER']) ? $_SERVER['USER'] : 'none',
                'user_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['USER'] : 'none',
            ];
            $this->log->info("UAPI [OK] $module $func", $context);
            $result = $this->parseReturnUapi($this->cpanel->uapi($module, $func, $params));
        } catch (\Exception $e) {
            $context = [
                'params' => Truncator::truncate($params),
                'cpuser' => isset($_SERVER['USER']) ? $_SERVER['USER'] : 'none',
                'user_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['USER'] : 'none',
                'error' => $e->getMessage(),
            ];
            $this->log->warning("UAPI [KO] $module $func", $context);

            return [
                'errors' => true,
                'exception' => $e->getMessage(),
            ];
        }

        return $result;
    }

    /**
     * Parse the UAPI data, remove one level of the array in the response.
     *
     * @param $data
     *
     * @return bool
     */
    public function parseReturnUapi($data)
    {
        if (isset($data['cpanelresult'])) {
            $errors = isset($data['errors']) ? $data['errors'] : $data['cpanelresult']['result']['errors'];
            $metadata = isset($data['metadata']) ? $data['metadata'] : $data['cpanelresult']['result']['metadata'];
            $messages = isset($data['messages']) ? $data['messages'] : $data['cpanelresult']['result']['messages'];
            $status = isset($data['status']) ? $data['status'] : $data['cpanelresult']['result']['status'];

            return [
                'errors' => $errors,
                'result' => $data['cpanelresult']['result']['data'],
                'module' => $data['cpanelresult']['module'],
                'func' => $data['cpanelresult']['func'],
                'metadata' => $metadata,
                'messages' => $messages,
                'status' => $status,
            ];
        } else {
            return false;
        }
    }
}

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