changeset 89:d9524db8f6a5

Improve how language etc. cookie settings are handled.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 15 Sep 2016 17:32:32 +0300
parents 6558eb187e4b
children 3953f534c874
files mgallery.php
diffstat 1 files changed, 29 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/mgallery.php	Thu Sep 15 16:05:55 2016 +0300
+++ b/mgallery.php	Thu Sep 15 17:32:32 2016 +0300
@@ -18,6 +18,12 @@
 //
 // Various utility functions
 //
+function mgSetCookie($name, $value)
+{
+  global $galBaseURL;
+  setcookie($name, $value, time() + 365*24*60*60, $galBaseURL); // expire in a year
+}
+
 function mgTrimIfString($val)
 {
   if (is_string($val))
@@ -67,7 +73,7 @@
     if (isset($_GET["css"]))
     {
       $pageCSSIndex = intval($_GET["css"]);
-      setcookie($pageCSSData["cookie"], $pageCSSIndex, time() + 365*24*60*60, "/"); // expire in a year
+      mgSetCookie($pageCSSData["cookie"], $pageCSSIndex); // expire in a year
     }
     else
     {
@@ -160,12 +166,12 @@
 
 function mgGetLanguageSelector()
 {
-  global $pageLang, $pageLanguages, $galBaseURL;
+  global $pageLang, $pageLanguages;
   if (isset($pageLanguages) && count($pageLanguages) > 0)
   {
     $str = "<div id=\"languages\">";
     foreach ($pageLanguages as $id => $lang)
-      $str .= "<a href=\"".$galBaseURL."?from=".urlencode($_SERVER["REQUEST_URI"])."&amp;lang=".$id."\">".$lang."</a> ";
+      $str .= "<a href=\"?lang=".$id."\">".$lang."</a> ";
     return $str."</div>\n";
   }
   else
@@ -458,39 +464,7 @@
 
 
 //
-// Check language
-//
-if (($tmp = mgGetRequestItem("lang", FALSE, TRUE)) !== FALSE)
-{
-  $tmp = strtolower($tmp);
-  if (array_key_exists($tmp, $pageLanguages))
-  {
-    $pageLang = $tmp;
-    setcookie("mglang", $tmp, time() + 365*24*60*60); // expire in a year
-  }
-}
-else
-if (isset($_COOKIE["mglang"]))
-{
-  $tmp = strtolower($_COOKIE["mglang"]);
-  if (array_key_exists($tmp, $pageLanguages))
-    $pageLang = $tmp;
-}
-
-
-//
-// Check for "from" redirection
-//
-if (($from = mgGetRequestItem("from", FALSE, TRUE)) !== FALSE &&
-    preg_match("@^[a-z0-9/_.]+\$@i", $from))
-{
-  header("Location: ".$from);
-  exit;
-}
-
-
-//
-// Get gallery settings
+// Read and process gallery settings
 //
 mgReadSettings();
 
@@ -519,6 +493,25 @@
   $galImage = basename($galImage);
 
 
+// Check language setting
+if (($tmp = mgGetRequestItem("lang", FALSE, TRUE)) !== FALSE)
+{
+  $tmp = strtolower($tmp);
+  if (array_key_exists($tmp, $pageLanguages))
+  {
+    $pageLang = $tmp;
+    mgSetCookie("mglang", $tmp);
+  }
+}
+else
+if (isset($_COOKIE["mglang"]))
+{
+  $tmp = strtolower($_COOKIE["mglang"]);
+  if (array_key_exists($tmp, $pageLanguages))
+    $pageLang = $tmp;
+}
+
+
 //
 // Attempt to read the data cache file
 //