-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_handler.php
More file actions
112 lines (98 loc) · 4.07 KB
/
session_handler.php
File metadata and controls
112 lines (98 loc) · 4.07 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
// Allow CORS for local development
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: application/json');
// Directory where session files are stored
$sessionDir = __DIR__ . '/sessions';
// Ensure the sessions directory exists
if (!is_dir($sessionDir)) {
if (!mkdir($sessionDir, 0777, true)) {
echo json_encode(['error' => 'Failed to create sessions directory']);
exit;
}
}
// Handle preflight OPTIONS request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
// Log the request for debugging
file_put_contents('php_debug.log', "Request received: " . print_r($_SERVER, true) . "\n", FILE_APPEND);
file_put_contents('php_debug.log', "Request body: " . file_get_contents('php://input') . "\n", FILE_APPEND);
// Read the request body
$input = file_get_contents('php://input');
if (empty($input)) {
file_put_contents('php_debug.log', "Error: No input provided\n", FILE_APPEND);
echo json_encode(['error' => 'No input provided']);
exit;
}
$data = json_decode($input, true);
if (json_last_error() !== JSON_ERROR_NONE) {
file_put_contents('php_debug.log', "Error: Invalid JSON input: " . json_last_error_msg() . "\n", FILE_APPEND);
echo json_encode(['error' => 'Invalid JSON input: ' . json_last_error_msg()]);
exit;
}
if (!isset($data['action'])) {
file_put_contents('php_debug.log', "Error: Invalid action\n", FILE_APPEND);
echo json_encode(['error' => 'Invalid action']);
exit;
}
$action = $data['action'];
$password = isset($data['password']) ? $data['password'] : null;
$sessionFile = $sessionDir . '/' . $password . '.json';
// Clean up old session files (older than 60 days)
$files = glob($sessionDir . '/*.json');
foreach ($files as $file) {
if (filemtime($file) < time() - 60 * 24 * 60 * 60) {
unlink($file);
}
}
switch ($action) {
case 'check':
if (!$password) {
file_put_contents('php_debug.log', "Error: Password required for check action\n", FILE_APPEND);
echo json_encode(['error' => 'Password required']);
exit;
}
$exists = file_exists($sessionFile);
file_put_contents('php_debug.log', "Check action - Password: $password, Exists: " . ($exists ? 'true' : 'false') . "\n", FILE_APPEND);
echo json_encode(['exists' => $exists]);
break;
case 'load':
if (!$password) {
file_put_contents('php_debug.log', "Error: Password required for load action\n", FILE_APPEND);
echo json_encode(['error' => 'Password required']);
exit;
}
if (!file_exists($sessionFile)) {
file_put_contents('php_debug.log', "Error: Session not found for password: $password\n", FILE_APPEND);
echo json_encode(['error' => 'Session not found']);
exit;
}
$content = file_get_contents($sessionFile);
file_put_contents('php_debug.log', "Load action - Password: $password, Content: $content\n", FILE_APPEND);
echo $content;
break;
case 'save':
if (!$password || !isset($data['data'])) {
file_put_contents('php_debug.log', "Error: Password and data required for save action\n", FILE_APPEND);
echo json_encode(['error' => 'Password and data required']);
exit;
}
$result = file_put_contents($sessionFile, json_encode($data['data']));
if ($result === false) {
file_put_contents('php_debug.log', "Error: Failed to save session for password: $password\n", FILE_APPEND);
echo json_encode(['error' => 'Failed to save session']);
exit;
}
file_put_contents('php_debug.log', "Save action - Password: $password, Data saved successfully\n", FILE_APPEND);
echo json_encode(['success' => true]);
break;
default:
file_put_contents('php_debug.log', "Error: Invalid action: $action\n", FILE_APPEND);
echo json_encode(['error' => 'Invalid action']);
break;
}
?>