<?php
/**
 * +-------------------------------------------------------------------+
 * |                        7t-File Manager                            |
 * |                                                                   |
 * | Copyright Ankur Sharma               http://www.7tech.co.in       |
 * |                                                                   |
 * +-------------------------------------------------------------------+
 * | The file is a part of 7T-FileManager plugin script.               |
 * +-------------------------------------------------------------------+
 */
class FileManagerMain
{
	private $root_path;
	private $current_working_dir;
	private $unit = array('Bytes','KB','MB','GB','TB','PB','EB');
	
	function __construct() {
		$this->current_working_dir=str_replace("\\","/",getcwd());
		if(stripos($this->current_working_dir,"/public_html/")!==false){
			$this->root_path=substr($this->current_working_dir,0,strpos($this->current_working_dir,"/public_html/")+13);
		}else if(stripos($this->current_working_dir,"/htdocs/")!==false){
			$this->root_path=substr($this->current_working_dir,0,stripos($this->current_working_dir,"/htdocs/")+8);
		}else{
			$temp = explode("/",$this->current_working_dir,4);
			array_pop($temp);
			$this->root_path=implode("/",$temp);
		}
		$this->root_path=(strrpos($this->root_path,"/")==strlen($this->root_path)-1)?$this->root_path:$this->root_path."/";
	}
	
	function get_root_path()
	{
		return $this->root_path;
	}
	
	function get_current_working_dir()
	{
		return $this->current_working_dir;
	}

	function get_dir($path="",$check_if_have_dir=0)
	{
		$path=str_replace("\\","/",$path);
		$path=(strpos($path,$this->root_path)===false)?$this->root_path.$path:$path;
		$path=(strrpos($path,"/")==strlen($path)-1)?$path:$path."/";
		$path=str_replace("//","/",$path);
		$all_files=glob($path."*",GLOB_ONLYDIR);
		$dir = array();

		$path_len=strlen($path);
		foreach($all_files as $file)
		{
			$cfile= array("name"=>substr($file,$path_len) );
			if($check_if_have_dir)
			{
				$cfile['dir']=count(glob($file."/*",GLOB_ONLYDIR));
			}
			$dir[$cfile['name']]=$cfile;
		}
		return $dir;
	}
	
	function get_left_col_dir_array()
	{		
		$dir=array();
		$working_path = $this->current_working_dir;		
		$working_path_array = explode("/","/".substr($working_path,strlen($this->root_path)));		
		$last="";
		$flag=true;
		while(count($working_path_array))
		{
			$child=$this->get_dir(implode("/",$working_path_array)."/",1);	
			if($last!="")
			{
				$child[$last]['sub_dir']=$dir;
				if($flag){
					$child[$last]['selected']=1;
					$flag=0;
				}
			}
			$dir=$child;	
			$last=array_pop($working_path_array);
		}
		return $dir;
	}
	
	function get_left_col($dir_array=null)
	{
		static $fun_count=0;
		$return="";
		if($dir_array==null)
		{
			$dir_array=$this->get_left_col_dir_array();
		}
		$fun_count++;
		$dir_count=count($dir_array);
		if($dir_count)
		{
			$i=1;
			foreach($dir_array as $child_array)
			{
				$return.='<div class="folder-wrapper level-'.$fun_count.'"><div class="folder-row-wrap">'.str_repeat('<div class="folder-spacer">&nbsp;</div>',$fun_count-1).'<a href="#" class="folder-link '.(isset($child_array['sub_dir'])?'folder-open':($child_array['dir']?'folder-close':'folder-line')).($i==$dir_count?' folder-last':'').'">&nbsp;</a>
				<div class="folder-name '.((isset($child_array['selected']) && $child_array['selected']==1)?'selected-folder':'').'">'.$child_array['name'].'</div><div class="clear"></div></div>';
				$return.='<div class="sub-folder-wrapper">';
				if(isset($child_array['sub_dir']))
				{
					$return.=$this->get_left_col($child_array['sub_dir']);
				}
				$return.='</div></div>';
				$i++;
			}
		}
		$fun_count--;
		return $return;
	}
	
	function get_left_col_sub_dir($path)
	{
		$return="";
		$fun_count=count($path);
		$path=implode('/',$path);
		$dir_array=$this->get_dir($path,1);
		$dir_count=count($dir_array);
		if($dir_count)
		{
			$i=1;
			foreach($dir_array as $child_array)
			{
				$return.= '<div class="folder-wrapper level-'.count(explode("/","/".$path)).'"><div class="folder-row-wrap">'.str_repeat('<div class="folder-spacer">&nbsp;</div>',$fun_count).'<a href="#" class="folder-link '.($child_array['dir']?'folder-close':'folder-line').($i==$dir_count?' folder-last':'').'">&nbsp;</a>
				<div class="folder-name">'.$child_array['name'].'</div><div class="clear"></div></div><div class="sub-folder-wrapper"></div></div>';
				$i++;
			}
		}
		return $return;		
	}
	
	function get_files($path="",$check_if_have_dir=0)
	{
		$path=str_replace("\\","/",$path);
		$path=(strpos($path,$this->root_path)===false)?$this->root_path.$path:$path;
		$path=(strrpos($path,"/")==strlen($path)-1)?$path:$path."/";
		$path=str_replace("//","/",$path);
		$all_files=glob($path."*",GLOB_MARK);
		$files = array();

		$path_len=strlen($path);
		$only_files=array();
		foreach($all_files as $file)
		{
			$cfile= array("name"=>substr($file,$path_len), "permission"=>decoct(fileperms($file)%01000), "size"=>$this->bytesToSize(filesize($file)), "last_mod"=>date("M d,Y g:i A", filemtime($file) ));
			if(substr($cfile['name'],-1)=="/" || substr($cfile['name'],-1)=="\\")
			{
				$cfile['name']=substr($cfile['name'],0,-1);
				$cfile['type']="folder";
				$cfile['ext']="folder";	
				$files[]=$cfile;
			}else{
				$cfile['type']="file";
				$cfile['ext']=Strtolower(pathinfo($file,PATHINFO_EXTENSION));
				$only_files[]=$cfile;
			}			
			
		}
		return array_merge($files,$only_files);
	}
	
	function get_right_col()
	{
		$dir=array();
		$return="";
		$all_files=$this->get_files($this->current_working_dir);
		foreach($all_files as $file)
		{
			$return.=$this->print_file($file);
		}
		return $return;
	}
	
	function get_right_col_sub_dir($path)
	{
		$fun_count=count($path);
		$return="";
		$path=implode('/',$path);
		$all_files=$this->get_files($path);
		//$dir_count=count($dir_array);
		foreach($all_files as $file)
		{
			$return.=$this->print_file($file);
		}	
		return $return;	
	}
	
	function print_file($file)
	{
		static $i=0;
		$i++;
		return '<div ext="'.$file['ext'].'" class="'.$file['type'].'-type ext-'.$file['ext'].' '.($i%2==0?'even':'odd').'">
			<div class="file-icon"></div>
			<div class="file-name">'.$file['name'].'</div>
			<div class="file-size">'.$file['size'].'</div>
			<div class="file-last-mod">'.$file['last_mod'].'</div>
			<div class="file-permission">'.$file['permission'].'</div>
			<div class="clear"></div>
		</div>';
	}
	
	function bytesToSize($bytes, $precision = 2)
	{	
	    return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), $precision ).' '.$this->unit[$i];
	}

	function rrmdir($dir) {
	    foreach(glob($dir . '/*') as $file) {
	        if(is_dir($file))
	            $this->rrmdir($file);
	        else
	            @unlink($file);
	    }
	   return @rmdir($dir);
	}
	
	function copy_dir( $path, $dest )
   	{
	        if( is_dir($path) )
	        {
	            @mkdir( $dest,0755);
	            $objects = scandir($path);
	            if( sizeof($objects) > 0 )
	            {
	            	$ret=true;
	                foreach( $objects as $file )
	                {
	                    if( $file == "." || $file == ".." )
	                        continue;	                    
	                        if(!$this->copy_dir( $path."/".$file, $dest."/".$file )){
	                        	$ret=false;
	                        }
	                }
	                return $ret;
	            }
	        }
	        else
	        {
	            return @copy($path, $dest);
	        }
	}
}