1 | <?php |
||
31 | abstract class AbstractScanner { |
||
32 | |||
33 | /** |
||
34 | * Scan result |
||
35 | * @var Status |
||
36 | */ |
||
37 | protected $status; |
||
38 | |||
39 | /** |
||
40 | * If scanning was done part by part |
||
41 | * the first detected infected part is stored here |
||
42 | * @var Status |
||
43 | */ |
||
44 | protected $infectedStatus; |
||
45 | |||
46 | /** @var int */ |
||
47 | protected $byteCount; |
||
48 | |||
49 | /** @var resource */ |
||
50 | protected $writeHandle; |
||
51 | |||
52 | /** @var AppConfig */ |
||
53 | protected $appConfig; |
||
54 | |||
55 | /** @var ILogger */ |
||
56 | protected $logger; |
||
57 | |||
58 | /** @var string */ |
||
59 | protected $lastChunk; |
||
60 | |||
61 | /** @var bool */ |
||
62 | protected $isLogUsed = false; |
||
63 | |||
64 | /** @var bool */ |
||
65 | protected $isAborted = false; |
||
66 | |||
67 | /** |
||
68 | * Close used resources |
||
69 | */ |
||
70 | abstract protected function shutdownScanner(); |
||
71 | |||
72 | 12 | public function __construct(AppConfig $config, ILogger $logger){ |
|
76 | |||
77 | 7 | public function getStatus(){ |
|
86 | |||
87 | /** |
||
88 | * Synchronous scan |
||
89 | * @param IScannable $item |
||
90 | * @return Status |
||
91 | */ |
||
92 | 2 | public function scan(IScannable $item) { |
|
102 | |||
103 | /** |
||
104 | * Async scan - new portion of data is available |
||
105 | * @param string $data |
||
106 | */ |
||
107 | 5 | public function onAsyncData($data){ |
|
110 | |||
111 | /** |
||
112 | * Async scan - resource is closed |
||
113 | * @return Status |
||
114 | */ |
||
115 | 5 | public function completeAsyncScan(){ |
|
119 | |||
120 | /** |
||
121 | * Get write handle here. |
||
122 | * Do NOT open connection in constructor because this method is used for reconnection |
||
123 | */ |
||
124 | 9 | public function initScanner(){ |
|
125 | 9 | $this->byteCount = 0; |
|
126 | 9 | if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){ |
|
127 | $this->infectedStatus = clone $this->status; |
||
128 | } |
||
129 | 9 | $this->status = new Status(); |
|
130 | 9 | } |
|
131 | |||
132 | /** |
||
133 | * @param string $chunk |
||
134 | */ |
||
135 | 7 | protected function writeChunk($chunk){ |
|
136 | 7 | $this->fwrite( |
|
137 | 7 | $this->prepareChunk($chunk) |
|
138 | ); |
||
139 | 7 | } |
|
140 | |||
141 | /** |
||
142 | * @param string $data |
||
143 | */ |
||
144 | 7 | protected final function fwrite($data){ |
|
145 | 7 | if ($this->isAborted){ |
|
146 | return; |
||
147 | } |
||
148 | |||
149 | 7 | $dataLength = strlen($data); |
|
150 | 7 | $streamSizeLimit = intval($this->appConfig->getAvStreamMaxLength()); |
|
151 | 7 | if ($this->byteCount + $dataLength > $streamSizeLimit){ |
|
152 | 3 | $this->logger->debug( |
|
153 | 3 | 'reinit scanner', |
|
154 | 3 | ['app' => 'files_antivirus'] |
|
155 | ); |
||
156 | 3 | $this->shutdownScanner(); |
|
157 | 3 | $isReopenSuccessful = $this->retry(); |
|
158 | } else { |
||
159 | 5 | $isReopenSuccessful = true; |
|
160 | } |
||
161 | |||
162 | 7 | if (!$isReopenSuccessful || !$this->writeRaw($data)){ |
|
163 | if (!$this->isLogUsed) { |
||
164 | $this->isLogUsed = true; |
||
165 | $this->logger->warning( |
||
166 | 'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings', |
||
167 | ['app' => 'files_antivirus'] |
||
168 | ); |
||
169 | } |
||
170 | // retry on error |
||
171 | $isRetrySuccessful = $this->retry() && $this->writeRaw($data); |
||
172 | $this->isAborted = !$isRetrySuccessful; |
||
173 | } |
||
174 | 7 | } |
|
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | 3 | protected function retry(){ |
|
186 | |||
187 | /** |
||
188 | * @param $data |
||
189 | * @return bool |
||
190 | */ |
||
191 | 7 | protected function writeRaw($data){ |
|
201 | |||
202 | /** |
||
203 | * Get a resource to write data into |
||
204 | * @return resource |
||
205 | */ |
||
206 | 9 | protected function getWriteHandle(){ |
|
209 | |||
210 | /** |
||
211 | * Prepare chunk (if required) |
||
212 | * @param string $data |
||
213 | * @return string |
||
214 | */ |
||
215 | 3 | protected function prepareChunk($data){ |
|
218 | } |
||
219 |