Subversion Repositories oidplus

Rev

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