site version 5.25.28
PHP –– Editor


Date Written: 4/14/10 Last Updated: 8/30/10



The benefit of this program as opposed to other ftp plugins or programs is that this one can be used in other browsers and is connected to your site, so you can use it when you are away from your computer, but still have access to the internet.

This script will allow you to view and edit files on your website.  Lines 1, 7, 8 are included security files that you will need to have in place for security purposes.  These you will create yourself to will check if a user is logged in as admin or has the correct session, etc.  You need to have some security in place so that only the right people can use this file.

On lines 13, 14, and XX where it says test.php you can rename this to whatever default page you want.  This will be the page that first loads for editing when you load the editor page.  You can also simply create the test.php page and leave those lines alone.

With that in place you should be ready to go.  Changelog

<?php session_start();
##   Editor ©2009 - 2010 Joe Vander Jagt
##   as first seen in <a href="http://www.animeviews.com">www.animeviews.com</a>
##   This Notice Must Remain for Legal Use
##
$root="$_SERVER[DOCUMENT_ROOT]";
include $root.'/include/dbconnect.php';
include $root.'/include/loggedincheck.php';
$dir=@$_POST['dir'];
if ($dir=="") $dir=$_GET['dir'];
if ($dir=="") $dir=$_SERVER['DOCUMENT_ROOT'];
$file=$_GET['file'];

if ($_POST['queryButton']=="create file" and $_POST['newfile'] !=""){
$go="$_SERVER[SCRIPT_URI]"."?file=$_POST[newfile]&dir=$dir";$newfile="$dir"."/$_POST[newfile]";
$fh = fopen("$newfile","x+");
fclose($fh);
?><script type="text/javascript">
window.location = "<?php print $go ;?>"
</script><?php
}

elseif ($_POST['queryButton']=="delete file" and $_POST['deletefile'] !=""){
unlink("$dir"."/$_POST[deletefile]");
$location="$_SERVER[PHP_SELF]?file=&dir=$dir";
?><script type="text/javascript">
window.location = "<?php print $location ;?>"
</script><?php
$location="";
}

elseif ($_POST['queryButton']=="rename file" and $_POST['renamefr']!="" and $_POST['renameto']!=""){
rename("$dir"."/$_POST[renamefr]","$dir"."/$_POST[renameto]");
$location="$_SERVER[PHP_SELF]?file=$_POST[renameto]&dir=$dir";
?>
<script type="text/javascript">
window.location = "<?php print $location ;?>"
</script>
<?php
$location="";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<style type="text/css">
textarea{background-color:#FFFFFF;color:black;}
.o {color:orange;
}
a:link {
color:#00FFFF;
text-decoration: none;
}
a:visited {
color:#00FFFF;
text-decoration: none;
}
a:hover {
color: #ffcc33;
text-decoration: none;
}
body{
color:#FFFFFF;
background-color:navy;
}
</style>
<?php
if ($_POST['queryButton']=="Save")
{
$data2=@$_POST['data'];
$n=strlen($data2);$data=mysql_real_escape_string($data);
$handle = fopen("$dir/$file","wb");
fwrite($handle,$data2, strlen($data2));
fclose($handle);
}
if ($file!=""){
$dir=str_replace("//","/",$dir);
$handle = @fopen("$dir/$file","r+b") or exit("bad filname $dir/$file<br><a href=\"$_SERVER[PHP_SELF]\">go back $dir $file</a>");
while(@!feof($handle))
{
$data.=@fgets($handle);
}@fclose($handle);

$yn=$_GET['folder'];}
?>
<div style="width:100%;"><div style="width:75%;float:left;"><form action=<?php echo "$_SERVER[PHP_SELF]"."?file=$_GET[file]&amp;dir=$_GET[dir]&amp;folder=$yn"; ?> method="POST">
contents of the web file:<?php echo " <b><span class='o'>$file</b></span>";?>

<?php if ($file==""){}
else {?>
<br><textarea name="data" rows=45 style="width:100%;scrollbar-base-color:tan;" wrap="off"><?php print htmlentities($data);?>
</textarea>
<?php
}

echo"<input type='hidden' name='dir' value='$dir'>";?>

<br><br><input type='submit' name="queryButton" value="Save">
<div style="width:25%;float:right;text-align:right;">
<input type='submit' name="queryButton" value="create file"> <input type='text' name="newfile"><br>
<input type='submit' name="queryButton" value="delete file"> <input type='text' name="deletefile"><br><br>
<input type='text'   name="renamefr"><br>
<input type='submit' name="queryButton" value="rename file"> <input type='text' name="renameto"></div>
</form>
</div>

<div style="float:left;width:23%;padding-left:2%;">
<?php
if ($dir != "")
{
$handle=@opendir($dir) or exit("bad file name<br> This directory is protected.<a href=\"$_SERVER[HTTP_REFERER]\"> Start over</a>?");
while (false !== ($file = @readdir($handle))) {
$pos = strpos($file, '.');
if ($pos === false) {$file.="--DIRECTORY";}
$data1 .= $file ." \r\n";
}@fclose($handle);
}
$string="";

$data1=explode("\r\n",$data1);
$data1 = DIRsort($data1);
function DIRsort($list){
    $dirSubList = array();
    $subList = array();
    foreach($list as $li){
        if(strstr($li, '--DIRECTORY')) $dirSubList[] = $li;
        else $subList[] = $li;
    }
    sort($dirSubList);
    sort($subList);
    $List = array_merge($dirSubList, $subList);
    return $List;
}
$data1=implode("\r\n",$data1);$data1="\r\n$data1";
$data1=str_replace("\r\n\r\n","",$data1);
$data1=str_replace('..',"",$data1);
$data1=str_replace(". \r\n","",$data1);
?>

<b>contents of folder:</b><br>

<?php
$data1=preg_replace('/(\r\n)(.*?)(\s)/',"<a href=\"$_SERVER[PHP_SELF]"."?file=$2&amp;dir=$dir\">$2</a><br>",$data1);

$data1=explode("<br>",$data1);
foreach($data1 as &$data2)
{$pos=strpos($data2,"DIRECTORY");
if ($pos>0)
{$data2=preg_replace('/file=.*\&amp;/',"file=$file1&amp;",$data2);
preg_match('/>(.*?)-/',$data2,$match);
$match1=$match[1];
$data2=preg_replace('/(dir=.*?)\"/',"$1/$match1&amp;folder=yes\"",$data2);
$data2=str_replace(".//","./",$data2);
unset($match);
}}
$data1=implode("<br>",$data1);
$data1=str_replace("--DIRECTORY","<span class='o'>--DIRECTORY</span>",$data1);
?><div style='height:740px;overflow:auto;scrollbar-base-color:tan;background-color:blue;'><?php print $data1; ?></div>
</div></div>


TAGS: programming, php, flatfile
Comments

Add Comment
 
Daily Bible Quote
#8 of 44

"The lord has created all things for Himself, yes even the wicked for the day of doom."
Proverbs 16:4
10 most recently updated articles.

PHP –– include
14 hours ago.
Webcomics
1 day ago.
Game Links
3 days ago.
Homosexuality
3 days ago.
todo
4 days ago.
copyright 2005-2010