<HTML>
<HEAD>
<TITLE>Member Utilities</TITLE>
</HEAD>
<BODY bgcolor="#99CCCC" background="../graphics/web3.jpg">
<center>
<h2>Member Utilities</h2>
<br>

<?
// last updated June 4, 2005

// configure variables for your particular instance of the MVD. Make sure they match the other cgi and php scripts.
$publistfile = "doclist.txt";                 // complete list of MVD files
$adminemail  = "admin@global-dialog.org";     // address to which notices of file actions are sent

// Change anything below this line at your own risk

$groups_in;
$groups_not;


function addtolist($list,$entry){  

  $FILE = fopen($list,"a");    
  fputs($FILE, "$entry");  
  fclose($FILE);
}

function updatelist($list,$existing,$replacement){

  $File = file($list);

  $FILE = fopen($list,"w");

  for($index=0;$index < count($File);$index++){
    $line = eregi_replace("^$existing",$replacement,$File[$index]);
    fputs($FILE, $line);
  }
  fclose($FILE);
}

function removeentry($list,$entry){

  $File = file($list);

  $FILE = fopen($list,"w");

  for($index=0;$index < count($File);$index++){
    if(!eregi("^$entry(,|:|\n|$)",$File[$index])){
      fputs($FILE, $File[$index]);
    }
  }
  fclose($FILE);
}

function removemember($list,$member){

  $File = file($list);

  $FILE = fopen($list,"w");

  for($index=0;$index < count($File);$index++){
    if(!eregi("^$member:",$File[$index])){
      fputs($FILE, $File[$index]);
    }
  }
  fclose($FILE);
}

function testentry($list,$entry){

  $File = file($list);  

  for($index=0;$index <= count($File);$index++){    
    if(eregi("^$entry(,|:|\n|$)",$File[$index])){
      return(1);
    }
  }  
  return(0);
}

function testmember($list,$member){

  $File = file($list);  

  for($index=0;$index <= count($File);$index++){    
    if(eregi("^$member:",$File[$index])){
      return(1);
    }
  }  
  return(0);
}

function groupstatus($member){
  global $groups_in;
  global $groups_not;

  $grpfile = fopen("groups","r");

  if(!($grpfile)){
    print("Error: File 'groups' could not be read.");
    exit; 
  }

  while(!feof($grpfile)){
    $gline = fgets($grpfile, 255);
    $ingroup = 0;
     
    if( chop($gline) != "main" && chop($gline) != ""){
      
      $usrfile = chop($gline).".ul";
      
      $ulfile = fopen("$usrfile","r");
      
      if(!($ulfile)){
        print("Error: File $usrfile could not be read.");
        exit; 
      }

      while(!feof($ulfile)){
        $usrline = fgets($ulfile, 255);

        if(eregi("^$member(,|:|\n|$)",$usrline)){
          $groups_in[] = chop($gline);
          $ingroup = 1;
          break;
        } 
      }
      if(!($ingroup)){
        $groups_not[] = chop($gline);
      }

    }
  }
  fclose($grpfile);  
}

     
      $ok = 0;
      $askdel = 0;
      $userlevel = 0;
      $userfile = $username.".dat";
      $noprivatefiles = 0;

      if($uname != "" && $pword != ""){
        $pw = crypt($pword,$pword);
          $ustring = $uname.":".$pw."\n";
          $ulfile = fopen("ul.txt","r");

          if(!($ulfile)){
            print("Error: ");
            print("File 'ul.txt' could not be read.");
            exit; 
          }

          while(!feof($ulfile)){
            $uline = fgets($ulfile, 255);
            $userlevel++;
            if($uline == $ustring){
              $ok = 1;
              break; 
            }
          }
          fclose($ulfile);             
        
      }
      else{
        print("You need to supply your username and password<p><HR><p>\n");
      }

      if(!($ok)){
        print("Invalid username or password<p><HR><p>\n");
        exit;
      }

      //print("Userlevel: $userlevel<p>\n");      

      if($userlevel != 1){
        $ok = 0;
        print("You need to be the MVD administrator to use these functions.<p><HR><p>\n");
      }

           
      if($ok){
      
        if($action == "add"){
          if($username != ""){
            $groupuserfile = $togroup.".ul";
            if(testentry($groupuserfile,$username)){
              print("<b>$username</b> is already a member of <b>$togroup</b><p><HR><p>\n");
            }
            else {
              addtolist($groupuserfile,$username);
              print("<b>$username</b> added to group <b>$togroup</b>\n");
              print("<p><HR><p>\n");              
              mail($adminemail, "MVD member added to group", "$username added to $togroup by $uname\n$groupuserfile updated\n");              
            }
          }
          else {
            print("You need to specifiy a user to add to <b>$togroup</b><p><HR><p>\n");
          }
        }
        else if($action == "remove") {
          if($username != ""){
            $groupuserfile = $fromgroup.".ul"; 
            if(!(testentry($groupuserfile,$username))){
              print("<b>$username</b> is not a member of <b>$fromgroup</b><p><HR><p>\n");
            }
            else {
              removeentry($groupuserfile,$username);
              print("<b>$username</b> removed from group <b>$fromgroup</b>\n");
              print("<p><HR><p>\n");              
              mail($adminemail, "MVD member removed from group", "$username removed from $fromgroup by $uname\n$groupuserfile updated\n");              
            }
          }
          else {
            print("You need to specifiy a user to remove from<b>$fromgroup</b><p><HR><p>\n");
          }                
        }        
        else if($action == "delete") {
          print("<center><font size=+1>Are you sure you want to delete the member <b>$username</b>?</font><p>\n");
          print("<FORM ACTION=\"uutl.php\" METHOD=\"post\">\n");
          print("<INPUT TYPE=\"hidden\" name='username' value='$username'>\n");
          print("<INPUT TYPE=\"hidden\" name=\"uname\" value=\"$uname\">\n");
          print("<INPUT TYPE=\"hidden\" name=\"pword\" value=\"$pword\">\n");
          print("<INPUT TYPE=\"hidden\" name=\"fileaction\" value=\"$fileaction\">\n");          

          print("<INPUT TYPE=\"hidden\" name=\"action\" value=\"confirmdelete\">\n");
          print("<INPUT TYPE=\"submit\" name=\"sbmt\" VALUE=\"Delete\">");
          print("&nbsp;&nbsp;&nbsp;&nbsp;<INPUT TYPE=\"submit\" VALUE=\"Cancel\"></FORM>");
          print("</center><p><HR><p>\n");
          $askdel = 1;
        }
        else if($action == "confirmdelete") {
          if($sbmt == "Delete"){
            $privatefilelist = $username.".dat";
            groupstatus($username);
            $gr_in_index = count($groups_in);

            if(testmember("ul.txt", $username)){
              if($fileaction == "delete"){
                //unlink each private file in $privatefilelist 
                $datfile = fopen("$privatefilelist","r");

                if(!($datfile)){
                  print("Error: File '$privatefilelist' could not be read.");
                  exit; 
                }

                while(!feof($datfile)){
                  $datline = fgets($datfile, 255); 
                  if($datline != "" && !(eregi("^~$privatefilelist,",$datline))){
                    $pieces = explode(",", $datline);
                    $pfilename = $pieces[0];
                    unlink($pfilename);
                  }   
                }
                fclose($datfile);                

                for($index=0; $index < $gr_in_index; $index++){                  
                  if($groups_in[$index] != ""){
                    $groupuserfile = $groups_in[$index].".ul"; 
                    removeentry($groupuserfile,$username);
                  }
                }
                removemember("ul.txt",$username);
                unlink($privatefilelist);
                print("Member <b>$username</b> deleted. All private files, lists and group entries also removed.<p><HR><p>\n");
                mail($adminemail, "MVD member deletion", "Member $username deleted by $uname\nAll private files, lists and group entries also removed.\n");
              }
              else if($fileaction == "move"){
                //move each private file in $privatefilelist to "new files.grp"                
                $datfile = fopen("$privatefilelist","r");

                if(!($datfile)){
                  print("Error: File '$privatefilelist' could not be read.");
                  exit; 
                }

                while(!feof($datfile)){
                  $datline = fgets($datfile, 255); 
                  if($datline != "" && !(eregi("^~$privatefilelist,",$datline))){
                    addtolist("new files.grp",$datline);
                    addtolist($publistfile,$datline);
                  }   
                }
                fclose($datfile);

                for($index=0; $index < $gr_in_index; $index++){                  
                  if($groups_in[$index] != ""){
                    $groupuserfile = $groups_in[$index].".ul"; 
                    removeentry($groupuserfile,$username);
                  }
                }
                removemember("ul.txt",$username);
                unlink($privatefilelist);
                print("Member <b>$username</b> deleted. All private files moved to the <b>new files</b> group.<p><HR><p>\n");                
                mail($adminemail, "MVD member deletion", "Member $username deleted by $uname\nAll private files moved to the 'new files' group.\n");
              }            
              else {
                removemember("ul.txt",$username);
                mail($adminemail, "MVD member deletion", "Member $username deleted by $uname\nAll private files, lists and group entries left unchanged.\n"); 
              }
            }
            else{
              print("<b>$username</b> does not exist as a member<p><HR><p>\n");
              exit;
            }            
            $askdel = 1;
          }
          else{
            print("<b>$username</b> deletion cancelled<p><HR><p>\n");
          }
        }                
      }   


if($userlevel == 1 && !$askdel && $username != ""){

  groupstatus($username);

  print("<font size=+1>Member: <b>$username</b></font><p>\n");
  print("$username is a member of these groups:<br><b>main</b><br>\n");
  $gr_in_index = count($groups_in);
  $gr_not_index = count($groups_not);

  for($index=0; $index < $gr_in_index; $index++){
    print("<b>$groups_in[$index]</b><br>\n");
  }

  $pfilelist = $username.".dat";

  if(file_exists($pfilelist)){

    print("<br>Private files of $username:<br>\n");
    $uprfile = fopen("$pfilelist","r");

    if(!($uprfile)){
      print("Error: File '$pfilelist' could not be read.");
      exit; 
    }

    while(!feof($uprfile)){
      $pline = fgets($uprfile, 255);
      $pieces = explode(",", $pline);    
      if($pline != "" && $pieces[0] != "~$pfilelist"){
        print("<b>$pieces[1]:</b>  <A HREF=$pieces[0]> $pieces[0]</a><br>\n");
      }
    }
    fclose($uprfile);
  }
  else{
    print("<br><b>$username</b> doesn't have a private file list 
yet.\n");
    $noprivatefiles = 1;
  }
}

print("<p><hr><p><font color=red>SECURITY WARNING!</font><br>DO NOT leave your computer unattended with this page loaded.<br>Shut down your browser when you are finished this session.");
print("<p><hr><p>\n");

if($userlevel == 1 && !$askdel && $username != ""){
  print("<font size=-1>Members can neither be removed from <b>main</b><br>nor added to <b>admin</b> or <b>new files</b>.</font><br>");
  print("<table border=2 cellpadding=8 bgcolor=\"#99CCCC\"><td>\n");
  print("<FORM ACTION=\"uutl.php\" METHOD=\"post\">\n");  
  print("Choose an action for this member:<p>\n");

  print("<input type=\"radio\" name=\"action\" value=\"delete\"> Delete this member.<br>\n");
  if(!$noprivatefiles){
    print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size=-1><input type=\"radio\" name=\"fileaction\" value=\"leave\" CHECKED> No action on private files<br></font>\n");
    print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size=-1><input type=\"radio\" name=\"fileaction\" value=\"delete\"> Delete private files<br></font>\n");
    print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size=-1><input type=\"radio\" name=\"fileaction\" value=\"move\"> Move private files to <b>new files</b><p></font>\n");
  }
  else {
    print("<p>\n");
  }
  
print("<input type=\"radio\" name=\"action\" value=\"remove\"> \n");
print("Remove member from group: <select name=\"fromgroup\">\n");

  for($index=0; $index < $gr_in_index; $index++){
    if($groups_in[$index] != "main" && $groups_in[$index] != "admin" && $groups_in[$index] != "new files" && $groups_in[$index] != ""){
      print("<option>$groups_in[$index]</option>\n");
    }
  }
print("</select><p>\n");

print("<input type=\"radio\" name=\"action\" value=\"add\"> \n");
print("Add member to group: <select name=\"togroup\">\n");

  for($index=0; $index < $gr_not_index; $index++){
    if($groups_not[$index] != "admin" && $groups_not[$index] != "" && $groups_not[$index] != "new files"){
      print("<option>$groups_not[$index]</option>\n");
    }
  }
print("</select><p>\n");  

  print("<INPUT TYPE='hidden' name='username' value='$username'>\n");

  print("<center><br><table><td>\n");
  print("<INPUT TYPE=TEXT SIZE=23 NAME=\"uname\" value=$uname> username<br>\n");
  print("<INPUT TYPE=password SIZE=23 NAME=\"pword\"> password</td></table><p>\n");
  print("<INPUT TYPE=\"submit\" VALUE=\"Submit\"></center></FORM></td></table><p>\n");
}

print("<center><hr size=2>\n");
print("<A HREF=\"mvladmin.cgi?Action=List%20Users&Public=on&uname=$uname&pword=$pword#Files\"> [ Back To Members List ] &nbsp;&nbsp;\n");
print("<A HREF=\"mvladmin.cgi?Action=Add/Edit%20Entry&Public=on&uname=$uname&pword=$pword#Files\"> [ Public File Form ] &nbsp;&nbsp;\n");
print("<A HREF=\"mvladmin.cgi?Action=Add/Edit%20Entry&private=true&uname=$uname&pword=$pword#Files\"> [ Private File Form ] &nbsp;&nbsp;<a href=\"mvl.cgi?NextName=List\"> [ List ] </a><hr><br></center>\n");

?>
</BODY>
</HTML>
