|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenVPN; |
|
4
|
|
|
|
|
5
|
|
|
use OpenVPN\Interfaces\ConfigInterface; |
|
6
|
|
|
use OpenVPN\Interfaces\ImportInterface; |
|
7
|
|
|
use function strlen; |
|
8
|
|
|
|
|
9
|
|
|
class Import implements ImportInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Lines of config file |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
public $lines = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Import constructor, can import file on starting |
|
20
|
|
|
* |
|
21
|
|
|
* @param string|null $filename Path to config file |
|
22
|
|
|
* @param bool $isContent If true, then path mean content of config file |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(string $filename = null, bool $isContent = false) |
|
25
|
|
|
{ |
|
26
|
|
|
if ($isContent) { |
|
27
|
|
|
$this->load($filename); |
|
28
|
|
|
} elseif (null !== $filename) { |
|
29
|
|
|
$this->read($filename); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Check if line is valid config line, TRUE if line is okay. |
|
35
|
|
|
* If empty line or line with comment then FALSE. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $line |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
private function isLine(string $line): bool |
|
42
|
|
|
{ |
|
43
|
|
|
return !( |
|
44
|
|
|
// Empty lines |
|
45
|
|
|
preg_match('/^\n+|^[\t\s]*\n+/m', $line) || |
|
46
|
|
|
// Lines with comments |
|
47
|
|
|
preg_match('/^#/m', $line) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function read(string $filename): array |
|
55
|
|
|
{ |
|
56
|
|
|
$content = file_get_contents($filename); |
|
57
|
|
|
|
|
58
|
|
|
return $this->load($content); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritDoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function load($content, string $type = 'raw'): array |
|
65
|
|
|
{ |
|
66
|
|
|
$result = ['total' => 0, 'read' => 0]; |
|
67
|
|
|
$lines = []; |
|
68
|
|
|
|
|
69
|
|
|
if (is_array($content)) { |
|
70
|
|
|
$lines = $content; |
|
71
|
|
|
} elseif ($type === 'raw') { |
|
72
|
|
|
$lines = explode("\n", $content); |
|
73
|
|
|
} elseif ($type === 'json') { |
|
74
|
|
|
$lines = json_decode($content, false); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Read line by line |
|
78
|
|
|
foreach ($lines as $line) { |
|
79
|
|
|
$line = trim($line); |
|
80
|
|
|
// Save line only of not empty |
|
81
|
|
|
if ($this->isLine($line) && strlen($line) > 1) { |
|
82
|
|
|
$line = trim(preg_replace('/\s+/', ' ', $line)); |
|
83
|
|
|
$this->lines[] = $line; |
|
84
|
|
|
$result['read']++; |
|
85
|
|
|
} |
|
86
|
|
|
$result['total']++; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $result; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function parse(): ConfigInterface |
|
96
|
|
|
{ |
|
97
|
|
|
$config = new Config(); |
|
98
|
|
|
|
|
99
|
|
|
array_map( |
|
100
|
|
|
static function ($line) use ($config) { |
|
101
|
|
|
if (preg_match('/^(\S+)( (.*))?/', $line, $matches)) { |
|
102
|
|
|
$config->set($matches[1], $matches[3] ?? true); |
|
103
|
|
|
} |
|
104
|
|
|
}, |
|
105
|
|
|
$this->lines |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return $config; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|