reasonableframework/system/config.php

194 lines
5.2 KiB
PHP
Raw Permalink 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
2020-03-25 02:43:09 +00:00
* @updated 2020-03-25
2022-11-25 14:15:20 +00:00
* @author Go Namhyeon <abuse@catswords.net>
2018-04-13 05:07:39 +00:00
* @brief Configuration module
2018-03-07 04:44:06 +00:00
*/
if(!is_fn("read_config")) {
2019-05-20 08:19:05 +00:00
function read_config() {
$config = array();
2020-03-25 02:43:55 +00:00
$is_compatible = 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
2020-03-25 02:43:55 +00:00
if(!$is_compatible && check_file_extension($file, "ini.php", array("multiple" => true))) {
2019-11-14 04:52:57 +00:00
$str = include($file);
2019-10-13 13:05:06 +00:00
$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
if(!is_fn("get_config")) {
2019-05-20 08:19:05 +00:00
function get_config() {
2020-01-28 04:36:56 +00:00
$config = get_shared_var("config");
2019-05-20 08:19:05 +00:00
if(!is_array($config)) {
2020-01-28 04:36:56 +00:00
set_shared_var("config", read_config());
2019-05-20 08:19:05 +00:00
}
2020-01-29 11:43:14 +00:00
2020-01-28 04:36:56 +00:00
return get_shared_var("config");
2019-05-20 08:19:05 +00:00
}
2018-03-14 04:34:08 +00:00
}
if(!is_fn("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
}
if(!is_fn("get_current_timestamp")) {
2019-11-14 04:52:57 +00:00
function get_current_timestamp($options=array()) {
2019-10-16 13:26:35 +00:00
$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)) {
2019-12-05 06:59:52 +00:00
$adjust = trim($options['adjust']);
$_adjust = "";
if(strlen($adjust) > 0) {
2019-12-05 07:02:15 +00:00
$units = array(
"s" => array( 1, "second", "seconds"),
"m" => array( 60, "minute", "minutes"),
2020-03-25 02:41:37 +00:00
"h" => array( 3600, "hour", "hours" ),
2019-12-05 07:02:15 +00:00
"d" => array(86400, "day", "days" )
);
2019-12-05 06:59:52 +00:00
$_L = intval(substr($adjust, 0, -1));
$_R = substr($adjust, -1);
if(array_key_exists($_R, $units)) {
if(abs($_L) > 1) {
$_adjust = sprintf("%s %s", $_L, $units[$_R][2]);
} else {
$_adjust = sprintf("%s %s", $_L, $units[$_R][1]);
}
2019-12-05 07:02:15 +00:00
} else {
$_adjust = $adjust;
2019-12-05 06:59:52 +00:00
}
2019-12-05 07:02:15 +00:00
$timestamp = strtotime($_adjust, $timestamp);
2019-12-05 06:59:52 +00:00
}
2019-10-08 06:21:30 +00:00
}
2019-10-16 13:26:35 +00:00
return $timestamp;
}
}
2020-03-25 02:41:37 +00:00
if(!is_fn("get_seconds")) {
function get_seconds($interval) {
$time = 0;
$_U = array("s" => 1, "m" => 60, "h" => 3600, "d" => 86400);
$_L = intval(substr($interval, 0, -1));
$_R = substr($interval, -1);
if(array_key_exists($_R, $_U)) {
$time = $_L * $_U[$_R];
}
return $time;
}
}
if(!is_fn("get_current_datetime")) {
2019-10-16 13:26:35 +00:00
function get_current_datetime($options=array()) {
2019-11-14 04:52:57 +00:00
$config = get_config();
2019-10-16 13:26:35 +00:00
// 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
}
2019-11-17 13:34:51 +00:00
if(!is_fn("get_old_version")) {
2019-12-30 08:41:41 +00:00
function get_old_version() {
$config = get_config();
return get_value_in_array("old_version", $config, 0);
}
2019-11-17 13:34:51 +00:00
}
2020-02-18 03:22:23 +00:00
if(!is_fn("set_max_execution_time")) {
function set_max_execution_time($time) {
if($time > -1) {
@ini_set("max_execution_time", $time);
2020-02-18 03:29:47 +00:00
@set_time_limit($time);
2020-02-18 03:22:23 +00:00
}
}
}
if(!is_fn("set_memory_limit")) {
function set_memory_limit($limit) {
if($limit > -1) {
@ini_set("memory_limit", $limit);
@ini_set("suhosin.memory_limit", $limit);
}
}
}
if(!is_fn("set_upload_max_filesize")) {
2020-02-18 03:31:02 +00:00
function set_upload_max_filesize($size) {
if($size > -1) {
@ini_set("upload_max_filesize", $size);
2020-02-18 03:22:23 +00:00
}
}
}
if(!is_fn("set_post_max_size")) {
function set_post_max_size($size) {
if($size > -1) {
2020-02-18 03:31:02 +00:00
@ini_set("post_max_size", $size);
2020-02-18 03:22:23 +00:00
}
}
}