Skip to content

Commit

Permalink
chore: Move away from app.php
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Dec 8, 2023
1 parent 842145a commit fcbfcd5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 79 deletions.
52 changes: 0 additions & 52 deletions appinfo/app.php

This file was deleted.

75 changes: 48 additions & 27 deletions lib/AppInfo/Application.php
Expand Up @@ -27,9 +27,14 @@
use OC\Files\Type\Detection;
use OC\Security\CSP\ContentSecurityPolicy;
use OCA\Federation\TrustedServers;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files_Sharing\Listener\LoadAdditionalListener;
use OCA\Officeonline\Capabilities;
use OCA\Officeonline\Hooks\WopiLockHooks;
use OCA\Officeonline\Listener\FilesScriptListener;
use OCA\Officeonline\Listener\LoadViewerListener;
use OCA\Officeonline\Middleware\WOPIMiddleware;
use OCA\Officeonline\PermissionManager;
use OCA\Officeonline\Preview\MSExcel;
use OCA\Officeonline\Preview\MSWord;
use OCA\Officeonline\Preview\OOXML;
Expand All @@ -38,44 +43,46 @@
use OCA\Officeonline\Service\FederationService;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
use OCP\AppFramework\QueryException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\IPreview;
use Psr\Log\LoggerInterface;

class Application extends App {
class Application extends App implements IBootstrap {
public const APP_ID = 'officeonline';

/**
* Strips the path and query parameters from the URL.
*
* @param string $url
* @return string
*/
private function domainOnly(string $url): string {
$parsed_url = parse_url(trim($url));
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
return "$scheme$host$port";
}

public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
}

try {
/** @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $this->getContainer()->getServer()->query(IEventDispatcher::class);
if (class_exists(LoadViewer::class)) {
$eventDispatcher->addListener(LoadViewer::class, function () {
\OCP\Util::addScript('officeonline', 'viewer');
});
public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerMiddleWare(WOPIMiddleware::class);
$context->registerEventListener(LoadAdditionalScriptsEvent::class, FilesScriptListener::class);
$context->registerEventListener(LoadAdditionalListener::class, FilesScriptListener::class);
$context->registerEventListener(LoadViewer::class, LoadViewerListener::class);
}

public function boot(IBootContext $context): void {
if (!$this->isEnabled()) {
return;
}
$this->registerProvider();
$this->updateCSP();
}

public function isEnabled(): bool {
$currentUser = \OC::$server->getUserSession()->getUser();
if ($currentUser !== null) {
/** @var PermissionManager $permissionManager */
$permissionManager = \OCP\Server::get(PermissionManager::class);
if (!$permissionManager->isEnabledForUser($currentUser)) {
return false;
}
} catch (QueryException $e) {
}

$this->getContainer()->registerCapability(Capabilities::class);
$this->getContainer()->registerMiddleWare(WOPIMiddleware::class);
return true;
}

public function registerProvider() {
Expand Down Expand Up @@ -159,4 +166,18 @@ public function updateCSP() {

$cspManager->addDefaultPolicy($policy);
}

/**
* Strips the path and query parameters from the URL.
*
* @param string $url
* @return string
*/
private function domainOnly(string $url): string {
$parsed_url = parse_url(trim($url));
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
return "$scheme$host$port";
}
}
12 changes: 12 additions & 0 deletions lib/Listener/FilesScriptListener.php
@@ -0,0 +1,12 @@
<?php

namespace OCA\Officeonline\Listener;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

class FilesScriptListener implements IEventListener {
public function handle(Event $event): void {
\OCP\Util::addScript('officeonline', 'files', 'viewer');
}
}
15 changes: 15 additions & 0 deletions lib/Listener/LoadViewerListener.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace OCA\Officeonline\Listener;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

class LoadViewerListener implements IEventListener {
public function handle(Event $event): void {
Util::addScript('officeonline', 'viewer');
}
}

0 comments on commit fcbfcd5

Please sign in to comment.