Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/ncssl/source/src/Service/CpanelApi/CpanelApi.php

<?php

namespace App\Service\CpanelApi;

use App\Service\CpanelApi\Adapter\AdapterInterface;
use App\Service\NcPlugin\PluginException;
use App\Service\State\StateUser;
use App\Traits\ClearSensitiveData;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class CpanelApi
{
    use ClearSensitiveData;

    private AdapterInterface $adapter;

    public function __construct(
        #[Autowire(service: 'cpanel.adapter.cli')]
        AdapterInterface $cliAdapter,
        #[Autowire(service: 'cpanel.adapter.web')]
        AdapterInterface $webAdapter,
        private readonly StateUser $stateUser,
        private readonly LoggerInterface $cpanelLogger,
    ) {
        $this->adapter = PHP_SAPI === "cli" ? $cliAdapter : $webAdapter;
    }

    /**
     * @param $module
     * @param $function
     * @param array $params
     * @return CpanelApiResponse
     * @throws PluginException
     */
    public function uapiCall($module, $function, array $params = []): CpanelApiResponse
    {
        $user = $this->stateUser->getUser();

        try {
            $res = $this->adapter->call($module, $function, $params);
            $res = $res['cpanelresult']['result'];
            $this->checkException($res);
        } catch (\Throwable $e) {
            $this->clearSensitiveDataFromParams($params);
            $error_message = $this->clearSensitiveData($e->getMessage());
            $this->cpanelLogger->error('Cpanel uApi Error.', array_merge([
                'error_message' => $error_message,
                'error_file' => $e->getFile(),
                'error_line' => $e->getLine(),
                'module' => $module,
                'function' => $function,
                'login' => $user->getNcLogin(),
                'username' => $user->getName(),
            ], $params));

            throw new PluginException($error_message, $e->getCode(), $e);
        }

        $this->clearSensitiveDataFromParams($params);
        $messageResponse = $res['data']['message'] ?? null;

        $this->cpanelLogger->info('Cpanel uApi success.', array_merge([
            'module' => $module,
            'function' => $function,
            'login' => $user->getNcLogin(),
            'username' => $user->getName(),
            'cpanel_response_message' => $messageResponse,
        ], $params));

        return new CpanelApiResponse($res);
    }

    /**
     * @param $module
     * @param $function
     * @param array $params
     * @return CpanelApiResponse
     * @throws PluginException
     */
    public function api2Call($module, $function, $params = array())
    {
        $user = $this->stateUser->getUser();

        try {
            $res = $this->adapter->callApi2($module, $function, $params);
            $res = $res['cpanelresult'];
            $this->checkException($res);
        } catch (\Throwable $e) {
            $this->clearSensitiveDataFromParams($params);
            $error_message = $this->clearSensitiveData($e->getMessage());
            $this->cpanelLogger->error('Cpanel Api2 Error.', array_merge([
                'error_message' => $error_message,
                'error_file' => $e->getFile(),
                'error_line' => $e->getLine(),
                'module' => $module,
                'function' => $function,
                'login' => $user->getNcLogin(),
                'username' => $user->getName(),
            ], $params));

            throw new PluginException($error_message, $e->getCode(), $e);
        }

        $this->clearSensitiveDataFromParams($params);
        $messageResponse = $res['data']['message'] ?? null;

        $this->cpanelLogger->info('Cpanel Api2 success.', array_merge([
            'module' => $module,
            'function' => $function,
            'login' => $user->getNcLogin(),
            'username' => $user->getName(),
            'cpanel_response_message' => $messageResponse,
        ], $params));

        return new CpanelApiResponse($res);
    }

    /**
     * @return AdapterInterface
     */
    public function getAdapter(): AdapterInterface
    {
        return $this->adapter;
    }

    /**
     * Throws PluginException if ERRORS part isn't empty.
     *
     * @param array $response
     *
     * @return void
     *@throws PluginException
     *
     */
    private function checkException(array $response): void
    {
        if (!empty($response['errors'])) {
            // TODO it might be need to refactor creation of exception message
            throw new PluginException(implode("\n", $response['errors']));
        }
    }

    /**
     * @param array $params
     */
    private function clearSensitiveDataFromParams(array &$params): void
    {
        unset($params['cabundle'], $params['cert'], $params['key'], $params['certificate']);
    }
}
Back to Directory File Manager