<?php
/**
 *
 * Author: Steve Bourgeois <owl@bozzit.com>
 *
 * Owl Ultra Lite
 *
 * Copyright (c) 2005-2006 Bozz IT Consulting Inc
 * Licensed under the GNU GPL. For full terms see http://www.gnu.org/licenses/licenses.html#TOCGPL
 *
 */


require_once(dirname(__FILE__) . "/config/owl.php");
require_once($default->root_fs . "/lib/owlulite.lib.php");

import_request_variables('pgc');

global $owl_lang;

$iNumberOfOptionalColumns = $default->display_permissions + $default->display_size + $default->display_last_modified + 1; 

if (!isset($folder) || $folder == "" or $folder == "/" or $folder == "." or $folder == "..")
{
      if (!file_exists($default->trash_can_location))
      {
         mkdir($default->trash_can_location, $default->directory_mask);
      }
      $default->trash_can_location = $default->trash_can_location . "/" . $sRootFolderName;
      $sRecyclePath = $sRootFolderName;
      $folder = $sRootFolderName;
}
else
{
      $folder = ereg_replace("\.\.","",$folder);
      $default->trash_can_location .= "/" . $folder;
      $sRecyclePath = $folder;
} 

if(isset($download))
{
   $fspath = $default->trash_can_location  . "/" . $download;

   if(file_exists($fspath))
   {
      $fsize = filesize($fspath); 
      header( 'Pragma: ' );
      header( 'Cache-Control: ' );
      header( 'Content-Type: application/octet-stream' );
      header( 'Content-Disposition: attachment; filename="' . $download . '"' );
      header("Content-Length: $fsize");
      header("Expires: 0");
   
      if (substr(php_uname(), 0, 7) != "Windows")
      {
         $fp = fopen("$fspath", "r");
      }
      else
      {
         $fp = fopen("$fspath", "rb");
      }
      fpassthru($fp);
      exit();
   }
   else
   {
      $sMessage = $owl_lang->err_file_not_accessible;
   }

}


if(isset($btn_browse))
{
      header("Location: $default->root_url" . "/index.php");
      exit;
}

if (isset($btn_empty_trash))
{
   mydelete($default->trash_can_location);
   mkdir($default->trash_can_location, $default->directory_mask);
   
}

if(isset($delete_file))
{
   $delete_file = ereg_replace("\.\.","",$delete_file);
   $fspath = $default->trash_can_location  . "/" . $delete_file;

   if(file_exists($fspath))
   {
      unlink($fspath);
   }
   else
   {
      $sMessage = $owl_lang->err_file_not_accessible;
   }
}

if(isset($restore_folder))
{
   $restore_folder = ereg_replace("\.\.","",$restore_folder);
   $fspath = $default->trash_can_location  . "/" . $restore_folder;

   if(file_exists($fspath))
   {
      $pathArray = explode( '/',  $folder);
      foreach ($pathArray as $sPath)
      {
        $sNewPath .= "/" . $sPath;
        if (!file_exists($default->root_folder . $sNewPath))
        {
           mkdir($default->root_folder . $sNewPath, $default->directory_mask);
        }
      }
      if (file_exists($default->root_folder . "/" . $folder . "/" . $restore_folder))
      {
         $sMessage = $owl_lang->err_folder_already_exists;
      }
      else
      {
         rename($fspath, $default->root_folder . "/" . $folder . "/" . $restore_folder);
      }
   }
   else
   {
      $sMessage = $owl_lang->err_file_not_accessible;
   }
}

if(isset($restore_file))
{
   $restore_file = ereg_replace("\.\.","",$restore_file);
   $fspath = $default->trash_can_location  . "/" . $restore_file;

   if(file_exists($fspath))
   {
      $pathArray = explode( '/',  $folder);
      foreach ($pathArray as $sPath)
      {
        $sNewPath .= "/" . $sPath;
        if (!file_exists($default->root_folder . $sNewPath))
        {
           mkdir($default->root_folder . $sNewPath, $default->directory_mask);
        }
      }
      if (file_exists($default->root_folder . "/" . $folder . "/" . $restore_file))
      {
         $sMessage = $owl_lang->err_file_already_exists;
      }
      else
      {
         rename($fspath, $default->root_folder . "/" . $folder . "/" . $restore_file);
      }
   }
   else
   {
      $sMessage = $owl_lang->err_file_not_accessible;
   }
}

if(isset($delete_folder))
{
   $delete_folder = ereg_replace("\.\.","",$delete_folder);
   $fspath = $default->trash_can_location  . "/" . $delete_folder;

   if(file_exists($fspath))
   {
      mydelete($fspath);
   }
   else
   {
      $sMessage = $owl_lang->err_file_not_accessible;
   }
}

include("lib/header.inc");
include("lib/userheader.inc");


if (!file_exists($default->trash_can_location))
{
   mkdir($default->trash_can_location, $default->directory_mask);
}

if ($default->use_progress_bar == 1)
{
   $sid = md5(uniqid(rand()));
   print("<form name=\"FileList\" enctype=\"multipart/form-data\" action=\"/cgi-bin/upload.cgi?sid=$sid\" method=\"post\">\n");
   print("<input type=\"hidden\" name=\"sessionid\" value=\"$sid\"></input>\n");
}
else
{
   print("<form name=\"FileList\" enctype=\"multipart/form-data\" action=\"" . $_SERVER["PHP_SELF"] ."\" method=\"post\">\n");
   print("<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$default->max_filesize\"></input>\n");
}

print("<input type=\"hidden\" name=\"folder\" value=\"$folder\"></input>\n");

print("<table class=\"border1\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"90%\"><tr><td align=\"left\" valign=\"top\" width=\"100%\">\n");
print("<br />\n");
print("<table class=\"border2\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"><tr><td align=\"left\" valign=\"top\" width=\"100%\">\n");
                                                                                                                                                                                                 
print("<br />\n");

print("<table class=\"margin2\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"><tr><td align=\"left\" valign=\"top\">\n");
print("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">");
if(!empty($sMessage))
{
   print("<tr><td class=\"admin3\" width=\"100%\" colspan=\"10\">$sMessage</td></tr>\n");
}
print("<tr><td class=\"admin1\" width=\"100%\" colspan=\"10\"><a href=\"http://owl.sourceforge.net\"><img src=\"$default->root_url/graphics/owl_lite.jpg\" alt=\"Owl Utra-Lite Version $default->version\" title=\"Owl Utra-Lite Version $default->version\" border=\"0\"></img></td></tr>\n");
print("<tr><td width=\"100%\" colspan=\"10\">&nbsp;</td></tr>\n");
print("<tr><td class=\"admin0\" width=\"100%\" colspan=\"10\">$owl_lang->recycle_title </td></tr>\n");

// 
// Show the Directories in the
// Recycle Bin
// 

$iCountLines = 0;

if ($Dir = opendir($default->trash_can_location))
{
   print("<tr><td class=\"dir1\" colspan=\"10\" height=\"20\" align=\"left\" wrap=\"nowrap\">$owl_lang->current_folder ");
   $aBreadCrumb = array();
   $aBreadCrumb =  split("\/",  $folder);
   $sPath  = $sRootFolderName;

   $iFirstime = true;

   foreach(  $aBreadCrumb as $sDirectory)
   {
      if ($iFirstime === false)
      {
         $sPath .= "/" . $sDirectory;
      }
      else
      {
         $iFirstime = false;
      }
      print("/<a class=\"lfile1\" href=\"" . $_SERVER["PHP_SELF"] . "?folder=" . $sPath . "\">$sDirectory</a>");
   }
   print("</td>\n</tr>\n");

   print("<tr><td class=\"title1\" align=\"left\">&nbsp;</td>");
   print("<td class=\"title1\" align=\"left\">$owl_lang->title_name</td>");
   print("<td class=\"title1\" align=\"left\">$owl_lang->title_actions</td>");
   if ($default->display_permissions)
   {
      print("<td class=\"title1\" align=\"left\">$owl_lang->title_permission</td>");
   }
   if ($default->display_size)
   {
      print("<td class=\"title1\" align=\"left\">$owl_lang->title_size</td>");
   }
   if ($default->display_last_modified)
   {
      print("<td class=\"title1\" align=\"left\">$owl_lang->title_last_modified</td></tr>");
   }


   if (!($sRecyclePath == $sRootFolderName))
   { 
      $filesearch = explode('/', $sRecyclePath);
      $extensioncounter = 0;
      while ($filesearch[$extensioncounter + 1] != null)
      { 
         // pre-append a "." separator in the name for each
         // subsequent part of the the name of the file.
         if ($extensioncounter != 0)
         {
            $firstpart = $firstpart . "/";
         } 
         $firstpart = $firstpart . $filesearch[$extensioncounter];
         $extensioncounter++;
      } 

      if ($extensioncounter == 0)
      {
         $firstpart = $filename;
         $file_extension = '';
      } 
      else
      {
         $file_extension = $filesearch[$extensioncounter];
      } 

      print("\t\t\t\t<tr>");
      print("<td class=\"title1\" width=\"1%\" align=\"left\"><img src=\"$default->root_url/graphics/folder_closed.gif\" border=\"0\" alt=\"\"></img></td>\n");
      print("<td class=\"title1\" align=\"left\"><a class=\"lfile1\" href=\"" . $_SERVER["PHP_SELF"] . "?folder=" . $firstpart . "\">..</a></td>\n");
      print("<td class=\"title1\" align=\"left\">&nbsp;</td>");
      if ($default->display_permissions)
      {
         print("<td class=\"title1\" align=\"left\">&nbsp;</td>\n");
      }
      if ($default->display_size)
      {
         print("<td class=\"title1\" align=\"left\">&nbsp;</td>\n");
      }
      if ($default->display_last_modified)
      {
         print("<td class=\"title1\" align=\"left\">&nbsp;</td>\n");
      }
      print("</tr>\n");
   } 

   while ($file = readdir($Dir))
   {
      if ($file[0] == '.')
      {
         continue;
      } 
      if (!is_file($default->trash_can_location . "/" . $file))
      {
         $iCountLines++;
         $iPrintLines = $iCountLines % 2;

         if ($iPrintLines == 0)
         {
            $sTrClass = "hover1";
            $sLfList = "lfile1";
            $sTrClassHilite = "mouseover1";
            $sTrClassHiliteAlt = "mouseover3";
         }
         else
         {
            $sTrClass = "hover2";
            $sLfList = "lfile1";
            $sTrClassHilite = "mouseover2";
            $sTrClassHiliteAlt = "mouseover3";
         }

         print("<tr id=\"foldertr" . $iCountLines  . "\" class=\"$sTrClassHilite\" onmouseover=\"alt_css_style('fcheckid" .  $iCountLines . "', this, '$sTrClassHiliteAlt')\"  onmouseout=\"alt_css_style('fcheckid" . $iCountLines . "', this, '$sTrClassHilite')\">\n");
         print("<td class=\"$sTrClass\"><img src=\"$default->root_url/graphics/folder_closed.gif\" border=\"0\" alt=\"\"></img></td>\n<td width=\"100%\" class=\"$sTrClass\"><a class=\"$sLfList\" href=\"" . $_SERVER["PHP_SELF"] ."?folder=" . $sRecyclePath . "/" . $file . "\">" . $file . "</a></td>\n");
         print("<td class=\"$sTrClass\" align=\"left\">");
         print("<a class=\"$sLfList\" href=\"" . $_SERVER["PHP_SELF"] ."?folder=" . $sRecyclePath . htmlentities("&") . "delete_folder=$file\"");
         print(" onclick=\"return confirm('");
         printf($owl_lang->reallydelete, $file);
         print("');\">");
         print("<img src=\"$default->root_url/graphics/trash.gif\" border=\"0\" alt=\"");
         printf($owl_lang->alt_reallydelete, $file);
         print("\"");
         print(" title=\"");
         printf($owl_lang->alt_reallydelete, $file);
         print("\">");
         print("</img>");
         print("</a>");
         print("&nbsp;<a class=\"$sLfList\" href=\"" . $_SERVER["PHP_SELF"] ."?folder=" . $sRecyclePath . htmlentities("&") . "restore_folder=$file\"");
         print(" onclick=\"return confirm('");
         printf($owl_lang->reallyrestore, $file);
         print("');\">");
         print("<img src=\"$default->root_url/graphics/prev.gif\" border=\"0\" alt=\"");
         printf($owl_lang->alt_reallyrestore, $file);
         print("\"");
         print(" title=\"");
         printf($owl_lang->alt_reallyrestore, $file);
         print("\">");
         print("</img>");
         print("</a>");
         print("</td>");

         if ($default->display_permissions)
         {
            print("<td class=\"$sTrClass\" align=\"left\">&nbsp;</td>\n");
         }
         if ($default->display_size)
         {
            print("<td class=\"$sTrClass\" align=\"left\">&nbsp;</td>\n");
         }
         if ($default->display_last_modified)
         {
            print("<td class=\"$sTrClass\" align=\"left\">&nbsp;</td>\n");
         }
         print("</tr>\n");
      } 
   } 
} 
else
{
   exit($owl_lang->err_general);
} 
// 
// Show the Files in the
// Recycle Bin
// 
if ($Dir = opendir($default->trash_can_location))
{
   while ($file = readdir($Dir))
   {
      if ($file[0] == '.')
         continue;

      if (is_file($default->trash_can_location . "/" . $file))
      {
         $iCountLines++;
         $iPrintLines = $iCountLines % 2;

         if ($iPrintLines == 0)
         {
            $sTrClass = "hover1";
            $sLfList = "lfile1";
            $sTrClassHilite = "mouseover1";
            $sTrClassHiliteAlt = "mouseover3";
         }
         else
         {
            $sTrClass = "hover2";
            $sLfList = "lfile1";
            $sTrClassHilite = "mouseover2";
            $sTrClassHiliteAlt = "mouseover3";
         }

         print("<tr id=\"foldertr" . $iCountLines  . "\" class=\"$sTrClassHilite\" onmouseover=\"alt_css_style('fcheckid" .  $iCountLines . "', this, '$sTrClassHiliteAlt')\"  onmouseout=\"alt_css_style('fcheckid" . $iCountLines . "', this, '$sTrClassHilite')\">\n");

         print("<td  class=\"$sTrClass\" width=\"1%\">");


         $choped = split("\.", $file);
         $pos = count($choped);
         if ( $pos > 1 )
         {
            $ext = strtolower($choped[$pos-1]);
            $sDispIcon = $ext;
         }
         else
         {
            $sDispIcon = "NoExtension";
         }
                                                                                                                                                                                                     
         if (($ext == "gz") && ($pos > 2))
         {
            $exttar = strtolower($choped[$pos-2]);
            if (strtolower($choped[$pos-2]) == "tar")
               $ext = "tar.gz";
         }
                                                                                                                                                                                                  
         if (!file_exists("$default->root_fs/graphics/icon_filetype/$sDispIcon.gif"))
         {
            $sDispIcon = "file";
         }
         print("<img src=\"$default->root_url/graphics/icon_filetype/$sDispIcon.gif\" border=\"0\" alt=\"\"></img>");
         print("</td>\n<td class=\"$sTrClass\">");
         print("<a class=\"$sLfList\" href=\"" . $_SERVER["PHP_SELF"] ."?folder=" . $sRecyclePath . htmlentities("&") . "download=$file\">" . $file . "</a>");
         print("</td>\n");
         print("<td class=\"$sTrClass\" align=\"left\">");
         print("<a class=\"$sLfList\" href=\"" . $_SERVER["PHP_SELF"] ."?folder=" . $sRecyclePath . htmlentities("&") . "delete_file=$file\"");
         print(" onclick=\"return confirm('");
         printf($owl_lang->reallydelete, $file);
         print("');\">");
         print("<img src=\"$default->root_url/graphics/trash.gif\" border=\"0\" alt=\"");
         printf($owl_lang->alt_reallydelete, $file);
         print("\"");
         print(" title=\"");
         printf($owl_lang->alt_reallydelete, $file);
         print("\">");
         print("</img>");
         print("</a>");
         print("&nbsp;<a class=\"$sLfList\" href=\"" . $_SERVER["PHP_SELF"] ."?folder=" . $sRecyclePath . htmlentities("&") . "restore_file=$file\"");
         print(" onclick=\"return confirm('");
         printf($owl_lang->reallyrestore, $file);
         print("');\">");
         print("<img src=\"$default->root_url/graphics/prev.gif\" border=\"0\" alt=\"");
         printf($owl_lang->alt_reallyrestore, $file);
         print("\"");
         print(" title=\"");
         printf($owl_lang->alt_reallyrestore, $file);
         print("\">");
         print("</img>");
         print("</a>");
         print("</td>");
         if ($default->display_permissions)
         {
            print("<td class=\"$sTrClass\">" . fShowPerms($default->trash_can_location . "/" . $file) ."</td>\n");
         }
         if ($default->display_size)
         {
            print("<td class=\"$sTrClass\">" . gen_filesize(filesize($default->trash_can_location . "/" . $file)) ."</td>\n");
         }
         if ($default->display_last_modified)
         {
            print("<td class=\"$sTrClass\">" . date("M d, Y \a\\t h:i a", filemtime($default->trash_can_location . "/" . $file)) ."</td>\n");
         }
         print("</tr>\n");
      } 
   } 
} 
else
{
   exit($owl_lang->err_general);
} 

if ($default->owl_read_only == 0)
{
   print("<tr><td  class=\"$sTrClass\" colspan=\"" . (2 + $iNumberOfOptionalColumns) . "\" height=\"40\" align=\"left\">&nbsp;</td></tr>\n");
   print("<tr>\n");
   print("<td colspan=\"" . (1 + $iNumberOfOptionalColumns) . "\" class=\"form1\">&nbsp;</td>\n");
   print("<td class=\"form2\" width=\"100%\" nowrap=\"nowrap\">");
   print("<table>");
   print("<tr><td colspan=\"2\">");
   fPrintSubmitButton($owl_lang->btn_empty_trash, $owl_lang->alt_btn_empty_trash , "submit", "btn_empty_trash");
   fPrintSubmitButton($owl_lang->btn_browse, $owl_lang->alt_btn_browse , "submit", "btn_browse");
   print("</td></tr>");
   print("</table>");
   print("</td>");
   print("</tr>");
}

print("</table>");
print("</td></tr></table>\n");
print("<br />\n");
print("</td></tr></table>\n");

include("lib/footer.inc");

?>
