Subversion Repositories oidplus

Rev

Rev 112 | Rev 155 | 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 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplusConfig {
  23.  
  24.         protected $values = array();
  25.         protected $dirty = true;
  26.  
  27.         public function prepareConfigKey($name, $description, $init_value, $protected, $visible) {
  28.                 $this->buildConfigArray();
  29.                 if (!isset($this->values[$name])) {
  30.                         OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values (?, ?, ?, ?, ?)", array($name, $description, $init_value, $protected, $visible));
  31.                         $this->values[$name] = $value;
  32.                 }
  33.         }
  34.  
  35.         public function __construct() {
  36.                 // TODO: Auslagern in ein Plugin
  37.                 $this->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', 0, 1);
  38.                 $this->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', 0, 1);
  39.                 $this->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', 0, 1);
  40.                 $this->prepareConfigKey('ra_min_password_length', 'Minimum length for RA passwords', '6', 0, 1);
  41.         }
  42.  
  43.         public function systemTitle() {
  44.                 return trim($this->getValue('system_title'));
  45.         }
  46.  
  47.         public function globalCC() {
  48.                 return trim($this->getValue('global_cc'));
  49.         }
  50.  
  51.         public function minRaPasswordLength() {
  52.                 return $this->getValue('ra_min_password_length');
  53.         }
  54.  
  55.         /* hardcoded in setup/, because during installation, we don't have a settings database
  56.         public function minAdminPasswordLength() {
  57.                 return 10;
  58.         }
  59.         */
  60.  
  61.         protected function buildConfigArray() {
  62.                 if ($this->dirty) {
  63.                         $this->values = array();
  64.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."config");
  65.                         while ($row = OIDplus::db()->fetch_object($res)) {
  66.                                 $this->values[$row->name] = $row->value;
  67.                         }
  68.                         $this->dirty = false;
  69.                 }
  70.         }
  71.  
  72.         public function getValue($name) {
  73.                 $this->buildConfigArray();
  74.                 if (isset($this->values[$name])) {
  75.                         return $this->values[$name];
  76.                 } else {
  77.                         return null;
  78.                 }
  79.         }
  80.  
  81.         public function exists($name) {
  82.                 $this->buildConfigArray();
  83.                 return !is_null($this->getValue($name));
  84.         }
  85.  
  86.         public function setValue($name, $value) {
  87.                 // Check for valid values
  88.  
  89.                 if ($name == 'system_title') {
  90.                         if (empty($value)) {
  91.                                 throw new Exception("Please enter a value for the system title.");
  92.  
  93.                         }
  94.                 }
  95.                 if (($name == 'global_cc') || ($name == 'admin_email')) {
  96.                         if (!empty($value) && !oidplus_valid_email($value)) {
  97.                                 throw new Exception("This is not a correct email address");
  98.                         }
  99.                 }
  100.                 if ($name == 'ra_min_password_length') {
  101.                         if (!is_numeric($value) || ($value < 1)) {
  102.                                 throw new Exception("Please enter a valid password length.");
  103.                         }
  104.                 }
  105.  
  106.                 if ($name == 'objecttypes_enabled') {
  107.                         # TODO: when objecttypes_enabled is changed at the admin control panel, we need to do a reload of the page, so that jsTree will be updated. Is there anything we can do?
  108.  
  109.                         $ary = explode(';',$value);
  110.                         $uniq_ary = array_unique($ary);
  111.  
  112.                         if (count($ary) != count($uniq_ary)) {
  113.                                 throw new Exception("Please check your input. Some object types are double.");
  114.                         }
  115.  
  116.                         foreach ($ary as $ot_check) {
  117.                                 $ns_found = false;
  118.                                 foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
  119.                                         if ($ot::ns() == $ot_check) {
  120.                                                 $ns_found = true;
  121.                                                 break;
  122.                                         }
  123.                                 }
  124.                                 foreach (OIDplus::getDisabledObjectTypes() as $ot) {
  125.                                         if ($ot::ns() == $ot_check) {
  126.                                                 $ns_found = true;
  127.                                                 break;
  128.                                         }
  129.                                 }
  130.                                 if (!$ns_found) {
  131.                                         throw new Exception("Please check your input. Namespace \"$ot_check\" is not found");
  132.                                 }
  133.                         }
  134.                 }
  135.  
  136.                 // Give plugins the possibility to stop the process (e.g. if the value is invalid)
  137.  
  138.                 foreach (OIDplus::getPagePlugins('*') as $plugin) {
  139.                         $plugin->cfgSetValue($name, $value);
  140.                 }
  141.  
  142.                 // Now change the value in the database
  143.  
  144.                 if (!OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."config set value = ? where name = ?", array($value, $name))) {
  145.                         throw new Exception(OIDplus::db()->error());
  146.                 }
  147.                 $this->values[$name] = $value;
  148.         }
  149.  
  150. }
  151.