From 1fe40503e1863c873844ce3cdc1a6ae43b6abd23 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 20 Oct 2020 09:17:02 +0200 Subject: [PATCH] Improve readability in CsrfTokenFunctionTemplatePlugin see #3612 Co-authored-by: Matthias Schmidt --- .../CsrfTokenFunctionTemplatePlugin.class.php | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/wcfsetup/install/files/lib/system/template/plugin/CsrfTokenFunctionTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/CsrfTokenFunctionTemplatePlugin.class.php index 6dd1aa02de..8ac5c4fbc2 100644 --- a/wcfsetup/install/files/lib/system/template/plugin/CsrfTokenFunctionTemplatePlugin.class.php +++ b/wcfsetup/install/files/lib/system/template/plugin/CsrfTokenFunctionTemplatePlugin.class.php @@ -2,6 +2,7 @@ namespace wcf\system\template\plugin; use wcf\system\exception\SystemException; use wcf\system\template\TemplateEngine; +use wcf\system\WCF; /** * Template compiler plugin that prints the CSRF token ("Security Token"). @@ -21,17 +22,18 @@ class CsrfTokenFunctionTemplatePlugin implements IFunctionTemplatePlugin { * @inheritDoc */ public function execute($tagArgs, TemplateEngine $tplObj) { - if (isset($tagArgs['type']) && $tagArgs['type'] === 'raw') { - return \wcf\system\WCF::getSession()->getSecurityToken(); - } - else if (isset($tagArgs['type']) && $tagArgs['type'] === 'url') { - return \rawurlencode(\wcf\system\WCF::getSession()->getSecurityToken()); - } - else if (!isset($tagArgs['type']) || $tagArgs['type'] === 'form') { - return sprintf('', \wcf\system\WCF::getSession()->getSecurityToken()); - } - else { - throw new SystemException("Invalid type '".$tagArgs['type']."' given."); + $token = WCF::getSession()->getSecurityToken(); + $type = $tagArgs['type'] ?? 'form'; + + switch ($type) { + case 'raw': + return $token; + case 'url': + return \rawurlencode($token); + case 'form': + return \sprintf('', $token); + default: + throw new SystemException("Invalid type '".$type."' given."); } } } -- 2.20.1