-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.php
More file actions
286 lines (254 loc) · 9 KB
/
Copy pathsync.php
File metadata and controls
286 lines (254 loc) · 9 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* DevPair Sync Backend
* Upload alongside index.html on MilesWeb hosting.
* Flat JSON storage — no database required.
*/
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('DATA_DIR', __DIR__ . '/devpair_data/');
define('PROJECTS_DIR', DATA_DIR . 'projects/');
define('TEAM_FILE', DATA_DIR . 'team.json');
foreach ([DATA_DIR, PROJECTS_DIR] as $dir) {
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
}
function read_json($path, $default = null) {
if (!file_exists($path)) return $default;
$data = json_decode(file_get_contents($path), true);
return is_array($data) ? $data : $default;
}
function write_json($path, $data) {
file_put_contents($path, json_encode($data, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
function slug_id($s) {
$s = strtolower(trim($s));
$s = preg_replace('/[^a-z0-9]+/', '-', $s);
$s = trim($s, '-');
return $s ?: 'project-' . time();
}
function team_defaults() {
return [
'groqKey' => '',
'projects' => [],
'updatedAt' => 0,
];
}
function load_team() {
return array_merge(team_defaults(), read_json(TEAM_FILE, team_defaults()));
}
function save_team($team) {
$team['updatedAt'] = time() * 1000;
write_json(TEAM_FILE, $team);
}
function project_path($id) {
$id = preg_replace('/[^a-z0-9\-_]/', '', $id);
return PROJECTS_DIR . $id . '.json';
}
function sanitize_role($role) {
return in_array($role, ['dev1', 'dev2'], true) ? $role : 'dev1';
}
function sanitize_project($body) {
return [
'id' => preg_replace('/[^a-z0-9\-_]/', '', $body['id'] ?? ''),
'name' => substr($body['name'] ?? 'Untitled', 0, 100),
'tasks' => is_array($body['tasks'] ?? null) ? $body['tasks'] : [],
'messages' => array_slice(is_array($body['messages'] ?? null) ? $body['messages'] : [], -80),
'activity' => array_slice(is_array($body['activity'] ?? null) ? $body['activity'] : [], -50),
'version' => intval($body['version'] ?? 0),
'updatedAt' => time() * 1000,
'updatedBy' => sanitize_role($body['updatedBy'] ?? 'dev1'),
];
}
$action = $_GET['action'] ?? 'team';
$body = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$body = json_decode(file_get_contents('php://input'), true);
if (!$body) {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON']);
exit();
}
$action = $body['action'] ?? $action;
}
switch ($action) {
/* ── Team config (shared Groq key + project list) ── */
case 'team':
$team = load_team();
echo json_encode([
'groqKey' => $team['groqKey'] ?? '',
'hasGroqKey' => !empty($team['groqKey']),
'projects' => $team['projects'] ?? [],
'updatedAt' => $team['updatedAt'] ?? 0,
]);
break;
case 'save_team':
$team = load_team();
if (isset($body['groqKey'])) {
$team['groqKey'] = substr($body['groqKey'], 0, 200);
}
save_team($team);
echo json_encode(['ok' => true, 'hasGroqKey' => !empty($team['groqKey'])]);
break;
/* ── Projects ── */
case 'list_projects':
$team = load_team();
echo json_encode(['projects' => $team['projects'] ?? []]);
break;
case 'get_project':
$id = preg_replace('/[^a-z0-9\-_]/', '', $_GET['id'] ?? ($body['id'] ?? ''));
$file = project_path($id);
if (!file_exists($file)) {
http_response_code(404);
echo json_encode(['error' => 'Project not found']);
break;
}
$data = read_json($file);
echo json_encode($data);
break;
case 'create_project':
$name = substr(trim($body['name'] ?? 'New Project'), 0, 100) ?: 'New Project';
$id = slug_id($body['id'] ?? $name);
$base = $id;
$n = 1;
while (file_exists(project_path($id))) {
$id = $base . '-' . $n++;
}
$project = [
'id' => $id,
'name' => $name,
'tasks' => $body['tasks'] ?? [],
'messages' => $body['messages'] ?? [],
'activity' => $body['activity'] ?? [],
'version' => 1,
'updatedAt' => time() * 1000,
'updatedBy' => sanitize_role($body['updatedBy'] ?? 'dev1'),
];
write_json(project_path($id), $project);
$team = load_team();
$team['projects'][] = [
'id' => $id,
'name' => $name,
'updatedAt' => $project['updatedAt'],
];
save_team($team);
echo json_encode(['ok' => true, 'project' => $project]);
break;
case 'save_project':
$incoming = sanitize_project($body);
if (!$incoming['id']) {
http_response_code(400);
echo json_encode(['error' => 'Missing project id']);
break;
}
$file = project_path($incoming['id']);
$existing = read_json($file);
if ($existing && isset($existing['version']) && isset($body['version'])) {
$clientVersion = intval($body['version']);
if ($clientVersion < $existing['version']) {
echo json_encode([
'ok' => false,
'conflict' => true,
'project' => $existing,
]);
break;
}
}
$incoming['version'] = ($existing['version'] ?? 0) + 1;
write_json($file, $incoming);
$team = load_team();
$found = false;
foreach ($team['projects'] as &$p) {
if ($p['id'] === $incoming['id']) {
$p['name'] = $incoming['name'];
$p['updatedAt'] = $incoming['updatedAt'];
$found = true;
break;
}
}
unset($p);
if (!$found) {
$team['projects'][] = [
'id' => $incoming['id'],
'name' => $incoming['name'],
'updatedAt' => $incoming['updatedAt'],
];
}
save_team($team);
echo json_encode(['ok' => true, 'project' => $incoming]);
break;
case 'delete_project':
$id = preg_replace('/[^a-z0-9\-_]/', '', $body['id'] ?? '');
$file = project_path($id);
if (file_exists($file)) {
unlink($file);
}
$team = load_team();
$team['projects'] = array_values(array_filter($team['projects'], function ($p) use ($id) {
return $p['id'] !== $id;
}));
save_team($team);
echo json_encode(['ok' => true]);
break;
/* ── Presence (pash / void online status) ── */
case 'presence_push':
$role = sanitize_role($body['role'] ?? 'dev1');
$name = substr($body['name'] ?? '', 0, 50);
$projectId = preg_replace('/[^a-z0-9\-_]/', '', $body['projectId'] ?? '');
$data = [
'role' => $role,
'name' => $name,
'projectId' => $projectId,
'tab' => substr($body['tab'] ?? 'board', 0, 20),
'ts' => time() * 1000,
];
write_json(DATA_DIR . $role . '.json', $data);
echo json_encode(['ok' => true, 'ts' => $data['ts']]);
break;
case 'presence_pull':
$role = sanitize_role($_GET['role'] ?? 'dev2');
$file = DATA_DIR . $role . '.json';
if (file_exists($file)) {
$data = read_json($file);
$data['online'] = isset($data['ts']) && (time() * 1000 - $data['ts']) < 120000;
echo json_encode($data);
} else {
echo json_encode(['online' => false, 'name' => $role === 'dev1' ? 'pash' : 'void']);
}
break;
/* ── Legacy endpoints (backward compat) ── */
case 'push':
$role = sanitize_role($body['role'] ?? 'dev1');
write_json(DATA_DIR . $role . '.json', [
'role' => $role,
'name' => substr($body['name'] ?? '', 0, 50),
'project' => substr($body['project'] ?? '', 0, 100),
'tasks' => $body['tasks'] ?? [],
'messages' => array_slice($body['messages'] ?? [], -30),
'activity' => array_slice($body['activity'] ?? [], -20),
'ts' => time() * 1000,
]);
echo json_encode(['ok' => true]);
break;
case 'pull':
$role = sanitize_role($_GET['role'] ?? 'dev2');
$file = DATA_DIR . $role . '.json';
if (file_exists($file)) {
$data = read_json($file);
$data['online'] = isset($data['ts']) && (time() * 1000 - $data['ts']) < 120000;
echo json_encode($data);
} else {
echo json_encode(['online' => false, 'tasks' => [], 'messages' => []]);
}
break;
default:
http_response_code(400);
echo json_encode(['error' => 'Unknown action: ' . $action]);
}