raspap-webgui/includes/restapi.php

71 lines
2.0 KiB
PHP
Raw Normal View History

<?php
require_once 'includes/functions.php';
require_once 'config.php';
/**
2024-02-19 19:33:59 +01:00
* Handler for RestAPI settings
*/
function DisplayRestAPI()
{
2024-02-19 19:33:59 +01:00
// initialize status object
$status = new \RaspAP\Messages\StatusMessage;
// create instance of DotEnv
$dotenv = new \RaspAP\DotEnv\DotEnv;
$dotenv->load();
2024-02-19 19:33:59 +01:00
// set defaults
2024-02-27 11:50:32 +01:00
$apiKey = $_ENV['RASPAP_API_KEY'];
2024-02-19 19:33:59 +01:00
if (!RASPI_MONITOR_ENABLED) {
if (isset($_POST['SaveAPIsettings'])) {
if (isset($_POST['txtapikey'])) {
$apiKey = trim($_POST['txtapikey']);
if (strlen($apiKey) == 0) {
$status->addMessage('Please enter a valid API key', 'danger');
} else {
$return = saveAPISettings($status, $apiKey, $dotenv);
2024-02-19 19:33:59 +01:00
}
}
} elseif (isset($_POST['StartRestAPIservice'])) {
$status->addMessage('Attempting to start raspap-restapi.service', 'info');
exec('sudo /bin/systemctl start raspap-restapi', $return);
foreach ($return as $line) {
$status->addMessage($line, 'info');
}
} elseif (isset($_POST['StopRestAPIservice'])) {
$status->addMessage('Attempting to stop raspap-restapi.service', 'info');
exec('sudo /bin/systemctl stop raspap-restapi.service', $return);
foreach ($return as $line) {
$status->addMessage($line, 'info');
}
}
}
exec('pidof uvicorn | wc -l', $uvicorn);
$serviceStatus = $uvicorn[0] == 0 ? "down" : "up";
echo renderTemplate("restapi", compact(
"status",
"apiKey",
2024-02-19 19:33:59 +01:00
"serviceStatus",
"serviceLog"
));
}
2024-02-19 19:33:59 +01:00
/**
* Saves RestAPI settings
*
* @param object status
* @param object dotenv
2024-02-19 19:33:59 +01:00
* @param string $apiKey
*/
function saveAPISettings($status, $apiKey, $dotenv)
2024-02-19 19:33:59 +01:00
{
$status->addMessage('Saving API key', 'info');
$dotenv->set('RASPAP_API_KEY', $apiKey);
2024-02-19 19:33:59 +01:00
return $status;
}