Subversion Repositories oidplus

Rev

Rev 112 | Rev 155 | 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) {
150 daniel-mar 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
                }
13 daniel-mar 33
        }
34
 
2 daniel-mar 35
        public function __construct() {
150 daniel-mar 36
                // TODO: Auslagern in ein Plugin
75 daniel-mar 37
                $this->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', 0, 1);
76 daniel-mar 38
                $this->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', 0, 1);
75 daniel-mar 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);
2 daniel-mar 41
        }
42
 
43
        public function systemTitle() {
75 daniel-mar 44
                return trim($this->getValue('system_title'));
2 daniel-mar 45
        }
46
 
47
        public function globalCC() {
108 daniel-mar 48
                return trim($this->getValue('global_cc'));
2 daniel-mar 49
        }
50
 
51
        public function minRaPasswordLength() {
75 daniel-mar 52
                return $this->getValue('ra_min_password_length');
2 daniel-mar 53
        }
54
 
75 daniel-mar 55
        /* hardcoded in setup/, because during installation, we don't have a settings database
2 daniel-mar 56
        public function minAdminPasswordLength() {
80 daniel-mar 57
                return 10;
2 daniel-mar 58
        }
59
        */
60
 
150 daniel-mar 61
        protected function buildConfigArray() {
75 daniel-mar 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
                        }
150 daniel-mar 68
                        $this->dirty = false;
75 daniel-mar 69
                }
150 daniel-mar 70
        }
75 daniel-mar 71
 
150 daniel-mar 72
        public function getValue($name) {
73
                $this->buildConfigArray();
60 daniel-mar 74
                if (isset($this->values[$name])) {
75
                        return $this->values[$name];
76
                } else {
77
                        return null;
78
                }
46 daniel-mar 79
        }
80
 
74 daniel-mar 81
        public function exists($name) {
150 daniel-mar 82
                $this->buildConfigArray();
75 daniel-mar 83
                return !is_null($this->getValue($name));
74 daniel-mar 84
        }
85
 
14 daniel-mar 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
                }
76 daniel-mar 95
                if (($name == 'global_cc') || ($name == 'admin_email')) {
104 daniel-mar 96
                        if (!empty($value) && !oidplus_valid_email($value)) {
14 daniel-mar 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
 
66 daniel-mar 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
                                }
74 daniel-mar 124
                                foreach (OIDplus::getDisabledObjectTypes() as $ot) {
125
                                        if ($ot::ns() == $ot_check) {
126
                                                $ns_found = true;
127
                                                break;
128
                                        }
129
                                }
66 daniel-mar 130
                                if (!$ns_found) {
131
                                        throw new Exception("Please check your input. Namespace \"$ot_check\" is not found");
132
                                }
133
                        }
134
                }
135
 
74 daniel-mar 136
                // Give plugins the possibility to stop the process (e.g. if the value is invalid)
137
 
61 daniel-mar 138
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
139
                        $plugin->cfgSetValue($name, $value);
140
                }
60 daniel-mar 141
 
14 daniel-mar 142
                // Now change the value in the database
143
 
150 daniel-mar 144
                if (!OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."config set value = ? where name = ?", array($value, $name))) {
14 daniel-mar 145
                        throw new Exception(OIDplus::db()->error());
146
                }
66 daniel-mar 147
                $this->values[$name] = $value;
14 daniel-mar 148
        }
149
 
2 daniel-mar 150
}