-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_cache.php
More file actions
112 lines (96 loc) · 3.31 KB
/
Copy pathscript_cache.php
File metadata and controls
112 lines (96 loc) · 3.31 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
require_once __DIR__ . '/config.php';
checkMaintenance(true);
ensureStorageWritable();
const DB_PATH = __DIR__ . '/data/intake.sqlite';
function readInput(): array
{
$raw = file_get_contents('php://input');
if ($raw === false || trim($raw) === '') {
return [];
}
$input = json_decode($raw, true);
return is_array($input) ? $input : [];
}
try {
$pdo = pdoConnect(DB_PATH);
} catch (Throwable $e) {
errorResponse('Database connection failed', 500);
}
try {
$pdo->exec(<<<'SQL'
CREATE TABLE IF NOT EXISTS script_cache (
sku_normalized TEXT PRIMARY KEY,
sku_display TEXT NOT NULL,
prompt_text TEXT,
chatgpt_text TEXT,
final_text TEXT,
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
SQL);
$pdo->exec("CREATE INDEX IF NOT EXISTS idx_script_cache_updated_at ON script_cache (updated_at)");
} catch (Throwable $e) {
errorResponse('Schema initialization failed', 500);
}
try {
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
if ($method === 'GET') {
$sku = normalizeSku((string)($_GET['sku'] ?? ''));
if ($sku === '') {
successResponse(['status' => 'ok', 'has_cache' => false]);
}
$stmt = $pdo->prepare('SELECT sku_normalized, sku_display, prompt_text, chatgpt_text, final_text, updated_at FROM script_cache WHERE sku_normalized = :sku LIMIT 1');
$stmt->execute(['sku' => $sku]);
$row = $stmt->fetch();
if (!$row) {
successResponse(['status' => 'ok', 'has_cache' => false]);
}
successResponse([
'status' => 'ok',
'has_cache' => true,
'data' => $row,
]);
}
if ($method !== 'POST') {
errorResponse('Method not allowed', 405);
}
require_csrf();
$input = readInput();
$sku = normalizeSku((string)($input['sku'] ?? ''));
$skuDisplay = trim((string)($input['sku_display'] ?? $sku));
$promptText = (string)($input['prompt_text'] ?? '');
$chatgptText = (string)($input['chatgpt_text'] ?? '');
$finalText = (string)($input['final_text'] ?? '');
if ($sku === '') {
errorResponse('SKU is required', 400);
}
if ($skuDisplay === '') {
$skuDisplay = $sku;
}
$now = (new DateTimeImmutable('now'))->format('c');
$stmt = $pdo->prepare(<<<'SQL'
INSERT INTO script_cache (sku_normalized, sku_display, prompt_text, chatgpt_text, final_text, updated_at)
VALUES (:sku_normalized, :sku_display, :prompt_text, :chatgpt_text, :final_text, :updated_at)
ON CONFLICT(sku_normalized) DO UPDATE SET
sku_display = excluded.sku_display,
prompt_text = excluded.prompt_text,
chatgpt_text = excluded.chatgpt_text,
final_text = excluded.final_text,
updated_at = excluded.updated_at
SQL);
$stmt->execute([
'sku_normalized' => $sku,
'sku_display' => $skuDisplay,
'prompt_text' => $promptText !== '' ? $promptText : null,
'chatgpt_text' => $chatgptText !== '' ? $chatgptText : null,
'final_text' => $finalText !== '' ? $finalText : null,
'updated_at' => $now,
]);
successResponse([
'status' => 'ok',
'saved_at' => $now,
]);
} catch (Throwable $e) {
errorResponse('Internal server error', 500);
}