2022-01-22 16:37:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class CheckController
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $serverName
|
|
|
|
* @param int $versionIP
|
|
|
|
* @param String $apiKey
|
|
|
|
* @param String $command
|
|
|
|
*
|
2022-01-23 16:28:46 +01:00
|
|
|
* @return String
|
2022-01-22 16:37:00 +01:00
|
|
|
*/
|
2022-01-23 16:28:46 +01:00
|
|
|
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command): String
|
2022-01-22 16:37:00 +01:00
|
|
|
{
|
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
|
|
|
|
curl_setopt($curl, option: CURLOPT_RETURNTRANSFER, value: 1);
|
2022-01-23 16:28:46 +01:00
|
|
|
curl_setopt($curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
|
2022-01-22 16:37:00 +01:00
|
|
|
|
|
|
|
if ($versionIP == 4) {
|
|
|
|
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
|
|
|
|
} else {
|
|
|
|
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_setopt($curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
|
|
|
|
|
|
|
|
if ($resultJSON = curl_exec($curl)) {
|
|
|
|
$httpResponse = curl_getinfo($curl)['http_code'];
|
|
|
|
|
|
|
|
switch($httpResponse) {
|
|
|
|
case 200:
|
|
|
|
$apiResult = json_decode($resultJSON);
|
|
|
|
if ($command == "ping" ) {
|
|
|
|
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-23 16:28:46 +01:00
|
|
|
$result = $resultJSON;
|
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;
|
|
|
|
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;
|
|
|
|
default:
|
2022-01-23 16:28:46 +01:00
|
|
|
$result = 'Unhandled error: ' . $httpResponse;
|
2022-01-22 16:37:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-23 16:28:46 +01:00
|
|
|
$result = curl_error($curl);
|
2022-01-22 16:37:00 +01:00
|
|
|
}
|
|
|
|
curl_close($curl);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|