php code for a tree class file viewer

<?php

class TreeIt{
  var 
$exclude_files =     array(    "TreeIt.class.php",
                                  
"index.php");
  var 
$exclude_dirs =     array(    "ico");
  var 
$cache_array =     array();
  var 
$cache_output =     array();
  var 
$tree =             array();
  var 
$settings =         array(    "ico"    =>         true,
                                  
"icofolder" =>    "ico/",
                                
"JS"    =>         true);
  var 
$icons =             array(
                                
"png"     => "picture.png",
                                
"jpg"     => "picture.png",
                                
"jpeg"     => "picture.png"
                                
"gif"     => "gif.png",
                                
"bmp"     => "bmp.png",
                                
"zip"     => "zip.png",
                                
"rar"     => "rar.png",
                                
"7z"     => "rar.png",
                                
"gz"     => "rar.png",
                                
"bz"     => "rar.png",
                                
"ace"     => "rar.png",
                                
"uha"     => "rar.png",
                                
"bat"     => "cmd.png",
                                
"com"     => "cmd.png",
                                
"exe"     => "exe.png",
                                
"setup" => "setup.png",
                                
"msi"     => "setup.png",
                                
"iso"     => "cd.png",
                                
"bin"     => "cd.png",
                                
"txt"     => "text.png",
                                
"js"     => "js.png",
                                
"htm"     => "html.png",
                                
"html"     => "html.png",
                                
"shtml" => "html.png",
                                
"php"     => "php.png",
                                
"tpl"     => "tpl.png",
                                
"cfg"     => "cfg.png",
                                
"fla"     => "fla.png",
                                
"swf"     => "swf.png",
                                
"pps"     => "pps.png",
                                
"xls"     => "excel.png",
                                
"doc"     => "doc.png",
                                
"sig"     => "sig.png",
                                
"asc"     => "sig.png",
                                
"fh10"     => "fh10.png",
                                
"pdf"     => "pdf.png",
                                
"psd"     => "psd.png",
                                
"rm"     => "real.png",
                                
"ram"     => "real.png",
                                
"mpg"     => "video.png",
                                
"mpeg"     => "video.png",
                                
"mov"     => "video.png",
                                
"avi"     => "video.png",
                                
"wmv"     => "video.png",
                                
"eps"     => "eps.gif",
                                
"pls"     => "m3u.png",
                                
"m3u"     => "m3u.png",
                                
"mp3"     => "music.png",
                                
"wma"     => "music.png");

  
//-->> save setting
  
function set($setting$value){
    if(!empty(
$setting)){
      
$this->settings[$setting] = $value;
      return 
true;
    }else{
      return 
false;
    }
  }  
  
  
//-->> JavaScript (perform function in <head>)
  
function javascript(){
    return 
"<script type=\"text/javascript\" language=\"javascript\">
function openclose(container){
  if(document.getElementById(container).style.display=='none'){
    document.getElementById(container+'_ico').src='"
.$this->settings['icofolder']."folder_opened.png';
    document.getElementById(container).style.display='block';
  }else{
    document.getElementById(container+'_ico').src='"
.$this->settings['icofolder']."folder_closed.png';
    document.getElementById(container).style.display='none';
  }
}
</script>"
;
  }
  
  
//-->> read the directory
  
function read($folder){
    if(
$folder != "." && $folder != ".." && $folder != "./" && !strpos($folder"../")){
      while(
strpos($folder"//")){
        
$folder str_replace("//""/"$folder);
      }
      
$folder trim($folder"./");
    }
    
$folder str_replace("./""."$folder);;
    
$tree = array();
    
$tmpdirs = array();
    
$tmpfiles = array();
    if(@
is_dir($folder)){
      
$handle opendir($folder);
      while(
$file readdir($handle)){
        if(
$this->allowed_dir($folder."/".$file)){
          
$tmpdirs[] = $file;
        }elseif(
$this->allowed_file($folder."/".$file)){
          
$tmpfiles[] = $file;
        }
      }
      
sort($tmpdirs);
      
sort($tmpfiles);

      foreach(
$tmpdirs as $tmpdir)
        
$tree[$tmpdir] = $this->read($folder."/".$tmpdir);
      
$tree array_merge($tree$tmpfiles);
      return array(    
">PATH"    => $folder,
                      
">TREE"    => $tree);
    }else{
      return 
false;
    }
  }

  
//-->> build the html-tree
  
function build($structure$path ""$offset 0$depth 0){
    
$offset = ($offset 0) ? 16 0;
    if(
array_key_exists(">PATH"$structure)){
      
$path $structure['>PATH'];
    }
    if(
$path != "")
      
$path $path."/";
    if(
array_key_exists(">TREE"$structure)){
      
$tree $structure['>TREE'];
    }
    if(
is_array($tree)){
      
$result "\n";
      
$result .= str_repeat("\t"$depth);
      
$result .= "<!--content start '".$path."'-->\n";
      
$i 1;
      foreach(
$tree as $key => $value){
        if(
$i count($tree)){
          
$spacer "nl.gif";
          
$background " background: url(".$this->settings['icofolder']."d.gif); background-repeat: repeat-y;";
        }else{
          
$spacer "l.gif";
          
$background "";
        }
        
$i++;
        if(
is_array($value)){
          if(
$this->settings['JS']){
            
$rand rand(1000000099999999);
            
$js " style=\"cursor: pointer;\" onclick=\"openclose('".$rand."');\"";
            
$ico " id=\"".$rand."_ico\"";
            
$div " style=\"display: block;\" id=\"".$rand."\"";
          }else{
            
$rand $js $ico $div "";
          }
          
$result .= str_repeat("\t"$depth);
          
$result .= "<!--folder start '".$path.$key."'-->\n";
          
$result .= str_repeat("\t"$depth);
          
$result .= "<div style=\"margin-left:".$offset."px;".$background."\">\n";
          
$result .= str_repeat("\t"$depth+1);
          
$result .= "<div>\n";
          
$result .= str_repeat("\t"$depth+2);
          
$result .= "<img src=\"".$this->settings['icofolder'].$spacer."\" align=\"absmiddle\" alt=\"\" width=\"20\" /><span".$js."><img src=\"".$this->settings['icofolder']."folder_opened.png\" align=\"absmiddle\" border=\"0\"".$ico." /><span style=\"font-weight: bold;\">".htmlentities($keyENT_QUOTES)."</span></span>\n";
          
$result .= str_repeat("\t"$depth+1);
          
$result .= "</div>\n";
          
$result .= str_repeat("\t"$depth+1);
          
$result .= "<div".$div.">";
          
$result .= $this->build($value$path.$key1$depth+2);
          
$result .= str_repeat("\t"$depth+1);
          
$result .= "</div>\n";
          
$result .= str_repeat("\t"$depth);
          
$result .= "</div>\n";
          
$result .= str_repeat("\t"$depth);
          
$result .= "<!--folder end '".$path.$key."'-->\n";
        }else{
          if(
$this->settings['ico']){
            
$extension $this->get_extension($value);
            if(
array_key_exists($extension$this->icons)){
              
$icon "<img src=\"".$this->settings['icofolder'].$this->icons[$extension]."\" alt=\"".$extension."\" border=\"0\" />";
            }else{
              
$icon "<img src=\"".$this->settings['icofolder']."unknown.png\" alt=\"".$extension."\" border=\"0\" />";
            }
          }else{
            
$icon "";
          }
          
$result .= str_repeat("\t"$depth);
          
$result .= "<!--item start-->\n";
          
$result .= str_repeat("\t"$depth);
          
$result .= "<div style=\"margin-left:".$offset."px;\">\n";
          
$result .= str_repeat("\t"$depth+1);
          
$result .= "<img src=\"".$this->settings['icofolder'].$spacer."\" align=\"top\" alt=\"\" width=\"20\" /><a href=\"".$path.$value."\" target='frame2' onclick=\"callFunction();\">".$icon." ".htmlentities($valueENT_QUOTES)."</a>\n";
          
$result .= str_repeat("\t"$depth);
          
$result .= "</div>\n";
          
$result .= str_repeat("\t"$depth);
          
$result .= "<!--item end-->\n";
        }
      }
      
$result .= str_repeat("\t"$depth);
      
$result .= "<!--content end '".$path."'-->\n";
      return 
$result;
    }
  }

  
//-->> read the directory and cache it in an array for further use
  
function cache_array($path ""$key ""){
    if(
$path == "." || $path == ".."){
      
$path .= "/";
    }
    if(
$this->allowed_dir($path)){
      if(!empty(
$key)){
        
$this->cached_array[$key] = $this->read($path);
      }else{
        
$this->cached_array[$path] = $this->read($path);
      }
    }else{
      return 
false;
    }
  }

  
//-->> read the directory, build the html-tree and cache it in an array for further use
  
function cache_output($path ""$key ""){
    if(
$path == "." || $path == ".."){
      
$path .= "/";
    }
    if(
$this->allowed_dir($path)){
      if(!empty(
$key))
        
$this->cached_output[$key] = $this->build($this->read($path));
      else
        
$this->cached_output[$path] = $this->build($this->read($path));
    }else{
      return 
false;
    }
  }
  
  function 
allowed_dir($path ""){
    
$dir $this->get_dir($path);
    if(@
is_dir($path) && $dir != "." && $dir != ".." && !$this->in_array_regex($path$this->exclude_dirs))
      return 
true;
    else
      return 
false;
  }
  
  function 
allowed_file($path ""){
    if(@
is_file($path) && !$this->in_array_regex($path$this->exclude_files))
      return 
true;
    else
      return 
false;  
  }

  function 
get_dir($path){
    
$array explode("/"$path);
    
$retval strtolower(array_pop($array));
    return 
$retval;
  }

  function 
get_extension($file){
    
$array explode("."$file);
    
$retval strtolower(array_pop($array));
    return 
$retval;
  }

  function 
in_array_regex($string$array){
    
reset($array);
    foreach(
$array as $value){
      if((
strpos($value'*') === 0) || (strpos($value'?') === 0))
        
$value ".".$value;
      if(
preg_match("/".$value."/i"$string))
        return 
true;
    }
    return 
false;
  }
}
?>

<script type="text/javascript"> 
/* 
   _ 
  | | 
  | | 
  | | 
  |_| 
  (_)

Visit me here

Root http://ger23.free.fr
Main http://ger23.free.fr/main

Youtube http://www.youtube.com/user/ger23z/videos
Facebook http://www.facebook.com/pages/Cours-de-fac/274012045982052
Twitter http://twitter.com/#!/coursdefac
Google plus https://plus.google.com/108155428330026323332#108155428330026323332/posts

*/
</script>









Sitemap    Cours de fac
Home        Faq
● Intro           ● Recettes
● Video         ● Tutoriaux
● Streaming     ● Liens



RSS Feed Youtube Twitter ger23 Facebook G+