added fileGetContents

This commit is contained in:
tracer 2022-10-08 10:53:31 +02:00
parent f66298842a
commit 2e1f1ac8b1
1 changed files with 130 additions and 98 deletions

View File

@ -7,108 +7,140 @@ use UnhandledMatchError;
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
/** /**
* *
*/ */
class ApiController class ApiController
{ {
/**
* @param String $requestType
* @param String $serverName
* @param int $versionIP
* @param String $apiKey
* @param String $command
* @param String $serverType
* @param array $body
*
* @return array
*/
function sendCommand(string $requestType, string $serverName, int $versionIP, string $apiKey, string $command, string $serverType, array $body = []): array
{
$error = false;
$curl = curl_init();
try { /**
match ($serverType) { * @param String $requestType
'panel' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command), * @param String $serverName
'nameserver' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command) * @param int $versionIP
}; * @param String $apiKey
} catch (UnhandledMatchError) { * @param String $command
echo 'Unhandled match: ' . $serverType; * @param String $serverType
} * @param array $body
*
curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1); * @return array
curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 19999); */
curl_setopt(handle: $curl, option: CURLOPT_HTTP_VERSION, value: CURL_HTTP_VERSION_2TLS); function sendCommand(string $requestType, string $serverName, int $versionIP, string $apiKey, string $command, string $serverType, array $body = []): array
{
if ($versionIP == 4) { $error = false;
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4); $curl = curl_init();
} else {
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6); try {
} match ($serverType) {
'panel' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command),
curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]); 'nameserver' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command)
};
if ($requestType == "POST") { } catch (UnhandledMatchError) {
curl_setopt(handle: $curl, option: CURLOPT_POST, value: true); echo 'Unhandled match: ' . $serverType;
curl_setopt(handle: $curl, option: CURLOPT_POSTFIELDS, value: $body); }
}
if ($requestType == "PUT") { curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1);
curl_setopt(handle: $curl, option: CURLOPT_CUSTOMREQUEST, value: 'PUT'); curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 19999);
curl_setopt(handle: $curl, option: CURLOPT_POSTFIELDS, value: json_encode(value: $body)); curl_setopt(handle: $curl, option: CURLOPT_HTTP_VERSION, value: CURL_HTTP_VERSION_2TLS);
}
if ($versionIP == 4) {
curl_setopt(handle: $curl, option: CURLOPT_CUSTOMREQUEST, value: $requestType); curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
} else {
if ($resultJSON = curl_exec(handle: $curl)) { curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
$httpResponse = curl_getinfo(handle: $curl)['http_code']; }
switch ($httpResponse) { curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
case 200:
$apiResult = json_decode(json: $resultJSON); if ($requestType == "POST") {
if ($command == "ping") { curl_setopt(handle: $curl, option: CURLOPT_POST, value: true);
if ($apiResult->response == "pong") { curl_setopt(handle: $curl, option: CURLOPT_POSTFIELDS, value: $body);
$result = $apiResult->response; }
} else { if ($requestType == "PUT") {
$result = $apiResult; curl_setopt(handle: $curl, option: CURLOPT_CUSTOMREQUEST, value: 'PUT');
} curl_setopt(handle: $curl, option: CURLOPT_POSTFIELDS, value: json_encode(value: $body));
} else { }
$result = $resultJSON;
} curl_setopt(handle: $curl, option: CURLOPT_CUSTOMREQUEST, value: $requestType);
break;
case 400: if ($resultJSON = curl_exec(handle: $curl)) {
$result = $resultJSON; $httpResponse = curl_getinfo(handle: $curl)['http_code'];
break;
case 401: switch ($httpResponse) {
$result = 'Missing or wrong API Key'; case 200:
$error = true; $apiResult = json_decode(json: $resultJSON);
break; if ($command == "ping") {
case 404: if ($apiResult->response == "pong") {
$result = '404 Not Found'; $result = $apiResult->response;
break; } else {
case 500: $result = $apiResult;
$result = 'server error'; }
break; } else {
default: $result = $resultJSON;
$result = 'Unhandled error: ' . $httpResponse; }
} break;
} else { case 400:
$error = true; $result = $resultJSON;
$result = curl_error(handle: $curl); break;
} case 401:
$result = 'Missing or wrong API Key';
$info = curl_getinfo(handle: $curl); $error = true;
$responseTime = $info['total_time']; break;
case 404:
curl_close(handle: $curl); $result = '404 Not Found';
return [ break;
'responseTime' => $responseTime, case 500:
'error' => $error, $result = 'server error';
'data' => $result, break;
'header' => $httpResponse ?? '' default:
]; $result = 'Unhandled error: ' . $httpResponse;
} }
} else {
$error = true;
$result = curl_error(handle: $curl);
}
$info = curl_getinfo(handle: $curl);
$responseTime = $info['total_time'];
curl_close(handle: $curl);
return [
'responseTime' => $responseTime,
'error' => $error,
'data' => $result,
'header' => $httpResponse ?? ''
];
}
function fileGetContents(string $url, int $versionIP): ?array
{
$curl = curl_init(url: $url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
if ($versionIP == 4) {
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
} else {
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
}
curl_setopt_array(handle: $curl, options: $options);
$content = curl_exec(handle: $curl);
$error = curl_errno(handle: $curl);
$errorMessage = curl_error(handle: $curl);
$header = curl_getinfo(handle: $curl);
curl_close(handle: $curl);
$header['error'] = $error;
$header['errorMessage'] = $errorMessage;
$header['content'] = $content;
return $header;
}
} }