|
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) |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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
|
|
|
|
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.