Foreach tag unset variables that have the value NULL
authorMarcel Werk <burntime@woltlab.com>
Wed, 28 Jul 2021 12:24:50 +0000 (14:24 +0200)
committerMarcel Werk <burntime@woltlab.com>
Wed, 28 Jul 2021 12:24:50 +0000 (14:24 +0200)
wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php

index 0193cde72175d4a698a77b98b781880f989f46d5..6a6c78f6a655159d7bf7859445b99755abce315f 100644 (file)
@@ -819,17 +819,13 @@ class TemplateScriptingCompiler {
                $foreachData['itemVar'] = $itemVar;
                
                $phpCode .= "\$this->foreachVars['{$hash}'] = [];\n";
-               $phpCode .= "if (isset({$itemVar})) {\n";
-               $phpCode .= "\$this->foreachVars['{$hash}']['item'] = {$itemVar};\n";
-               $phpCode .= "}\n";
+               $phpCode .= "\$this->foreachVars['{$hash}']['item'] = {$itemVar} ?? null;\n";
                
                if (isset($args['key'])) {
                        $keyVar = mb_substr($args['key'], 0, 1) != '$' ? "\$this->v[".$args['key']."]" : $args['key'];
                        $foreachData['keyVar'] = $keyVar;
                        
-                       $phpCode .= "if (isset({$keyVar})) {\n";
-                       $phpCode .= "\$this->foreachVars['{$hash}']['key'] = {$keyVar};\n";
-                       $phpCode .= "}\n";
+                       $phpCode .= "\$this->foreachVars['{$hash}']['key'] = {$keyVar} ?? null;\n";
                        
                        $phpCode .= "foreach (".$foreachHash." as {$keyVar} => {$itemVar}) {\n";
                }
@@ -862,13 +858,11 @@ class TemplateScriptingCompiler {
                // unset `item` and `key` variables and restore their sandboxed values
                $phpCode = "<?php }\n";
                $phpCode .= "unset({$foreachData['itemVar']});";
-               $phpCode .= "if (isset(\$this->foreachVars['{$foreachData['hash']}']['item'])) {\n";
                $phpCode .= "{$foreachData['itemVar']} = \$this->foreachVars['{$foreachData['hash']}']['item'];\n";
-               $phpCode .= "}\n";
                
                if (isset($foreachData['keyVar'])) {
                        $phpCode .= "unset({$foreachData['keyVar']});";
-                       $phpCode .= "if (isset(\$this->foreachVars['{$foreachData['hash']}']['key'])) {\n";
+                       $phpCode .= "if (array_key_exists('key', \$this->foreachVars['{$foreachData['hash']}'])) {\n";
                        $phpCode .= "{$foreachData['keyVar']} = \$this->foreachVars['{$foreachData['hash']}']['key'];\n";
                        $phpCode .= "}\n";
                }