GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 2.9 ( 9df237...73761e )
by Thorsten
13:21
created

PMF_Configuration::getLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * The main class for fetching the configuration, update and delete items. This
5
 * class is also a small Dependency Injection Container for phpMyFAQ.
6
 *
7
 * PHP Version 5.5
8
 *
9
 * This Source Code Form is subject to the terms of the Mozilla Public License,
10
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
11
 * obtain one at http://mozilla.org/MPL/2.0/.
12
 *
13
 * @category  phpMyFAQ
14
 *
15
 * @author    Thorsten Rinne <[email protected]>
16
 * @copyright 2006-2015 phpMyFAQ Team
17
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
18
 *
19
 * @link      http://www.phpmyfaq.de
20
 * @since     2006-01-04
21
 */
22
if (!defined('IS_VALID_PHPMYFAQ')) {
23
    exit();
24
}
25
26
/**
27
 * PMF_Configuration.
28
 *
29
 * @category  phpMyFAQ
30
 *
31
 * @author    Thorsten Rinne <[email protected]>
32
 * @copyright 2006-2015 phpMyFAQ Team
33
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
34
 *
35
 * @link      http://www.phpmyfaq.de
36
 * @since     2006-01-04
37
 */
38
class PMF_Configuration
39
{
40
    /**
41
     * Tablename.
42
     *
43
     * @var string
44
     */
45
    protected $_tableName = 'faqconfig';
46
47
    /**
48
     * Configuration array.
49
     *
50
     * @var array
51
     */
52
    public $config = [];
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param PMF_DB_Driver $database
58
     *
59
     * @return PMF_Configuration
60
     */
61
    public function __construct(PMF_DB_Driver $database)
62
    {
63
        $this->setDb($database);
64
    }
65
66
    /**
67
     * Fetches all configuration items into an array.
68
     */
69
    public function getAll()
70
    {
71
        $config = [];
72
        $query = sprintf('
73
            SELECT
74
                config_name, config_value
75
            FROM
76
                %s%s',
77
            PMF_Db::getTablePrefix(),
78
            $this->_tableName
79
        );
80
81
        $result = $this->getDb()->query($query);
82
        try {
83
            $config = $this->getDb()->fetchAll($result);
84
        } catch (Exception $e) {
85
            // @todo Added proper handling of exception
86
            echo $e->getMessage();
87
        }
88
        foreach ($config as $items) {
89
            $this->config[$items->config_name] = $items->config_value;
90
        }
91
    }
92
93
    /**
94
     * Returns a configuration item.
95
     *
96
     * @param string $item Configuration item
97
     *
98
     * @return mixed
99
     */
100
    public function get($item)
101
    {
102
        if (!isset($this->config[$item])) {
103
            $this->getAll();
104
        }
105
106
        if (isset($this->config[$item])) {
107
            switch ($this->config[$item]) {
108
                case 'true':
109
                    return true;
110
                    break;
111
                case 'false':
112
                    return false;
113
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
114
                default:
115
                    return $this->config[$item];
116
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
117
            }
118
        }
119
120
        return;
121
    }
122
123
    /**
124
     * Sets one single configuration item.
125
     *
126
     * @param string $key
127
     * @param mixed  $value
128
     *
129
     * @return bool
130
     */
131 View Code Duplication
    public function set($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $query = sprintf("UPDATE %s%s SET config_value = '%s' WHERE config_name = '%s'",
134
            PMF_Db::getTablePrefix(),
135
            $this->_tableName,
136
            $this->getDb()->escape(trim($value)),
137
            $this->getDb()->escape(trim($key))
138
        );
139
140
        return $this->getDb()->query($query);
141
    }
142
143
    /**
144
     * Sets the PMF_DB_Driver object.
145
     *
146
     * @param PMF_DB_Driver $database
147
     */
148
    public function setDb(PMF_DB_Driver $database)
149
    {
150
        $this->config['core.database'] = $database;
151
    }
152
153
    /**
154
     * Returns the PMF_DB_Driver object.
155
     *
156
     * @return PMF_DB_Driver
157
     */
158
    public function getDb()
159
    {
160
        return $this->config['core.database'];
161
    }
162
163
    /**
164
     * Sets the PMF_Instance object.
165
     *
166
     * @param PMF_Instance $instance
167
     */
168
    public function setInstance(PMF_Instance $instance)
169
    {
170
        $this->config['core.instance'] = $instance;
171
    }
172
173
    /**
174
     * Returns the PMF_Instance object.
175
     *
176
     * @return PMF_Instance
177
     */
178
    public function getInstance()
179
    {
180
        return $this->config['core.instance'];
181
    }
182
183
    /**
184
     * Sets the Language object.
185
     *
186
     * @param PMF_Language $language
187
     */
188
    public function setLanguage(PMF_Language $language)
189
    {
190
        $this->config['core.language'] = $language;
191
    }
192
193
    /**
194
     * Returns the Language object.
195
     *
196
     * @return PMF_Language
197
     */
198
    public function getLanguage()
199
    {
200
        return $this->config['core.language'];
201
    }
202
203
    /**
204
     * Returns the default language.
205
     *
206
     * @return string
207
     */
208
    public function getDefaultLanguage()
209
    {
210
        return str_replace(['language_', '.php'], '', $this->config['main.language']);
211
    }
212
213
    /**
214
     * Returns the default URL of the phpMyFAQ installation.
215
     *
216
     * @return string
217
     */
218
    public function getDefaultUrl()
219
    {
220
        $defaultUrl = $this->get('main.referenceURL');
221
222
        if (substr($defaultUrl, -1) !== '/') {
223
            return $defaultUrl.'/';
224
        } else {
225
            return $defaultUrl;
226
        }
227
    }
228
229
    /**
230
     * Sets the PMF_Ldap object.
231
     *
232
     * @param PMF_Ldap $ldap
233
     */
234
    public function setLdap(PMF_Ldap $ldap)
235
    {
236
        $this->config['core.ldap'] = $ldap;
237
    }
238
239
    /**
240
     * Returns the PMF_Ldap object.
241
     *
242
     * @return PMF_Ldap
243
     */
244
    public function getLdap()
245
    {
246
        return $this->config['core.ldap'];
247
    }
248
249
    /**
250
     * Sets the LDAP configuration.
251
     *
252
     * @param Array $ldapConfig
253
     */
254
    public function setLdapConfig(Array $ldapConfig)
255
    {
256
        // Always add main LDAP server
257
        $this->config['core.ldapServer'][0] = [
258
            'ldap_server' => $ldapConfig['ldap_server'],
259
            'ldap_port' => $ldapConfig['ldap_port'],
260
            'ldap_user' => $ldapConfig['ldap_user'],
261
            'ldap_password' => $ldapConfig['ldap_password'],
262
            'ldap_base' => $ldapConfig['ldap_base'],
263
        ];
264
265
        // Add multiple LDAP servers if enabled
266
        if (true === $ldapConfig['ldap_use_multiple_servers']) {
267
            $key = 1;
268
            while ($key >= 1) {
269
                if (isset($ldapConfig[$key])) {
270
                    $this->config['core.ldapServer'][$key] = $ldapConfig[$key];
271
                    ++$key;
272
                } else {
273
                    break;
274
                }
275
            }
276
        }
277
278
        // Set LDAP configuration
279
        $this->config['core.ldapConfig'] = [
280
            'ldap_use_multiple_servers' => $ldapConfig['ldap_use_multiple_servers'],
281
            'ldap_mapping' => $ldapConfig['ldap_mapping'],
282
            'ldap_use_domain_prefix' => $ldapConfig['ldap_use_domain_prefix'],
283
            'ldap_options' => $ldapConfig['ldap_options'],
284
            'ldap_use_memberOf' => $ldapConfig['ldap_use_memberOf'],
285
            'ldap_use_sasl' => $ldapConfig['ldap_use_sasl'],
286
            'ldap_use_anonymous_login' => $ldapConfig['ldap_use_anonymous_login'],
287
        ];
288
    }
289
290
    /**
291
     * Returns the LDAP configuration.
292
     *
293
     * @return array
294
     */
295
    public function getLdapConfig()
296
    {
297
        return isset($this->config['core.ldapConfig']) ? $this->config['core.ldapConfig'] : [];
298
    }
299
300
    /**
301
     * Returns the LDAP server(s).
302
     *
303
     * @return array
304
     */
305
    public function getLdapServer()
306
    {
307
        return isset($this->config['core.ldapServer']) ? $this->config['core.ldapServer'] : [];
308
    }
309
310
    /**
311
     * Adds a configuration item for the database.
312
     *
313
     * @param string $name
314
     * @param mixed  $value
315
     *
316
     * @return bool
317
     */
318 View Code Duplication
    public function add($name, $value)
319
    {
320
        $insert = sprintf(
321
            "INSERT INTO
322
                %s%s
323
            VALUES
324
                ('%s', '%s')",
325
            PMF_Db::getTablePrefix(),
326
            $this->_tableName,
327
            $this->getDb()->escape(trim($name)),
328
            $this->getDb()->escape(trim($value))
329
        );
330
331
        return $this->getDb()->query($insert);
332
    }
333
334
    /**
335
     * Deletes a configuration item for the database.
336
     *
337
     * @param string $name
338
     *
339
     * @return bool
340
     */
341 View Code Duplication
    public function delete($name)
342
    {
343
        $delete = sprintf(
344
            "DELETE FROM
345
                %s%s
346
            WHERE
347
              config_name = '%s'",
348
            PMF_Db::getTablePrefix(),
349
            $this->_tableName,
350
            $this->getDb()->escape(trim($name))
351
        );
352
353
        return $this->getDb()->query($delete);
354
    }
355
356
    /**
357
     * Updates all configuration items.
358
     *
359
     * @param array $newConfigs Array with new configuration values
360
     *
361
     * @return bool
362
     */
363
    public function update(Array $newConfigs)
364
    {
365
        $runtimeConfigs = array(
366
            'core.database',  // PMF_DB_Driver
367
            'core.instance',  // PMF_Instance
368
            'core.language',  // Language
369
            'core.ldap',      // PMF_Ldap
370
            'core.ldapConfig', // $PMF_LDAP
371
        );
372
        if (is_array($newConfigs)) {
373
            foreach ($newConfigs as $name => $value) {
374
                if ($name != 'main.phpMyFAQToken' &&
375
                    !in_array($name, $runtimeConfigs)
376
                ) {
377
                    $update = sprintf("
378
                        UPDATE
379
                            %s%s
380
                        SET
381
                            config_value = '%s'
382
                        WHERE
383
                            config_name = '%s'",
384
                        PMF_Db::getTablePrefix(),
385
                        $this->_tableName,
386
                        $this->getDb()->escape(trim($value)),
387
                        $name
388
                    );
389
390
                    $this->getDb()->query($update);
391
                    if (isset($this->config[$name])) {
392
                        unset($this->config[$name]);
393
                    }
394
                }
395
            }
396
397
            return true;
398
        }
399
400
        return false;
401
    }
402
403
    /**
404
     * Returns all sorting possibilities for FAQ records.
405
     *
406
     * @param string $current
407
     *
408
     * @return string
409
     */
410
    public static function sortingOptions($current)
411
    {
412
        global $PMF_LANG;
413
414
        $options = ['id', 'thema', 'visits', 'updated', 'author'];
415
        $output = '';
416
417
        foreach ($options as $value) {
418
            printf('<option value="%s"%s>%s</option>',
419
                $value,
420
                ($value == $current) ? ' selected' : '',
421
                $PMF_LANG['ad_conf_order_'.$value]);
422
        }
423
424
        return $output;
425
    }
426
}
427