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 — master ( 6a1128...a558d1 )
by François
03:40
created

FileConfigStorage::getUserConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * Copyright 2015 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace fkooman\VPN\Server\Config;
19
20
use RuntimeException;
21
use fkooman\Json\Json;
22
23
class FileConfigStorage implements ConfigStorageInterface
24
{
25
    /** @var string */
26
    private $usersConfigDir;
27
28
    /** @var string */
29
    private $commonNamesConfigDir;
30
31
    public function __construct($configDir)
32
    {
33
        $this->usersConfigDir = sprintf('%s/users', $configDir);
34
        $this->commonNamesConfigDir = sprintf('%s/common_names', $configDir);
35
    }
36
37
    /**
38
     * Get configuration specific to a particular user.
39
     *
40
     * @return UserConfig
41
     */
42
    public function getUserConfig($userId)
43
    {
44
        $userData = $this->readFile(
45
            sprintf('%s/%s', $usersConfigDir, $userId)
0 ignored issues
show
Bug introduced by
The variable $usersConfigDir does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
46
        );
47
48
        return new UserConfig($userData);
49
    }
50
51
    /**
52
     * Set the configuration for a particular user.
53
     */
54
    public function setUserConfig($userId, UserConfig $userConfig)
55
    {
56
        $this->writeFile(
57
            sprintf('%s/%s', $this->usersConfigDir, $userId),
58
            $userConfig->toArray()
59
        );
60
    }
61
62
    /**
63
     * Get the configuration for a particular common name.
64
     *
65
     * @return CommonNameConfig
66
     */
67
    public function getCommonNameConfig($commonName)
68
    {
69
        $commonNameData = $this->readFile(
70
            sprintf('%s/%s', $this->commonNamesConfigDir, $commonName)
71
        );
72
73
        return new CommonNameConfig($commonNameData);
74
    }
75
76
    public function getAllCommonNameConfig($userId)
77
    {
78
        $configArray = [];
79
        $pathFilter = sprintf('%s/*', $this->commonNamesConfigDir);
80
        if (!is_null($userId)) {
81
            $pathFilter = sprintf('%s/%s_*', $this->commonNamesConfigDir, $userId);
82
        }
83
        foreach (glob($pathFilter) as $commonNamePath) {
0 ignored issues
show
Security File Exposure introduced by
$pathFilter can contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
84
            $commonName = basename($commonNamePath);
85
            $configArray[$commonName] = $this->getCommonNameConfig($commonName)->toArray();
86
        }
87
88
        return $configArray;
89
    }
90
91
    /** 
92
     * Set the configuration for a particular common name.
93
     */
94
    public function setCommonNameConfig($commonName, CommonNameConfig $commonNameConfig)
95
    {
96
        $this->writeFile(
97
            sprintf('%s/%s', $this->commonNamesConfigDir, $commonName),
98
            $commonNameConfig->toArray()
99
        );
100
    }
101
102
    private function readFile($fileName)
103
    {
104
        if (false === $fileContent = @file_get_contents($fileName)) {
105
            return [];
106
        }
107
108
        return Json::decode($fileContent);
109
    }
110
111
    private function writeFile($fileName, array $fileContent)
112
    {
113
        self::checkMakeDirectory(dirname($fileName));
114
        if (false === @file_put_contents($fileName, Json::encode($fileContent))) {
115
            throw new RuntimeException(sprintf('unable to write file "%s"', $fileName));
116
        }
117
    }
118
119
    private static function checkMakeDirectory($dirName)
120
    {
121
        if (!is_dir($dirName)) {
122
            if (false === @mkdir($dirName, 0755, true)) {
123
                throw new RuntimeException('unable to create directory "%s"', $dirName);
124
            }
125
        }
126
    }
127
}
128