From c2043e47dabc23100ecc388ae1e9d8ae20c2257e Mon Sep 17 00:00:00 2001 From: javiexin Date: Wed, 31 May 2017 13:57:41 +0200 Subject: [PATCH] [ticket/14994] Refactor template->assign_block_var Refactor assign_block_var to use the same block selection mechanism as is used in alter_block_array. This allows creating new blocks at any position in the template structure, not only on the last block. Allows selecting a block as outer[2].middle. Added tests. Added PHP 7.2 compatibility. PHPBB3-14994 --- phpBB/phpbb/template/context.php | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/phpBB/phpbb/template/context.php b/phpBB/phpbb/template/context.php index 55d7b9c861..5a15e12582 100644 --- a/phpBB/phpbb/template/context.php +++ b/phpBB/phpbb/template/context.php @@ -193,30 +193,15 @@ class context // For nested block, $blockcount > 0, for top-level block, $blockcount == 0 $blocks = explode('.', $blockname); - $blockcount = sizeof($blocks) - 1; + $blockcount = count($blocks) - 1; $block = &$this->tpldata; for ($i = 0; $i < $blockcount; $i++) { - if (($pos = strpos($blocks[$i], '[')) !== false) - { - $name = substr($blocks[$i], 0, $pos); - - if (strpos($blocks[$i], '[]') === $pos) - { - $index = sizeof($block[$name]) - 1; - } - else - { - $index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1); - } - } - else - { - $name = $blocks[$i]; - $index = sizeof($block[$name]) - 1; - } + $pos = strpos($blocks[$i], '['); + $name = ($pos !== false) ? substr($blocks[$i], 0, $pos) : $blocks[$i]; $block = &$block[$name]; + $index = (!$pos || strpos($blocks[$i], '[]') === $pos) ? (count($block) - 1) : (min((int) substr($blocks[$i], $pos + 1, -1), count($block) - 1)); $block = &$block[$index]; }