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

<?
// last updated Mar. 21, 2004

// configure variables for your particular instance of the MVD. Make sure they match the other cgi scripts.
$adminemail  = "shambhala@telusplanet.net";   // address to which notices of file actions are sent

// Change anything below this line at your own risk

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 testentry($list,$entry){

  $File = file($list);  

  for($index=0;$index <= count($File);$index++){
    //was this before... can't be right -->  if(!eregi("^$entry",$File[$index])){   
    if(eregi("^$entry(,|:|\n|$)",$File[$index])){
      return(1);
    }
  }  
  return(0);
}

     
      $ok = 0;
      $askdel = 0;
      $userlevel = 0;
      $groupfile = $groupname.".grp";
      $groupuserfile = $groupname.".ul";          

      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 == "adduser"){
          if($addeduser != ""){
            if(testentry($groupuserfile,$addeduser)){
              print("<b>$addeduser</b> is already a member of <b>$groupname</b><p><HR><p>\n");
            }
            else {
              addtolist($groupuserfile,$addeduser);
              print("<b>$addeduser</b> added to group <b>$groupname</b>\n");
              print("<p><HR><p>\n");              
              mail($adminemail, "MVD member added to group", "$addeduser added to $groupname by $uname\n$groupuserfile updated\n");              
            }
          }
          else {
            print("You need to specifiy a user to add to <b>$groupname</b><p><HR><p>\n");
          }
        }
        else if($action == "removeuser") {
          if($removeduser != ""){
            if(!(testentry($groupuserfile,$removeduser))){
              print("<b>$removeduser</b> is not a member of <b>$groupname</b><p><HR><p>\n");
            }
            else {
              removeentry($groupuserfile,$removeduser);
              print("<b>$removeduser</b> removed from group <b>$groupname</b>\n");
              print("<p><HR><p>\n");              
              mail($adminemail, "MVD member removed from group", "$removeduser removed from $groupname by $uname\n$groupuserfile updated\n");              
            }
          }
          else {
            print("You need to specifiy a user to remove from<b>$groupname</b><p><HR><p>\n");
          }                
        }        
        else if($action == "delete") {
          print("<center><font size=+1>Are you sure you want to delete the group <b>$groupname</b>?</font><p>\n");
          print("<FORM ACTION=\"gutl.php\" METHOD=\"post\">\n");
          print("<INPUT TYPE=\"hidden\" name='groupname' value='$groupname'>\n");
          print("<INPUT TYPE=\"hidden\" name=\"uname\" value=\"$uname\">\n");
          print("<INPUT TYPE=\"hidden\" name=\"pword\" value=\"$pword\">\n");
          print("<INPUT TYPE=\"hidden\" name=\"destgroup\" value=\"$destgroup\">\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"){
            if(file_exists($groupfile)){
              $destgroupfile = $destgroup.".grp"; 
              $gfile = fopen($groupfile,"r");

              if(!($gfile)){
                print("Error: ");
                print("File <b>$groupfile</b> could not be read.");
                exit; 
              }

              while(!feof($gfile)){
                $line = fgets($gfile, 255);
                chop($line);  
                if($line != "" && !(testentry($destgroupfile,$line))){
                  addtolist($destgroupfile,$line);
                }
              }
              fclose($gfile);            
            }
            else{
              print("Can't find groupfile: $groupfile.<p><HR><p>\n");
              exit;
            }
            
            removeentry("groups",$groupname);
            unlink($groupfile);
            unlink($groupuserfile); 
            print("Group <b>$groupname</b> deleted<p><HR><p>\n");
            mail($adminemail, "MVD group deletion", "Group $groupname deleted by $uname\nAll files in group, if any, moved to <b>$destgroup</b>\n");
            $groupname = "";
          }
          else{
            print("<b>$groupname</b> deletion cancelled<p><HR><p>\n");
          }
        }                
      }   


if($userlevel == 1 && !$askdel && $groupname != ""){
  print("<font size=+1>Group: <b>$groupname</b></font><p>\n");

  if($groupname == "main"){
    print("All MVD users have access to this group.<br>This is a system group and can't be deleted.<br>\n");
  } 
  else if($groupname == "admin" || $groupname == "new files"){
    print("Only the MVD administrator has access to this group.<br>This is a system group and can't be deleted.<br>\n");
  }
  else{
    print("Users in this group:<br>\n");
  
    if(file_exists($groupuserfile)){
      $gulfile = fopen($groupuserfile,"r");

      if(!($gulfile)){
        print("Error: ");
        print("File <b>$groupuserfile</b> could not be read.");
        exit; 
      }

      while(!feof($gulfile)){
        $line = fgets($gulfile, 255);
        print("<b>$line</b><br>\n");
      }
      fclose($gulfile); 
            
    }
    else{
      print("Can't find groupuser file: $groupuserfile.<p><HR><p>\n");
      exit;
    }
  }  
}

print("</table></td>\n");
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 && $groupname != "main" && $groupname != 
"admin" && $groupname != "new files" && $groupname != ""){
  print("<table border=2 cellpadding=8 bgcolor=\"#99CCCC\"><td>\n");
  print("<FORM ACTION=\"gutl.php\" METHOD=\"post\">\n");  
  print("Choose an action for this group:<p>\n");

  if($groupname != "new files"){
    print("<input type=\"radio\" name=\"action\" value=\"delete\"> Delete this group.");

    print("<br>&nbsp;<font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;move files to:  <select name=\"destgroup\">\n");    

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

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

    while(!feof($gfile)){
      $uline = fgets($gfile, 255);
     
      if( chop($uline) != $groupname && chop($uline) != ""){
        print("<option>$uline</option>"); 
      }
    }
    fclose($gfile);
  
    print("</select></font><br><br>\n");

  }
  else {
    print("System group can't be deleted.");
  }
  print("<p>\n");  

  print("<input type=\"radio\" name=\"action\" value=\"removeuser\"> \n");
  print("Remove user: <select name=\"removeduser\">\n");    

  $gufile = fopen($groupuserfile,"r");

  if(!($gufile)){
    print("Error: ");
    print("File <b>$groupuserfile</b> could not be read.");
    exit; 
  }

  while(!feof($gufile)){
    $uline = fgets($gufile, 255);
    chop($uline);
    if($uline != ""){
      print("<option>$uline</option>");
    }
  }
  fclose($gufile);  
  print("</select><p>\n");

  print("<input type=\"radio\" name=\"action\" value=\"adduser\"> \n");
  print("Add this user: <select name=\"addeduser\">\n");    

  $ufile = fopen("ul.txt","r");

  if(!($ufile)){
    print("Error: ");
    print("The MVD userlist could not be read.");
    exit; 
  }

  while(!feof($ufile)){
    $uline = fgets($ufile, 255);
    //chop($uline);
    $pieces = explode(":", $uline);
    if($uline != "" && !(testentry($groupuserfile,$pieces[0]))){
      print("<option>$pieces[0]</option>");
    }
  }
  fclose($ufile);  
  print("</select><p>\n");

  print("<INPUT TYPE='hidden' name='groupname' value='$groupname'>\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=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>
