diff --git a/agent/post/document.php b/agent/post/document.php index 30ce36b3..fe4819b7 100644 --- a/agent/post/document.php +++ b/agent/post/document.php @@ -16,10 +16,23 @@ if (isset($_POST['add_document'])) { $asset_id = intval($_POST['asset'] ?? 0); // Document add query - mysqli_query($mysqli,"INSERT INTO documents SET document_name = '$name', document_description = '$description', document_content = '$content', document_content_raw = '$content_raw', document_folder_id = $folder, document_created_by = $session_user_id, document_client_id = $client_id"); + mysqli_query($mysqli,"INSERT INTO documents SET document_name = '$name', document_description = '$description', document_content = '', document_content_raw = '$content_raw', document_folder_id = $folder, document_created_by = $session_user_id, document_client_id = $client_id"); $document_id = mysqli_insert_id($mysqli); + $processed_content = mysqli_escape_string( + $mysqli, + saveBase64Images( + $_POST['content'], + $_SERVER['DOCUMENT_ROOT'] . "/uploads/documents/", + "uploads/documents/", + $document_id + ) + ); + + // Document update content + mysqli_query($mysqli,"UPDATE documents SET document_content = '$processed_content' WHERE document_id = $document_id"); + if ($contact_id) { mysqli_query($mysqli,"INSERT INTO contact_documents SET contact_id = $contact_id, document_id = $document_id"); } diff --git a/agent/post/uploads/documents/23/img_61e0659c85d99df688f08ad1891769ef.png b/agent/post/uploads/documents/23/img_61e0659c85d99df688f08ad1891769ef.png new file mode 100644 index 00000000..334bee38 Binary files /dev/null and b/agent/post/uploads/documents/23/img_61e0659c85d99df688f08ad1891769ef.png differ diff --git a/functions.php b/functions.php index 0cae5ae8..636d90e2 100644 --- a/functions.php +++ b/functions.php @@ -1642,4 +1642,87 @@ function sanitize_filename($filename, $strict = false) { } return $filename; -} \ No newline at end of file +} + +function saveBase64Images(string $html, string $baseFsPath, string $baseWebPath, int $ownerId): string { + // Normalize paths + $baseFsPath = rtrim($baseFsPath, '/\\') . '/'; + $baseWebPath = rtrim($baseWebPath, '/\\') . '/'; + + $targetDir = $baseFsPath . $ownerId . "/"; + + $folderCreated = false; // <-- NEW FLAG + $savedAny = false; // <-- Track if ANY images processed + + libxml_use_internal_errors(true); + $dom = new DOMDocument(); + $dom->loadHTML('' . $html); + libxml_clear_errors(); + + $imgs = $dom->getElementsByTagName('img'); + + foreach ($imgs as $img) { + $src = $img->getAttribute('src'); + + // Match base64 images + if (preg_match('/^data:image\/([a-zA-Z0-9+]+);base64,(.*)$/s', $src, $matches)) { + + $savedAny = true; // <-- We are actually saving at least 1 image + + // Create folder ONLY when needed + if (!$folderCreated) { + if (!is_dir($targetDir)) { + mkdir($targetDir, 0775, true); + } + $folderCreated = true; + } + + $mimeType = strtolower($matches[1]); + $base64 = $matches[2]; + + $binary = base64_decode($base64); + if ($binary === false) { + continue; + } + + // Extension mapping + switch ($mimeType) { + case 'jpeg': + case 'jpg': $ext = 'jpg'; break; + case 'png': $ext = 'png'; break; + case 'gif': $ext = 'gif'; break; + case 'webp': $ext = 'webp'; break; + default: $ext = 'png'; + } + + // Secure random filename + $uid = bin2hex(random_bytes(16)); + $filename = "img_{$uid}.{$ext}"; + + $filePath = $targetDir . $filename; + + if (file_put_contents($filePath, $binary) !== false) { + $webPath = "/" . $baseWebPath . $ownerId . "/" . $filename; + $img->setAttribute('src', $webPath); + } + } + } + + // If no images were processed, return original HTML immediately + if (!$savedAny) { + return $html; + } + + // Extract body content only + $body = $dom->getElementsByTagName('body')->item(0); + + if ($body) { + $innerHTML = ''; + foreach ($body->childNodes as $child) { + $innerHTML .= $dom->saveHTML($child); + } + return $innerHTML; + } + + return $html; +} diff --git a/uploads/documents/25/img_aeaefaa4e3465d46a9042600bc1a95d7.png b/uploads/documents/25/img_aeaefaa4e3465d46a9042600bc1a95d7.png new file mode 100644 index 00000000..2c7af824 Binary files /dev/null and b/uploads/documents/25/img_aeaefaa4e3465d46a9042600bc1a95d7.png differ diff --git a/uploads/documents/index.php b/uploads/documents/index.php new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/uploads/documents/index.php @@ -0,0 +1 @@ +