Ebook Cover

' . $title . '

' . $description . '

' . $checkoutHtml . '
'; $templateDom = new DOMDocument(); // Wrap the template HTML in a single root element to ensure proper parsing @$templateDom->loadHTML('
' . $templateHtml . '
', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); // Remove the back button before publishing $backButton = $templateDom->getElementById('back-to-templates-btn'); if ($backButton) { $backButton->parentNode->removeChild($backButton); } $imgTag = $templateDom->getElementById('previewImage'); if ($imgTag) $imgTag->setAttribute('src', $imageUrl); $titleTag = $templateDom->getElementById('previewTitle'); if ($titleTag) $titleTag->nodeValue = $title; $descTag = $templateDom->getElementById('previewDescription'); if ($descTag) $descTag->nodeValue = $description; $ctaContainer = $templateDom->getElementById('previewCtaContainer'); if ($ctaContainer) { while ($ctaContainer->hasChildNodes()) { $ctaContainer->removeChild($ctaContainer->firstChild); } if (!empty(trim($checkoutHtml))) { $fragment = $templateDom->createDocumentFragment(); // Append HTML, suppressing errors for potentially malformed user input @$fragment->appendXML($checkoutHtml); $ctaContainer->appendChild($fragment); } elseif ($showCtaButton && $ctaUrl) { $link = $templateDom->createElement('a', $ctaText); $link->setAttribute('href', $ctaUrl); $link->setAttribute('class', 'btn btn-primary btn-lg'); $link->setAttribute('role', 'button'); $ctaContainer->appendChild($link); } else { // If no checkout HTML and button is disabled or no URL, remove the container if ($ctaContainer->parentNode) { $ctaContainer->parentNode->removeChild($ctaContainer); } } } // Extract the inner HTML of the temporary div $body = $templateDom->getElementsByTagName('div')->item(0); $finalPreviewHtml = ''; foreach ($body->childNodes as $child) { $finalPreviewHtml .= $templateDom->saveHTML($child); } $htmlContent = << $title
$finalPreviewHtml
HTML; // Save the page $publishedDir = 'published'; if (!is_dir($publishedDir)) { mkdir($publishedDir, 0755, true); } $pageId = uniqid(); $filename = $publishedDir . '/' . $pageId . '.html'; file_put_contents($filename, $htmlContent); $pageUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $filename; // Database interaction require_once 'db/config.php'; $management_token = bin2hex(random_bytes(16)); $expires_at = date('Y-m-d H:i:s', strtotime('+60 days')); try { $pdo = db(); $stmt = $pdo->prepare("INSERT INTO published_pages (filename, management_token, expires_at) VALUES (?, ?, ?)"); $stmt->execute([$pageId . '.html', $management_token, $expires_at]); } catch (PDOException $e) { // Log error, but don't show to user error_log("DB Error: " . $e->getMessage()); // Fallback to just showing the page URL without management link echo "
"; echo "

Your page has been published!

"; echo "

You can view it here:

"; echo "{$pageUrl}"; echo "
"; exit; } $managementUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/manage.php?token=' . $management_token; // Display the success message and URL echo "
"; echo "

Your page has been published!

"; echo "

You can view it here:

"; echo "

{$pageUrl}

"; echo "
"; echo "

Manage your page:

"; echo "

To prevent your page from being deleted after 60 days, use the following link to extend its lifetime:

"; echo "

{$managementUrl}

"; echo "
"; } else { header('Location: index.php'); exit; } ?>