1 | <?php |
||
20 | class AvirWrapper extends Wrapper{ |
||
21 | |||
22 | /** |
||
23 | * Modes that are used for writing |
||
24 | * @var array |
||
25 | */ |
||
26 | private $writingModes = array('r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'); |
||
27 | |||
28 | /** |
||
29 | * @var \OCA\Files_Antivirus\ScannerFactory |
||
30 | */ |
||
31 | protected $scannerFactory; |
||
32 | |||
33 | /** |
||
34 | * @var IL10N |
||
35 | */ |
||
36 | protected $l10n; |
||
37 | |||
38 | /** |
||
39 | * @var ILogger; |
||
40 | */ |
||
41 | protected $logger; |
||
42 | |||
43 | /** |
||
44 | * @param array $parameters |
||
45 | */ |
||
46 | 4 | public function __construct($parameters) { |
|
52 | |||
53 | /** |
||
54 | * Asynchronously scan data that are written to the file |
||
55 | * @param string $path |
||
56 | * @param string $mode |
||
57 | * @return resource | bool |
||
58 | */ |
||
59 | 3 | public function fopen($path, $mode){ |
|
60 | 3 | $stream = $this->storage->fopen($path, $mode); |
|
61 | 3 | if (is_resource($stream) && $this->isWritingMode($mode)) { |
|
62 | try { |
||
63 | 2 | $scanner = $this->scannerFactory->getScanner(); |
|
64 | 2 | $scanner->initScanner(); |
|
65 | 1 | return CallBackWrapper::wrap( |
|
66 | $stream, |
||
67 | 1 | null, |
|
68 | function ($data) use ($scanner){ |
||
69 | 1 | $scanner->onAsyncData($data); |
|
70 | 1 | }, |
|
71 | 1 | function () use ($scanner, $path) { |
|
72 | 1 | $status = $scanner->completeAsyncScan(); |
|
73 | 1 | if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){ |
|
74 | //prevent from going to trashbin |
||
75 | 1 | if (App::isEnabled('files_trashbin')) { |
|
76 | 1 | \OCA\Files_Trashbin\Storage::preRenameHook([]); |
|
77 | } |
||
78 | |||
79 | 1 | $owner = $this->getOwner($path); |
|
80 | 1 | $this->unlink($path); |
|
81 | |||
82 | 1 | if (App::isEnabled('files_trashbin')) { |
|
83 | 1 | \OCA\Files_Trashbin\Storage::postRenameHook([]); |
|
84 | } |
||
85 | |||
86 | 1 | \OC::$server->getActivityManager()->publishActivity( |
|
87 | 1 | 'files_antivirus', |
|
88 | 1 | Activity::SUBJECT_VIRUS_DETECTED, |
|
89 | 1 | [$path, $status->getDetails()], |
|
90 | 1 | Activity::MESSAGE_FILE_DELETED, |
|
91 | 1 | [], |
|
92 | $path, |
||
93 | 1 | '', |
|
94 | $owner, |
||
95 | 1 | Activity::TYPE_VIRUS_DETECTED, |
|
96 | 1 | Activity::PRIORITY_HIGH |
|
97 | ); |
||
98 | |||
99 | 1 | throw new InvalidContentException( |
|
100 | 1 | $this->l10n->t( |
|
101 | 1 | 'Virus %s is detected in the file. Upload cannot be completed.', |
|
102 | 1 | $status->getDetails() |
|
103 | ) |
||
104 | ); |
||
105 | } |
||
106 | 1 | } |
|
107 | ); |
||
108 | 2 | } catch (\Exception $e){ |
|
109 | 2 | $message = implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]); |
|
110 | 2 | $this->logger->warning($message); |
|
111 | } |
||
112 | } |
||
113 | 3 | return $stream; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Checks whether passed mode is suitable for writing |
||
118 | * @param string $mode |
||
119 | * @return bool |
||
120 | */ |
||
121 | 3 | private function isWritingMode($mode){ |
|
130 | } |
||
131 |