1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ownCloud - files_antivirus |
5
|
|
|
* |
6
|
|
|
* @author Manuel Deglado |
7
|
|
|
* @copyright 2012 Manuel Deglado [email protected] |
8
|
|
|
* |
9
|
|
|
* This library is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
11
|
|
|
* License as published by the Free Software Foundation; either |
12
|
|
|
* version 3 of the License, or any later version. |
13
|
|
|
* |
14
|
|
|
* This library is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public |
20
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Files_Antivirus; |
25
|
|
|
|
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
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Close used resources |
46
|
|
|
*/ |
47
|
|
|
abstract protected function shutdownScanner(); |
48
|
|
|
|
49
|
|
|
|
50
|
2 |
|
public function getStatus(){ |
51
|
2 |
|
if ($this->status instanceof Status){ |
52
|
2 |
|
return $this->status; |
53
|
|
|
} |
54
|
|
|
return new Status(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Synchronous scan |
59
|
|
|
* @param IScannable $item |
60
|
|
|
* @return Status |
61
|
|
|
*/ |
62
|
2 |
|
public function scan(IScannable $item) { |
63
|
2 |
|
$this->initScanner(); |
64
|
|
|
|
65
|
2 |
|
while (false !== ($chunk = $item->fread())) { |
66
|
2 |
|
$this->writeChunk($chunk); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
$this->shutdownScanner(); |
70
|
2 |
|
return $this->getStatus(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Async scan - prepare resources |
75
|
|
|
*/ |
76
|
|
|
public function initAsyncScan(){ |
77
|
|
|
$this->initScanner(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Async scan - new portion of data is available |
82
|
|
|
* @param string $data |
83
|
|
|
*/ |
84
|
|
|
public function onAsyncData($data){ |
85
|
|
|
$this->writeChunk($data); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Async scan - resource is closed |
90
|
|
|
* @return Status |
91
|
|
|
*/ |
92
|
|
|
public function completeAsyncScan(){ |
93
|
|
|
$this->shutdownScanner(); |
94
|
|
|
return $this->getStatus(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Open write handle. etc |
99
|
|
|
*/ |
100
|
2 |
|
protected function initScanner(){ |
101
|
2 |
|
$this->status = new Status(); |
102
|
2 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $chunk |
106
|
|
|
*/ |
107
|
2 |
|
protected function writeChunk($chunk){ |
108
|
2 |
|
$this->fwrite( |
109
|
2 |
|
$this->prepareChunk($chunk) |
110
|
|
|
); |
111
|
2 |
|
} |
112
|
|
|
|
113
|
2 |
|
protected function fwrite($data){ |
114
|
2 |
|
$bytesWritten = @fwrite($this->getWriteHandle(), $data); |
|
|
|
|
115
|
2 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get a resource to write data into |
119
|
|
|
* @return resource |
120
|
|
|
*/ |
121
|
2 |
|
protected function getWriteHandle(){ |
122
|
2 |
|
return $this->writeHandle; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Prepare chunk (if required) |
127
|
|
|
*/ |
128
|
2 |
|
protected function prepareChunk($data){ |
129
|
2 |
|
return $data; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.