Documents in Markdown: a document can be written in Markdown with Write and Preview or in the rich text editor and switched either way, pictures pasted or dropped into the Markdown are kept as files, New Document starts in the way last used, Import Markdown Files beside Upload makes a document from each .md file, a Markdown button beside PDF downloads a document as a .md file, and Markdown pasted into a document's rich text editor goes in as formatting; versions, restore, copy and the API carry the Markdown, and the API's document create and update no longer escape values twice; database 4.2.3 adds documents.document_markdown and document_versions.document_version_markdown, and league/commonmark 2.10 and league/html-to-markdown 5.2 are added to libs/vendor through composer; 26.12.3 changelog entries

This commit is contained in:
johnnyq
2026-11-29 00:48:15 -05:00
parent a84c3eca79
commit 8b4fa5a1a0
484 changed files with 43707 additions and 63 deletions

View File

@@ -13,7 +13,7 @@ defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
function documentForHandler($document_id) {
global $mysqli, $client_id;
$document_id = intval($document_id);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_id, document_name, document_description, document_content,
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_id, document_name, document_description, document_content, " . documentMarkdownSql() . ",
document_client_id, document_folder_id, document_client_visible, document_archived_at,
document_created_at, document_created_by, document_updated_at, document_updated_by
FROM documents WHERE document_id = $document_id"));
@@ -34,7 +34,8 @@ function documentKeepVersion($row) {
mysqli_query($mysqli, "INSERT INTO document_versions SET
document_version_name = '" . escapeSql($row['document_name']) . "',
document_version_description = '" . escapeSql($row['document_description']) . "',
document_version_content = '" . mysqli_real_escape_string($mysqli, (string) $row['document_content']) . "',
document_version_content = '" . mysqli_real_escape_string($mysqli, (string) $row['document_content']) . "'
" . documentMarkdownSet($row['document_markdown'] ?? null, 'document_version_markdown') . ",
document_version_created_by = $by,
document_version_created_at = '$at',
document_version_document_id = " . intval($row['document_id']));
@@ -75,14 +76,11 @@ if (isset($_POST['add_document'])) {
$document_id = mysqli_insert_id($mysqli);
// Pasted images become files under uploads/documents/<id>/, so it needs its id first
$processed_content = mysqli_real_escape_string($mysqli, saveBase64Images(
$_POST['content'] ?? '',
$_SERVER['DOCUMENT_ROOT'] . "/uploads/documents/",
"uploads/documents/",
$document_id
));
mysqli_query($mysqli, "UPDATE documents SET document_content = '$processed_content' WHERE document_id = $document_id");
// Pasted images become files under uploads/documents/<id>/, so it needs its id first - written in Markdown, the HTML is made from it
[$processed_html, $markdown] = documentPostedContent($document_id);
$processed_content = mysqli_real_escape_string($mysqli, $processed_html);
mysqli_query($mysqli, "UPDATE documents SET document_content = '$processed_content', document_content_raw = '" . escapeSql(documentContentRaw($_POST['name'] ?? '', $processed_html)) . "'
" . documentMarkdownSet($markdown) . " WHERE document_id = $document_id");
if ($contact_id) {
mysqli_query($mysqli, "INSERT IGNORE INTO contact_documents SET contact_id = $contact_id, document_id = $document_id");
@@ -184,12 +182,8 @@ if (isset($_POST['edit_document'])) {
// What it said before, kept as a version
$document_version_id = documentKeepVersion($row);
$processed_html = saveBase64Images(
$_POST['content'] ?? '',
$_SERVER['DOCUMENT_ROOT'] . "/uploads/documents/",
"uploads/documents/",
$document_id
);
// Written in the rich text editor or in Markdown - the HTML every page shows, and what was written
[$processed_html, $markdown] = documentPostedContent($document_id);
$content = mysqli_real_escape_string($mysqli, $processed_html);
$content_raw = escapeSql(documentContentRaw($_POST['name'], $processed_html));
@@ -203,6 +197,7 @@ if (isset($_POST['edit_document'])) {
document_content_raw = '$content_raw',
document_updated_by = $session_user_id
$visible_sql
" . documentMarkdownSet($markdown) . "
WHERE document_id = $document_id");
// Only a form that showed the tags changes them
@@ -459,7 +454,11 @@ if (isset($_GET['export_document'])) {
$pdf->SetTitle($row['document_name']);
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->writeHTML('<h1>' . escapeHtml($row['document_name']) . '</h1>' . $row['document_content'], true, false, true, false, '');
// A Markdown checklist's boxes as [x] and [ ] - the PDF's font has no box characters
$pdf_content = preg_replace_callback('#<span class="itflow-task( is-done)?">[^<]*</span>#', function ($m) {
return empty($m[1]) ? '[ ]' : '[x]';
}, $row['document_content']);
$pdf->writeHTML('<h1>' . escapeHtml($row['document_name']) . '</h1>' . $pdf_content, true, false, true, false, '');
// The PDF is the response - nothing may follow it
$pdf->Output(preg_replace('/[^A-Za-z0-9 ._-]/', '', $row['document_name']) . '.pdf', 'I');
@@ -467,6 +466,106 @@ if (isset($_GET['export_document'])) {
}
/*
* A document as a Markdown file (functions/markdown.php documentMarkdownFile()) - what a Markdown
* document says as it was written, a rich text one made Markdown - with its name on top.
*/
if (isset($_GET['export_document_markdown'])) {
validateCSRFToken();
// Reading, not changing - as for its PDF
enforceUserPermission('module_client');
$row = documentForHandler($_GET['export_document_markdown']);
$document_id = intval($row['document_id']);
logAudit("Document", "Export", "$session_name downloaded document " . escapeSql($row['document_name']) . " as Markdown", $client_id, $document_id);
$file_name = trim(preg_replace('/[^A-Za-z0-9 ._-]/', '', $row['document_name'])) ?: 'Document';
header('Content-Type: text/markdown; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $file_name . '.md"; filename*=UTF-8\'\'' . rawurlencode(trim($row['document_name']) . '.md'));
header('X-Content-Type-Options: nosniff');
echo documentMarkdownFile($row['document_name'], $row['document_content'], $row['document_markdown']);
exit;
}
/*
* Markdown files made into documents (agent/modals/document/document_import_markdown.php) - one
* each, written in Markdown, in the folder picked. Each is named by the title in its front
* matter or the heading it starts with, else its file name (markdownImportSplit()).
*/
if (isset($_POST['import_markdown_documents'])) {
validateCSRFToken();
enforceUserPermission('module_support', 2);
$client_id = intval($_POST['client_id'] ?? 0);
enforceClientAccess();
if (!documentMarkdownReady() || !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM clients WHERE client_id = $client_id"))) {
flashAlert('Markdown files can\'t be imported here yet.', 'error');
redirect();
}
$folder_id = folderOwnedId($_POST['folder'] ?? 0, $client_id);
$client_visible = !empty($config_client_portal_enable) && intval($_POST['client_visible'] ?? 0) ? 1 : 0;
$files = $_FILES['files'] ?? null;
$count = is_array($files['name'] ?? null) ? count($files['name']) : 0;
$imported = [];
$skipped = [];
for ($i = 0; $i < $count && count($imported) < 50; $i++) {
$original = basename((string) $files['name'][$i]);
if ($files['error'][$i] === UPLOAD_ERR_NO_FILE) {
continue;
}
if ($files['error'][$i] !== UPLOAD_ERR_OK || !is_uploaded_file($files['tmp_name'][$i])) {
$skipped[] = "$original (it didn't upload)";
continue;
}
if (!preg_match('/\.(md|markdown|mdown|mkd)$/i', $original)) {
$skipped[] = "$original (not a .md file)";
continue;
}
if ($files['size'][$i] > 5 * 1024 * 1024) {
$skipped[] = "$original (over 5 MB)";
continue;
}
$text = (string) file_get_contents($files['tmp_name'][$i]);
if (!mb_check_encoding($text, 'UTF-8')) {
$skipped[] = "$original (not UTF-8 text)";
continue;
}
[$name, $markdown] = markdownImportSplit($text, preg_replace('/\.(md|markdown|mdown|mkd)$/i', '', $original));
$name_sql = escapeSql($name);
mysqli_query($mysqli, "INSERT INTO documents SET document_name = '$name_sql', document_description = '', document_content = '', document_content_raw = '',
document_client_visible = $client_visible, document_folder_id = $folder_id, document_created_by = $session_user_id, document_client_id = $client_id");
$document_id = mysqli_insert_id($mysqli);
$markdown = markdownSaveImages($markdown, $document_id);
$html = markdownToHtml($markdown);
mysqli_query($mysqli, "UPDATE documents SET document_content = '" . mysqli_real_escape_string($mysqli, $html) . "',
document_content_raw = '" . escapeSql(documentContentRaw($name, $html)) . "'" . documentMarkdownSet($markdown) . " WHERE document_id = $document_id");
logAudit("Document", "Create", "$session_name imported document $name_sql from " . escapeSql($original), $client_id, $document_id);
$imported[] = [$document_id, $name];
}
if (!$imported) {
flashAlert('Nothing was imported' . ($skipped ? ': ' . escapeHtml(implode(', ', $skipped)) : ' - pick the .md files to import.'), 'error');
redirect();
}
$what = count($imported) === 1 ? 'Document <strong>' . escapeHtml($imported[0][1]) . '</strong> imported' : count($imported) . ' documents imported';
flashAlert($what . ($skipped ? ' - left out ' . escapeHtml(implode(', ', $skipped)) : ''), $skipped ? 'warning' : 'success');
// One goes straight to it; several, to the folder they are in
redirect(count($imported) === 1 ? "document.php?client_id=$client_id&document_id={$imported[0][0]}" : "files.php?client_id=$client_id&folder_id=$folder_id&type=document");
}
if (isset($_GET['archive_document'])) {
validateCSRFToken();
@@ -522,7 +621,7 @@ if (isset($_POST['restore_document_version'])) {
$document_version_id = intval($_POST['document_version_id']);
$version = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_version_document_id, document_version_name, document_version_description,
document_version_content, document_version_created_at
document_version_content, " . documentMarkdownSql('document_version_markdown') . ", document_version_created_at
FROM document_versions WHERE document_version_id = $document_version_id"));
if (!$version) {
flashAlert('That version no longer exists.', 'error');
@@ -543,6 +642,7 @@ if (isset($_POST['restore_document_version'])) {
document_content = '$content',
document_content_raw = '$content_raw',
document_updated_by = $session_user_id
" . documentMarkdownSet($version['document_version_markdown']) . "
WHERE document_id = $document_id");
$version_date = date('M j, Y g:i A', strtotime($version['document_version_created_at']));