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

<?
// last updated Mar. 21, 2004

// configure variables for your particular instance of the MVD. Make sure they match the other cgi scripts.
$publistfile = "doclist.txt";                 // complete list of MVD files
$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,$fname,$title){

  global $publistfile;

  $FILE = fopen($list,"a");    
  fputs($FILE, "$fname,$title");   // need \n on end?
  fclose($FILE);

  if($list != $uname.".dat" && !(testentry($publistfile,$fname))){
    $FILE = fopen($publistfile,"a");    
    fputs($FILE, "$fname,$title");  // need \n on end?
    fclose($FILE);
  }

}

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

  global $publistfile;

  $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);

  if($list != $uname.".dat"){
    $File = file($publistfile);

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

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

}

function removeentry($list,$entry,$nopubupdate){

  global $publistfile;

  $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);

  if($list != $uname.".dat" && !$nopubupdate){
    $File = file($publistfile);

    $FILE = fopen($publistfile,"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);
}

function displaylastedit($mvddoc){

  $file = fopen("$mvddoc","r");

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

  while(!feof($file)){
    $uline = fgets($file, 255);
    if( eregi("^Last Edit:",$uline)){
      print("$uline");
      return; 
    }
  }
  fclose($file);  
}

function displaysubmitter($mvddoc){

  $file = fopen("$mvddoc","r");

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

  while(!feof($file)){
    $uline = fgets($file, 255);
    if( eregi("^Submitted by",$uline)){
      print("$uline");
      return; 
    }
  }
  fclose($file);  
}

   //check for file to act on
   if(isset($File) && $File != ""){
      $ok = 0;
      $askdel = 0;
      $userlevel = 0;
 
      if(!(isset($listfile))){
        $listfile = $uname.".dat";
      }    

      if($uname != "" && $pword != ""){
        if(file_exists($listfile)){
          $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("Can't find user file. Perhaps wrong username supplied.<p><HR><p>\n");
          exit;
        }
      }
      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;
      }      

      if(!(file_exists($File))){
        $ok = 0;
        print("Can't find $File<p><HR><p>\n");
      }

      if(!(testentry($listfile,$File))){
        $ok = 0;
        print("Can't find $File in $listfile<p><HR><p>\n");
      }
     
      if($ok){
      
        if($action == "copy"){
          if($destname != ""){
            if(file_exists($destname) && $overwrite != "on"){
              print("A file already exists by the name of $destname<br>Please choose another name<p><HR><p>\n");
            }
            else {
              if(copy($File, $destname)) {                
                print("<b>$File</b> copied to<br> <b>$destname</b>\n");                
                print("<p><HR><p>\n");
                addtolist($listfile,$destname,"Copy of $File");
                mail($adminemail, "MVD file copy", "$File copied to $destname by $uname\n$listfile updated\n");
                $File = $destname;
              }
              else {
                print("$File could not be copied to $destname<p><HR><p>\n");
              }
            }
          }
          else {
            print("You need to supply a destination filename to which to copy $File<p><HR><p>\n");
          }
        }
        else if($action == "move") {
          if($destname != ""){
            if(file_exists($destname) && $overwrite != "on"){
              print("A file already exists by the name of $destname<br>Please choose another name<p><HR><p>\n");
            }
            else {
              if(copy($File, $destname)) {
                print("<b>$File</b> moved to<br> <b>$destname</b>\n");
                print("<p><HR><p>\n");
                unlink($File);
                updatelist($listfile,$File,$destname);
                mail($adminemail, "MVD file moved", "$File moved to $destname by $uname\n$listfile updated\n");
                $File = $destname;
              }
              else {
                print("$File could not be moved to $destname<p><HR><p>\n");
              }
            }
          }
          else {
            print("You need to supply a destination filename to which to move $File<p><HR><p>\n");
          }          
        }
        else if($action == "movegroup") {

          if(file_exists($listfile)){
          
            $lfile = fopen($listfile,"r");

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

            while(!feof($lfile)){
              $uline = fgets($lfile, 255);
              if(eregi("^$File",$uline)){
                $pieces = explode(",", $uline);
                $filetitle = $pieces[1];
                break; 
              }
            }
            fclose($lfile);            
          }
          else{
            print("Can't find $listfile.<p><HR><p>\n");
            exit;
          }

          $destlistfile = $destlist.".grp";

          addtolist($destlistfile,$File,$filetitle);
          removeentry($listfile,$File,1);           
          
          $oldgroupname = ereg_replace(".grp","", $listfile);
          
          mail($adminemail, "MVD file moved from group", "$File moved from $oldgroupname to $destlist by $uname\n");		
          print("<b>$File</b> moved from group <b>$oldgroupname</b> to group <b>$destlist</b> by $uname\n");
          print("<p><HR><p>\n");
          $listfile = $destlistfile;
        }
        else if($action == "movetoprivate") {

          if(file_exists($listfile)){
          
            $lfile = fopen($listfile,"r");

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

            while(!feof($lfile)){
              $uline = fgets($lfile, 255);
              if(eregi("^$File",$uline)){
                $pieces = explode(",", $uline);
                $filetitle = $pieces[1];
                break; 
              }
            }
            fclose($lfile);            
          }
          else{
            print("Can't find $listfile.<p><HR><p>\n");
            exit;
          }

          $userlistfile = $userlist.".dat";

          addtolist($userlistfile,$File,$filetitle);
          removeentry($listfile,$File,0);           
          
          $oldgroupname = ereg_replace(".grp","", $listfile);
          
          mail($adminemail, "MVD file moved to private list", "$File moved from $oldgroupname to the private files of $userlist by $uname\n");		
          print("<b>$File</b> moved from group <b>$oldgroupname</b> to the private files of  <b>$userlist</b> by $uname\n");
          print("<p><HR><p>\n");
          //$listfile = $destlistfile;
          $askdel = 1; 
        }
        else if($action == "rename") {
          if($destname != ""){
            if(file_exists($destname) && $overwrite != "on"){
              print("A file already exists by the name of $destname<br>Please choose another name<p><HR><p>\n");
            }
            else {
              if(rename($File, $destname)) {
                print("<b>$File</b> renamed to <b>$destname</b>\n");
                print("<p><HR><p>\n");
                updatelist($listfile,$File,$destname);
                mail($adminemail, "MVD file renamed", "$File renamed to $destname by $uname\n$listfile updated\n");
                $File = $destname;
              }
              else {
                print("$File could not be renamed to $destname<p><HR><p>\n");
              }
            }
          }
          else {
            print("You need to supply a destination filename to which to rename $File<p><HR><p>\n");
          }          
        }
        else if($action == "delete") {
          print("<center><font size=+1>Are you sure you want to delete <b>$File</b>?</font><p>\n");
          print("<FORM ACTION=\"privutl.php\" METHOD=\"post\">\n");
          print("<INPUT TYPE=\"hidden\" name=\"File\" value=\"$File\">\n");
          print("<INPUT TYPE=\"hidden\" name=\"uname\" value=\"$uname\">\n");
          print("<INPUT TYPE=\"hidden\" name=\"pword\" value=\"$pword\">\n");

          print("<INPUT TYPE='hidden' name='listfile' value='$listfile'>\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"){
            unlink($File);
            removeentry($listfile,$File,0);
            print("<b>$File</b> deleted<p><HR><p>\n");
            mail($adminemail, "MVD file deletion", "$File deleted by $uname\n$listfile updated\n");
            $File = "";
          }
          else{
            print("$File deletion cancelled<p><HR><p>\n");
          }
        }                
      }
   }

if($File != "" && !$askdel && $ok){
$fsize = filesize($File);
//$lastaccess = date("h:i:s a, l, F jS, Y",fileatime($File));
//$lastchange = date("h:i:s a, l, F jS, Y",filectime($File));
//$lastmodification = date("h:i:s a, l, F jS, Y",filemtime($File));
//$owner = fileowner($File);
//$group = filegroup($File);
//$inode = fileinode($File);
//$ftype = filetype($File);

print("<table border=0><td>\n");
print("Filename: $File<br>\n");
print("Size: $fsize bytes<br>\n");
//print("Last access: $lastaccess<br>\n");
//print("Last modification: $lastmodification<br>\n");
displaylastedit($File);
print("<br>\n");

if($userlevel == 1){
  //print("Last change: $lastchange<br>\n");
  printf("Permissions: %o<br>\n", (fileperms($File) & 0777));
  displaysubmitter($File);
  print("<br>\n");
  //print("INode: $inode<br>\n");
  //print("Type: $ftype<br>\n");
  //print("Owner: $owner<br>\n");
  //print("Group: $group<br>\n");
}
//print("<font size=1>user $userlevel</font><br>\n");

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><table border=2 cellpadding=8 bgcolor=\"#99CCCC\"><td>\n");
print("<FORM ACTION=\"privutl.php\" METHOD=\"post\">\n");
print("<center><INPUT NAME=\"File\" SIZE=30 TYPE=\"hidden\" value=$File>Filename: <b>$File</b><br>\n");
print(" New Filename: <INPUT TYPE='text' name='destname' size=30><p></center>\n");
print("Choose an action for this file:<br>\n");

print("<input type=\"radio\" name=\"action\" value=\"copy\"> Copy <br>\n");

if($userlevel == 1 || $listfile == $uname.".dat"){
  print("<input type=\"radio\" name=\"action\" value=\"rename\"> Rename <br>\n");
  print("<input type=\"radio\" name=\"action\" value=\"delete\"> Delete");
}
print("<p>\n");

if($userlevel == 1 && $listfile != $uname.".dat"){

  $groupname = ereg_replace(".grp","", $listfile);

  print("<input type=\"radio\" name=\"action\" value=\"movegroup\"> \n");
  print("Move file from group<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size=+1><b>$groupname</font></b> to <select name=\"destlist\">\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><p>\n");
  
  print("<input type=\"radio\" name=\"action\" value=\"movetoprivate\"> \n");
  print("Move file to the private list of: <select name=\"userlist\">\n");    

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

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

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

if($userlevel == 1){
  print("<INPUT TYPE='checkbox' name='overwrite' value='on'> Overwrite 
if New Filename already exists<p>\n");
}

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

print("<center><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");
}

if($listfile == $uname.".dat"){
print("<center><hr size=2> <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></center>\n");
}
else {
print("<center><hr size=2> <A 
HREF=\"mvladmin.cgi?Action=Add/Edit%20Entry&Public=on&uname=$uname&pword=$pword&group=$groupname#Files\"> 
[ Public File Form ] &nbsp;&nbsp;<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></center>\n");
}

print("<br></center>\n");

?>
</BODY>
</HTML>
