Repository navigation
Expand file tree
/
Copy pathreset.php
More file actions
51 lines (45 loc) · 1.48 KB
/
Copy pathreset.php
File metadata and controls
51 lines (45 loc) · 1.48 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
<?php
// Reset chat session - deletes the session on OpenCode server and clears persisted ID
header('Content-Type: application/json');
// CORS: default allow-all for compatibility; set OPENCODE_CORS_ORIGIN to tighten.
$corsOrigin = getenv('OPENCODE_CORS_ORIGIN');
if (!$corsOrigin) {
$corsOrigin = '*';
}
header('Access-Control-Allow-Origin: ' . $corsOrigin);
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
$opencode = getenv('OPENCODE_URL');
if (!$opencode) {
$opencode = 'http://127.0.0.1:4096';
}
$opencode = rtrim($opencode, '/');
$sessionFile = __DIR__ . '/.opencode_session';
$deleted = false;
if (file_exists($sessionFile)) {
$sessionID = trim(file_get_contents($sessionFile));
if ($sessionID !== '') {
// Delete session on OpenCode server
$cmd = 'curl -s -X DELETE ' . escapeshellarg($opencode . '/session/' . $sessionID);
$descriptors = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
$proc = proc_open($cmd, $descriptors, $pipes);
if (is_resource($proc)) {
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($proc);
}
$deleted = true;
}
unlink($sessionFile);
}
echo json_encode(array('ok' => true, 'deleted' => $deleted));
?>