<?php
$template_path='admin/';
$editable_file = array(
     'Cart/home.php'
);

function EvalTemplate($templatefile, $vars, $display=false) {
  $line=implode(file($templatefile), '');
  eval( "global $vars;");
  $vars = str_replace(",","",$vars);
  $var_list = explode('$',$vars);
  $i=0;
  while($i<=sizeof($var_list)) {    
    $line = str_replace("<%$var_list[$i]%>",$$var_list[$i],$line);
    $i++;
  }   
 if($display) print $line;
 else return $line;
}  

function revers_htmlspecialchars($msg) {
     $msg = str_replace("/&gt;/i", ">", $msg);
     $msg = str_replace("/&lt;/i", "<", $msg);
     $msg = str_replace("/&quot;/i", "\"", $msg);
     $msg = str_replace("/&amp;/i", "&", $msg);
     return $msg;
}


function SelectFile() {
  global $template_path, $editable_file, $content, $PHP_SELF, $d, $f;

$content = '<table><tr><td><b>Click on file name to view</b><td>&nbsp;</td></tr>';
for ($i=0; $i<count($editable_file); $i++) {
     $fn = $editable_file[$i];
     $content .= "<tr><td><a href=$PHP_SELF?topic=$f&file_name=$fn&pflag=view>$fn</a></td>";
     $content .= "<td><a href=$PHP_SELF?topic=$f&file_name=$fn&pflag=edit>EDIT</a></td></tr>";
}
$content .= '</table>';
echo $content;
}

function ShowScriptFile() {
  global $d, $f, $file_name, $content;
  
  // read template file
  $content = implode(file($file_name),"");
  $content = htmlspecialchars($content);
  $content = nl2br($content);

  $content = eregi_replace("&lt;\?", "<font color=blue>&lt;?", $content);
  $content = eregi_replace("\?&gt;", "?&gt;</font>", $content);

  $content = eregi_replace("&lt;script", "<font color=red>&lt;script", $content);
  $content = eregi_replace("/script&gt;", "/script&gt;</font>", $content);

echo "
<table width=500 border=0 cellpadding=\"5\">
  <tr> 
    <td colspan=\"3\">
       <table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">
        <tr valign=\"top\">
          <td width=\"00\"><font face=\"Arial\" size=\"2\">File</td>
          <td width=\"*\"><font face=\"Arial\" size=\"2\">
          $file_name
          <a href=\"$PHP_SELF?topic=$file_name&pflag=edit\">EDIT</a> |
          <a href=\"$PHP_SELF?d=$d&f=$f\">BACK to file list</a>
       </td>
        </tr>
        <tr valign=\"top\">
          <td width=\"200\"><font face=\"Arial\" size=\"2\">Content</font></td>
          <td width=\"*\" nowrap>$content</td>
        </tr>
      </table>
    </td>
  </tr>
</table>
";
}

function ShowScriptFileForm() {
  global $d, $f, $file_name, $content;
  
  // read template file
  $icontent = implode(file($file_name),"");
  $lcontent = strtolower($icontent); // get a lower case version for parsing

// parse content into editable/noneditable parts
$last = strlen($icontent);
$first = 0;
$editable = 1;
$lno = 0;

while ($first < $last) {
     if ($editable) { // find script start
          $ph = strpos($lcontent, '<?', $first);
          $ph = ($ph === false) ? $last+1 : $ph;

          $js = strpos($lcontent, '<script', $first);
          $js = ($js === false) ? $last+1 : $js;
     } else { // find script end
          $ph = strpos($lcontent, '?>', $first);
          $ph = ($ph === false) ? $last+1 : $ph+2;

          $js = strpos($lcontent, '/script>', $first);
          $js = ($js === false) ? $last+1 : $js+8;
     }

     $next = ($ph < $js) ? $ph : $js;
     if ($next > $first) {
          $content .= "<input type=hidden name=edit[$lno] value=$editable>\n";
          $line = substr($icontent, $first, $next - $first);

          if ($editable) {
               $line = htmlspecialchars($line);
               $content .= "<textarea name=line[$lno] rows=5 cols=80 wrap=off>$line</textarea>";
          } else {
               $rline = rawurlencode($line);
               $line = nl2br(htmlspecialchars($line));
               $content .= "<input type=hidden name=line[$lno] value=\"$rline\">\n$line";
          }
          $content .= "<br>\n";
          $lno++;
     }

     $editable = ($editable == 0) ? 1 : 0;
     $first = $next;
}

echo "
<table width=\"500\" border=\"0\" cellpadding=\"5\">
  <tr> 
    <td colspan=\"3\">
     <form method=post action=\"$PHP_SELF?topic=$f\">
      <input type=hidden name=pflag value=modify>
      <input type=hidden name=file_name value=$file_name>
       <table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">
        <tr valign=\"top\">
          <td width=\"00\"><font face=\"Arial\" size=\"2\">File</td>
          <td width=\"*\"><font face=\"Arial\" size=\"2\">
          $file_name
          <a href=\"$PHP_SELF?topic=&file_name=$file_name&pflag=view\">VIEW</a> |
          <a href=\"$PHP_SELF?topic=admin/\">BACK to file list</a>
          </td>
        </tr>
        <tr valign=\"top\">
          <td width=\"200\"><font face=\"Arial\" size=\"2\">Content</font></td>
          <td width=\"*\">$content</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type=submit name=submit value=\"Modify\" class=\"button\"></td>
        </tr>
      </table>
      </form>    
    </td>
  </tr>
</table>
";
}

function ProcessFormModifyFile() {
  global $template_path, $file_name, $content;
  global $line, $edit;

for($i=0; $i < count($line); $i++) {
  if ($edit[$i] == 1) {
    $line[$i] = revers_htmlspecialchars($line[$i]);
    $line[$i] = stripslashes($line[$i]);
  } else {
    $line[$i] = rawurldecode($line[$i]);
    $line[$i] = ereg_replace("(\r)+", "\r", $line[$i]);
  }
  $content .= $line[$i];
}

  $fp = fopen($file_name, 'w+');
  fwrite($fp,$content);
  fclose($fp);

  ShowScriptFile();
}


// ---------------
//  main program
// ---------------
  if (empty($pflag))
     $pflag = 'view';

  if (empty($file_name)) {  
    SelectFile();
  } elseif ($pflag == 'modify') {
    ProcessFormModifyFile();
  } elseif ($pflag == 'edit') {
    ShowScriptFileForm();
  } elseif ($pflag == 'view') {
    ShowScriptFile();
  }
?>
