Failed Conditions
Push — psr2-config ( e063ba...8ea568 )
by Andreas
03:29
created

Configuration::get_plugintpl_metadata()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
nc 24
nop 1
dl 0
loc 41
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Configuration Class
4
 *
5
 * @author  Chris Smith <[email protected]>
6
 * @author  Ben Coburn <[email protected]>
7
 */
8
9
namespace dokuwiki\plugin\config\core;
10
11
/**
12
 * Class configuration
13
 */
14
class Configuration {
15
16
    const KEYMARKER = '____';
17
18
    protected $_name = 'conf';     // name of the config variable found in the files (overridden by $config['varname'])
19
    protected $_format = 'php';    // format of the config file, supported formats - php (overridden by $config['format'])
20
    protected $_heading = '';      // heading string written at top of config file - don't include comment indicators
21
22
    /** @var ConfigParser */
23
    protected $parser;
24
25
    protected $_loaded = false;    // set to true after configuration files are loaded
26
    protected $_metadata = array();// holds metadata describing the settings
27
28
    public $locked = false;     // configuration is considered locked if it can't be updated
29
    public $show_disabled_plugins = false;
30
31
    // configuration filenames
32
    protected $_default_files = array();
33
    protected $_local_files = array();      // updated configuration is written to the first file
34
    protected $_protected_files = array();
35
36
    protected $_plugin_list = null;
37
38
    /** @var ConfigSettings FIXME better name? */
39
    protected $confset;
40
41
42
    /**
43
     * constructor
44
     *
45
     * @param string $datafile path to config metadata file
46
     */
47
    public function __construct($datafile) {
48
        global $conf, $config_cascade;
49
50
        if(!file_exists($datafile)) {
51
            msg('No configuration metadata found at - ' . htmlspecialchars($datafile), -1);
52
            return;
53
        }
54
55
        /** @var array $config gets loaded via include here */
56
        /* FIXME I think we can remove this
57
        include($datafile);
58
        if(isset($config['varname'])) $this->_name = $config['varname'];
59
        if(isset($config['format'])) $this->_format = $config['format'];
60
        if(isset($config['heading'])) $this->_heading = $config['heading'];
61
        */
62
63
        $this->_local_files = $config_cascade['main']['local']; // FIXME simplify and remove this later
64
        $this->locked = $this->_is_locked();
65
66
        $this->confset = new ConfigSettings();
67
    }
68
69
    /**
70
     * Stores setting[] array to file
71
     *
72
     * @param string $id Name of plugin, which saves the settings
73
     * @param string $header Text at the top of the rewritten settings file
74
     * @param bool $backup backup current file? (remove any existing backup)
75
     * @return bool succesful?
76
     */
77
    public function save_settings($id, $header = '', $backup = true) {
78
        global $conf;
79
80
        if($this->locked) return false;
81
82
        // write back to the last file in the local config cascade
83
        $file = end($this->_local_files);
84
85
        // backup current file (remove any existing backup)
86
        if(file_exists($file) && $backup) {
87
            if(file_exists($file . '.bak')) @unlink($file . '.bak');
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
88
            if(!io_rename($file, $file . '.bak')) return false;
89
        }
90
91
        if(!$fh = @fopen($file, 'wb')) {
92
            io_rename($file . '.bak', $file);     // problem opening, restore the backup
93
            return false;
94
        }
95
96
        if(empty($header)) $header = $this->_heading;
97
98
        $out = $this->_out_header($id, $header);
99
100
        foreach($this->confset->getSettings() as $setting) {
101
            $out .= $setting->out($this->_name, $this->_format);
102
        }
103
104
        $out .= $this->_out_footer();
105
106
        @fwrite($fh, $out);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
107
        fclose($fh);
108
        if($conf['fperm']) chmod($file, $conf['fperm']);
109
        return true;
110
    }
111
112
    /**
113
     * Update last modified time stamp of the config file
114
     *
115
     * @return bool
116
     */
117
    public function touch_settings() {
118
        if($this->locked) return false;
119
        $file = end($this->_local_files);
120
        return @touch($file);
121
    }
122
123
    /**
124
     * Returns header of rewritten settings file
125
     *
126
     * @param string $id plugin name of which generated this output
127
     * @param string $header additional text for at top of the file
128
     * @return string text of header
129
     */
130
    protected function _out_header($id, $header) {
131
        $out = '';
132
        if($this->_format == 'php') {
133
            $out .= '<' . '?php' . "\n" .
134
                "/*\n" .
135
                " * " . $header . "\n" .
136
                " * Auto-generated by " . $id . " plugin\n" .
137
                " * Run for user: " . $_SERVER['REMOTE_USER'] . "\n" .
138
                " * Date: " . date('r') . "\n" .
139
                " */\n\n";
140
        }
141
142
        return $out;
143
    }
144
145
    /**
146
     * Returns footer of rewritten settings file
147
     *
148
     * @return string text of footer
149
     */
150
    protected function _out_footer() {
151
        $out = '';
152
        if($this->_format == 'php') {
153
            $out .= "\n// end auto-generated content\n";
154
        }
155
156
        return $out;
157
    }
158
159
    /**
160
     * Configuration is considered locked if there is no local settings filename
161
     * or the directory its in is not writable or the file exists and is not writable
162
     *
163
     * @return bool true: locked, false: writable
164
     */
165
    protected function _is_locked() {
166
        if(!$this->_local_files) return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_local_files of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
167
168
        $local = $this->_local_files[0];
169
170
        if(!is_writable(dirname($local))) return true;
171
        if(file_exists($local) && !is_writable($local)) return true;
172
173
        return false;
174
    }
175
176
}
177
178