1 | <?php |
||
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 | 5 | public function __construct(IConfig $config) { |
|
60 | |||
61 | 4 | public function getAvChunkSize(){ |
|
66 | |||
67 | /** |
||
68 | * Get full commandline |
||
69 | * @return string |
||
70 | */ |
||
71 | 2 | public function getCmdline(){ |
|
72 | 2 | $avCmdOptions = $this->getAvCmdOptions(); |
|
73 | |||
74 | 2 | $shellArgs = []; |
|
75 | 2 | if ($avCmdOptions) { |
|
76 | $shellArgs = explode(',', $avCmdOptions); |
||
77 | $shellArgs = array_map(function($i){ |
||
78 | return escapeshellarg($i); |
||
79 | }, |
||
80 | $shellArgs |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | 2 | $preparedArgs = ''; |
|
85 | 2 | if (count($shellArgs)){ |
|
86 | $preparedArgs = implode(' ', $shellArgs); |
||
87 | } |
||
88 | 2 | return $preparedArgs; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get all setting values as an array |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getAllValues() { |
||
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) { |
||
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 | 5 | protected function getter($key) { |
|
151 | |||
152 | /** |
||
153 | * Translates property_name into propertyName |
||
154 | * @param string $property |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function camelCase($property){ |
||
163 | |||
164 | /** |
||
165 | * Does all the someConfig to some_config magic |
||
166 | * @param string $property |
||
167 | * @return string |
||
168 | */ |
||
169 | 5 | protected function propertyToKey($property){ |
|
170 | 5 | $parts = preg_split('/(?=[A-Z])/', $property); |
|
171 | 5 | $column = null; |
|
172 | |||
173 | 5 | foreach($parts as $part){ |
|
174 | 5 | if($column === null){ |
|
175 | 5 | $column = $part; |
|
176 | } else { |
||
177 | 5 | $column .= '_' . lcfirst($part); |
|
178 | } |
||
179 | } |
||
180 | |||
181 | 5 | return $column; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get/set an option value by calling getSomeOption method |
||
186 | * @param string $methodName |
||
187 | * @param array $args |
||
188 | * @return string|null |
||
189 | * @throws \BadFunctionCallException |
||
190 | */ |
||
191 | 5 | public function __call($methodName, $args){ |
|
203 | } |
||
204 |