# lev1.pl - Perl library file containing user and password functions
# for MVD

sub listall{

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

print "Content-type: text/html\n\n";   
print <<EOF;
<HTML>
<HEAD><TITLE> MVD Administration </TITLE></HEAD>
<BODY BGCOLOR="#99CCCC"><CENTER>
<p><hr size=2><p>
<h1>MVD Administration: Level-1</h1>
<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="/list"><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>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="#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 BGCOLOR="#FFFF99"><INPUT TYPE=RESET VALUE=" Reset All Input Boxes "><p></td></tr>
</table><p>
<a href="searchmvl.cgi?Query=C:admin">Return to list of public MVD files.</a><p>
EOF

  $pth = "/www/worldzone/social/wyrd/cgi/";
  $txtfiles = 0;
  $directories = 0;
  $binfiles = 0; 
  
  if($Form{'cd'}) {
    chdir($Form{'cd'});
    if($Form{'cd'} =~ /\.\.\Z/ ) {      
      $Form{'cd'} =~ /(.+\/)[0-9A-Za-z]+\/\.\.\Z/; 
      $pth = $1;      
    }
    else {
      $pth = $Form{'cd'}."/";
    }    
  }
 
  $currentdir = $htpath = $pth;
  $htpath =~ s/\A\/www\/worldzone//i;

  opendir(DIR,".");
  if ($Form{'file'} ne "") { 
    @Files = grep( /$Form{'file'}/i, readdir(DIR));
  }
  else {
    @Files = sort(grep( !/\A\.\Z/, readdir(DIR)));
  }
  closedir(DIR);

$FileNumb = @Files;

  if ($Form{'file'} ne ""){
    print "There are $FileNumb objects which match <i>\/$Form{'file'}\/</i> in this directory.<br>$currentdir<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";
  }
  else{
    print "There are currently $FileNumb total objects in this directory.<br>$currentdir<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><tr valign=middle><td align=center valign=middle>Name</td><td align=center valign=middle>Size</td><td align=center valign=middle>Mode</td><td align=center valign=middle>Action</td></tr>\n";
  $c1=1;
  foreach $File (@Files){

    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($File);
    
    if (-T $File){
    $txtfiles++;   
    print "<tr valign=middle><td valign=middle><font size=-1><A HREF=\"$htpath$File\">$File</a></font></td><td align=center valign=middle><font size=-1>$size</font></td><td align=center valign=middle><font size=-1><A HREF=\"mvladmin.cgi\?Action=Add\/Edit%20Entry&title=\/chmod&uname=$Form{'uname'}&pword=$Form{'pword'}&file=$pth$File\">";
    printf("%o</a></font></td>",($mode & 0777));
    print "<td align=center valign=middle><font size=-1><A HREF=\"mvladmin.cgi\?Action=Add\/Edit%20Entry&title=\/edit&uname=$Form{'uname'}&pword=$Form{'pword'}&file=$pth$File\">Edit</a></font></td></tr>";
    }
    elsif (-d $File){
    $directories++;
    print "<tr valign=middle><td valign=middle><font size=-1><A HREF=\"mvladmin.cgi\?Action=Add\/Edit%20Entry&title=\/list&uname=$Form{'uname'}&pword=$Form{'pword'}&cd=$pth$File\"><font color=red>$File</font></a></font></td><td align=center valign=middle><font size=-1>$size</font></td><td align=center valign=middle><font size=-1><A HREF=\"mvladmin.cgi\?Action=Add\/Edit%20Entry&title=\/chmod&uname=$Form{'uname'}&pword=$Form{'pword'}&file=$pth$File\">";
    printf("%o</a></font></td><td align=center valign=middle><font size=-1><A HREF=\"$htpath$File\">Open</a></font></td></tr>",($mode & 0777));
    }
    else{
    $binfiles++;
    print "<tr valign=middle><td valign=middle><font size=-1>$File</font></td><td align=center valign=middle><font size=-1>$size</font></td><td align=center valign=middle><font size=-1><A HREF=\"mvladmin.cgi\?Action=Add\/Edit%20Entry&title=\/chmod&uname=$Form{'uname'}&pword=$Form{'pword'}&file=$pth$File\">";
    printf("%o</a></font></td><td align=center valign=middle><font size=-1><A HREF=\"$htpath$File\">Open</a></font></td></tr>",($mode & 0777));
    } 
    $c1++;
  }
  print "</table></form>Text files: $txtfiles Directories: $directories Binary files: $binfiles<p><hr>\n";

print <<EOFF;
<a href="http://global-dialog.org" target=_top> [ Global Dialog Home ] </a>&nbsp;&nbsp;<a href="http://global-dialog.org/mvd"> [ MVD Home ] </a>&nbsp;&nbsp;<a href="mvl.cgi?NextName=List"> [ List ]  </a><hr><p></CENTER></BODY></HTML>
EOFF
exit;
  }
  &BadCombo("member with level 1 clearance");  
  exit;
}

sub changemode{
  local (*Form) = @_;
  if (&CheckUser($Form{'uname'},$Form{'pword'},1)){
  if($Form{'file'} eq ""){
      &Success("You need to supply a name for the file you wish to chmod.<p>Use your browser's 'Back' button to go back and retry.",1);       
    }

  if($Form{'mode'} eq "777"){ $adjmode = 0777;}
  elsif($Form{'mode'} eq "755"){ $adjmode = 0755;}
  elsif($Form{'mode'} eq "744"){ $adjmode = 0744;}
  elsif($Form{'mode'} eq "711"){ $adjmode = 0711;}
  elsif($Form{'mode'} eq "700"){ $adjmode = 0700;}
  elsif($Form{'mode'} eq "666"){ $adjmode = 0666;}
  elsif($Form{'mode'} eq "644"){ $adjmode = 0644;}
  elsif($Form{'mode'} eq "600"){ $adjmode = 0600;}
      
  $testchmod = chmod $adjmode, $Form{'file'};
  
  if ($testchmod ne 1) { 
      &Success("Could not chmod file named < $Form{'file'} > .<p>The file may not exist or have restricted access.",1);      
    }
    
      $message = "< $Form{'file'} > chmodded to $Form{'mode'} by $Form{'uname'}.";
      &mailForm($owner, $owner, "MVD file mode changed", "$message\n");
      &Success("$message<p>");
  }
  &BadCombo("member with level 1 clearance");  
  exit;
}

sub chmodform{
  
  if (&CheckUser($Form{'uname'},$Form{'pword'},1)){
  if($Form{'file'} eq ""){
      &Success("You need to supply a name for the file you wish to chmod.<p>Use your browser's 'Back' button to go back and retry.",1);       
    }

    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($Form{'file'});
    $adjmode = sprintf("%o",($mode & 0777));

$lasta = localtime($atime);
$lastm = localtime($mtime);
$lastc = localtime($ctime);
  
  print <<EOF;
Content-type: text/html

<HTML>
<HEAD><TITLE> CHMOD File </TITLE></HEAD>
<BODY BGCOLOR="#99CCCC"><CENTER>
<p><hr size=2><p>
<h2>Stats for "$Form{'file'}"</h2>
<table width=600 border=0>
<tr><td>INode: </td><td> $ino<br></td></tr>
<tr><td>Device: </td><td> $dev</td></tr>
<tr><td>Device Type: </td><td> $rdev</td></tr>
<tr><td>User: </td><td> $uid</td></tr>
<tr><td>Group: </td><td> $gid</td></tr>
<tr><td>Size: </td><td> $size bytes</td></tr>
<tr><td>Last Access: </td><td> $lasta</td></tr>
<tr><td>Last Modification: </td><td> $lastm</td></tr>
<tr><td>Last Change: </td><td> $lastc</td></tr>
</table>
<p>
<table width=600 border=0>
<td>
Mode: $adjmode<br>
<font size=1>0 = No Access 1=Execute 2=Write 4=Read</font><br>
Choose from the list and press the CHMOD button<br>
to change the permissions of "$Form{'file'}".<p> 

<FORM METHOD=POST ACTION="mvladmin.cgi">
<INPUT TYPE=hidden NAME="uname" value="$Form{'uname'}">
<input type=radio name="mode" value="777"> 777<br>
<input type=radio name="mode" value="755"> 755<br>
<input type=radio name="mode" value="744"> 744<br>
<input type=radio name="mode" value="711"> 711<br>
<input type=radio name="mode" value="700"> 700<br>
<input type=radio name="mode" value="666"> 666<br>
<input type=radio name="mode" value="644"> 644<br>
<input type=radio name="mode" value="600"> 600<br>
<input type=hidden name="file" value="$Form{'file'}">
<INPUT TYPE=hidden NAME="pword" value="$Form{'pword'}">
<INPUT TYPE=Submit NAME="Action" VALUE="CHMOD">
</form>
</td>
</table>

<p><hr size=2><a href="$homelink" target=_top> [ Global Dialog Home ] </a>&nbsp;&nbsp;<a href="$mvdhomelink"> [ MVD Home ] </a>&nbsp;&nbsp;<a href="$listlink"> [ List ] </a><hr>
</CENTER></BODY></HTML>
EOF
  exit;
  }
  &BadCombo("member with level 1 clearance");  
  exit;
}

sub delany{
  local (*Form) = @_;
  if (&CheckUser($Form{'uname'},$Form{'pword'},1) || $filelist ne "vidlist.txt"){
    if($Form{'title'} eq "" || $Form{'file'} eq ""){
      &Success("You need to supply both a filename and title (or keyword) for the entry you wish to delete.<p>Use your browser's 'Back' button to go back and retry.",1);       
    }
    
    if (!(unlink $Form{'file'})) { 
      &Success("Could not delete file named < $Form{'file'} > .<p>The file may not exist or have restricted access.",1);      
    };
    
      $message = "< $Form{'file'} > deleted by $Form{'uname'}.";
      &mailForm($owner, $owner, "MVD entry deleted", "$message\n");
      &Success("$message<p>");
  }
  &BadCombo("member with level 1 clearance");  
  exit;
}

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

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

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

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

  $Description = $Form{'description'};
  
  $Description =~ s/\&lt\;textarea\&gt\;/<textarea>/gi;
  $Description =~ s/\&lt\;\/textarea\&gt\;/<\/textarea>/gi;

  $message = "Level-1 Edit: < $Filename > successfully created/edited by $Form{'uname'}.\n";

  open (FILE,">$Filename") || &Success("saveany could not open $Filename for writing.<p>Please report this to the site administrator: <a href=mailto:posy\@post.com\?subject=MVD_error_report> Mariposa </a>",1);
  print FILE $Description;
  close (FILE);
  
  &mailForm($owner, $owner, "MVD entry added", "$message\n");
   
    if ($email ne "") 
    {   
        &mailForm($email, $owner, "MVD entry added", "$message\n\nFilename: $Filename\nBody: $Description\n");
    }

   &Success("$message<p>Use this link <A HREF=\"$Filename\"> $Filename</a> to check the 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.");
  
  }
  &BadCombo("member with level 1 clearance");  
  exit;
}

sub editany{

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

  if (!$Form{'file'}) { 
    &Success("You need to supply a filename for the entry you wish to edit.<p>Use your browser's 'Back' button to go back and retry.",1);    
  }
  if (!(-e $Form{'file'})){
    &Success("Could not find a file named < $Form{'file'} > .<p>The file may not exist or have restricted access.",1);    
  }

  $nhtml = 1; 

  $Title = $Body = $Head = "Level 1 edit: this field ignored";
  $Description = "";  
  
  $File = $Filename = $Form{'file'};
  open(FILE,$File) || &Success("editany could not open $File for reading.<p>Please report this to the site administrator: <a href=mailto:posy\@post.com\?subject=MVD_error_report> Mariposa </a>",1);
  while ($line = <FILE>) {
      $Description = $Description.$line; 
  }
  close(FILE);     

  $lev1edit = 1;
    
  &VidForm($Filename,$Title,$Body,$Head,$Description);
  exit;

  }
  &BadCombo("member with level 1 clearance");  
  exit;
}

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

  $Body = $Head = "non-html file: this field ignored";
  $Description = "";
  $Title = $Files2Names{$Form{'file'}};

      foreach $File (@ValidFiles){
        if ($File eq $Form{'file'}){
          $Filename = $File;
          open(FILE,$File) || &Success("Editnonht could not open $File for reading.<p>Please report this to the site administrator: <a href=mailto:posy\@post.com\?subject=MVD_error_report> Mariposa </a>",1);
          while ($line = <FILE>) {
             $Description = $Description.$line; 
          }                                           
          close(FILE);
        }  
      } 
    &VidForm($Filename,$Title,$Body,$Head,$Description);
    exit;  
}

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

  $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'}.";
          $new = 0;
        }
        else {
          $new = 1;
        }    
      }
    }
    open (FILE,">$Filename") || &Success("Savenonht could not open $Filename for writing.<p>Please report this to the site administrator: <a href=mailto:posy\@post.com\?subject=MVD_error_report> Mariposa </a>",1);
    print FILE $Description;
    close (FILE);
  
    open(FILE, $filelist) || &Success("Savenonht could not open $filelist for reading.<p>Please report this to the site administrator: <a href=mailto:posy\@post.com\?subject=MVD_error_report> Mariposa </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("Savenonht could not open $filelist for writing.<p>Please report this to the site administrator: <a href=mailto:posy\@post.com\?subject=MVD_error_report> Mariposa </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 "vidlist.txt"){
    &Success("$message<p>Use this link <A HREF=\"$Filename\"> < $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.");
  }
  else{
    &Success("$message<p>Use this link <A HREF=\"mvl.cgi\?NextName=$Filename\"> < $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 ConfirmDelete{
  local (*Form) = @_;

  $deltype = "Delete";

  if (&CheckUser($Form{'uname'},$Form{'pword'},1) || $filelist ne "vidlist.txt"){
    if($Form{'title'} eq "" || $Form{'file'} eq ""){
      &Success("You need to supply both a filename and title (or keyword) for the entry you wish to delete.<p>Use your browser's 'Back' button to go back and retry.",1);       
    }
    if($Files2Names{$Form{'file'}} eq "" && $Form{'title'} ne "/del"){
      &Success("Could not find a file named < $Form{'file'} > .<p>Use your browser's 'Back' button to go back and retry.",1);      
    }

  if ($Form{'title'} eq "/del") {
    $deltype = "Unlink";
  }

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

<HTML>
<HEAD><TITLE> Confirm Delete </TITLE></HEAD>
<BODY BGCOLOR="#99CCCC"><CENTER>
<p><hr size=2><p>
<h2>Are you sure you want to delete the file "$Form{'file'}" ?</h2>
Press the button below to delete "$Form{'file'}" or else use your browser's 'Back' button, or one of the links at the bottom, to leave this page 
and keep the file intact.<p>

<FORM METHOD=POST ACTION="mvladmin.cgi">
<INPUT TYPE=hidden NAME="uname" value="$Form{'uname'}">
<input type=hidden name="title" value="$Form{'title'}">
<input type=hidden name="file" value="$Form{'file'}">
<INPUT TYPE=hidden NAME="pword" value="$Form{'pword'}">
<INPUT TYPE=Submit NAME="Action" VALUE="$deltype">
</form>

<p><hr size=2><a href="$homelink" target=_top> [ Global Dialog Home ] </a>&nbsp;&nbsp;<a href="$mvdhomelink"> [ MVD Home ] </a>&nbsp;&nbsp;<a href="$listlink"> [ List ] </a><hr>
</CENTER></BODY></HTML>
EOF
  exit;
  }
  &BadCombo("member with level 1 clearance");  
  exit;
}

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

  if (&CheckUser($Form{'uname'},$Form{'pword'},1) || $filelist ne "vidlist.txt"){
    if($Form{'title'} eq "" || $Form{'file'} eq ""){
      &Success("You need to supply both a filename and title (or keyword) for the entry you wish to delete.<p>Use your browser's 'Back' button to go back and retry.",1);       
    }
    if($Files2Names{$Form{'file'}} eq ""){
      &Success("Could not find a file named < $Form{'file'} > .<p>Use your browser's 'Back' button to go back and retry.",1);
    }     

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

    if (!(unlink $Form{'file'})) { 
      &Success("Could not delete file named < $Form{'file'} > .<p>The file may not exist or have restricted access.<p>It has been removed from the list.",1);      
    };
    
      $message = "< $Form{'file'} > deleted by $Form{'uname'}.";
      &mailForm($owner, $owner, "MVD entry deleted", "$message\n");
      &Success("$message<p>");
  }
  &BadCombo("member with level 1 clearance");  
  exit;
}

sub Success {  # or not
local ($message,$success) = @_;

  if($success){
    $head = "Unsuccessful";
  }
  else {
    $head = "Successful";
  }

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

<HTML>
<HEAD><TITLE> MVD Operation $head </TITLE></HEAD>
<BODY BGCOLOR="#99CCCC" background="../graphics/web3.jpg"><CENTER>
<p><hr size=2><p>
<h1>Operation $head</h1>
$message
<p><hr size=2><a href="searchmvl.cgi?Query=C:admin" target=_top> [ MVD Admin ] </a>&nbsp;&nbsp;<a href="$homelink" target=_top> [ Global Dialog Home ] </a>&nbsp;&nbsp;<a href="$mvdhomelink" target=_top> [ MVD Home ] </a>&nbsp;&nbsp;<a href="$listlink" target=_top> [ List ] </a><hr>
</CENTER></BODY></HTML>
EOF
exit;
}

1; # Library files must end with the satement "1;" 
   # to satisfy the require operator.




