1 | <?php |
||
34 | abstract class AbstractScanner { |
||
35 | |||
36 | /** |
||
37 | * Scan result |
||
38 | * |
||
39 | * @var Status |
||
40 | */ |
||
41 | protected $status; |
||
42 | |||
43 | /** |
||
44 | * If scanning was done part by part |
||
45 | * the first detected infected part is stored here |
||
46 | * |
||
47 | * @var Status |
||
48 | */ |
||
49 | protected $infectedStatus; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $byteCount; |
||
55 | |||
56 | /** |
||
57 | * @var resource |
||
58 | */ |
||
59 | protected $writeHandle; |
||
60 | |||
61 | /** |
||
62 | * @var AppConfig |
||
63 | */ |
||
64 | protected $appConfig; |
||
65 | |||
66 | /** |
||
67 | * @var ILogger |
||
68 | */ |
||
69 | protected $logger; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $lastChunk; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $isLogUsed = false; |
||
80 | |||
81 | /** |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected $isAborted = false; |
||
85 | |||
86 | /** |
||
87 | * Close used resources |
||
88 | */ |
||
89 | abstract protected function shutdownScanner(); |
||
90 | |||
91 | /** |
||
92 | * AbstractScanner constructor. |
||
93 | * |
||
94 | * @param AppConfig $config |
||
95 | * @param ILogger $logger |
||
96 | */ |
||
97 | 12 | public function __construct(AppConfig $config, ILogger $logger) { |
|
101 | |||
102 | /** |
||
103 | * @return Status |
||
104 | */ |
||
105 | 7 | public function getStatus() { |
|
114 | |||
115 | /** |
||
116 | * Synchronous scan |
||
117 | * |
||
118 | * @param IScannable $item |
||
119 | * |
||
120 | * @return Status |
||
121 | */ |
||
122 | 2 | public function scan(IScannable $item) { |
|
123 | 2 | $this->initScanner(); |
|
124 | |||
125 | 2 | while (($chunk = $item->fread()) !== false) { |
|
126 | 2 | $this->writeChunk($chunk); |
|
127 | 2 | } |
|
128 | |||
129 | 2 | $this->shutdownScanner(); |
|
130 | 2 | return $this->getStatus(); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Async scan - new portion of data is available |
||
135 | * |
||
136 | * @param string $data |
||
137 | */ |
||
138 | 5 | public function onAsyncData($data) { |
|
141 | |||
142 | /** |
||
143 | * Async scan - resource is closed |
||
144 | * |
||
145 | * @return Status |
||
146 | */ |
||
147 | 5 | public function completeAsyncScan() { |
|
151 | |||
152 | /** |
||
153 | * Get write handle here. |
||
154 | * Do NOT open connection in constructor because this method |
||
155 | * is used for reconnection |
||
156 | */ |
||
157 | 9 | public function initScanner() { |
|
158 | 9 | $this->byteCount = 0; |
|
159 | 9 | if ($this->status instanceof Status |
|
160 | 9 | && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED |
|
161 | 9 | ) { |
|
162 | $this->infectedStatus = clone $this->status; |
||
163 | } |
||
164 | 9 | $this->status = new Status(); |
|
165 | 9 | } |
|
166 | |||
167 | /** |
||
168 | * @param string $chunk |
||
169 | */ |
||
170 | 7 | protected function writeChunk($chunk) { |
|
171 | 7 | $this->fwrite( |
|
172 | 7 | $this->prepareChunk($chunk) |
|
173 | 7 | ); |
|
174 | 7 | } |
|
175 | |||
176 | /** |
||
177 | * @param string $data |
||
178 | */ |
||
179 | 7 | final protected function fwrite($data) { |
|
180 | 7 | if ($this->isAborted) { |
|
181 | return; |
||
182 | } |
||
183 | |||
184 | 7 | $dataLength = \strlen($data); |
|
185 | 7 | $streamSizeLimit = \intval($this->appConfig->getAvStreamMaxLength()); |
|
186 | 7 | if ($this->byteCount + $dataLength > $streamSizeLimit) { |
|
187 | 3 | $this->logger->debug( |
|
188 | 3 | 'reinit scanner', |
|
189 | 3 | ['app' => 'files_antivirus'] |
|
190 | 3 | ); |
|
191 | 3 | $this->shutdownScanner(); |
|
192 | 3 | $isReopenSuccessful = $this->retry(); |
|
193 | 3 | } else { |
|
194 | 5 | $isReopenSuccessful = true; |
|
195 | } |
||
196 | |||
197 | 7 | if (!$isReopenSuccessful || !$this->writeRaw($data)) { |
|
198 | if (!$this->isLogUsed) { |
||
199 | $this->isLogUsed = true; |
||
200 | $this->logger->warning( |
||
201 | 'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings', |
||
202 | ['app' => 'files_antivirus'] |
||
203 | ); |
||
204 | } |
||
205 | // retry on error |
||
206 | $isRetrySuccessful = $this->retry() && $this->writeRaw($data); |
||
207 | $this->isAborted = !$isRetrySuccessful; |
||
208 | } |
||
209 | 7 | } |
|
210 | |||
211 | /** |
||
212 | * @return bool |
||
213 | */ |
||
214 | 3 | protected function retry() { |
|
221 | |||
222 | /** |
||
223 | * @param string $data |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 7 | protected function writeRaw($data) { |
|
237 | |||
238 | /** |
||
239 | * Get a resource to write data into |
||
240 | * |
||
241 | * @return resource |
||
242 | */ |
||
243 | 9 | protected function getWriteHandle() { |
|
246 | |||
247 | /** |
||
248 | * Prepare chunk (if needed) |
||
249 | * |
||
250 | * @param string $data |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | 3 | protected function prepareChunk($data) { |
|
257 | } |
||
258 |