From 20c03cccdde2302412d1e14adda370e7eb57b8e8 Mon Sep 17 00:00:00 2001 From: javiexin Date: Sun, 8 Jan 2017 00:00:47 +0100 Subject: [ticket/14944] Add possibility to search for template loop indexes by key Adds a new function to the template interface, and implements it in the context class. The function returns the ordinal index for a specified key, with the same structure that the key for alter_block_array. Reuses same code. Remove unneeded references, do nothing for int keys. Check out of bounds or wrong blockname errors. Added tests. PHPBB3-14944 --- phpBB/phpbb/template/context.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/template/context.php') diff --git a/phpBB/phpbb/template/context.php b/phpBB/phpbb/template/context.php index 1617ca3d19..8bfbd73f1a 100644 --- a/phpBB/phpbb/template/context.php +++ b/phpBB/phpbb/template/context.php @@ -305,16 +305,28 @@ class context $name = $blocks[$i]; $index = sizeof($block[$name]) - 1; } + if (!isset($block[$name])) + { + return false; + } $block = $block[$name]; + if (!isset($block[$index])) + { + return false; + } $block = $block[$index]; } + if (!isset($block[$blocks[$i]])) + { + return false; + } $block = $block[$blocks[$i]]; // Traverse the last block // Change key to zero (change first position) if false and to last position if true if ($key === false || $key === true) { - return ($key === false) ? 0 : sizeof($block); + return ($key === false) ? 0 : sizeof($block) - 1; } // Get correct position if array given @@ -331,7 +343,7 @@ class context } } - return is_int($key) ? $key : false; + return (is_int($key) && ((0 <= $key) && ($key < sizeof($block)))) ? $key : false; } /** -- cgit v1.2.1