[TikiWiki-commits] [Git][tikiwiki/tiki][29.x] [FIX] PreferencesLib:getPreference() did not use preferences defaults, and...

Benoit Grégoire (@benoitg) via TikiWiki-cvs <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <69c5961e4a302_3b18ff607378a@gitlab-sidekiq-low-urgency-cpu-bound-v2-5b596b4d8c-klh4m.mail>

Benoit Grégoire pushed to branch 29.x at Tiki Wiki CMS Groupware / Tiki


Commits:
1366c79b by Benoit Grégoire at 2026-03-26T20:17:14+00:00
[FIX] PreferencesLib:getPreference() did not use preferences defaults, and...
---
* [FIX] PreferencesLib:getPreference() did not use preferences defaults, and could return incompatible types if a preference is unset.  Not a full fix because I am currently working on a branch.  Only fixes what I can directly observe.

See merge request tikiwiki/tiki!9865

- - - - -


2 changed files:

- lib/prefslib.php
- lib/tikilib.php


Changes:

=====================================
lib/prefslib.php
=====================================
@@ -170,14 +170,26 @@ class PreferencesLib
             $source = $prefs;
         }
 
-        $value = isset($source[$name]) ? $source[$name] : null;
-        if (
-            ! empty($value) &&
-            is_string($value) &&
-            (strlen($value) > 1 && $value[1] == ':' && str_contains($value, '{')) &&
-            false !== $unserialized = @unserialize($value)
-        ) {
-            $value = $unserialized;
+        //Determine the effective value
+        if (array_key_exists($name, $source)) { //Cannot use isset here, null can be a valid value for a pref
+            $value = $source[$name];
+            if (
+                ! empty($value) &&
+                is_string($value) &&
+                (strlen($value) > 1 && $value[1] == ':' && str_contains($value, '{')) &&
+                false !== $unserialized = @unserialize($value)
+            ) {
+                $value = $unserialized;
+            }
+        } elseif (array_key_exists('default', $data[$name])) { //Here again, null may be a valid default value
+            $value = $data[$name]['default'];
+        } else {
+            /*  Note:  This is still incomplete, 'default' isn't mandatory.
+                'multilist' and 'multicheckbox' should return arrays.
+                The different string types should return an empty string (because that seems to be the current semantics of the pref system)
+                Once again, in a stable branch, so I don't want to do such a large change of behaviour right now - benoitg - 2026-03-26
+                */
+            $value = null;
         }
 
         $info['preference'] = $name;
@@ -260,6 +272,10 @@ class PreferencesLib
             if (is_string($info['default'])) {
                 $info['modified'] = str_replace("\r\n", "\n", $info['value'] ?? '') != $info['default'];
             } else {
+                /* This is from https://gitlab.com/tikiwiki/tiki/-/merge_requests/2666
+                   It is wrong.  Now any preference with non-string values will not be compared at all.
+                   But I'm currently working on a branch, so I cannot fix this immediately - benoitg - 2026-03-26
+                   */
                 $info['modified'] = false;
             }
         }
@@ -1350,6 +1366,7 @@ class PreferencesLib
                 if (isset($info['default'])) {
                     $defaults[$name] = $info['default'];
                 } else {
+                    //This isn't checking types and will be wildly incorrect for array types. And currently other functions are defaulting to null - benoitg - 2026-03-26
                     $defaults[$name] = '';
                 }
             }


=====================================
lib/tikilib.php
=====================================
@@ -4418,6 +4418,15 @@ class TikiLib extends TikiDb_Bridge
     }
 
     /**
+     * This seems ridiculous for a public function.  The preference system has
+     * all the information to know the format, and default value, without the user
+     * doing it.  And this will return possibly completely incompatibe values if
+     * the preference isn't set, and the wrong default is provided.  And most current
+     * code use do not provide a value at all.
+     * different unserialize heuristic than PreferencesLib::getPreference()
+     * Maybe this was meant as some sort of performance optimization and is now
+     * lost to history.  See how PreferencesLib::getPreference() does it.
+     * benoitg - 2026-03-26
      * @param $name
      * @param string $default
      * @param bool $expectArray



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/1366c79b4fdc043cd065f955fb8bc0d0a1e5df35

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/1366c79b4fdc043cd065f955fb8bc0d0a1e5df35
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.