1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules { |
4
|
|
|
|
5
|
|
|
use Utils\Lister, Utils\Validate, Explorer, Geo\Timezone, Request; |
6
|
|
|
|
7
|
|
|
abstract class Settings { |
8
|
|
|
|
9
|
|
|
private static $loaded = false; |
10
|
|
|
|
11
|
|
|
# Params |
12
|
|
|
|
13
|
|
|
private static $settings = [ |
14
|
|
|
|
15
|
|
|
'admin_language' => CONFIG_ADMIN_LANGUAGE_DEFAULT, |
16
|
|
|
'admin_template' => CONFIG_ADMIN_TEMPLATE_DEFAULT, |
17
|
|
|
|
18
|
|
|
'site_language' => CONFIG_SITE_LANGUAGE_DEFAULT, |
19
|
|
|
'site_template' => CONFIG_SITE_TEMPLATE_DEFAULT, |
20
|
|
|
|
21
|
|
|
'site_title' => CONFIG_SITE_TITLE_DEFAULT, |
22
|
|
|
'site_slogan' => CONFIG_SITE_SLOGAN_DEFAULT, |
23
|
|
|
|
24
|
|
|
'site_status' => STATUS_ONLINE, |
25
|
|
|
|
26
|
|
|
'site_description' => '', |
27
|
|
|
'site_keywords' => '', |
28
|
|
|
|
29
|
|
|
'system_url' => '', |
30
|
|
|
'system_email' => '', |
31
|
|
|
'system_timezone' => CONFIG_SYSTEM_TIMEZONE_DEFAULT, |
32
|
|
|
|
33
|
|
|
'users_registration' => false |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
# Autoloader |
37
|
|
|
|
38
|
|
|
public static function __autoload() { |
39
|
|
|
|
40
|
|
|
if (self::$loaded || empty($host = getenv('HTTP_HOST'))) return; |
41
|
|
|
|
42
|
|
|
self::$settings['system_url'] = ((Request::isSecure() ? 'https://' : 'http://') . $host); |
43
|
|
|
|
44
|
|
|
self::$settings['system_email'] = ('admin@' . $host); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
# Init settings |
48
|
|
|
|
49
|
|
|
public static function init() { |
50
|
|
|
|
51
|
|
|
$settings_file = (DIR_SYSTEM_DATA . 'Settings.json'); |
52
|
|
|
|
53
|
|
|
self::$loaded = (false !== ($settings = Explorer::json($settings_file))); |
54
|
|
|
|
55
|
|
|
if (self::$loaded) foreach ($settings as $name => $value) self::set($name, $value); |
56
|
|
|
|
57
|
|
|
# ------------------------ |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
# Save settings |
63
|
|
|
|
64
|
|
|
public static function save() { |
65
|
|
|
|
66
|
|
|
$settings_file = (DIR_SYSTEM_DATA . 'Settings.json'); |
67
|
|
|
|
68
|
|
|
if (false === ($settings = json_encode(self::$settings, JSON_PRETTY_PRINT))) return false; |
69
|
|
|
|
70
|
|
|
if (false === Explorer::save($settings_file, $settings, true)) return false; |
71
|
|
|
|
72
|
|
|
# ------------------------ |
73
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
# Check if settings file is loaded |
78
|
|
|
|
79
|
|
|
public static function loaded() { |
80
|
|
|
|
81
|
|
|
return self::$loaded; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
# Set value |
85
|
|
|
|
86
|
|
|
public static function set(string $name, $value) { |
87
|
|
|
|
88
|
|
|
if (!isset(self::$settings[$name])) return false; |
89
|
|
|
|
90
|
|
|
# Validate language |
91
|
|
|
|
92
|
|
|
if (($name === 'admin_language') || ($name === 'site_language')) { |
93
|
|
|
|
94
|
|
|
if (false === ($value = Extend\Languages::validate($value))) return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
# Validate template |
98
|
|
|
|
99
|
|
|
else if (($name === 'admin_template') || ($name === 'site_template')) { |
100
|
|
|
|
101
|
|
|
if (false === ($value = Extend\Templates::validate($value))) return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
# Validate site title |
105
|
|
|
|
106
|
|
|
else if ($name === 'site_title') { |
107
|
|
|
|
108
|
|
|
if ('' === ($value = (function(string $value) { return $value; })($value))) return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
# Validate site status |
112
|
|
|
|
113
|
|
|
else if ($name === 'site_status') { |
114
|
|
|
|
115
|
|
|
if (false === ($value = Lister\Status::validate($value))) return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
# Validate site slogan/description/keywords |
119
|
|
|
|
120
|
|
|
else if (($name === 'site_slogan') || ($name === 'site_description') || ($name === 'site_keywords')) { |
121
|
|
|
|
122
|
|
|
$value = (function(string $value) { return $value; })($value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
# Validate system url |
126
|
|
|
|
127
|
|
|
else if ($name === 'system_url') { |
128
|
|
|
|
129
|
|
|
if (false === ($value = Validate::url($value))) return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
# Validate system email |
133
|
|
|
|
134
|
|
|
else if ($name === 'system_email') { |
135
|
|
|
|
136
|
|
|
if (false === ($value = Validate::email($value))) return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
# Validate system timezone |
140
|
|
|
|
141
|
|
|
else if ($name === 'system_timezone') { |
142
|
|
|
|
143
|
|
|
if (false === ($value = Timezone::validate($value))) return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
# Validate users registration |
147
|
|
|
|
148
|
|
|
else if ($name === 'users_registration') { |
149
|
|
|
|
150
|
|
|
$value = (function(bool $value) { return $value; })($value); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
# Set value |
154
|
|
|
|
155
|
|
|
self::$settings[$name] = $value; |
156
|
|
|
|
157
|
|
|
# ------------------------ |
158
|
|
|
|
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
# Get value |
163
|
|
|
|
164
|
|
|
public static function get(string $name = null) { |
165
|
|
|
|
166
|
|
|
if (null === $name) return self::$settings; |
167
|
|
|
|
168
|
|
|
return (isset(self::$settings[$name]) ? self::$settings[$name] : null); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|