changeset 348:596196f2b0c5 default tip

Improve relative URL translation in header text blobs.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 20 Dec 2023 09:17:55 +0200
parents 7da360685721
children
files mgallery.php
diffstat 1 files changed, 28 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/mgallery.php	Wed Sep 06 13:04:41 2023 +0300
+++ b/mgallery.php	Wed Dec 20 09:17:55 2023 +0200
@@ -209,6 +209,30 @@
 }
 
 
+function mgTranslateRelativeURLs($text, $path, $base)
+{
+  // Translate / transform relative href="" URLs in given text
+  // XXX TODO: Perhaps use regexp backreferences instead of loop
+  $quoteList = ["'", "\""];
+  foreach ($quoteList as $quote)
+  {
+    $text = preg_replace_callback(
+      "@href\s*=\s*".$quote."([^\"]+)".$quote."@i",
+      function ($matches) use($path, $base)
+      {
+        $mstmp = $matches[1];
+        if (preg_match("@^[a-z]+://@i", $mstmp) === 0)
+        {
+          if ($mstmp[0] != "/")
+            $mstmp = $base."/".$path."/".str_replace("//", "/", $mstmp);
+        }
+        return "href=\"".$mstmp."\"";
+      }, $text);
+  }
+  return $text;
+}
+
+
 function mgGetImageTitleStr(&$data, $filename)
 {
   return
@@ -722,26 +746,12 @@
   if ($ctrlFlags & MGF_BREADCRUMBS)
     mgPrintBreadCrumbs($galData);
 
-  if (isset($galData["header"]) && strlen($gheader = mgGetTrans($galData["header"])) > 0)
+  if (isset($galData["header"]) && strlen($headerText = mgGetTrans($galData["header"])) > 0)
   {
     // Translate relative URLs in header, if needed
-    $baseURL = mgGetSetting("image_url");
-
-    $headerText = preg_replace_callback(
-      "@href\s*=\s*\"([^\"]+)\"@i",
-      function ($matches) use($galPath, $baseURL)
-      {
-        $mstmp = $matches[1];
-        if (preg_match("@^[a-z]+://@i", $mstmp) === 0)
-        {
-          if ($mstmp[0] != "/")
-            $mstmp = $baseURL."/".$galPath."/".str_replace("//", "/", $mstmp);
-        }
-        return "href=\"".$mstmp."\"";
-      },
-      $gheader);
-
-    echo "<div class=\"albumHeaderText\">".$headerText."</div>\n";
+    echo "<div class=\"albumHeaderText\">".
+      mgTranslateRelativeURLs($headerText, $galPath, mgGetSetting("image_url")).
+      "</div>\n";
   }
 
   echo mgGetPageInfoHeaderEnd();