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) { |
|
93 | 2 | $this->initScanner(); |
|
94 | |||
95 | 2 | while (false !== ($chunk = $item->fread())) { |
|
96 | 2 | $this->writeChunk($chunk); |
|
97 | 2 | } |
|
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 | 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(){ |
|
131 | |||
132 | /** |
||
133 | * @param string $chunk |
||
134 | */ |
||
135 | 7 | protected function writeChunk($chunk){ |
|
140 | |||
141 | /** |
||
142 | * @param string $data |
||
143 | */ |
||
144 | 7 | protected final function fwrite($data){ |
|
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 |