<?php

class dirbrowser {

    private 
$dirtobrowse;
    private 
$scandir;
    private 
$pathvar;
    private 
$path;
    private 
$urlpath;
    private 
$aantal;
    private 
$dirs;
    private 
$list '';
    private 
$status;
    private 
$error;
    private 
$ignorepath = array();
    private 
$urlbase64 false;
    private 
$url;
    private 
$linkurl;

    public function 
getStatus() {

        return 
$this->status;

    }

    public function 
setLinkURL($i) {

        
$this->linkurl $i;

    }

    private function 
ifPathVarSet() {

        if(isset(
$this->pathvar)) {

            return 
true;

        }
        else {

            return 
false;

        }

    }

    public function 
urlEncode($i) {

        
$this->urlbase64 $i;
    
    }

    private function 
setStatus($i) {

        
$this->status $i;

    }


    public function 
setIgnorePath($i) {

        
$this->ignorepath $i;

    }

    public function 
setURL($i) {

        
$this->url $i;

    }

    public function 
getError() {

        return 
$this->error;

    }

    private function 
setError($i) {

        
$this->error $i;

    }

    public function 
setDTB($dirtobrowse) {

        
$this->dirtobrowse $dirtobrowse;

    }

    public function 
setPathVar($i) {

        
$this->pathvar $i;

    }

    private function 
getDTB() {

        return 
$this->dirtobrowse;

    }

    private function 
getLinkURL() {

        return 
$this->linkurl;

    }

    private function 
genScandir($i) {

        if(
$sd scandir($i)) {

            
$this->scandir $sd;
            return 
true;

        }
        else {

            return 
false;

        }
        

    }

    private function 
validatePath() {

        if(
array_search("..",$this->dirs) !== false) {
  
              return 
false;
  
        }
        else {

            return 
true;

        }

    }

    private function 
pathExists() {

        if(
is_dir($this->path)) {
  
              return 
true;
  
        }
        else {

            return 
false;

        }

    }

    private function 
getPathVar() {

        if(
$this->urlbase64 == true) {

            return 
base64_decode($this->pathvar);

        }
        else {

            return 
$this->pathvar;

        }

    }

    private function 
genSafeVar($i) {

        if(
$this->urlbase64 == true) {

            return 
base64_encode($i);

        }
        else {

            return 
$i;

        }

    }

    private function 
bepaalPath() {

        if(
$this->ifPathVarSet()) {

            
$pathvar $this->getPathVar();
            
$this->path $this->getDTB().$pathvar;
            
$this->urlpath $pathvar;
            
$this->dirs explode("/",$this->urlpath);
            
$this->aantal count($this->dirs) - 1;
        }
        else {
            
$this->path $this->getDTB();
            
$this->urlpath '';
            
$this->dirs = array();
        }

    }

    private function 
toList($i) {

        
$this->list .= $i;

    }

    public function 
getList() {

        return 
$this->list;

    }

    public function 
genList() {

        
        
$this->bepaalPath();

        if(
$this->validatePath()) {

            if(
$this->pathExists()) {

                if(
$this->genScandir($this->path)) {

                    
$dirarray $this->scandir;

                    
$this->toList('<table>');
                    if(
$this->ifPathVarSet()) {
                        if((
$this->dirs[0] == "") && ($this->dirs[1] == "")) {
                            
$nieuwe_dir "";
                        } else if((
$this->dirs[1] != "") && ($this->dirs[2] == "")) {
                            
$nieuwe_dir "";
                        } else {  
                            for(
$i 0;$i $this->aantal;$i++) {
                                
$nieuwe_dir $nieuwe_dir."/".$this->dirs[$i];
                            }
                            
$nieuwe_dir substr($nieuwe_dir,1);
                        }  
                        if(
$this->urlpath !== "")  {
                            
$this->toList('<tr><td><a href="'.$this->url.$this->genSafeVar($nieuwe_dir).'">Bovenliggende map</a></tr></td>'); 
                        }
                    }
                    
                    foreach(
$dirarray as $itemid => $itemname) {
                        if(!
in_array($itemname,$this->ignorepath)) {
                            if(!
is_dir($this->path."/".$itemname)) {
                                
$this->toList('<tr><td><a href="'.$this->getLinkURL().$this->urlpath.'/'.$itemname.'">'.$itemname.'</a></td></tr>');
                            }
                            else {
                                
$this->toList('<tr><td><a href="'.$this->url.$this->genSafeVar($this->urlpath.'/'.$itemname).'">'.$itemname.'</a></td></tr>');
                            }
                        }
                    
                    }
                    
                    
$this->toList('</table>');
                    
$this->setStatus(true);

                }
                else {
    
                    
$this->setError("#3: Unable to scandir");
                    
$this->setStatus(false);    
    
                }

            }
            else {

                
$this->setError("#2: Directory doesn't exist");
                
$this->setStatus(false);    

            }

        }
        else {

            
$this->setError("#1: Action not allowed");
            
$this->setStatus(false);

        }
        

    }


}

?>