Subversion Repositories oidplus

Rev

Rev 46 | Rev 61 | 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
26
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value) values ('system_title', 'What is the name of your RA?', 'OIDplus 2.0')");
27
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value) values ('global_cc', 'Global CC for all outgoing emails?', '')");
28
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value) values ('ra_min_password_length', 'Minimum length for RA passwords', '6')");
29
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value) values ('max_ra_invite_time', 'Max RA invite time in seconds (0 = infinite)', '0')");
30
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value) values ('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0')");
14 daniel-mar 31
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value) values ('whois_auth_token', 'OID-over-WHOIS authentication token to display confidential data', '')");
60 daniel-mar 32
 
33
                // Also ask the plugins if they have defaults
34
                $ary = glob(__DIR__ . '/../../plugins/'.'*'.'/'.'*'.'/cfg_loadconfig.inc.php');
35
                sort($ary);
36
                foreach ($ary as $a) include $a;
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
 
14 daniel-mar 89
        public function setValue($name, $value) {
90
                // Check for valid values
91
 
92
                if ($name == 'system_title') {
93
                        if (empty($value)) {
94
                                throw new Exception("Please enter a value for the system title.");
95
 
96
                        }
97
                }
98
                if ($name == 'global_cc') {
99
                        if (!empty($value) && !oiddb_valid_email($value)) {
100
                                throw new Exception("This is not a correct email address");
101
                        }
102
                }
103
                if ($name == 'ra_min_password_length') {
104
                        if (!is_numeric($value) || ($value < 1)) {
105
                                throw new Exception("Please enter a valid password length.");
106
                        }
107
                }
60 daniel-mar 108
                if (($name == 'max_ra_invite_time') || ($name == 'max_ra_pwd_reset_time')) {
14 daniel-mar 109
                        if (!is_numeric($value) || ($value < 0)) {
110
                                throw new Exception("Please enter a valid value.");
111
                        }
112
                }
113
                if ($name == 'whois_auth_token') {
114
                        $test_value = preg_replace('@[0-9a-zA-Z]*@', '', $value);
115
                        if ($test_value != '') {
116
                                throw new Exception("Only characters and numbers are allowed as authentication token.");
117
                        }
118
                }
119
 
60 daniel-mar 120
 
121
                $ary = glob(__DIR__ . '/../../plugins/'.'*'.'/'.'*'.'/cfg_setvalue.inc.php');
122
                sort($ary);
123
                foreach ($ary as $a) include $a;
124
 
14 daniel-mar 125
                // Now change the value in the database
126
 
127
                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)."'")) {
128
                        throw new Exception(OIDplus::db()->error());
129
                }
130
        }
131
 
2 daniel-mar 132
}