Failed Conditions
Push — psr2-config ( 8ea568...077c27 )
by Andreas
06:42 queued 03:35
created

Setting::is_protected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\plugin\config\core;
4
5
/**
6
 * Class setting
7
 */
8
class Setting {
9
10
    protected $_key = '';
11
    protected $_default = null;
12
    protected $_local = null;
13
    protected $_protected = null;
14
15
    protected $_pattern = '';
16
    protected $_error = false;            // only used by those classes which error check
17
    protected $_input = null;             // only used by those classes which error check
18
    protected $_caution = null;           // used by any setting to provide an alert along with the setting
19
    // valid alerts, 'warning', 'danger', 'security'
20
    // images matching the alerts are in the plugin's images directory
21
22
    static protected $_validCautions = array('warning', 'danger', 'security');
23
24
    /**
25
     * @param string $key
26
     * @param array|null $params array with metadata of setting
27
     */
28
    public function __construct($key, $params = null) {
29
        $this->_key = $key;
30
31
        if(is_array($params)) {
32
            foreach($params as $property => $value) {
33
                $this->$property = $value;
34
            }
35
        }
36
    }
37
38
    /**
39
     * Receives current values for the setting $key
40
     *
41
     * @param mixed $default default setting value
42
     * @param mixed $local local setting value
43
     * @param mixed $protected protected setting value
44
     */
45
    public function initialize($default, $local, $protected) {
46
        if(isset($default)) $this->_default = $default;
47
        if(isset($local)) $this->_local = $local;
48
        if(isset($protected)) $this->_protected = $protected;
49
    }
50
51
    /**
52
     * Should this type of config have a default?
53
     *
54
     * @return bool
55
     */
56
    public function shouldHaveDefault() {
57
        return true;
58
    }
59
60
    /**
61
     * Get this setting's unique key
62
     *
63
     * @return string
64
     */
65
    public function getKey() {
66
        return $this->_key;
67
    }
68
69
    /**
70
     * update changed setting with user provided value $input
71
     * - if changed value fails error check, save it to $this->_input (to allow echoing later)
72
     * - if changed value passes error check, set $this->_local to the new value
73
     *
74
     * @param  mixed $input the new value
75
     * @return boolean          true if changed, false otherwise (also on error)
76
     */
77
    public function update($input) {
78
        if(is_null($input)) return false;
79
        if($this->is_protected()) return false;
80
81
        $value = is_null($this->_local) ? $this->_default : $this->_local;
82
        if($value == $input) return false;
83
84
        if($this->_pattern && !preg_match($this->_pattern, $input)) {
85
            $this->_error = true;
86
            $this->_input = $input;
87
            return false;
88
        }
89
90
        $this->_local = $input;
91
        return true;
92
    }
93
94
    /**
95
     * Build html for label and input of setting
96
     *
97
     * @param \admin_plugin_config $plugin object of config plugin
98
     * @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting
99
     * @return string[] with content array(string $label_html, string $input_html)
100
     */
101
    public function html(\admin_plugin_config $plugin, $echo = false) {
102
        $disable = '';
103
104
        if($this->is_protected()) {
105
            $value = $this->_protected;
106
            $disable = 'disabled="disabled"';
107
        } else {
108
            if($echo && $this->_error) {
109
                $value = $this->_input;
110
            } else {
111
                $value = is_null($this->_local) ? $this->_default : $this->_local;
112
            }
113
        }
114
115
        $key = htmlspecialchars($this->_key);
116
        $value = formText($value);
117
118
        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
119
        $input = '<textarea rows="3" cols="40" id="config___' . $key .
120
            '" name="config[' . $key . ']" class="edit" ' . $disable . '>' . $value . '</textarea>';
121
        return array($label, $input);
122
    }
123
124
    /**
125
     * Generate string to save setting value to file according to $fmt
126
     *
127
     * @param string $var name of variable
128
     * @param string $fmt save format
129
     * @return string
130
     */
131
    public function out($var, $fmt = 'php') {
132
133
        if($this->is_protected()) return '';
134
        if(is_null($this->_local) || ($this->_default == $this->_local)) return '';
135
136
        $out = '';
137
138
        if($fmt == 'php') {
139
            $tr = array("\\" => '\\\\', "'" => '\\\'');
140
141
            $out = '$' . $var . "['" . $this->_out_key() . "'] = '" . strtr(cleanText($this->_local), $tr) . "';\n";
142
        }
143
144
        return $out;
145
    }
146
147
    /**
148
     * Returns the localized prompt
149
     *
150
     * @param \admin_plugin_config $plugin object of config plugin
151
     * @return string text
152
     */
153
    public function prompt(\admin_plugin_config $plugin) {
154
        $prompt = $plugin->getLang($this->_key);
155
        if(!$prompt) $prompt = htmlspecialchars(str_replace(array('____', '_'), ' ', $this->_key));
156
        return $prompt;
157
    }
158
159
    /**
160
     * Is setting protected
161
     *
162
     * @return bool
163
     */
164
    public function is_protected() {
165
        return !is_null($this->_protected);
166
    }
167
168
    /**
169
     * Is setting the default?
170
     *
171
     * @return bool
172
     */
173
    public function is_default() {
174
        return !$this->is_protected() && is_null($this->_local);
175
    }
176
177
    /**
178
     * Has an error?
179
     *
180
     * @return bool
181
     */
182
    public function error() {
183
        return $this->_error;
184
    }
185
186
    /**
187
     * Returns caution
188
     *
189
     * @return false|string caution string, otherwise false for invalid caution
190
     */
191
    public function caution() {
192
        if(!empty($this->_caution)) {
193
            if(!in_array($this->_caution, Setting::$_validCautions)) {
194
                trigger_error(
195
                    'Invalid caution string (' . $this->_caution . ') in metadata for setting "' . $this->_key . '"',
196
                    E_USER_WARNING
197
                );
198
                return false;
199
            }
200
            return $this->_caution;
201
        }
202
        // compatibility with previous cautionList
203
        // TODO: check if any plugins use; remove
204
        if(!empty($this->_cautionList[$this->_key])) {
0 ignored issues
show
Bug introduced by
The property _cautionList does not seem to exist. Did you mean _caution?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
205
            $this->_caution = $this->_cautionList[$this->_key];
0 ignored issues
show
Bug introduced by
The property _cautionList does not seem to exist. Did you mean _caution?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
206
            unset($this->_cautionList);
207
208
            return $this->caution();
209
        }
210
        return false;
211
    }
212
213
    /**
214
     * Returns setting key, eventually with referer to config: namespace at dokuwiki.org
215
     *
216
     * @param bool $pretty create nice key
217
     * @param bool $url provide url to config: namespace
218
     * @return string key
219
     */
220
    public function _out_key($pretty = false, $url = false) {
221
        if($pretty) {
222
            $out = str_replace(Configuration::KEYMARKER, "»", $this->_key);
223
            if($url && !strstr($out, '»')) {//provide no urls for plugins, etc.
224
                if($out == 'start') //one exception
225
                    return '<a href="http://www.dokuwiki.org/config:startpage">' . $out . '</a>';
226
                else
227
                    return '<a href="http://www.dokuwiki.org/config:' . $out . '">' . $out . '</a>';
228
            }
229
            return $out;
230
        } else {
231
            return str_replace(Configuration::KEYMARKER, "']['", $this->_key);
232
        }
233
    }
234
}
235