1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2015 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\services\blocks; |
11
|
|
|
|
12
|
|
|
class cfg_handler extends cfg_fields |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\request\request_interface */ |
15
|
|
|
protected $request; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\template\template */ |
18
|
|
|
protected $template; |
19
|
|
|
|
20
|
|
|
/** @var \phpbb\user */ |
21
|
|
|
protected $user; |
22
|
|
|
|
23
|
|
|
/** @var \blitze\sitemaker\services\groups */ |
24
|
|
|
protected $groups; |
25
|
|
|
|
26
|
|
|
/** @var string phpBB root path */ |
27
|
|
|
protected $phpbb_root_path; |
28
|
|
|
|
29
|
|
|
/** @var string phpEx */ |
30
|
|
|
protected $php_ext; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* |
35
|
|
|
* @param \phpbb\request\request_interface $request Request object |
36
|
|
|
* @param \phpbb\template\template $template Template object |
37
|
|
|
* @param \phpbb\user $user User object |
38
|
|
|
* @param \blitze\sitemaker\services\groups $groups Groups object |
39
|
|
|
* @param string $phpbb_root_path phpBB root path |
40
|
|
|
* @param string $php_ext phpEx |
41
|
|
|
*/ |
42
|
51 |
|
public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user, \blitze\sitemaker\services\groups $groups, $phpbb_root_path, $php_ext) |
43
|
|
|
{ |
44
|
51 |
|
parent::__construct($user); |
45
|
|
|
|
46
|
51 |
|
$this->request = $request; |
47
|
51 |
|
$this->template = $template; |
48
|
51 |
|
$this->user = $user; |
49
|
51 |
|
$this->groups = $groups; |
50
|
51 |
|
$this->phpbb_root_path = $phpbb_root_path; |
51
|
51 |
|
$this->php_ext = $php_ext; |
52
|
51 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $block_data |
56
|
|
|
* @param array $default_settings |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
8 |
|
public function get_edit_form(array $block_data, array $default_settings) |
60
|
|
|
{ |
61
|
8 |
|
global $module; |
62
|
|
|
|
63
|
8 |
|
if (!function_exists('build_cfg_template')) |
64
|
8 |
|
{ |
65
|
|
|
include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); // @codeCoverageIgnore |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
// We fake this class as it is needed by the build_cfg_template function |
69
|
8 |
|
$module = new \stdClass(); |
70
|
8 |
|
$module->module = $this; |
71
|
|
|
|
72
|
8 |
|
$this->generate_config_fields($block_data['settings'], $default_settings); |
73
|
|
|
|
74
|
8 |
|
return $this->get_form($block_data); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $default_settings |
79
|
|
|
* @return array|void |
80
|
|
|
*/ |
81
|
6 |
|
public function get_submitted_settings(array $default_settings) |
82
|
|
|
{ |
83
|
|
|
// @codeCoverageIgnoreStart |
84
|
|
|
if (!function_exists('validate_config_vars')) |
85
|
|
|
{ |
86
|
|
|
include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); |
87
|
|
|
} |
88
|
|
|
// @codeCoverageIgnoreEnd |
89
|
|
|
|
90
|
6 |
|
$cfg_array = utf8_normalize_nfc($this->request->variable('config', array('' => ''), true)); |
91
|
6 |
|
$cfg_array = $this->decode_source_html($cfg_array); |
92
|
|
|
|
93
|
6 |
|
$errors = array(); |
94
|
6 |
|
validate_config_vars($default_settings, $cfg_array, $errors); |
95
|
|
|
|
96
|
6 |
|
if (sizeof($errors)) |
97
|
6 |
|
{ |
98
|
1 |
|
return array('errors' => join("\n", $errors)); |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
$this->get_multi_select($cfg_array, $default_settings); |
102
|
|
|
|
103
|
5 |
|
return array_intersect_key($cfg_array, $default_settings); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* As a workaround to prevent mod_security and similar from preventing us |
108
|
|
|
* from posting html/script, we use encodeURI before submitting data. |
109
|
|
|
* This decodes the data before submitting to the database |
110
|
|
|
* |
111
|
|
|
* @param array $cfg_array |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
6 |
|
private function decode_source_html(array $cfg_array) |
115
|
|
|
{ |
116
|
6 |
|
if (isset($cfg_array['source'])) { |
117
|
|
|
$cfg_array['source'] = urldecode($cfg_array['source']); |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
return $cfg_array; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the html form |
125
|
|
|
* |
126
|
|
|
* @param array $block_data |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
8 |
|
private function get_form(array $block_data) |
130
|
|
|
{ |
131
|
8 |
|
$selected_groups = $this->ensure_array($block_data['permission']); |
132
|
|
|
|
133
|
8 |
|
$this->template->assign_vars(array( |
134
|
8 |
|
'S_ACTIVE' => $block_data['status'], |
135
|
8 |
|
'S_TYPE' => $block_data['type'], |
136
|
8 |
|
'S_NO_WRAP' => $block_data['no_wrap'], |
137
|
8 |
|
'S_HIDE_TITLE' => $block_data['hide_title'], |
138
|
8 |
|
'S_BLOCK_CLASS' => trim($block_data['class']), |
139
|
8 |
|
'S_GROUP_OPS' => $this->groups->get_options('all', $selected_groups), |
140
|
8 |
|
)); |
141
|
|
|
|
142
|
8 |
|
$this->template->set_filenames(array( |
143
|
8 |
|
'block_settings' => 'block_settings.html', |
144
|
8 |
|
)); |
145
|
|
|
|
146
|
8 |
|
return $this->template->assign_display('block_settings'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Generate block configuration fields |
151
|
|
|
* |
152
|
|
|
* @param array $db_settings |
153
|
|
|
* @param array $default_settings |
154
|
|
|
*/ |
155
|
8 |
|
private function generate_config_fields(array &$db_settings, array $default_settings) |
156
|
|
|
{ |
157
|
8 |
|
foreach ($default_settings as $field => $vars) |
158
|
|
|
{ |
159
|
7 |
|
if ($this->set_legend($field, $vars) || !is_array($vars)) |
160
|
7 |
|
{ |
161
|
7 |
|
continue; |
162
|
|
|
} |
163
|
|
|
|
164
|
7 |
|
$db_settings[$field] = $this->get_field_value($field, $vars['default'], $db_settings); |
165
|
7 |
|
$content = $this->get_field_template($field, $db_settings, $vars); |
166
|
|
|
|
167
|
7 |
|
$this->template->assign_block_vars('options', array( |
168
|
7 |
|
'KEY' => $field, |
169
|
7 |
|
'TITLE' => $this->user->lang($vars['lang']), |
170
|
7 |
|
'S_EXPLAIN' => $vars['explain'], |
171
|
7 |
|
'TITLE_EXPLAIN' => $vars['lang_explain'], |
172
|
7 |
|
'CONTENT' => $content, |
173
|
7 |
|
)); |
174
|
7 |
|
unset($default_settings[$field]); |
175
|
8 |
|
} |
176
|
8 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the field html |
180
|
|
|
* |
181
|
|
|
* @param string $field |
182
|
|
|
* @param array $db_settings |
183
|
|
|
* @param array $vars |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
7 |
|
private function get_field_template($field, array &$db_settings, array &$vars) |
187
|
|
|
{ |
188
|
7 |
|
$vars['lang_explain'] = $this->explain_field($vars); |
189
|
7 |
|
$vars['append'] = $this->append_field($vars); |
190
|
|
|
|
191
|
7 |
|
$type = explode(':', $vars['type']); |
192
|
7 |
|
$method = 'prep_' . $type[0] . '_field_for_display'; |
193
|
|
|
|
194
|
7 |
|
if (is_callable(array($this, $method))) |
195
|
7 |
|
{ |
196
|
7 |
|
$this->set_params($field, $vars, $db_settings); |
197
|
7 |
|
$this->$method($vars, $type, $field, $db_settings); |
198
|
7 |
|
} |
199
|
|
|
|
200
|
7 |
|
return build_cfg_template($type, $field, $db_settings, $field, $vars); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Set field legend |
205
|
|
|
* |
206
|
|
|
* @param string $field |
207
|
|
|
* @param string|array $vars |
208
|
|
|
* @return boolean |
209
|
|
|
*/ |
210
|
7 |
|
private function set_legend($field, $vars) |
211
|
|
|
{ |
212
|
7 |
|
if (strpos($field, 'legend') !== false) |
213
|
7 |
|
{ |
214
|
7 |
|
$this->template->assign_block_vars('options', array( |
215
|
7 |
|
'S_LEGEND' => $field, |
216
|
7 |
|
'LEGEND' => $this->user->lang($vars) |
217
|
7 |
|
)); |
218
|
|
|
|
219
|
7 |
|
return true; |
220
|
|
|
} |
221
|
|
|
|
222
|
7 |
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get field details |
227
|
|
|
* |
228
|
|
|
* @param array $vars |
229
|
|
|
* @return mixed|string |
230
|
|
|
*/ |
231
|
7 |
|
private function explain_field(array $vars) |
232
|
|
|
{ |
233
|
7 |
|
$l_explain = ''; |
234
|
7 |
|
if (!empty($vars['explain'])) |
235
|
7 |
|
{ |
236
|
2 |
|
$l_explain = (isset($vars['lang_explain'])) ? $this->user->lang($vars['lang_explain']) : $this->user->lang($vars['lang'] . '_EXPLAIN'); |
237
|
2 |
|
} |
238
|
|
|
|
239
|
7 |
|
return $l_explain; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Add text after field |
244
|
|
|
* |
245
|
|
|
* @param array $vars |
246
|
|
|
* @return mixed|string |
247
|
|
|
*/ |
248
|
7 |
|
private function append_field(array $vars) |
249
|
|
|
{ |
250
|
7 |
|
$append = ''; |
251
|
7 |
|
if (!empty($vars['append'])) |
252
|
7 |
|
{ |
253
|
1 |
|
$append = $this->user->lang($vars['append']); |
254
|
1 |
|
} |
255
|
|
|
|
256
|
7 |
|
return $append; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Set field parameters |
261
|
|
|
* |
262
|
|
|
* @param string $field |
263
|
|
|
* @param array $vars |
264
|
|
|
* @param array $settings |
265
|
|
|
*/ |
266
|
7 |
|
private function set_params($field, array &$vars, array $settings) |
267
|
|
|
{ |
268
|
7 |
|
if (isset($vars['options'])) |
269
|
7 |
|
{ |
270
|
4 |
|
$vars['params'][] = $vars['options']; |
271
|
4 |
|
$vars['params'][] = $settings[$field]; |
272
|
4 |
|
} |
273
|
7 |
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Get field value |
277
|
|
|
* |
278
|
|
|
* @param string $field |
279
|
|
|
* @param mixed $default |
280
|
|
|
* @param array $db_settings |
281
|
|
|
* @return mixed |
282
|
|
|
*/ |
283
|
7 |
|
private function get_field_value($field, $default, array $db_settings) |
284
|
|
|
{ |
285
|
7 |
|
return (isset($db_settings[$field])) ? $db_settings[$field] : $default; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param array $vars |
290
|
|
|
*/ |
291
|
1 |
|
private function prep_select_field_for_display(array &$vars) |
292
|
|
|
{ |
293
|
1 |
|
$this->add_lang_vars($vars['params'][0]); |
294
|
|
|
|
295
|
1 |
|
$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : 'build_select'; |
296
|
1 |
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param array $vars |
300
|
|
|
* @param array $type |
301
|
|
|
* @param string $field |
302
|
|
|
*/ |
303
|
2 |
|
private function prep_checkbox_field_for_display(array &$vars, array &$type, $field) |
304
|
|
|
{ |
305
|
2 |
|
$this->add_lang_vars($vars['params'][0]); |
306
|
|
|
|
307
|
2 |
|
$vars['method'] = 'build_checkbox'; |
308
|
2 |
|
$vars['params'][] = $field; |
309
|
2 |
|
$type[0] = 'custom'; |
310
|
2 |
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param array $vars |
314
|
|
|
* @param array $type |
315
|
|
|
* @param string $field |
316
|
|
|
*/ |
317
|
2 |
|
private function prep_radio_field_for_display(array &$vars, array &$type, $field) |
318
|
1 |
|
{ |
319
|
2 |
|
if (!isset($type[1])) |
320
|
2 |
|
{ |
321
|
1 |
|
$this->add_lang_vars($vars['params'][0]); |
322
|
|
|
|
323
|
1 |
|
$vars['method'] = 'build_radio'; |
324
|
1 |
|
$vars['params'][] = $field; |
325
|
1 |
|
$type[0] = 'custom'; |
326
|
1 |
|
} |
327
|
2 |
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param array $vars |
331
|
|
|
* @param array $type |
332
|
|
|
* @param string $field |
333
|
|
|
*/ |
334
|
1 |
|
private function prep_multi_select_field_for_display(array &$vars, array &$type, $field) |
335
|
|
|
{ |
336
|
1 |
|
$this->prep_checkbox_field_for_display($vars, $type, $field); |
337
|
|
|
|
338
|
1 |
|
$vars['method'] ='build_multi_select'; |
339
|
1 |
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param array $vars |
343
|
|
|
* @param array $type |
344
|
|
|
*/ |
345
|
1 |
|
private function prep_hidden_field_for_display(array &$vars, array &$type) |
346
|
|
|
{ |
347
|
1 |
|
$vars['method'] = 'build_hidden'; |
348
|
1 |
|
$vars['explain'] = ''; |
349
|
1 |
|
$vars['lang'] = ''; |
350
|
1 |
|
$type[0] = 'custom'; |
351
|
1 |
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param array $vars |
355
|
|
|
* @param array $type |
356
|
|
|
*/ |
357
|
1 |
|
private function prep_custom_field_for_display(array &$vars, array &$type) |
358
|
|
|
{ |
359
|
1 |
|
$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : ''; |
360
|
1 |
|
$type[0] = 'custom'; |
361
|
1 |
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* this looks bad but its the only way without modifying phpbb code |
365
|
|
|
* this is for select items that do not need to be translated |
366
|
|
|
* @param array $options |
367
|
|
|
*/ |
368
|
4 |
|
private function add_lang_vars(array $options) |
369
|
|
|
{ |
370
|
4 |
|
foreach ($options as $title) |
371
|
|
|
{ |
372
|
4 |
|
if (!isset($this->user->lang[$title])) |
373
|
4 |
|
{ |
374
|
4 |
|
$this->user->lang[$title] = $title; |
375
|
4 |
|
} |
376
|
4 |
|
} |
377
|
4 |
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @param array $cfg_array |
381
|
|
|
* @param array $df_settings |
382
|
|
|
*/ |
383
|
5 |
|
private function get_multi_select(array &$cfg_array, array $df_settings) |
384
|
|
|
{ |
385
|
5 |
|
$multi_select = utf8_normalize_nfc($this->request->variable('config', array('' => array('' => '')), true)); |
386
|
5 |
|
$multi_select = array_filter($multi_select); |
387
|
|
|
|
388
|
5 |
|
foreach ($multi_select as $field => $settings) |
389
|
|
|
{ |
390
|
1 |
|
$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default']; |
391
|
5 |
|
} |
392
|
5 |
|
} |
393
|
|
|
} |
394
|
|
|
|