Skip to content

Commit

Permalink
fix: Replace all usage of OC_Template by the new API
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Feb 27, 2025
1 parent b467b90 commit 0573048
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 46 deletions.
5 changes: 3 additions & 2 deletions apps/dav/lib/Files/BrowserErrorPagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
namespace OCA\DAV\Files;

use OC\AppFramework\Http\Request;
use OC_Template;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest;
use OCP\Template\ITemplateManager;
use Sabre\DAV\Exception;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function generateBody(int $httpCode) {
$templateName = (string)$httpCode;
}

$content = new OC_Template('core', $templateName, 'guest');
$content = \OCP\Server::get(ITemplateManager::class)->getTemplate('core', $templateName, TemplateResponse::RENDER_AS_GUEST);
$content->assign('title', $this->server->httpResponse->getStatusText());

Check failure on line 89 in apps/dav/lib/Files/BrowserErrorPagePlugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

apps/dav/lib/Files/BrowserErrorPagePlugin.php:89:3: UndefinedClass: Class, interface or enum named OCP\Template\ITemplate does not exist (see https://psalm.dev/019)
$content->assign('remoteAddr', $request->getRemoteAddress());

Check failure on line 90 in apps/dav/lib/Files/BrowserErrorPagePlugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

apps/dav/lib/Files/BrowserErrorPagePlugin.php:90:3: UndefinedClass: Class, interface or enum named OCP\Template\ITemplate does not exist (see https://psalm.dev/019)
$content->assign('requestID', $request->getId());

Check failure on line 91 in apps/dav/lib/Files/BrowserErrorPagePlugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

apps/dav/lib/Files/BrowserErrorPagePlugin.php:91:3: UndefinedClass: Class, interface or enum named OCP\Template\ITemplate does not exist (see https://psalm.dev/019)
Expand Down
54 changes: 24 additions & 30 deletions lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OC\Template\CSSResourceLocator;
use OC\Template\JSConfigHelper;
use OC\Template\JSResourceLocator;
use OC\Template\Template;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Defaults;
Expand All @@ -25,35 +26,29 @@
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Server;
use OCP\ServerVersion;
use OCP\Support\Subscription\IRegistry;
use OCP\Util;

class TemplateLayout extends \OC_Template {
private static $versionHash = '';
class TemplateLayout extends Template {
private static string $versionHash = '';
/** @var string[] */
private static $cacheBusterCache = [];

/** @var CSSResourceLocator|null */
public static $cssLocator = null;

/** @var JSResourceLocator|null */
public static $jsLocator = null;
public static ?CSSResourceLocator $cssLocator = null;
public static ?JSResourceLocator $jsLocator = null;

private IConfig $config;
private IAppManager $appManager;
private InitialStateService $initialState;
private INavigationManager $navigationManager;

/**
* @param string $renderAs
* @param string $appId application id
*/
public function __construct($renderAs, $appId = '') {
$this->config = \OCP\Server::get(IConfig::class);
$this->appManager = \OCP\Server::get(IAppManager::class);
$this->initialState = \OCP\Server::get(InitialStateService::class);
$this->navigationManager = \OCP\Server::get(INavigationManager::class);
public function __construct(string $renderAs, string $appId = '') {
$this->config = Server::get(IConfig::class);
$this->appManager = Server::get(IAppManager::class);
$this->initialState = Server::get(InitialStateService::class);
$this->navigationManager = Server::get(INavigationManager::class);

// Add fallback theming variables if not rendered as user
if ($renderAs !== TemplateResponse::RENDER_AS_USER) {
Expand Down Expand Up @@ -84,8 +79,7 @@ public function __construct($renderAs, $appId = '') {
// Set body data-theme
$this->assign('enabledThemes', []);
if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
/** @var \OCA\Theming\Service\ThemesService */
$themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
$themesService = Server::get(\OCA\Theming\Service\ThemesService::class);
$this->assign('enabledThemes', $themesService->getEnabledThemes());
}

Expand Down Expand Up @@ -122,7 +116,7 @@ public function __construct($renderAs, $appId = '') {
}

$userDisplayName = false;
$user = \OC::$server->get(IUserSession::class)->getUser();
$user = Server::get(IUserSession::class)->getUser();
if ($user) {
$userDisplayName = $user->getDisplayName();
}
Expand Down Expand Up @@ -161,17 +155,15 @@ public function __construct($renderAs, $appId = '') {
// Set body data-theme
$this->assign('enabledThemes', []);
if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
/** @var \OCA\Theming\Service\ThemesService $themesService */
$themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
$themesService = Server::get(\OCA\Theming\Service\ThemesService::class);
$this->assign('enabledThemes', $themesService->getEnabledThemes());
}

// Set logo link target
$logoUrl = $this->config->getSystemValueString('logo_url', '');
$this->assign('logoUrl', $logoUrl);

/** @var IRegistry $subscription */
$subscription = \OCP\Server::get(IRegistry::class);
$subscription = Server::get(IRegistry::class);
$showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true);
if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) {
$showSimpleSignup = false;
Expand All @@ -184,7 +176,7 @@ public function __construct($renderAs, $appId = '') {
}

if ($this->appManager->isEnabledForUser('registration')) {
$urlGenerator = \OCP\Server::get(IURLGenerator::class);
$urlGenerator = Server::get(IURLGenerator::class);
$signUpLink = $urlGenerator->getAbsoluteURL('/index.php/apps/registration/');
}

Expand All @@ -194,9 +186,10 @@ public function __construct($renderAs, $appId = '') {
parent::__construct('core', 'layout.base');
}
// Send the language, locale, and direction to our layouts
$lang = \OC::$server->get(IFactory::class)->findLanguage();
$locale = \OC::$server->get(IFactory::class)->findLocale($lang);
$direction = \OC::$server->getL10NFactory()->getLanguageDirection($lang);
$l10nFactory = Server::get(IFactory::class);
$lang = $l10nFactory->findLanguage();
$locale = $l10nFactory->findLocale($lang);
$direction = $l10nFactory->getLanguageDirection($lang);

$lang = str_replace('_', '-', $lang);
$this->assign('language', $lang);
Expand Down Expand Up @@ -249,15 +242,17 @@ public function __construct($renderAs, $appId = '') {
$this->append('jsfiles', $web . '/' . $file . $this->getVersionHashSuffix());
}

$request = \OCP\Server::get(IRequest::class);

try {
$pathInfo = \OC::$server->getRequest()->getPathInfo();
$pathInfo = $request->getPathInfo();
} catch (\Exception $e) {
$pathInfo = '';
}

// Do not initialise scss appdata until we have a fully installed instance
// Do not load scss for update, errors, installation or login page
if (\OC::$server->getSystemConfig()->getValue('installed', false)
if ($this->config->getSystemValueBool('installed', false)
&& !\OCP\Util::needUpgrade()
&& $pathInfo !== ''
&& !preg_match('/^\/login/', $pathInfo)
Expand Down Expand Up @@ -291,7 +286,6 @@ public function __construct($renderAs, $appId = '') {
}
}

$request = \OCP\Server::get(IRequest::class);
if ($request->isUserAgent([Request::USER_AGENT_CLIENT_IOS, Request::USER_AGENT_SAFARI, Request::USER_AGENT_SAFARI_MOBILE])) {
// Prevent auto zoom with iOS but still allow user zoom
// On chrome (and others) this does not work (will also disable user zoom)
Expand Down
2 changes: 1 addition & 1 deletion lib/private/legacy/OC_Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class OC_Template extends \OC\Template\Template {
* Shortcut to print a simple page for guests
* @param string $application The application we render the template for
* @param string $name Name of the template
* @param array|string $parameters Parameters for the template
* @param array $parameters Parameters for the template
* @return bool
* @deprecated 32.0.0 Use \OCP\Template\ITemplateManager instead
*/
Expand Down
3 changes: 2 additions & 1 deletion lib/public/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public static function emitHook($signalclass, $signalname, $params = []) {

/**
* Cached encrypted CSRF token. Some static unit-tests of ownCloud compare
* multiple OC_Template elements which invoke `callRegister`. If the value
* multiple Template elements which invoke `callRegister`. If the value
* would not be cached these unit-tests would fail.
* @var string
*/
Expand All @@ -393,6 +393,7 @@ public static function emitHook($signalclass, $signalname, $params = []) {
/**
* Register an get/post call. This is important to prevent CSRF attacks
* @since 4.5.0
* @deprecated 32.0.0 directly use CsrfTokenManager instead
*/
public static function callRegister() {
if (self::$token === '') {
Expand Down
13 changes: 8 additions & 5 deletions public.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
require_once __DIR__ . '/lib/versioncheck.php';

use OCP\IRequest;
use OCP\Server;
use OCP\Template\ITemplateManager;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -40,7 +43,7 @@ function resolveService(string $service): string {
throw new \Exception('Service unavailable', 503);
}

$request = \OC::$server->getRequest();
$request = Server::get(IRequest::class);
$pathInfo = $request->getPathInfo();
if ($pathInfo === false || $pathInfo === '') {
throw new \Exception('Path not found', 404);
Expand Down Expand Up @@ -86,10 +89,10 @@ function resolveService(string $service): string {
$status = 503;
}
//show the user a detailed error page
\OCP\Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);
OC_Template::printExceptionErrorPage($ex, $status);
Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, $status);
} catch (Error $ex) {
//show the user a detailed error page
\OCP\Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);
OC_Template::printExceptionErrorPage($ex, 500);
Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
}
16 changes: 9 additions & 7 deletions remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
use OCP\App\IAppManager;
use OCP\IRequest;
use OCP\Template\ITemplateManager;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\ServiceUnavailable;
use Sabre\DAV\Server;
Expand All @@ -23,15 +25,15 @@ class RemoteException extends \Exception {

function handleException(Exception|Error $e): void {
try {
$request = \OC::$server->getRequest();
$request = \OCP\Server::get(IRequest::class);
// in case the request content type is text/xml - we assume it's a WebDAV request
$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
if ($isXmlContentType === 0) {
// fire up a simple server to properly process the exception
$server = new Server();
if (!($e instanceof RemoteException)) {
// we shall not log on RemoteException
$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->get(LoggerInterface::class)));
$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OCP\Server::get(LoggerInterface::class)));
}
$server->on('beforeMethod:*', function () use ($e) {
if ($e instanceof RemoteException) {
Expand All @@ -54,19 +56,19 @@ function handleException(Exception|Error $e): void {
}
if ($e instanceof RemoteException) {
// we shall not log on RemoteException
OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
\OCP\Server::get(ITemplateManager::class)->printErrorPage($e->getMessage(), '', $e->getCode());
} else {
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
OC_Template::printExceptionErrorPage($e, $statusCode);
\OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
\OCP\Server::get(ITemplateManager::class)->printExceptionErrorPage($e, $statusCode);
}
}
} catch (\Exception $e) {
OC_Template::printExceptionErrorPage($e, 500);
\OCP\Server::get(ITemplateManager::class)->printExceptionErrorPage($e, 500);
}
}

/**
* @param $service
* @param string $service
* @return string
*/
function resolveService($service) {
Expand Down

0 comments on commit 0573048

Please sign in to comment.