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