Subversion Repositories oidplus

Rev

Rev 1199 | Rev 1206 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusPageAdminSystemConfig extends OIDplusPagePluginAdmin {
  27.  
  28.         /**
  29.          * @param string $actionID
  30.          * @param array $params
  31.          * @return array
  32.          * @throws OIDplusException
  33.          */
  34.         public function action(string $actionID, array $params): array {
  35.                 if ($actionID == 'config_update') {
  36.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  37.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')));
  38.                         }
  39.  
  40.                         _CheckParamExists($params, 'name');
  41.                         _CheckParamExists($params, 'value');
  42.  
  43.                         $name = $params['name'];
  44.                         $value = $params['value'];
  45.  
  46.                         $res = OIDplus::db()->query("select protected, visible from ###config where name = ?", array($name));
  47.                         if (!$res->any()) {
  48.                                 throw new OIDplusException(_L('Setting does not exist'));
  49.                         }
  50.                         $row = $res->fetch_array();
  51.                         if (($row['protected'] == 1) || ($row['visible'] == 0)) {
  52.                                 throw new OIDplusException(_L("Setting %1 is read-only",$name));
  53.                         }
  54.  
  55.                         $old_value = OIDplus::config()->getValue($name, '');
  56.                         OIDplus::config()->setValue($name, $value);
  57.                         OIDplus::logger()->log("[OK]A?", "Changed system config setting '%1' from '%2' to '%3'", $name, $old_value, $value);
  58.  
  59.                         return array("status" => 0);
  60.                 } else {
  61.                         return parent::action($actionID, $params);
  62.                 }
  63.         }
  64.  
  65.         /**
  66.          * @param bool $html
  67.          * @return void
  68.          */
  69.         public function init(bool $html=true) {
  70.                 // Nothing
  71.         }
  72.  
  73.         /**
  74.          * @param string $id
  75.          * @param array $out
  76.          * @param bool $handled
  77.          * @return void
  78.          * @throws OIDplusException
  79.          */
  80.         public function gui(string $id, array &$out, bool &$handled) {
  81.                 if (explode('$',$id)[0] == 'oidplus:edit_config') {
  82.                         $handled = true;
  83.                         $out['title'] = _L('System configuration');
  84.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  85.  
  86.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  87.                                 $out['icon'] = 'img/error.png';
  88.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  89.                                 return;
  90.                         }
  91.  
  92.                         $output  = '<div class="container box"><div id="suboid_table" class="table-responsive">';
  93.                         $output .= '<table class="table table-bordered table-striped">';
  94.                         $output .= '<thead>';
  95.                         $output .= '    <tr>';
  96.                         $output .= '         <th>'._L('Setting').'</th>';
  97.                         $output .= '         <th>'._L('Description').'</th>';
  98.                         $output .= '         <th>'._L('Value').'</th>';
  99.                         $output .= '         <th>'._L('Update').'</th>';
  100.                         $output .= '    </tr>';
  101.                         $output .= '</thead>';
  102.                         $output .= '<tbody>';
  103.  
  104.                         OIDplus::config(); // <-- make sure that the config table is loaded/filled correctly before we do a select
  105.  
  106.                         $result = OIDplus::db()->query("select * from ###config where visible = ? order by name", array(true));
  107.                         while ($row = $result->fetch_object()) {
  108.                                 $output .= '<tr>';
  109.                                 $output .= '     <td>'.htmlentities($row->name).'</td>';
  110.                                 $output .= '     <td>'.htmlentities($row->description).'</td>';
  111.                                 if ($row->protected == 1) {
  112.                                         $desc = $row->value;
  113.                                         if (strlen($desc) > 100) $desc = substr($desc, 0, 100) . '...';
  114.                                         $output .= '     <td style="word-break: break-all;">'.htmlentities($desc).'</td>';
  115.                                         $output .= '     <td>&nbsp;</td>';
  116.                                 } else {
  117.                                         $output .= '     <td><input type="text" id="config_'.$row->name.'" value="'.htmlentities($row->value).'"></td>';
  118.                                         $output .= '     <td><button type="button" name="config_update_'.$row->name.'" id="config_update_'.$row->name.'" class="btn btn-success btn-xs update" onclick="OIDplusPageAdminSystemConfig.crudActionConfigUpdate('.js_escape($row->name).')">'._L('Update').'</button></td>';
  119.                                 }
  120.                                 $output .= '</tr>';
  121.                         }
  122.  
  123.                         $output .= '</tbody>';
  124.                         $output .= '</table>';
  125.                         $output .= '</div></div>';
  126.  
  127.                         $output .= '<br><p>'._L('See also').':</p>';
  128.                         $output .= '<ul>';
  129.                         $output .= '<li><a href="'.OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'setup/">'._L('Setup part 1: Create %1 (contains database settings, CAPTCHA, admin password and SSL enforcement)','userdata/baseconfig/config.inc.php').'</a></li>';
  130.                         $oobePlugin = OIDplus::getPluginByOid('1.3.6.1.4.1.37476.2.5.2.4.3.50'); // OIDplusPageAdminOOBE
  131.                         if (!is_null($oobePlugin)) {
  132.                                 $output .= '<li><a href="'.OIDplus::webpath($oobePlugin->getPluginDirectory(),OIDplus::PATH_RELATIVE).'oobe.php">'._L('Setup part 2: Basic settings (they are all available above, too)').'</a></li>';
  133.                         } else {
  134.                                 $output .= '<li>'._L('Setup part 2 requires plugin %1 (the basic settings are all available above, too)','OIDplusPageAdminOOBE').'</a></li>';
  135.                         }
  136.                         $output .= '</ul>';
  137.  
  138.                         $out['text'] = $output;
  139.                 }
  140.         }
  141.  
  142.         /**
  143.          * @param array $json
  144.          * @param string|null $ra_email
  145.          * @param bool $nonjs
  146.          * @param string $req_goto
  147.          * @return bool
  148.          * @throws OIDplusException
  149.          */
  150.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  151.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  152.  
  153.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  154.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  155.                 } else {
  156.                         $tree_icon = null; // default icon (folder)
  157.                 }
  158.  
  159.                 $json[] = array(
  160.                         'id' => 'oidplus:edit_config',
  161.                         'icon' => $tree_icon,
  162.                         'text' => _L('System configuration')
  163.                 );
  164.  
  165.                 return true;
  166.         }
  167.  
  168.         /**
  169.          * @param string $request
  170.          * @return array|false
  171.          */
  172.         public function tree_search(string $request) {
  173.                 return false;
  174.         }
  175. }
  176.