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