photoprismupload/vote.php

31 lines
923 B
PHP
Raw Normal View History

2022-05-05 15:12:01 +00:00
<?php
$votes_file = 'votes.json';
if (isset($_GET['name'])) {
$name = $_GET['name'];
$contents = file_get_contents($votes_file);
if ($contents === false) {
$contents = '{}';
}
$contents = json_decode($contents, true);
$data = $contents[$name] ?? [];
header('Content-Type: application/json; charset=utf-8');
die(json_encode($data));
} elseif (isset($_POST['name']) && isset($_POST['uid']) && isset($_POST['vote'])) {
$name = $_POST['name'];
$contents = file_get_contents($votes_file);
if ($contents === false) {
$contents = '{}';
}
$contents = json_decode($contents, true);
$data = $contents[$name] ?? [];
$data[$_POST['uid']] = $_POST['vote'];
$contents[$name] = $data;
file_put_contents($votes_file, json_encode($contents));
header('HTTP/1.1 204 No Content');
die();
} else {
header('HTTP/1.1 400 Bad Request');
die();
}