Revision [3769]

This is an old revision of ForeachBugInPhp4dot3dot10 made by DotMG on 2004-12-28 06:41:59.

 

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
 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
 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
 $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.
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki