From 5fed4cb5ab94da9668aae72f22d55ef277eedef7 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 8 Aug 2020 22:18:12 +0100
Subject: [PATCH] Bugfixx: improve rebustness of new filepath_to_pagename and
 pageindex rebuilder

---
 core/05-functions.php        | 16 +++++++---------
 core/20-pageindex-loader.php | 16 +++++++---------
 2 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/core/05-functions.php b/core/05-functions.php
index 67d2630..746ec17 100644
--- a/core/05-functions.php
+++ b/core/05-functions.php
@@ -167,18 +167,16 @@ function path_resolve(string $path, string $basePath = null) {
 function filepath_to_pagename(string $filepath) : string {
 	global $env;
 	// Strip the storage prefix, but only if it isn't a dot
-	if(starts_with($filepath, $env->storage_prefix) && $env->storage_prefix !== ".") {
-		$filepath = substr($filepath, strlen($env->storage_prefix));
-		// Strip the forward slash at the beginning
-		if($filepath[0] == "/" && $env->storage_prefix[-1] !== "/")
-			$filepath = substr($filepath, 1);
-	}
+	if(starts_with($filepath, $env->storage_prefix) && $env->storage_prefix !== ".")
+		$filepath = mb_substr($filepath, mb_strlen($env->storage_prefix));
 	
-	if(preg_match("/\.r[0-9]+$/", $filepath) !== false)
-		$filepath = substr($filepath, 0, strrpos($filepath, ".r"));
+	// If a revision number is detected, strip it
+	if(preg_match("/\.r[0-9]+$/", $filepath) > 0)
+		$filepath = mb_substr($filepath, 0, mb_strrpos($filepath, ".r"));
 	
+	// Strip the .md file extension
 	if(ends_with($filepath, ".md"))
-		$filepath = substr($filepath, 0, -3);
+		$filepath = mb_substr($filepath, 0, -3);
 	
 	return $filepath;
 }
diff --git a/core/20-pageindex-loader.php b/core/20-pageindex-loader.php
index d15e876..984fea6 100644
--- a/core/20-pageindex-loader.php
+++ b/core/20-pageindex-loader.php
@@ -22,25 +22,21 @@ if(!file_exists($paths->pageindex))
 		
 		// Create a new entry
 		$newentry = new stdClass();
-		$newentry->filename = substr( // Store the filename, whilst trimming the storage prefix
+		$newentry->filename = mb_substr( // Store the filename, whilst trimming the storage prefix
 			$pagefilename,
 			mb_strlen(preg_replace("/^\.\//iu", "", $env->storage_prefix)) // glob_recursive trim the ./ from returned filenames , so we need to as well
 		);
 		// Remove the `./` from the beginning if it's still hanging around
-		if(substr($newentry->filename, 0, 2) == "./")
-			$newentry->filename = substr($newentry->filename, 2);
+		if(mb_substr($newentry->filename, 0, 2) == "./")
+			$newentry->filename = mb_substr($newentry->filename, 2);
 		$newentry->size = filesize($pagefilename); // Store the page size
 		$newentry->lastmodified = filemtime($pagefilename); // Store the date last modified
 		// Todo find a way to keep the last editor independent of the page index
 		$newentry->lasteditor = "unknown"; // Set the editor to "unknown"
 		
-		
-		
-		// POTENTIAL BUG: If $env->storage_prefix is not ., then this we need to be more intelligent here
-		
-		
 		// Extract the name of the (sub)page without the ".md"
-		$pagekey = mb_substr($newentry->filename, 0, -3);
+		$pagekey = filepath_to_pagename($newentry->filename);
+		error_log("pagename '$newentry->filename' → filepath '$pagekey'");
 		
 		if(file_exists($env->storage_prefix . $pagekey) && // If it exists...
 			!is_dir($env->storage_prefix . $pagekey)) // ...and isn't a directory
@@ -84,6 +80,7 @@ if(!file_exists($paths->pageindex))
 			}
 		}
 		
+		// If the initial revision doesn't exist on disk, create it (if it does, then we handle that later)
 		if(function_exists("history_add_revision") && !file_exists("{$pagefilename}.r0")) { // Can't use module_exists - too early
 			copy($pagefilename, "{$pagefilename}.r0");
 			$newentry->history = [ (object) [
@@ -111,6 +108,7 @@ if(!file_exists($paths->pageindex))
 			$revid_b = intval($revid_b[0]);
 			return $revid_a - $revid_b;
 		});
+		// We can guarantee that the direcotry separator is present on the end - it's added explicitly earlier
 		$strlen_storageprefix = strlen($env->storage_prefix);
 		foreach($history_revs as $filename) {
 			preg_match("/[0-9]+$/", $filename, $revid);
