+
+ /**
+ * Tries to load the specified configuration array into the App->config array.
+ * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
+ *
+ * @param array $config
+ * @param bool $overwrite Force value overwrite if the config key already exists
+ */
+ public function loadConfigArray(array $config, $overwrite = false)
+ {
+ foreach ($config as $category => $values) {
+ foreach ($values as $key => $value) {
+ if ($overwrite) {
+ self::setConfigValue($category, $key, $value);
+ } else {
+ self::setDefaultConfigValue($category, $key, $value);
+ }
+ }
+ }
+ }
+
+ /**
+ * @param string $cat Config category
+ * @param string $k Config key
+ * @param mixed $default Default value if it isn't set
+ *
+ * @return string Returns the value of the Config entry
+ */
+ public static function getConfigValue($cat, $k = null, $default = null)
+ {
+ $return = $default;
+
+ if ($cat === 'config') {
+ if (isset(self::$config[$k])) {
+ $return = self::$config[$k];
+ }
+ } else {
+ if (isset(self::$config[$cat][$k])) {
+ $return = self::$config[$cat][$k];
+ } elseif ($k == null && isset(self::$config[$cat])) {
+ $return = self::$config[$cat];
+ }
+ }
+
+ return $return;
+ }
+
+ /**
+ * Sets a default value in the config cache. Ignores already existing keys.
+ *
+ * @param string $cat Config category
+ * @param string $k Config key
+ * @param mixed $v Default value to set
+ */
+ private static function setDefaultConfigValue($cat, $k, $v)
+ {
+ if (!isset(self::$config[$cat][$k])) {
+ self::setConfigValue($cat, $k, $v);
+ }
+ }
+
+ /**
+ * Sets a value in the config cache. Accepts raw output from the config table
+ *
+ * @param string $cat Config category
+ * @param string $k Config key
+ * @param mixed $v Value to set
+ */
+ public static function setConfigValue($cat, $k, $v)
+ {
+ // Only arrays are serialized in database, so we have to unserialize sparingly
+ $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
+
+ if ($cat === 'config') {
+ self::$config[$k] = $value;
+ } else {
+ if (!isset(self::$config[$cat])) {
+ self::$config[$cat] = [];
+ }
+
+ self::$config[$cat][$k] = $value;
+ }
+ }
+
+ /**
+ * Deletes a value from the config cache
+ *
+ * @param string $cat Config category
+ * @param string $k Config key
+ */
+ public static function deleteConfigValue($cat, $k)
+ {
+ if ($cat === 'config') {
+ if (isset(self::$config[$k])) {
+ unset(self::$config[$k]);
+ }
+ } else {
+ if (isset(self::$config[$cat][$k])) {
+ unset(self::$config[$cat][$k]);
+ }
+ }
+ }
+
+ /**
+ * Returns the whole configuration
+ *
+ * @return array The configuration
+ */
+ public static function getAll()
+ {
+ return self::$config;
+ }