-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.php
More file actions
53 lines (44 loc) · 1.56 KB
/
Copy pathlog.php
File metadata and controls
53 lines (44 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* log.php
* GET → returns all log entries as JSON
* POST → appends a new entry {task, session, time}
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit; }
define('LOG_FILE', __DIR__ . '/focus-sessions/task-log.json');
function loadLog() {
if (!file_exists(LOG_FILE)) return [];
$data = json_decode(file_get_contents(LOG_FILE), true);
return is_array($data) ? $data : [];
}
function saveLog($entries) {
$dir = dirname(LOG_FILE);
if (!is_dir($dir)) mkdir($dir, 0755, true);
file_put_contents(LOG_FILE, json_encode($entries, JSON_PRETTY_PRINT));
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo json_encode(['entries' => loadLog()]);
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$body = json_decode(file_get_contents('php://input'), true);
$task = trim($body['task'] ?? '');
$session = trim($body['session'] ?? '');
$time = trim($body['time'] ?? date('c'));
if (!$task) { http_response_code(400); echo json_encode(['error' => 'No task provided']); exit; }
$entries = loadLog();
$entries[] = [
'task' => $task,
'session' => $session,
'time' => $time,
];
saveLog($entries);
echo json_encode(['ok' => true, 'count' => count($entries)]);
exit;
}
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);