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

<?

function addtolist($list,$fname,$title){

  $FILE = fopen($list,"a");    
  fputs($FILE, "$fname,$title");  
  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",$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",$File[$index])){
      return(1);
    }
  }  
  return(0);
}

   //check for file to act on
   if(isset($File) && $File != ""){
      $ok = 0;
      $listfile = $uname.".dat";
    
      if($uname != "" && $pword != ""){
        if(file_exists($listfile)){
          $pass = file("ul.txt");
          $un = substr($uname,0,2);
          $pw = substr($pword,0,2);
           
          for($index = 0; $index < count($pass); $index++){
            if(ereg("^$un",$pass[$index])){
              if(ereg(":$pw",$pass[$index])){
                $ok = 1;
                break;
              }
            }
          }              
        }
      }
      else{
        print("You need to supply your username and password<p><HR><p>\n");
      }

      if(!(file_exists($File)) || !(testentry($listfile,$File))){
        $ok = 0;
        print("Can't find $File in your private list<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("$File copied to<br> $destname\n");                
                print("<p><HR><p>\n");
                addtolist($listfile,$destname,"Copy of $File");
                mail("shambhala@telusplanet.net", "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("$File moved to<br> $destname\n");
                print("<p><HR><p>\n");
                unlink($File);
                updatelist($listfile,$File,$destname);
                mail("shambhala@telusplanet.net", "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 == "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("$File renamed to $destname\n");
                print("<p><HR><p>\n");
                updatelist($listfile,$File,$destname);
                mail("shambhala@telusplanet.net", "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.php3\" 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=\"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");
        }
        else if($action == "confirmdelete") {
          if($sbmt == "Delete"){
            unlink($File);
            removeentry($listfile,$File);
            print("$File deleted<p><HR><p>\n");
            mail("shambhala@telusplanet.net", "MVD file deletion", "$File deleted by $uname\n$listfile updated\n");
            $File = "";
          }
          else{
            print("$File deletion cancelled<p><HR><p>\n");
          }
        }
        else if($action == "chmod") {
          if($mode != ""){
            if(!(file_exists($File))){
              print("Can't find $File<p><HR><p>\n");
            }
            else {              
              if($mode == "777"){ $setperm = 0777;}
              else if($mode == "755"){ $setperm = 0755;}
              else if($mode == "744"){ $setperm = 0744;}
              else if($mode == "711"){ $setperm = 0711;}
              else if($mode == "700"){ $setperm = 0700;}
              else if($mode == "666"){ $setperm = 0666;}
              else if($mode == "644"){ $setperm = 0644;}
              else if($mode == "600"){ $setperm = 0600;}
              else { $setperm = "755";}
              if(chmod($File, $setperm)) {
                print("$File chmodded to $mode\n");                
                print("<p><HR><p>\n");
                mail("shambhala@telusplanet.net", "MVD file chmodded", "$File chmodded to $mode by $uname\n");
              }
              else {
                print("$File could not be chmodded<p><HR><p>\n");
              }
            }
          }
          else {
            print("You need to select an option to which to chmod $File<p><HR><p>\n");
          }          
        }        
      }
   }
$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");

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

print("</table></td>\n");
print("<p><hr><p><table border=2 cellpadding=8 bgcolor=\"#99CCCC\"><td>\n");
print("<FORM ACTION=\"privutl.php3\" METHOD=\"post\">\n");
print("<center><INPUT NAME=\"File\" SIZE=30 TYPE=\"text\" value=$File> Src Filename<br>\n");
print("<INPUT TYPE='text' name='destname' size=30> Dst Filename<p></center>\n");
print("Choose an action for this file:<br>\n");
if($uname == "carve"){
print("<input type=\"radio\" name=\"action\" value=\"copy\"> Copy <br>\n");
print("<input type=\"radio\" name=\"action\" value=\"move\"> Move <br>\n");
print("<input type=\"radio\" name=\"action\" value=\"rename\"> Rename <br>\n");
print("<input type=\"radio\" name=\"action\" value=\"delete\"> Delete <p>\n");
print("<input type=\"radio\" name=\"action\" value=\"chmod\"> Chmod<br>\n");
print("<input type=radio name=\"mode\" value=\"777\"> 777<br>\n");
print("<input type=radio name=\"mode\" value=\"755\"> 755<br>\n");
print("<input type=radio name=\"mode\" value=\"744\"> 744<br>\n");
print("<input type=radio name=\"mode\" value=\"711\"> 711<br>\n");
print("<input type=radio name=\"mode\" value=\"700\"> 700<br>\n");
print("<input type=radio name=\"mode\" value=\"666\"> 666<br>\n");
print("<input type=radio name=\"mode\" value=\"644\"> 644<br>\n");
print("<input type=radio name=\"mode\" value=\"600\"> 600<p>\n");
print("<INPUT TYPE='checkbox' name='overwrite' value='on'> Overwrite if Dst file already exists<p>\n");
}
else{
print("<input type=\"radio\" name=\"action\" value=\"rename\"> Rename <br>\n");
print("<input type=\"radio\" name=\"action\" value=\"delete\"> Delete <p>\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");
print("<hr size=2> <A HREF=\"mvladmin.cgi?Action=Add/Edit%20Entry&title=/private&uname=$uname&pword=$pword\"> [ Private File Form ] </a>&nbsp;&nbsp;<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>\n");
print("<br></center>\n");

?>
</BODY>
</HTML>
