37686-vm/api/share.php
2026-01-22 04:06:30 +00:00

51 lines
1.3 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
$data = json_decode(file_get_contents('php://input'), true);
if (!$data || !isset($data['content'])) {
echo json_encode(['error' => 'Content is required']);
exit;
}
$content = $data['content'];
// Wrap content in full HTML if it's just a snippet
if (!preg_match('/<html/i', $content)) {
$content = "<!DOCTYPE html>
<html>
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>AI Generated App</title>
<link href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css\" rel=\"stylesheet\">
<style>
body { padding: 20px; font-family: sans-serif; }
</style>
</head>
<body>
$content
</body>
</html>";
}
try {
$token = bin2hex(random_bytes(16));
$stmt = db()->prepare("INSERT INTO shared_apps (token, content) VALUES (?, ?)");
$stmt->execute([$token, $content]);
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$shareUrl = "$protocol://$host/view.php?t=$token";
echo json_encode([
'success' => true,
'token' => $token,
'url' => $shareUrl
]);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
}