Skip to content

Commit 4b3577e

Browse files
committed
More windows fixes and better ajax error responses
1 parent 9f2982c commit 4b3577e

4 files changed

Lines changed: 150 additions & 20 deletions

File tree

‎public/assets/sandbox-ui.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,18 @@ function bindAjax() {
3838
},
3939
body: new FormData(form),
4040
});
41-
const data = await response.json();
41+
const responseText = await response.text();
42+
let data;
43+
try {
44+
data = JSON.parse(responseText);
45+
} catch (error) {
46+
const detail = responseText
47+
.replace(/<[^>]*>/g, ' ')
48+
.replace(/\s+/g, ' ')
49+
.trim()
50+
.slice(0, 500);
51+
throw new Error(detail || 'QuickInstall returned an invalid response. Check the UI error log.');
52+
}
4253
dashboard.innerHTML = data.html;
4354
bindAjax();
4455
showActionResult(data, context);

‎src/QuickInstall/Sandbox/SourceProvider.php‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ protected function ensureFloating(string $version, array $selection): array
213213
{
214214
$this->project->deleteTree($actualPath);
215215
}
216-
if (!rename($tempSource['path'], $actualPath))
216+
error_clear_last();
217+
if (!@rename($tempSource['path'], $actualPath))
217218
{
218-
throw new RuntimeException("Unable to move source into place: $actualPath");
219+
throw new RuntimeException("Unable to move source into place: $actualPath" . $this->lastFilesystemError());
219220
}
220221
}
221222

@@ -326,7 +327,11 @@ public function fetch(array $source): void
326327

327328
if (is_dir($path))
328329
{
329-
rmdir($path);
330+
error_clear_last();
331+
if (!@rmdir($path) && is_dir($path))
332+
{
333+
throw new RuntimeException("Unable to remove empty source directory: $path" . $this->lastFilesystemError());
334+
}
330335
}
331336

332337
$command = $this->composerCommand([
@@ -363,9 +368,10 @@ protected function normalizeGitSourceRoot(string $path): void
363368
{
364369
$this->project->deleteTree($temporaryAppRoot);
365370
}
366-
if (!rename($appRoot, $temporaryAppRoot))
371+
error_clear_last();
372+
if (!@rename($appRoot, $temporaryAppRoot))
367373
{
368-
throw new RuntimeException("Unable to prepare Git source root: $appRoot");
374+
throw new RuntimeException("Unable to prepare Git source root: $appRoot" . $this->lastFilesystemError());
369375
}
370376

371377
foreach (scandir($path) ?: [] as $item)
@@ -391,9 +397,10 @@ protected function normalizeGitSourceRoot(string $path): void
391397
{
392398
throw new RuntimeException("Unable to normalize Git source. Target already exists: $target");
393399
}
394-
if (!rename($source, $target))
400+
error_clear_last();
401+
if (!@rename($source, $target))
395402
{
396-
throw new RuntimeException("Unable to move Git source file into place: $source");
403+
throw new RuntimeException("Unable to move Git source file into place: $source" . $this->lastFilesystemError());
397404
}
398405
}
399406

@@ -406,6 +413,13 @@ protected function hasFiles(string $path): bool
406413
return $files !== false && count(array_diff($files, ['.', '..'])) > 0;
407414
}
408415

416+
private function lastFilesystemError(): string
417+
{
418+
$error = error_get_last();
419+
$message = is_array($error) ? trim((string) ($error['message'] ?? '')) : '';
420+
return $message !== '' ? ": $message" : '';
421+
}
422+
409423
protected function installedPhpbbVersion(string $path): string
410424
{
411425
$version = $this->detectedPhpbbVersion($path);

‎src/QuickInstall/Sandbox/Web/Application.php‎

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,57 @@ public function run(): void
4545
{
4646
$this->assertTrustedPost();
4747
$this->assertCsrfToken($csrfToken);
48-
$this->handlePost();
4948
if ($this->isAjax())
5049
{
51-
$this->renderJson();
50+
$this->handleAjaxPost();
5251
return;
5352
}
53+
$this->handlePost();
5454
}
5555

5656
$this->notice = $this->notice ?: (string) ($_GET['notice'] ?? '');
5757
$this->error = $this->error ?: (string) ($_GET['error'] ?? '');
5858
$this->render();
5959
}
6060

61+
private function handleAjaxPost(): void
62+
{
63+
$previousDisplayErrors = ini_set('display_errors', '0');
64+
ob_start();
65+
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
66+
if (!(error_reporting() & $severity))
67+
{
68+
return false;
69+
}
70+
71+
throw new \ErrorException($message, 0, $severity, $file, $line);
72+
});
73+
74+
try
75+
{
76+
$this->handlePost();
77+
}
78+
catch (\Throwable $e)
79+
{
80+
$this->error = $this->friendlyError($e->getMessage());
81+
}
82+
finally
83+
{
84+
restore_error_handler();
85+
$unexpectedOutput = trim((string) ob_get_clean());
86+
if ($previousDisplayErrors !== false)
87+
{
88+
ini_set('display_errors', $previousDisplayErrors);
89+
}
90+
if ($unexpectedOutput !== '')
91+
{
92+
$this->output->error(trim(strip_tags($unexpectedOutput)) . "\n");
93+
}
94+
}
95+
96+
$this->renderJson();
97+
}
98+
6199
private function assertLocalRequest(): void
62100
{
63101
$remote = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
@@ -129,6 +167,7 @@ private function handlePost(): void
129167
{
130168
try
131169
{
170+
$this->disableExecutionTimeLimit();
132171
$action = (string) ($_POST['action'] ?? '');
133172
switch ($action)
134173
{
@@ -234,6 +273,24 @@ private function handlePost(): void
234273
}
235274
}
236275

276+
private function disableExecutionTimeLimit(): void
277+
{
278+
if ((int) ini_get('max_execution_time') === 0)
279+
{
280+
return;
281+
}
282+
283+
if (function_exists('set_time_limit') && @set_time_limit(0))
284+
{
285+
return;
286+
}
287+
@ini_set('max_execution_time', '0');
288+
if ((int) ini_get('max_execution_time') !== 0)
289+
{
290+
throw new RuntimeException('QuickInstall could not disable the PHP execution time limit for this action. Allow set_time_limit or set max_execution_time=0 for the sandbox UI.');
291+
}
292+
}
293+
237294
private function friendlyError(string $message): string
238295
{
239296
if ($this->isDockerConnectivityError($message))
@@ -345,20 +402,52 @@ private function render(): void
345402

346403
private function renderJson(): void
347404
{
405+
$previousDisplayErrors = ini_set('display_errors', '0');
406+
ob_start();
407+
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
408+
if (!(error_reporting() & $severity))
409+
{
410+
return false;
411+
}
412+
throw new \ErrorException($message, 0, $severity, $file, $line);
413+
});
414+
415+
try
416+
{
417+
$html = $this->renderTemplate('dashboard.php', $this->viewData());
418+
$json = json_encode([
419+
'ok' => $this->error === '',
420+
'notice' => $this->notice,
421+
'error' => $this->error,
422+
'output' => $this->output->all(),
423+
'html' => $html,
424+
], JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
425+
}
426+
catch (\Throwable $e)
427+
{
428+
$json = json_encode([
429+
'ok' => false,
430+
'notice' => '',
431+
'error' => $this->friendlyError($e->getMessage()),
432+
'output' => $this->output->all(),
433+
'html' => '',
434+
], JSON_UNESCAPED_SLASHES) ?: '{"ok":false,"error":"Unable to encode QuickInstall response."}';
435+
}
436+
finally
437+
{
438+
restore_error_handler();
439+
ob_end_clean();
440+
if ($previousDisplayErrors !== false)
441+
{
442+
ini_set('display_errors', $previousDisplayErrors);
443+
}
444+
}
445+
348446
if (!headers_sent())
349447
{
350448
header('Content-Type: application/json; charset=utf-8');
351449
}
352-
353-
$html = $this->renderTemplate('dashboard.php', $this->viewData());
354-
355-
echo json_encode([
356-
'ok' => $this->error === '',
357-
'notice' => $this->notice,
358-
'error' => $this->error,
359-
'output' => $this->output->all(),
360-
'html' => $html,
361-
], JSON_UNESCAPED_SLASHES);
450+
echo $json;
362451
}
363452

364453
private function viewData(?UpdateService $updates = null): array

‎tests/Integration/WebApplicationTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ public function testAjaxPostReturnsDashboardJson(): void
129129
self::assertStringContainsString('activity-log', $data['html']);
130130
}
131131

132+
public function testAjaxResponseRemainsJsonWhenDashboardRenderingFails(): void
133+
{
134+
$root = $this->createTempProjectRoot();
135+
$project = new Project($root);
136+
$project->init();
137+
$token = $this->csrfTokenFromRender($root);
138+
$project->writeJson('boards.json', ['broken' => 'not a board record']);
139+
140+
$json = $this->runWebApplication($root, ['action' => 'unknown', 'qi_csrf_token' => $token], true);
141+
$data = json_decode($json, true);
142+
143+
self::assertIsArray($data);
144+
self::assertFalse($data['ok']);
145+
self::assertStringNotContainsString('<br', $json);
146+
}
147+
132148
public function testAjaxSourceRemoveDeletesSource(): void
133149
{
134150
$root = $this->createTempProjectRoot();

0 commit comments

Comments
 (0)