File Directory Php

//
// View Source -> Edit Page -> Preview -> Save Page ... All online!!!!
// file that opens a HTML/PHP file for editing and view preview of Web pages
// right now only supports editing of HTML files over http
// To edit and preview PHP files, Wedit has to be in the server where PHP files are persent
//
//1. System requirements
//2. Installation
//
//1. System Requirements
// Any system which has PHP and a Web server installed.
// ftp server on the system from where the page is opened and has to be uploaded.
// Test Broweres - IE, Mozilla
//
//2. Installation
// i) Untar Wedit.tar.gz
// ii) Copy Wedit directory into /var/www/html (DocumentRoot directory of the Web Server).
// iii) File thats opened should have 777 permission
// iv) set "magic_quotes_gpc = off;" in php.ini and restart Web server
//
//Open the page http://Wedit-server/Wedit/Wedit.php and start working
// Readme file
//Wedit is a Web based HTML page editor. Html files over a http server or on the machine where Wedit is installed can be edited.
//
//Http page : html page on a http server can be accessed by entering the http://servername/path/filename.html
//
//Local page: The documentroot is assumed as /var/www/html. So any files (tested for PHP/html file) inside /var/www/html on the server where Wedit is installed can be opened, Edited, Previewd and Saved.
//
//Save page : For Saving page thats on a HTTP server, user's Login and password has to be provided. Wedit saves files on HTTP servers using ftp (file transfer protocol)
//
//Preview page : HTML/PHP page can be viewed by just the click of a button. To preview a PHP page after making changes, first save the file and then Preview.
// CONDITIONS
// file thats opened should have 777 permission
// set "magic_quotes_gpc = off;" in php.ini and restart Web server
// only files over HTTP can be edited
// To edit files over HTTPS, Wedit must be installed on the server only
// PHP files can be previewed only after saving
// All local files are opened using relative path
// Assumes DocumentRoot as /var/www/html
// Does not support HTTPS
$documentroot = "/var/www/html";
printf("Wedit -- Edit html and PHP pages in Web Browser\n\n");
printf(" \n");
// file to open in browser
$file = $_POST['path'] ? $_POST['path'] : "http://server/index.html"; // should be selected by user
$msg = "";
// Parse the Page path ($file)
// if the first part is HTTP then set $type=http else local folder
$bits = explode("/", $file);
if ($bits[0] == "http:") {
$type = "http";
}
else {
$type = "local";
# $openfile= "../" . $file;
$openfile= $file;
}
if ( $type == "http" )
{
$ftp_server = $bits[2];
$filepath = "";
for($i=3; $i {
$filepath = $filepath . "/" . $bits[$i];
$filepath = "temp.html";
}
$file = $bits[3] ? $file :($file."/index.html");
$filepath = $bits[3] ? $filepath : "/index.html";
$openfile= $file;
}
printf("
We (Wedit) Edit Web pages online\n");
printf("

\n");
// page select form
printf("
\n");
// page select form
printf(" \n");
// Message Block
printf("
\n");
printf("
\n");
printf(" File: \n",$file);
printf(" \n");
printf("
\n");
printf("
\n");
printf(" \n");
printf("
\n");
// Get a file into an array
$lines = @file($openfile) or send2msgblock("Error: in opening Page");
if( $lines )
{
// Loop through our array, show HTML source as HTML source
foreach ($lines as $line_num => $line)
{
$pagesource = $pagesource . htmlspecialchars($line);
}
}
if ( $_GET['action'] == "save" )
{
// save file
$pagesource = $_POST['editbox'];
$pagesource = str_replace("\r","",$_POST['editbox']); // remove '\r' that's appended
// $pagesource = str_replace("\\","",$pagesource); // remove '\r' that's appended
// Logic
// 1. Save the file in /tmp on the machine from where Wedit is running
// 2. Connect to the remote system using ftp, User and password
// 3. put the file in $DOCUMENTROOT/$PATH
// Step 2
$path = $documentroot . "/" . $filepath;
$ftp_user = $_POST['username'];
$ftp_pass = $_POST['password'];
if ($type == "http")
{
if ( !$ftp_user || !$ftp_pass )
{
send2msgblock("Error: Login and Password have to be entered to Save page on the server");
}
else
{
// Step 1
$tempfile = "/tmp/Wedit_pagesource";
$f = fopen($tempfile,"w") or send2msgblock("Error: Cannot Open file");
if ($f)
{
fwrite($f,$pagesource) or send2msgblock("Error: Cannot save file"); //place $pagesource back in file
fclose($f);
}
// set up a connection or die
@$conn_id = ftp_connect($ftp_server) or send2msgblock("Error: Cannot connect to $ftp_server");
// try to login
if ($conn_id && @ftp_login($conn_id, $ftp_user, $ftp_pass))
{
send2msgblock("Connected as $ftp_user@$ftp_server");
}
else
{
send2msgblock("Error: Cannot connect as $ftp_user");
}
// Step 3
if (@ftp_put($conn_id, $path, $tempfile, FTP_ASCII)) {
send2msgblock("successfully uploaded $file");
} else {
send2msgblock("Error: There was a problem while uploading to $file");
}
ftp_close($conn_id);
}
} // save local file
else if ($type == "local")
{
// Step 1
$f = @fopen($openfile,"w") or send2msgblock("Error: Couldn't Open file");
if ($f)
{
@fwrite($f,$pagesource,strlen($pagesource)) or send2msgblock("Error: Couldn't save file"); //place $pagesource back in file
fclose($f);
}
send2msgblock("File is saved on the server");
}
}
// Show the page source in a Text box
printf("\n
\n");
if ($type == "http")
{
$sourcevar = "document.pageform.editbox.value";
}
else if ($type == "local")
{
$localfile = "http://localhost/" . $file ;
// Get a file into an array.
$lines = @file($localfile) or send2msgblock("Error in opening Page");
// Loop through our array, show HTML source as HTML source; and line numbers too.
if ($lines)
{
foreach ($lines as $line_num => $line)
{
$localpagesource = $localpagesource . htmlentities($line);
}
}
$sourcevar = "document.pageform.source.value";
}
// Hidden fields
printf(" \n",$file);
printf(" \n",$type);
printf(" \n",$localpagesource);
// FTP login and password
printf(" \n");
printf(" \n");
#printf(" \n");
printf(" \n");
// Save and Preview buttons
printf(" \n");
// Source code
printf(" ");
printf(" \n");
printf("
\n");
printf(" *Login: \n",$ftp_user);
printf("
\n");
printf(" *Password: \n",$ftp_pass);
printf("
*Login and Password of the http server
 
\n");
printf(" \n");
printf("\n \n");
printf("
\n");
printf(" \n");
// Preview Page
printf(" ");
printf("
\n");
printf("\n");
printf("
\n");
echo "\n\n";
function send2msgblock($showmsg)
{
$msg = $msg . $showmsg . "
";
printf("\n\n");
}
?>