Repository navigation
Expand file tree
/
Copy pathserve.php
More file actions
395 lines (372 loc) · 17 KB
/
Copy pathserve.php
File metadata and controls
395 lines (372 loc) · 17 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/*
* Copyright 2026 M. Bryce Hudson
* SPDX-License-Identifier: Apache-2.0
*/
// serve.php — run Loop Studio with no web server: php -S 0.0.0.0:8080 serve.php
//
// Two jobs in one file, split by SAPI so they can never cross:
// * CLI ("php serve.php --check ..."): first-run helpers for the launchers.
// This branch ONLY runs under a real terminal (PHP_SAPI === 'cli'). Under
// `php -S` the SAPI is 'cli-server', so none of this is reachable from the
// web, which matters because one of the commands writes the access token.
// * cli-server: the request router. Replicates what .htaccess does under
// Apache, because there is no Apache here:
// - storage, logs, tmp, sessions, tests and dotfiles: refused (403),
// always, at any depth -- a nested copy of the app has its own storage/
// holding the same keys. storage/ holds the access token and API keys;
// Apache denies it via storage/.htaccess, and this is the equivalent
// here. tests/ is dev
// tooling the app never loads -- its .mjs sources are ordinary static
// text, so without this they were simply handed out. Do not relax this.
// - /api/* (except direct .php): the api/index.php front controller.
// - anywhere.php except /api/index.php and the two IDE entry points
// (ide/index.php, ide/api.php): refused. Under php -S a requested
// .php file would otherwise execute, including src/ classes.
// - real files: served with a content type plus the .htaccess cache rules
// (HTML never cached, JS/CSS revalidated).
// - everything else: index.html. The single-page fallback (NOT
// public/index.php, which is the legacy page).
//
// Deliberately PHP 7.0-compatible syntax throughout: the whole point is
// running on whatever PHP the machine already has (7.4+ required by the app
// itself, checked by --check).
// ---------------------------------------------------------------
if (PHP_SAPI === 'cli') {
$cmd = isset($argv[1]) ? $argv[1] : '--check';
if ($cmd === '--check') {
// PHP version first: nothing else matters if this fails.
if (PHP_VERSION_ID < 70400) {
fwrite(STDERR, 'PHP 7.4 or newer is required (found ' . PHP_VERSION . ').' . "\n");
fwrite(STDERR, 'Get it: https://www.php.net/downloads.php' . "\n");
exit(2);
}
$storage = __DIR__ . '/storage';
if (!is_dir($storage)) {
fwrite(STDERR, 'storage/ is missing or not writable.' . "\n");
exit(3);
}
// OneDrive/cloud-backed folders can carry a read-only reparse tag that
// makes is_writable() return false even when real writes succeed. Prove
// it with a write instead of trusting the attribute.
$probe = $storage . '/.write-check-' . getmypid() . '-' . bin2hex(random_bytes(4));
if (@file_put_contents($probe, 'ok') === false) {
fwrite(STDERR, 'storage/ is missing or not writable.' . "\n");
exit(3);
}
@unlink($probe);
$cfg = read_config();
$hasToken = is_array($cfg) && isset($cfg['sandbox']['token'])
&& trim((string)$cfg['sandbox']['token']) !== '';
echo 'PHP=' . PHP_VERSION . "\n";
echo 'TOKEN=' . ($hasToken ? 'yes' : 'no') . "\n";
if (!function_exists('curl_init')) {
// Run after --setup with its -c ini, so this means setup failed.
fwrite(STDERR, "PHP's curl extension is not enabled: the app could not reach any model.\n");
fwrite(STDERR, "Run: php serve.php --setup (it says what is missing)\n");
exit(4);
}
echo "CURL=yes\n";
echo "OK\n";
exit(0);
}
if ($cmd === '--ensure-token') {
$cfg = read_config();
if (!is_array($cfg)) { $cfg = array(); }
$existing = isset($cfg['sandbox']['token']) ? trim((string)$cfg['sandbox']['token']) : '';
if ($existing !== '') {
// Never print an existing token: it may already be pasted into a
// browser somewhere, and terminal output gets screenshotted.
echo "TOKEN=exists\n";
exit(0);
}
if (!isset($cfg['sandbox']) || !is_array($cfg['sandbox'])) { $cfg['sandbox'] = array(); }
try {
$token = bin2hex(random_bytes(32));
} catch (Exception $e) {
fwrite(STDERR, 'Could not generate a token.' . "\n");
exit(1);
}
$cfg['sandbox']['token'] = $token;
if (file_put_contents(__DIR__ . '/storage/config.json', json_encode($cfg, JSON_PRETTY_PRINT) . "\n") === false) {
fwrite(STDERR, 'Could not write storage/config.json.' . "\n");
exit(1);
}
echo 'TOKEN=' . $token . "\n";
exit(0);
}
if ($cmd === '--free-port') {
$want = isset($argv[2]) ? (int)$argv[2] : 8080;
if ($want < 1 || $want > 65535) { $want = 8080; }
for ($p = $want; $p < $want + 20; $p++) {
$fp = @fsockopen('127.0.0.1', $p, $errno, $errstr, 0.2);
if (!$fp) { echo $p . "\n"; exit(0); }
fclose($fp);
}
fwrite(STDERR, 'No free port near ' . $want . '.' . "\n");
exit(1);
}
if ($cmd === '--setup') {
exit(runtime_setup(in_array('--quiet', $argv, true)));
}
fwrite(STDERR, 'Usage: php serve.php [--check|--setup|--ensure-token|--free-port N]' . "\n");
exit(1);
}
/**
* Make THIS php able to run the app, without touching the system php.ini.
*
* The usual failure: a PHP with no php.ini (the winget package, a bare zip,
* Arch's default) has curl switched off, so every model call dies inside the
* server and the browser can only say "empty response". This works out what
* is missing and, when anything is, writes storage/php-runtime.ini: the
* machine's own php.ini (if it has one) plus the extension_dir, the missing
* extensions and a CA bundle. The launchers then run `php -c` with it and set
* PHPRC, so child php processes (sandbox code runs) get the same setup.
*
* stdout, last line: PHPINI=<path> (empty path = this PHP needs nothing)
* exit 0 ready · 5 curl cannot be enabled (not installed for this PHP)
*/
function runtime_setup($quiet) {
$say = function ($m) use ($quiet) { if (!$quiet) { fwrite(STDERR, $m . "\n"); } };
$required = array('curl');
$optional = array('openssl', 'mbstring', 'fileinfo', 'zip');
$win = DIRECTORY_SEPARATOR === '\\';
$iniPath = __DIR__ . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'php-runtime.ini';
$caFile = __DIR__ . DIRECTORY_SEPARATOR . 'tools' . DIRECTORY_SEPARATOR . 'cacert.pem';
// Where the extension binaries live. With no php.ini the compiled-in
// default (C:\php\ext on Windows) is usually wrong, so prefer the folder
// beside the php binary when it actually holds the modules.
$modFile = function ($dir, $ext) use ($win) {
return $dir . DIRECTORY_SEPARATOR . ($win ? 'php_' . $ext . '.dll' : $ext . '.so');
};
$extDir = (string)ini_get('extension_dir');
$besideBinary = dirname(PHP_BINARY) . DIRECTORY_SEPARATOR . 'ext';
$dirOverride = null;
if (!is_file($modFile($extDir, 'curl')) && is_dir($besideBinary)) {
$extDir = $besideBinary;
$dirOverride = $besideBinary;
}
$add = array();
$missingRequired = array();
foreach (array_merge($required, $optional) as $ext) {
if (extension_loaded($ext)) { continue; }
if (is_file($modFile($extDir, $ext))) {
$add[] = $ext;
} elseif (in_array($ext, $required, true)) {
$missingRequired[] = $ext;
} else {
$say(" note: optional PHP extension '$ext' is not installed (some sandbox tools degrade).");
}
}
// A CA bundle, so HTTPS certificate checks pass (search, fetch, MCP...).
$needCa = trim((string)ini_get('curl.cainfo')) === '' && trim((string)ini_get('openssl.cafile')) === '';
if ($needCa && !is_file($caFile)) {
$say(' note: tools/cacert.pem is missing; HTTPS certificate checks may fail.');
$needCa = false;
}
if ($missingRequired) {
$say('');
$say(' PHP extension(s) needed but not installed: ' . implode(', ', $missingRequired));
if ($win) {
$say(' This PHP has no ' . $modFile($extDir, $missingRequired[0]) . '.');
$say(' Reinstall PHP (winget install PHP.PHP.8.3) or use setup-xampp.bat.');
} else {
$say(' Install them, for example:');
$say(' Debian/Ubuntu: sudo apt install php-curl');
$say(' Fedora/RHEL: sudo dnf install php-common');
$say(' Arch: sudo pacman -S php (then re-run start.sh)');
$say(' macOS: brew install php');
}
echo "PHPINI=\n";
return 5;
}
if (!$add && !$needCa && !$dirOverride) {
@unlink($iniPath);
echo "PHPINI=\n";
return 0;
}
// Start from the machine's own php.ini so nothing it sets is lost (-c
// replaces the lookup entirely), then layer our additions on top.
$lines = array('; Generated by serve.php --setup. Safe to delete; it is rebuilt on start.');
$loaded = php_ini_loaded_file();
if ($loaded && is_readable($loaded)) {
$lines[] = '; --- copied from ' . $loaded;
$lines[] = (string)file_get_contents($loaded);
$lines[] = '; --- Loop Studio additions';
}
if ($dirOverride) { $lines[] = 'extension_dir = "' . $dirOverride . '"'; }
foreach ($add as $ext) { $lines[] = 'extension=' . $ext; }
if ($needCa) {
$lines[] = 'curl.cainfo = "' . $caFile . '"';
$lines[] = 'openssl.cafile = "' . $caFile . '"';
}
if (@file_put_contents($iniPath, implode("\n", $lines) . "\n") === false) {
$say(' Could not write ' . $iniPath . '.');
echo "PHPINI=\n";
return 5;
}
// Prove it: a fresh php with the new ini must have curl.
$probe = escapeshellarg(PHP_BINARY) . ' -c ' . escapeshellarg($iniPath)
. ' -r ' . escapeshellarg("exit(function_exists('curl_init') ? 0 : 1);");
$out = array();
$rc = 1;
@exec($probe . ($win ? ' 2>NUL' : ' 2>/dev/null'), $out, $rc);
if ($rc !== 0) {
$say(' Enabled curl in ' . $iniPath . ' but PHP still cannot load it.');
$say(' On Windows this usually means the Visual C++ runtime is missing:');
$say(' winget install Microsoft.VCRedist.2015+.x64');
echo "PHPINI=\n";
return 5;
}
if ($add) { $say(' Enabled PHP extensions: ' . implode(', ', $add)); }
if ($needCa) { $say(' Using bundled CA certificates (tools/cacert.pem).'); }
echo 'PHPINI=' . $iniPath . "\n";
return 0;
}
function read_config() {
$raw = @file_get_contents(__DIR__ . '/storage/config.json');
if (!is_string($raw)) { return null; }
$d = json_decode($raw, true);
return is_array($d) ? $d : null;
}
// ---------------------------------------------------------------
// Request router (php -S only; unreachable from the CLI branch above).
$ROOT = __DIR__;
$uri = parse_url(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/', PHP_URL_PATH);
if (!is_string($uri) || $uri === '') { $uri = '/'; }
// Apache mounts this tree at /router; php -S serves it at the origin root.
// Accept the Apache mount here too, so the URL the Windows launcher opens --
// and any bookmark made from it -- keeps working under either server. Only
// this documented prefix is stripped; a general leading segment would turn
// unrelated paths into app paths.
$mountedAtRouter = false;
if ($uri === '/router') {
$uri = '/';
$mountedAtRouter = true;
} elseif (strpos($uri, '/router/') === 0) {
$uri = substr($uri, strlen('/router'));
$mountedAtRouter = true;
}
// 1. Never serve these, ever. Compared case-insensitively: on Windows and
// macOS the disk is case-insensitive, so /STORAGE/state.json would have
// resolved to the real file and slipped through a case-sensitive check.
//
// Checked at ANY depth, not just at the root. A nested copy of the app -- an
// archive of an older version, a backup, a folder someone dropped in to
// compare against -- carries its own storage/ with the same config.json,
// tokens and sessions in it, and a root-only check happily served those.
//
// API routes are exempt: they are endpoints, not files. The UI calls
// /api/sessions for the session LIST, and refusing that because "sessions"
// looks like the folder broke loading the sidebar. No API route maps onto a
// path from disk -- handlers open storage/ themselves, server-side.
$lowerUri = strtolower($uri);
$isApiRoute = ($lowerUri === '/api' || strpos($lowerUri, '/api/') === 0);
if (!$isApiRoute) {
foreach (explode('/', $lowerUri) as $part) {
if ($part !== '' && in_array($part, array('storage', 'logs', 'tmp', 'sessions', 'tests'), true)) {
http_response_code(403);
header('Content-Type: text/plain');
echo "Forbidden\n";
return true;
}
}
}
foreach (explode('/', $uri) as $part) {
if ($part !== '' && $part[0] === '.') {
http_response_code(403);
header('Content-Type: text/plain');
echo "Forbidden\n";
return true;
}
}
// Exact $uri, not the lower-cased copy: /API/index.php is a case variant of the
// front controller, and serving it as a static file would print PHP source.
// The IDE (ide/) has its own two entry points, allowlisted here for the same
// reason api/index.php is: they are first-party PHP, everything else stays
// refused (see the ide/README for why the IDE needs real PHP). Both the root
// shape and the /router-mount shape are accepted, like the API below.
$idePhp = false;
$ideFile = '';
if (preg_match('#^(?:/router)?/ide/(index|api)\.php$#', $uri, $ideM)) {
$idePhp = true;
$ideFile = '/ide/' . $ideM[1] . '.php';
}
if (substr($lowerUri, -4) === '.php' && $uri !== '/api/index.php' && !$idePhp) {
http_response_code(403);
header('Content-Type: text/plain');
echo "Forbidden\n";
return true;
}
// 2. API front controller (mirrors api/.htaccess).
//
// api/index.php derives its endpoint from SCRIPT_NAME. Under the Apache mount
// that is /router/api/index.php and the original /router/api/... REQUEST_URI
// matches it; under php -S at the root it is /api/index.php. Describe the
// current mount explicitly so both shapes resolve the same endpoint instead
// of falling through to the API's "Endpoint not found".
if ($uri === '/api' || strpos($uri, '/api/') === 0) {
$_SERVER['SCRIPT_NAME'] = ($mountedAtRouter ? '/router' : '') . '/api/index.php';
require $ROOT . '/api/index.php';
return true;
}
// 2b. The IDE app's own entry points (ide/index.php + ide/api.php). They need
// PHP to run: index.php relaxes the X-Frame-Options header so the studio can
// host it as a same-origin screen, and api.php is the IDE's file backend.
// SCRIPT_NAME stays the browser-facing path so the IDE's relative fetches
// (api.php) resolve under either mount; everything else under ide/ is static
// and falls through to branch 3.
if ($idePhp) {
$_SERVER['SCRIPT_NAME'] = $uri;
require $ROOT . $ideFile;
return true;
}
// 3. Real files, with content type + the .htaccess cache policy.
$disk = $ROOT . $uri;
if (is_dir($disk)) { $disk = rtrim($disk, '/') . '/index.html'; }
// realpath containment: /.. above the root falls through to the app, never
// to the disk outside it.
$real = realpath($disk);
if ($real !== false && is_file($real) && strpos($real, $ROOT) === 0) {
$ext = strtolower(pathinfo($real, PATHINFO_EXTENSION));
$types = array(
'html' => 'text/html; charset=utf-8',
'js' => 'text/javascript; charset=utf-8',
'mjs' => 'text/javascript; charset=utf-8',
'css' => 'text/css; charset=utf-8',
'json' => 'application/json; charset=utf-8',
'map' => 'application/json; charset=utf-8',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'webp' => 'image/webp',
'svg' => 'image/svg+xml',
'ico' => 'image/x-icon',
'txt' => 'text/plain; charset=utf-8',
'md' => 'text/plain; charset=utf-8',
'woff' => 'font/woff',
'woff2'=> 'font/woff2',
'ttf' => 'font/ttf',
'otf' => 'font/otf',
'eot' => 'application/vnd.ms-fontobject',
'mp3' => 'audio/mpeg',
'wav' => 'audio/wav',
'ogg' => 'audio/ogg',
);
header('Content-Type: ' . (isset($types[$ext]) ? $types[$ext] : 'application/octet-stream'));
header('X-Content-Type-Options: nosniff');
if ($ext === 'html' || $ext === 'htm') {
header('Cache-Control: no-store, no-cache, must-revalidate');
} elseif ($ext === 'js' || $ext === 'css') {
header('Cache-Control: no-cache, must-revalidate');
}
readfile($real);
return true;
}
// 4. Single-page fallback: the app entry.
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('X-Content-Type-Options: nosniff');
readfile($ROOT . '/index.html');
return true;