Subversion Repositories oidplus

Rev

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