1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2015 Victor Dubiniuk <[email protected]> |
4
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
5
|
|
|
* later. |
6
|
|
|
* See the COPYING-README file. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OCA\Files_Antivirus; |
10
|
|
|
|
11
|
|
|
use \OCP\IConfig; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @method string getAvMode() |
15
|
|
|
* @method string getAvSocket() |
16
|
|
|
* @method string getAvHost() |
17
|
|
|
* @method int getAvPort() |
18
|
|
|
* @method int getAvMaxFileSize() |
19
|
|
|
* @method string getAvCmdOptions() |
20
|
|
|
* @method string getAvPath() |
21
|
|
|
* @method string getAvInfectedAction() |
22
|
|
|
* |
23
|
|
|
* @method null setAvMode(string $avMode) |
24
|
|
|
* @method null setAvSocket(string $avsocket) |
25
|
|
|
* @method null setAvHost(string $avHost) |
26
|
|
|
* @method null setAvPort(int $avPort) |
27
|
|
|
* @method null setAvMaxFileSize(int $fileSize) |
28
|
|
|
* @method null setAvCmdOptions(string $avCmdOptions) |
29
|
|
|
* @method null setAvChunkSize(int $chunkSize) |
30
|
|
|
* @method null setAvPath(string $avPath) |
31
|
|
|
* @method null setAvInfectedAction(string $avInfectedAction) |
32
|
|
|
*/ |
33
|
|
|
class AppConfig { |
34
|
|
|
private $appName = 'files_antivirus'; |
35
|
|
|
|
36
|
|
|
/** @var IConfig */ |
37
|
|
|
private $config; |
38
|
|
|
|
39
|
|
|
private $defaults = [ |
40
|
|
|
'av_mode' => 'executable', |
41
|
|
|
'av_socket' => '/var/run/clamav/clamd.ctl', |
42
|
|
|
'av_host' => '', |
43
|
|
|
'av_port' => '', |
44
|
|
|
'av_cmd_options' => '', |
45
|
|
|
'av_path' => '/usr/bin/clamscan', |
46
|
|
|
'av_max_file_size' => -1, |
47
|
|
|
'av_stream_max_length' => '26214400', |
48
|
|
|
'av_infected_action' => 'only_log', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* AppConfig constructor. |
53
|
|
|
* |
54
|
|
|
* @param IConfig $config |
55
|
|
|
*/ |
56
|
5 |
|
public function __construct(IConfig $config) { |
57
|
5 |
|
$this->config = $config; |
58
|
5 |
|
} |
59
|
|
|
|
60
|
4 |
|
public function getAvChunkSize(){ |
61
|
|
|
// See http://php.net/manual/en/function.stream-wrapper-register.php#74765 |
62
|
|
|
// and \OC_Helper::streamCopy |
63
|
4 |
|
return 8192; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get full commandline |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
2 |
|
public function getCmdline(){ |
71
|
2 |
|
$avCmdOptions = $this->getAvCmdOptions(); |
72
|
|
|
|
73
|
2 |
|
$shellArgs = []; |
74
|
2 |
|
if ($avCmdOptions) { |
75
|
|
|
$shellArgs = explode(',', $avCmdOptions); |
76
|
|
|
$shellArgs = array_map(function($i){ |
77
|
|
|
return escapeshellarg($i); |
78
|
|
|
}, |
79
|
|
|
$shellArgs |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
$preparedArgs = ''; |
84
|
2 |
|
if (count($shellArgs)){ |
85
|
|
|
$preparedArgs = implode(' ', $shellArgs); |
86
|
|
|
} |
87
|
2 |
|
return $preparedArgs; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get all setting values as an array |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getAllValues() { |
95
|
|
|
$keys = array_keys($this->defaults); |
96
|
|
|
$values = array_map([$this, 'getAppValue'], $keys); |
97
|
|
|
$preparedKeys = array_map([$this, 'camelCase'], $keys); |
98
|
|
|
return array_combine($preparedKeys, $values); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get a value by key |
103
|
|
|
* @param string $key |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
1 |
|
public function getAppValue($key) { |
107
|
1 |
|
$defaultValue = null; |
108
|
1 |
|
if (array_key_exists($key, $this->defaults)){ |
109
|
1 |
|
$defaultValue = $this->defaults[$key]; |
110
|
|
|
} |
111
|
1 |
|
return $this->config->getAppValue($this->appName, $key, $defaultValue); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set a value by key |
116
|
|
|
* @param string $key |
117
|
|
|
* @param string $value |
118
|
|
|
*/ |
119
|
|
|
public function setAppValue($key, $value) { |
120
|
|
|
$this->config->setAppValue($this->appName, $key, $value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set a value with magic __call invocation |
125
|
|
|
* @param string $key |
126
|
|
|
* @param array $args |
127
|
|
|
* @throws \BadFunctionCallException |
128
|
|
|
*/ |
129
|
|
|
protected function setter($key, $args) { |
130
|
|
|
if (array_key_exists($key, $this->defaults)) { |
131
|
|
|
$this->setAppValue($key, $args[0]); |
132
|
|
|
} else { |
133
|
|
|
throw new \BadFunctionCallException($key . ' is not a valid key'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get a value with magic __call invocation |
139
|
|
|
* @param string $key |
140
|
|
|
* @return string |
141
|
|
|
* @throws \BadFunctionCallException |
142
|
|
|
*/ |
143
|
3 |
|
protected function getter($key) { |
144
|
3 |
|
if (array_key_exists($key, $this->defaults)) { |
145
|
3 |
|
return $this->getAppValue($key); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
throw new \BadFunctionCallException($key . ' is not a valid key'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Translates property_name into propertyName |
153
|
|
|
* @param string $property |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
protected function camelCase($property){ |
157
|
|
|
$split = explode('_', $property); |
158
|
|
|
$ucFirst = implode('', array_map('ucfirst', $split)); |
159
|
|
|
return lcfirst($ucFirst); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Does all the someConfig to some_config magic |
164
|
|
|
* @param string $property |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
3 |
|
protected function propertyToKey($property){ |
168
|
3 |
|
$parts = preg_split('/(?=[A-Z])/', $property); |
169
|
3 |
|
$column = null; |
170
|
|
|
|
171
|
3 |
|
foreach($parts as $part){ |
172
|
3 |
|
if($column === null){ |
173
|
3 |
|
$column = $part; |
174
|
|
|
} else { |
175
|
3 |
|
$column .= '_' . lcfirst($part); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
3 |
|
return $column; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get/set an option value by calling getSomeOption method |
184
|
|
|
* @param string $methodName |
185
|
|
|
* @param array $args |
186
|
|
|
* @return string|null |
187
|
|
|
* @throws \BadFunctionCallException |
188
|
|
|
*/ |
189
|
3 |
|
public function __call($methodName, $args){ |
190
|
3 |
|
$attr = lcfirst( substr($methodName, 3) ); |
191
|
3 |
|
$key = $this->propertyToKey($attr); |
192
|
3 |
|
if(strpos($methodName, 'set') === 0){ |
193
|
|
|
$this->setter($key, $args); |
194
|
3 |
|
} elseif(strpos($methodName, 'get') === 0) { |
195
|
3 |
|
return $this->getter($key); |
196
|
|
|
} else { |
197
|
|
|
throw new \BadFunctionCallException($methodName . |
198
|
|
|
' does not exist'); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|