<?php

// stage 1 - revert WebEdit divs to InstanceEditable comments
// stripslashes doesn't seem to be needed, so I don't apply them
//
// for the PCRE lib in PHP, this is a quite simple preg_replace, not too recursive, easy on stackspace

$pattern = "/<div\s+id=\"?WebEdit\"?\s+comment=\"([^\"]+)\">/siU";
$replace = "<!-- InstanceBeginEditable name=\"\\1\" -->";

$stage1 = preg_replace($pattern, $replace, $fileContent);

// php function stripos only exists in cvs for the moment,
// so we have to 'normalize' all div tags --> all upper- or all lowercase
// let's make them all lowercase ...

$stage2a = str_replace("<DIV", "<div", $stage1);
$stage2 = str_replace("</DIV", "</div", $stage2a);

// inits
$odivcnt = 0;		// div counter
$owepcnt = 0;		// wep counter
$newpos  = 0;

$tmp = $stage2;		// stage2 is the output from the prvious step
$out = "";		// out will contain the converted content

while ( substr_count ($tmp, "</div") > 0 )		// loop as long as there are still </div tags
{

// cdiv = position of first closing div --> </div
// odiv = position of first opening div --> <div
// owep = position of first opening wep --> <!-- InstanceBeginEditable

// strpos is a bit nasty, let's work around it

$cdiv = strpos ($tmp, "</div");
if ($cdiv === false)
 {
   $cdiv = 104857600;                   // 100 megabytes ... should be sufficient
 }

$odiv = strpos ($tmp, "<div");
if ($odiv === false)
 {
   $odiv = 104857600;                   // 100 megabytes ... should be sufficient
 }

$owep = strpos ($tmp, "<!-- InstanceBeginEditable");
if ($owep === false)
 {
   $owep = 104857600;			// 100 megabytes ... should be sufficient
 }


if (( $cdiv < $odiv ) AND ( $cdiv < $owep ))		// close DIV
 {
   if ( $owepcnt == 0 )			// normal </div
    {
      $odivcnt--;
      // lookup first occurrence of > in $tmp starting at offset $cdiv
      $newpos = strpos($tmp, ">", $cdiv) + 1;
      $out .= substr($tmp, 0, $newpos);
    }
   else if ( $owepcnt == 1 )		// closing of a WEP region!
    {
      $odivcnt--;
      $owepcnt = 0;
      // lookup first occurrence of > in $tmp starting at offset $cdiv
      $newpos = strpos($tmp, ">", $cdiv) + 1;
      // code to replace </div> by <!-- InstanceEndEditable --> in $out
      $out .= substr($tmp, 0, $cdiv) . "<!-- InstanceEndEditable -->";
    }
   else if ( $owepcnt > 1 )		// div group within a WEP region
    {
      $owepcnt--;
      $odivcnt--;
      // lookup first occurrence of > in $tmp starting at offset $cdiv
      $newpos = strpos($tmp, ">", $cdiv) + 1;
      $out .= substr($tmp, 0, $newpos);
    }
 }
else if (( $odiv < $cdiv ) AND ( $odiv < $owep ))	// open DIV
 {
   $odivcnt++;
   // lookup first occurrence of > in $tmp starting at offset $odiv
   $newpos = strpos($tmp, ">", $odiv) + 1;
   $out .= substr($tmp, 0, $newpos);
   if ( $owepcnt > 0 )			// div group within a WEP region
    {
      $owepcnt++;
    }
 }
else if (( $owep < $odiv ) AND ( $owep < $cdiv ))       // open WEP
 {
   // WEP regions cannot be nested, so putting the count at 1 is safe
   $owepcnt = 1;
   $odivcnt++;
   // lookup first occurrence of --> in $tmp starting at offset $owep
   $newpos = strpos($tmp, "-->", $owep) + 3;
   $out .= substr($tmp, 0, $newpos);
 }

// make new $tmp
$tmp = substr ($tmp, $newpos);
}		// loop

// ok, no more div tags to convert --> remainder of $tmp
$out .= $tmp;

// result
$fileContent = $out;
?>