bindAPI/src/Controller/CheckController.php

90 lines
2.4 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2022-01-22 16:37:00 +01:00
namespace App\Controller;
error_reporting(error_level: E_ALL);
2022-01-22 16:37:00 +01:00
/**
*
*/
class CheckController
{
/**
* @param String $requestType
2022-01-22 16:37:00 +01:00
* @param String $serverName
* @param int $versionIP
* @param String $apiKey
* @param String $command
* @param String $serverType
* @param array $body
2022-01-22 16:37:00 +01:00
*
* @return array
2022-01-22 16:37:00 +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
{
$error = false;
2022-01-22 16:37:00 +01:00
$curl = curl_init();
if ($serverType == "panel") {
curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
} else {
curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command);
}
curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1);
curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 3000);
curl_setopt(handle: $curl, option: CURLOPT_HTTP_VERSION, value: CURL_HTTP_VERSION_2TLS);
2022-01-22 16:37:00 +01:00
if ($versionIP == 4) {
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
2022-01-22 16:37:00 +01:00
} else {
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
2022-01-22 16:37:00 +01:00
}
curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
2022-01-22 16:37:00 +01:00
if ($requestType == "POST") {
curl_setopt(handle: $curl, option: CURLOPT_POST, value: true);
curl_setopt(handle: $curl, option: CURLOPT_POSTFIELDS, value: $body);
}
curl_setopt(handle: $curl, option: CURLOPT_CUSTOMREQUEST, value: $requestType);
if ($resultJSON = curl_exec(handle: $curl)) {
$httpResponse = curl_getinfo(handle: $curl)['http_code'];
2022-01-22 16:37:00 +01:00
switch($httpResponse) {
case 200:
$apiResult = json_decode(json: $resultJSON);
2022-01-22 16:37:00 +01:00
if ($command == "ping" ) {
if ($apiResult->response == "pong") {
$result = $apiResult->response;
2022-01-22 16:37:00 +01:00
} else {
$result = $apiResult;
2022-01-22 16:37:00 +01:00
}
} else {
$result = $resultJSON;
2022-01-22 16:37:00 +01:00
}
break;
case 401:
$result = 'Missing or wrong API Key';
2022-01-22 16:37:00 +01:00
break;
2022-01-29 16:20:55 +01:00
case 404:
$result = '404 Not Found';
break;
2022-01-22 16:37:00 +01:00
default:
$result = 'Unhandled error: ' . $httpResponse;
2022-01-22 16:37:00 +01:00
}
} else {
$error = true;
$result = curl_error(handle: $curl);
2022-01-22 16:37:00 +01:00
}
curl_close(handle: $curl);
return [
2022-01-29 16:20:55 +01:00
'error' => $error,
'data' => $result,
'header' => $httpResponse ?? ''
];
2022-01-22 16:37:00 +01:00
}
}