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