1 | <?php |
||
16 | class Status { |
||
17 | |||
18 | /* |
||
19 | * The file was not checked (e.g. because the AV daemon wasn't running). |
||
20 | */ |
||
21 | const SCANRESULT_UNCHECKED = -1; |
||
22 | |||
23 | /* |
||
24 | * The file was checked and found to be clean. |
||
25 | */ |
||
26 | const SCANRESULT_CLEAN = 0; |
||
27 | |||
28 | /* |
||
29 | * The file was checked and found to be infected. |
||
30 | */ |
||
31 | const SCANRESULT_INFECTED = 1; |
||
32 | |||
33 | /* |
||
34 | * Should be SCANRESULT_UNCHECKED | SCANRESULT_INFECTED | SCANRESULT_CLEAN |
||
35 | */ |
||
36 | protected $numericStatus; |
||
37 | |||
38 | /* |
||
39 | * Virus name or error message |
||
40 | */ |
||
41 | protected $details = ""; |
||
42 | |||
43 | protected $ruleMapper; |
||
44 | |||
45 | 10 | public function __construct() { |
|
49 | |||
50 | /** |
||
51 | * Get scan status as integer |
||
52 | * |
||
53 | * @return int |
||
54 | */ |
||
55 | 8 | public function getNumericStatus() { |
|
58 | |||
59 | /** |
||
60 | * Get scan status as string |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 4 | public function getDetails() { |
|
67 | |||
68 | /** |
||
69 | * @param string $rawResponse |
||
70 | * @param integer $result |
||
71 | */ |
||
72 | 8 | public function parseResponse($rawResponse, $result = null) { |
|
73 | 8 | $matches = []; |
|
74 | 8 | $ruleMapper = new Db\RuleMapper(\OC::$server->getDb()); |
|
75 | 8 | if ($result === null) { // Daemon or socket mode |
|
76 | try { |
||
77 | 5 | $allRules = $this->getResponseRules(); |
|
78 | 5 | } catch (\Exception $e) { |
|
79 | \OCP\Util::writeLog( |
||
80 | 'files_antivirus', |
||
81 | __METHOD__.', exception: '.$e->getMessage(), |
||
82 | \OCP\Util::ERROR |
||
83 | ); |
||
84 | return; |
||
85 | } |
||
86 | |||
87 | 5 | $isMatched = false; |
|
88 | 5 | foreach ($allRules as $rule) { |
|
89 | 5 | if (\preg_match($rule->getMatch(), $rawResponse, $matches)) { |
|
90 | 4 | $isMatched = true; |
|
91 | 4 | $this->numericStatus = \intval($rule->getStatus()); |
|
92 | 4 | if (\intval($rule->getStatus()) === self::SCANRESULT_CLEAN) { |
|
93 | 1 | $this->details = ''; |
|
94 | 1 | } else { |
|
95 | 4 | $this->details = isset($matches[1]) ? $matches[1] : 'unknown'; |
|
96 | } |
||
97 | 4 | break; |
|
98 | } |
||
99 | 5 | } |
|
100 | |||
101 | 5 | if (!$isMatched) { |
|
102 | 3 | $this->numericStatus = self::SCANRESULT_UNCHECKED; |
|
103 | 3 | $this->details = 'No matching rules. Please check antivirus rules.'; |
|
104 | 3 | } |
|
105 | 5 | } else { // Executable mode |
|
106 | 4 | $scanStatus = $ruleMapper->findByResult($result); |
|
107 | 4 | if (\is_array($scanStatus) && \count($scanStatus)) { |
|
108 | 4 | $this->numericStatus = \intval($scanStatus[0]->getStatus()); |
|
109 | 4 | $this->details = $scanStatus[0]->getDescription(); |
|
110 | 4 | } |
|
111 | |||
112 | 4 | switch ($this->numericStatus) { |
|
113 | 4 | case self::SCANRESULT_INFECTED: |
|
114 | 2 | $report = []; |
|
115 | 2 | $rawResponse = \explode("\n", $rawResponse); |
|
116 | |||
117 | 2 | foreach ($rawResponse as $line) { |
|
118 | 2 | if (\preg_match('/.*: (.*) FOUND\s*$/', $line, $matches)) { |
|
119 | 2 | $report[] = $matches[1]; |
|
120 | 2 | } |
|
121 | 2 | } |
|
122 | 2 | $this->details = \implode(', ', $report); |
|
123 | |||
124 | 2 | break; |
|
125 | 4 | case self::SCANRESULT_UNCHECKED: |
|
126 | 1 | if (!$this->details) { |
|
127 | $this->details = 'No matching rule for exit code ' . $this->numericStatus .'. Please check antivirus rules configuration.' ; |
||
128 | } |
||
129 | 4 | } |
|
130 | } |
||
131 | 8 | } |
|
132 | |||
133 | 5 | protected function getResponseRules() { |
|
146 | |||
147 | public function dispatch($item, $isBackground = false) { |
||
160 | } |
||
161 |