From b317745ac973ced560269f9d694d7d719fbea884 Mon Sep 17 00:00:00 2001 From: mistic100 Date: Thu, 29 May 2014 14:11:14 +0000 Subject: feature 3038 : add $updateGlobal and $parser options to conf_update_param git-svn-id: http://piwigo.org/svn/trunk@28567 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/functions.inc.php | 74 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/functions.inc.php b/include/functions.inc.php index 03d6c6b0f..bdd430171 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -1162,34 +1162,34 @@ SELECT param, value * * @param string $param * @param string $value + * @param boolean $updateGlobal update global *$conf* variable + * @param callable $parser function to apply to the value before save in database + (eg: serialize, json_encode) will not be applied to *$conf* if *$parser* is *true* */ -function conf_update_param($param, $value) +function conf_update_param($param, $value, $updateGlobal=false, $parser=null) { - $query = ' -SELECT param - FROM '.CONFIG_TABLE.' - WHERE param = \''.$param.'\' -;'; - $params = query2array($query, null, 'param'); - - if (count($params) == 0) + if ($parser != null) { - $query = ' -INSERT - INTO '.CONFIG_TABLE.' - (param, value) - VALUES(\''.$param.'\', \''.$value.'\') -;'; - pwg_query($query); + $dbValue = call_user_func($parser, $value); } else { - $query = ' -UPDATE '.CONFIG_TABLE.' - SET value = \''.$value.'\' - WHERE param = \''.$param.'\' + $dbValue = $value; + } + + $query = ' +INSERT INTO + '.CONFIG_TABLE.' (param, value) + VALUES(\''.$param.'\', \''.$dbValue.'\') + ON DUPLICATE KEY UPDATE value = \''.$dbValue.'\' ;'; - pwg_query($query); + + pwg_query($query); + + if ($updateGlobal) + { + global $conf; + $conf[$param] = $value; } } @@ -1224,6 +1224,38 @@ DELETE FROM '.CONFIG_TABLE.' } } +/** + * Apply *unserialize* on a value only if it is a string + * @since 2.7 + * + * @param array|string $value + * @return array + */ +function safe_unserialize($value) +{ + if (is_string($value)) + { + return unserialize($value); + } + return $value; +} + +/** + * Apply *json_decode* on a value only if it is a string + * @since 2.7 + * + * @param array|string $value + * @return array + */ +function safe_json_decode($value) +{ + if (is_string($value)) + { + return json_decode($value, true); + } + return $value; +} + /** * Prepends and appends strings at each value of the given array. * -- cgit v1.2.3