<?php
$phpbb_code = '<!-- BEGIN navlinks -->
<span>{navlinks.U_VIEW_FORUM}{$MICRODATA}{navlinks.MICRODATA}{navlinks.FORUM_NAME}</span>
<!-- END navlinks -->';
function fix_begin_tokens($code, $parent_nodes = array())
{
// PHP 5.3 cannot use $this in an anonymous function, so use this as a work-around
$parent_class = $this;
$callback = function ($matches) use ($parent_class, $parent_nodes)
{
$hard_parents = explode('.', $matches[1]);
array_pop($hard_parents); // ends with .
if ($hard_parents)
{
$parent_nodes = array_merge($hard_parents, $parent_nodes);
}
$name = $matches[2];
$subset = trim(substr($matches[3], 1, -1)); // Remove parenthesis
$body = $matches[4];
// Replace <!-- BEGINELSE -->
$body = str_replace('<!-- BEGINELSE -->', '{% else %}', $body);
// Is the designer wanting to call another loop in a loop?
// <!-- BEGIN loop -->
// <!-- BEGIN !loop2 -->
// <!-- END !loop2 -->
// <!-- END loop -->
// 'loop2' is actually on the same nesting level as 'loop' you assign
// variables to it with template->assign_block_vars('loop2', array(...))
if (strpos($name, '!') === 0)
{
// Count the number if ! occurrences
$count = substr_count($name, '!');
for ($i = 0; $i < $count; $i++)
{
array_pop($parent_nodes);
$name = substr($name, 1);
}
}
// Remove all parent nodes, e.g. foo, bar from foo.bar.foobar.VAR
foreach ($parent_nodes as $node)
{
$body = preg_replace('#([^a-zA-Z0-9_])' . $node . '\.([a-zA-Z0-9_]+)\.#', '$1$2.', $body);
}
// Add current node to list of parent nodes for child nodes
$parent_nodes[] = $name;
// Recursive...fix any child nodes
$body = $parent_class->fix_begin_tokens($body, $parent_nodes);
// Need the parent variable name
array_pop($parent_nodes);
$parent = (!empty($parent_nodes)) ? end($parent_nodes) . '.' : '';
if ($subset !== '')
{
$subset = '|subset(' . $subset . ')';
}
$parent = ($parent) ?: 'loops.';
// Turn into a Twig for loop
return "{% for {$name} in {$parent}{$name}{$subset} %}{$body}{% endfor %}";
};
return preg_replace_callback('#<!-- BEGIN ((?:[a-zA-Z0-9_]+\.)*)([!a-zA-Z0-9_]+)(\([0-9,\-]+\))? -->(.+?)<!-- END \1\2 -->#s', $callback, $code);
}
$code = fix_begin_tokens($phpbb_code, '');
echo $code;
Fatal error: Uncaught Error: Using $this when not in object context in /in/vrAvM:10
Stack trace:
#0 /in/vrAvM(64): fix_begin_tokens('<!-- BEGIN navl...', '')
#1 {main}
thrown in /in/vrAvM on line 10
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Fatal error: Uncaught Error: Using $this when not in object context in /in/vrAvM:10
Stack trace:
#0 /in/vrAvM(64): fix_begin_tokens('<!-- BEGIN navl...', '')
#1 {main}
thrown in /in/vrAvM on line 10
Process exited with code 255.
Output for 7.0.0 - 7.0.33
Notice: Undefined variable: this in /in/vrAvM on line 10
Warning: Invalid argument supplied for foreach() in /in/vrAvM on line 42
Fatal error: Uncaught Error: Call to a member function fix_begin_tokens() on null in /in/vrAvM:49
Stack trace:
#0 [internal function]: {closure}(Array)
#1 /in/vrAvM(61): preg_replace_callback('#<!-- BEGIN ((?...', Object(Closure), '<!-- BEGIN navl...')
#2 /in/vrAvM(64): fix_begin_tokens('<!-- BEGIN navl...', '')
#3 {main}
thrown in /in/vrAvM on line 49
Process exited with code 255.
Output for 5.6.0 - 5.6.38
Notice: Undefined variable: this in /in/vrAvM on line 10
Warning: Invalid argument supplied for foreach() in /in/vrAvM on line 42
Fatal error: Call to a member function fix_begin_tokens() on null in /in/vrAvM on line 49
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38
Notice: Undefined variable: this in /in/vrAvM on line 10
Warning: Invalid argument supplied for foreach() in /in/vrAvM on line 42
Fatal error: Call to a member function fix_begin_tokens() on a non-object in /in/vrAvM on line 49
Process exited with code 255.