Passed
Push — master ( 90e83d...4a3542 )
by El
03:14
created

lib/Configuration.php (2 issues)

Labels
1
<?php
2
/**
3
 * PrivateBin
4
 *
5
 * a zero-knowledge paste bin
6
 *
7
 * @link      https://github.com/PrivateBin/PrivateBin
8
 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
9
 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10
 * @version   1.2
11
 */
12
13
namespace PrivateBin;
14
15
use Exception;
16
use PDO;
17
use PrivateBin\Persistence\DataStore;
0 ignored issues
show
The type PrivateBin\Persistence\DataStore was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Configuration
21
 *
22
 * parses configuration file, ensures default values present
23
 */
24
class Configuration
25
{
26
    /**
27
     * parsed configuration
28
     *
29
     * @var array
30
     */
31
    private $_configuration;
32
33
    /**
34
     * default configuration
35
     *
36
     * @var array
37
     */
38
    private static $_defaults = array(
39
        'main' => array(
40
            'name'                     => 'PrivateBin',
41
            'discussion'               => true,
42
            'opendiscussion'           => false,
43
            'password'                 => true,
44
            'fileupload'               => false,
45
            'burnafterreadingselected' => false,
46
            'defaultformatter'         => 'plaintext',
47
            'syntaxhighlightingtheme'  => null,
48
            'sizelimit'                => 2097152,
49
            'template'                 => 'bootstrap',
50
            'notice'                   => '',
51
            'languageselection'        => false,
52
            'languagedefault'          => '',
53
            'urlshortener'             => '',
54
            'qrcode'                   => true,
55
            'icon'                     => 'identicon',
56
            'cspheader'                => 'default-src \'none\'; manifest-src \'self\'; connect-src *; script-src \'self\'; style-src \'self\'; font-src \'self\'; img-src \'self\' data:; media-src data:; object-src data:; Referrer-Policy: \'no-referrer\'; sandbox allow-same-origin allow-scripts allow-forms allow-popups allow-modals',
57
            'zerobincompatibility'     => false,
58
        ),
59
        'expire' => array(
60
            'default' => '1week',
61
        ),
62
        'expire_options' => array(
63
            '5min'   => 300,
64
            '10min'  => 600,
65
            '1hour'  => 3600,
66
            '1day'   => 86400,
67
            '1week'  => 604800,
68
            '1month' => 2592000,
69
            '1year'  => 31536000,
70
            'never'  => 0,
71
        ),
72
        'formatter_options' => array(
73
            'plaintext'          => 'Plain Text',
74
            'syntaxhighlighting' => 'Source Code',
75
            'markdown'           => 'Markdown',
76
        ),
77
        'traffic' => array(
78
            'limit'  => 10,
79
            'header' => null,
80
            'dir'    => 'data',
81
        ),
82
        'purge' => array(
83
            'limit'     => 300,
84
            'batchsize' => 10,
85
            'dir'       => 'data',
86
        ),
87
        'model' => array(
88
            'class' => 'Filesystem',
89
        ),
90
        'model_options' => array(
91
            'dir' => 'data',
92
        ),
93
    );
94
95
    /**
96
     * parse configuration file and ensure default configuration values are present
97
     *
98
     * @throws Exception
99
     */
100 119
    public function __construct()
101
    {
102 119
        $config     = array();
103 119
        $configFile = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php';
104 119
        $configIni  = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
105
106
        // rename INI files to avoid configuration leakage
107 119
        if (is_readable($configIni)) {
108 2
            DataStore::prependRename($configIni, $configFile, ';');
109
110
            // cleanup sample, too
111 2
            $configIniSample = $configIni . '.sample';
112 2
            if (is_readable($configIniSample)) {
113 2
                DataStore::prependRename($configIniSample, PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php', ';');
114
            }
115
        }
116
117 119
        if (is_readable($configFile)) {
118 117
            $config = parse_ini_file($configFile, true);
119 117
            foreach (array('main', 'model', 'model_options') as $section) {
120 117
                if (!array_key_exists($section, $config)) {
0 ignored issues
show
It seems like $config can also be of type false; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
                if (!array_key_exists($section, /** @scrutinizer ignore-type */ $config)) {
Loading history...
121 117
                    throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
122
                }
123
            }
124
        }
125
126 116
        $opts = '_options';
127 116
        foreach (self::getDefaults() as $section => $values) {
128
            // fill missing sections with default values
129 116
            if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
130 6
                $this->_configuration[$section] = $values;
131 6
                if (array_key_exists('dir', $this->_configuration[$section])) {
132 6
                    $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
133
                }
134 6
                continue;
135
            }
136
            // provide different defaults for database model
137
            elseif (
138 110
                $section == 'model_options' && in_array(
139 110
                    $this->_configuration['model']['class'],
140 110
                    array('Database', 'privatebin_db', 'zerobin_db')
141
                )
142
            ) {
143
                $values = array(
144 56
                    'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
145
                    'tbl' => null,
146
                    'usr' => null,
147
                    'pwd' => null,
148
                    'opt' => array(PDO::ATTR_PERSISTENT => true),
149
                );
150
            }
151
152
            // "*_options" sections don't require all defaults to be set
153
            if (
154 110
                $section !== 'model_options' &&
155 110
                ($from = strlen($section) - strlen($opts)) >= 0 &&
156 110
                strpos($section, $opts, $from) !== false
157
            ) {
158 110
                if (is_int(current($values))) {
159 110
                    $config[$section] = array_map('intval', $config[$section]);
160
                }
161 110
                $this->_configuration[$section] = $config[$section];
162
            }
163
            // check for missing keys and set defaults if necessary
164
            else {
165 110
                foreach ($values as $key => $val) {
166 110
                    if ($key == 'dir') {
167 110
                        $val = PATH . $val;
168
                    }
169 110
                    $result = $val;
170 110
                    if (array_key_exists($key, $config[$section])) {
171 110
                        if ($val === null) {
172 3
                            $result = $config[$section][$key];
173 110
                        } elseif (is_bool($val)) {
174 110
                            $val = strtolower($config[$section][$key]);
175 110
                            if (in_array($val, array('true', 'yes', 'on'))) {
176 1
                                $result = true;
177 110
                            } elseif (in_array($val, array('false', 'no', 'off'))) {
178 1
                                $result = false;
179
                            } else {
180 110
                                $result = (bool) $config[$section][$key];
181
                            }
182 110
                        } elseif (is_int($val)) {
183 110
                            $result = (int) $config[$section][$key];
184 110
                        } elseif (is_string($val) && !empty($config[$section][$key])) {
185 110
                            $result = (string) $config[$section][$key];
186
                        }
187
                    }
188 110
                    $this->_configuration[$section][$key] = $result;
189
                }
190
            }
191
        }
192
193
        // support for old config file format, before the fork was renamed and PSR-4 introduced
194 116
        $this->_configuration['model']['class'] = str_replace(
195 116
            'zerobin_', 'privatebin_',
196 116
            $this->_configuration['model']['class']
197
        );
198
199 116
        $this->_configuration['model']['class'] = str_replace(
200 116
            array('privatebin_data', 'privatebin_db'),
201 116
            array('Filesystem', 'Database'),
202 116
            $this->_configuration['model']['class']
203
        );
204
205
        // ensure a valid expire default key is set
206 116
        if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
207 1
            $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
208
        }
209 116
    }
210
211
    /**
212
     * get configuration as array
213
     *
214
     * @return array
215
     */
216 6
    public function get()
217
    {
218 6
        return $this->_configuration;
219
    }
220
221
    /**
222
     * get default configuration as array
223
     *
224
     * @return array
225
     */
226 117
    public static function getDefaults()
227
    {
228 117
        return self::$_defaults;
229
    }
230
231
    /**
232
     * get a key from the configuration, typically the main section or all keys
233
     *
234
     * @param string $key
235
     * @param string $section defaults to main
236
     * @throws Exception
237
     * @return mixed
238
     */
239 108
    public function getKey($key, $section = 'main')
240
    {
241 108
        $options = $this->getSection($section);
242 108
        if (!array_key_exists($key, $options)) {
243 1
            throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
244
        }
245 107
        return $this->_configuration[$section][$key];
246
    }
247
248
    /**
249
     * get a section from the configuration, must exist
250
     *
251
     * @param string $section
252
     * @throws Exception
253
     * @return mixed
254
     */
255 108
    public function getSection($section)
256
    {
257 108
        if (!array_key_exists($section, $this->_configuration)) {
258 1
            throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
259
        }
260 108
        return $this->_configuration[$section];
261
    }
262
}
263