-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathConfig.php
153 lines (132 loc) · 4.12 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Class: Config
* Holds all of the configuration settings for the entire site.
*/
class Config {
# Array: $data
# Holds the configuration data as a $key => $val array.
private $data = array();
/**
* Function: __construct
* Loads the configuration file from disk.
*/
private function __construct() {
if (!$this->read() and !INSTALLING)
trigger_error(
__("Could not read the configuration file."),
E_USER_ERROR
);
fallback($this->data["sql"], array());
fallback($this->data["enabled_modules"], array());
fallback($this->data["enabled_feathers"], array());
fallback($this->data["routes"], array());
}
/**
* Function: __get
* Handles access to the configuration data.
*
* Returns:
* @mixed@
*/
public function __get(
$name
): mixed {
if (isset($this->data[$name]))
return $this->data[$name];
trigger_error(
__("Requested configuration setting not found."),
E_USER_NOTICE
);
return null;
}
/**
* Function: __isset
* Handles access to the configuration data.
*/
public function __isset(
$name
): bool {
return isset($this->data[$name]);
}
/**
* Function: read
* Reads the configuration file and decodes the settings.
*/
private function read(
): array|false {
$security = "<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n";
$contents = @file_get_contents(
INCLUDES_DIR.DIR."config.json.php"
);
if ($contents === false)
return false;
$json = json_get(
str_replace($security, "", $contents),
true
);
if (!is_array($json))
return false;
return $this->data = $json;
}
/**
* Function: write
* Encodes the settings and writes the configuration file.
*/
private function write(
): int|false {
$contents = "<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n";
$contents.= json_set(
$this->data,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);
return @file_put_contents(
INCLUDES_DIR.DIR."config.json.php",
$contents
);
}
/**
* Function: set
* Adds or replaces a configuration setting with the given value.
*
* Parameters:
* $setting - The setting name.
* $value - The value to set.
* $fallback - Add the setting only if it doesn't exist.
*/
public function set(
$setting,
$value,
$fallback = false
): int|bool {
if (isset($this->data[$setting]) and $fallback)
return true;
$this->data[$setting] = $value;
if (class_exists("Trigger"))
Trigger::current()->call("change_setting", $setting, $value);
return $this->write();
}
/**
* Function: remove
* Removes a configuration setting.
*
* Parameters:
* $setting - The setting name.
*/
public function remove(
$setting
): int|false {
unset($this->data[$setting]);
return $this->write();
}
/**
* Function: current
* Returns a singleton reference to the current configuration.
*/
public static function & current(
): self {
static $instance = null;
$instance = (empty($instance)) ? new self() : $instance ;
return $instance;
}
}