/** * Asynchronously scan data that are written to the file * @param string $path * @param string $mode * @return resource | bool */ public function fopen($path, $mode) { $stream = $this->storage->fopen($path, $mode); if (is_resource($stream) && $this->isWritingMode($mode)) { try { $scanner = $this->scannerFactory->getScanner(); $scanner->initAsyncScan(); return CallBackWrapper::wrap($stream, null, function ($data) use($scanner) { $scanner->onAsyncData($data); }, function () use($scanner, $path) { $status = $scanner->completeAsyncScan(); if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED) { //prevent from going to trashbin if (App::isEnabled('files_trashbin')) { \OCA\Files_Trashbin\Storage::preRenameHook([]); } $owner = $this->getOwner($path); $this->unlink($path); if (App::isEnabled('files_trashbin')) { \OCA\Files_Trashbin\Storage::postRenameHook([]); } \OC::$server->getActivityManager()->publishActivity('files_antivirus', Activity::SUBJECT_VIRUS_DETECTED, [$path, $status->getDetails()], Activity::MESSAGE_FILE_DELETED, [], $path, '', $owner, Activity::TYPE_VIRUS_DETECTED, Activity::PRIORITY_HIGH); throw new InvalidContentException($this->l10n->t('Virus %s is detected in the file. Upload cannot be completed.', $status->getDetails())); } }); } catch (\Exception $e) { $message = implode(' ', [__CLASS__, __METHOD__, $e->getMessage()]); $this->logger->warning($message); } } return $stream; }
/** * Asynchronously scan data that are written to the file * @param string $path * @param string $mode * @return resource | bool */ public function fopen($path, $mode) { $stream = $this->storage->fopen($path, $mode); if (is_resource($stream) && $this->isWritingMode($mode)) { try { $scanner = $this->scannerFactory->getScanner(); $scanner->initAsyncScan(); return CallBackWrapper::wrap($stream, null, function ($data) use($scanner) { $scanner->onAsyncData($data); }, function () use($scanner, $path) { $status = $scanner->completeAsyncScan(); if ($status->getNumericStatus() == \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED) { $this->unlink($path); throw new InvalidContentException($this->l10n->t('Virus %s is detected in the file. Upload cannot be completed.', $status->getDetails())); } }); } catch (\Exception $e) { $message = implode(' ', [__CLASS__, __METHOD__, $e->getMessage()]); $this->logger->warning($message); } } return $stream; }
/** * @param string $path * @param string $mode * @return resource */ public function fopen($path, $mode) { $fullPath = $this->buildPath($path); try { switch ($mode) { case 'r': case 'rb': if (!$this->file_exists($path)) { return false; } return $this->share->read($fullPath); case 'w': case 'wb': return $this->share->write($fullPath); case 'a': case 'ab': case 'r+': case 'w+': case 'wb+': case 'a+': case 'x': case 'x+': case 'c': case 'c+': //emulate these if (strrpos($path, '.') !== false) { $ext = substr($path, strrpos($path, '.')); } else { $ext = ''; } if ($this->file_exists($path)) { if (!$this->isUpdatable($path)) { return false; } $tmpFile = $this->getCachedFile($path); } else { if (!$this->isCreatable(dirname($path))) { return false; } $tmpFile = \OCP\Files::tmpFile($ext); } $source = fopen($tmpFile, $mode); $share = $this->share; return CallBackWrapper::wrap($source, null, null, function () use($tmpFile, $fullPath, $share) { $share->put($tmpFile, $fullPath); unlink($tmpFile); }); } return false; } catch (NotFoundException $e) { return false; } }