1 | <?php |
||
26 | abstract class Scanner { |
||
27 | |||
28 | /** |
||
29 | * Scan result |
||
30 | * @var \OCA\Files_Antivirus\Status |
||
31 | */ |
||
32 | protected $status; |
||
33 | |||
34 | /** |
||
35 | * @var \OCA\Files_Antivirus\AppConfig |
||
36 | */ |
||
37 | protected $appConfig; |
||
38 | |||
39 | /** |
||
40 | * Close used resources |
||
41 | */ |
||
42 | abstract protected function shutdownScanner(); |
||
43 | |||
44 | /** |
||
45 | * Get a resource to write data into |
||
46 | */ |
||
47 | abstract protected function getWriteHandle(); |
||
48 | |||
49 | 3 | public function getStatus(){ |
|
50 | 3 | if ($this->status instanceof Status){ |
|
51 | 3 | return $this->status; |
|
52 | } |
||
53 | return new Status(); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Synchronous scan |
||
58 | * @param IScannable $item |
||
59 | * @return Status |
||
60 | */ |
||
61 | 3 | public function scan(IScannable $item) { |
|
62 | 3 | $this->initScanner(); |
|
63 | |||
64 | 3 | while (false !== ($chunk = $item->fread())) { |
|
65 | 3 | fwrite( |
|
66 | 3 | $this->getWriteHandle(), |
|
67 | 3 | $this->prepareChunk($chunk) |
|
68 | 3 | ); |
|
69 | 3 | } |
|
70 | |||
71 | 3 | $this->shutdownScanner(); |
|
72 | 3 | return $this->getStatus(); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Async scan - prepare resources |
||
77 | */ |
||
78 | public function initAsyncScan(){ |
||
81 | |||
82 | /** |
||
83 | * Async scan - new portion of data is available |
||
84 | * @param string $data |
||
85 | */ |
||
86 | public function onAsyncData($data){ |
||
87 | fwrite( |
||
88 | $this->getWriteHandle(), |
||
89 | $this->prepareChunk($data) |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Async scan - resource is closed |
||
95 | * @return Status |
||
96 | */ |
||
97 | public function completeAsyncScan(){ |
||
101 | |||
102 | /** |
||
103 | * Open write handle. etc |
||
104 | */ |
||
105 | 3 | protected function initScanner(){ |
|
108 | |||
109 | /** |
||
110 | * Prepare chunk (if required) |
||
111 | */ |
||
112 | protected function prepareChunk($data){ |
||
115 | } |
||
116 |