Version: 0.5

Type: Full Script

Category: File Management

License: GNU General Public License

Description: An Online web editor made to go with Virtual FTP. I have had several requests to put this on, so here it is... It needs several improvments, and I welcome suggesstions



<?php

set_magic_quotes_runtime(0);

function show_header($title)
{
	print "<HTML>\n";
	print "<HEAD>\n";
	print "<TITLE>$title</TITLE>\n";
	print "</HEAD>\n";
	print "<BODY BGCOLOR=\"#FFFFFF\">\n";
}

function show_footer()
{
	?>
	</BODY>
	</HTML>
	<?php
}

function check_ext($filename)
{
	$extns = array("html","htm","shtml","php","phps","css","txt","text","js");
	$fileext = substr($filename,strrpos($filename,'.'),strlen($filename));

	while(list($id,$ext) = each($extns))
	{
		$ext = ".$ext";
		if($ext == $fileext)
		{
		return 1;
		}
	}
	return 0;
}

function get_dir_list($directory)
{
	$files = array();
	$numfiles = 0;

	$handle=opendir($directory);

	while($file = readdir($handle))
	{
		if($file != ".")
		{
			if((($directory != ".")||($file != ".."))&&(($directory != ".")||($file != "data")))
			{
				if(is_file($file) && check_ext($file))
				{
				$files[$numfiles] = $file;
				$numfiles++;
				}
			}
		}
	}

	sort($files);

	reset($files);

	return $files;
}

$hasaccess = 1;

if(!$hasaccess)
{
	// Show Access denied error here
}
else
{
	$action = (isset($action) ? $action : "");

	switch($action)
	{
	case "new":
	case "edit":
		if(isset($file))
		{
			show_header("Web Editor ( '".$file."' )");
			$filedata = join("",file($file));
			$filedata = htmlspecialchars($filedata);
			print "<FONT SIZE=\"+1\">Editing $file</FONT>\n";
			print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";	
			print "<INPUT TYPE=\"hidden\" NAME=\"file\" VALUE=\"".(isset($file) ? $file : "")."\">\n";	
		}
		else
		{
			show_header("Web Editor ( 'Untitled' )");
			$filedata = "";
			print "<FONT SIZE=\"+1\">Editing new file</FONT>\n";
			print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";	
			print "Filename: <INPUT TYPE=\"text\" NAME=\"file\" VALUE=\"Untitled\">\n";	
			print "<BR>\n";	
		}

		print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"save\">\n";	
		print "<TEXTAREA NOWRAP WRAP=\"off\" STYLE=\"display: block; white-space: pre;\" NAME=\"filecontent\" ROWS=20 COLS=55 NOWRAP>\n";
		print $filedata;
		print "</TEXTAREA>\n";
		print "<TABLE BORDER=0>\n";
		print "<TR>\n";
		print "<TD>\n";
		print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Save\">\n";	
		print "</FORM>\n";
		print "</TD>\n";
		print "<TD>\n";
		print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";

		if(isset($file))
		{
			print "<INPUT TYPE=\"hidden\" NAME=\"file\" VALUE=\"".(isset($file) ? $file : "")."\">\n";	
		}
		
		print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"cancel\">\n";
		print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Cancel\">\n";	
		print "</FORM>\n";
		print "</TD>\n";
		print "</TR>\n";
		print "</TABLE>\n";
		show_footer();
		break;
	case "rename":
		show_header("Online Web Editor");
		print "Renaming: $file\n";
		print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";
		print "New Name: <INPUT TYPE=\"text\" NAME=\"newfile\" SIZE=\"20\"VALUE=\"\">\n";	
		print "<BR><BR>\n";
		print "<TABLE BORDER=0 CELLSPACING=4 CELLPADDING=0>\n";
		print "<TR>\n";
		print "<TD ALIGN=\"center\" VALIGN=\"middle\">\n";
		print "<INPUT TYPE=\"hidden\" NAME=\"file\" VALUE=\"".(isset($file) ? $file : "")."\">\n";	
		print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"rename-check\">\n";
		print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Rename\">\n";	
		print "</TD>\n";
		print "</FORM>\n";
		print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";	
		print "<TD ALIGN=\"center\" VALIGN=\"middle\">\n";
		print "<INPUT TYPE=\"hidden\" NAME=\"file\" VALUE=\"".(isset($file) ? $file : "")."\">\n";	
		print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"cancel\">\n";
		print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Cancel\">\n";	
		print "</TD>\n";
		print "</FORM>\n";
		print "</TR>\n";
		print "</TABLE>\n";
		show_footer();
		break;
	case "delete":
		show_header("Online Web Editor");
		print "<CENTER>\n";
		print "<FONT SIZE=\"+1\">Are you sure you want to permanently remove the file $file?</FONT>\n";
		print "<CENTER>\n";
		print "<TABLE BORDER=0>\n";
		print "<TR>\n";
		print "<TD>\n";
		print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";	
		print "<TD ALIGN=\"center\" VALIGN=\"middle\">\n";
		print "<INPUT TYPE=\"hidden\" NAME=\"file\" VALUE=\"".(isset($file) ? $file : "")."\">\n";		
		print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"delete-ok\">\n";
		print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Yes\">\n";	
		print "</TD>\n";
		print "</FORM>\n";
		print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";	
		print "<TD ALIGN=\"center\" VALIGN=\"middle\">\n";
		print "<INPUT TYPE=\"hidden\" NAME=\"file\" VALUE=\"".(isset($file) ? $file : "")."\">\n";	
		print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"cancel\">\n";
		print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"No\">\n";	
		print "</TD>\n";
		print "</FORM>\n";
		print "</TR>\n";
		print "</TABLE>\n";
		print "</CENTER>\n";
		show_footer();
		break;
	case "delete-ok":
		if(file_exists($file))
		{
		unlink($file);
		}
	case "rename-check":
	case "rename-ok":
		if(isset($file) && isset($newfile))
		{
			if((file_exists($newfile))&&($action != "rename-ok"))
			{
			show_header("Online Web Editor");
			print "<CENTER>\n";
			print "<FONT SIZE=\"+1\">The file $newfile aready exists. If you rename $file to $newfile, $newfile wil be destroyed! Do you want to continue?</FONT>\n";
			print "<CENTER>\n";
			print "<TABLE BORDER=0>\n";
			print "<TR>\n";
			print "<TD>\n";
			print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";	
			print "<TD ALIGN=\"center\" VALIGN=\"middle\">\n";
			print "<INPUT TYPE=\"hidden\" NAME=\"file\" VALUE=\"".(isset($file) ? $file : "")."\">\n";	
			print "<INPUT TYPE=\"hidden\" NAME=\"newfile\" VALUE=\"$newfile\">\n";	
			print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"rename-ok\">\n";
			print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Yes\">\n";	
			print "</TD>\n";
			print "</FORM>\n";
			print "<FORM METHOD=\"POST\" ACTION=\"webedit.php\">\n";	
			print "<TD ALIGN=\"center\" VALIGN=\"middle\">\n";
			print "<INPUT TYPE=\"hidden\" NAME=\"file\" VALUE=\"".(isset($file) ? $file : "")."\">\n";	
			print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"rename\">\n";
			print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"No\">\n";	
			print "</TD>\n";
			print "</FORM>\n";
			print "</TR>\n";
			print "</TABLE>\n";
			print "</CENTER>\n";
			show_footer();
			break;
			}
			else
			{
				if(file_exists($newfile))
				{
				unlink($newfile);
				}
				copy($file,$newfile);
				unlink($file);
			}
		}
	case "save":
	case "cancel":
	default:
		if(isset($file)&&($action == "save"))
		{
			$filecontent = stripslashes($filecontent);
			$filehandle = fopen($file,"w");
			fwrite($filehandle,$filecontent);
			fclose($filehandle);
		}
		show_header("Online Web Editor");
		$files = get_dir_list(".");
		$numfiles = sizeof($files);
		print "<CENTER>\n";
		print "<FONT SIZE=\"+1\">Online Web Editor</FONT>\n";
		print "<TABLE WIDTH=\"90%\" BORDER=1>\n";
		print "<TR>\n";
		print "<TD>&nbsp;</TD><TD COLSPAN=3><A HREF=\"webedit.php?action=new\">Create a new file</A></TD>\n";
		print "</TR>\n";
		for($filenum = 0; $filenum < $numfiles; $filenum++)
		{
			$filename = $files[$filenum];
			print "<TR>\n";
			print "<TD>$filename</TD>\n";
			print "<TD><A HREF=\"webedit.php?action=edit&file=".rawurlencode($filename)."\">Edit</A></TD>\n";
			print "<TD><A HREF=\"webedit.php?action=rename&file=".rawurlencode($filename)."\">Rename</A></TD>\n";
			print "<TD><A HREF=\"webedit.php?action=delete&file=".rawurlencode($filename)."\">Delete</A></TD>\n";
			print "</TR>\n";
		}
		print "</TABLE>\n";
		print "</CENTER>\n";
		show_footer();
		break;
	}
}
?>