Wiki source for ForeachBugInPhp4dot3dot10


Show raw source

I had my server crashed after upgrading to version 4.3.10
Wikka showed this message : Action Header not found, Action Footer not found.
After debuging, I saw that the code
%%(php) foreach ($ar as $val) %% is the cause of the problem, as $val didn' t get the values of the array but another array.
Replacing the code above by %%(php) foreach ($ar as $index => $val) %% has solved the problem.

Here is a code I used to upgrade my Wikka. I named it foreachfix.php and put it at actions/
%%(php)
<?php
$h = ob_get_contents();
ob_end_clean();
correct_foreach("/path/to/wikka/");
ob_start();
print($h);
echo 'Foreach fixed<br />';

function correct_foreach($d)
{
$hdl = opendir($d);
while ($f = readdir($hdl))
{
if (($f != '.') && ($f != '..'))
{
$f = $d . $f;
if (is_dir($f))
{
correct_foreach($f. '/');
}
else if (eregi('php$', $f))
{
ob_start();
readfile($f);
$data = ob_get_contents();
ob_end_clean();
#TODO : This regex is Okay for Wikka, but maybe insufficient for other scripts.
$data = preg_replace('#foreach \((.*?) as ([^=]*?)\)#i', "foreach (\\1 as \$dotmg"."_idx => \\2)", $data);
$fp = fopen($f, 'w');
fwrite($fp, $data);
fclose ($fp);
}
}
}
}
?>
%%

Then, I put ""{{foreachfix}}"" somewhere in a page and previewed it.
Note that in my wikka installation, Apache has write access to all wikka files. So, you should test it on an dev environment.
Please note the TODO in the above code.

Searching all files containing $dotmg_idx shows the files corrected.

''Nice work. But, as can be seen from the references I gave on (now) WikkaBugsResolved, **there is no bug**. All bug reports in the PHP bugtracker have been been classiified as "Bogus". Sorry, but this page is misnamed. //There is no bug.// So the (clever) "correction" presesented above is not a correction, but merely a **temporary workaround**.

The **real fix** is to disable ""ZendOptimizer"" (or other accelerator used, they are also affected) or upgrade it to the latest version. ""ZendOptimizer"" (v2.5.7) has a solution; I'm not sure whether the others do as yet.
For those running Wikka in a hosted environment and running into this foreach problem: Bug your hoster!

**The Wikka code base should not be changed with respect to foreach usage.** An //installation// might be temporarily helped with the above workaround, though. --JavaWoman''
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki