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

@@ -59,6 +59,25 @@ if (isset($_POST['vault_unlock'])) {
exit;
}
/*
* Markdown for a document's editors (js/document_markdown.js, functions/markdown.php): Markdown as
* the HTML it shows as - its Preview, switching it to the rich text editor, and Markdown pasted
* into that editor - and the rich text editor's HTML as Markdown, switching the other way.
* Changes nothing; answers {html}, or {markdown, kept, dropped}.
*/
if (isset($_GET['markdown_to_html']) || isset($_GET['html_to_markdown'])) {
header('Content-Type: application/json');
$text = (string) ($_POST[isset($_GET['markdown_to_html']) ? 'markdown' : 'html'] ?? '');
if (strlen($text) > 16 * 1024 * 1024) {
exit(json_encode(['error' => 'That is too long to convert - over 16 MB.']));
}
if (isset($_GET['markdown_to_html'])) {
exit(json_encode(['html' => markdownToHtml($text)]));
}
$markdown = htmlToMarkdown($text, $markdown_notes);
exit(json_encode(['markdown' => $markdown, 'kept' => $markdown_notes['kept'], 'dropped' => $markdown_notes['dropped']]));
}
/*
* The host in a New / Edit Certificate form, checked when asked: connect,
* take the certificate it serves and say what it is - OK, for another name,

View File

@@ -22,6 +22,7 @@ $document_id = intval($_GET['document_id'] ?? 0);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_archived_at, document_client_visible, document_favorite, document_content, document_created_at,
document_description, document_folder_id, document_name, document_updated_at, document_reviewed_at, document_template_id, document_template_name,
document_template_review_months, document_template_archived_at, " . (documentReviewOwnReady() ? 'document_review_months' : 'NULL AS document_review_months') . ",
" . (documentMarkdownReady() ? 'document_markdown IS NOT NULL' : '0') . " AS document_is_markdown,
creator.user_name AS created_by_name, updater.user_name AS updated_by_name, reviewer.user_name AS reviewed_by_name
FROM documents
LEFT JOIN users creator ON document_created_by = creator.user_id
@@ -105,6 +106,7 @@ $page_title = $row['document_name'];
<?php if ($row['document_template_name'] !== null) { ?>
&middot; From template <?= $session_is_admin ? '<a class="text-reset" href="/admin/document_template.php?document_template_id=' . intval($row['document_template_id']) . '">' . escapeHtml($row['document_template_name']) . '</a>' : escapeHtml($row['document_template_name']) ?>
<?php } ?>
<?php if (intval($row['document_is_markdown'])) { ?>&middot; <span title="Edit shows the Markdown it was written in"><i class="fab fa-markdown me-1"></i>Written in Markdown</span><?php } ?>
<?php if ($versions) { ?>&middot; <a class="text-reset" href="#documentHistory"><?= count($versions) ?> earlier version<?= count($versions) === 1 ? '' : 's' ?></a><?php } ?>
</div>
</div>
@@ -125,6 +127,7 @@ $page_title = $row['document_name'];
</button>
<?php } ?>
<a class="btn btn-outline-secondary" href="post.php?export_document=<?= $document_id ?>&csrf_token=<?= $csrf ?>" title="Export as PDF"><i class="fas fa-file-pdf"></i><span class="d-none d-md-inline ms-2">PDF</span></a>
<a class="btn btn-outline-secondary" href="post.php?export_document_markdown=<?= $document_id ?>&csrf_token=<?= $csrf ?>" title="Download as a Markdown (.md) file"><i class="fab fa-markdown"></i><span class="d-none d-md-inline ms-2">Markdown</span></a>
<button type="button" class="btn btn-outline-secondary" onclick="window.print();" title="Print"><i class="fas fa-print"></i></button>
<?php if ($can_edit) { ?>
<div class="dropdown">

View File

@@ -16,7 +16,7 @@ enforceUserPermission('module_support', 2);
$document_id = intval($_GET['document_id'] ?? 0);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_client_visible, document_content, document_description, document_folder_id,
document_name, document_archived_at FROM documents WHERE document_client_id = $client_id AND document_id = $document_id LIMIT 1"));
document_name, document_archived_at, " . documentMarkdownSql() . " FROM documents WHERE document_client_id = $client_id AND document_id = $document_id LIMIT 1"));
if (!$row || !empty($row['document_archived_at'])) {
echo "<div class='text-center'><h1 class='text-secondary mt-5'>Nothing to edit here</h1><a class='btn btn-lg btn-secondary mt-3' href='files.php?client_id=$client_id'><i class='fas fa-fw fa-arrow-left me-2'></i>Back to files</a></div>";
@@ -28,6 +28,7 @@ $document_form_mode = 'edit';
$document_name = escapeHtml($row['document_name']);
$document_description = escapeHtml($row['document_description']);
$document_content = escapeHtml($row['document_content']);
$document_markdown = $row['document_markdown']; // raw - null for a document in the rich text editor
$document_folder_id = intval($row['document_folder_id']);
$document_client_visible = intval($row['document_client_visible']);

View File

@@ -357,6 +357,11 @@ $preview = []; // the grid's previewer payload
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/document/document_add_from_template.php?client_id=<?= $client_id ?>&folder_id=<?= $folder_id ?>">
<i class="fas fa-fw fa-puzzle-piece me-2"></i>Document from Template
</a>
<?php if (documentMarkdownReady()) { ?>
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/document/document_import_markdown.php?client_id=<?= $client_id ?>&folder_id=<?= $folder_id ?>">
<i class="fab fa-fw fa-markdown me-2"></i>Import Markdown Files
</a>
<?php } ?>
<div class="dropdown-divider"></div>
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/folder/folder_add.php?client_id=<?= $client_id ?>&current_folder_id=<?= $folder_id ?>">
<i class="fas fa-fw fa-folder-plus me-2"></i>New Folder

View File

@@ -27,6 +27,8 @@ $document_form_mode = 'add';
$document_name = $document_description = $document_content = '';
$document_folder_id = folderOwnedId($_GET['folder_id'] ?? 0, $client_id);
$document_client_visible = 0;
$document_markdown = null;
$document_format_remember = true; // starts in the way the agent wrote last (js/document_markdown.js)
// Drafted by AI when a model is set up for documentation and the role may use AI
$ai_draft = aiReady('Documentation');
@@ -122,6 +124,13 @@ ob_start();
if (description.value.trim() === '' && answer.description) {
description.value = answer.description;
}
// Written in Markdown - the draft goes in as Markdown
if (window.itflowDocumentFormat && itflowDocumentFormat.isMarkdown(form)) {
return itflowDocumentFormat.putHtml(form, answer.html).then(function () {
say('success', 'Drafted by AI - check it and fill in what is in [square brackets].');
said.setAttribute('data-ai-document-drafted', '');
});
}
var editor = window.tinymce ? tinymce.get().filter(function (ed) { return ed.getElement() === textarea; })[0] : null;
if (editor) {
editor.undoManager.add();

View File

@@ -11,7 +11,7 @@ enforceUserPermission('module_support', 2);
$document_id = intval($_GET['id'] ?? 0);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_client_id, document_client_visible, document_content, document_description,
document_folder_id, document_name, document_archived_at FROM documents WHERE document_id = $document_id LIMIT 1"));
document_folder_id, document_name, document_archived_at, " . documentMarkdownSql() . " FROM documents WHERE document_id = $document_id LIMIT 1"));
if (!$row) {
echo json_encode(['error' => 'That document no longer exists.']);
exit;
@@ -23,6 +23,7 @@ $document_form_mode = 'edit';
$document_name = escapeHtml($row['document_name']);
$document_description = escapeHtml($row['document_description']);
$document_content = escapeHtml($row['document_content']);
$document_markdown = $row['document_markdown']; // raw - null for a document in the rich text editor
$document_folder_id = intval($row['document_folder_id']);
$document_client_visible = intval($row['document_client_visible']);

View File

@@ -8,8 +8,19 @@
* Expects $document_form_mode ('add' or 'edit'), $client_id, and the
* escaped $document_name, $document_description, $document_content,
* $document_folder_id, $document_client_visible - and $document_id when
* editing, for its tags.
* editing, for its tags. $document_markdown is what a Markdown document says,
* raw - unset or null for one in the rich text editor - and with
* $document_format_remember (New Document) it starts in the way of writing the
* agent used last.
*
* It is written in the rich text editor or in Markdown, picked above the editor
* and switched either way as it is written (js/document_markdown.js); the form
* posts format, with content or markdown.
*/
$document_markdown_ready = documentMarkdownReady();
$document_is_markdown = $document_markdown_ready && isset($document_markdown) && $document_markdown !== null;
$document_format_uid = bin2hex(random_bytes(4));
?>
<div class="row g-3 mb-3">
@@ -57,7 +68,58 @@
<?php } ?>
</div>
<textarea class="form-control tinymce" name="content" aria-label="Content"><?= $document_content ?></textarea>
<div data-document-editors<?= !empty($document_format_remember) ? ' data-format-remember' : '' ?>>
<?php if ($document_markdown_ready) { ?>
<div class="d-flex flex-wrap align-items-center gap-2 mb-2">
<div class="btn-group btn-group-sm" role="group" aria-label="Write it in">
<input type="radio" class="btn-check" name="format" value="html" id="documentFormatRich<?= $document_format_uid ?>" autocomplete="off" <?= $document_is_markdown ? '' : 'checked' ?>>
<label class="btn btn-outline-secondary" for="documentFormatRich<?= $document_format_uid ?>"><i class="fas fa-fw fa-font me-1"></i>Rich text</label>
<input type="radio" class="btn-check" name="format" value="markdown" id="documentFormatMarkdown<?= $document_format_uid ?>" autocomplete="off" <?= $document_is_markdown ? 'checked' : '' ?>>
<label class="btn btn-outline-secondary" for="documentFormatMarkdown<?= $document_format_uid ?>"><i class="fab fa-fw fa-markdown me-1"></i>Markdown</label>
</div>
<span class="small" data-format-note></span>
</div>
<?php } ?>
<div data-editor-rich<?= $document_is_markdown ? ' hidden' : '' ?>>
<textarea class="form-control tinymce" name="content" aria-label="Content"><?= $document_content ?></textarea>
</div>
<?php if ($document_markdown_ready) { ?>
<div data-editor-markdown<?= $document_is_markdown ? '' : ' hidden' ?>>
<div class="d-flex flex-wrap align-items-end border-bottom">
<ul class="nav nav-tabs border-bottom-0 itflow-markdown-tabs" role="tablist">
<li class="nav-item"><button type="button" class="nav-link active" data-markdown-tab="write"><i class="fas fa-fw fa-pen me-1"></i>Write</button></li>
<li class="nav-item"><button type="button" class="nav-link" data-markdown-tab="preview"><i class="fas fa-fw fa-eye me-1"></i>Preview</button></li>
</ul>
<button type="button" class="btn btn-link btn-sm ms-auto mb-1 text-decoration-none" data-bs-toggle="collapse" data-bs-target="#documentMarkdownHelp<?= $document_format_uid ?>" aria-expanded="false"><i class="far fa-fw fa-question-circle me-1"></i>Markdown help</button>
</div>
<div class="collapse" id="documentMarkdownHelp<?= $document_format_uid ?>">
<div class="table-responsive border border-top-0 small">
<table class="table table-sm mb-0 itflow-markdown-help">
<tbody>
<tr><td><code># Heading</code> <code>## Smaller</code></td><td>Headings</td><td><code>**bold**</code> <code>_italic_</code> <code>~~struck~~</code></td><td>Text</td></tr>
<tr><td><code>- item</code> <code>1. item</code></td><td>Lists - indent for a list inside one</td><td><code>- [ ] to do</code> <code>- [x] done</code></td><td>Checklist</td></tr>
<tr><td><code>[words](https://...)</code></td><td>Link</td><td><code>`code`</code>, or <code>```</code> on the lines above and below</td><td>Code</td></tr>
<tr><td><code>| A | B |</code> then <code>|---|---|</code></td><td>Table</td><td><code>&gt; quote</code> <code>---</code></td><td>Quote, line</td></tr>
</tbody>
</table>
</div>
</div>
<textarea class="form-control font-monospace itflow-markdown-input" name="markdown" rows="18" aria-label="Content, in Markdown" data-markdown-input placeholder="# A heading&#10;&#10;Write here in Markdown - paste or drop a picture to add it."><?= isset($document_markdown) && $document_markdown !== null ? escapeHtml($document_markdown) : '' ?></textarea>
<div class="itflow-markdown-preview itflow-document-body border border-top-0 rounded-bottom" data-markdown-preview hidden></div>
</div>
<?php } ?>
</div>
<?php if ($document_markdown_ready) { ?>
<script>
// js/document_markdown.js - on a page of its own this runs before that file has loaded, and it takes the form up then
(window.itflowDocumentForms = window.itflowDocumentForms || []).push(document.currentScript.previousElementSibling);
if (window.itflowDocumentFormat) {
window.itflowDocumentFormat.start();
}
</script>
<?php } ?>
<?php if ($document_form_mode === 'edit') { ?>
<div class="form-text"><i class="fas fa-history me-1"></i>Saving keeps what it says now in the document's history, so it can be put back.</div>

View File

@@ -0,0 +1,71 @@
<?php
/*
* Import Markdown files - each .md file picked becomes a document written in Markdown, in the
* folder picked (import_markdown_documents, agent/post/document.php).
*/
require_once '../../../includes/modal_header.php';
enforceUserPermission('module_support', 2);
$client_id = intval($_GET['client_id'] ?? 0);
enforceClientAccess();
$folder_id = folderOwnedId($_GET['folder_id'] ?? 0, $client_id);
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fab fa-fw fa-markdown me-2"></i>Import Markdown Files</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="post.php" method="post" enctype="multipart/form-data" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="hidden" name="client_id" value="<?= $client_id ?>">
<div class="modal-body">
<?php if (!documentMarkdownReady()) { ?>
<div class="alert alert-warning mb-0">Markdown documents arrive with the next database update - run it from Maintenance &gt; Update first.</div>
<?php } else { ?>
<div class="mb-3">
<label class="form-label">Markdown files <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-file-import"></i></span>
<input type="file" class="form-control" name="files[]" multiple required accept=".md,.markdown,.mdown,.mkd,text/markdown">
</div>
<div class="form-text">Each becomes a document written in Markdown, named by the heading it starts with or its file name - up to 50 at a time. Pictures a file points to on your computer aren't brought in.</div>
</div>
<div class="mb-3">
<label class="form-label">Folder</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-folder"></i></span>
<select class="form-select select2" name="folder">
<?= folderOptionsHtml($client_id, $folder_id) ?>
</select>
</div>
</div>
<?php if (!empty($config_client_portal_enable)) { ?>
<div class="form-check form-switch">
<input type="hidden" name="client_visible" value="0">
<input class="form-check-input" type="checkbox" role="switch" name="client_visible" value="1" id="importMarkdownVisible">
<label class="form-check-label" for="importMarkdownVisible">
<i class="fas fa-fw fa-eye text-body-secondary me-1"></i>Show them in the client portal <span class="text-body-secondary small">- to their technical contacts</span>
</label>
</div>
<?php } ?>
<?php } ?>
</div>
<div class="modal-footer">
<?php if (documentMarkdownReady()) { ?>
<button type="submit" name="import_markdown_documents" class="btn btn-primary fw-bold"><i class="fas fa-file-import me-2"></i>Import</button>
<?php } ?>
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
</div>
</form>
<?php
require_once '../../../includes/modal_footer.php';

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']));