summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Bruederli <thomas@roundcube.net>2014-01-22 16:55:28 (GMT)
committerThomas Bruederli <thomas@roundcube.net>2014-01-22 16:55:28 (GMT)
commit1afa909e6352de28b09862fa0507f3db92b9935b (patch)
treea9fe34bbf547395f9d52dba11a52ae5c83a9cd6f
parent7e88e7770d6ee6443d17fdf0210113dd78291bdc (diff)
downloadkolab-freebusy-1afa909e6352de28b09862fa0507f3db92b9935b.tar.gz
Update Kolab\Config class with latest version
-rw-r--r--lib/Kolab/Config.php122
1 files changed, 108 insertions, 14 deletions
diff --git a/lib/Kolab/Config.php b/lib/Kolab/Config.php
index c84426f..2aa49bb 100644
--- a/lib/Kolab/Config.php
+++ b/lib/Kolab/Config.php
@@ -35,6 +35,8 @@ class Config
const INT = 2;
const FLOAT = 3;
const ARR = 4;
+ const HASH = 5;
+ const EMAIL = 6;
protected static $instance;
@@ -68,24 +70,23 @@ class Config
function __construct($env = '')
{
$this->env = $env;
- $this->load($this->basedir . '/config.ini');
}
/**
* Load config from the given .ini file
*/
- private function load($file, $use_env = true)
+ protected function load($file, $use_env = true)
{
// check for relative path
- if (!is_readable($inifile) && is_readable($this->basedir . '/' . $inifile)) {
- $inifile = $this->basedir . '/' . $inifile;
+ if (!is_readable($file) && is_readable($this->basedir . '/' . $file)) {
+ $file = $this->basedir . '/' . $file;
}
$inifile = $this->resolve_path($file, $use_env);
if ($raw = self::parse_ini_file($inifile, true)) {
foreach ($raw as $section => $values) {
$sub = null;
- if (preg_match('/^(\w+)\s+"?(.+)"?$/Ui', $section, $m)) {
+ if (preg_match('/^(\w+)\s+"?(.+)"?$/i', $section, $m)) {
$section = $m[1];
$sub = trim($m[2], '"');
}
@@ -109,7 +110,7 @@ class Config
* Helper method to resolve the absolute path to the given config file.
* This also takes the 'env' property into account.
*/
- private function resolve_path($file, $use_env)
+ protected function resolve_path($file, $use_env)
{
if ($file[0] != '/') {
$file = realpath($this->basedir . '/' . $file);
@@ -121,20 +122,23 @@ class Config
if (is_file($envfile))
return $envfile;
}
-
+
return $file;
}
/**
* Replacement for PHP's parse_ini_file()
*/
- private static function parse_ini_file($filename)
+ protected static function parse_ini_file($filename)
{
$raw = array();
foreach (file($filename) as $_line) {
if ($_line[0] == ';') // skip comments
continue;
+ // chop off comments at the end of a line
+ $_line = preg_replace('/;[^\'"]*$/', '', $_line);
+
if (preg_match('/^\[([a-z0-9-_\.]+[^\]]*)\]/', $_line, $matches)) {
$_cur_section = $matches[1];
$raw[$_cur_section] = array();
@@ -144,7 +148,10 @@ class Config
if (preg_match('/^([a-z0-9\.-_]+)\s*=\s*(.*)/', $_line, $matches)) {
if (isset($_cur_section) && !empty($_cur_section)) {
$_cur_key = $matches[1];
- $raw[$_cur_section][$matches[1]] = isset($matches[2]) ? trim($matches[2], ' "') : '';
+ $value = isset($matches[2]) ? trim($matches[2], ' "') : '';
+ if (preg_match('/^(true|yes|on)$/', $matches[2])) // convert boolean values right away
+ $value = true;
+ $raw[$_cur_section][$matches[1]] = $value;
}
}
else if (preg_match('/^\s+(.*)$/', $_line, $matches)) {
@@ -160,7 +167,7 @@ class Config
/**
* Dump the hierarchical structure of config options into a flat list with keys delimited by dots
*/
- private function register($config, $prefix = '')
+ public function register($config, $prefix = '')
{
// merge the new config values over existing data
if (empty($prefix)) {
@@ -189,7 +196,7 @@ class Config
/**
* Callback to resolve references in the given config option value
*/
- private function resolve_reference(&$value, $key)
+ protected function resolve_reference(&$value, $key)
{
if (is_string($value)) {
$value = preg_replace_callback('/%[({]([\w.]+)[})]/i', array($this, 'replace_reference'), $value);
@@ -199,7 +206,7 @@ class Config
/**
* Callback function to replace the given reference with the read config value
*/
- private function replace_reference($m)
+ protected function replace_reference($m)
{
return $this->data[$m[1]];
}
@@ -235,7 +242,9 @@ class Config
case 'session.savepath':
// return an absolute path for relative directory properties
if (isset($this->data[$name]) && $this->data[$name][0] != '/') {
- $value = realpath(INSTALL_PATH . '/' . $this->data[$name]);
+ if (!($value = realpath(INSTALL_PATH . '/' . $this->data[$name]))) {
+ $value = INSTALL_PATH . '/' . $this->data[$name];
+ }
break;
}
@@ -248,6 +257,18 @@ class Config
}
/**
+ * Adjust, override a config option
+ */
+ public function set($name, $value)
+ {
+ $this->data[$name] = $value;
+
+ if (is_array($value)) {
+ $this->register($this->data[$name], $name.'.');
+ }
+ }
+
+ /**
* Determines whether we have a valid configuration loaded
*
* @return boolean True if valid, False otherwise
@@ -275,10 +296,83 @@ class Config
case self::BOOL:
return (bool)preg_match('/^(true|1|on|enabled|yes)$/i', $value);
case self::ARR:
- return preg_split('/,\s*/', strval($value));
+ return array_filter(preg_split('/,\s*/', strval($value)));
+ case self::HASH:
+ $arr = array();
+ if (preg_match_all('/([\w]+)\s*=\s*([^,]*)(,\s*|$)/', trim(strval($value), '{} '), $matches, PREG_SET_ORDER)) {
+ foreach ($matches as $m) {
+ $arr[$m[1]] = is_numeric($m[2]) ? floatval($m[2]) : $m[2];
+ }
+ }
+ return $arr;
+
+ case self::EMAIL:
+ if (preg_match('/(\S+|("[^"]+"))@\S+/', $value, $m)) {
+ $email = trim($m[0], '<>');
+ }
+
+ return $email;
}
return $value;
}
+
+ /**
+ * Shortcut method to convert to a boolean value
+ */
+ public static function boolean($value)
+ {
+ return self::convert($value, self::BOOL);
+ }
+
+ /**
+ * Shortcut method to convert to a integer value
+ */
+ public static function intval($value)
+ {
+ return self::convert($value, self::INT);
+ }
+
+ /**
+ * Shortcut method to convert to a float value
+ */
+ public static function floatval($value)
+ {
+ return self::convert($value, self::FLOAT);
+ }
+
+ /**
+ * Shortcut method to convert to an array value
+ */
+ public static function arr($value)
+ {
+ return self::convert($value, self::ARR);
+ }
+
+ /**
+ * Shortcut method to convert to a hash array value
+ */
+ public static function hash($value)
+ {
+ return self::convert($value, self::HASH);
+ }
+
+ /**
+ * Convenience method to check whether a certain value is part of an option (list)
+ *
+ * @param mixed Value to compare
+ * @param mixed Config option value
+ * @param boolean Treat undefined options as 'match'
+ * @return boolean True of the given value is listed in config
+ */
+ public static function in_array($value, $option, $or_not_set = false)
+ {
+ // return true if option is not set (means 'allow all')
+ if (!isset($option) && $or_not_set) {
+ return true;
+ }
+
+ return in_array($value, self::convert($option, self::ARR));
+ }
}