2022-01-26 19:35:32 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-02-05 20:26:32 +01:00
|
|
|
|
2022-01-22 16:37:00 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-02-05 20:26:32 +01:00
|
|
|
use UnhandledMatchError;
|
|
|
|
|
2022-01-26 19:35:32 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
|
2022-01-22 16:37:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2022-02-05 20:26:32 +01:00
|
|
|
class ApiController
|
2022-01-22 16:37:00 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2022-01-31 20:59:36 +01:00
|
|
|
* @param String $requestType
|
2022-01-22 16:37:00 +01:00
|
|
|
* @param String $serverName
|
|
|
|
* @param int $versionIP
|
|
|
|
* @param String $apiKey
|
|
|
|
* @param String $command
|
2022-01-31 20:59:36 +01:00
|
|
|
* @param String $serverType
|
|
|
|
* @param array $body
|
2022-01-22 16:37:00 +01:00
|
|
|
*
|
2022-01-25 20:32:34 +01:00
|
|
|
* @return array
|
2022-01-22 16:37:00 +01:00
|
|
|
*/
|
2022-02-05 20:26:32 +01:00
|
|
|
function sendCommand(string $requestType, string $serverName, int $versionIP, string $apiKey, string $command, string $serverType, array $body = []): array
|
2022-01-22 16:37:00 +01:00
|
|
|
{
|
2022-01-29 14:56:07 +01:00
|
|
|
$error = false;
|
2022-01-22 16:37:00 +01:00
|
|
|
$curl = curl_init();
|
2022-03-01 16:44:07 +01:00
|
|
|
|
2022-02-05 20:26:32 +01:00
|
|
|
try {
|
|
|
|
match ($serverType) {
|
|
|
|
'panel' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command),
|
|
|
|
'nameserver' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command)
|
|
|
|
};
|
|
|
|
} catch (UnhandledMatchError) {
|
|
|
|
echo 'Unhandled match: ' . $serverType;
|
2022-01-25 20:32:34 +01:00
|
|
|
}
|
2022-02-05 20:26:32 +01:00
|
|
|
|
2022-01-26 19:35:32 +01:00
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1);
|
2022-02-22 15:32:59 +01:00
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 19999);
|
2022-01-29 14:56:07 +01:00
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_HTTP_VERSION, value: CURL_HTTP_VERSION_2TLS);
|
2022-01-22 16:37:00 +01:00
|
|
|
|
|
|
|
if ($versionIP == 4) {
|
2022-01-26 19:35:32 +01:00
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
|
2022-01-22 16:37:00 +01:00
|
|
|
} else {
|
2022-01-26 19:35:32 +01:00
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
|
2022-01-22 16:37:00 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 19:35:32 +01:00
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
|
2022-01-22 16:37:00 +01:00
|
|
|
|
2022-01-31 20:59:36 +01:00
|
|
|
if ($requestType == "POST") {
|
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_POST, value: true);
|
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_POSTFIELDS, value: $body);
|
2022-01-27 15:14:37 +01:00
|
|
|
}
|
2022-03-01 16:44:07 +01:00
|
|
|
if ($requestType == "PUT") {
|
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_CUSTOMREQUEST, value: 'PUT');
|
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_POSTFIELDS, value: json_encode(value: $body));
|
|
|
|
}
|
2022-01-31 20:59:36 +01:00
|
|
|
|
|
|
|
curl_setopt(handle: $curl, option: CURLOPT_CUSTOMREQUEST, value: $requestType);
|
|
|
|
|
2022-01-26 19:35:32 +01:00
|
|
|
if ($resultJSON = curl_exec(handle: $curl)) {
|
|
|
|
$httpResponse = curl_getinfo(handle: $curl)['http_code'];
|
2022-01-22 16:37:00 +01:00
|
|
|
|
2022-02-05 20:26:32 +01:00
|
|
|
switch ($httpResponse) {
|
2022-01-22 16:37:00 +01:00
|
|
|
case 200:
|
2022-01-26 19:35:32 +01:00
|
|
|
$apiResult = json_decode(json: $resultJSON);
|
2022-02-05 20:26:32 +01:00
|
|
|
if ($command == "ping") {
|
2022-01-22 16:37:00 +01:00
|
|
|
if ($apiResult->response == "pong") {
|
2022-01-23 16:28:46 +01:00
|
|
|
$result = $apiResult->response;
|
2022-01-22 16:37:00 +01:00
|
|
|
} else {
|
2022-01-25 20:32:34 +01:00
|
|
|
$result = $apiResult;
|
2022-01-22 16:37:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-23 16:28:46 +01:00
|
|
|
$result = $resultJSON;
|
2022-01-22 16:37:00 +01:00
|
|
|
}
|
|
|
|
break;
|
2022-03-01 16:44:07 +01:00
|
|
|
case 400:
|
|
|
|
$result = $resultJSON;
|
|
|
|
break;
|
2022-01-22 16:37:00 +01:00
|
|
|
case 401:
|
2022-01-23 16:28:46 +01:00
|
|
|
$result = 'Missing or wrong API Key';
|
2022-01-22 16:37:00 +01:00
|
|
|
break;
|
2022-02-05 20:26:32 +01:00
|
|
|
case 404:
|
|
|
|
$result = '404 Not Found';
|
|
|
|
break;
|
|
|
|
case 500:
|
|
|
|
$result = 'server error';
|
2022-01-31 20:59:36 +01:00
|
|
|
break;
|
2022-01-22 16:37:00 +01:00
|
|
|
default:
|
2022-01-23 16:28:46 +01:00
|
|
|
$result = 'Unhandled error: ' . $httpResponse;
|
2022-01-22 16:37:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-29 14:56:07 +01:00
|
|
|
$error = true;
|
2022-01-26 19:35:32 +01:00
|
|
|
$result = curl_error(handle: $curl);
|
2022-01-22 16:37:00 +01:00
|
|
|
}
|
2022-02-22 15:32:59 +01:00
|
|
|
|
|
|
|
$info = curl_getinfo(handle: $curl);
|
|
|
|
$responseTime = $info['total_time'];
|
|
|
|
|
2022-01-26 19:35:32 +01:00
|
|
|
curl_close(handle: $curl);
|
2022-01-25 20:32:34 +01:00
|
|
|
return [
|
2022-02-22 15:32:59 +01:00
|
|
|
'responseTime' => $responseTime,
|
|
|
|
'error' => $error,
|
|
|
|
'data' => $result,
|
|
|
|
'header' => $httpResponse ?? ''
|
2022-01-25 20:32:34 +01:00
|
|
|
];
|
2022-01-22 16:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|