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 ( a07628...6a1128 )
by François
04:21
created

FileConfigStorage::setUserConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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 $configDir;
27
28
    public function __construct($configDir)
29
    {
30
        $this->configDir = $configDir;
31
    }
32
33
    /**
34
     * Get configuration specific to a particular user.
35
     *
36
     * @return UserConfig
37
     */
38
    public function getUserConfig($userId)
39
    {
40
        $userData = $this->readFile(
41
            sprintf('%s/users/%s', $this->configDir, $userId)
42
        );
43
44
        return new UserConfig($userData);
45
    }
46
47
    /**
48
     * Set the configuration for a particular user.
49
     */
50
    public function setUserConfig($userId, UserConfig $userConfig)
51
    {
52
        $this->writeFile(
53
            sprintf('%s/users/%s', $this->configDir, $userId),
54
            $userConfig->toArray()
55
        );
56
    }
57
58
    /**
59
     * Get the configuration for a particular common name.
60
     *
61
     * @return CommonNameConfig
62
     */
63
    public function getCommonNameConfig($commonName)
64
    {
65
        $commonNameData = $this->readFile(
66
            sprintf('%s/common_names/%s', $this->configDir, $commonName)
67
        );
68
69
        return new CommonNameConfig($commonNameData);
70
    }
71
72
    public function getAllCommonNameConfig($userId)
73
    {
74
        $configArray = [];
75
        $pathFilter = sprintf('%s/common_names/*', $this->configDir);
76
        if (!is_null($userId)) {
77
            $pathFilter = sprintf('%s/common_names/%s_*', $this->configDir, $userId);
78
        }
79
        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...
80
            $commonName = basename($commonNamePath);
81
            $configArray[$commonName] = $this->getCommonNameConfig($commonName)->toArray();
82
        }
83
84
        return $configArray;
85
    }
86
87
    /** 
88
     * Set the configuration for a particular common name.
89
     */
90
    public function setCommonNameConfig($commonName, CommonNameConfig $commonNameConfig)
91
    {
92
        $this->writeFile(
93
            sprintf('%s/common_names/%s', $this->configDir, $commonName),
94
            $commonNameConfig->toArray()
95
        );
96
    }
97
98
    private function readFile($fileName)
99
    {
100
        if (false === $fileContent = @file_get_contents($fileName)) {
101
            return [];
102
        }
103
104
        return Json::decode($fileContent);
105
    }
106
107
    private function writeFile($fileName, array $fileContent)
108
    {
109
        if (false === @file_put_contents($fileName, Json::encode($fileContent))) {
110
            throw new RuntimeException(sprintf('unable to write file "%s"', $fileName));
111
        }
112
    }
113
}
114