#native_company# #native_desc#
#native_cta#

DirFilebrowse

By mark
on July 12, 2002

Version: 0.4

Type: Full Script

Category: File Management

License: GNU General Public License

Description: Dir and Filebrowser.

showing
– file content (if permitted)
– filepermissions
– date of last access
– date of last change
– files starting with a dot (“.”)

Edits files (If this operation is permitted to your user status).

Known bug:

– open a file with the html tag “textarea” in it , then you’ll see.

This code can be used to test the servers Securitylevel as well.

<html>
<head>
 <title>DirFilebrowse v 0.3</title>
 <style>
  body{
        font-family: Verdana ;
        font-size: 11px;
        text-decoration: none;
        color:black;
  }
  .normal{
         font-family: Verdana ;
         font-size: 10px;
         text-decoration: none;
  }
 </style>
</head>
<body>

<form action="<? echo $_SERVER['PHP_SELF'];?>" method="get">
<?


/*
*  DirFilebrowse v 0.3 by Ali Chahvand <[email protected]>
*  You can use this code however you want to. The  only limitation that i set is
*  that you may not declare this script as your own work.
*  If anyone out there uses it please drop me
*  a mail , so i know someone uses my code and the time i spent on this script was
*  not waste of time.
*
* I know this code is not perfect, but i'll improve that code when i have some time.
*
* - textarea bug fix by Mark Kraus http://www.reasonmaster.net/ 
* - please include this credit with file as well
*/



// DirFilebrowse v 0.3
if(phpversion() >= "4.2.0"){  // Checking for correct PHP-Version
   $i = 1;

   function get_perms($file) { // this Function is copied from PHP.net
      $p_bin = substr(decbin(fileperms($file)), -9) ;
      $p_arr = explode(".", substr(chunk_split($p_bin, 1,"."), 0, 17)) ;
      $perms = ""; $i = 0;
      foreach ($p_arr as $this) {
         $p_char = ( $i%3==0 ? "r" : ( $i%3==1 ? "w":"x" ) );
         $perms .= ( $this=="1" ? $p_char : "-" ) . ($i%3==2 ? " " : "" );
         $i++;
      }
      return $perms;
   }

   if(!isset($_GET["opendir"])) $_GET["opendir"] = "";
   // Directoryhandling
   if (!isset($_GET["path"])){
        $strOpenDir = $_SERVER["DOCUMENT_ROOT"];
        $strPath = $_SERVER["DOCUMENT_ROOT"];
   }else{
     $strDir = $_GET["opendir"];
     $strOpenDir = $_GET["path"]."/".$strDir;
     if($strDir != "..") $strOpenDir = $_GET["path"]."/".$strDir;
     else $strOpenDir = strrev(substr(strrev($_GET["path"]),strpos(strrev($_GET["path"]),"/")+1,strlen($_GET["path"]))); // getting path of a directory above current one
     $strPath = $strOpenDir;
   }

     if(@chdir($strOpenDir) == 0) echo "No such Directory dude. (connot open : <b>$strOpenDir</b>)";

                                 echo "current Path: <code style="color:orange;">$strOpenDir</code><br>";
   if(isset($_GET['openfile']))  echo "current File: <code style="color:orange;">$_GET[openfile]</code>";

   // Reading directories and files from current dir.
   if ($handle = @opendir($strOpenDir)) {
     echo "
           <table class="normal" cellpadding="1" cellspacing="0" border="0" width="100%">
            <tr bgcolor="black" style="color:white;">
             <td>Name</td>
             <td>Permissions</td>
             <td>last changed</td>
             <td>last accessed</td>
             <td>Size</td>
            </tr>
          ";
      while (false !== ($file = readdir($handle))) {
              if($i%2 != 0) $bg = "white"; else $bg = "#FFCC99"; // Hintergrund der Zeile
              if ($file != ".") {
                 if($file != ".."){
                  $intFilefilesize = round(filesize($file)/1024,2) ;
                  $strfileperms = get_perms($file);// set file parameters
                  $intFilectime = date("d.m.y G:i:s",filectime($file)); // set last changed date
                  $intFileatime = date("d.m.y",fileatime($file)); // set last accessed date
                 }else{
                  $intFilefilesize = "" ;
                  $strfileperms = "";
                  $intFilectime = "";
                  $intFileatime = "";
                 }
                 $self = $_SERVER['PHP_SELF'];
                  if(is_file($file))
                     echo "<tr bgcolor="$bg">
                            <td>
                             <a style="color: black;text-decoration:none;" href="$self?path=$strPath&openfile=$file">$file</a>
                            </td>
                            <td>
                             $strfileperms
                            </td>
                            <td>
                             $intFilectime
                            </td>
                            <td>
                             $intFileatime
                            </td>
                            <td>
                             <i>$intFilefilesize kb</i>
                            </td>
                          </tr>";
                  else
                     echo "<tr bgcolor="$bg">
                            <td>
                             <a style="color: #999999; text-decoration:none;" href="$self?path=$strPath&opendir=$file">$file</a>
                            </td>
                            <td>
                             $strfileperms
                            </td>
                            <td>
                             $intFilectime
                            </td>
                            <td>
                             $intFileatime
                            </td>
                            <td>
                             <!-- is a dir -->
                            </td>
                          </tr>";
              }
           $i++;
      } // while
      closedir($handle);
   } // if

 // Fileviewing  / handling

  if(isset($_GET["openfile"])) $file = $_GET["openfile"];

 // Writing to file
 if(isset($_GET["save"])){
    $dat  = fopen($_GET["path"]."/".$_GET["opendir"]."/".$file,"w");
    if($data = fwrite($dat,$_GET["modifi"])){
       echo "<b>Modification written to file.</b><br><br>";
       unset($_GET["openfile"]);
    }else echo "Error while writing to file !";
 }

 // Reading the file
 if(isset($_GET["openfile"])){
    echo "<b>Filecontent</b><br><br>";
    if(is_writable($file)){
       $mod = "r+";
       $submit = "<input type="submit">";
    }else{
       $mod = "r";
       $submit = "";
    }
    $dat  = fopen($file,$mod);
    echo "<textarea name="modifi" style="color: orange;" cols="120" rows="15">";
    while(!feof($dat)){
          $data = fgets($dat,1024);
          echo htmlentities($data);
    }
    $strOpenfile = $_GET["openfile"];
    $strPath = $_GET["path"];
    $strOpenDir = $_GET["opendir"];
    echo "</textarea>
         <input type="hidden" name="save" value="true">
         <input type="hidden" name="openfile" value="$strOpenfile">
         <input type="hidden" name="path" value="$strPath">
         <input type="hidden" name="opendir" value="$strOpenDir">
         $submit
         ";
 }// isset

}else{
 $intPhpVersion = phpversion();
 echo "wrong PHP Version. Can not execute DirFilebrowse with this version (<b>PHP Version $intPhpVersion</b>)";
}
?>
</table>
</form>
</body>
</html>