Subversion Repositories oidplus

Rev

Rev 277 | Rev 295 | 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
 
261 daniel-mar 20
// OIDplusConfig contains settings that are stored in the database.
294 daniel-mar 21
// Not to be confused with OIDplusBaseConfig which is the basic ("static")
22
// configuration stored in userdata/baseconfig/config.inc.php,
261 daniel-mar 23
// e.g. database access credentials.
24
class OIDplusConfig implements OIDplusConfigInterface {
2 daniel-mar 25
 
269 daniel-mar 26
        /*public*/ const PROTECTION_EDITABLE = 0;
27
        /*public*/ const PROTECTION_READONLY = 1;
28
        /*public*/ const PROTECTION_HIDDEN   = 2;
263 daniel-mar 29
 
75 daniel-mar 30
        protected $values = array();
150 daniel-mar 31
        protected $dirty = true;
263 daniel-mar 32
        protected $validateCallbacks = array();
13 daniel-mar 33
 
263 daniel-mar 34
        public function prepareConfigKey($name, $description, $init_value, $protection, $validateCallback) {
35
                switch ($protection) {
36
                        case OIDplusConfig::PROTECTION_EDITABLE:
37
                                $protected = 0;
38
                                $visible   = 1;
39
                                break;
40
                        case OIDplusConfig::PROTECTION_READONLY:
41
                                $protected = 1;
42
                                $visible   = 1;
43
                                break;
44
                        case OIDplusConfig::PROTECTION_HIDDEN:
45
                                $protected = 1;
46
                                $visible   = 0;
47
                                break;
48
                        default:
49
                                throw new OIDplusException("Invalid protection flag, use OIDplusConfig::PROTECTION_* constants");
50
                }
51
 
166 daniel-mar 52
                if (strlen($name) > 50) {
250 daniel-mar 53
                        throw new OIDplusException("Config key name '$name' is too long. (max 50).");
166 daniel-mar 54
                }
55
                if (strlen($description) > 255) {
250 daniel-mar 56
                        throw new OIDplusException("Description for config key '$name' is too long (max 255).");
166 daniel-mar 57
                }
150 daniel-mar 58
                $this->buildConfigArray();
59
                if (!isset($this->values[$name])) {
261 daniel-mar 60
                        OIDplus::db()->query("insert into ###config (name, description, value, protected, visible) values (?, ?, ?, ?, ?)", array($name, $description, $init_value, $protected, $visible));
155 daniel-mar 61
                        $this->values[$name] = $init_value;
150 daniel-mar 62
                }
263 daniel-mar 63
                if (!is_null($validateCallback)) {
64
                        $this->validateCallbacks[$name] = $validateCallback;
65
                }
13 daniel-mar 66
        }
67
 
150 daniel-mar 68
        protected function buildConfigArray() {
75 daniel-mar 69
                if ($this->dirty) {
70
                        $this->values = array();
261 daniel-mar 71
                        $res = OIDplus::db()->query("select name, value from ###config");
236 daniel-mar 72
                        while ($row = $res->fetch_object()) {
75 daniel-mar 73
                                $this->values[$row->name] = $row->value;
74
                        }
150 daniel-mar 75
                        $this->dirty = false;
75 daniel-mar 76
                }
150 daniel-mar 77
        }
75 daniel-mar 78
 
261 daniel-mar 79
        public function getValue($name, $default=null) {
150 daniel-mar 80
                $this->buildConfigArray();
60 daniel-mar 81
                if (isset($this->values[$name])) {
82
                        return $this->values[$name];
83
                } else {
261 daniel-mar 84
                        return $default;
60 daniel-mar 85
                }
46 daniel-mar 86
        }
87
 
74 daniel-mar 88
        public function exists($name) {
150 daniel-mar 89
                $this->buildConfigArray();
75 daniel-mar 90
                return !is_null($this->getValue($name));
74 daniel-mar 91
        }
92
 
14 daniel-mar 93
        public function setValue($name, $value) {
263 daniel-mar 94
                // Give plugins the possibility to stop the process by throwing an Exception (e.g. if the value is invalid)
95
                // Required is that the plugin previously prepared the config setting
14 daniel-mar 96
 
263 daniel-mar 97
                if (isset($this->validateCallbacks[$name])) {
98
                        $this->validateCallbacks[$name]($value);
14 daniel-mar 99
                }
257 daniel-mar 100
 
14 daniel-mar 101
                // Now change the value in the database
102
 
261 daniel-mar 103
                OIDplus::db()->query("update ###config set value = ? where name = ?", array($value, $name));
66 daniel-mar 104
                $this->values[$name] = $value;
14 daniel-mar 105
        }
106
 
294 daniel-mar 107
        public function deleteConfigKey($name) {
108
                $this->buildConfigArray();
109
                if (isset($this->values[$name])) {
110
                        OIDplus::db()->query("delete from ###config where name = ?", array($name));
111
                }
112
        }
113
 
2 daniel-mar 114
}