Subversion Repositories oidplus

Rev

Rev 166 | Rev 236 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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