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/WhmApi.php

<?php


namespace O2switch\CpanelLib\Client;


class WhmApi
{
    const HTTP_TIMEOUT = 5;
    const RESELLER_PACKAGE_NAME = 'subaccount';

    /**
     * @var \GuzzleHttp\Client
     */
    private $httpClient;

    /**
     * @var \Monolog\Logger
     */
    private $log;

    private $user;

    private $pass;

    private $endpoint;

    /**
     * Whm constructor.
     * @param \GuzzleHttp\Client $guzzle
     * @param array $params user & pass & endpoint
     * @param \Monolog\Logger $log
     */
    public function __construct(array $params, \GuzzleHttp\Client $guzzle, \Monolog\Logger $log)
    {
        $this->httpClient = $guzzle;
        $this->log = $log;
        $this->user = $params['user'];
        $this->pass = $params['pass'];
        $this->endpoint = $params['endpoint'];
    }

    /**
     * Call a $function with $params from the WHM API $apiVersion and return the data in an array
     *
     * @param string $function
     * @param array $params
     * @param string $apiVersion
     * @return array
     *              success
     *              data
     *              metadata
     */
    public function api1($function, $params = [], $apiVersion='1'){
        $endpoint = $this->createFinalEndpoint($function, $params, 'json', $apiVersion);

        try {
            $res = $this->httpClient->request('GET', $endpoint, [
                'auth' => [$this->user, $this->pass],
                'verify' => false,
                'connect_timeout' => self::HTTP_TIMEOUT,

            ]);
        } catch (\Exception $e){
            $this->log->error("Error on the WHM API 1 call with Guzzle", [
                'endpoint' => $endpoint,
                'user' => $this->user,
                'error' => $e->getMessage()
            ]);

            return [
                'success' => false,
                'data' => [],
                'metadata' => []
            ];
        }

        $parsedRes = $this->parseResponse($res);

        if($parsedRes['success'] === false){
            $this->log->error("Error on the WHM API 1", array_merge($parsedRes, [
                'endpoint' => $endpoint,
                'user' => $this->user
            ]));
        } else {
            $this->log->info("Successfull called to the WHM API 1", [
                'user' => $this->user,
                'endpoint' => $endpoint
            ]);
        }

        return $parsedRes;
    }

    /**
     * Check that the API call is successful, check for errors
     * @param \GuzzleHttp\Psr7\Response $res
     * @return array
     *              success (bool)
     *              data (array)
     *              metadata (array)
     */
    private function parseResponse(\GuzzleHttp\Psr7\Response $res){
        if ($res->getStatusCode() !== 200){
            $err = [
                'success' => false,
                'message' => 'Not a 200 response, getStatusCode = '.$res->getStatusCode(),
                'data' => []
            ];
            return $err;
        }

        $body = json_decode($res->getBody(), true);

        $jsonErrCode = json_last_error();

        if($jsonErrCode !== JSON_ERROR_NONE){
            $err = [
                'success' => false,
                'message' => 'Problem while decoding the JSON return of api call, json_last_error ='. $jsonErrCode,
                'data' => []
            ];
            return $err;
        }

        if(!isset($body['metadata']) || !isset($body['metadata']['reason']) || !isset($body['metadata']['result'])){
            $err = [
                'success' => false,
                'message' => 'Metadata or reason and result keys not available',
                'data' => []
            ];
            return $err;
        }

        if($body['metadata']['result'] != 1){
            $err = [
                'success' => false,
                'message' => $body['metadata']['result'] . ' - ' . $body['metadata']['reason'],
                'data' => []
            ];
            return $err;
        }

        return [
            'success' => true,
            'data' => isset($body['data']) ? $body['data'] : [],
            'metadata' => $body['metadata'],
        ];
    }

    /**
     * Create the api endpoint
     * @param string $function
     * @param array $params
     * @param string $outputFormat
     * @param string $apiVersion
     * @return string
     */
    private function createFinalEndpoint($function, $params, $outputFormat='json', $apiVersion='1'){

        if($outputFormat != 'json' || $outputFormat != 'xml'){
            $outputFormat = 'json';
        }

        $endpoint = $this->endpoint . '/' . $outputFormat . '-api/' . $function . '?api.version=' . $apiVersion;

        foreach($params as $k => $v){
            $endpoint .= '&' . $k . '=' . urlencode($v);
        }

        return $endpoint;
    }
}

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