Files
vcr_capture/files/service_control_api/website/index.php
2025-08-24 19:49:45 -07:00

362 lines
11 KiB
PHP

<?php
date_default_timezone_set('America/Los_Angeles');
# set default values
$status = "Unchecked";
$message = "No attempt made yet.";
$button_text = "Pending";
$button_active = false;
$button_recent = false;
$button_action = "";
$button_result = "";
$http_host = $_SERVER['HTTP_HOST'];
$capture_duration = "3";
$elapsed_time = "0";
// Service Status API Checker
$status_API_URL = "http://172.17.0.1:5000/status";
// Use file_get_contents to fetch data from the API
try {
$status_API_response = file_get_contents($status_API_URL);
} catch (Exception $e) {
$status_API_response = '{ "Message": "unknowable", "Status": "unknowable" }';
}
// Check if the request was successful
if ($status_API_response === FALSE) {
$data = "Failed to fetch data.";
}
else {
// Decode the JSON response (assuming the API returns JSON)
$data = json_decode($status_API_response, true);
if (isset($data['Message'])) {
$message = $data['Message'];
} else {
$message = "No message found in the response.";
}
if (isset($data['Status'])) {
$status = $data['Status'];
} else {
$status = "None";
}
if (isset($data['Elapsed_Time'])) {
$elapsed_time = $data['Elapsed_Time'];
} else {
$elapsed_time = "-1";
}
}
if (isset($_GET['new_duration'])){
$button_recent = true;
set_duration($_GET['new_duration']);
}
if (isset($_GET['action'])) {
$button_recent = true;
$button_result = service_manager($_GET['action']);
}
function service_manager($submitted_status) {
switch ($submitted_status) {
case "stop":
$apiUrl = "http://172.17.0.1:5000/stop";
break;
case "start":
$apiUrl = "http://172.17.0.1:5000/start";
break;
default:
$apiUrl = "http://172.17.0.1:5000/status";
break;
}
// Use file_get_contents or cURL to fetch the API data
try {
$response = file_get_contents($apiUrl);
} catch (Exception $e) {
$response = '{ "Message": "unknowable", "Status": "unknowable" }';
}
if ($response === FALSE) {
return "error"; // API error
}
// Decode the JSON response (assuming the API returns JSON)
$data = json_decode($response, true);
// Assuming the API returns a single word result (like "success", "failure", etc.)
return isset($data['Status']) ? $data['Status'] : 'unknown';
}
switch ($status) {
case "active":
$button_text = "Stop Service";
$button_active = true;
break;
case "inactive":
$button_text = "Start Service";
$button_active = true;
break;
case "failed":
$button_text = "Start Service - Warning";
$button_active = true;
break;
case "deactivating":
$button_text = "Service Stopping...";
$button_active = false;
break;
case "unknowable":
$button_text = "Service Manager Down...";
$button_active = false;
break;
default:
$button_text = "Error, No Status";
$button_active = false;
$status="unknown";
break;
}
if($button_active && !$button_recent){
$action = "";
if($status == "active"){
$action = "stop";
}
else if($status == "inactive"){
$action = "start";
}
else if($status == "failed"){
$action = "start";
}
$button_action = ' onclick="location.href=\'/?action='.$action.'\'" ';
}
if($button_recent){
/*
$delay = 5; // Number of seconds before redirection
echo "You will be redirected in $delay seconds.";
header("Refresh: $delay; url=https://example.com");
exit();
*/
header("Location: http://".$http_host);
exit(); // Always include exit() after headers are sent
}
function duration_button_handler($button_ID, $function) {
// Duration Status API Checker
$duration_API_URL = "http://172.17.0.1:5000/check_duration";
// Use file_get_contents to fetch data from the API
try {
$duration_API_URL = file_get_contents($duration_API_URL);
} catch (Exception $e) {
$duration_API_URL = '{ "Message": "unknowable", "Status": "unknowable" }';
}
// Check if the request was successful
if ($duration_API_URL === FALSE) {
$raw_duration = "Failed to fetch data.";
}
else {
// Decode the JSON response (assuming the API returns JSON)
$raw_duration = json_decode($duration_API_URL, true);
if (isset($raw_duration['duration'])) {
$capture_duration = $raw_duration['duration'];
} else {
$capture_duration = "3";
}
}
// Service Status API Checker
$status_API_URL = "http://172.17.0.1:5000/status";
// Use file_get_contents to fetch data from the API
try {
$status_API_response = file_get_contents($status_API_URL);
} catch (Exception $e) {
$status_API_response = '{ "Message": "unknowable", "Status": "unknowable" }';
}
// Check if the request was successful
if ($status_API_response === FALSE) {
$data = "Failed to fetch data.";
}
else {
// Decode the JSON response (assuming the API returns JSON)
$data = json_decode($status_API_response, true);
if (isset($data['Message'])) {
$message = $data['Message'];
} else {
$message = "No message found in the response.";
}
if (isset($data['Status'])) {
$status = $data['Status'];
} else {
$status = "None";
}
}
# function 0 - return class string
# function 1 - return active string
switch ($function) {
case "0":
# these classes are weird because reasons
# specifically because the classes are named
# based on service status
# to decode this, inactive is green
# and deactivating is blue
# so all the selectable buttons are blue and the active one is green
if($button_ID == $capture_duration){
return "inactive";
}
else {
return "selected";
}
break;
case "1":
# this disables clicking the active button
if($status == "active") {
return " disabled";
}
else if($button_ID == $capture_duration){
return " disabled";
}
else {
return "";
}
break;
default:
break;
}
}
# this will be an integer from 0-3 for
# 30min, 1hr, 2hr, and 6rh respectively
# info = {
# "duration": new_duration
# }
function set_duration($duration) {
switch ($duration) {
case "0":
$apiUrl = "http://172.17.0.1:5000/store_duration?new_duration=0";
break;
case "1":
$apiUrl = "http://172.17.0.1:5000/store_duration?new_duration=1";
break;
case "2":
$apiUrl = "http://172.17.0.1:5000/store_duration?new_duration=2";
break;
case "3":
$apiUrl = "http://172.17.0.1:5000/store_duration?new_duration=3";
break;
default:
$apiUrl = "http://172.17.0.1:5000/status";
break;
}
try {
$response = file_get_contents($apiUrl);
} catch (Exception $e) {
$response = '{ "Message": "unknowable", "Status": "unknowable" }';
}
if ($response === FALSE) {
return "error"; // API error
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API Data Display</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="button-container">
<button <?php echo $button_action; ?> class="<?php echo $status; ?>" id="phpButton"<?php if(!$button_active || $button_recent) {echo " disabled";} ?>>
<?php echo $button_text; ?>
</button>
<p>
</div>
<p>
<div class="button-container">
<table>
<tr>
<td>
<button onclick="location.href='/?new_duration=3'" class="<?php echo duration_button_handler(3, 0); ?>" id="6_hour_button" <?php echo duration_button_handler(3, 1); ?>>6 Hours</button>
</td>
<td>
<button onclick="location.href='/?new_duration=2'" class="<?php echo duration_button_handler(2, 0); ?>" id="2_hour_button" <?php echo duration_button_handler(2, 1); ?>>2 Hours</button>
</td>
</tr>
<tr>
<td>
<button onclick="location.href='/?new_duration=1'" class="<?php echo duration_button_handler(1, 0); ?>" id="1_hour_button" <?php echo duration_button_handler(1, 1); ?>>1 Hour</button>
</td>
<td>
<button onclick="location.href='/?new_duration=0'" class="<?php echo duration_button_handler(0, 0); ?>" id="30_min_button" <?php echo duration_button_handler(0, 1); ?>>30 min</button>
</td>
</tr>
</table>
</div>
<p>
<div class="api-data">
<!-- PHP will inject the data here -->
<?php
echo "Full Message:<br>".htmlspecialchars($message)."<p>";
if ($status == "active"){
echo "Time Elapsed:<br>".htmlspecialchars($elapsed_time)."s<p>";
}
echo "Current Date:<br>".date("F j, Y, g:i:s a")."<p>";
?>
</div>
<p>
</div>
<script>
// When the button is clicked, send an AJAX request to fetch new data
document.getElementById('phpButton').addEventListener('click', function() {
// Disable button to avoid multiple clicks
this.disabled = true;
this.innerText = 'Loading...';
// Make an AJAX request
fetch('index.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'status': '<?php echo $status; ?>'
},
body: 'action=fetch_data' // Trigger the PHP function via POST
})
});
</script>
<script>
// Automatically refresh the page every second
setTimeout(function() {
location.reload();
}, <?php if($button_recent){ echo "5000"; } else{ echo "1000"; } ?>); // Refresh interval (1000 ms = 1 second)
</script>
</body>
</html>