]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add constants for the magic strings of template rendering 21869/head
authorJoas Schilling <coding@schilljs.com>
Thu, 16 Jul 2020 13:47:28 +0000 (15:47 +0200)
committerJoas Schilling <coding@schilljs.com>
Thu, 16 Jul 2020 13:47:28 +0000 (15:47 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/TemplateLayout.php
lib/private/legacy/OC_Template.php
lib/public/AppFramework/Http/TemplateResponse.php

index 1fbf0acb99c1f29def66df176cd9a89b3aaf11bc..69eb26ab8b2f95ba93b2019bda623071feeb42f1 100644 (file)
@@ -48,6 +48,7 @@ use OC\AppFramework\Http\Request;
 use OC\Template\JSCombiner;
 use OC\Template\JSConfigHelper;
 use OC\Template\SCSSCacher;
+use OCP\AppFramework\Http\TemplateResponse;
 use OCP\Defaults;
 use OCP\IInitialStateService;
 use OCP\Support\Subscription\IRegistry;
@@ -74,7 +75,7 @@ class TemplateLayout extends \OC_Template {
                }
 
                // Decide which page we show
-               if ($renderAs === 'user') {
+               if ($renderAs === TemplateResponse::RENDER_AS_USER) {
                        parent::__construct('core', 'layout.user');
                        if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
                                $this->assign('bodyid', 'body-settings');
@@ -123,12 +124,12 @@ class TemplateLayout extends \OC_Template {
                        } catch (\OCP\AutoloadNotAllowedException $e) {
                                $this->assign('themingInvertMenu', false);
                        }
-               } elseif ($renderAs === 'error') {
+               } elseif ($renderAs === TemplateResponse::RENDER_AS_ERROR) {
                        parent::__construct('core', 'layout.guest', '', false);
                        $this->assign('bodyid', 'body-login');
                        $this->assign('user_displayname', '');
                        $this->assign('user_uid', '');
-               } elseif ($renderAs === 'guest') {
+               } elseif ($renderAs === TemplateResponse::RENDER_AS_GUEST) {
                        parent::__construct('core', 'layout.guest');
                        \OC_Util::addStyle('guest');
                        $this->assign('bodyid', 'body-login');
@@ -136,7 +137,7 @@ class TemplateLayout extends \OC_Template {
                        $userDisplayName = \OC_User::getDisplayName();
                        $this->assign('user_displayname', $userDisplayName);
                        $this->assign('user_uid', \OC_User::getUser());
-               } elseif ($renderAs === 'public') {
+               } elseif ($renderAs === TemplateResponse::RENDER_AS_PUBLIC) {
                        parent::__construct('core', 'layout.public');
                        $this->assign('appid', $appId);
                        $this->assign('bodyid', 'body-public');
@@ -172,7 +173,7 @@ class TemplateLayout extends \OC_Template {
                // Add the js files
                $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
                $this->assign('jsfiles', []);
-               if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
+               if ($this->config->getSystemValue('installed', false) && $renderAs != TemplateResponse::RENDER_AS_ERROR) {
                        if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
                                $jsConfigHelper = new JSConfigHelper(
                                        \OC::$server->getL10N('lib'),
@@ -210,7 +211,7 @@ class TemplateLayout extends \OC_Template {
                        && !\OCP\Util::needUpgrade()
                        && $pathInfo !== ''
                        && !preg_match('/^\/login/', $pathInfo)
-                       && $renderAs !== 'error'
+                       && $renderAs !== TemplateResponse::RENDER_AS_ERROR
                ) {
                        $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
                } else {
index 856d5491090162b9c5414f61e759b24f5c19b406..32d185d25fdc90a11b2938813412b43b2f792836 100644 (file)
@@ -39,6 +39,7 @@
  */
 
 use OC\TemplateLayout;
+use OCP\AppFramework\Http\TemplateResponse;
 
 require_once __DIR__.'/template/functions.php';
 
@@ -72,7 +73,7 @@ class OC_Template extends \OC\Template\Base {
         *                         "admin".
         * @param bool $registerCall = true
         */
-       public function __construct($app, $name, $renderAs = "", $registerCall = true) {
+       public function __construct($app, $name, $renderAs = TemplateResponse::RENDER_AS_BLANK, $registerCall = true) {
                // Read the selected theme from the config file
                self::initTemplateEngine($renderAs);
 
@@ -104,7 +105,7 @@ class OC_Template extends \OC\Template\Base {
                        //apps that started before the template initialization can load their own scripts/styles
                        //so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true
                        //meaning the last script/style in this list will be loaded first
-                       if (\OC::$server->getSystemConfig()->getValue('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) {
+                       if (\OC::$server->getSystemConfig()->getValue('installed', false) && $renderAs !== TemplateResponse::RENDER_AS_ERROR && !\OCP\Util::needUpgrade()) {
                                if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {
                                        OC_Util::addScript('backgroundjobs', null, true);
                                }
index 48e6c43411f626eb713b348657bd920b80dd5a1f..9622d9696dc9b8e0a508ad6c8f2c197eb9711e0d 100644 (file)
@@ -38,6 +38,27 @@ namespace OCP\AppFramework\Http;
  * @since 6.0.0
  */
 class TemplateResponse extends Response {
+       /**
+        * @since 20.0.0
+        */
+       public const RENDER_AS_GUEST = 'guest';
+       /**
+        * @since 20.0.0
+        */
+       public const RENDER_AS_BLANK = '';
+       /**
+        * @since 20.0.0
+        */
+       public const RENDER_AS_USER = 'user';
+       /**
+        * @since 20.0.0
+        */
+       public const RENDER_AS_ERROR = 'error';
+       /**
+        * @since 20.0.0
+        */
+       public const RENDER_AS_PUBLIC = 'public';
+
        /**
         * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent
         */
@@ -81,7 +102,7 @@ class TemplateResponse extends Response {
         * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
         */
        public function __construct($appName, $templateName, array $params=[],
-                                                               $renderAs='user') {
+                                                               $renderAs = self::RENDER_AS_USER) {
                parent::__construct();
 
                $this->templateName = $templateName;
@@ -160,8 +181,18 @@ class TemplateResponse extends Response {
         * @since 6.0.0
         */
        public function render() {
-               // \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
-               $renderAs = $this->renderAs === 'blank' ? '' : $this->renderAs;
+               $renderAs = self::RENDER_AS_USER;
+               if ($this->renderAs === 'blank') {
+                       // Legacy fallback as \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
+                       $renderAs = self::RENDER_AS_BLANK;
+               } elseif (in_array($this->renderAs, [
+                       self::RENDER_AS_GUEST,
+                       self::RENDER_AS_BLANK,
+                       self::RENDER_AS_ERROR,
+                       self::RENDER_AS_PUBLIC,
+                       self::RENDER_AS_USER], true)) {
+                       $renderAs = $this->renderAs;
+               }
 
                \OCP\Util::addHeader('meta', ['name' => 'robots', 'content' => 'noindex, nofollow']);
                $template = new \OCP\Template($this->appName, $this->templateName, $renderAs);