#!/usr/bin/perl
# mvladmin.cgi - administration script for mvl.cgi
# adds/edits/deletes members(users) and documents(dadabase entries)
# created by Gerry Smith <sfguu4658@agt.net>
# last update: Feb. 5, 2004

require 'FormData.pl';
require 'MailForm.pl';
require 'user.pl';
require 'lev1.pl';

#open(STDERR,">error.log");

$mailprog    = '/usr/sbin/sendmail';
$owner       = 'valor@global-dialog.org';
$Script      = $ENV{'SCRIPT_NAME'};
$passwd_file = "ul.txt";

$admincommand = "C:admin";

$publiclist  = $filelist  =  "doclist.txt";

$sitetitle   = "Global Dialog Testbed";

$homelink        = "http://www.global-dialog.org/";
$homelinkstring  = "Global Dialog Home";

$mvdhomelink     = "http://www.global-dialog.org/mvdtestbed/";
$mvdhomelinkstring = "MVD Home";

$listlink        = "mvl.cgi?NextName=List";
$listlinkstring  = "List";

$bgcolor     = "#99CCCC";
$background  = "../graphics/web3.jpg";

$subscribe = "email.txt";

##################################################################

&getFormData(*Form);

#if ($Form{'title'} eq "/private" || $Form{'private'} eq "true" && $Form{'Public'} ne "on" && $Form{'Action'} ne "Preview"){
if ($Form{'Public'} ne "on" && $Form{'Action'} ne "Preview"){

  if (&CheckUser($Form{'uname'},$Form{'pword'},0)){

    $filelist = $Form{'uname'} . ".dat";
    if (!(-e $filelist)) {
      open(FILE,">$filelist") || &Success("Could not open $filelist for writing.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> Administrator.</a>",1);
      print FILE "~$filelist,File list of $Form{'uname'}\n";
      close(FILE);
    }
    
  }
  else {
  &BadCombo("registered member with priviledges to create and edit private files");
  exit;}
}

open(FILE,"$filelist") || &Success("Could not open $filelist for reading.<p>Please report this to the site <a href=mailto:$owner\?subject=MVD_error_report>administrator. </a>",1);
@array = <FILE>;
close(FILE);

foreach $line (@array){
chop($line);
($key,$value) = split(',',$line);
if($value eq ""){ $value = "noname"; }
$Files2Names{$key} = $value;
}

$FileNum = 0;
foreach (sort(keys(%Files2Names))) {
  $ValidFiles[ $FileNum ] = $_;
  $ValidNames[ $FileNum ] = $NextName = $Files2Names{$_};
  $Names2Numbs{$NextName} = $FileNum++; # Used to validate names.
}

unless ($Script) {              # Must be testing in the shell...
  $ENV{'SCRIPT_NAME'}    = $0;
  $ENV{'REQUEST_METHOD'} = 'GET';
  $ENV{'QUERY_STRING'}   = '';
  $ENV{'CONTENT_LENGTH'} = length($ENV{'QUERY_STRING'});
}

if ($Action = $Form{'Action'}) {
  $Action  =  &ChangeUser(*Form) if ($Action eq "Change User");
  $Action  =  &ChangeUser(*Form) if ($Action eq "Change Password");

  $Action  =  &AddUser(*Form) if ($Action eq "Add User");
  $Action  =  &DeleteUser(*Form) if ($Action eq "Delete User");
  $Action  =  &EditEntry(*Form) if ($Action eq "Add/Edit Entry");
  $Action  =  &EditEntry(*Form) if ($Action eq "Login");

  $Action  =  &ConfirmDelete(*Form) if ($Action eq "Delete Entry");
  $Action  =  &DeleteEntry(*Form) if ($Action eq "Delete");
  $Action  =  &delany(*Form) if ($Action eq "Unlink");
  $Action  =  &PreviewEntry(*Form) if ($Action eq "Preview");
  $Action  =  &SaveEntry(*Form) if ($Action eq "Save");
  $Action  =  &changemode(*Form) if ($Action eq "CHMOD");
  &NotYetDefined(*Form);
}
&NoAction;

############### Local Subroutines: ###########################

sub NotYetDefined{
  local (*Form) =@_;
  $Func = $Form{'Action'};

print <<EOF;
Content-type: text/html

<HTML>
<HEAD><TITLE> MVD Warning </TITLE></HEAD>
<BODY BGCOLOR=$bgcolor background=$background><CENTER>
<p><hr size=2><p>
<h1>Undefined MVD Administration Function</h1>
Sorry $Form{'uname'}.
EOF

print " Or should I call you $Form{'ouname'}." if $Form{'ouname'};

print <<EOF;
<p>The function  < $Func > is either not a valid function or has not yet been implemented.<br>
Use your browsers 'Back' button to return whence you came or use the link below.
<p><hr size=2><a href="$homelink" target=_top>[ $homelinkstring ]</a>&nbsp;&nbsp;<a href="$mvdhomelink" target=_top>[ $mvdhomelinkstring ]</a>&nbsp;&nbsp;<a href="$listlink" target=_top> [ $listlinkstring ] </a><hr>
</CENTER></BODY></HTML>
EOF

exit;

}

sub NoAction{

print <<EOF;
Content-type: text/html

<HTML>
<HEAD><TITLE> MVD Warning </TITLE></HEAD>
<BODY BGCOLOR=$bgcolor background=$background><CENTER>
<p><hr size=2><p>
<h1>No Action</h1>
You called this script without the proper form data. You call yourself a hacker?
</CENTER></BODY></HTML>
EOF

exit;
}

sub PreviewEntry{
  local (*Form) = @_;

  $Filename = $Form{'filename'};
  $Filename =~ s/\n+/ /g;
  $Title = $Form{'title'};
  $Title =~ s/\n+/ /g;
  $Description = $Form{'description'};
  
  $Description =~ s/\*\</\*\&lt\;/gi;

#if ($Form{'htmlswitch'} ne "on") { $Description =~ s/\n/\n<br>/gi;}

  $Description =~ s/\&lt\;textarea\&gt\;/<textarea>/gi;
  $Description =~ s/\&lt\;\/textarea\&gt\;/<\/textarea>/gi;

  $Body = $Form{'body'};
  $Head = "" unless ( $Head = $Form{'head'});

  $new = 1;

if ($Form{'htmlswitch'} ne "on") {
print <<EOF;
Content-type: text/html

<HTML><HEAD>
<TITLE>$Title</TITLE>
<!--Head-->$Head<!--EndHead-->
</HEAD>
<!--Body-->$Body<!--EndBody-->
<!--Description-->
$Description
<!--EndDescription-->
<p>
<hr>
<!--Insert Next-->
<p><center><table cellpadding=4 border=4><td>
<small>This is what the page will look like. Use your browser's back button to make further
changes or to save it if you like what you see.</small></td></table></center>
<p>
</BODY></HTML>
EOF
}
elsif ($Filename =~ /\.((\w*)htm(l*)|php)/i ) {
print <<EOF;
Content-type: text/html

$Description<p>

<hr size=2>
<p> <center><table cellpadding=4 border=4><td>
<small>
This is what this page will look like in a browser, unless it is a <i>php</i>
file, in which case you will need to save it and then load it in a browser
before you can see how it will look (this preview feature doesn't support the
parsing of <i>php</i> commands). Use your browser's back button to make
further changes or to save it if you like what you see.
</small> </td></table></center>
<p>
EOF
}
else {
print <<EOF;
Content-type: text/plain

$Description\n\n

---------------------------------------------------------------------------------------\n
This is what this non-html page will look like in a browser. Use your browser's back\n
button to make further changes or to save it if you like what you see.
EOF
}
 
exit;
}

sub SaveEntry{
  local (*Form) = @_;

  $switch = 0;

if((-e $Form{'filename'}) && $Form{'filename'} ne $Form{'origname'}){
  &Success("$Form{'filename'} already exists and is not the file you are currently editing [$Form{'origname'}].<br>The current $Form{'filename'} will have to be deleted before this version can be saved in its place.<p>Use your browser's 'Back' button to go back to choose another name if you still want to save this file.",1);
}

if($Form{'L1ed'} eq "on"){
  &saveany(*Form);
}

if($Form{'mvdswitch'} eq "on"){

  open(FILE, "$filelist") || &Success("SaveEntry could not open $filelist for reading.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
    @lines = <FILE>;
    close(FILE);
    foreach $line (@lines){
      $line =~ s/$Form{'filename'},$Files2Names{$Form{'filename'}}\n//;
    }

  open(FILE,">$filelist") || &Success("SaveEntry could not open $filelist for writing.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator.</a>",1);
  print FILE @lines;
  close(FILE);

  $filelist = $publiclist;
  $switch = 1;
}

  $email = "" unless ($email = $Form{'email'});

  $Filename = $Form{'filename'};
  $Filename =~ s/\n+/ /g;

  $Title = $Form{'title'};
  $Title =~ s/\n+/ /g;
  $Description = $Form{'description'};

  $Description =~ s/\&lt\;textarea\&gt\;/<textarea>/gi;
  $Description =~ s/\&lt\;\/textarea\&gt\;/<\/textarea>/gi;

#check for html ext here
$filext = $Filename;
if ($filext !~ /\.(\w*)htm(l*)/i || $Form{'htmlswitch'} eq "on") { &Savenonht(*Form);}

  $Description =~ s/\n\Z//;
#  $Description =~ s/\n/\n<br>/gi;

  $Body = $Form{'body'};
  $Head = "" unless ( $Head = $Form{'head'});

  $new = 1;

  $message = "< $Filename > successfully created by $Form{'uname'}.\n";

  if (&CheckUser($Form{'uname'},$Form{'pword'},0)){
    foreach $entry (@ValidFiles){
      if ($entry eq $Filename && !$switch){
        if ($Files2Names{$Filename} eq $Title){
          $message = "< $Filename > successfully updated by $Form{'uname'}.\n";
          $new = 0;
        }
        else {
          $new = 1;
        }
      }
    }
    open (FILE,">$Filename") || &Success("SaveEntry could not open $Filename for writing.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);

    print FILE <<EOF;
<HTML><HEAD>
<TITLE>$Title</TITLE>
<!--Head-->$Head<!--EndHead-->
</HEAD>
<!--Body-->$Body<!--EndBody-->
<!--Description-->
$Description
<!--EndDescription-->
<p>
<!--Insert Next-->
</BODY></HTML>
EOF

  close (FILE);

  open(FILE, $filelist) || &Success("SaveEntry could not open $filelist for reading.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
    @lines = <FILE>;
    close(FILE);
    foreach $line (@lines){
      if($new){
        $line =~ s/$Filename,.*\n//;
      }
        else{$line =~ s/$Filename,.*\n/$Filename,$Title\n/;
      }
    }
    open(FILE,">$filelist") || &Success("SaveEntry could not open $filelist for writing.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
    print FILE @lines;
    if($new){print FILE "$Filename,$Title\n";}
    close(FILE);

  &mailForm($owner, $owner, "MVD entry added", "$message\n");

    if ($email ne "")
    {
        &mailForm($email, $owner, "MVD entry added", "$message\n\nFilename: $Filename\nTitle: $Title\nHeader: $Head\nBody Attributes: $Body\nBody: $Description\n");
    }

  if($filelist ne $publiclist){
    &Success("$message<p>Use this link <A HREF=\"$Filename\" target=_top> < $Title ></a> to check your file.<br>You can use your browser's back button to return to the previous page to make changes altho it is best to use the preview option before saving.<p> Use this link to return to your private file list - <A HREF=\"mvladmin.cgi?Action=Add/Edit%20Entry&title=/private&uname=$Form{'uname'}&pword=$Form{'pword'}\" target=_top>[ $Form{'uname'} files ]</a>");
  }
  else{
    &Success("$message<p>Use this link <A HREF=\"mvl.cgi\?shonav=1&NextName=$Filename\"  target=_top> < $Title > </a> to check the entry.<br>You can use your browser's back button to return to the previous page to make changes altho it is best to use the preview option before saving.");
  }

  }
  &BadCombo("registered member");
  exit;
}

sub privateform{

print "Content-type: text/html\n\n";
print <<EOF;
<HTML>
<HEAD><TITLE> $sitetitle Administration </TITLE></HEAD>
<BODY BGCOLOR=$bgcolor background=$background><CENTER>
<p><hr size=2><p>
<h1>$sitetitle Administration: Private files of $Form{'uname'}</h1>
<p>
<font size=-1><a href=#Files> [ Jump to file list ] </a>&nbsp;&nbsp;<a href="$homelink"> [ $homelinkstring ] </a></font><p>

<FORM METHOD=POST ACTION="mvladmin.cgi">
<p>
<a name=Form>
<table border=2 width=534 cellpadding=10 BGCOLOR=lightblue>
<tr valign=middle>
<td align=center valign=middle><b>Your Username</b><br>
<INPUT TYPE=TEXT SIZE=23 NAME="uname"><p>
<b>Your Password</b><br>
<INPUT TYPE=password SIZE=23 NAME="pword"><p></td>
<td align=center valign=middle>
&nbsp;&nbsp;<p>
&nbsp;<br>
<b>New Password</b><br>
<INPUT TYPE=password SIZE=23 NAME="opword"><p>
</td>
</tr>
<tr valign=middle>
<td align=center valign=middle><b>Entry as Title</b><br>
<INPUT TYPE=TEXT SIZE=23 NAME="title"><p></td>
<td align=center valign=middle><b>Entry as File</b><br>
<INPUT TYPE=TEXT SIZE=23 NAME="file"><p></td>
</tr>
</table>
<table border=2 width=534 cellpadding=10 BGCOLOR=gray>
<tr valign=middle>
<td align=center valign=middle>
<INPUT TYPE=SUBMIT NAME="Action" VALUE="Login"> &nbsp;&nbsp;
<INPUT TYPE=checkbox NAME="Public" VALUE="on"> Administration<p>
<INPUT TYPE=SUBMIT NAME="Action" VALUE="Change Password">
</td>
</tr>
<tr valign=middle>
<td align=center valign=middle BGCOLOR=lightgray>
<INPUT TYPE=RESET VALUE=" Reset All Input Boxes "><p></td>
</tr>
</table></form><p>

<a href="upl.php?&uname=$Form{'uname'}&pword=$Form{'pword'}">Upload File</a><p>
EOF

$FileNumb = $FileNum-1;
  print "<a name=Files>There are currently $FileNumb private documents 
for $Form{'uname'}.<br><font size=2>Click on the <i>Title</i> to view 
that document. Click on the <i>Filename</i> to 
edit.<br>Click on the asterix (*) to go to the <i>File 
Stats and Utilities</i> page for that file.</font><p><table 
width=500 border=0><tr><td align=center valign=middle><font color=red>SECURITY WARNING!</td></tr><tr><td>DO NOT leave your computer unattended with this page loaded as each of the edit links below contains your username and password. Shut down your browser when you are finished this session.</font></td></tr></table><p>\n";
  print "<table border=2 cellpadding=5>\n";
  print "<tr valign=middle><td align=center valign=middle>Title</td><td align=center valign=middle>Filename</td><td align=center valign=middle><b><font size=1><sup>Stat</sup><font size=+1>/</font>Util</font></b></td></tr>\n";
  $c1=1;
  for($c=0;$c<$FileNumb;$c++){
    print "<tr valign=middle><td valign=middle><font size=-1>$c1: <A HREF=\"$ValidFiles[$c]\">$ValidNames[$c]</a></font></td><td align=right valign=middle><font size=-1><A HREF=\"mvladmin.cgi\?Action=Add\/Edit%20Entry&title=\/private&uname=$Form{'uname'}&pword=$Form{'pword'}&file=$ValidFiles[$c]\"> $ValidFiles[$c] </a></font></td><td align=center valign=bottom><font size=+1><A HREF=\"privutl.php\?uname=$Form{'uname'}&pword=$Form{'pword'}&File=$ValidFiles[$c]\"> * </a></font></td></tr>";
    $c1++;
  }
  print "</table><p><hr>\n";

print <<EOFF;
<a href="$homelink" target=_top> [ $homelinkstring ] </a>&nbsp;&nbsp;<a href="$mvdhomelink" target=_top> [ $mvdhomelinkstring ] </a>&nbsp;&nbsp;<a href="$listlink" target=_top> [ $listlinkstring ]  </a><hr><p></CENTER></BODY></HTML>
EOFF
exit;
}

sub publicform{

print "Content-type: text/html\n\n";
print <<EOF;
<HTML>
<HEAD><TITLE> $sitetitle Administration </TITLE></HEAD>
<BODY BGCOLOR=$bgcolor background=$background><CENTER>
<p><hr size=2><p>
<h1>$sitetitle Administration: Public MVD files</h1>
<p>
<font size=-1><a href=#Files> [ Jump to file list ] </a>&nbsp;&nbsp;<a href="../"> [ Home ] </a></font><p>

<FORM METHOD=POST ACTION="mvladmin.cgi">
<p>
<table border=2 width=534 cellpadding=10 BGCOLOR="#FFFF99">
<tr valign=middle><td align=center valign=middle>Your Username<br><INPUT TYPE=TEXT SIZE=23 NAME="uname" value="$Form{'uname'}"><p>Your Password<br><INPUT TYPE=password SIZE=23 NAME="pword"><p></td><td align=center valign=middle>Other Username<br><INPUT TYPE=TEXT SIZE=23 NAME="ouname"><p>Other Password<br><INPUT TYPE=password SIZE=23 NAME="opword"><p></td></tr>
<tr valign=middle><td align=center valign=middle>Entry as Title<br><INPUT TYPE=TEXT SIZE=23 NAME="title" value="/private"><p></td><td align=center valign=middle>Entry as File<br><INPUT TYPE=TEXT SIZE=23 NAME="file"><p></td></tr>
</table>
<table border=2 width=534 cellpadding=10 BGCOLOR="#0099CC">
<tr valign=middle><td align=center valign=middle BGCOLOR="#33CCCC">Entry Functions<br>&nbsp;&nbsp;<INPUT TYPE=SUBMIT NAME="Action" VALUE="Add/Edit Entry">&nbsp;&nbsp;<INPUT TYPE=SUBMIT NAME="Action" VALUE="Delete Entry"><p></td></tr>
<tr valign=middle><td align=center valign=middle>Membership Functions<br><INPUT TYPE=SUBMIT NAME="Action" VALUE="Change User">&nbsp;&nbsp;<INPUT TYPE=SUBMIT NAME="Action" VALUE="Add User">&nbsp;&nbsp;<INPUT TYPE=SUBMIT NAME="Action" VALUE="Delete User"><p></td></tr>
<tr valign=middle><td align=center valign=middle BGCOLOR="#FFFF99"><INPUT TYPE=RESET VALUE=" Reset All Input Boxes "><p></td></tr>
</table><p>

<a href="upl.php?&uname=$Form{'uname'}&pword=$Form{'pword'}">Upload File</a><p>
EOF

$FileNumb = $FileNum-1;
  print "<a name=Files>There are currently $FileNumb public mvd documents.<br><font size=2>Click on the <i>Title</i> to view that document. Click on the <i>Filename</i> to edit.</font><p><table width=500 border=0><tr><td align=center valign=middle><font color=red>SECURITY WARNING!</td></tr><tr><td>DO NOT leave your computer unattended with this page loaded as each of the edit links below contains your username and password. Shut down your browser when you are finished this session.</font></td></tr></table><p>\n";
  print "<table border=2 cellpadding=5>\n";
  print "<tr valign=middle><td align=center valign=middle>Title</td><td align=center valign=middle>Filename</td><td align=center valign=middle><b><font size=1><sup>Stat</sup><font size=+1>/</font>Util</font></b></td></tr>\n";
  $c1=1;
  for($c=0;$c<$FileNumb;$c++){
    print "<tr valign=middle><td valign=middle><font size=-1>$c1: <A HREF=\"$ValidFiles[$c]\">$ValidNames[$c]</a></font></td><td align=right valign=middle><font size=-1><A HREF=\"mvladmin.cgi\?Action=Add\/Edit%20Entry&title=\/private&uname=$Form{'uname'}&pword=$Form{'pword'}&file=$ValidFiles[$c]\"> $ValidFiles[$c] </a></font></td><td align=center valign=bottom><font size=+1><A HREF=\"privutl.php\?uname=$Form{'uname'}&pword=$Form{'pword'}&File=$ValidFiles[$c]\"> * </a></font></td></tr>";
    $c1++;
  }
  print "</table></form><p><hr>\n";

print <<EOFF;
<a href="$homelink" target=_top> [ $homelinkstring ] </a>&nbsp;&nbsp;<a href="$mvdhomelink" target=_top> [ $mvdhomelinkstring ] </a>&nbsp;&nbsp;<a href="$listlink" target=_top> [ $listlinkstring ]  </a><hr><p></CENTER></BODY></HTML>
EOFF
exit;
}

sub EditEntry{
  local (*Form) = @_;

  $nhtml = 0;
  $lev1edit = 0;

  if($Form{'Public'} eq "on" && !$Form{'title'} && !$Form{'file'}){
    &publicform;
  }

  if($Form{'title'} eq "/private" && !$Form{'file'} || $Form{'Public'} ne "on" && !$Form{'title'}){
    &privateform;
  }
  elsif($Form{'title'} eq "/list"){
    &listall;
  }
  elsif($Form{'title'} eq "/edit"){
    &editany;
  }
  elsif($Form{'title'} eq "/chmod"){
    &chmodform;
  }

if ($Form{'file'} && (-e $Form{'file'})){

    $permission = 0;

    open(FILE, "$filelist") || &Success("EditEntry could not open $filelist for reading.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
    @lines = <FILE>;
    close(FILE);
    foreach $line (@lines){
      if ($line =~ /\b$Form{'file'}/) { $permission = 1;}
    }

    if ($permission ne 1) {
      if($filelist eq $publiclist) {
        &Success("$Form{'file'} already exists as a private file.<br>If it is your private file you will need to use the <i>/private</i> keyword in the <i>Title</i> field and also supply your username and password to prove you are allowed to access this file.<p>Use your browser's 'Back' button to go back and retry.",1);
      }
      else {
        &Success("$Form{'file'} already exists as a public file or as someone else's private file.<br>If it is a public file you will need to access it without using the <i>/private</i> keyword in the <i>Title</i> field. If it is a private file then you supplied the wrong username and password to access it... in other words it must be a private file of someone else.<p>Use your browser's 'Back' button to go back and retry.",1);
      }
    }
}

  if($Form{'title'} && !$Form{'file'}){
    $squeeze = $Form{'title'};
    $squeeze =~ s/\s+//g;
    $ext = ".html";
    $Filename = "w$squeeze$ext";
  }
  else {
    $Filename = "wNewEntry.html" unless ($Filename = $Form{'file'});
  }
  $Title = "New Entry" unless ($Title = $Form{'title'});
  if($Form{'title'} eq "/private") { $Title = "New File for " . $Form{'uname'};}

#check for html ext here
$filext = $Form{'file'};
if ($Form{'file'} ne "" && $filext !~ /\.(\w*)htm(l*)/i) { $nhtml = 1; &Editnonht(*Form);}

  $founddesc = 0;

  $Body = '<BODY BGCOLOR='.$bgcolor.' background='.$background.'>' unless ($Body = $Form{'body'});

  $Head = $Description = "";

    if ($Form{'file'}){
      foreach $File (@ValidFiles){
        if ($File eq $Form{'file'}){
          $Filename = $File;
          open(FILE,$File) || &Success("EditEntry could not open $File for reading.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
          while ($line = <FILE>) {

            if ( $line =~ /^<TITLE>(.+)<\/TITLE>/gi) {
              $Title = $1;
            }
            elsif ( $line =~ /^<!--Head-->(.+)<!--EndHead-->/gi) {
              $Head = $1;
            }
            elsif ( $line =~ /^<!--Body-->(.+)<!--EndBody-->/gi) {
              $Body = $1;
            }	                
            elsif ( $line =~ /^<!--Description-->/gi) {
               $founddesc = 1;
               while ( $linea = <FILE>) {
                   last if ( $linea =~ /<!--EndDescription-->/gi);
                   $Description = $Description.$linea;
               }     
            }                      
         }
         close(FILE);
         if ($founddesc ne 1) { $nhtml = 1; &Editnonht(*Form);}
        }
      }
    }
    elsif ($Form{'title'}){
      foreach $VTitle (@ValidNames){
        if ($VTitle eq $Form{'title'}){
          $Filename = $ValidFiles[$Names2Numbs{$Title}];
          open(FILE,$Filename) || &Success("EditEntry could not open $Filename for reading.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
          while ($line = <FILE>) {
            if ( $line =~ /^<TITLE>(.+)<\/TITLE>/gi) {
              $Title = $1;
            }
            elsif ( $line =~ /^<!--Body-->(.+)<!--EndBody-->/gi) {
              $Body = $1;
            }
            elsif ( $line =~ /^<!--Head-->(.+)<!--EndHead-->/gi) {
              $Head = $1;
            }	                
            elsif ( $line =~ /^<!--Description-->/gi) {
               while ( $linea = <FILE>) {
                   last if ( $linea =~ /<!--EndDescription-->/gi);
                   $Description = $Description.$linea;
               }
            }
         }
         close(FILE);
        }
      }
    }

    &VidForm($Filename,$Title,$Body,$Head,$Description);
    exit;
}


sub VidForm{
  local ($Filename,$Title,$Body,$Head,$Description) = @_;

  $usename = "";
  $origname = $Filename;

  if ($Form{'title'} eq "/private"){ $priv = "true"; $usename = $Form{'uname'};}

  $Filename = "wFileName.html" unless ($Filename);
  $Title = "Put title here" unless ($Title);

  $Body = '<BODY BGCOLOR='.$bgcolor.' background='.$background.'>' unless ($Body);
  $Head = "" unless ($Head);

  if($Form{'insbreaks'}){ 
    $Description =~ s/\n/<br>\n/gio; 
  } 

#  if(!$nhtml){
#    $Description =~ s/<p>//gio;
#    $Description =~ s/<br>//gio;
#  }

  $Description =~ s/<textarea>/\&lt\;textarea\&gt\;/gi;
  $Description =~ s/<\/textarea>/\&lt\;\/textarea\&gt\;/gi;

open(TEMP,"javascrp") || &Success("VidForm could not open javascrp for reading.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
open(FILE,">form.html") || &Success("VidForm could not open form.html for writing.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
while (<TEMP>) { print FILE; }
close(TEMP);
close(FILE);

 open(FILE, ">>form.html") || &Success("VidForm could not open form.html for appending.<p>Please report this to the site<a href=mailto:$owner\?subject=MVD_error_report> administrator. </a>",1);
print FILE <<EF;
<small><b>FILENAME</b><br>Enter the .html filename here: The standard convention used is the
title with all white space removed and a lower case w prefix.
The "w" prefix is a nice way to keep all the files organized
in one place in an alphabetically sorted subdirectory.
<br>e.g. Title = wTitle.html</small><br>
<INPUT TYPE=TEXT NAME="filename" size=80 VALUE="$Filename" ><p>
<small><b>TITLE</b><br>Enter the title here:</small><br>
<INPUT TYPE=TEXT NAME="title" size=80 VALUE="$Title" ><p>

<small><b>HEADER</b><br>Put anything extra that needs to be in the header here, such as META
tags etc.:</small><br>
<INPUT TYPE=TEXT NAME="head" size=80 VALUE="$Head"><br>
<input type='button' value='Wizard' onclick="parent.frames[1].document.open();
parent.frames[1].document.writeln('<body bgcolor=white> <center> <h2> META Tags Wizard </h2>' +
'</center> <p> <form>Description:<br><input type=text size=40 name=desc><p>' +
'Key Words:<br><input type=text size=40 name=keyw><p>' +
'<input type=button value=Submit onclick=parent.frames[0].makemetatag(forms[0].desc.value,forms[0].keyw.value)> </form>' +
'<p><center><font size=2> <b> Hints for usage </b> </center> <p>' +
  'These are just a couple of the more commonly used META tags; more specifically they are' +
  ' HTTP-EQUIV tags, which binds the information to a HTTP response header. This is useful for purposes of file indexing and searching. If you do not understand any of this do not worry about it.');
parent.frames[1].document.close();">
<p>

<small><b>BODY ATTRIBUTES</b><br>Change any attributes here (Bg color; text color etc.) Warning! Do not use quotation marks.</small>
<br>
<INPUT TYPE=TEXT NAME="body" size=80 VALUE="$Body"><br>
<input type='button' value='Wizard' onclick="parent.frames[1].document.open();
parent.frames[1].document.writeln('<body bgcolor=white> <center> <h2> Body Attributes Wizard </h2>' +
'</center> <p> <form>Background Color:<br><input type=text size=40 name=bgcolor><p>' +
'Background Image:<br><input type=text size=40 name=bgimg><p>' +
'Text Color:<br><input type=text size=40 name=txtcolor><p>' +
'Link Color:<br><input type=text size=40 name=lnkcolor><p>' +
'Visited Link Color:<br><input type=text size=40 name=vlnkcolor><p>' +
'<input type=button value=Submit onclick=parent.frames[0].makebodytag(forms[0].bgcolor.value,forms[0].bgimg.value,forms[0].txtcolor.value,forms[0].lnkcolor.value,forms[0].vlnkcolor.value)> </form>' +
'<p><center><font size=2> <b> Hints for usage </b> </center> <p>' +
  '<i>Background Image</i> refers to an image used as wallpaper instead of the one color set with <i>Background color</i>.' +
  'You can find a table of colors here: <a href=http://www.ma.utexas.edu/~tbryan/colors.html target=_new>HTML Color Table</a>.');
parent.frames[1].document.close();">
<p>

<small><b>BODY</b><br>Enter your main text in this box.<br>
Use html line-break and paragraph-break tags. Hitting enter is useful for clarity
in editing these files but will not affect the way these files appear in a browser. See the
<i>Reload and Insert Breaks</i> link below to automatically insert html breaks.</small><br>
<TEXTAREA NAME="description" ROWS=20 COLS=80 WRAP=PHYSICAL>$Description</TEXTAREA>
<br><center>
<select name='tags'>
<option selected>Centering
<option>Font
<option>Link
<option>Link to MVD File
<option>Image
<option>Mail-to
<option>Horizonal ruled line
<option>Table
<option>Italics
<option>Bold
<option>Horizontal Spacing
<option>Ordered List
<option>Comment
</select>
<input type='button' value='Wizard' onclick="insertag(this.form)">
<p>
<input type=hidden name="nhtml" value="$nhtml">
<INPUT TYPE=Submit NAME="Action" VALUE="Preview"></center>
</td></table><p>
<table width=650><td>
<center><a href="$ENV{'REQUEST_URI'}&insbreaks=1" target=_top> [ Reload and Insert Breaks ] </a><br></center>
<font size=-1><i>
Use this link to reload the above document into this editor and at the same time to automagically insert html line breaks 
beside each newline character in already saved, non-htmlized, text. <font color=red> Warning</font>: Use this before 
adding any new text or else you will lose your changes. Save the document after each application if you want to use it 
more than once, i.e., for double spacing. To get the best formatting it is usually best to ignore this and insert the html line breaks your self.
</i></font>
</td></table>

<p>
<hr><p>
<table width=600 border=0><td>
You don't need to input your username or password to preview your entry but
if you wish to save your changes you will need to supply your membership data.
</td></table>
Your Username<br><INPUT TYPE=TEXT SIZE=23 NAME="uname" value="$usename">
<input type=hidden name="private" value="$priv">
<input type=hidden name="origname" value="$origname">
<p>Your Password<br><INPUT TYPE=password SIZE=23 NAME="pword"><p>
<INPUT TYPE=Submit NAME="Action" VALUE="Save"> * <INPUT TYPE=Reset VALUE="Reset"><p><font size=1>Input your email address here, before pressing <i>Save</i>, if you would like a copy of this entry mailed to you.</font><br><INPUT TYPE=TEXT SIZE=23 NAME="email">
<p>
EF

if ($lev1edit eq 1){
print FILE <<eof;
<input type=hidden name="L1ed" value="on">
eof
}
elsif ($priv eq "true"){
print FILE <<eof;
<input type=checkbox name="mvdswitch" value="on"> <b>Add to the MVD</b><br>
<table width=510>
<td><font size=1>Checking this box will remove this file from your private list of files and add it to the public list of files. If you select this box it is recommended that you fill in your email address so you will have a back-up of the data in this file because when it is made public any member will be able to edit it.</font></td>
</table>
<p>
eof
}

if ($nhtml){
  print FILE <<ef;
<input type=checkbox name="htmlswitch" value="on" CHECKED> <b>Do not wrap in HTML</b><br>
ef
}
else {
  print FILE <<ef;
<input type=checkbox name="htmlswitch" value="on"> <b>Do not wrap in HTML</b><br>
ef
}

print FILE <<EoF;
<table width=510>
<td><font size=1>Check this box if you are not editing an HTML file or if you want to supply all your own HTML tags in the body field. The <i>HEADER</i> and <i>BODY ATTRIBUTES</i> fields will be ignored. This box is automatically checked if the file does not have an HTML extention or if it is an HTML file that was created with the box checked. Before using it on an HTML file make sure you are aware of how that will affect how it is viewed in the MVD. There are some hidden tags which are automatically included when the box is not checked, which you may need to supply yourself.</font></td>
</table>
<p>
</form>
</center>

<hr size=2>
<center><a href="$homelink" target=_top> [ $homelinkstring ] </a>&nbsp;&nbsp;
<a href="$mvdhomelink" target=_top> [ $mvdhomelinkstring ] </a>&nbsp;&nbsp;<a href="$listlink" target=_top> [ $listlinkstring ] </a>
<hr>
</center>
</BODY></HTML>
EoF
 close (FILE);

print <<EOF;
Content-type: text/html

<html>
<head>
<TITLE>MVD Add/Edit Form</TITLE>
</head>

<frameset rows=*,32%>
  <frame src="form.html" name="main">
  <frame src="insert.html" name="insert">

<noframes>
<body>
<center>
Sorry, you need a browser which supports frames in order to access this site.
</center>
</body>
</noframes>
</frameset>
</html>
EOF
}
