From 6d19af2e1b27d602e218837c93265bf6d635eae3 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 20 Oct 2019 21:42:13 +0100
Subject: [PATCH] Refactor errorimage into core & greatly improve it

---
 core/05-functions.php        | 52 ++++++++++++++++++++++++++++++++++++
 module_index.json            |  6 ++---
 modules/feature-upload.php   | 35 +-----------------------
 modules/parser-parsedown.php | 10 +++++++
 4 files changed, 66 insertions(+), 37 deletions(-)

diff --git a/core/05-functions.php b/core/05-functions.php
index cfb2805..878cb02 100644
--- a/core/05-functions.php
+++ b/core/05-functions.php
@@ -432,6 +432,58 @@ function system_extension_mime_type($ext) {
 	return isset($types[$ext]) ? $types[$ext] : null;
 }
 
+/**
+ * Creates an images containing the specified text.
+ * Useful for sending errors back to the client.
+ * @package core
+ * @param	string	$text			The text to include in the image.
+ * @param	int		$target_size	The target width to aim for when creating 
+ * 									the image. Not not specified, a value is 
+ * 									determined automatically.
+ * @return	resource				The handle to the generated GD image.
+ */
+function errorimage($text, $target_size = null)
+{
+	$width = 0;
+	$height = 0;
+	$border_size = 10; // in px, if $target_size isn't null has no effect
+	$line_spacing = 2; // in px
+	$font_size = 5; // 1 - 5
+	
+	$font_width = imagefontwidth($font_size);	// in px
+	$font_height = imagefontheight($font_size);	// in px
+	$text_lines = array_map("trim", explode("\n", $text));
+	
+	if(!empty($target_size)) {
+		$width = $target_size;
+		$height = $target_size * (2 / 3);
+	}
+	else {
+		$height = count($text_lines) * $font_height + 
+			(count($text_lines) - 1) * $line_spacing +
+			$border_size * 2;
+		foreach($text_lines as $line)
+			$width = max($width, $font_width * strlen($line));
+		$width += $border_size * 2;
+	}
+	
+	$image = imagecreatetruecolor($width, $height);
+	imagefill($image, 0, 0, imagecolorallocate($image, 250, 249, 251)); // Set the background to #faf8fb
+	
+	$i = 0;
+	foreach($text_lines as $line) {
+		imagestring($image, $font_size,
+			($width / 2) - (($font_width * strlen($line)) / 2),
+			$border_size + $i * ($font_height + $line_spacing),
+			$line,
+			imagecolorallocate($image, 68, 39, 113) // #442772
+		);
+		$i++;	
+	}
+	
+	return $image;
+}
+
 /**
  * Generates a stack trace.
  * @package core
diff --git a/module_index.json b/module_index.json
index fc30539..9d2a5c6 100755
--- a/module_index.json
+++ b/module_index.json
@@ -162,10 +162,10 @@
     {
         "id": "feature-upload",
         "name": "Uploader",
-        "version": "0.6.1",
+        "version": "0.6.2",
         "author": "Starbeamrainbowlabs",
         "description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File\/' prefix.",
-        "lastupdate": 1568296731,
+        "lastupdate": 1571603165,
         "optional": false,
         "extra_data": []
     },
@@ -337,7 +337,7 @@
         "version": "0.10",
         "author": "Emanuil Rusev & Starbeamrainbowlabs",
         "description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation.",
-        "lastupdate": 1571601235,
+        "lastupdate": 1571603632,
         "optional": false,
         "extra_data": {
             "Parsedown.php": "https:\/\/raw.githubusercontent.com\/erusev\/parsedown\/fe7a50eceb4a3c867cc9fa9c0aa906b1067d1955\/Parsedown.php",
diff --git a/modules/feature-upload.php b/modules/feature-upload.php
index 6828161..51adc39 100644
--- a/modules/feature-upload.php
+++ b/modules/feature-upload.php
@@ -1,7 +1,7 @@
 <?php
 register_module([
 	"name" => "Uploader",
-	"version" => "0.6.1",
+	"version" => "0.6.2",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File/' prefix.",
 	"id" => "feature-upload",
@@ -656,37 +656,4 @@ function getsvgsize($svgFilename)
 	return $imageSize;
 }
 
-/**
- * Creates an images containing the specified text.
- * Useful for sending errors back to the client.
- * @package feature-upload
- * @param	string	$text			The text to include in the image.
- * @param	int		$target_size	The target width to aim for when creating
- * 									the image.
- * @return	resource				The handle to the generated GD image.
- */
-function errorimage($text, $target_size = null)
-{
-	$width = 640;
-	$height = 480;
-	
-	if(!empty($target_size))
-	{
-		$width = $target_size;
-		$height = $target_size * (2 / 3);
-	}
-	
-	$image = imagecreatetruecolor($width, $height);
-	imagefill($image, 0, 0, imagecolorallocate($image, 238, 232, 242)); // Set the background to #eee8f2
-	$fontwidth = imagefontwidth(3);
-	imagestring($image, 3,
-		($width / 2) - (($fontwidth * strlen($text)) / 2),
-		($height / 2) - (imagefontheight(3) / 2),
-		$text,
-		imagecolorallocate($image, 17, 17, 17) // #111111
-	);
-	
-	return $image;
-}
-
 ?>
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index 4e7df37..1def460 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -52,6 +52,16 @@ register_module([
 			)));
 		});
 		
+		add_action("parsedown-render-ext", function() {
+			global $settings, $env, $paths;
+			
+			if(!isset($_GET["source"])) {
+				http_response_code(400);
+				header("content-type: image/png");
+				imagepng(errorimage("Error: No source text \nspecified."));
+			}
+		});
+		
 		/*
  		 * ███████ ████████  █████  ████████ ██ ███████ ████████ ██  ██████ ███████
  		 * ██         ██    ██   ██    ██    ██ ██         ██    ██ ██      ██
