fix: Replace all usage of OC_Template by the new API

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/51029/head
Côme Chilliet 2025-02-27 11:59:44 +07:00 committed by Côme Chilliet
parent 253628ad5a
commit 2cd90f8281
6 changed files with 42 additions and 43 deletions

@ -8,9 +8,10 @@
namespace OCA\DAV\Files; namespace OCA\DAV\Files;
use OC\AppFramework\Http\Request; use OC\AppFramework\Http\Request;
use OC_Template;
use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest; use OCP\IRequest;
use OCP\Template\ITemplateManager;
use Sabre\DAV\Exception; use Sabre\DAV\Exception;
use Sabre\DAV\Server; use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin; use Sabre\DAV\ServerPlugin;
@ -84,7 +85,7 @@ class BrowserErrorPagePlugin extends ServerPlugin {
$templateName = (string)$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()); $content->assign('title', $this->server->httpResponse->getStatusText());
$content->assign('remoteAddr', $request->getRemoteAddress()); $content->assign('remoteAddr', $request->getRemoteAddress());
$content->assign('requestID', $request->getId()); $content->assign('requestID', $request->getId());

@ -15,6 +15,7 @@ use OC\Search\SearchQuery;
use OC\Template\CSSResourceLocator; use OC\Template\CSSResourceLocator;
use OC\Template\JSConfigHelper; use OC\Template\JSConfigHelper;
use OC\Template\JSResourceLocator; use OC\Template\JSResourceLocator;
use OC\Template\Template;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\Defaults; use OCP\Defaults;
@ -25,35 +26,29 @@ use OCP\IRequest;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
use OCP\Server;
use OCP\ServerVersion; use OCP\ServerVersion;
use OCP\Support\Subscription\IRegistry; use OCP\Support\Subscription\IRegistry;
use OCP\Util; use OCP\Util;
class TemplateLayout extends \OC_Template { class TemplateLayout extends Template {
private static $versionHash = ''; private static string $versionHash = '';
/** @var string[] */ /** @var string[] */
private static $cacheBusterCache = []; private static $cacheBusterCache = [];
/** @var CSSResourceLocator|null */ public static ?CSSResourceLocator $cssLocator = null;
public static $cssLocator = null; public static ?JSResourceLocator $jsLocator = null;
/** @var JSResourceLocator|null */
public static $jsLocator = null;
private IConfig $config; private IConfig $config;
private IAppManager $appManager; private IAppManager $appManager;
private InitialStateService $initialState; private InitialStateService $initialState;
private INavigationManager $navigationManager; private INavigationManager $navigationManager;
/** public function __construct(string $renderAs, string $appId = '') {
* @param string $renderAs $this->config = Server::get(IConfig::class);
* @param string $appId application id $this->appManager = Server::get(IAppManager::class);
*/ $this->initialState = Server::get(InitialStateService::class);
public function __construct($renderAs, $appId = '') { $this->navigationManager = Server::get(INavigationManager::class);
$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);
// Add fallback theming variables if not rendered as user // Add fallback theming variables if not rendered as user
if ($renderAs !== TemplateResponse::RENDER_AS_USER) { if ($renderAs !== TemplateResponse::RENDER_AS_USER) {
@ -84,8 +79,7 @@ class TemplateLayout extends \OC_Template {
// Set body data-theme // Set body data-theme
$this->assign('enabledThemes', []); $this->assign('enabledThemes', []);
if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) { if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
/** @var \OCA\Theming\Service\ThemesService */ $themesService = Server::get(\OCA\Theming\Service\ThemesService::class);
$themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
$this->assign('enabledThemes', $themesService->getEnabledThemes()); $this->assign('enabledThemes', $themesService->getEnabledThemes());
} }
@ -122,7 +116,7 @@ class TemplateLayout extends \OC_Template {
} }
$userDisplayName = false; $userDisplayName = false;
$user = \OC::$server->get(IUserSession::class)->getUser(); $user = Server::get(IUserSession::class)->getUser();
if ($user) { if ($user) {
$userDisplayName = $user->getDisplayName(); $userDisplayName = $user->getDisplayName();
} }
@ -161,8 +155,7 @@ class TemplateLayout extends \OC_Template {
// Set body data-theme // Set body data-theme
$this->assign('enabledThemes', []); $this->assign('enabledThemes', []);
if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) { if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
/** @var \OCA\Theming\Service\ThemesService $themesService */ $themesService = Server::get(\OCA\Theming\Service\ThemesService::class);
$themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
$this->assign('enabledThemes', $themesService->getEnabledThemes()); $this->assign('enabledThemes', $themesService->getEnabledThemes());
} }
@ -170,8 +163,7 @@ class TemplateLayout extends \OC_Template {
$logoUrl = $this->config->getSystemValueString('logo_url', ''); $logoUrl = $this->config->getSystemValueString('logo_url', '');
$this->assign('logoUrl', $logoUrl); $this->assign('logoUrl', $logoUrl);
/** @var IRegistry $subscription */ $subscription = Server::get(IRegistry::class);
$subscription = \OCP\Server::get(IRegistry::class);
$showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true); $showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true);
if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) { if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) {
$showSimpleSignup = false; $showSimpleSignup = false;
@ -184,7 +176,7 @@ class TemplateLayout extends \OC_Template {
} }
if ($this->appManager->isEnabledForUser('registration')) { if ($this->appManager->isEnabledForUser('registration')) {
$urlGenerator = \OCP\Server::get(IURLGenerator::class); $urlGenerator = Server::get(IURLGenerator::class);
$signUpLink = $urlGenerator->getAbsoluteURL('/index.php/apps/registration/'); $signUpLink = $urlGenerator->getAbsoluteURL('/index.php/apps/registration/');
} }
@ -194,9 +186,10 @@ class TemplateLayout extends \OC_Template {
parent::__construct('core', 'layout.base'); parent::__construct('core', 'layout.base');
} }
// Send the language, locale, and direction to our layouts // Send the language, locale, and direction to our layouts
$lang = \OC::$server->get(IFactory::class)->findLanguage(); $l10nFactory = Server::get(IFactory::class);
$locale = \OC::$server->get(IFactory::class)->findLocale($lang); $lang = $l10nFactory->findLanguage();
$direction = \OC::$server->getL10NFactory()->getLanguageDirection($lang); $locale = $l10nFactory->findLocale($lang);
$direction = $l10nFactory->getLanguageDirection($lang);
$lang = str_replace('_', '-', $lang); $lang = str_replace('_', '-', $lang);
$this->assign('language', $lang); $this->assign('language', $lang);
@ -249,15 +242,17 @@ class TemplateLayout extends \OC_Template {
$this->append('jsfiles', $web . '/' . $file . $this->getVersionHashSuffix()); $this->append('jsfiles', $web . '/' . $file . $this->getVersionHashSuffix());
} }
$request = \OCP\Server::get(IRequest::class);
try { try {
$pathInfo = \OC::$server->getRequest()->getPathInfo(); $pathInfo = $request->getPathInfo();
} catch (\Exception $e) { } catch (\Exception $e) {
$pathInfo = ''; $pathInfo = '';
} }
// Do not initialise scss appdata until we have a fully installed instance // Do not initialise scss appdata until we have a fully installed instance
// Do not load scss for update, errors, installation or login page // 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() && !\OCP\Util::needUpgrade()
&& $pathInfo !== '' && $pathInfo !== ''
&& !preg_match('/^\/login/', $pathInfo) && !preg_match('/^\/login/', $pathInfo)
@ -291,7 +286,6 @@ class TemplateLayout extends \OC_Template {
} }
} }
$request = \OCP\Server::get(IRequest::class);
if ($request->isUserAgent([Request::USER_AGENT_CLIENT_IOS, Request::USER_AGENT_SAFARI, Request::USER_AGENT_SAFARI_MOBILE])) { 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 // Prevent auto zoom with iOS but still allow user zoom
// On chrome (and others) this does not work (will also disable user zoom) // On chrome (and others) this does not work (will also disable user zoom)

@ -19,7 +19,7 @@ class OC_Template extends \OC\Template\Template {
* Shortcut to print a simple page for guests * Shortcut to print a simple page for guests
* @param string $application The application we render the template for * @param string $application The application we render the template for
* @param string $name Name of the template * @param string $name Name of the template
* @param array|string $parameters Parameters for the template * @param array $parameters Parameters for the template
* @return bool * @return bool
* @deprecated 32.0.0 Use \OCP\Template\ITemplateManager instead * @deprecated 32.0.0 Use \OCP\Template\ITemplateManager instead
*/ */

@ -384,7 +384,7 @@ class Util {
/** /**
* Cached encrypted CSRF token. Some static unit-tests of ownCloud compare * 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. * would not be cached these unit-tests would fail.
* @var string * @var string
*/ */
@ -393,6 +393,7 @@ class Util {
/** /**
* Register an get/post call. This is important to prevent CSRF attacks * Register an get/post call. This is important to prevent CSRF attacks
* @since 4.5.0 * @since 4.5.0
* @deprecated 32.0.0 directly use CsrfTokenManager instead
*/ */
public static function callRegister() { public static function callRegister() {
if (self::$token === '') { if (self::$token === '') {

@ -14,6 +14,7 @@ use OCP\App\IAppManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IRequest; use OCP\IRequest;
use OCP\Server; use OCP\Server;
use OCP\Template\ITemplateManager;
use OCP\Util; use OCP\Util;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -92,9 +93,9 @@ try {
} }
//show the user a detailed error page //show the user a detailed error page
Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]); Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);
OC_Template::printExceptionErrorPage($ex, $status); Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, $status);
} catch (Error $ex) { } catch (Error $ex) {
//show the user a detailed error page //show the user a detailed error page
Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]); Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);
OC_Template::printExceptionErrorPage($ex, 500); Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
} }

@ -9,6 +9,8 @@ require_once __DIR__ . '/lib/versioncheck.php';
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin; use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\IRequest;
use OCP\Template\ITemplateManager;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\ServiceUnavailable; use Sabre\DAV\Exception\ServiceUnavailable;
use Sabre\DAV\Server; use Sabre\DAV\Server;
@ -23,7 +25,7 @@ class RemoteException extends \Exception {
function handleException(Exception|Error $e): void { function handleException(Exception|Error $e): void {
try { 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 // in case the request content type is text/xml - we assume it's a WebDAV request
$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml'); $isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
if ($isXmlContentType === 0) { if ($isXmlContentType === 0) {
@ -31,7 +33,7 @@ function handleException(Exception|Error $e): void {
$server = new Server(); $server = new Server();
if (!($e instanceof RemoteException)) { if (!($e instanceof RemoteException)) {
// we shall not log on 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) { $server->on('beforeMethod:*', function () use ($e) {
if ($e instanceof RemoteException) { if ($e instanceof RemoteException) {
@ -54,19 +56,19 @@ function handleException(Exception|Error $e): void {
} }
if ($e instanceof RemoteException) { if ($e instanceof RemoteException) {
// we shall not log on 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 { } else {
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]); \OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
OC_Template::printExceptionErrorPage($e, $statusCode); \OCP\Server::get(ITemplateManager::class)->printExceptionErrorPage($e, $statusCode);
} }
} }
} catch (\Exception $e) { } catch (\Exception $e) {
OC_Template::printExceptionErrorPage($e, 500); \OCP\Server::get(ITemplateManager::class)->printExceptionErrorPage($e, 500);
} }
} }
/** /**
* @param $service * @param string $service
* @return string * @return string
*/ */
function resolveService($service) { function resolveService($service) {