From e3e2a0143519e2949436e42bf06124440cb8e2df Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 6 Aug 2020 15:28:24 +0100
Subject: [PATCH] Improve PDF preview when embedded in pages - fixes #202

---
 Changelog.md                 |  2 ++
 modules/feature-upload.php   |  3 +-
 modules/parser-parsedown.php | 56 +++++++++++++++++++++++++++++-------
 3 files changed, 48 insertions(+), 13 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 81b5c08..14ddceb 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -26,6 +26,7 @@ Make sure you have PHP 7.3+ when you update past this point! It isn't the end of
      - `comment_enabled` controls whether _anyone_ is allowed to comment at all or not
      - `comment_hide_all` determines whether the commenting system displays anything at all (if disabled, it's (almost) like the `feature-comments` doesn't exist - consider using the downloader to exclude the commenting system instead of enabling this setting)
      - `avatars_gravatar_enabled` determines whether redirects to [gravatar.com](https://gravatar.com/) should be performed if a user hasn't yet uploaded an avatar (if disabled then a blank image is returned instead of a redirect).
+ - PDF previews now show the browser's UI when embedded in pages with the `![alt text](File/somefile.png)` syntax
 
 ### Changed
  - **New policy:** Only [officially supported](https://www.php.net/supported-versions.php) versions of PHP are officially supported by Pepperminty Wiki.
@@ -53,6 +54,7 @@ Make sure you have PHP 7.3+ when you update past this point! It isn't the end of
  - Fixed an obscure bug in the search engine when excluding terms that appear both in a page's title and body
  - Squashed a warning at the top of search results (more insight is needed though to squash the inconsistencies in the search index that creep in though)
  - Removed annoying scrollbars when editing long pages
+ - Fixed an obscure warning when previewing PDFs
 
 
 ## v0.21.1-hotfix1
diff --git a/modules/feature-upload.php b/modules/feature-upload.php
index 35f760b..da3c874 100644
--- a/modules/feature-upload.php
+++ b/modules/feature-upload.php
@@ -387,8 +387,7 @@ register_module([
 					break;
 				
 				case "application":
-					if($mime_type == "application/pdf")
-					{
+					if($mime_type == "application/pdf") {
 						$preview = new imagick();
 						$preview->readImage("{$filepath}[0]");
 						$preview->setResolution(300,300);
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index 5b4e12f..66d32e5 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -647,8 +647,12 @@ class PeppermintParsedown extends ParsedownExtra
 						$previewSize = 300;
 						$previewUrl = "?action=preview&size=$previewSize&page=" . rawurlencode($pagename);
 						
+						$previewType = substr($mime_type, 0, strpos($mime_type, "/"));
+						if($mime_type == "application/pdf")
+							$previewType = "pdf";
+						
 						$previewHtml = "";
-						switch(substr($mime_type, 0, strpos($mime_type, "/")))
+						switch($previewType)
 						{
 							case "video":
 								$previewHtml .= "<video src='$previewUrl' controls preload='metadata'>$pagename</video>\n";
@@ -656,6 +660,9 @@ class PeppermintParsedown extends ParsedownExtra
 							case "audio":
 								$previewHtml .= "<audio src='$previewUrl' controls preload='metadata'>$pagename</audio>\n";
 								break;
+							case "pdf":
+								$previewHtml .= "<object type='application/pdf' data='$previewUrl' style='width: {$previewStyle}px;'></object>";
+								break;
 							case "application":
 							case "image":
 							default:
@@ -1124,7 +1131,11 @@ class PeppermintParsedown extends ParsedownExtra
 			{
 				// We have a short url! Expand it.
 				$shortImageUrl = $imageUrl;
-				$imageUrl = "index.php?action=preview&size=" . max($imageSize["x"], $imageSize["y"]) ."&page=" . rawurlencode($imageUrl);
+				$imageUrl = "index.php?action=preview";
+				if($imageSize !== false) $imageUrl .= "&size=" . max($imageSize["x"], $imageSize["y"]);
+				else $imageUrl .= "&size=original";
+				// This has to be last in order for the extension auto-detection to work correctly
+				$imageUrl .= "&page=" . rawurlencode($shortImageUrl);
 			}
 			
 			$style = "";
@@ -1135,9 +1146,30 @@ class PeppermintParsedown extends ParsedownExtra
 			
 			$urlExtension = pathinfo($imageUrl, PATHINFO_EXTENSION);
 			$urlType = system_extension_mime_type($urlExtension);
+			$embed_type = substr($urlType, 0, strpos($urlType, "/"));
+			if($urlType == "application/pdf") $embed_type = "pdf";
 			$result = [];
-			switch(substr($urlType, 0, strpos($urlType, "/")))
+			switch($embed_type)
 			{
+				case "pdf":
+					$imageUrl = preg_replace("/&size=[0-9]+/", "&size=original", $imageUrl);
+					if($imageSize === false) $style .= "width: 100%;";
+					else $style = str_replace("max-width", "width", $style);
+					
+					$result = [
+						"extent" => strlen($matches[0]),
+						"element" => [
+							"name" => "object",
+							"text" => "",
+							"attributes" => [
+								"type" => "application/pdf",
+								"data" => $imageUrl,
+								"style" => trim($style)
+							]
+						]
+					];
+					break;
+				
 				case "audio":
 					$result = [
 						"extent" => strlen($matches[0]),
@@ -1189,14 +1221,16 @@ class PeppermintParsedown extends ParsedownExtra
 			// ~ Image linker ~
 			
 			$imageHref = $shortImageUrl !== false ? "?page=" . rawurlencode($shortImageUrl) : $imageUrl;
-			$result["element"] = [
-				"name" => "a",
-				"attributes" => [
-					"href" => $imageHref
-				],
-				"text" => [$result["element"]],
-				"handler" => "elements"
-			];
+			if($embed_type !== "pdf") {
+				$result["element"] = [
+					"name" => "a",
+					"attributes" => [
+						"href" => $imageHref
+					],
+					"text" => [$result["element"]],
+					"handler" => "elements"
+				];
+			}
 			
 			// ~
 			
