1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014 Viktar Dubiniuk <[email protected]> |
4
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
5
|
|
|
* later. |
6
|
|
|
* See the COPYING-README file. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OCA\Files_Antivirus; |
10
|
|
|
|
11
|
|
|
use OCA\Files_Antivirus\Scanner\InitException; |
12
|
|
|
use \OCP\ILogger; |
13
|
|
|
|
14
|
|
|
class ScannerFactory{ |
15
|
|
|
// We split it in two parts in order to prevent reports from av scanners |
16
|
|
|
const EICAR_PART_1 = 'X5O!P%@AP[4\PZX54(P^)7CC)7}$'; |
17
|
|
|
const EICAR_PART_2 = 'EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \OCA\Files_Antivirus\AppConfig |
21
|
|
|
*/ |
22
|
|
|
protected $appConfig; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ILogger; |
26
|
|
|
*/ |
27
|
|
|
protected $logger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $scannerClass; |
33
|
|
|
|
34
|
15 |
|
public function __construct(AppConfig $appConfig, ILogger $logger) { |
35
|
15 |
|
$this->appConfig = $appConfig; |
36
|
15 |
|
$this->logger = $logger; |
37
|
|
|
try { |
38
|
15 |
|
$this->getScannerClass(); |
39
|
|
|
} catch (InitException $e) { |
40
|
|
|
// rethrow misconfiguration exception |
41
|
|
|
throw $e; |
42
|
|
|
} catch (\Exception $e) { |
43
|
|
|
$message = implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]); |
44
|
|
|
$this->logger->warning($message, ['app' => 'files_antivirus']); |
45
|
|
|
} |
46
|
15 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @throws InitException |
50
|
|
|
*/ |
51
|
15 |
|
protected function getScannerClass() { |
52
|
15 |
|
switch ($this->appConfig->getAvMode()) { |
53
|
15 |
|
case 'daemon': |
54
|
9 |
|
$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Daemon'; |
55
|
9 |
|
break; |
56
|
7 |
|
case 'socket': |
57
|
1 |
|
$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Socket'; |
58
|
1 |
|
break; |
59
|
6 |
|
case 'executable': |
60
|
6 |
|
$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Local'; |
61
|
6 |
|
break; |
62
|
|
|
default: |
63
|
|
|
throw new InitException( |
64
|
|
|
sprintf( |
65
|
|
|
'Please check the settings at the admin page. Invalid mode: "%s"', |
66
|
|
|
$this->appConfig->getAvMode() |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
15 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Produce a scanner instance |
74
|
|
|
* @return \OCA\Files_Antivirus\Scanner\AbstractScanner |
75
|
|
|
*/ |
76
|
8 |
|
public function getScanner() { |
77
|
8 |
|
return new $this->scannerClass($this->appConfig, $this->logger); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param AppConfig $appConfig |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function testConnection(AppConfig $appConfig) { |
85
|
|
|
$this->appConfig = $appConfig; |
86
|
|
|
try { |
87
|
|
|
$scanner = $this->getScanner(); |
88
|
|
|
$item = new Content(self::EICAR_PART_1 . self::EICAR_PART_2, 4096); |
89
|
|
|
$status = $scanner->scan($item); |
90
|
|
|
return $status->getNumericStatus() === Status::SCANRESULT_INFECTED; |
91
|
|
|
} catch (\Exception $e) { |
92
|
|
|
$this->logger->warning($e->getMessage(), ['app' => 'files_antivirus']); |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|