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 | 4 | public function __construct(AppConfig $config, ILogger $logger){ |
|
73 | 4 | $this->appConfig = $config; |
|
74 | 4 | $this->logger = $logger; |
|
75 | 4 | } |
|
76 | |||
77 | 4 | public function getStatus(){ |
|
78 | 4 | if ($this->infectedStatus instanceof Status){ |
|
79 | return $this->infectedStatus; |
||
80 | } |
||
81 | 4 | if ($this->status instanceof Status){ |
|
82 | 4 | return $this->status; |
|
83 | } |
||
84 | return new Status(); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Synchronous scan |
||
89 | * @param IScannable $item |
||
90 | * @return Status |
||
91 | */ |
||
92 | 2 | public function scan(IScannable $item) { |
|
93 | 2 | $this->initScanner(); |
|
94 | |||
95 | 2 | while (false !== ($chunk = $item->fread())) { |
|
96 | 2 | $this->writeChunk($chunk); |
|
97 | } |
||
98 | |||
99 | 2 | $this->shutdownScanner(); |
|
100 | 2 | return $this->getStatus(); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Async scan - new portion of data is available |
||
105 | * @param string $data |
||
106 | */ |
||
107 | 2 | public function onAsyncData($data){ |
|
108 | 2 | $this->writeChunk($data); |
|
109 | 2 | } |
|
110 | |||
111 | /** |
||
112 | * Async scan - resource is closed |
||
113 | * @return Status |
||
114 | */ |
||
115 | 2 | public function completeAsyncScan(){ |
|
116 | 2 | $this->shutdownScanner(); |
|
117 | 2 | return $this->getStatus(); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Open write handle. etc |
||
122 | */ |
||
123 | 4 | public function initScanner(){ |
|
124 | 4 | $this->byteCount = 0; |
|
125 | 4 | if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){ |
|
126 | $this->infectedStatus = clone $this->status; |
||
127 | } |
||
128 | 4 | $this->status = new Status(); |
|
129 | 4 | } |
|
130 | |||
131 | /** |
||
132 | * @param string $chunk |
||
133 | */ |
||
134 | 4 | protected function writeChunk($chunk){ |
|
135 | 4 | $this->fwrite( |
|
136 | 4 | $this->prepareChunk($chunk) |
|
137 | ); |
||
138 | 4 | } |
|
139 | |||
140 | /** |
||
141 | * @param string $data |
||
142 | */ |
||
143 | 4 | protected final function fwrite($data){ |
|
144 | 4 | if ($this->isAborted){ |
|
145 | return; |
||
146 | } |
||
147 | |||
148 | 4 | $dataLength = strlen($data); |
|
149 | 4 | $streamSizeLimit = intval($this->appConfig->getAvStreamMaxLength()); |
|
150 | 4 | if ($this->byteCount + $dataLength > $streamSizeLimit){ |
|
151 | 3 | $this->logger->debug( |
|
152 | 3 | 'reinit scanner', |
|
153 | 3 | ['app' => 'files_antivirus'] |
|
154 | ); |
||
155 | 3 | $this->shutdownScanner(); |
|
156 | 3 | $isReopenSuccessful = $this->retry(); |
|
157 | } else { |
||
158 | 2 | $isReopenSuccessful = true; |
|
159 | } |
||
160 | |||
161 | 4 | if (!$isReopenSuccessful || !$this->writeRaw($data)){ |
|
162 | if (!$this->isLogUsed) { |
||
163 | $this->isLogUsed = true; |
||
164 | $this->logger->warning( |
||
165 | 'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings', |
||
166 | ['app' => 'files_antivirus'] |
||
167 | ); |
||
168 | } |
||
169 | // retry on error |
||
170 | $isRetrySuccessful = $this->retry() && $this->writeRaw($data); |
||
171 | $this->isAborted = !$isRetrySuccessful; |
||
172 | } |
||
173 | 4 | } |
|
174 | |||
175 | /** |
||
176 | * @return bool |
||
177 | */ |
||
178 | 3 | protected function retry(){ |
|
179 | 3 | $this->initScanner(); |
|
180 | 3 | if (!is_null($this->lastChunk)) { |
|
181 | 1 | return $this->writeRaw($this->lastChunk); |
|
182 | } |
||
183 | 2 | return true; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param $data |
||
188 | * @return bool |
||
189 | */ |
||
190 | 4 | protected function writeRaw($data){ |
|
191 | 4 | $dataLength = strlen($data); |
|
192 | 4 | $bytesWritten = @fwrite($this->getWriteHandle(), $data); |
|
193 | 4 | if ($bytesWritten === $dataLength){ |
|
194 | 4 | $this->byteCount += $bytesWritten; |
|
195 | 4 | $this->lastChunk = $data; |
|
196 | 4 | return true; |
|
197 | } |
||
198 | return false; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Get a resource to write data into |
||
203 | * @return resource |
||
204 | */ |
||
205 | 4 | protected function getWriteHandle(){ |
|
208 | |||
209 | /** |
||
210 | * Prepare chunk (if required) |
||
211 | * @param string $data |
||
212 | * @return string |
||
213 | */ |
||
214 | 2 | protected function prepareChunk($data){ |
|
215 | 2 | return $data; |
|
216 | } |
||
217 | } |
||
218 |