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 AppConfig |
||
30 | */ |
||
31 | protected $appConfig; |
||
32 | |||
33 | /** |
||
34 | * @var \OCA\Files_Antivirus\ScannerFactory |
||
35 | */ |
||
36 | protected $scannerFactory; |
||
37 | |||
38 | /** |
||
39 | * @var IL10N |
||
40 | */ |
||
41 | protected $l10n; |
||
42 | |||
43 | /** |
||
44 | * @var ILogger; |
||
45 | */ |
||
46 | protected $logger; |
||
47 | |||
48 | /** @var RequestHelper */ |
||
49 | protected $requestHelper; |
||
50 | |||
51 | /** |
||
52 | * @param array $parameters |
||
53 | */ |
||
54 | 5 | public function __construct($parameters) { |
|
62 | |||
63 | /** |
||
64 | * Asynchronously scan data that are written to the file |
||
65 | * @param string $path |
||
66 | * @param string $mode |
||
67 | * @return resource | bool |
||
68 | */ |
||
69 | 4 | public function fopen($path, $mode){ |
|
70 | 4 | $stream = $this->storage->fopen($path, $mode); |
|
71 | |||
72 | if ( |
||
73 | 4 | is_resource($stream) |
|
74 | 4 | && $this->isWritingMode($mode) |
|
75 | 4 | && $this->isScannableSize($path) |
|
76 | 4 | ) { |
|
77 | try { |
||
78 | 2 | $scanner = $this->scannerFactory->getScanner(); |
|
79 | 2 | $scanner->initScanner(); |
|
80 | 2 | return CallBackWrapper::wrap( |
|
81 | 2 | $stream, |
|
82 | 2 | null, |
|
83 | function ($data) use ($scanner){ |
||
84 | 2 | $scanner->onAsyncData($data); |
|
85 | 2 | }, |
|
86 | 2 | function () use ($scanner, $path) { |
|
87 | 2 | $status = $scanner->completeAsyncScan(); |
|
88 | 2 | if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){ |
|
89 | //prevent from going to trashbin |
||
90 | 2 | if (App::isEnabled('files_trashbin')) { |
|
91 | 2 | \OCA\Files_Trashbin\Storage::preRenameHook([ |
|
92 | 2 | Filesystem::signal_param_oldpath => '', |
|
93 | 2 | Filesystem::signal_param_newpath => '' |
|
94 | 2 | ]); |
|
95 | 2 | } |
|
96 | |||
97 | 2 | $owner = $this->getOwner($path); |
|
98 | 2 | $this->unlink($path); |
|
99 | |||
100 | 2 | if (App::isEnabled('files_trashbin')) { |
|
101 | 2 | \OCA\Files_Trashbin\Storage::postRenameHook([]); |
|
102 | 2 | } |
|
103 | 2 | $this->logger->warning( |
|
104 | 2 | 'Infected file deleted. ' . $status->getDetails() |
|
105 | 2 | . ' Account: ' . $owner . ' Path: ' . $path, |
|
106 | 2 | ['app' => 'files_antivirus'] |
|
107 | 2 | ); |
|
108 | |||
109 | 2 | \OC::$server->getActivityManager()->publishActivity( |
|
110 | 2 | 'files_antivirus', |
|
111 | 2 | Activity::SUBJECT_VIRUS_DETECTED, |
|
112 | 2 | [$path, $status->getDetails()], |
|
113 | 2 | Activity::MESSAGE_FILE_DELETED, |
|
114 | 2 | [], |
|
115 | 2 | $path, |
|
116 | 2 | '', |
|
117 | 2 | $owner, |
|
118 | 2 | Activity::TYPE_VIRUS_DETECTED, |
|
119 | Activity::PRIORITY_HIGH |
||
120 | 2 | ); |
|
121 | |||
122 | 2 | throw new InvalidContentException( |
|
123 | 2 | $this->l10n->t( |
|
124 | 2 | 'Virus %s is detected in the file. Upload cannot be completed.', |
|
125 | 2 | $status->getDetails() |
|
126 | 2 | ) |
|
127 | 2 | ); |
|
128 | } |
||
129 | } |
||
130 | 2 | ); |
|
131 | } catch (\Exception $e){ |
||
132 | $message = implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]); |
||
133 | $this->logger->warning($message); |
||
134 | } |
||
135 | } |
||
136 | 3 | return $stream; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Checks whether passed mode is suitable for writing |
||
141 | * @param string $mode |
||
142 | * @return bool |
||
143 | */ |
||
144 | 4 | private function isWritingMode($mode){ |
|
153 | |||
154 | /** |
||
155 | * Checks upload size against the av_max_file_size config option |
||
156 | * |
||
157 | * @param string $path |
||
158 | * @return bool |
||
159 | */ |
||
160 | 3 | private function isScannableSize($path) { |
|
186 | } |
||
187 |