1 | <?php |
||
26 | abstract class Scanner { |
||
27 | |||
28 | /** |
||
29 | * Scan result |
||
30 | * @var Status |
||
31 | */ |
||
32 | protected $status; |
||
33 | |||
34 | /** |
||
35 | * If scanning was done part by part |
||
36 | * the first detected infected part is stored here |
||
37 | * @var Status |
||
38 | */ |
||
39 | protected $infectedStatus; |
||
40 | |||
41 | /** @var int */ |
||
42 | protected $byteCount; |
||
43 | |||
44 | /** @var resource */ |
||
45 | protected $writeHandle; |
||
46 | |||
47 | /** @var \OCA\Files_Antivirus\AppConfig */ |
||
48 | protected $appConfig; |
||
49 | |||
50 | /** @var string */ |
||
51 | protected $lastChunk; |
||
52 | |||
53 | /** @var bool */ |
||
54 | protected $isLogUsed = false; |
||
55 | |||
56 | /** @var bool */ |
||
57 | protected $isAborted = false; |
||
58 | |||
59 | /** |
||
60 | * Close used resources |
||
61 | */ |
||
62 | abstract protected function shutdownScanner(); |
||
63 | |||
64 | |||
65 | 2 | public function getStatus(){ |
|
66 | 2 | if ($this->infectedStatus instanceof Status){ |
|
67 | return $this->infectedStatus; |
||
68 | } |
||
69 | 2 | if ($this->status instanceof Status){ |
|
70 | 2 | return $this->status; |
|
71 | } |
||
72 | return new Status(); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Synchronous scan |
||
77 | * @param IScannable $item |
||
78 | * @return Status |
||
79 | */ |
||
80 | 2 | public function scan(IScannable $item) { |
|
81 | 2 | $this->initScanner(); |
|
82 | |||
83 | 2 | while (false !== ($chunk = $item->fread())) { |
|
84 | 2 | $this->writeChunk($chunk); |
|
85 | } |
||
86 | |||
87 | 2 | $this->shutdownScanner(); |
|
88 | 2 | return $this->getStatus(); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Async scan - new portion of data is available |
||
93 | * @param string $data |
||
94 | */ |
||
95 | public function onAsyncData($data){ |
||
98 | |||
99 | /** |
||
100 | * Async scan - resource is closed |
||
101 | * @return Status |
||
102 | */ |
||
103 | public function completeAsyncScan(){ |
||
107 | |||
108 | /** |
||
109 | * Open write handle. etc |
||
110 | */ |
||
111 | 2 | public function initScanner(){ |
|
118 | |||
119 | /** |
||
120 | * @param string $chunk |
||
121 | */ |
||
122 | 2 | protected function writeChunk($chunk){ |
|
127 | |||
128 | /** |
||
129 | * @param string $data |
||
130 | */ |
||
131 | 2 | protected final function fwrite($data){ |
|
165 | |||
166 | /** |
||
167 | * @param $data |
||
168 | * @return bool |
||
169 | */ |
||
170 | 2 | protected function writeRaw($data){ |
|
180 | |||
181 | /** |
||
182 | * Get a resource to write data into |
||
183 | * @return resource |
||
184 | */ |
||
185 | 2 | protected function getWriteHandle(){ |
|
188 | |||
189 | /** |
||
190 | * Prepare chunk (if required) |
||
191 | * @param string $data |
||
192 | * @return string |
||
193 | */ |
||
194 | 2 | protected function prepareChunk($data){ |
|
197 | } |
||
198 |