gnucash-htdocs master: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Thu Jul 7 20:41:43 EDT 2022


Updated	 via  https://github.com/Gnucash/gnucash-htdocs/commit/b6cdfa67 (commit)
	 via  https://github.com/Gnucash/gnucash-htdocs/commit/ed74076f (commit)
	 via  https://github.com/Gnucash/gnucash-htdocs/commit/6ec7bb2c (commit)
	from  https://github.com/Gnucash/gnucash-htdocs/commit/31227423 (commit)



commit b6cdfa679a59807ee21deb9f8599623578938ef4
Author: John Ralls <jralls at ceridwen.us>
Date:   Thu Jul 7 15:24:16 2022 -0700

    Refactor locale detection.
    
    Parse all language strings the same regardless of source.
    Use language quality factors if included.

diff --git a/lang.php b/lang.php
index f1848ac..87601f6 100644
--- a/lang.php
+++ b/lang.php
@@ -2,6 +2,27 @@
 $top_dir = ".";
 $locale_dir = "locale";
 
+# key: locale, value: lang_dir
+$supported_languages = array(
+    'ca_ES' => 'ca',
+    'de_DE' => 'de',
+    'en_US' => 'en',
+    'es_ES' => 'es',
+    'fr_FR' => 'fr',
+    'he_IL' => 'he',
+    'hr_HR' => 'hr',
+    'hu_HU' => 'hu',
+    'id_ID' => 'id',
+    'it_IT' => 'it',
+    'ja_JP' => 'ja',
+    'nb_NO' => 'nb',
+    'nl_NL' => 'nl',
+    'pl_PL' => 'pl',
+    'pt_PT' => 'pt',
+    'zh_CN' => 'zh_CN',
+    'zh_TW' => 'zh_TW'
+);
+
 if (!array_key_exists('HTTP_HOST', $_SERVER) || ($_SERVER["HTTP_HOST"] == "lists.gnucash.org"))
 {
     $home = "https://www.gnucash.org";
@@ -21,76 +42,53 @@ if (array_key_exists('lang_cookie', $_COOKIE)) {
 }
 
 # allow user override.
-$get_lang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING);
-
-if ($get_lang) { $locale = $get_lang; }
+$languages = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING);
 
 # choose a default language based on the client browser's preferred
 # language list
 #echo ("<!-- top_dir: $top_dir, me: ".__FILE__."-->\n");
 
+# Find the locale from Client Accept language
+# Get user preferred languages, and match against supported language
+if ($languages == "" and isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
+{
+    # tolower() => remove space => '-' -> '_'
+    $accept_language = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITIZE_STRING);
+    $languages = str_replace('-','_', str_replace(' ', '', strtolower($accept_language)));
+}
 
-# key: locale, value: lang_dir
-$supported_languages = array(
-    'ca_ES' => 'ca',
-    'de_DE' => 'de',
-    'en_US' => 'en',
-    'es_ES' => 'es',
-    'fr_FR' => 'fr',
-    'he_IL' => 'he',
-    'hr_HR' => 'hr',
-    'hu_HU' => 'hu',
-    'id_ID' => 'id',
-    'it_IT' => 'it',
-    'ja_JP' => 'ja',
-    'nb_NO' => 'nb',
-    'nl_NL' => 'nl',
-    'pl_PL' => 'pl',
-    'pt_PT' => 'pt',
-    'zh_CN' => 'zh_CN',
-    'zh_TW' => 'zh_TW'
-);
-
-# Find the full locale name for short language name.
-if (strlen($locale) == 2) {
-    foreach($supported_languages as $loc_lang => $loc_dir)
+$ranked_langs = [];
+$languages = explode(",", $languages);
+if (count($languages) > 1) {
+    foreach ($languages as $item)
     {
-        if ( (strtolower($locale) == strtolower($loc_dir))
-          || (strtolower($locale) == substr($loc_lang, 0, 2 )) )
-        {
-            $locale = $loc_lang;
-            break;
+        $parts = explode(";", $item);
+        if (!$parts[1]) {
+            $ranked_langs[$parts[0]] = 1.0;
+        } else {
+            $ranked_langs[$parts[0]] = (float)substr($parts[1], 2);
         }
     }
+    arsort($ranked_langs, SORT_NUMERIC);
+} else if ($languages[0] != "") {
+    $ranked_langs[$languages[0]] = 1.0;
 }
 
-# Find the locale from Client Accept language
-if ($locale == "") {
-    # Get user preferred languages, and match against supported language
-    if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
+foreach (array_keys($ranked_langs) as $lang) {
+    if ($ranked_langs[$lang] == 0) {
+        break;
+    }
+    $lang_short = substr($lang, 0, 2);
+    # full match is prefer, but short match is acceptable.
+    foreach ($supported_languages as $loc_lang => $loc_dir)
     {
-        # tolower() => remove space => '-' -> '_'
-        # "fr-ch;q=0.3, en, zh-cn;q=0.7" => "fr_ch;q=0.3,en,zh_cn;q=0.7"
-        $accept_language = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITZE_STRING);
-        $languages = str_replace('-','_', str_replace(' ', '', strtolower($accept_language)));
-        $languages = explode(",", $languages);
-        foreach ($languages as $item)
-        {
-            # "zh_cn;q=0.7" => "zh_cn"
-            $lang = substr($item, 0, strcspn($item, ';'));
-            $lang_short = substr($lang, 0, 2);
-            # full match is prefer, but short match is acceptable.
-            foreach ($supported_languages as $loc_lang => $loc_dir)
-            {
-                if ($lang == strtolower($loc_lang)) { $locale = $loc_lang; break; }
-                if ($lang_short == substr($loc_lang, 0, 2 )) { $locale = $loc_lang; }
-            }
-            if ($locale != "") { break; }
-        }
+        if ($lang == strtolower($loc_lang)) { $locale = $loc_lang; break; }
+        if ($lang_short == substr($loc_lang, 0, 2 )) { $locale = $loc_lang; }
     }
-    # nothing matched, use default language
-    if ($locale == "") { $locale = "en_US"; }
+    if ($locale != "") { break; }
 }
+# nothing matched, use default language
+if ($locale == "") { $locale = "en_US"; }
 
 $lang_dir = array_key_exists($locale, $supported_languages) ?
             $supported_languages[$locale] : "en";

commit ed74076f0505e9b171153f8a1c4f30e88a6b1218
Author: John Ralls <jralls at ceridwen.us>
Date:   Thu Jul 7 14:26:57 2022 -0700

    Filter language cookie and HTTP_ACCEPT_LANGUAGE_HEADER.
    
    To prevent attacks with forged cookies or headers.

diff --git a/lang.php b/lang.php
index c46e59d..f1848ac 100644
--- a/lang.php
+++ b/lang.php
@@ -14,7 +14,7 @@ if ( !isset($lang_dir) ) { $lang_dir = $locale; }
 
 # get the cookie setting
 if (array_key_exists('lang_cookie', $_COOKIE)) {
-    $locale = $_COOKIE['lang_cookie'];
+    $locale = filter_input(INPUT_COOKIE, 'lang_cookie', FILTER_SANITIZE_STRING);
     $lang_cookie = $locale;
 }else{
     $lang_cookie = "";
@@ -32,31 +32,31 @@ if ($get_lang) { $locale = $get_lang; }
 
 # key: locale, value: lang_dir
 $supported_languages = array(
-        'ca_ES' => 'ca',
-        'de_DE' => 'de',
-        'en_US' => 'en',
-        'es_ES' => 'es',
-        'fr_FR' => 'fr',
-        'he_IL' => 'he',
-        'hr_HR' => 'hr',
-        'hu_HU' => 'hu',
-        'id_ID' => 'id',
-        'it_IT' => 'it',
-        'ja_JP' => 'ja',
-        'nb_NO' => 'nb',
-        'nl_NL' => 'nl',
-        'pl_PL' => 'pl',
-        'pt_PT' => 'pt',
-        'zh_CN' => 'zh_CN', 
-        'zh_TW' => 'zh_TW'
-        );
+    'ca_ES' => 'ca',
+    'de_DE' => 'de',
+    'en_US' => 'en',
+    'es_ES' => 'es',
+    'fr_FR' => 'fr',
+    'he_IL' => 'he',
+    'hr_HR' => 'hr',
+    'hu_HU' => 'hu',
+    'id_ID' => 'id',
+    'it_IT' => 'it',
+    'ja_JP' => 'ja',
+    'nb_NO' => 'nb',
+    'nl_NL' => 'nl',
+    'pl_PL' => 'pl',
+    'pt_PT' => 'pt',
+    'zh_CN' => 'zh_CN',
+    'zh_TW' => 'zh_TW'
+);
 
 # Find the full locale name for short language name.
 if (strlen($locale) == 2) {
     foreach($supported_languages as $loc_lang => $loc_dir)
     {
         if ( (strtolower($locale) == strtolower($loc_dir))
-            || (strtolower($locale) == substr($loc_lang, 0, 2 )) )
+          || (strtolower($locale) == substr($loc_lang, 0, 2 )) )
         {
             $locale = $loc_lang;
             break;
@@ -66,33 +66,30 @@ if (strlen($locale) == 2) {
 
 # Find the locale from Client Accept language
 if ($locale == "") {
-        # Get user preferred languages, and match against supported language
-        if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
+    # Get user preferred languages, and match against supported language
+    if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
+    {
+        # tolower() => remove space => '-' -> '_'
+        # "fr-ch;q=0.3, en, zh-cn;q=0.7" => "fr_ch;q=0.3,en,zh_cn;q=0.7"
+        $accept_language = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITZE_STRING);
+        $languages = str_replace('-','_', str_replace(' ', '', strtolower($accept_language)));
+        $languages = explode(",", $languages);
+        foreach ($languages as $item)
         {
-                # tolower() => remove space => '-' -> '_'
-                # "fr-ch;q=0.3, en, zh-cn;q=0.7" => "fr_ch;q=0.3,en,zh_cn;q=0.7"
-                /* Todo: use Locale::canonicalize ( string $locale ) : string instead
-                 * See https://www.php.net/manual/en/locale.canonicalize.php
-                 * Full description: https://unicode-org.github.io/icu/userguide/locale/#canonicalization
-                 * if ICU is installed on the server */
-                $languages = str_replace('-','_', str_replace(' ', '', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
-                $languages = explode(",", $languages);
-                foreach ($languages as $item)
-                {
-                        # "zh_cn;q=0.7" => "zh_cn"
-                        $lang = substr($item, 0, strcspn($item, ';'));
-                        $lang_short = substr($lang, 0, 2);
-                        # full match is prefer, but short match is acceptable.
-                        foreach ($supported_languages as $loc_lang => $loc_dir)
-                        {
-                                if ($lang == strtolower($loc_lang)) { $locale = $loc_lang; break; }
-                                if ($lang_short == substr($loc_lang, 0, 2 )) { $locale = $loc_lang; }
-                        }
-                        if ($locale != "") { break; }
-                }
+            # "zh_cn;q=0.7" => "zh_cn"
+            $lang = substr($item, 0, strcspn($item, ';'));
+            $lang_short = substr($lang, 0, 2);
+            # full match is prefer, but short match is acceptable.
+            foreach ($supported_languages as $loc_lang => $loc_dir)
+            {
+                if ($lang == strtolower($loc_lang)) { $locale = $loc_lang; break; }
+                if ($lang_short == substr($loc_lang, 0, 2 )) { $locale = $loc_lang; }
+            }
+            if ($locale != "") { break; }
         }
-        # nothing matched, use default language
-        if ($locale == "") { $locale = "en_US"; }
+    }
+    # nothing matched, use default language
+    if ($locale == "") { $locale = "en_US"; }
 }
 
 $lang_dir = array_key_exists($locale, $supported_languages) ?

commit 6ec7bb2c80e77e792848627fbc5887c0ec71c260
Author: John Ralls <jralls at ceridwen.us>
Date:   Thu Jul 7 14:11:08 2022 -0700

    Test locale against lang array keys.
    
    Prevents error messages for invalid/unsupported locales.

diff --git a/lang.php b/lang.php
index 794558d..c46e59d 100644
--- a/lang.php
+++ b/lang.php
@@ -95,7 +95,8 @@ if ($locale == "") {
         if ($locale == "") { $locale = "en_US"; }
 }
 
-$lang_dir = $supported_languages[$locale];
+$lang_dir = array_key_exists($locale, $supported_languages) ?
+            $supported_languages[$locale] : "en";
 setcookie("lang_cookie", $locale);
 
 # We should have a locale now, let's set up the required bits and pieces to show



Summary of changes:
 lang.php | 122 ++++++++++++++++++++++++++++++---------------------------------
 1 file changed, 59 insertions(+), 63 deletions(-)



More information about the gnucash-changes mailing list