|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 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; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\IO\IOInterface; |
|
21
|
|
|
use fkooman\IO\IO; |
|
22
|
|
|
|
|
23
|
|
|
class VootToken |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $configDir; |
|
27
|
|
|
|
|
28
|
|
|
/** @var \fkooman\IO\IOInterface */ |
|
29
|
|
|
private $io; |
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
public function __construct($configDir, IOInterface $io = null) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$this->configDir = $configDir; |
|
34
|
|
|
if (is_null($io)) { |
|
35
|
|
|
$io = new IO(); |
|
36
|
|
|
} |
|
37
|
|
|
$this->io = $io; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Check whether a particular user has a VOOT token set. |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
public function getVootToken($userId) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$vootTokenFile = sprintf('%s/%s', $this->configDir, $userId); |
|
46
|
|
|
|
|
47
|
|
|
if ($this->io->isFile($vootTokenFile)) { |
|
48
|
|
|
return $this->io->readFile($vootTokenFile); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set or delete the VOOT token for a particular user. |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function setVootToken($userId, $vootToken) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$vootTokenFile = sprintf('%s/%s', $this->configDir, $userId); |
|
60
|
|
|
|
|
61
|
|
|
if (false === $vootToken) { |
|
62
|
|
|
// remove the token |
|
63
|
|
|
if ($this->io->isFile($vootTokenFile)) { |
|
64
|
|
|
$this->io->deleteFile($vootTokenFile); |
|
65
|
|
|
|
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// set the token, if it is already set override |
|
73
|
|
|
$this->io->writeFile($vootTokenFile, $vootToken, true); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
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.