From 8ea03118c68be77611b1c223e3074ab286343c75 Mon Sep 17 00:00:00 2001 From: Diederik de Groot Date: Mon, 17 Feb 2020 01:59:32 +0100 Subject: [PATCH] First step for adding templated settings/file where placeholders can be filled out. Add 'settings' structure to config.ini Add 'settings' multidimensional config parser to lib/utils.php Signed-off-by: Diederik de Groot --- config.ini | 38 ++++++++++++++++++++++++++++++++++++++ lib/config.php | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- lib/utils.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) diff --git a/config.ini b/config.ini index 21f7036..cc8e145 100644 --- a/config.ini +++ b/config.ini @@ -5,6 +5,15 @@ default_language = English_United_States log_type = SYSLOG ; SYSLOG|STDERR|STDOUT|NULL|FILE log_level = LOG_INFO ; LOG_EMERG|LOG_ALERT|LOG_CRIT|LOG_ERR|LOG_WARNING|LOG_NOTICE|LOG_INFO|LOG_DEBUG ;log_filename = provision.log ; only in case of log_type = FILE +auto_generate_settings = FALSE +auto_sign = FALSE +auto_encrypt = FALSE + +[security] +cert_ca = +cert_priv = +cert_pub = +hash = [subdirs] tftproot = tftpboot @@ -15,3 +24,32 @@ ringtones = ringtones locales = locales countries = countries languages = languages + +[settings] +sshUserId = cisco +sshPassword = cisco +ipAddress = ipv4|ipv6 ; ipv4 | ipv4 | ipv4|ipv6 | ipv6|ipv4 +datetime.template=M/D/YA +datetime.timezone=W. Europe Standard/Daylight Time +datetime.ipaddress=10.x.x.x +datetime.mode=Unicast +members.myhost.hostname = myhost.domain.com +members.myhost.ipv4 = 10.x.x.x +members.myhost.ipv6 = 2001:470::x:x +members.myhost.port = 2000 +;srts. +;common. +;vendor. +locale.country=United_States +locale.language=English_United_States +locale.langcode=en_US +locale.charset=utf-8 +urls.secury = FALSE +urls.information= +urls.authentication= +urls.services= +urls.direcory= +urls.messages= +urls.proxyserver= +;vpn. +;phoneservices. diff --git a/lib/config.php b/lib/config.php index 0106c13..67bbe1b 100644 --- a/lib/config.php +++ b/lib/config.php @@ -1,5 +1,6 @@ 'locales', 'countries' => 'countries', 'languages' => 'languages', + ), + 'security' => Array( + 'cert_ca' => null, + 'cert_priv' => null, + 'cert_pub' => null, + 'hash' => null, + ), + 'settings' => Array( + 'sshUserId' => 'cisco', + 'sshPassword' => 'cisco', + 'ipAddress' => '0', + 'datetime' => Array( + 'template' => 'M/D/YA', + 'timezone' => 'W. Europe Standard/Daylight Time', + 'ipaddress' => '10.x.x.x', + 'mode' => 'Unicast', + ), + 'members' => Array( + 'myhost' => Array( + 'hostname' => 'myhost.domain.com', + 'ipv4' => '10.x.x.x', + 'ipv6' => '2001:470::x:x', + 'port' => '2000', + ) + ), + 'locale' => Array( + 'country' => 'United_States', + 'language' => 'English_United_States', + 'langcode' => 'en_US', + 'charset' => 'utf-8', + ), + 'urls' => Array( + 'secury' => '', + 'information' => '', + 'authentication' => '', + 'services' => '', + 'direcory' => '', + 'messages' => '', + 'proxyserver' => '', + ) ) ); $tree_base = Array( @@ -32,7 +73,8 @@ $tree_base = Array( ); # Merge config -$ini_array = parse_ini_file('../config.ini', TRUE, INI_SCANNER_TYPED); +//$ini_array = parse_ini_file("$base_path/config.ini", TRUE, INI_SCANNER_TYPED); +$ini_array = parse_ini_file_multi("$base_path/config.ini", TRUE, INI_SCANNER_TYPED); if (!empty($ini_array)) { $config = array_merge($base_config, $ini_array); } @@ -77,4 +119,10 @@ switch($config['main']['log_type']) { # Fixup debug $print_debug = (!empty($config['main']['debug'])) ? $config['main']['debug'] : 'off'; $print_debug = ($print_debug == 1) ? 'on' : $print_debug; + +if(defined('STDIN') ) { + print_r($config); + //var_dump($config); +} + ?> diff --git a/lib/utils.php b/lib/utils.php index 28e7652..e884a95 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -7,4 +7,47 @@ function utf8_urldecode($str) { $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str)); return html_entity_decode($str,null,'UTF-8');; } + +function parse_ini_file_multi($file, $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL) { + $explode_str = '.'; + $escape_char = "'"; + // load ini file the normal way + $data = parse_ini_file($file, $process_sections, $scanner_mode); + if (!$process_sections) { + $data = array($data); + } + foreach ($data as $section_key => $section) { + // loop inside the section + foreach ($section as $key => $value) { + if (strpos($key, $explode_str)) { + if (substr($key, 0, 1) !== $escape_char) { + // key has a dot. Explode on it, then parse each subkeys + // and set value at the right place thanks to references + $sub_keys = explode($explode_str, $key); + $subs =& $data[$section_key]; + foreach ($sub_keys as $sub_key) { + if (!isset($subs[$sub_key])) { + $subs[$sub_key] = []; + } + $subs =& $subs[$sub_key]; + } + // set the value at the right place + $subs = $value; + // unset the dotted key, we don't need it anymore + unset($data[$section_key][$key]); + } + // we have escaped the key, so we keep dots as they are + else { + $new_key = trim($key, $escape_char); + $data[$section_key][$new_key] = $value; + unset($data[$section_key][$key]); + } + } + } + } + if (!$process_sections) { + $data = $data[0]; + } + return $data; +} ?>