1<?php
2
3/**
4 * Observium
5 *
6 *   This file is part of Observium.
7 *
8 * @package    observium
9 * @subpackage web
10 * @copyright  (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
11 *
12 */
13
14if ($_SESSION['userlevel'] < 10)
15{
16  print_error_permission();
17  return;
18}
19
20  register_html_resource('js', 'clipboard.min.js');
21  register_html_resource('script', 'new Clipboard("#clipboard");');
22
23  // Load SQL config into $database_config
24  load_sqlconfig($database_config);
25
26  // cache default and config.php-defined values
27  $defined_config = get_defined_settings();
28  $default_config = get_default_settings();
29
30  echo '<form id="settings" name="settings" method="post" action="" class="form form-inline">' . PHP_EOL;
31
32  //echo '<div class="box box-solid" style="padding:10px;">';
33
34  // Pretty inefficient looping everything if section != all, but meh
35  // This is only done on this page, so there is no performance issue for the rest of Observium
36  foreach ($config_subsections as $section => $subdata)
37  {
38    if (isset($config_sections[$section]['edition']) && $config_sections[$section]['edition'] != OBSERVIUM_EDITION)
39    {
40      // Skip sections not allowed for current Observium edition
41      continue;
42    }
43
44    echo('  <div class="row"> <div class="col-md-12"> <!-- BEGIN SECTION '.$section.' -->' . PHP_EOL);
45    if ($vars['section'] == 'all' || $vars['section'] == $section)
46    {
47      if ($vars['section'] == 'all')
48      {
49        // When printing all, also print the section name
50        echo generate_box_open(array('title' => $config_sections[$section]['text'], 'header-border' => TRUE));
51        echo generate_box_close();
52      }
53
54      foreach ($subdata as $subsection => $vardata)
55      {
56
57        //echo '<div class="box box-solid" style="padding:10px;">';
58        //echo '<h2 style="padding: 0px 5px; color: #555;">'.$subsection.'</h2>';
59
60        echo generate_box_open(array('title' => $subsection, 'header-border' => FALSE,
61                                     'box-style' => 'margin-bottom: 30px; margin-top: 10px;',
62                                     'title-style' => 'padding: 15px 10px; color: #555; font-size: 21px;',
63                                     'title-element' => 'h2'));
64
65        //echo generate_box_open(array('box-style' => 'margin-bottom: 30px; margin-top: 10px;'));
66
67        echo '<table class="table table-striped table-cond">' . PHP_EOL;
68
69        $cols = array(
70          array(NULL, 'class="state-marker"'),
71          //array(NULL, 'style="width: 0px;"'),
72          array('Description', 'style="width: 40%;"'),
73          array(NULL,          'style="width: 50px;"'),
74          'Configuration Value',
75          array(NULL,      'style="width: 75px;"'),
76          //array(NULL,      'style="width: 10px;"'),
77        );
78        //echo(get_table_header($cols));
79
80        foreach ($vardata as $varname => $variable)
81        {
82          if (isset($variable['edition']) && $variable['edition'] != OBSERVIUM_EDITION)
83          {
84            // Skip variables not allowed for current Observium edition
85            continue;
86          }
87          $linetype = '';
88          // Check if this variable is set in SQL
89          if (sql_to_array($varname, $database_config) !== FALSE)
90          {
91            $sqlset = 1;
92            $linetype = 'info';
93            $content = sql_to_array($varname, $database_config, FALSE);
94          } else {
95            $sqlset = 0;
96            //$linetype = "disabled";
97          }
98
99          // Check if this variable is set in the config. If so, lock it
100          $locked = sql_to_array($varname, $defined_config) !== FALSE;
101          $locked = $locked || isset($variable['locked']) && $variable['locked']; // Locked in definition
102          if ($locked)
103          {
104            $offtext  = "Locked";
105            $offtype  = "danger";
106            $linetype = 'error';
107            $content  = sql_to_array($varname, $defined_config, FALSE);
108          } else {
109            $offtext  = "Default";
110            $offtype  = "success";
111          }
112
113          $htmlname = str_replace('|','__',$varname); // JQuery et al don't like the pipes a lot, replace once here in temporary variable
114          $confname = '$config[\'' . implode("']['",explode('|',$varname)) . '\']';
115
116          echo('  <tr class="' . $linetype . ' vertical-align">' . PHP_EOL);
117          echo('    <td class="state-marker"></td>');
118          //echo('    <td style="width: 0px;"></td>');
119          echo('    <td style="width: 40%; padding-left: 15px;"><strong style="color: #0a5f7f; font-size: 1.6rem">' . $variable['name'] . '</strong>');
120          echo('<br /><small>' . escape_html($variable['shortdesc']) . '</small>' . PHP_EOL);
121          echo('      </td>' . PHP_EOL);
122          echo('      <td class="text-nowrap" style="width: 50px">' . PHP_EOL);
123          echo('<div class="pull-right">');
124          if ($locked && !isset($variable['locked']))
125          {
126            echo(generate_tooltip_link(NULL, '<i class="'.$config['icon']['lock'].'"></i>', 'This setting is locked because it has been set in your <strong>config.php</strong> file.'));
127            echo '&nbsp;';
128          }
129          echo(generate_tooltip_link(NULL, '<i id="clipboard" class="'.$config['icon']['question'].'" data-clipboard-text="'.$confname.'"></i>', 'Variable name to use in <strong>config.php</strong>: ' . $confname));
130          echo('      </div>' . PHP_EOL);
131          echo('      </td>'. PHP_EOL);
132          echo('      <td>' . PHP_EOL);
133
134          // Split enum|foo|bar into enum  foo|bar
135          list($vartype, $varparams) = explode('|', $variable['type'], 2);
136          $params = array();
137
138          // If a callback function is defined, use this to fill params.
139          if ($variable['params_call'] && function_exists($variable['params_call']))
140          {
141            $params = call_user_func($variable['params_call']);
142          // Else if the params are defined directly, use these.
143          } else if (is_array($variable['params']))
144          {
145            $params = $variable['params'];
146          }
147          // Else use parameters specified in variable type (e.g. enum|1|2|5|10)
148          else if (!empty($varparams))
149          {
150            foreach (explode('|', $varparams) as $param)
151            {
152              $params[$param] = array('name' => nicecase($param));
153            }
154          }
155
156          if (sql_to_array($varname, $config) === FALSE)
157          {
158            // Variable is not configured, set $content to its default value so the form is pre-filled
159            $content = sql_to_array($varname, $default_config, FALSE);
160          } else {
161            $content = sql_to_array($varname, $config, FALSE); // Get current value
162          }
163          //r($varname); r($content); r($sqlset); r($locked);
164
165          $readonly = !($sqlset || $locked);
166
167          echo('      <div id="' . $htmlname . '_content_div">' . PHP_EOL);
168
169          switch ($vartype)
170          {
171            case 'bool':
172            case 'boolean':
173              echo('      <div>' . PHP_EOL);
174              $item = array('id'       => $htmlname,
175                            'size'     => 'small',
176                            //'on-text'  => 'True',
177                            //'off-text' => 'False',
178                            'readonly' => $readonly,
179                            'disabled' => (bool)$locked,
180                            'value'    => $content);
181              //echo(generate_form_element($item, 'switch'));
182              $item['view'] = 'toggle';
183              $item['size'] = 'huge';
184              //$item['size'] = 'huge';
185              echo(generate_form_element($item, 'toggle'));
186              //echo('        <input data-toggle="switch-bool" type="checkbox" ' . ($content ? 'checked="1" ' : '') . 'id="' . $htmlname . '" name="' . $htmlname . '" ' . ($locked ? 'disabled="1" ' : '').'>' . PHP_EOL);
187              echo('      </div>' . PHP_EOL);
188              break;
189            case 'enum-array':
190              //r($content);
191              if ($variable['value_call'] && function_exists($variable['value_call']))
192              {
193                $values = array();
194                foreach ($content as $value)
195                {
196                  $values[] = call_user_func($variable['value_call'], $value);
197                }
198                $content = $values;
199                unset($values);
200              }
201              //r($content);
202            case 'enum':
203              foreach ($params as $param => $entry)
204              {
205                if (isset($entry['subtext'])) {} // continue
206                else if (isset($entry['allowed']))
207                {
208                  $params[$param]['subtext'] = "Allowed to use " . $config_variable[$entry['allowed']]['name'];
209                }
210                else if (isset($entry['required']))
211                {
212                  $params[$param]['subtext'] = '<strong>REQUIRED to use ' . $config_variable[$entry['required']]['name'] . '</strong>';
213                }
214              }
215              //r($params);
216              $item = array('id'       => $htmlname,
217                            'title'    => 'Any', // only for enum-array
218                            'width'    => '150px',
219                            'readonly' => $readonly,
220                            'disabled' => (bool)$locked,
221                            'onchange' => 'switchDesc(\'' . $htmlname . '\')',
222                            'values'   => $params,
223                            'value'    => $content);
224              echo(generate_form_element($item, ($vartype != 'enum-array' ? 'select' : 'multiselect')));
225              foreach ($params as $param => $entry)
226              {
227                if (isset($entry['desc']))
228                {
229                  echo('      <div id="param_' . $htmlname . '_' .$param. '" style="' . ($content != $param ? ' display: none;' : '') . '">' . PHP_EOL);
230                  echo('        ' . $entry['desc'] . PHP_EOL);
231                  echo('      </div>' . PHP_EOL);
232                }
233              }
234              break;
235            case 'enum-freeinput':
236              $item = array('id'       => $htmlname,
237                            'type'     => 'tags',
238                            //'title'    => 'Any',
239                            'width'    => '150px',
240                            'readonly' => $readonly,
241                            'disabled' => (bool)$locked,
242                            'onchange' => 'switchDesc(\'' . $htmlname . '\')',
243                            'placeholder' => $variable['example'] ? 'Example: ' .$variable['example'] : FALSE,
244                            'values'   => $params,
245                            'value'    => $content);
246              echo(generate_form_element($item));
247              break;
248            case 'password':
249            case 'string':
250            default:
251              $item = array('id'       => $htmlname,
252                            //'width'    => '500px',
253                            'class'    => 'input-xlarge',
254                            'type'     => 'text',
255                            'readonly' => $readonly,
256                            'disabled' => (bool)$locked,
257                            'placeholder' => TRUE,
258                            'value'    => $content);
259              if ($vartype == 'password')
260              {
261                $item['type'] = 'password';
262                $item['show_password'] = 1;
263              } else {
264                $item['placeholder'] = $variable['example'] ? 'Example: ' .$variable['example'] : escape_html($content);
265              }
266              echo(generate_form_element($item));
267              //echo('         <input name="' . $htmlname . '" style="width: 500px" type="text" ' . ($locked ? 'disabled="1" ' : '') . 'value="' . escape_html($content) . '" />' . PHP_EOL);
268              break;
269          }
270
271          echo('        <input type="hidden" name="varset_' . $htmlname . '" />' . PHP_EOL);
272          echo('      </div>' . PHP_EOL);
273          echo('    </td>' . PHP_EOL);
274          echo('    <td>' . PHP_EOL);
275          echo('      <div class="pull-right">' . PHP_EOL);
276          $item = array('id'       => $htmlname . '_custom',
277                        //'size'     => 'small',
278                        //'width'    => 100,
279                        //'on-color' => 'primary',
280                        //'off-color' => $offtype,
281                        //'on-text'  => 'Custom',
282                        //'off-text' => $offtext,
283                        'size'     => 'large',
284                        'view'     => $locked ? 'lock' : 'square', // note this is data-tt-type, but 'type' key reserved for element type
285                        'onchange' => "toggleAttrib('readonly', obj.attr('data-onchange-id'));",
286                        'onchange-id' => $htmlname, // target id for onchange, set attrib: data-onchange-id
287                        //'onchange' => "toggleAttrib('readonly', '" . $htmlname . "')",
288                        //'onchange' => '$(\'#' . $htmlname . '_content_div\').toggle()',
289                        'disabled' => (bool)$locked,
290                        'value'    => $sqlset && !$locked);
291          echo(generate_form_element($item, 'toggle'));
292
293          //$onchange = "toggleAttrib('readonly', '" . $htmlname . "')";
294
295
296          // echo '<input type="checkbox" data-off-label="false" data-on-label="false" data-off-icon-cls="glyphicon-thumbs-down" data-on-icon-cls="glyphicon-thumbs-up" data-group-cls="btn-group-sm" data-on="primary" data-off="' . $offtype . '" data-on-label="Custom" data-off-label="' . $offtext . '" onchange="'.$onchange.'" type="checkbox" ' . ($sqlset && !$locked ? 'checked="1" ' : '') . 'name="' . $htmlname . '_custom"' . ($locked ? ' disabled="1"' : '') . '>';
297
298          //echo '<input type="checkbox" class="tiny-toggle" data-id="' . $htmlname . '" data-tt-type="'.($locked ? 'lock' : 'square').'" data-tt-size="large" onchange="'.$onchange.'" ' . ($sqlset && !$locked ? 'checked="1" ' : '') . 'name="' . $htmlname . '_custom"' . ($locked ? ' disabled="1"' : '') . '>';
299
300
301          echo '      </div>' . PHP_EOL;
302          echo '    </td>' . PHP_EOL;
303          //echo '    <td></td>' . PHP_EOL;
304          echo '  </tr>' . PHP_EOL;
305        }
306
307        echo('  </table>' . PHP_EOL);
308        echo generate_box_close();
309
310      }
311      //echo('  <br />' . PHP_EOL);
312    }
313    echo('  </div> </div> <!-- END SECTION '.$section.' -->' . PHP_EOL);
314  }
315
316?>
317<div class="row">
318  <div class="col-sm-12">
319
320<!--    <div class="box box-solid">
321      <div class="box-content no-padding">
322        <div class="form-actions" style="margin: 0px;"> -->
323  <?php
324
325  // Add CSRF Token
326  $item = array('type'  => 'hidden',
327                'id'    => 'requesttoken',
328                'value' => $_SESSION['requesttoken']);
329  echo(generate_form_element($item) . PHP_EOL);
330
331  $item = array('type'        => 'submit',
332                'id'          => 'submit',
333                'name'        => 'Save Changes',
334                'class'       => 'btn-primary',
335                'right'       => TRUE,
336                'icon'        => 'icon-ok icon-white',
337                'value'       => 'save');
338  echo(generate_form_element($item) . PHP_EOL);
339  ?>
340<!--
341        </div>
342      </div>
343    </div>
344-->
345  </div>
346</div>
347
348</form>
349
350<script>
351  function switchDesc(form_id) {
352    var selected = $('#'+form_id+' option:selected').val();
353    //console.log(selected);
354    $('[id^="param_'+form_id+'_"]').each( function( index, element ) {
355      if ($(this).prop('id') == 'param_'+form_id+'_'+selected) {
356        $(this).css('display', '');
357      } else {
358        $(this).css('display', 'none');
359      }
360      //console.log( $(this).prop('id') );
361    });
362  }
363</script>
364
365<?php
366
367// EOF
368
369