<html>

<head>
  <title>Edit Article</title>

<!-- tinyMCE -->
<script language="javascript" type="text/javascript" src="./jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
        // Notice: The simple theme does not use all options some of them are limited to the advanced theme
        tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins : "advlink,insertdatetime,preview",
        theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,undo,redo, separator, link,unlink, separator, preview",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        // theme_advanced_path_location : "bottom",
        extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
        });
</script>
<!-- /tinyMCE -->

</head>

<body bgcolor="#FFFFFF" link="#0000FF" vlink="#0000FF" alink="#0000FF">

<?php
if(isset($_REQUEST['submit']) == 'Update'){
  if(is_writeable($_REQUEST['file'])){
   $fp = fopen($_REQUEST['file'], "w+");
   $fntxtdescription = str_replace("&lt;br&gt;", "\n", trim($_REQUEST['artdescription']));
   $fntxtdescription = str_replace("<br>", "\n", trim($_REQUEST['artdescription']));
   $fntxtdescription = str_replace("<br />", "\n", trim($_REQUEST['artdescription']));
   fwrite ($fp, stripslashes($_REQUEST['arttitle']). "\n\r" .$fntxtdescription);
   fclose ($fp);

   echo "<div align=\"center\"><big><b>Updated...</b></big></div>";
  } else {
   echo "<div align=\"center\">This file is not writeable: ".$_REQUEST['file']."</div>";
  }
}

echo "<table border=\"0\" align=\"center\" style=\"border-collapse: collapse\" bordercolor=\"#C0C0C0\" cellpadding=\"2\" cellspacing=\"2\">";
echo "<form name=\"FormName\" action=\"\" method=\"post\">\n";
echo "<input name=\"file\" type=\"hidden\" value=\"".$_REQUEST['file']."\">\n";

function mk_txt_line_array($buf){
    $lnarray = split("\n", $buf);

  return $lnarray;
}

$search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
                 "'<[/!]*?[^<>]*?>'si",          // Strip out HTML tags
                 //"'([rn])[s]+'",                // Strip out white space
                 "'&(quot|#34);'i",                // Replace HTML entities
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(d+);'e");                    // evaluate as php

$replace = array ("",
                 "",
                 //"\1",
                 "\"",
                 "&",
                 "<",
                 ">",
                 " ",
                 chr(161),
                 chr(162),
                 chr(163),
                 chr(169),
                 "chr(\1)");

$artpth = $_REQUEST['file'];

$fp = fopen($artpth, "r");
$buf = fread($fp, filesize($artpth));
fclose($fp);

//echo "<br /><br />$buf";
//echo "$buf";
//$buf = str_replace("'", "", $buf);
//$buf = str_replace("!", "", $buf);

$file_ext = substr(strrchr($artpth, '.'), 1);

if($file_ext == "htm" or $file_ext == "html"){

$fntext = preg_replace($search, $replace, $buf);

$resultln = mk_txt_line_array(trim($fntext));

$txttitle = trim($resultln[0]);

if($txttitle == "\r"){
$txttitle = trim($resultln[1]);
}

$txtdescription = str_replace($txttitle, "", trim($fntext));

$txttitle = preg_replace($search, $replace, $txttitle); //must be after $txtdescription

$txtdescription = str_replace("\n", "<br>", trim($txtdescription));
$txtdescription = str_replace("’", "'", trim($txtdescription));
$txtdescription = str_replace("“", "\"", trim($txtdescription));
$txtdescription = str_replace("”", "\"", trim($txtdescription));
$txtdescription = str_replace("–", "-", trim($txtdescription));
$txtdescription = htmlentities(trim($txtdescription));

echo "<tr><td><b>Title:</b></td><td><input name=\"arttitle\" type=\"text\" size=\"80\" value=\"" . trim($txttitle) . "\"></td></tr>";
echo "<tr><td><b>Body:</b></td><td><textarea name=\"artdescription\" rows=15 cols=60 wrap=\"off\">".trim($txtdescription)."</textarea></td></tr>";
}

if($file_ext == "txt"){

$resultln = mk_txt_line_array(trim($buf));

$txttitle = trim($resultln[0]);

if($txttitle == "\r"){
$txttitle = trim($resultln[1]);
}

$txtdescription = str_replace($txttitle, "", trim($buf));

$txtdescription = str_replace("\n", "<br>", trim($txtdescription));
$txtdescription = str_replace("’", "'", trim($txtdescription));
$txtdescription = str_replace("“", "\"", trim($txtdescription));
$txtdescription = str_replace("”", "\"", trim($txtdescription));
$txtdescription = str_replace("–", "-", trim($txtdescription));
$txtdescription = htmlentities(trim($txtdescription));

$txttitle = preg_replace($search, $replace, $txttitle); //must be after $txtdescription

echo "<tr><td><b>Title:</b></td><td><input name=\"arttitle\" type=\"text\" size=\"80\" value=\"" . trim($txttitle) . "\"></td></tr>";
echo "<tr><td><b>Body:</b></td><td><textarea name=\"artdescription\" rows=15 cols=60 wrap=\"off\">".trim($txtdescription)."</textarea></td></tr>";
}

if(is_writeable($artpth)){
   echo "<tr><td><b>Writeable:</b></td><td>Yes</td></tr>";
} else {
   echo "<tr><td><b>Writeable:</b></td><td>No</td></tr>";
}
?>

<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Update"> |
<script language="JavaScript">
<!--
document.write('<a href="javascript:self.close()">Close this window</a>');
// -->
</script>
</td></tr>

</form></table>

</body>

</html>