CMSimple_XH 開発者ドキュメント
ArrayFileEdit.php
1 <?php
2 
16 namespace XH;
17 
28 abstract class ArrayFileEdit extends FileEdit
29 {
30 
36  protected $cfg = null;
37 
44  protected $lang = null;
45 
52  protected $metaLangFile;
53 
57  public function __construct()
58  {
59  if (is_readable($this->metaLangFile)) {
60  $mtx = array();
61  include $this->metaLangFile;
62  $this->lang = $mtx;
63  } else {
64  $this->lang = array();
65  }
66  }
67 
74  protected function save()
75  {
76  $ok = parent::save();
77  if (function_exists('opcache_invalidate')) {
78  opcache_invalidate($this->filename);
79  }
80  return $ok;
81  }
82 
90  protected function translate($key)
91  {
92  $altKey = str_replace(' ', '_', $key);
93  if (isset($this->lang[$key])) {
94  $result = $this->lang[$key];
95  } elseif (isset($this->lang[$altKey])) {
96  $result = $this->lang[$altKey];
97  } else {
98  $result = utf8_ucfirst($key);
99  }
100  return $result;
101  }
102 
110  protected function splitKey($key)
111  {
112  if (strpos($key, '_') !== false) {
113  list($first, $rest) = explode('_', $key, 2);
114  } else {
115  $first = $key;
116  $rest = '';
117  }
118  return array($first, $rest);
119  }
120 
129  protected function hasVisibleFields(array $options, $advanced)
130  {
131  foreach ($options as $opt) {
132  if ($opt['type'] != 'hidden' && $opt['type'] != 'random'
133  && $advanced == $opt['isAdvanced']
134  ) {
135  return true;
136  }
137  }
138  return false;
139  }
140 
152  protected function formField($cat, $name, array $opt)
153  {
154  global $tx;
155 
156  $iname = XH_FORM_NAMESPACE . $cat . '_' . $name;
157  switch ($opt['type']) {
158  case 'text':
159  $class = 'xh_setting';
160  if (utf8_strlen($opt['val']) < 50) {
161  $class .= ' xh_setting_short';
162  }
163  return '<textarea name="' . $iname . '" rows="1" cols="50"'
164  . ' class="' . $class . '">'
165  . XH_hsc($opt['val'])
166  . '</textarea>';
167  case 'bool':
168  return '<input type="checkbox" name="' . $iname . '"'
169  . ($opt['val'] ? ' checked="checked"' : '') . '>';
170  case 'enum':
171  $o = '<select name="' . $iname . '">';
172  foreach ($opt['vals'] as $val) {
173  $sel = ($val == $opt['val']) ? ' selected="selected"' : '';
174  $label = ($val == '')
175  ? ' label="' . $tx['label']['empty'] . '"'
176  : '';
177  $o .= '<option' . $sel . $label . '>' . XH_hsc($val) . '</option>';
178  }
179  $o .= '</select>';
180  return $o;
181  case 'xenum':
182  $o = '<input type="text" name="' . $iname . '" value="'
183  . XH_hsc($opt['val']) . '" class="xh_setting" list="'
184  . $iname . '_DATA">';
185  $o .= '<datalist id="' . $iname . '_DATA">';
186  foreach ($opt['vals'] as $val) {
187  $label = ($val == '')
188  ? ' label="' . $tx['label']['empty'] . '"'
189  : '';
190  $o .= '<option' . $label . ' value="' . XH_hsc($val) . '">';
191  }
192  $o .= '</datalist>';
193  return $o;
194  case 'hidden':
195  case 'random':
196  return '<input type="hidden" name="' . $iname . '" value="'
197  . XH_hsc($opt['val']) . '">';
198  default:
199  return '<input type="text" name="' . $iname . '" value="'
200  . XH_hsc($opt['val'])
201  . '" class="xh_setting">';
202  }
203  }
204 
215  public function form()
216  {
217  global $sn, $tx, $title, $_XH_csrfProtection;
218 
219  $title = $this->caption;
220  $action = isset($this->plugin) ? $sn . '?&amp;' . $this->plugin : $sn;
221  $value = utf8_ucfirst($tx['action']['save']);
222  $button = '<input type="submit" class="submit" value="' . $value . '">';
223  if (isset($_GET['xh_success'])) {
224  $filetype = utf8_ucfirst($tx['filetype'][stsl($_GET['xh_success'])]);
225  $message = XH_message('success', $tx['message']['saved'], $filetype);
226  } else {
227  $message = '';
228  }
229  $o = '<h1>' . $this->caption . '</h1>' . $message
230  . '<form id="xh_config_form" action="' . $action
231  . '" method="post" accept-charset="UTF-8">'
232  . $button
233  . $this->renderFormFields(false)
234  . '<div id="xh_config_form_advanced">'
235  . $this->renderFormFields(true) . '</div>';
236  foreach ($this->params as $param => $value) {
237  $o .= '<input type="hidden" name="' . $param . '" value="'
238  . $value . '">';
239  }
240  $o .= $_XH_csrfProtection->tokenInput();
241  $o .= $button . '</form>';
242 
243  return $o;
244  }
245 
253  protected function renderFormFields($advanced)
254  {
255  $o = '';
256  foreach ($this->cfg as $category => $options) {
257  $hasVisibleFields = $this->hasVisibleFields($options, $advanced);
258  if ($hasVisibleFields) {
259  $o .= '<fieldset><legend>' . $this->translate($category)
260  . '</legend>';
261  }
262  foreach ($options as $name => $opt) {
263  if ($opt['isAdvanced'] != $advanced) {
264  continue;
265  }
266  $info = isset($opt['hint']) ? XH_helpIcon($opt['hint']) . ' ' : '';
267  if ($opt['type'] == 'hidden' || $opt['type'] == 'random') {
268  $o .= $this->formField($category, $name, $opt);
269  } else {
270  $displayName = $name != ''
271  ? str_replace('_', ' ', $name)
272  : $category;
273  $o .= '<div class="xh_label">'
274  . $info . '<span class="xh_label">'
275  . $this->translate($displayName) . '</span>';
276  if ($category == 'meta' && $name == 'description') {
277  $o .= ' <span id="xh_description_length">['
278  . utf8_strlen($opt['val']) . ']</span>';
279  }
280  $o .= '</div>'
281  . '<div class="xh_field">'
282  . $this->formField($category, $name, $opt) . '</div>'
283  . '<br>';
284  }
285  }
286  if ($hasVisibleFields) {
287  $o .= '</fieldset>';
288  }
289  }
290  return $o;
291  }
292 
305  public function submit()
306  {
307  global $e, $_XH_csrfProtection;
308 
309  $_XH_csrfProtection->check();
310  $errors = array();
311  foreach ($this->cfg as $cat => $opts) {
312  foreach ($opts as $name => $opt) {
313  $iname = XH_FORM_NAMESPACE . $cat . '_' . $name;
314  $val = isset($_POST[$iname]) ? stsl($_POST[$iname]) : '';
315  if ($opt['type'] == 'bool') {
316  $val = isset($_POST[$iname]) ? 'true' : '';
317  } elseif ($opt['type'] == 'random') {
318  $val = bin2hex(random_bytes(12));
319  }
320  $this->cfg[$cat][$name]['val'] = $val;
321  }
322  }
323  if (!empty($errors)) {
324  $e .= implode('', $errors);
325  return $this->form();
326  } elseif ($this->save()) {
327  header('Location: ' . CMSIMPLE_URL . $this->redir, true, 303);
328  XH_exit();
329  } else {
330  e('cntsave', 'file', $this->filename);
331  return $this->form();
332  }
333  }
334 
344  protected function option($mcf, $val, $hint)
345  {
346  $type = isset($mcf) ? $mcf : 'string';
347  list($typeTag) = explode(':', $type);
348  if (strpos($typeTag, '+') === 0) {
349  $type = substr($type, 1);
350  $typeTag = substr($typeTag, 1);
351  $isAdvanced = true;
352  } else {
353  $isAdvanced = false;
354  }
355  switch ($typeTag) {
356  case 'enum':
357  case 'xenum':
358  $vals = explode(',', substr($type, strlen($typeTag) + 1));
359  $type = $typeTag;
360  break;
361  case 'function':
362  case 'xfunction':
363  $func = substr($type, strlen($typeTag) + 1);
364  if (function_exists($func)) {
365  $vals = $func();
366  } else {
367  $vals = array();
368  }
369  $type = ($typeTag == 'function') ? 'enum' : 'xenum';
370  break;
371  default:
372  $vals = null;
373  }
374  $co = compact('val', 'type', 'vals', 'isAdvanced');
375  if (isset($hint)) {
376  $co['hint'] = $hint;
377  }
378  return $co;
379  }
380 }
$action
Definition: cms.php:460
option($mcf, $val, $hint)
$e
Definition: cms.php:127
const CMSIMPLE_URL
Definition: cms.php:761
hasVisibleFields(array $options, $advanced)
utf8_ucfirst($string)
Definition: utf8.php:118
$title
Definition: cms.php:100
if($cf['site']['compat']) $errors
Definition: cms.php:309
XH_exit($status=0)
Definition: functions.php:2401
utf8_strlen($string)
Definition: utf8.php:28
$o
Definition: cms.php:113
renderFormFields($advanced)
e($et, $ft, $fn)
Definition: functions.php:655
const XH_FORM_NAMESPACE
Definition: cms.php:1035
$sn
Definition: cms.php:434
foreach(XH_plugins() as $plugin) $_XH_csrfProtection
Definition: cms.php:879
$tx
Definition: cms.php:363
stsl($t)
Definition: functions.php:606
XH_hsc($string)
Definition: functions.php:2204
XH_helpIcon($tooltip)
Definition: functions.php:1927
XH_message($type, $message)
Definition: functions.php:1806
formField($cat, $name, array $opt)