<?php
  /**************************************************************************\
  * phpGroupWare module (File Manager)                                       *
  * Written by Dan Markus Graf <m.graf@cyberunix.com>                        *
  * --------------------------------------------                             *
  *  This program is free software; you can redistribute it and/or modify it *
  *  under the terms of the GNU General Public License as published by the   *
  *  Free Software Foundation; either version 2 of the License, or (at your  *
  *  option) any later version.                                              *
  \**************************************************************************/
  //
  // if (! $sessionid)
  //              Header("Location: ../login.php");
  $phpgw_info["flags"]["currentapp"] = "filemanager";
  $phpgw_info["flags"]["noheader"] = True;
  include("../header.inc.php");
  if ($download) {
     header("Content-disposition: filename=$fn");
     header("Content-type: application/octetstream");
     header("Pragma: no-cache");
     header("Expires: 0");
     echo $phpgw->vfs->read($actiondir.$pwd.$fn);
     $phpgw->common->phpgw_exit();
   } else {
     $phpgw->common->phpgw_header();
   }
?>
<?php
  if (isset($exit)) { unset($edit); unset($upload); }
  $phpgw->vfs->verifydir();
  
  if (empty($currentdir)){ 
    $phpgw->vfs->cd("/"); 
  }else{
    $phpgw->vfs->cd($currentdir); 
  }
  
  if (empty($pwd)){ $pwd = "/"; }
  if (empty($actiondir)){ $actiondir = $currentdir; }
  if ($action == "upload"){ $phpgw->vfs->cp($userfile,"$actiondir$pwd$userfile_name",True); }
  if ($action=="upload") {
          exec ("cp $userfile $baserep/$dir/$userfile_name");
  }
  
  
  $file = $actiondir.$pwd.$fn;
  $userdir = $phpgw_info["user"]["private_dir"];
  ?>
  
  <?php
  function listrep ($base,$rep,$reste) {
    if (ereg("^/.*",$dir)) {
      $dir = substr($dir,1);
    }
    $pos = strpos($rep,"/");
    $rep1 = substr($rep,$pos+1);
    $token = strtok($rep,"/");
    $d = dir("$base");
    $ancrep = exec("pwd");
    exec ("cd $base");
    chdir("$base");
    $res = "";
    while($entry=$d->read()) {
      if ($entry == $token) {
        $res = $res. "<a HREF=\"index.php?sessionid=" . $GLOBALS["sessionid"] . "&dir=$reste/$entry\"><img src=\"folder_open.gif\" border=0>".str_replace("_"," ",$entry)."</A><br>\n";
        $res = $res."<table width=100%><tr><td width=5%>&nbsp;</td><td>".listrep("$base/$token","$rep1","$reste/$token")."</td></tr></table>";
      } else {
        if (is_dir($entry) && $entry != "." && $entry != "..") {
          $res = $res."<a HREF=\"index.php?sessionid=" . $GLOBALS["sessionid"] . "&dir=$reste/$entry" . "\"><img src=\"folder_close.gif\" border=0>".str_replace("_"," ",$entry)."</A><br>\n";
        }
      }
    }
    $d->close();
    exec ("cd $ancrep");
    chdir ($ancrep);
    return($res);
  }
  //------------------------------------------------
  //               Name of directory
  //------------------------------------------------
  $baserep = $userdir; 
  exec("pwd");
  if (ereg("^/.*",$dir)) {
    $dir = substr($dir,1);
  }
  echo "<H1>DIRECTORY \"$currentdir/$dir\"</H1><BR>";

  //------------------------------------------------
  //               What kind of action ?
  //------------------------------------------------
  if ($action=="Delete") {
    chdir ("$baserep/$dir");
    exec ("cd $baserep/$dir");
    exec ("cd $baserep/$dir");
    if (is_file($file)) {
      exec ("rm $baserep/$dir/$file");
    } elseif (is_dir($file)) {
      exec ("rm -r $baserep/$dir/$file");
    }
  }

  if ($action=="Rename") {
    chdir ("$baserep/$dir");
    exec ("cd $baserep/$dir");
    if (is_file($file) || is_dir($file)) {
      $newfile =  str_replace(" ","_",$newfile);
      $newfile =  str_replace("|","_",$newfile);
      $newfile =  str_replace("&","_",$newfile);
      exec ("mv $baserep/$dir/$file $baserep/$dir/$newfile");
    }
  }

  if ($action=="Create") {
    chdir ("$baserep/$dir");
    exec ("cd $baserep/$dir");
    if (!is_file($file) || !is_dir($file)) {
      //save for illegal hacking
      $file =  str_replace(" ","_",$file);
      $file =  str_replace("|","_",$file);
      $file =  str_replace("&","_",$file);
   
      exec ("mkdir ". $baserep . "/" . $dir . "/" .$file);
    }
  }

?>
  <form method=post enctype="multipart/form-data" action="<?php echo $phpgw->link($PHP_SELF);?>">
  <small>Upload a file in the open directory:</small>
  <input type=hidden name="dir" value=<? echo $dir; ?>>
  <input type=hidden name="action" value=upload>
  <input type=file name=userfile size=40 value=toto>
  <input name=upload value=" Upload " type=submit>
  </form>
  <table width=100% border=1>
    <TR><TH>Dossiers</TH><TH>Possible actions on the selected file:</TH></TR>
    <TR><TD valign=top>
<?
    echo listrep($baserep,$dir,"");
?>  <br><br>
    <a href="<?php echo $phpgw->link("index.php");?>"><img src="folder_close.gif" border=0 alt="Rootdirectory"><big>  /</big></a>
    <BR>
    <form method=post action="<?php echo $phpgw->link($PHP_SELF);?>">
    Create a new directory: <input type=text name=file><input type=submit name=action value="Create">
    &nbsp;</TD>
    <TD valign=top>
      <input type=submit  name=action value=Delete>  <BR>
      <input type=submit name=action value=Rename> in <input type=text size=40 name=newfile><BR>
      <input type=hidden name=dir value="<? echo $dir; ?>"><BR>
    <HR>
    <B><div align=center>Files list in the open directory:</div></B><HR>
<?
  $d = dir("$baserep/$dir");
   exec ("cd $baserep/$dir");
   chdir("$baserep/$dir");
    while($entry=$d->read()) {
      if ($entry != "." && $entry != "..") {
        echo "&nbsp;<input type=radio name=file value=\"$entry\"> ";
        if (is_dir($entry)) {
          echo "<img src=folder_close.gif> ";
        }
        echo $entry."<BR>\n";
      }
    }
    $d->close();
?>
    &nbsp;</form></TD></TR>
  </table>
  <br>
<?php $phpgw->common->phpgw_footer(); ?>
