<?php

// A strange sort of line editor
// copyright 2005 by srirupa deadwyler under the GPL.
// throw into a directory and go to http://path/to/slED.php?-h

$qs = $_ENV['QUERY_STRING'];
$qs = str_replace("%20", " ", $qs);
$loc = $_ENV['SCRIPT_URI'];
if (basename($loc) == "index.php") {
 $loc = dirname($loc);
}

// help page

if ($qs == "-h") {
 print "<big><b>SLEd.php : a web based text editor.</b></big><br /><br />";
 print "SLEd is line oriented (sort of like ed).<br />";
 print "SLEd is url based, so commands are of the form <b>http://url_of_SLEd?[command]</b><br /><br />";
 print "commands:<br />";
 print "'<b>-h</b>' : show this help document<br />";
 print "'<b>-ls [path]</b>' directory listing for path<br />";
 print "'<b>-o [filename]</b>' : opens [filename] in buffer<br />";
 print "'<b>-s [filename]</b>' : saves buffer as [filename]<br />";
 print "'<b>-q</b>' : clears buffer<br />";
 print "'<b>-d [filename]</b>' : deletes [filename]. careful!<br />";
 print "'<b>d[line number]</b>' : deletes line from buffer<br />";
 print "'<b>r[line number] [text]</b>' : replaces line with new text<br />";
 print "'<b>i[line number] [text]</b>' : inserts text as new line after specified line. use 0 to insert before line 1.<br />";
 print "'<b> [text]</b>' : appends text to buffer as new line. (remember the space!)<br /><br />";
 print "notes:<br />";
 print "1) #'s are not passed correctly, therefore use ::: when you mean #<br />"; 
 print "2) note the spaces in commands. these are neccessary<br />";
 print "3) files read into buffer can be local filenames or remote urls<br />";
 print "4) filenames can be relative including the use of ../ and such. this is a security risk so be careful!<br />";
 print "5) but this whole thing is completely insecure cause thats the whole point so keep it passworded!<br />";
 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++;
}

?>