SLEd.php : a web based text editor.
";
print "SLEd is line oriented (sort of like ed).
";
print "SLEd is url based, so commands are of the form http://url_of_SLEd?[command]
";
print "commands:
";
print "'-h' : show this help document
";
print "'-ls [path]' directory listing for path
";
print "'-o [filename]' : opens [filename] in buffer
";
print "'-s [filename]' : saves buffer as [filename]
";
print "'-q' : clears buffer
";
print "'-d [filename]' : deletes [filename]. careful!
";
print "'d[line number]' : deletes line from buffer
";
print "'r[line number] [text]' : replaces line with new text
";
print "'i[line number] [text]' : inserts text as new line after specified line. use 0 to insert before line 1.
";
print "' [text]' : appends text to buffer as new line. (remember the space!)
";
print "notes:
";
print "1) #'s are not passed correctly, therefore use ::: when you mean #
";
print "2) note the spaces in commands. these are neccessary
";
print "3) files read into buffer can be local filenames or remote urls
";
print "4) filenames can be relative including the use of ../ and such. this is a security risk so be careful!
";
print "5) but this whole thing is completely insecure cause thats the whole point so keep it passworded!
";
die;
}
// ls command
if (preg_match("/^-ls (.+)/", $qs, $regs)) {
header('Content-type: text/plain');
if ($handle = opendir($regs[1])) {
while (false !== ($file = readdir($handle))) {
$list[] = "$file";
}
}
closedir($handle);
sort($list);
foreach($list as $ls) {
print "$ls\n";
}
die;
}
// open file in buffer
if (preg_match("/^-o (.+)/", $qs, $regs)) {
if (!copy($regs[1], "sled_data")) {
print "cannot open $regs[1]";
}
header("Location: $loc");
die;
}
// save buffer as
if (preg_match("/^-s (.+)/", $qs, $regs)) {
if (!rename("sled_data", $regs[1])) {
print "cannot save $regs[1]";
}
header("Location: $loc");
}
// empty buffer
if ($qs == "-q") {
unlink("sled_data");
header("Location: $loc");
die;
}
// delete file
if (preg_match("/^-d (.+)/", $qs, $regs)) {
unlink("$regs[1]");
header("Location: $loc");
}
// delete line in buffer
if (preg_match("/^d([0-9]+)$/", $qs, $regs)) {
$file = file("sled_data");
$i = 1;
foreach($file as $line) {
if ($i != $regs[1]) {
$temp[] = $line;
}
$i++;
}
$fp = fopen("sled_data", "w");
foreach($temp as $tline) {
fwrite($fp, $tline);
}
fclose($fp);
header("Location: $loc");
die;
}
// replace line in buffer
if (preg_match("/^r([0-9]+?) (.+)/", $qs, $regs)) {
$regs[2] = str_replace("%22", "\"", $regs[2]);
$regs[2] = str_replace(":::", "#", $regs[2]);
$regs[2] = stripslashes($regs[2]);
$file = file("sled_data");
$i = 1;
foreach($file as $line) {
if ($i != $regs[1]) {
$temp[] = $line;
$i++;
} else {
$temp[] = "$regs[2]\n";
$i++;
}
}
$fp = fopen("sled_data", "w");
foreach($temp as $tline) {
fwrite($fp, $tline);
}
fclose($fp);
header("Location: $loc");
die;
}
// insert new line after specified line
if (preg_match("/^i([0-9]+?) (.+)/", $qs, $regs)) {
$regs[2] = str_replace("%22", "\"", $regs[2]);
$regs[2] = str_replace(":::", "#", $regs[2]);
$regs[2] = stripslashes($regs[2]);
$file = file("sled_data");
if ($regs[1] == 0) {
$temp[] = "$regs[2]\n";
}
$i = 1;
foreach($file as $line) {
if ($i != $regs[1]) {
$temp[] = $line;
$i++;
} else {
$temp[] = "$line";
$temp[] = "$regs[2]\n";
$i++;
}
}
$fp = fopen("sled_data", "w");
foreach($temp as $tline) {
fwrite($fp, $tline);
}
fclose($fp);
header("Location: $loc");
die;
}
// append line to buffer
if (preg_match("/^ (.+)/", $qs, $regs)) {
$regs[1] = str_replace("%22", "\"", $regs[1]);
$regs[1] = str_replace(":::", "#", $regs[1]);
$regs[1] = stripslashes($regs[1]);
if (file_exists("sled_data")) {
$fp = fopen("sled_data", "a");
fwrite($fp, "$regs[1]\n");
} else {
$fp = fopen("sled_data", "w");
fwrite($fp, "$regs[1]\n");
}
fclose($fp);
header("Location: $loc");
die;
}
// print buffer to screen
header('Content-type: text/plain');
$file = file("sled_data");
$i = 1;
foreach($file as $line) {
print "[$i] $line";
$i++;
}
?>