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