winzik/tools/embed/tests/fake-playground.php
David Nurdin 87ad5126aa Embed kit and docs: a playground application on a partner's site without iframe nor CORS, and custom domains
tools/embed/jumphp-embed.php: one PHP file (7.4+, curl) that signs the requests to the
playground's /api/embed/<slug>/ (HMAC-SHA256 over method, path, query, time and body
hash), serves the application under the partner's base path (streamed, Range, gzip
passed through, immutable files kept on disk), relays the TCP tickets with the
visitor's address, and gives the blocks (disk cache, ETag revalidation, last copy when
the playground is down); no cookie either way, security headers. Its end-to-end test
(tools/embed/tests/test.sh, Docker), the WordPress plugin (shortcode
[jumphp_block app block], the base path answered at plugins_loaded, settings page,
secret possibly in wp-config.php), nginx / Caddy / plain PHP examples.

docs/embed.md (+ .en), docs/domains.md (+ .en), AGENTS.md section 14.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-14 02:41:11 +01:00

55 lines
2.6 KiB
PHP

<?php
// A fake playground for test.sh: checks the signatures like App\Service\EmbedSignature of jumphp-playground, answers like its /api/embed/.
$secret = 'jes_test-secret';
$uri = $_SERVER['REQUEST_URI'];
[$path, $query] = array_pad(explode('?', $uri, 2), 2, '');
$body = (string)file_get_contents('php://input');
$time = (string)($_SERVER['HTTP_X_JUMPHP_TIME'] ?? '');
$expected = hash_hmac('sha256', $_SERVER['REQUEST_METHOD'] . "\n$path\n$query\n$time\n" . hash('sha256', $body), $secret);
file_put_contents(sys_get_temp_dir() . '/fake-hits.log', $_SERVER['REQUEST_METHOD'] . " $uri\n", FILE_APPEND);
header('X-Robots-Tag: noindex');
header('Set-Cookie: playground=1');
if (($_SERVER['HTTP_X_JUMPHP_SITE'] ?? '') !== 'es_00000000000000000000' || abs(time() - (int)$time) > 300 || !hash_equals($expected, (string)($_SERVER['HTTP_X_JUMPHP_SIGNATURE'] ?? ''))) {
http_response_code(401);
header('Content-Type: application/json');
echo '{"error":"wrong signature"}';
return;
}
$big = str_repeat('0123456789abcdef', 16384); // 256 KB
switch (true) {
case $path === '/api/embed/demo/block/login':
header('ETag: "b1"');
header('Cache-Control: public, max-age=300');
if (($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') === '"b1"') {
http_response_code(304);
return;
}
header('Content-Type: text/html; charset=utf-8');
echo '<form action="https://partner.test/chat/">login</form>';
return;
case $path === '/api/embed/demo/app/':
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-cache');
echo '<!doctype html><title>page</title>' . $_SERVER['REQUEST_METHOD'] . ' query=' . $query . ' body=' . $body;
return;
case $path === '/api/embed/demo/app/engine/1.0.0/big.bin':
header('Content-Type: application/octet-stream');
header('Cache-Control: public, max-age=31536000, immutable');
header('ETag: "big"');
header('Accept-Ranges: bytes');
if (preg_match('/bytes=(\d+)-(\d+)/', $_SERVER['HTTP_RANGE'] ?? '', $m)) {
http_response_code(206);
header("Content-Range: bytes $m[1]-$m[2]/" . strlen($big));
echo substr($big, (int)$m[1], $m[2] - $m[1] + 1);
return;
}
header('Content-Length: ' . strlen($big));
echo $big;
return;
case $path === '/api/embed/demo/tcp/ticket':
header('Content-Type: application/json');
echo json_encode(['ip' => $_SERVER['HTTP_X_JUMPHP_CLIENT_IP'] ?? null, 'body' => json_decode($body, true)]);
return;
}
http_response_code(404);
echo 'not found';