<?php
class dir
{
	var $path;
	var $name;
	
	function dir($path)
	{
		$this->path = $path;
		$this->name = find_file_name($this->file_path);
	}
	
	function delete()
	{
		global $user,$ftp_conn_id;
		
		$directory_list = get_subdirectories($this->path);				
		$directory_list = array_merge(array($this->path),$directory_list);
		//Alle DATEIEN löschen
		foreach($directory_list as $directory)
		{		
			$dh = opendir( ROOT_DIR."/".$directory );
			while ( gettype($file = readdir( $dh )) != "boolean")
			{
				if(is_file(ROOT_DIR."/".$directory."/".$file))
				{
					$dieing_file = new file($directory."/".$file);							
					$working_user = is_locked($dieing_file->file_path);

					if($working_user == $user->user_name || $working_user == false)
					{
						if(!$dieing_file->delete())
							message_die("Error while deleting file","I can't delete this file: ".$dieing_file->file_path);
						unlock_file($directory."/".$file);
					}
					else
						message_die("Locked file in directory","One file is locked by another User of this myFielAdmin. Stopped deleting ...");
				}
			}
			closedir($dh);
		}
		$I = count($directory_list) -1;
		for($I; $I >= 0; $I--)
		{
			if(FTP_USED == 1)
				ftp_rmdir($ftp_conn_id , FTP_ROOT."/".$directory_list[$I]);
			else
				rmdir(ROOT_DIR."/".$directory_list[$I]);
		}
	}
	
	function rename($newname)
	{
		global $verzname,$ftp_conn_id,$user;	
		$dirlist = get_subdirectories($this->path);
		$dirlist = array_merge(array($this->path),$dirlist);
		foreach($dirlist as $dir)
		{
			//$verzname heraustrennen
			$cdir = str_replace($user->user_root_dir,"",$dir);
			$cdir = str_replace($verzname,"",$cdir);
			if(substr($cdir, 0, 1) == "/") $cdir= substr($cdir, 1);
			$pos = strpos($cdir,"/");
			if($pos === false) 
				$cdir = "";
			else 
				$cdir = substr($cdir,$pos);
			
			//neuen Verzeichnisbaum erstellen
			if(FTP_USED == 1)
				ftp_mkdir($ftp_conn_id , FTP_ROOT."/".$newname.$cdir)or die("error while creating new directory");
			else	
				mkdir(ROOT_DIR."/".$newname.$cdir) or die("error while creating new directory");
			
			//Dateien kopieren
			$dh = opendir( ROOT_DIR."/".$dir);
			while ( gettype($file = readdir( $dh )) != "boolean")
			{
				if(is_file(ROOT_DIR."/".$dir."/".$file) && $file != "." && $file != "..")
				{	
						$to_rename = new file($dir."/".$file);
						$to_rename->rename($newname.$cdir."/".$file);
				}
			}	
			closedir($dh);
		}
		$this->delete();
	}
}
?>