<?php
################################################################################
#preditor vs. 0.5:: php file editor
#Author: Rick Hopkins
#Date: October 22, 2002
#This Program Will Ask For The Path Of The File
#To Be Re-Written And Then Re-Writes According To
#To The Given Criteria
################################################################################
class preditor_class
{ #class in session
var $name; #name of file
var $type; #type of file
var $tmp_name; #temporary name given to file
var $error; #1 for error, 0 for no error
var $size; #size of file
var $type_ok; #mime types that are okay to allow editing
var $direct; #default directory for storing uploads
############################################
#function frw_class() sets up the needed variables for file rewrite program
#& runs the correct function based upon $_GET variable passed to it
function preditor_class($gt)
{
#setup $direct variable //default directory for uploads
$this->direct = "upload/";
#direct page according to $_GET ($gt) variable
$key = key($gt);
$val = $gt[$key];
switch ($key){
case "file":
#check to see if $_SESSION already exist
if (!$_SESSION){
if (!$_FILES["filename"]["error"] > 0){
#setup some variables
$this->name = $_FILES["filename"]["name"];
$this->type = $_FILES["filename"]["type"];
$this->tmp_name = $_FILES["filename"]["tmp_name"];
$this->error = $_FILES["filename"]["error"];
$this->size = $_FILES["filename"]["size"];
#get file extension
$split = explode(".", $this->name);
$ext = $split[1];
$file_name = $split[0];
#copy file to tmp dir
move_uploaded_file($_FILES["filename"]["tmp_name"], "tmp/".$this->name);
#set up sessions for remembering things
$_SESSION["name"] = $this->name;
$_SESSION["type"] = $this->type;
$_SESSION["tmp_name"] = $this->tmp_name;
$_SESSION["error"] = $this->error;
$_SESSION["size"] = $this->size;
$_SESSION["ext"] = $ext;
$_SESSION["file_name"] = $file_name;
$_SESSION["temp_file"] = "tmp/".$this->name;
#form page
$this->create_html_head();
$this->create_fileinfo_page($this->name, $ext,$this->type, $this->tmp_name, $this->error, $this->size);
$this->create_html_foot();
} else {
$redirect = sprintf("index.php?err=%d", $_FILES["filename"]["error"]);
header("Location: $redirect");
}
} else {
#form page
$this->create_html_head();
$this->create_fileinfo_page($_SESSION["name"], $_SESSION["ext"],$_SESSION["type"], $_SESSION["tmp_name"], $_SESSION["error"], $_SESSION["size"]);
$this->create_html_foot();
}
break;
case "wft":
if ($_POST["choice"] == "choose"){
unlink($_SESSION["temp_file"]);
session_destroy();
$redirect = "index.php";
header("Location:$redirect");
} else {
$this->create_html_head();
$this->create_wftcopy_page();
$this->create_html_foot();
}
break;
case "wft_copy":
if (!(@copy($_SESSION["temp_file"], $_POST["wft_directory"]))){
$redirect = "index.php?wft=1&err=1";
} else {
unlink($_SESSION["temp_file"]);
session_destroy();
$redirect = "index.php?file_copy=1";
}
header("Location:$redirect");
break;
case "rft":
$this->create_html_head();
$this->create_rftcopy_page();
$this->create_html_foot();
break;
case "rft_copy":
if (!(@copy($_SESSION["temp_file"], $_POST["rft_directory"]))){
$redirect = "index.php?rft=1&err=1";
} else {
unlink($_SESSION["temp_file"]);
session_destroy();
$redirect = "index.php?file_copy=1";
}
header("Location:$redirect");
break;
case "file_copy":
$this->create_html_head();
$this->create_initial_page();
$this->create_html_foot();
break;
case "print":
$this->create_printable_page();
break;
case "editfile":
#form page
$this->create_html_head();
$this->create_editfile_page($_SESSION["name"], $_SESSION["ext"],$_SESSION["type"], $_SESSION["tmp_name"], $_SESSION["error"], $_SESSION["size"]);
$this->create_html_foot();
break;
case "edit_done":
$this->create_html_head();
$this->create_editdone_page($_SESSION["name"]);
$this->create_html_foot();
break;
case "save_file":
unlink($_SESSION["temp_file"]);
$body = str_replace("<","<",str_replace(">",">",str_replace(""","\"", $_SESSION["contents"])));
$file = $_POST["saveas"];
$fp = fopen($file, "w+");
fwrite($fp, $body);
fclose($fp);
$redirect = "index.php?file_copy=1";
header("Location: $redirect");
break;
default:
if ($_SESSION["temp_file"]){
unlink($_SESSION["temp_file"]);
session_destroy();
}
$this->create_html_head();
$this->create_initial_page();
$this->create_html_foot();
break;
}
}
############################################
#function create_html_head() will create an html header for a visible page
function create_html_head()
{
#For Testing Purposes
#print_r($_SESSION);
print("<html>\n");
print("<head>\n");
print("<title>::Preditor:: php file editor</title>\n");
print("<style>\n");
print("body {margin:15px; background-color:#dddddd;}\n");
print("h1 {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: #333333; margin:0px; padding:0px;}\n");
print(".inputs {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: #333333; margin:0px; padding:0px; background-color:#cccccc; border:1px solid #000000;}\n");
print("a {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: GREEN; margin:0px; padding:0px; text-decoration:none;}\n");
print("a:hover {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: GREEN; margin:0px; padding:0px; text-decoration:underline;}\n");
print("#main {border:1px solid GREEN; padding:0px; margin:0px;}\n");
print(".dark {background-color:#bbbbbb;}\n");
print(".warn {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: RED; margin:0px; padding:0px;}\n");
print("</style>\n");
print("</head>\n");
print("<body>\n");
print("<table align=\"center\" id=\"main\">\n");
print("<tr>\n");
print("<td colspan=\"10\"><h1><a href=\"index.php\"><img src=\"graphics/preditor_logo.gif\" border=\"0\"></a></h1></td>");
print("</tr>\n");
}
############################################
#function create_html_foot() will close the html page
function create_html_foot()
{
print("</table>\n");
print("</body>\n");
print("</html>");
}
############################################
#function create_initial_page() will set up inputs for telling which file to work with
function create_initial_page()
{
print("<tr>\n");
print("<td class=\"dark\" width=\"400\" colspan=\"2\"><h1><b>Description:</b><br>This program was written to make my job a little easier. Since I spend much of my time re-writing already existing code, I decided to make a program where I could just select the file, put in some criteria, and have it re-written automatically. <u>At any time simple click on the logo at the top to erase all sessions and start fresh.</u></h1></td>\n");
print("</tr>\n");
print("<form method=\"post\" name=\"fileselect\" action=\"index.php?file=1\" enctype=\"multipart/form-data\"><tr>\n");
print("<td><h1>Select A File:</h1></td>");
print("<td><h1><input type=\"file\" class=\"inputs\" name=\"filename\"></h1></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td colspan=\"2\"><h1><a href=\"#\" onclick=\"document.fileselect.submit();\">Next Step...</a></h1></td>");
#<a href=\"#\" onclick=\"document.fileselect.reset();\">Clear Form...</a>
print("</tr></form>\n");
if ($_GET["file_copy"]){
print("</table>\n");
print("<table id=\"main\" align=\"center\">\n");
print("<tr>\n");
print("<td class=\"dark\"><h1 class=\"warn\"><b>Notice</b>: File Copied Correctly!</h1></td>");
print("</tr>\n");
}
if ($_GET["err"]){
print("</table>\n");
print("<table id=\"main\" align=\"center\">\n");
print("<tr>\n");
print("<td class=\"dark\"><h1 class=\"warn\"><b>Error</b>: There Was An Error In The File Upload. Try Again!</h1></td>");
print("</tr>\n");
}
}
############################################
#function create_fileinfo_page() will display a page with all the information about a particular file
#and some options on what to do with it
function create_fileinfo_page($name, $ext, $type, $tmp_name, $error, $size)
{
#testing purposes
#print_r($_FILES);
print("<tr>\n");
print("<td class=\"dark\" colspan=\"2\"><h1><b>File Information:</b></h1></td>\n");
print("</tr>\n");
switch ($error){
case 0:
#array of ok extension types for editing
$type_ok = array(0 => "php", 1 => "txt", 2 => "html", 3 => "htm", 4 => "js", 5 => "htaccess", 6 => "LOG", 7 => "log", 8 => "inc");
#start page
print("<tr>\n");
print("<td><h1>Filename:</h1></td>\n");
printf("<td><h1>%s</h1></td>\n", $name);
print("</tr>\n");
print("<tr>\n");
print("<td><h1>Type:</h1></td>\n");
printf("<td><h1>%s</h1></td>\n", $type);
print("</tr>\n");
print("<tr>\n");
print("<td><h1>Temporary Name:</h1></td>\n");
printf("<td><h1>%s</h1></td>\n", $tmp_name);
print("</tr>\n");
print("<tr>\n");
print("<td><h1>Size:</h1></td>\n");
printf("<td><h1>%sK</h1></td>\n", $size);
print("</tr>\n");
if (!(in_array($ext, $type_ok))){
print("<tr>\n");
print("<td class=\"dark\" colspan=\"2\"><h1 class=\"warn\"><b>Notice:</b><br>This file type ($ext) can not be edited.<br>It can be copied over to a directory however.</h1></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td colspan=\"2\"><h1>What would you like to do?</h1></td>\n");
print("</tr>\n");
print("<form method=\"post\" action=\"index.php?wft=1\" name=\"wft\"><tr>\n"); #wft = wrong file type
print("<td><h1>Copy To Directory? <input type=\"radio\" name=\"choice\" value=\"copy\"></h1></td>\n");
print("<td><h1>Choose Another File? <input type=\"radio\" name=\"choice\" value=\"choose\"></h1></td>\n");
print("</tr></form>\n");
print("<tr>\n");
print("<td colspan=\"2\"><h1><a href=\"#\" onclick=\"document.wft.submit();\">Next Step...</a></h1></td>\n");
print("</tr>\n");
}
if (in_array($ext, $type_ok)){
$file = file($_SESSION["temp_file"], "r");
$count = count($file);
$num = 0;
print("</table>\n");
print("<table align=\"center\" id=\"main\">\n");
print("<tr>\n");
print("<td class=\"dark\"><h1><b>File Contents</b>...</h1></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td class=\"dark\"><h1><textarea name=\"contents\" class=\"inputs\" cols=\"150\" rows=\"20\" readonly=\"readonly\">\n");
while ($num < $count){
#for line numbers in text
#$line = ($num + 1).": "; str_pad($line, 6).
print(str_replace("\"",""",str_replace(">",">",str_replace("<","<",str_replace(" "," ",$file[$num])))));
$num++;
}
print("\n</textarea></h1></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td class=\"dark\"><h1>What would you like to do? ... <a href=\"index.php?editfile=1\">Edit File?</a> ... <a href=\"index.php?rft=1\">Copy To Dir?</a> ... <a href=\"index.php?print=1\">Print File?</a> ... <a href=\"index.php\">Exit...</a></h1></td>\n");
print("</tr>\n");
}
break;
default:
print("<tr>\n");
print("<td><h1>There was an error in uploading the file.<br>Please <a href=\"index.php\">Try Again</a>.</h1></td>\n");
print("</tr>\n");
break;
}
}
############################################
#function create_wftcopy_page() will allow the user to choose where they would like
#to copy the file to in the event that it cannot be edited
function create_wftcopy_page()
{
print("<tr>\n");
print("<td colspan=\"2\"><h1>Where would you like to copy the file to?</h1></td>");
print("</tr>\n");
print("<form method=\"post\" action=\"index.php?wft_copy=1\" name=\"wftcopy\"><tr>\n");
print("<td><h1>Directory Path:</h1></td>");
printf("<td><h1><input type=\"text\" class=\"inputs\" name=\"wft_directory\" value=\"%s%s.%s\"></h1></td>\n", $this->direct, $_SESSION["file_name"], $_SESSION["ext"]);
print("</tr>\n");
print("<tr>\n");
print("<td colspan=\"2\"><h1><a href=\"#\" onclick=\"document.wftcopy.submit();\">Next Step...</a> <a href=\"index.php\">Cancel...</a></h1></td>\n");
print("</tr></form>\n");
if ($_GET["err"] == 1){
print("</table>\n");
print("<table id=\"main\" align=\"center\">\n");
print("<tr>\n");
print("<td class=\"dark\"><h1 class=\"warn\"><b>!Error!</b><br>There was an error in the copy execution.<br>Please ensure that your path name is<br>valid, as well as the extension.</h1></td>");
print("</tr>\n");
}
}
############################################
#function get_perms() finds the file permissions and displays them in a readable fashion
#only used during testing (could take out and be fine)
function get_perms($file)
{
$p_bin = substr(decbin(fileperms($file)), -9);
$p_arr = explode(".", substr(chunk_split($p_bin, 1, "."), 0, 17));
$perms = "";
$i = 0;
foreach ($p_arr as $this){
$p_char = ( $i%3==0 ? "r" : ( $i%3==1 ? "w" : "x" ) );
$perms .= ( $this=="1" ? $p_char : "-" ) . ( $i%3==2 ? " " : "" );
$i++;
}
return $perms;
}
############################################
#function create_rftcopy_page() will copy the file to said directory if file type is ok
function create_rftcopy_page()
{
print("<tr>\n");
print("<td colspan=\"2\"><h1>Where would you like to copy the file to?</h1></td>");
print("</tr>\n");
print("<form method=\"post\" action=\"index.php?rft_copy=1\" name=\"rftcopy\"><tr>\n"); #rft = right file type
print("<td><h1>Directory Path:</h1></td>");
printf("<td><h1><input type=\"text\" class=\"inputs\" name=\"rft_directory\" value=\"%s%s.%s\"></h1></td>\n", $this->direct, $_SESSION["file_name"], $_SESSION["ext"]);
print("</tr>\n");
print("<tr>\n");
print("<td colspan=\"2\"><h1><a href=\"#\" onclick=\"document.rftcopy.submit();\">Next Step...</a> <a href=\"index.php\">Cancel...</a></h1></td>\n");
print("</tr></form>\n");
if ($_GET["err"] == 1){
print("</table>\n");
print("<table id=\"main\" align=\"center\">\n");
print("<tr>\n");
print("<td class=\"dark\"><h1 class=\"warn\"><b>!Error!</b><br>There was an error in the copy execution.<br>Please ensure that your path name is<br>valid, as well as the extension.</h1></td>");
print("</tr>\n");
}
}
############################################
#function create_printable_page() will allow the user to print off the file
function create_printable_page()
{
#create temp.txt file for printing
$body .= sprintf("<html>\n");
$body .= sprintf("<head>\n");
$body .= sprintf("<title>File Re-Writer Program:: Author: Rick Hopkins</title>\n");
$body .= sprintf("<style>\n");
$body .= sprintf("body {margin:15px; background-color:#dddddd;}\n");
$body .= sprintf("h1 {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: #333333; margin:0px; padding:0px;}\n");
$body .= sprintf("h2 {font-family:Courier; font-weight: normal; font-size: 7px; color: #000000; margin:0px; padding:0px;}\n");
$body .= sprintf(".inputs {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: #333333; margin:0px; padding:0px; background-color:#eeeeee; border:1px solid #000000;}\n");
$body .= sprintf("a {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: GREEN; margin:0px; padding:0px; text-decoration:none;}\n");
$body .= sprintf("a:hover {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: GREEN; margin:0px; padding:0px; text-decoration:underline;}\n");
$body .= sprintf("#main {border:1px solid GREEN; padding:0px; margin:0px;}\n");
$body .= sprintf(".dark {background-color:#bbbbbb;}\n");
$body .= sprintf(".warn {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: RED; margin:0px; padding:0px;}\n");
$body .= sprintf("</style>\n");
$body .= sprintf("</head>\n");
$body .= sprintf("<body onload=\"window.print();\">\n");
$body .= sprintf("<table align=\"center\" id=\"main\">\n");
$body .= sprintf("<tr>\n");
$body .= sprintf("<td><h1><a href=\"../index.php\"><img src=\"../graphics/frw_logo.gif\" border=\"0\"></a></h1></td>");
$body .= sprintf("</tr>\n");
$body .= sprintf("<tr>\n");
$body .= sprintf("<td class=\"dark\"><h1>Filename: %s</h1></td>\n", $_SESSION["name"]);
$body .= sprintf("</tr>\n");
$body .= sprintf("<tr>\n");
$body .= sprintf("<td><pre><h2>\n");
$file = file($_SESSION["temp_file"], "r");
$count = count($file);
$num = 0;
while ($num < $count){
#for line numbers in text
#$line = ($num + 1).": "; str_pad($line, 6).
$body .= wordwrap(str_replace("<","<",str_replace(">",">",str_replace("\"",""",str_replace(" "," ",$file[$num])))), 150, "<br>");
$num++;
}
$body .= sprintf("</h2></pre></td>");
$body .= sprintf("</tr>\n");
$body .= sprintf("<tr>\n");
$body .= sprintf("<td class=\"dark\"><h1><a href=\"../index.php?file=1\">Click Here</a> To Get Back To The File Information Page</h1></td>");
$body .= sprintf("</tr>\n");
$body .= sprintf("</table>\n");
$body .= sprintf("</body>\n");
$body .= sprintf("</html>");
$tmp_filename = "tmp/temp.html";
$file_2 = fopen($tmp_filename, "w+");
fwrite($file_2, $body);
fclose($file_2);
print("<html>\n");
print("<head>\n");
print("<title>Printable Screen</title>\n");
print("<meta http-equiv=\"Refresh\" content=\"0; url=tmp/temp.html\">\n");
print("<style>\n");
print("body {margin:15px; background-color:#dddddd;}\n");
print("h1 {font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 10px; color: #333333; margin:0px; padding:0px;}\n");
print("</style>\n");
print("</head>\n");
print("<body>\n");
print("<h1 align=\"center\">Redirect... one moment please</h1>");
print("</body>\n");
print("</html>\n");
}
############################################
#function create_editfile_page() will create a page much like create_fileinfo_page() does
#except with the ability to edit the page
function create_editfile_page($name, $ext, $type, $tmp_name, $error, $size)
{
#test purposes
#print_r($_POST);
print("<tr>\n");
print("<td class=\"dark\" colspan=\"2\"><h1><b>File Information:</b></h1></td>\n");
print("</tr>\n");
switch ($error){
case 0:
#array of ok extension types for editing
$type_ok = array(0 => "php", 1 => "txt", 2 => "html", 3 => "htm", 4 => "js", 5 => "htaccess", 6 => "LOG", 7 => "log", 8 => "inc");
#start page
print("<tr>\n");
print("<td><h1>Filename:</h1></td>\n");
printf("<td><h1>%s</h1></td>\n", $name);
print("</tr>\n");
print("<tr>\n");
print("<td><h1>Type:</h1></td>\n");
printf("<td><h1>%s</h1></td>\n", $type);
print("</tr>\n");
print("<tr>\n");
print("<td><h1>Temporary Name:</h1></td>\n");
printf("<td><h1>%s</h1></td>\n", $tmp_name);
print("</tr>\n");
print("<tr>\n");
print("<td><h1>Size:</h1></td>\n");
printf("<td><h1>%sK</h1></td>\n", $size);
print("</tr>\n");
print("</table>\n");
print("<table align=\"center\" id=\"main\">\n");
print("<tr>\n");
print("<td class=\"dark\"><h1><b>File Contents</b>...</h1></td>\n");
print("</tr>\n");
print("<form method=\"post\" action=\"index.php?editfile=1\" name=\"fileedit\"><tr>\n");
print("<td class=\"dark\"><h1><textarea name=\"editcontents\" class=\"inputs\" cols=\"150\" rows=\"20\">\n");
if (!$_POST["editcontents"]){
#Read File
$file = file($_SESSION["temp_file"], "r");
$count = count($file);
$num = 0;
while ($num < $count){
#for line numbers in text
#$line = ($num + 1).": "; str_pad($line, 6).
print(str_replace("\"",""",str_replace(">",">",str_replace("<","<",str_replace(" "," ",$file[$num])))));
$num++;
}
} else {
$from = $_POST["editfrom"];
$to = $_POST["editto"];
$contents = $_POST["editcontents"];
if ($_POST["editfrom"]){
$body = str_replace("\"",""",str_replace(">",">",str_replace("<","<",str_replace("$from","$to",$contents))));
} else {
$body = str_replace("\"",""",str_replace(">",">",str_replace("<","<",$contents)));
}
print($body);
#setup session variable for file contents after editing
$_SESSION["contents"] = $body;
}
print("\n</textarea></h1></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td class=\"dark\"><h1><b>Editing</b>:<br>You can edit this file by simply entering in the string part you want to edit<br>followed by the replacement string. <u>You can also type right in the textarea box</u> to add whatever you would like.<br>From: <input type=\"text\" class=\"inputs\" value=\"<td>\" readonly=\"readonly\"> To: <input type=\"text\" class=\"inputs\" value=\"<td class="name">\" readonly=\"readonly\"></td></h1></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td class=\"dark\"><h1><b>Change</b>: --> in cases of a string occuring multiple times and needing to be changed in each<br>From: <input type=\"text\" class=\"inputs\" name=\"editfrom\"> To: <input type=\"text\" class=\"inputs\" name=\"editto\"></h1></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td class=\"dark\"><h1><b>Reload</b> after making a change. <b>Save</b> only after all changes have been made. <b>Cancel</b> to void all changes.<br><a href=\"#\" onclick=\"document.fileedit.submit();\">Reload File?</a>... <a href=\"index.php?edit_done=1\">Save File? </a>... <a href=\"index.php?file=1\">Cancel Edit?</a>...</h1></td>\n");
print("</tr></form>\n");
break;
default:
print("<tr>\n");
print("<td><h1>There was an error in uploading the file.<br>Please <a href=\"index.php\">Try Again</a>.</h1></td>\n");
print("</tr>\n");
break;
}
#testing purposes
#print_r($_SESSION);
}
############################################
#function create_editdone_page() will allow the user to give a name to the file
#they need to save, whether it stay the same or, they can give it a different name
function create_editdone_page($name)
{
print("<tr>\n");
print("<td colspan=\"2\"><h1>Where would you like to copy the file to?</h1></td>");
print("</tr>\n");
print("<form method=\"post\" action=\"index.php?save_file=1\" name=\"savefile\"><tr>\n");
print("<td class=\"dark\"><h1>Save File As...</h1></td>");
printf("<td class=\"dark\"><h1><input type=\"text\" name=\"saveas\" class=\"inputs\" value=\"%s%s\"></h1></td>\n",$this->direct, $name);
print("</tr>\n");
print("<tr>\n");
print("<td colspan=\"2\"><h1><a href=\"#\" onclick=\"document.savefile.submit();\">Next Step...</a> <a href=\"index.php?file=1\">Cancel Save...</a></h1></td>\n");
print("</tr></form>\n");
if ($_GET["err"] == 1){
print("</table>\n");
print("<table id=\"main\" align=\"center\">\n");
print("<tr>\n");
print("<td class=\"dark\"><h1 class=\"warn\"><b>!Error!</b><br>There was an error in the copy execution.<br>Please ensure that your path name is<br>valid, as well as the extension.</h1></td>");
print("</tr>\n");
}
}
############################################
} #end of class
################################################################################
?>