Skip to content

Commit

Permalink
do the early exit mechanism within the EventListener
Browse files Browse the repository at this point in the history
#TODO: remove the Sabre Server-Plugin stuff.
  • Loading branch information
ata-no-one committed Nov 15, 2024
1 parent e05b151 commit 28584a6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
5 changes: 2 additions & 3 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function __construct() {
$eventDispatcher->addListener(LoadAdditionalScriptsEvent::class, function () {
Util::addScript(self::APP_ID, 'gdatavaas-files-action');
});
$eventDispatcher->addServiceListener('beforeCreateFile', FileEventsListener::class);
}

/**
Expand All @@ -73,8 +72,8 @@ public function register(IRegistrationContext $context): void {
return new TagService($logger, $systemTagManager, $standardTagMapper, $silentTagMapper, $dbFileMapper);
}, true);

AntivirusSabrePluginAddEventListener::register($context);
#FileEventsListener::register($context);
#AntivirusSabrePluginAddEventListener::register($context);
FileEventsListener::register($context);
CacheEntryListener::register($context);

// Util::connection is deprecated, but required ATM by FileSystem::addStorageWrapper
Expand Down
59 changes: 52 additions & 7 deletions lib/EventListener/FileEventsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\GDataVaas\EventListener;

use OC_Template;
use OCA\Files_Versions\Versions\IVersionManager;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\Event;
Expand All @@ -10,9 +11,13 @@
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\HintException;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception;
use Sabre\DAV\Server;

/** @template-implements IEventListener<BeforeNodeCopiedEvent|BeforeNodeDeletedEvent|BeforeNodeRenamedEvent|BeforeNodeTouchedEvent|BeforeNodeWrittenEvent|NodeCopiedEvent|NodeCreatedEvent|NodeDeletedEvent|NodeRenamedEvent|NodeTouchedEvent|NodeWrittenEvent> */
class FileEventsListener implements IEventListener {
Expand All @@ -22,6 +27,9 @@ public function __construct(
private IMimeTypeLoader $mimeTypeLoader,
private IUserSession $userSession,
private LoggerInterface $logger,
private IConfig $config,
private Server $server,
private IRequest $request,
) {
}

Expand All @@ -32,13 +40,50 @@ public static function register(IRegistrationContext $context): void {

public function handle(Event $event): void {
if ($event instanceof BeforeNodeCreatedEvent) {
$node = $event->getNode();
$event->stopPropagation();
$exception = new Exception('virus detected', 415);
$trace = $exception->getTraceAsString();
throw $exception;
$this->logger->debug(BeforeNodeCreatedEvent::class . ':' . $event->getNode()->getPath());
return;
$this->server->httpResponse->setBody($this->generateBody());
$this->server->httpResponse->setStatus(415);
$this->server->sapi->sendResponse($this->server->httpResponse);
exit;
}
}

public function generateBody(): mixed {
if ($this->acceptHtml()) {
$templateName = 'exception';
$renderAs = 'guest';
$templateName = '425';
} else {
$templateName = 'xml_exception';
$renderAs = null;
$this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
}

$debug = $this->config->getSystemValueBool('debug', false);

$ex = new HintException('virus found', 'virus found', 415);
$content = new OC_Template('gdatavaas', $templateName, $renderAs);
$content->assign('title', 'virus found');
$content->assign('message', 'virus found');
$content->assign('remoteAddr', $this->request->getRemoteAddress());
$content->assign('requestID', $this->request->getId());
$content->assign('debugMode', $debug);
$content->assign('errorClass', get_class($ex));
$content->assign('errorMsg', $ex->getMessage());
$content->assign('errorCode', $ex->getCode());
$content->assign('file', $ex->getFile());
$content->assign('line', $ex->getLine());
$content->assign('exception', $ex);
$contentString = $content->fetchPage();
return $contentString;
}

private function acceptHtml(): bool {
foreach (explode(',', $this->request->getHeader('Accept')) as $part) {
$subparts = explode(';', $part);
if (str_ends_with($subparts[0], '/html')) {
return true;
}
}
return false;
}
}

0 comments on commit 28584a6

Please sign in to comment.