reasonableframework/system/config.php

115 lines
3.1 KiB
PHP
Raw Normal View History

2017-12-17 20:40:04 +00:00
<?php
2018-03-07 04:44:06 +00:00
/**
2018-03-07 04:45:25 +00:00
* @file config.php
2018-04-13 05:07:39 +00:00
* @date 2018-04-13
2019-10-13 11:01:15 +00:00
* @updated 2019-10-13
2018-03-07 04:44:06 +00:00
* @author Go Namhyeon <gnh1201@gmail.com>
2018-04-13 05:07:39 +00:00
* @brief Configuration module
2018-03-07 04:44:06 +00:00
*/
2019-02-26 05:40:03 +00:00
if(!check_function_exists("read_config")) {
2019-05-20 08:19:05 +00:00
function read_config() {
$config = array();
2019-10-16 13:26:35 +00:00
$is_legacy_version = version_compare(phpversion(), "5.3.0", "<");
2019-05-20 08:19:05 +00:00
$files = retrieve_storage_dir("config");
foreach($files as $file) {
$ini = array();
2019-10-16 13:29:33 +00:00
// `parse_ini_string` function is not supported under 5.3.0. Use only 'ini' file
2019-10-13 13:05:06 +00:00
if(!$is_legacy_version && check_file_extension($file, "ini.php", array("multiple" => true))) {
$ini = parse_ini_string($str);
2019-05-20 08:19:05 +00:00
} elseif(check_file_extension($file, "ini")) {
$ini = parse_ini_file($file);
}
foreach($ini as $k=>$v) {
$config[$k] = $v;
}
}
return $config;
}
2017-12-17 20:40:04 +00:00
}
2018-02-12 05:43:11 +00:00
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_config")) {
2019-05-20 08:19:05 +00:00
function get_config() {
$config = get_scope("config");
2018-04-13 05:06:21 +00:00
2019-05-20 08:19:05 +00:00
if(!is_array($config)) {
set_scope("config", read_config());
}
2018-04-13 05:06:21 +00:00
2019-05-20 08:19:05 +00:00
return get_scope("config");
}
2018-03-14 04:34:08 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_config_value")) {
2019-05-20 08:19:05 +00:00
function get_config_value($key) {
$config = get_config();
2018-03-14 04:34:08 +00:00
2019-05-20 08:19:05 +00:00
$config_value = "";
if(!array_key_empty($key, $config)) {
$config_value = $config[$key];
}
2018-03-14 04:34:08 +00:00
2019-05-20 08:19:05 +00:00
return $config_value;
}
2018-03-07 04:44:06 +00:00
}
2019-10-16 13:26:35 +00:00
if(!check_function_exists("get_current_timestamp")) {
function get_current_timestamp($options=array())
$timestamp = time();
2019-05-20 08:19:05 +00:00
$config = get_config();
2019-10-16 13:26:35 +00:00
// get timeformat
2019-05-20 08:19:05 +00:00
$timeformat = get_value_in_array("timeformat", $config, "Y-m-d H:i:s");
2019-10-16 13:26:35 +00:00
if(!array_key_empty("timeformat", $options)) {
$timeformat = $options['timeformat'];
}
2019-05-20 08:19:05 +00:00
2019-10-16 13:26:35 +00:00
// get time from NTP server
2019-05-20 08:19:05 +00:00
if(!array_key_empty("timeserver", $config)) {
if(loadHelper("timetool")) {
$timestamp = get_server_time($config['timeserver']);
}
}
2019-10-16 13:26:35 +00:00
// set now time
2019-10-10 07:10:32 +00:00
if(!array_key_empty("now", $options)) {
try {
2019-10-16 13:26:35 +00:00
$dateTimeObject = DateTime::createFromFormat($timeformat, $options['now']);
2019-10-10 07:10:32 +00:00
$timestamp = $dateTimeObject->getTimestamp();
} catch(Exception $e) {
$timestamp = strtotime($options['now']);
}
}
2019-10-16 13:26:35 +00:00
// adjust time
2019-10-08 06:21:30 +00:00
if(!array_key_empty("adjust", $options)) {
$timestamp = strtotime($options['adjust'], $timestamp);
}
2019-10-16 13:26:35 +00:00
return $timestamp;
}
}
if(!check_function_exists("get_current_datetime")) {
function get_current_datetime($options=array()) {
// get timeformat
$timeformat = get_value_in_array("timeformat", $config, "Y-m-d H:i:s");
if(!array_key_empty("timeformat", $options)) {
$timeformat = $options['timeformat'];
}
// get timestamp
2019-10-16 13:29:33 +00:00
$timestamp = get_current_timestamp($options);
2019-10-16 13:26:35 +00:00
// set datetime
2019-05-20 08:19:05 +00:00
$datetime = date($timeformat, $timestamp);
2019-10-16 13:26:35 +00:00
2019-05-20 08:19:05 +00:00
return $datetime;
}
2018-04-13 05:07:39 +00:00
}