anime-backlog-list/php/set_currently_watching.php
2025-01-07 12:23:54 +05:00

61 lines
2.0 KiB
PHP

<?php
include_once 'db_connect.php';
include_once 'check_allowed_ip.php';
$clientIP = $_SERVER['REMOTE_ADDR'];
if (!isAllowedIP($clientIP, $allowedSubnets)) {
http_response_code(403);
header('Content-Type: application/json');
echo json_encode([
'status' => 'error',
'message' => 'Access denied: Your IP is not authorized to modify records.'
]);
exit;
}
$id = $_POST['id'];
// Fetch the current state of currently_watching
$stmt = $conn->prepare("SELECT name, year, currently_watching FROM anime_list WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$record = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$record) {
http_response_code(404);
echo json_encode([
'status' => 'error',
'message' => 'Record not found.'
]);
exit;
}
$anime_name = $record['name'];
$anime_year = $record['year'];
$currentState = (int)$record['currently_watching'];
// Toggle currently_watching
$newState = $currentState === 1 ? 0 : 1;
$stmt = $conn->prepare("UPDATE anime_list SET currently_watching = :newState WHERE id = :id");
$stmt->bindParam(':newState', $newState, PDO::PARAM_INT);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
// Log the action
// $action_time = new DateTime('now', new DateTimeZone('GMT+5'));
// $action_time_formatted = $action_time->format('Y-m-d H:i:s');
// $ip_address = $_SERVER['REMOTE_ADDR'];
// $action_type = $newState === 1 ? 'set_currently_watching' : 'unset_currently_watching';
// $log_stmt = $conn->prepare("INSERT INTO action_logs (action_time, ip_address, anime_name, action_type, year) VALUES (:action_time, :ip_address, :anime_name, :action_type, :anime_year)");
// $log_stmt->bindParam(':action_time', $action_time_formatted);
// $log_stmt->bindParam(':ip_address', $ip_address);
// $log_stmt->bindParam(':anime_name', $anime_name);
// $log_stmt->bindParam(':anime_year', $anime_year);
// $log_stmt->bindParam(':action_type', $action_type);
// $log_stmt->execute();
echo json_encode(['status' => 'success']);