aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/template/context.php
diff options
context:
space:
mode:
authorjaviexin <javiexin@gmail.com>2017-01-12 21:25:39 +0100
committerjaviexin <javiexin@gmail.com>2017-01-12 21:25:39 +0100
commitd2ad751851c955c19d1688cbe511925489212121 (patch)
treee2e52c8a6d46f79ad9350a12adffd5fcb9803b1a /phpBB/phpbb/template/context.php
parent76b3fbc00669ca5d23255229b523cfa8e10c19bb (diff)
downloadforums-d2ad751851c955c19d1688cbe511925489212121.tar
forums-d2ad751851c955c19d1688cbe511925489212121.tar.gz
forums-d2ad751851c955c19d1688cbe511925489212121.tar.bz2
forums-d2ad751851c955c19d1688cbe511925489212121.tar.xz
forums-d2ad751851c955c19d1688cbe511925489212121.zip
[ticket/14943] Fix template loop access by index
Allows inserting elements in a loop specified as 'outer[3].inner'. This was coded, but malfunctioning. Name incorrectly set on insert. If block was empty, the insertion process should create it. Checking for out of bounds indexes. PHPBB3-14943
Diffstat (limited to 'phpBB/phpbb/template/context.php')
-rw-r--r--phpBB/phpbb/template/context.php69
1 files changed, 39 insertions, 30 deletions
diff --git a/phpBB/phpbb/template/context.php b/phpBB/phpbb/template/context.php
index f0bc4c859a..4235a8543e 100644
--- a/phpBB/phpbb/template/context.php
+++ b/phpBB/phpbb/template/context.php
@@ -293,47 +293,49 @@ class context
public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')
{
$this->num_rows_is_set = false;
- if (strpos($blockname, '.') !== false)
- {
- // Nested block.
- $blocks = explode('.', $blockname);
- $blockcount = sizeof($blocks) - 1;
- $block = &$this->tpldata;
- for ($i = 0; $i < $blockcount; $i++)
+ // For nested block, $blockcount > 0, for top-level block, $blockcount == 0
+ $blocks = explode('.', $blockname);
+ $blockcount = sizeof($blocks) - 1;
+
+ $block = &$this->tpldata;
+ for ($i = 0; $i < $blockcount; $i++)
+ {
+ if (($pos = strpos($blocks[$i], '[')) !== false)
{
- if (($pos = strpos($blocks[$i], '[')) !== false)
+ $name = substr($blocks[$i], 0, $pos);
+
+ if (strpos($blocks[$i], '[]') === $pos)
{
- $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);
- }
+ $index = sizeof($block[$name]) - 1;
}
else
{
- $name = $blocks[$i];
- $index = sizeof($block[$name]) - 1;
+ $index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1);
}
- $block = &$block[$name];
- $block = &$block[$index];
}
-
- $block = &$block[$blocks[$i]]; // Traverse the last block
- $name = $blocks[$i];
+ else
+ {
+ $name = $blocks[$i];
+ $index = sizeof($block[$name]) - 1;
+ }
+ $block = &$block[$name];
+ $block = &$block[$index];
}
- else
+ $name = $blocks[$i];
+
+ // If last block does not exist and we are inserting, and not searching for key, we create it empty; otherwise, nothing to do
+ if (!isset($block[$name]))
{
- // Top-level block.
- $block = &$this->tpldata[$blockname];
- $name = $blockname;
+ if ($mode != 'insert' || is_array($key))
+ {
+ return false;
+ }
+ $block[$name] = array();
}
+ $block = &$block[$name]; // Now we can traverse the last block
+
// Change key to zero (change first position) if false and to last position if true
if ($key === false || $key === true)
{
@@ -373,8 +375,9 @@ class context
unset($block[($key - 1)]['S_LAST_ROW']);
$vararray['S_LAST_ROW'] = true;
}
- else if ($key === 0)
+ if ($key <= 0)
{
+ $key = 0;
unset($block[0]['S_FIRST_ROW']);
$vararray['S_FIRST_ROW'] = true;
}
@@ -400,6 +403,12 @@ class context
// Which block to change?
if ($mode == 'change')
{
+ // If key is out of bounds, do not change anything
+ if ($key > sizeof($block) || $key < 0)
+ {
+ return false;
+ }
+
if ($key == sizeof($block))
{
$key--;