/
ArrayFileEdit.php
380 lines (360 loc) · 11.9 KB
/
ArrayFileEdit.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* The abstract base class for editing of config files.
*
* @category CMSimple_XH
* @package XH
* @author Peter Harteg <peter@harteg.dk>
* @author The CMSimple_XH developers <devs@cmsimple-xh.org>
* @copyright 1999-2009 Peter Harteg
* @copyright 2009-2017 The CMSimple_XH developers <http://cmsimple-xh.org/?The_Team>
* @license http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
* @link http://cmsimple-xh.org/
*/
namespace XH;
/**
* The abstract base class for editing of config files.
*
* @category CMSimple_XH
* @package XH
* @author The CMSimple_XH developers <devs@cmsimple-xh.org>
* @license http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
* @link http://cmsimple-xh.org/
* @since 1.6
*/
abstract class ArrayFileEdit extends FileEdit
{
/**
* The configuration.
*
* @var array
*/
protected $cfg = null;
/**
* A dictionary which maps from config and languages keys
* to their localization.
*
* @var array
*/
protected $lang = null;
/**
* The path of the meta language file,
* which contains localization of config and language keys.
*
* @var string
*/
protected $metaLangFile;
/**
* Construct an instance
*/
public function __construct()
{
if (is_readable($this->metaLangFile)) {
$mtx = array();
include $this->metaLangFile;
$this->lang = $mtx;
} else {
$this->lang = array();
}
}
/**
* Saves the file and returns whether that succeeded.
* Invalidates the cached file, if OPcache is enabled.
*
* @return bool
*/
protected function save()
{
$ok = parent::save();
if (function_exists('opcache_invalidate')) {
opcache_invalidate($this->filename);
}
return $ok;
}
/**
* Returns the localization of the given config or language key.
*
* @param string $key A config or language key.
*
* @return string
*/
protected function translate($key)
{
$altKey = str_replace(' ', '_', $key);
if (isset($this->lang[$key])) {
$result = $this->lang[$key];
} elseif (isset($this->lang[$altKey])) {
$result = $this->lang[$altKey];
} else {
$result = utf8_ucfirst($key);
}
return $result;
}
/**
* Returns a key split to category and rest.
*
* @param string $key The original key.
*
* @return array
*/
protected function splitKey($key)
{
if (strpos($key, '_') !== false) {
list($first, $rest) = explode('_', $key, 2);
} else {
$first = $key;
$rest = '';
}
return array($first, $rest);
}
/**
* Returns whether all options are hidden.
*
* @param array $options The list of options.
* @param bool $advanced Whether normal or advanced fields are to be checked.
*
* @return bool
*/
protected function hasVisibleFields(array $options, $advanced)
{
foreach ($options as $opt) {
if ($opt['type'] != 'hidden' && $opt['type'] != 'random'
&& $advanced == $opt['isAdvanced']
) {
return true;
}
}
return false;
}
/**
* Returns a form field.
*
* @param string $cat The category.
* @param string $name The name.
* @param array $opt The field options.
*
* @return string HTML
*
* @global array The localization of the core.
*/
protected function formField($cat, $name, array $opt)
{
global $tx;
$iname = XH_FORM_NAMESPACE . $cat . '_' . $name;
switch ($opt['type']) {
case 'text':
$class = 'xh_setting';
if (utf8_strlen($opt['val']) < 50) {
$class .= ' xh_setting_short';
}
return '<textarea name="' . $iname . '" rows="1" cols="50"'
. ' class="' . $class . '">'
. XH_hsc($opt['val'])
. '</textarea>';
case 'bool':
return '<input type="checkbox" name="' . $iname . '"'
. ($opt['val'] ? ' checked="checked"' : '') . '>';
case 'enum':
$o = '<select name="' . $iname . '">';
foreach ($opt['vals'] as $val) {
$sel = ($val == $opt['val']) ? ' selected="selected"' : '';
$label = ($val == '')
? ' label="' . $tx['label']['empty'] . '"'
: '';
$o .= '<option' . $sel . $label . '>' . XH_hsc($val) . '</option>';
}
$o .= '</select>';
return $o;
case 'xenum':
$o = '<input type="text" name="' . $iname . '" value="'
. XH_hsc($opt['val']) . '" class="xh_setting" list="'
. $iname . '_DATA">';
$o .= '<datalist id="' . $iname . '_DATA">';
foreach ($opt['vals'] as $val) {
$label = ($val == '')
? ' label="' . $tx['label']['empty'] . '"'
: '';
$o .= '<option' . $label . ' value="' . XH_hsc($val) . '">';
}
$o .= '</datalist>';
return $o;
case 'hidden':
case 'random':
return '<input type="hidden" name="' . $iname . '" value="'
. XH_hsc($opt['val']) . '">';
default:
return '<input type="text" name="' . $iname . '" value="'
. XH_hsc($opt['val'])
. '" class="xh_setting">';
}
}
/**
* Returns the form to edit the file contents.
*
* @return string HTML
*
* @global string The script name.
* @global array The localization of the core.
* @global string The title of the current page.
* @global object The CSRF protection object.
*/
public function form()
{
global $sn, $tx, $title, $_XH_csrfProtection;
$title = $this->caption;
$action = isset($this->plugin) ? $sn . '?&' . $this->plugin : $sn;
$value = utf8_ucfirst($tx['action']['save']);
$button = '<input type="submit" class="submit" value="' . $value . '">';
if (isset($_GET['xh_success'])) {
$filetype = utf8_ucfirst($tx['filetype'][stsl($_GET['xh_success'])]);
$message = XH_message('success', $tx['message']['saved'], $filetype);
} else {
$message = '';
}
$o = '<h1>' . $this->caption . '</h1>' . $message
. '<form id="xh_config_form" action="' . $action
. '" method="post" accept-charset="UTF-8">'
. $button
. $this->renderFormFields(false)
. '<div id="xh_config_form_advanced">'
. $this->renderFormFields(true) . '</div>';
foreach ($this->params as $param => $value) {
$o .= '<input type="hidden" name="' . $param . '" value="'
. $value . '">';
}
$o .= $_XH_csrfProtection->tokenInput();
$o .= $button . '</form>';
return $o;
}
/**
* Renders the form fields grouped by category.
*
* @param bool $advanced Whether to render the normal or the advanced fields.
*
* @return string HTML
*/
protected function renderFormFields($advanced)
{
$o = '';
foreach ($this->cfg as $category => $options) {
$hasVisibleFields = $this->hasVisibleFields($options, $advanced);
if ($hasVisibleFields) {
$o .= '<fieldset><legend>' . $this->translate($category)
. '</legend>';
}
foreach ($options as $name => $opt) {
if ($opt['isAdvanced'] != $advanced) {
continue;
}
$info = isset($opt['hint']) ? XH_helpIcon($opt['hint']) . ' ' : '';
if ($opt['type'] == 'hidden' || $opt['type'] == 'random') {
$o .= $this->formField($category, $name, $opt);
} else {
$displayName = $name != ''
? str_replace('_', ' ', $name)
: $category;
$o .= '<div class="xh_label">'
. $info . '<span class="xh_label">'
. $this->translate($displayName) . '</span>';
if ($category == 'meta' && $name == 'description') {
$o .= ' <span id="xh_description_length">['
. utf8_strlen($opt['val']) . ']</span>';
}
$o .= '</div>'
. '<div class="xh_field">'
. $this->formField($category, $name, $opt) . '</div>'
. '<br>';
}
}
if ($hasVisibleFields) {
$o .= '</fieldset>';
}
}
return $o;
}
/**
* Handles the form submission.
*
* Triggers a redirect, if the submission was valid
* and the file could be successfully saved.
* Otherwise writes an error message to $e, and returns the edit form.
*
* @return string HTML
*
* @global string Error messages.
* @global object The CSRF protection object.
*/
public function submit()
{
global $e, $_XH_csrfProtection;
$_XH_csrfProtection->check();
$errors = array();
foreach ($this->cfg as $cat => $opts) {
foreach ($opts as $name => $opt) {
$iname = XH_FORM_NAMESPACE . $cat . '_' . $name;
$val = isset($_POST[$iname]) ? stsl($_POST[$iname]) : '';
if ($opt['type'] == 'bool') {
$val = isset($_POST[$iname]) ? 'true' : '';
} elseif ($opt['type'] == 'random') {
$val = bin2hex(random_bytes(12));
}
$this->cfg[$cat][$name]['val'] = $val;
}
}
if (!empty($errors)) {
$e .= implode('', $errors);
return $this->form();
} elseif ($this->save()) {
header('Location: ' . CMSIMPLE_URL . $this->redir, true, 303);
XH_exit();
} else {
e('cntsave', 'file', $this->filename);
return $this->form();
}
}
/**
* Returns an option array.
*
* @param string $mcf The meta config of the option.
* @param mixed $val The current value of the option.
* @param string $hint A hint for the option usage.
*
* @return array
*/
protected function option($mcf, $val, $hint)
{
$type = isset($mcf) ? $mcf : 'string';
list($typeTag) = explode(':', $type);
if (strpos($typeTag, '+') === 0) {
$type = substr($type, 1);
$typeTag = substr($typeTag, 1);
$isAdvanced = true;
} else {
$isAdvanced = false;
}
switch ($typeTag) {
case 'enum':
case 'xenum':
$vals = explode(',', substr($type, strlen($typeTag) + 1));
$type = $typeTag;
break;
case 'function':
case 'xfunction':
$func = substr($type, strlen($typeTag) + 1);
if (function_exists($func)) {
$vals = $func();
} else {
$vals = array();
}
$type = ($typeTag == 'function') ? 'enum' : 'xenum';
break;
default:
$vals = null;
}
$co = compact('val', 'type', 'vals', 'isAdvanced');
if (isset($hint)) {
$co['hint'] = $hint;
}
return $co;
}
}