1 | <?php |
||
26 | abstract class Scanner { |
||
27 | |||
28 | /** |
||
29 | * Scan result |
||
30 | * @var \OCA\Files_Antivirus\Status |
||
31 | */ |
||
32 | protected $status; |
||
33 | |||
34 | /** @var int */ |
||
35 | protected $byteCount; |
||
36 | |||
37 | /** @var resource */ |
||
38 | protected $writeHandle; |
||
39 | |||
40 | /** @var \OCA\Files_Antivirus\AppConfig */ |
||
41 | protected $appConfig; |
||
42 | |||
43 | /** @var bool */ |
||
44 | protected $isLogUsed; |
||
45 | |||
46 | /** |
||
47 | * Close used resources |
||
48 | */ |
||
49 | abstract protected function shutdownScanner(); |
||
50 | |||
51 | |||
52 | 3 | public function getStatus(){ |
|
58 | |||
59 | /** |
||
60 | * Synchronous scan |
||
61 | * @param IScannable $item |
||
62 | * @return Status |
||
63 | */ |
||
64 | 3 | public function scan(IScannable $item) { |
|
65 | 3 | $this->initScanner(); |
|
66 | |||
67 | 3 | while (false !== ($chunk = $item->fread())) { |
|
68 | 3 | $this->writeChunk($chunk); |
|
69 | 3 | } |
|
70 | |||
71 | 3 | $this->shutdownScanner(); |
|
72 | 3 | return $this->getStatus(); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Async scan - new portion of data is available |
||
77 | * @param string $data |
||
78 | */ |
||
79 | public function onAsyncData($data){ |
||
82 | |||
83 | /** |
||
84 | * Async scan - resource is closed |
||
85 | * @return Status |
||
86 | */ |
||
87 | public function completeAsyncScan(){ |
||
91 | |||
92 | /** |
||
93 | * Open write handle. etc |
||
94 | */ |
||
95 | 3 | public function initScanner(){ |
|
99 | |||
100 | /** |
||
101 | * @param string $chunk |
||
102 | */ |
||
103 | 3 | protected function writeChunk($chunk){ |
|
104 | 3 | $this->fwrite( |
|
105 | 3 | $this->prepareChunk($chunk) |
|
106 | 3 | ); |
|
107 | 3 | } |
|
108 | |||
109 | /** |
||
110 | * @param string $data |
||
111 | */ |
||
112 | 3 | protected final function fwrite($data){ |
|
113 | 3 | $dataLength = strlen($data); |
|
114 | 3 | $fileSizeLimit = intval($this->appConfig->getAvMaxFileSize()); |
|
115 | 3 | if ($fileSizeLimit !== -1 && $this->byteCount + $dataLength > $fileSizeLimit){ |
|
116 | 3 | $this->shutdownScanner(); |
|
117 | 3 | $this->initScanner(); |
|
118 | 3 | } |
|
119 | |||
120 | 3 | $bytesWritten = @fwrite($this->getWriteHandle(), $data); |
|
121 | 3 | if ($bytesWritten === false || $bytesWritten < $dataLength){ |
|
122 | if (!$this->isLogUsed) { |
||
123 | $this->isLogUsed = true; |
||
124 | \OC::$server->getLogger()->warning( |
||
125 | 'Failed to write a chunk. File is too big?', |
||
126 | ['app' => 'files_antivirus'] |
||
127 | ); |
||
128 | } |
||
129 | // retry |
||
130 | $this->initScanner(); |
||
131 | @fwrite($this->getWriteHandle(), $data); |
||
|
|||
132 | } else { |
||
133 | 3 | $this->byteCount += $bytesWritten; |
|
134 | } |
||
135 | 3 | } |
|
136 | |||
137 | /** |
||
138 | * Get a resource to write data into |
||
139 | * @return resource |
||
140 | */ |
||
141 | 3 | protected function getWriteHandle(){ |
|
144 | |||
145 | /** |
||
146 | * Prepare chunk (if required) |
||
147 | * @param string $data |
||
148 | * @return string |
||
149 | */ |
||
150 | 3 | protected function prepareChunk($data){ |
|
153 | } |
||
154 |
If you suppress an error, we recommend checking for the error condition explicitly: