Skip to content

Commit 2cd90f8

Browse files
committed
fix: Replace all usage of OC_Template by the new API
Signed-off-by: Côme Chilliet <[email protected]>
1 parent 253628a commit 2cd90f8

File tree

6 files changed

+42
-43
lines changed

6 files changed

+42
-43
lines changed

apps/dav/lib/Files/BrowserErrorPagePlugin.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
namespace OCA\DAV\Files;
99

1010
use OC\AppFramework\Http\Request;
11-
use OC_Template;
1211
use OCP\AppFramework\Http\ContentSecurityPolicy;
12+
use OCP\AppFramework\Http\TemplateResponse;
1313
use OCP\IRequest;
14+
use OCP\Template\ITemplateManager;
1415
use Sabre\DAV\Exception;
1516
use Sabre\DAV\Server;
1617
use Sabre\DAV\ServerPlugin;
@@ -84,7 +85,7 @@ public function generateBody(int $httpCode) {
8485
$templateName = (string)$httpCode;
8586
}
8687

87-
$content = new OC_Template('core', $templateName, 'guest');
88+
$content = \OCP\Server::get(ITemplateManager::class)->getTemplate('core', $templateName, TemplateResponse::RENDER_AS_GUEST);
8889
$content->assign('title', $this->server->httpResponse->getStatusText());
8990
$content->assign('remoteAddr', $request->getRemoteAddress());
9091
$content->assign('requestID', $request->getId());

lib/private/TemplateLayout.php

+24-30
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OC\Template\CSSResourceLocator;
1616
use OC\Template\JSConfigHelper;
1717
use OC\Template\JSResourceLocator;
18+
use OC\Template\Template;
1819
use OCP\App\IAppManager;
1920
use OCP\AppFramework\Http\TemplateResponse;
2021
use OCP\Defaults;
@@ -25,35 +26,29 @@
2526
use OCP\IURLGenerator;
2627
use OCP\IUserSession;
2728
use OCP\L10N\IFactory;
29+
use OCP\Server;
2830
use OCP\ServerVersion;
2931
use OCP\Support\Subscription\IRegistry;
3032
use OCP\Util;
3133

32-
class TemplateLayout extends \OC_Template {
33-
private static $versionHash = '';
34+
class TemplateLayout extends Template {
35+
private static string $versionHash = '';
3436
/** @var string[] */
3537
private static $cacheBusterCache = [];
3638

37-
/** @var CSSResourceLocator|null */
38-
public static $cssLocator = null;
39-
40-
/** @var JSResourceLocator|null */
41-
public static $jsLocator = null;
39+
public static ?CSSResourceLocator $cssLocator = null;
40+
public static ?JSResourceLocator $jsLocator = null;
4241

4342
private IConfig $config;
4443
private IAppManager $appManager;
4544
private InitialStateService $initialState;
4645
private INavigationManager $navigationManager;
4746

48-
/**
49-
* @param string $renderAs
50-
* @param string $appId application id
51-
*/
52-
public function __construct($renderAs, $appId = '') {
53-
$this->config = \OCP\Server::get(IConfig::class);
54-
$this->appManager = \OCP\Server::get(IAppManager::class);
55-
$this->initialState = \OCP\Server::get(InitialStateService::class);
56-
$this->navigationManager = \OCP\Server::get(INavigationManager::class);
47+
public function __construct(string $renderAs, string $appId = '') {
48+
$this->config = Server::get(IConfig::class);
49+
$this->appManager = Server::get(IAppManager::class);
50+
$this->initialState = Server::get(InitialStateService::class);
51+
$this->navigationManager = Server::get(INavigationManager::class);
5752

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

@@ -122,7 +116,7 @@ public function __construct($renderAs, $appId = '') {
122116
}
123117

124118
$userDisplayName = false;
125-
$user = \OC::$server->get(IUserSession::class)->getUser();
119+
$user = Server::get(IUserSession::class)->getUser();
126120
if ($user) {
127121
$userDisplayName = $user->getDisplayName();
128122
}
@@ -161,17 +155,15 @@ public function __construct($renderAs, $appId = '') {
161155
// Set body data-theme
162156
$this->assign('enabledThemes', []);
163157
if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
164-
/** @var \OCA\Theming\Service\ThemesService $themesService */
165-
$themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
158+
$themesService = Server::get(\OCA\Theming\Service\ThemesService::class);
166159
$this->assign('enabledThemes', $themesService->getEnabledThemes());
167160
}
168161

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

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

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

@@ -194,9 +186,10 @@ public function __construct($renderAs, $appId = '') {
194186
parent::__construct('core', 'layout.base');
195187
}
196188
// Send the language, locale, and direction to our layouts
197-
$lang = \OC::$server->get(IFactory::class)->findLanguage();
198-
$locale = \OC::$server->get(IFactory::class)->findLocale($lang);
199-
$direction = \OC::$server->getL10NFactory()->getLanguageDirection($lang);
189+
$l10nFactory = Server::get(IFactory::class);
190+
$lang = $l10nFactory->findLanguage();
191+
$locale = $l10nFactory->findLocale($lang);
192+
$direction = $l10nFactory->getLanguageDirection($lang);
200193

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

245+
$request = \OCP\Server::get(IRequest::class);
246+
252247
try {
253-
$pathInfo = \OC::$server->getRequest()->getPathInfo();
248+
$pathInfo = $request->getPathInfo();
254249
} catch (\Exception $e) {
255250
$pathInfo = '';
256251
}
257252

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

294-
$request = \OCP\Server::get(IRequest::class);
295289
if ($request->isUserAgent([Request::USER_AGENT_CLIENT_IOS, Request::USER_AGENT_SAFARI, Request::USER_AGENT_SAFARI_MOBILE])) {
296290
// Prevent auto zoom with iOS but still allow user zoom
297291
// On chrome (and others) this does not work (will also disable user zoom)

lib/private/legacy/OC_Template.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class OC_Template extends \OC\Template\Template {
1919
* Shortcut to print a simple page for guests
2020
* @param string $application The application we render the template for
2121
* @param string $name Name of the template
22-
* @param array|string $parameters Parameters for the template
22+
* @param array $parameters Parameters for the template
2323
* @return bool
2424
* @deprecated 32.0.0 Use \OCP\Template\ITemplateManager instead
2525
*/

lib/public/Util.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public static function emitHook($signalclass, $signalname, $params = []) {
384384

385385
/**
386386
* Cached encrypted CSRF token. Some static unit-tests of ownCloud compare
387-
* multiple OC_Template elements which invoke `callRegister`. If the value
387+
* multiple Template elements which invoke `callRegister`. If the value
388388
* would not be cached these unit-tests would fail.
389389
* @var string
390390
*/
@@ -393,6 +393,7 @@ public static function emitHook($signalclass, $signalname, $params = []) {
393393
/**
394394
* Register an get/post call. This is important to prevent CSRF attacks
395395
* @since 4.5.0
396+
* @deprecated 32.0.0 directly use CsrfTokenManager instead
396397
*/
397398
public static function callRegister() {
398399
if (self::$token === '') {

public.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\IConfig;
1515
use OCP\IRequest;
1616
use OCP\Server;
17+
use OCP\Template\ITemplateManager;
1718
use OCP\Util;
1819
use Psr\Log\LoggerInterface;
1920

@@ -92,9 +93,9 @@ function resolveService(string $service): string {
9293
}
9394
//show the user a detailed error page
9495
Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);
95-
OC_Template::printExceptionErrorPage($ex, $status);
96+
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, $status);
9697
} catch (Error $ex) {
9798
//show the user a detailed error page
9899
Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);
99-
OC_Template::printExceptionErrorPage($ex, 500);
100+
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
100101
}

remote.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
1111
use OCP\App\IAppManager;
12+
use OCP\IRequest;
13+
use OCP\Template\ITemplateManager;
1214
use Psr\Log\LoggerInterface;
1315
use Sabre\DAV\Exception\ServiceUnavailable;
1416
use Sabre\DAV\Server;
@@ -23,15 +25,15 @@ class RemoteException extends \Exception {
2325

2426
function handleException(Exception|Error $e): void {
2527
try {
26-
$request = \OC::$server->getRequest();
28+
$request = \OCP\Server::get(IRequest::class);
2729
// in case the request content type is text/xml - we assume it's a WebDAV request
2830
$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
2931
if ($isXmlContentType === 0) {
3032
// fire up a simple server to properly process the exception
3133
$server = new Server();
3234
if (!($e instanceof RemoteException)) {
3335
// we shall not log on RemoteException
34-
$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->get(LoggerInterface::class)));
36+
$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OCP\Server::get(LoggerInterface::class)));
3537
}
3638
$server->on('beforeMethod:*', function () use ($e) {
3739
if ($e instanceof RemoteException) {
@@ -54,19 +56,19 @@ function handleException(Exception|Error $e): void {
5456
}
5557
if ($e instanceof RemoteException) {
5658
// we shall not log on RemoteException
57-
OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
59+
\OCP\Server::get(ITemplateManager::class)->printErrorPage($e->getMessage(), '', $e->getCode());
5860
} else {
59-
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
60-
OC_Template::printExceptionErrorPage($e, $statusCode);
61+
\OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
62+
\OCP\Server::get(ITemplateManager::class)->printExceptionErrorPage($e, $statusCode);
6163
}
6264
}
6365
} catch (\Exception $e) {
64-
OC_Template::printExceptionErrorPage($e, 500);
66+
\OCP\Server::get(ITemplateManager::class)->printExceptionErrorPage($e, 500);
6567
}
6668
}
6769

6870
/**
69-
* @param $service
71+
* @param string $service
7072
* @return string
7173
*/
7274
function resolveService($service) {

0 commit comments

Comments
 (0)