DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Wierd Little Online Text Editor

04.28.2005
| 4493 views |
  • submit to reddit
        careful who gets access to this script. as far as i can tell this can only modify files in its own directory (although i didn't explicitly make it that way). im new at perl so careful anyway. questions or comments? (dj_bidi@fastmail.fm)

#!/usr/bin/perl

##         SLEd (Sort Of Like Ed) Editor
## USE:
##  append commands to url after a question mark 
## COMMANDS:
##  open (temp) file:                      -f,[file_to_edit]
##  close (temp)file:                      -d 
##  delete real file (careful!):           -dd
##  insert line (0 inserts at beginning):  -[line_number]a,[new_text] 
##  replace line:                          -[line_number],[new_text]
##  regex replace in line (like s///g):    -[line_number],/[old]/[new]/
##  add line:                              [line_to_add]
##  delete line:                           -[line_number]
##  save over real file:                   . 
##  save as new file:                      .[newfile_name]
## QUIRKS:
##  use ":lb:" to pass a #

$in = $ENV{'QUERY_STRING'};
$in =~ s/%3C/</g;
$in =~ s/%3E/>/g;
$in =~ s/:lb:/#/g;
$in =~ s/%20/ /g;
$in =~ s/%22/"/g;

$url = $ENV{'SCRIPT_URI'};
($name = $url) =~ s/.+\///;
$file = "~SLEd.pl";

if ($in =~ /-f,(.*)/) {
 if ($file ne "") {
  unlink "$file";
 }
 $new = $1;
 $new =~ s/;//g;
 $new =~ s/"//g;
 open (IN, "$new");
 if (length($new) != 0) { 
  open (OUT, ">~$new");
 }
 while ($_ = <IN>) {
  print OUT "$_";
 }
 close IN;
 close OUT;
 rename "$name", "~bak";
 open (IN, "~bak");
 open (OUT, ">$name");
 while ($_ = <IN>) {
  if (length($new) == 0) {
   $_ =~ s/\$file = \".*\"/\$file = \"\"/g;
  } else {
   $_ =~ s/\$file = \".*\"/\$file = \"~$new\"/g;
  } 
  print OUT "$_";
 }
 close IN;
 close OUT;
 unlink "~bak";
 print "location: $url\n\n";
 die;
}

if ($in  eq ".") {
 ($f = $file) =~ s/^~//;
 unlink "$f";
 rename("$file", "$f");
 print "location: $url?-f,\n\n";
 die;
} 

if ($in eq "-dd") {
 ($f = $file) =~ s/^~//;
 unlink "$f";
 unlink "$file";
 print "location: $url?-f,\n\n";
 die;
} 

if ($in =~ /^\.(.+)/) {
 $new = $1;
 $new =~ s/;//g;
 $new =~ s/"//g;
 ($f = $file) =~ s/^~//;
 rename("$file", "$new");
 print "location: $url?-f,\n\n";
 die;
} 

if ($in eq "-d") {
 unlink "$file";
 print "location: $url?-f,\n\n";
 die;
} 

if ($in =~ /^-([0-9]+)a,(.+)/) {
  $i = 1;
  if ($1 == 0) {
   push(@print, "$2\n");
  }
  open (F, "$file");
  while (<F>) {
   if ($i != $1) {
    push(@print, $_);
   } else {
    push(@print, $_);
    push(@print, "$2\n");
   }
   $i++;
  }
  close F; 
  open (F, ">$file");
  foreach $line(@print) {
   print F "$line";
  }
  close F;
  print "location: $url\n\n";

} elsif ($in =~ /^-([0-9]+),(.+)/) {
  $lino = $1;
  $replace = $2;
  $i = 1;
  open (F, "$file");
  while (<F>) {
   if ($i != $lino) {
    push(@print, $_);
   } elsif ($replace =~ m/^\/(.+?)\/(.*)\/$/) {
    $ol = $1;
    $newe = $2;
    ($match = $_) =~ s/$ol/$newe/g;
    push(@print, $match);
   } else {
    push(@print, "$replace\n");
   }
   $i++;
  }
  close F;  
  open (F, ">$file");
  foreach $line(@print) {
   print F "$line";
  }
  close F;
  print "location: $url\n\n";

} elsif ($in =~ /^-([0-9]+)$/) {
 $i = 1;
 open (F, "$file");
 while (<F>) {
  if ($i != $1) {
   push(@print, $_);
  }
  $i++;
 }
 close F;
 open (F, ">$file");
 foreach $line(@print) {
  print F "$line";
 }
 close F;
 print "location: $url\n\n";

} elsif ($in) {
 open (F, ">>$file");
 print F "$in\n";
 close F;
 print "location: $url\n\n";

} else {
 print "Content-type: text/plain\n\n";
 open (F, "$file");
 $i = 1;
 while (<F>) {
  print "[$i] $_";
  $i++;
 }
}