Subversion Repositories oidplus

Rev

Rev 69 | Rev 75 | 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
 
20
class OIDplusConfig {
21
 
13 daniel-mar 22
        protected $values;
23
 
24
        protected function loadConfig() {
25
                // Add defaults
64 daniel-mar 26
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values ('system_title', 'What is the name of your RA?', 'OIDplus 2.0', 0, 1)");
27
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values ('global_cc', 'Global CC for all outgoing emails?', '', 0, 1)");
28
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values ('ra_min_password_length', 'Minimum length for RA passwords', '6', 0, 1)");
29
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values ('max_ra_invite_time', 'Max RA invite time in seconds (0 = infinite)', '0', 0, 1)");
30
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values ('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0', 0, 1)");
31
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values ('whois_auth_token', 'OID-over-WHOIS authentication token to display confidential data', '', 0, 1)");
60 daniel-mar 32
 
33
                // Also ask the plugins if they have defaults
61 daniel-mar 34
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
35
                        $plugin->cfgLoadConfig();
36
                }
60 daniel-mar 37
 
38
                // Now load the values
39
                $this->values = array();
40
                $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."config");
41
                while ($row = OIDplus::db()->fetch_object($res)) {
42
                        $this->values[$row->name] = $row->value;
43
                }
13 daniel-mar 44
        }
45
 
2 daniel-mar 46
        public function __construct() {
13 daniel-mar 47
                $this->loadConfig();
2 daniel-mar 48
        }
49
 
50
        public function systemTitle() {
13 daniel-mar 51
                return trim($this->values['system_title']);
2 daniel-mar 52
        }
53
 
54
        public function globalCC() {
13 daniel-mar 55
                return trim($this->values['global_cc']);
2 daniel-mar 56
        }
57
 
58
        public function minRaPasswordLength() {
13 daniel-mar 59
                return $this->values['ra_min_password_length'];
2 daniel-mar 60
        }
61
 
62
        /*   hardcoded in setup/ , because during installation, we dont have a settings database
63
        public function minAdminPasswordLength() {
64
                return 6;
65
        }
66
        */
67
 
68
        public function maxInviteTime() {
13 daniel-mar 69
                return $this->values['max_ra_invite_time'];
2 daniel-mar 70
        }
71
 
72
        public function maxPasswordResetTime() {
13 daniel-mar 73
                return $this->values['max_ra_pwd_reset_time'];
2 daniel-mar 74
        }
75
 
12 daniel-mar 76
        public function authToken() {
13 daniel-mar 77
                $val = trim($this->values['whois_auth_token']);
78
                return empty($val) ? false : $val;
12 daniel-mar 79
        }
14 daniel-mar 80
 
60 daniel-mar 81
        public function getValue($name) {
82
                if (isset($this->values[$name])) {
83
                        return $this->values[$name];
84
                } else {
85
                        return null;
86
                }
46 daniel-mar 87
        }
88
 
74 daniel-mar 89
        public function exists($name) {
90
                return isset($this->values[$name]);
91
        }
92
 
14 daniel-mar 93
        public function setValue($name, $value) {
94
                // Check for valid values
95
 
96
                if ($name == 'system_title') {
97
                        if (empty($value)) {
98
                                throw new Exception("Please enter a value for the system title.");
99
 
100
                        }
101
                }
102
                if ($name == 'global_cc') {
103
                        if (!empty($value) && !oiddb_valid_email($value)) {
104
                                throw new Exception("This is not a correct email address");
105
                        }
106
                }
107
                if ($name == 'ra_min_password_length') {
108
                        if (!is_numeric($value) || ($value < 1)) {
109
                                throw new Exception("Please enter a valid password length.");
110
                        }
111
                }
60 daniel-mar 112
                if (($name == 'max_ra_invite_time') || ($name == 'max_ra_pwd_reset_time')) {
14 daniel-mar 113
                        if (!is_numeric($value) || ($value < 0)) {
114
                                throw new Exception("Please enter a valid value.");
115
                        }
116
                }
117
                if ($name == 'whois_auth_token') {
118
                        $test_value = preg_replace('@[0-9a-zA-Z]*@', '', $value);
119
                        if ($test_value != '') {
120
                                throw new Exception("Only characters and numbers are allowed as authentication token.");
121
                        }
122
                }
123
 
66 daniel-mar 124
                if ($name == 'objecttypes_enabled') {
125
                        # 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?
126
 
127
                        $ary = explode(';',$value);
128
                        $uniq_ary = array_unique($ary);
129
 
130
                        if (count($ary) != count($uniq_ary)) {
131
                                throw new Exception("Please check your input. Some object types are double.");
132
                        }
133
 
134
                        foreach ($ary as $ot_check) {
135
                                $ns_found = false;
136
                                foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
137
                                        if ($ot::ns() == $ot_check) {
138
                                                $ns_found = true;
139
                                                break;
140
                                        }
141
                                }
74 daniel-mar 142
                                foreach (OIDplus::getDisabledObjectTypes() as $ot) {
143
                                        if ($ot::ns() == $ot_check) {
144
                                                $ns_found = true;
145
                                                break;
146
                                        }
147
                                }
66 daniel-mar 148
                                if (!$ns_found) {
149
                                        throw new Exception("Please check your input. Namespace \"$ot_check\" is not found");
150
                                }
151
                        }
152
                }
153
 
74 daniel-mar 154
                // Give plugins the possibility to stop the process (e.g. if the value is invalid)
155
 
61 daniel-mar 156
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
157
                        $plugin->cfgSetValue($name, $value);
158
                }
60 daniel-mar 159
 
14 daniel-mar 160
                // Now change the value in the database
161
 
162
                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)."'")) {
163
                        throw new Exception(OIDplus::db()->error());
164
                }
66 daniel-mar 165
                $this->values[$name] = $value;
14 daniel-mar 166
        }
167
 
2 daniel-mar 168
}