fixes #2488 improve handling of derivative config

Refactors how derivative and disabled_derivatives config values are loaded from the database, supporting both parameters and using a new safe_unserialize function. Updates ImageStdParams to use the global config and ensures proper serialization/deserialization of disabled type maps, with improved save logic to avoid unnecessary writes.
This commit is contained in:
Linty
2025-12-22 16:17:13 +01:00
parent 732145f6b7
commit 2526d4a911
2 changed files with 38 additions and 7 deletions

21
i.php
View File

@@ -365,6 +365,15 @@ function send_derivative($expires)
fclose($fp); fclose($fp);
} }
function safe_unserialize($value)
{
if (is_string($value))
{
return unserialize($value);
}
return $value;
}
$page=array(); $page=array();
$begin = $step = microtime(true); $begin = $step = microtime(true);
$timing=array(); $timing=array();
@@ -388,7 +397,17 @@ catch (Exception $e)
} }
pwg_db_check_charset(); pwg_db_check_charset();
list($conf['derivatives']) = pwg_db_fetch_row(pwg_query('SELECT value FROM '.$prefixeTable.'config WHERE param=\'derivatives\'')); $query = '
SELECT param, value
FROM '.$prefixeTable.'config
WHERE param IN (\'derivatives\', \'disabled_derivatives\')
;';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
$conf[ $row['param'] ] = $row['value'];
}
ImageStdParams::load_from_db(); ImageStdParams::load_from_db();

View File

@@ -103,11 +103,13 @@ final class ImageStdParams
*/ */
static function get_disabled_type_map() static function get_disabled_type_map()
{ {
global $conf;
if (count(self::$disabled_type_map)) if (count(self::$disabled_type_map))
{ {
return self::$disabled_type_map; return self::$disabled_type_map;
} }
return conf_get_param('disabled_derivatives', array()); return $conf['disabled_derivatives'] ?? array();
} }
/** /**
@@ -178,9 +180,16 @@ final class ImageStdParams
{ {
self::$watermark = new WatermarkParams(); self::$watermark = new WatermarkParams();
self::$type_map = self::get_enabled_default_sizes(); self::$type_map = self::get_enabled_default_sizes();
self::$disabled_type_map = self::get_disabled_default_sizes(); self::save(false);
self::save();
} }
self::$disabled_type_map = safe_unserialize(self::get_disabled_type_map());
if (empty(self::$disabled_type_map))
{
self::$disabled_type_map = self::get_disabled_default_sizes();
self::save_disabled();
}
self::build_maps(); self::build_maps();
} }
@@ -200,14 +209,14 @@ final class ImageStdParams
static function set_and_save($map) static function set_and_save($map)
{ {
self::$type_map = $map; self::$type_map = $map;
self::save(); self::save(false);
self::build_maps(); self::build_maps();
} }
/** /**
* Saves the configuration in database. * Saves the configuration in database.
*/ */
static function save() static function save($save_disabled = true)
{ {
$ser = serialize( array( $ser = serialize( array(
'd' => self::$type_map, 'd' => self::$type_map,
@@ -217,8 +226,11 @@ final class ImageStdParams
) ); ) );
conf_update_param('derivatives', addslashes($ser) ); conf_update_param('derivatives', addslashes($ser) );
if ($save_disabled)
{
self::save_disabled(); self::save_disabled();
} }
}
/** /**
* Saves the disabled configuration in database. * Saves the disabled configuration in database.