wrapperr/api/get_config.php
aunefyren 023158a619 Optional API logging
All the different PHP APIs can now log successful activity to a log file in the config directory. The logging of Wrapped activity includes the ID of the user. Enabled/disabled in the admin menu.
2021-09-21 15:01:14 +02:00

58 lines
No EOL
1.4 KiB
PHP

<?php
$data = json_decode(file_get_contents("php://input"));
$path = "../config/config.json";
if(!file_exists($path)) {
fopen($path, "w");
}
$config = json_decode(file_get_contents($path));
if(empty($data)) {
echo json_encode(array("error" => true, "message" => "No input provided."));
exit(0);
}
if(empty($config->password)) {
echo json_encode(array("error" => false, "message" => "No password set.", "password" => false, "data" => array()));
exit(0);
}
$password = htmlspecialchars($data->password);
if(password_verify($password, $config->password)) {
// Log API request if enabled
if($config->use_logs) {
if(!log_activity()) {
echo json_encode(array("message" => "Failed to log event.", "error" => true));
exit(0);
}
}
echo json_encode(array("error" => false, "message" => "Login successful.", "password" => true, "data" => $config));
exit(0);
} else {
echo json_encode(array("error" => true, "message" => "Password not accepted.", "password" => true, "data" => array()));
exit(0);
}
function log_activity() {
$date = date('Y-m-d H:i:s');
$path = "../config/wrapped.log";
if(!file_exists($path)) {
$temp = fopen($path, "w");
fwrite($temp, 'Plex Wrapped');
fclose($temp);
}
$log_file = fopen($path, 'a');
fwrite($log_file, PHP_EOL . $date . ' - get_config.php');
if(fclose($log_file)) {
return True;
}
return False;
}
?>