Location: PHPKode > projects > PHP Web FileManager - Similiar to Yahoo! > index.php
<?php
// Scroll down a about a page for Constructor Initialization

include_once("lib/filemanager.class.php");
include_once("lib/locale.class.php");
include_once("lib/lib.func.php");

$errormsg = "";
$isChanged = false;
$isUpload = false;
$menuOption = 0;
$frm = $_POST;


//
// Because of locale support we simply check if button has value
// if so that indicates button was pressed.  Possible problem
// with this.  Will consider fixing at a later date
$actionCancel  = !empty($frm["cancel"])  ? "Cancel"  : "";
$actionConfirm = !empty($frm["confirm"]) ? "Confirm" : "";
$actionCopy    = !empty($frm["copy"])    ? "Copy"    : "";
$actionRename  = !empty($frm["rename"])  ? "Rename"  : "";
$actionDelete  = !empty($frm["delete"])  ? "Delete"  : "";
$actionUpload  = !empty($frm["upload"])  ? "Upload Files"  : "";
$actionNewDir  = !empty($frm["newdir"])  ? "New Folder"  : "";
$actionRefresh = !empty($frm["refresh"])  ? "Refresh"  : "";
$subDir        = !empty($_GET["f"])      ? $_GET["f"]  : "";
$actionAdvance = !empty($frm["advance"])      ? "Advance"  : "";
$actionAdvanceOption = !empty($frm["advanceoption"]) ? $_POST["advanceoption"]  : 0;
if (empty($subDir))
  $subDir = !empty($frm["f"]) ? $frm["f"] : "";
$actionSort      = !empty($_GET["sort"])    ? $_GET["sort"]      : -1;
$actionSortPrev  = !empty($_GET["sortprev"]) ? $_GET["sortprev"] : 0;
$actionSortDir  = !empty($_GET["sortdir"]) ? $_GET["sortdir"] : 0;

if (strcmp($actionRefresh,"Refresh")==0) {
  $actionSort = -1;
  $actionSortPrev = 0;
  $actionSortDir = 0;
}

// Security Hack Patch.... double checks $subDir which is the variable
// input from $f ( POST and GET )
if (preg_match('/^[a-zA-Z0-9\/]+$/',$subDir )) {
  // okay - valid sub directory
} else {
  // bad - invalid possible hacking ../../  
  $subDir = "";
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Initial Setup/configuration
//

  // Init locale and set locale
  $locale = new Locale;  // set language
  $locale->setLanguage("en-us");  // Set to English
  //$locale->setLanguage("de");   // Set to German
  //$locale->setLanguage("dk");   // Set to Danish
  //$locale->setLanguage("pt-br");     // Set to Portuguese(Brazilian) 
  //$locale->setLanguage("hide@address.com");     // Set to Spanish
  //$locale->setLanguage("hu");     // Set to Spanish


  // if $_ENV["temp"] isn't working simply set you temp dir ie: c:/temp
//  $theFileManager = new FileManager("johndoe",$_ENV["TEMP"],"d:/home/users",$locale); 
  $theFileManager = new FileManager("johndoe","m:/temp","m:/program files/apache group/apache/htdocs/filemanagerusers",$locale,"/filemanagerusers");
  $theFileManager->setMaxChainSubDir(0);// 0 allows as many infinite chained subdirectories, any value greater then 0 sets a maximum
                                        // ie: 3 would allow you to only create 3 chained level
                                        //
                                        //  Root
                                        //   |
                                        //   |----Child1----Child1.1----Child1.1.1
                                        //   |----Child2
                                        //   |----Child3
                                        //   |----Child4
                                        //
                                        //  It is NOT a limit on how many subdirectories but a limit of subdirectories
                                        //  witin subdirectories.  In the above example you won't be able to create
                                        //  a directory under Child1.1.1
                                        //  But you are still allowed to create directories under the root or any other child

  $theFileManager->setIgnoreCaseNameSort(true);  // set ignore on - ignore capital letters when sorting 'Name' column


  // Set Compression Program Configuration
  // If under *nix environment should be okay
  // Under another environment you will need the equilvalent programs for tar and gzip
  // ie: For windows you can use the tar, gzip & gunzip from http://unxutils.sourceforge.net/
  // To make life easier for myself I broke the directory structure and stuck wbin at c: root
  $compress_cfg["set_export_on"] = true;
  $compress_cfg["set_import_on"] = true;
  $compress_cfg["dir_tar"] = "c:/wbin/tar.exe";
  $compress_cfg["dir_gzip"] = "c:/wbin/gzip.exe";
  $compress_cfg["dir_gunzip"] = "c:/wbin/gunzip.exe";


  // Set Extensions you do not want to allow for upload
  // Don't forget you might have to adjust you size limit in the php.ini file
  $invalidExtensions = array ("php","shtml","php3","inc","class","jsp","asp","xml","chm","exe");
  $theFileManager->setInvalidExt($invalidExtensions);

  if (!$theFileManager->isMemberDirExist()) {
    echo $locale->getString(LID_TYPE_GENERAL,LID_MEMBER_DOES_NOT_EXIST);
    die;
  }

  $theList = $theFileManager->getDirList($subDir);  // get current directory listing


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$isConfirm = false;

if ( strcmp($actionConfirm,"Confirm")==0) {
  $isConfirm = true;

  if ( strcmp($actionCopy,"Copy")==0) {
    $isChanged = false;
    for ($i=0; $i<$theFileManager->getFileCount(); $i++ ){
      if (!empty( $frm["newname" . $i])) {
           $isChanged = true;
           $errormsg .= $theFileManager->copyFile($theList[$i]["name"],$frm ["newname" . $i]);
      }
    }
    if (!$isChanged) {
      $errormsg = $locale->getString(LID_TYPE_MESSAGES,LID_MSG_NO_FILE_GIVEN);
    }
  }

  if (strcmp($actionRename,"Rename")==0) {
    $isChanged = false;
    for ($i=0; $i<$theFileManager->getFileCount(); $i++ ){
      if (!empty( $frm["newname" . $i])) {
           $isChanged = true;
           $errormsg .= $theFileManager->renameFile($theList[$i]["name"],$frm ["newname" . $i]);
      }
    } // end for

    if (!$isChanged) {
      $errormsg = $locale->getString(LID_TYPE_MESSAGES,LID_MSG_NO_FILE_GIVEN);
    }
  } // end rename

  if (strcmp($actionDelete,"Delete")==0) {
    $isChanged = false;
    $isUpload=true;
    for ($i=0; $i<$theFileManager->getFileCount(); $i++ ){
      if (!empty( $frm["newname" . $i])) {
        $isChanged = true;
        $errormsg .= $theFileManager->deleteFile($theList[$i]["name"],$frm ["newname" . $i]);

      } // end if empty
    } // end for
    if (!$isChanged) {
      $errormsg = $locale->getString(LID_TYPE_MESSAGES,LID_MSG_NO_DEL_FILE_GIVEN);
    }
  } // end delete

  if (strcmp($actionUpload,"Upload Files")==0) {
      $isUpload=true;
      $isChanged=true;
      $userfile = $_FILES["userfile"];
      uploadFiles($userfile,$errormsg,$theFileManager,$locale);
  }

  if (strcmp($actionNewDir,"New Folder")==0) {
    $theNewDirName = $frm["newdirname"];
    $errormsg = $theFileManager->makeDirectory($theNewDirName,$subDir);
    $isChanged=true;
  }

  if (strcmp($actionAdvance,"Advance")==0) {
    $advActionFind = !empty($frm["find"])  ? "Find"  : "";
    $advActionExport = !empty($frm["export"])  ? "Export"  : "";
    $advActionImport = !empty($frm["import"])  ? "Import"  : "";

    // Begin Find Section
    if (strcmp($advActionFind,"Find")==0) {
      $findtext = !empty($_POST["findtext"]) ? trim($_POST["findtext"]) : "";
      $findmatchcase = !empty($_POST["findmatchcase"]) ? $_POST["findmatchcase"]  : 0;
      $findexact = !empty($_POST["findexact"]) ? $_POST["findexact"]  : 0;

      if ($findmatchcase == 1) {
        $bfindmatchcase = true;
      } else {
        $bfindmatchcase = false;
      }

      if ($findexact == 1) {
       $bfindexact = true;
      } else {
       $bfindexact = false;
      }

      $theFileManager->initFind($findtext,$bfindmatchcase,$bfindexact);
      $theFileManager->processFind();
      $findResult = $theFileManager->getFindResult();
      $findResultSize = count ($findResult);

      $errormsg = "";

      if ($findResultSize > 0) {
        $errormsg .= $locale->getString(LID_TYPE_HTML,LID_HTML_LBL_FIND_TOTAL_MATCHES) . ": " . $findResultSize . "<br><br>";
        $errormsg .= "<ul>";
        for ($i=0; $i< $findResultSize; $i++) {
      	  if ($findResult[$i]["isDir"]) {
       	    $findPath = $findResult[$i]["path"];
	  } else {
	    $findPath = $findResult[$i]["path"];
	  }
          $errormsg .= "<li><a href=\"" . $PHP_SELF . "?f=" . $findPath . "\">";
          if ($findResult[$i]["isDir"])
            $errormsg .= "[Dir] - ";
          else
            $errormsg .= "[File] - ";
          if (strlen($findResult[$i]["path"]) > 0)
            $errormsg .= $findResult[$i]["path"] . "/" . $findResult[$i]["file"];
          else
            $errormsg .= $findResult[$i]["file"];
          $errormsg .= "</a></li>";
        }
        $errormsg .= "</ul>";
      } else {
        $errormsg = "No Results Found";
      }
      $menuOption = 6;
    }
    // End Find Section


    // Begin Export
    if (strcmp($advActionExport,"Export")==0) {
      $exportResult = $theFileManager->exportFile($_POST["exportfilename"],$compress_cfg);

      if ($exportResult[0]) {
        $errormsg = "Export Successfull! Right click on file and select 'save as' to download";
        $isChanged=true;
        $menuOption = 0;
      } else {
        $errormsg = "Export Failed: " . $exportResult[1];
        $menuOption = 7;
      }


    }
    // End Export Section


    // Begin Import
    if (strcmp($advActionImport,"Import")==0) {

      $isChanged=true;
      $userfile = $_FILES["userfile"];
      if (empty($userfile["name"]) || empty($userfile["tmp_name"]) ) {
        $importResult[0] = false;
        $importResult[1] = "No Import File given";
      } else {
        $importResult = $theFileManager->importFile($_POST["exportfilename"],$compress_cfg,$userfile);
      }

      if ($importResult[0]) {
        $errormsg = "Import Successfull!";
        $isChanged=true;
        $menuOption = 0;
      } else {
        $errormsg = "Import Failed: " . $importResult[1];
        $menuOption = 8;
      }
    }
    // End Export Section
  }

} else {
   //
   //  Call Appropriate Screen
   //

   if ( strcmp($actionCopy,"Copy")==0) {
     if (isLeastOneCheckBox($frm,$theFileManager->getFileCount(),$modList)) {
       $menuOption=1;
     } else {
       $errormsg = $locale->getString(LID_TYPE_MESSAGES,LID_MSG_NO_SEL_COPY_FILE_GIVEN);
     }
   } else if (strcmp($actionRename,"Rename")==0) {
     if (isLeastOneCheckBox($frm,$theFileManager->getFileCount(),$modList)) {
       $menuOption=2;
     } else {
       $errormsg = $locale->getString(LID_TYPE_MESSAGES,LID_MSG_NO_SEL_RENAME_FILE_GIVEN);
     }
   } else if (strcmp($actionDelete,"Delete")==0) {
     if (isLeastOneCheckBox($frm,$theFileManager->getFileCount(),$modList)) {
       $menuOption=3;
     } else {
       $errormsg = $locale->getString(LID_TYPE_MESSAGES,LID_MSG_NO_SEL_DEL_FILE_GIVEN);
     }
   } else if (strcmp($actionUpload,"Upload Files")==0) {
       $menuOption = 4;
   } else if (strcmp($actionNewDir,"New Folder")==0) {
       $menuOption = 5;
   } else if (strcmp($actionAdvance,"Advance")==0) {

       // Switch statement to set appropriate screen based on
       // Advance Option in the DropDown
       switch ($actionAdvanceOption) {
         case 0: $menuOption = 0; break;
         case 1: $menuOption = 6; break;  // Find File/Folder
         case 2: $menuOption = 7; break;  // Export
         case 3: $menuOption = 8; break;  // Import
         default:  $menuOption = 0; break;
       };
   }

}

if (strcmp($actionCancel,"Cancel")==0) {
  $menuOption = 0;
}

if ($isChanged) {
  $theList = $theFileManager->getDirList($subDir);
}

if ($actionSort > -1) {
  $isASC = true; // default sort ASC
  $sortdir = 0;  // default sort ASC this is for the $_GET

  if ($actionSortPrev == $actionSort) {    // check did we sort on the same column
    if (strcmp($actionSortDir,"1")==0) {   // check last sort direction and reverses it
      $isASC = true;
      $sortdir = 0;
    } else {
      $isASC = false;
      $sortdir = 1;
    }
  }

  $actionSortPrev = $actionSort;
  $sortResult = $theFileManager->sortDirList($theList,$actionSort,$isASC); // Call Sort

  if ($sortResult[0])
   $theList = $sortResult[1];
  else
    $errormsg = $sortResult[1];
} else {
  $sortdir = 0;
}



include_once("screens/_scn_filemanager.php");
?>
Return current item: PHP Web FileManager - Similiar to Yahoo!