/** * 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; }
/** * Background scanner main job * @return null */ public function run() { if (!$this->initFS()) { return; } // locate files that are not checked yet $dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); try { $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $qb->select(['fc.fileid'])->from('filecache', 'fc')->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))->innerJoin('fc', 'storages', 'ss', $qb->expr()->andX($qb->expr()->eq('fc.storage', 'ss.numeric_id'), $qb->expr()->orX($qb->expr()->like('ss.id', $qb->expr()->literal('local::%')), $qb->expr()->like('ss.id', $qb->expr()->literal('home::%')))))->where($qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId)))->andWhere($qb->expr()->orX($qb->expr()->isNull('fa.fileid'), $qb->expr()->gt('fc.mtime', 'fa.check_time')))->andWhere($qb->expr()->like('fc.path', $qb->expr()->literal('files/%')))->andWhere($qb->expr()->neq('fc.size', '0'))->setMaxResults(5); $result = $qb->execute(); } catch (\Exception $e) { \OC::$server->getLogger()->error(__METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); return; } $view = new \OC\Files\View(''); try { while ($row = $result->fetch()) { $path = $view->getPath($row['fileid']); if (!is_null($path)) { $item = new Item($this->l10n, $view, $path, $row['fileid']); $scanner = $this->scannerFactory->getScanner(); $status = $scanner->scan($item); $status->dispatch($item, true); } } } catch (\Exception $e) { \OC::$server->getLogger()->error(__METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); } \OC_Util::tearDownFS(); }
/** * Background scanner main job * @return null */ public function run() { if (!$this->initFS()) { return; } // locate files that are not checked yet $dirMimetypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); $sql = 'SELECT `*PREFIX*filecache`.`fileid`, `*PREFIX*storages`.*' . ' FROM `*PREFIX*filecache`' . ' LEFT JOIN `*PREFIX*files_antivirus` ON `*PREFIX*files_antivirus`.`fileid` = `*PREFIX*filecache`.`fileid`' . ' JOIN `*PREFIX*storages` ON `*PREFIX*storages`.`numeric_id` = `*PREFIX*filecache`.`storage`' . ' WHERE `mimetype` != ?' . ' AND (`*PREFIX*storages`.`id` LIKE ? OR `*PREFIX*storages`.`id` LIKE ?)' . ' AND (`*PREFIX*files_antivirus`.`fileid` IS NULL OR `mtime` > `check_time`)' . ' AND `path` LIKE ?'; $stmt = \OCP\DB::prepare($sql, 5); try { $result = $stmt->execute(array($dirMimetypeId, 'local::%', 'home::%', 'files/%')); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('files_antivirus', __METHOD__ . 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR); return; } } catch (\Exception $e) { \OCP\Util::writeLog('files_antivirus', __METHOD__ . ', exception: ' . $e->getMessage(), \OCP\Util::ERROR); return; } $view = new \OC\Files\View('/'); while ($row = $result->fetchRow()) { $path = $view->getPath($row['fileid']); if (!is_null($path)) { $item = new Item($this->l10n, $view, $path, $row['fileid']); $scanner = $this->scannerFactory->getScanner(); $status = $scanner->scan($item); $status->dispatch($item, true); } } \OC_Util::tearDownFS(); }
/** * 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; }