|
32 | 32 | exit(); |
33 | 33 | } |
34 | 34 |
|
35 | | -$validCommands = ['check', 'cron run', 'updates list all', 'updates run all', 'seed list']; |
| 35 | +$validCommands = ['check', 'cron run', 'updates list all', 'updates run all']; |
36 | 36 |
|
37 | | -// Allow seed run with a validated profile name. |
38 | | -if (preg_match('/^seed run ([a-z0-9_]+)$/', $json->command, $matches)) { |
39 | | - $validCommands[] = $json->command; |
| 37 | +// Sample-data tool: lives in tools/sampledata/ and talks to the Observer |
| 38 | +// API over HTTP. We pass Observer admin credentials through the environment |
| 39 | +// so they don't appear in the process list. |
| 40 | +if ($json->command === 'sampledata list') { |
| 41 | + $output = []; |
| 42 | + $resultCode = 0; |
| 43 | + $cmd = 'php ' . escapeshellarg(__DIR__ . '/../../tools/sampledata/seed.php') . ' list 2>&1'; |
| 44 | + exec($cmd, $output, $resultCode); |
| 45 | + |
| 46 | + echo json_encode([ |
| 47 | + 'message' => 'Listing sample data profiles.', |
| 48 | + 'result' => $converter->convert(implode(PHP_EOL, $output) ?: 'No output.'), |
| 49 | + 'theme' => $theme->asCss() |
| 50 | + ]); |
| 51 | + |
| 52 | + exit(); |
| 53 | +} |
| 54 | + |
| 55 | +if (preg_match('/^sampledata run ([a-z0-9_]+)$/', $json->command, $matches)) { |
| 56 | + $profile = $matches[1]; |
| 57 | + $obUser = $json->sampleDataUsername ?? null; |
| 58 | + $obPass = $json->sampleDataPassword ?? null; |
| 59 | + |
| 60 | + if (!$obUser || !$obPass) { |
| 61 | + http_response_code(400); |
| 62 | + echo json_encode(['message' => 'Observer admin credentials required to import sample data.']); |
| 63 | + exit(); |
| 64 | + } |
| 65 | + |
| 66 | + $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') |
| 67 | + . '://' . ($_SERVER['HTTP_HOST'] ?? '127.0.0.1'); |
| 68 | + |
| 69 | + $env = [ |
| 70 | + 'OB_BASE_URL' => $baseUrl, |
| 71 | + 'OB_USERNAME' => $obUser, |
| 72 | + 'OB_PASSWORD' => $obPass, |
| 73 | + // Preserve PATH so curl etc. resolve. |
| 74 | + 'PATH' => getenv('PATH') ?: '/usr/local/bin:/usr/bin:/bin', |
| 75 | + ]; |
| 76 | + |
| 77 | + $cmd = 'php ' . escapeshellarg(__DIR__ . '/../../tools/sampledata/seed.php') |
| 78 | + . ' run ' . escapeshellarg($profile) . ' 2>&1'; |
| 79 | + |
| 80 | + $descriptors = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; |
| 81 | + $process = proc_open($cmd, $descriptors, $pipes, null, $env); |
| 82 | + |
| 83 | + if (!is_resource($process)) { |
| 84 | + http_response_code(500); |
| 85 | + echo json_encode(['message' => 'Failed to start the sample-data tool.']); |
| 86 | + exit(); |
| 87 | + } |
| 88 | + |
| 89 | + $stdout = stream_get_contents($pipes[1]); |
| 90 | + fclose($pipes[1]); |
| 91 | + fclose($pipes[2]); |
| 92 | + $resultCode = proc_close($process); |
| 93 | + |
| 94 | + echo json_encode([ |
| 95 | + 'message' => $resultCode === 0 ? 'Sample data imported.' : 'Sample data import failed.', |
| 96 | + 'result' => $converter->convert($stdout ?: 'No output.'), |
| 97 | + 'theme' => $theme->asCss() |
| 98 | + ]); |
| 99 | + |
| 100 | + exit(); |
40 | 101 | } |
| 102 | + |
41 | 103 | if (in_array($json->command, $validCommands)) { |
42 | 104 | $output = []; |
43 | 105 | $resultCode = 0; |
|
0 commit comments