aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormistic100 <mistic@piwigo.org>2014-05-29 14:11:14 +0000
committermistic100 <mistic@piwigo.org>2014-05-29 14:11:14 +0000
commitb317745ac973ced560269f9d694d7d719fbea884 (patch)
tree06ff51441387ef869dea449049648ff49f8d7616
parent42274fb26fffb395a73e1fa60620307cfdac9699 (diff)
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
Diffstat (limited to '')
-rw-r--r--include/functions.inc.php74
1 files changed, 53 insertions, 21 deletions
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;
}
}
@@ -1225,6 +1225,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.
*
* @param array $array