1 | <?php |
||
32 | abstract class ScannerBase { |
||
33 | |||
34 | /** |
||
35 | * Scan result |
||
36 | * @var Status |
||
37 | */ |
||
38 | protected $status; |
||
39 | |||
40 | /** |
||
41 | * If scanning was done part by part |
||
42 | * the first detected infected part is stored here |
||
43 | * @var Status |
||
44 | */ |
||
45 | protected $infectedStatus; |
||
46 | |||
47 | /** @var int */ |
||
48 | protected $byteCount; |
||
49 | |||
50 | /** @var resource */ |
||
51 | protected $writeHandle; |
||
52 | |||
53 | /** @var AppConfig */ |
||
54 | protected $appConfig; |
||
55 | |||
56 | /** @var ILogger */ |
||
57 | protected $logger; |
||
58 | |||
59 | /** @var StatusFactory */ |
||
60 | protected $statusFactory; |
||
61 | |||
62 | /** @var string */ |
||
63 | protected $lastChunk; |
||
64 | |||
65 | /** @var bool */ |
||
66 | protected $isLogUsed = false; |
||
67 | |||
68 | /** @var bool */ |
||
69 | protected $isAborted = false; |
||
70 | |||
71 | /** |
||
72 | * ScannerBase constructor. |
||
73 | * |
||
74 | * @param AppConfig $config |
||
75 | * @param ILogger $logger |
||
76 | * @param StatusFactory $statusFactory |
||
77 | */ |
||
78 | public function __construct(AppConfig $config, ILogger $logger, StatusFactory $statusFactory) { |
||
79 | $this->appConfig = $config; |
||
80 | $this->logger = $logger; |
||
81 | $this->statusFactory = $statusFactory; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Close used resources |
||
86 | */ |
||
87 | abstract protected function shutdownScanner(); |
||
88 | |||
89 | |||
90 | public function getStatus(){ |
||
91 | if ($this->infectedStatus instanceof Status){ |
||
92 | return $this->infectedStatus; |
||
93 | } |
||
94 | if ($this->status instanceof Status){ |
||
95 | return $this->status; |
||
96 | } |
||
97 | return $this->statusFactory->newStatus(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Synchronous scan |
||
102 | * @param Item $item |
||
103 | * @return Status |
||
104 | */ |
||
105 | public function scan(Item $item) { |
||
106 | $this->initScanner(); |
||
107 | |||
108 | while (false !== ($chunk = $item->fread())) { |
||
109 | $this->writeChunk($chunk); |
||
110 | } |
||
111 | |||
112 | $this->shutdownScanner(); |
||
113 | return $this->getStatus(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Async scan - new portion of data is available |
||
118 | * @param string $data |
||
119 | */ |
||
120 | public function onAsyncData($data){ |
||
121 | $this->writeChunk($data); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Async scan - resource is closed |
||
126 | * @return Status |
||
127 | */ |
||
128 | public function completeAsyncScan(){ |
||
129 | $this->shutdownScanner(); |
||
130 | return $this->getStatus(); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Open write handle. etc |
||
135 | */ |
||
136 | public function initScanner(){ |
||
137 | $this->byteCount = 0; |
||
138 | if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){ |
||
139 | $this->infectedStatus = clone $this->status; |
||
140 | } |
||
141 | $this->status = $this->statusFactory->newStatus(); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param string $chunk |
||
146 | */ |
||
147 | protected function writeChunk($chunk){ |
||
148 | $this->fwrite( |
||
149 | $this->prepareChunk($chunk) |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param string $data |
||
155 | */ |
||
156 | final protected function fwrite($data){ |
||
157 | if ($this->isAborted){ |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | $dataLength = strlen($data); |
||
162 | $streamSizeLimit = (int)$this->appConfig->getAvStreamMaxLength(); |
||
|
|||
163 | if ($this->byteCount + $dataLength > $streamSizeLimit){ |
||
164 | \OC::$server->getLogger()->debug( |
||
165 | 'reinit scanner', |
||
166 | ['app' => 'files_antivirus'] |
||
167 | ); |
||
168 | $this->shutdownScanner(); |
||
169 | $isReopenSuccessful = $this->retry(); |
||
170 | } else { |
||
171 | $isReopenSuccessful = true; |
||
172 | } |
||
173 | |||
174 | if (!$isReopenSuccessful || !$this->writeRaw($data)){ |
||
175 | if (!$this->isLogUsed) { |
||
176 | $this->isLogUsed = true; |
||
177 | \OC::$server->getLogger()->warning( |
||
178 | 'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings', |
||
179 | ['app' => 'files_antivirus'] |
||
180 | ); |
||
181 | } |
||
182 | // retry on error |
||
183 | $isRetrySuccessful = $this->retry() && $this->writeRaw($data); |
||
184 | $this->isAborted = !$isRetrySuccessful; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return bool |
||
190 | */ |
||
191 | protected function retry(){ |
||
192 | $this->initScanner(); |
||
193 | if (!is_null($this->lastChunk)) { |
||
194 | return $this->writeRaw($this->lastChunk); |
||
195 | } |
||
196 | return true; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param $data |
||
201 | * @return bool |
||
202 | */ |
||
203 | protected function writeRaw($data){ |
||
204 | $dataLength = strlen($data); |
||
205 | $bytesWritten = @fwrite($this->getWriteHandle(), $data); |
||
206 | if ($bytesWritten === $dataLength){ |
||
207 | $this->byteCount += $bytesWritten; |
||
208 | $this->lastChunk = $data; |
||
209 | return true; |
||
210 | } |
||
211 | return false; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get a resource to write data into |
||
216 | * @return resource |
||
217 | */ |
||
218 | protected function getWriteHandle(){ |
||
221 | |||
222 | /** |
||
223 | * Prepare chunk (if required) |
||
224 | * @param string $data |
||
225 | * @return string |
||
226 | */ |
||
227 | protected function prepareChunk($data){ |
||
230 | } |
||
231 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: