<?php
//Herz aus Gold: die file-Klasse. Alles was mit Dateien so angestellt werden kann, wird über sie
//abgewickelt
class file
{
	var $file_path;
	var $file_name;
	var $file_size;
	var $lastchange;
	var $file_resource;
	var $extension;
	var $tmp_file;
	var $chmod;
	
	function file($file_path)
	{
			$this->file_path = $file_path;
			if(file_exists(ROOT_DIR."/".$file_path))
			{
				$this->file_name = basename($this->file_path);
				$this->file_size = filesize(ROOT_DIR."/".$this->file_path);
				$this->lastchange = filemtime(ROOT_DIR."/".$this->file_path);
				$path_info = @pathinfo(ROOT_DIR."/".$this->file_path);
				$this->extension = $path_info["extension"];
			}
	}
	function getchmod()
	{
		return @fileperms(ROOT_DIR."/".$this->file_path);	
	}
	function delete()
	{
		global $ftp_conn_id;
		if(!file_exists(ROOT_DIR."/".$this->file_path))
			return false;
		
		//history files löschen
		if(file_exists(ROOT_DIR."/".SCRIPT_PATH."/work/data/finfo_".md5($this->file_path).".php"))
		{
			$finfo = new file(SCRIPT_PATH."/work/data/finfo_".md5($this->file_path).".php");
			$finfo->delete();
		}
		if(file_exists(ROOT_DIR."/".SCRIPT_PATH."/work/data/fcomments_".md5($this->file_path).".php"))
		{
			$fcomments = new file(SCRIPT_PATH."/work/data/fcomments_".md5($this->file_path).".php");
			$fcomments->delete();
		}
		
		if(FTP_USED == 1)
			return ftp_delete($ftp_conn_id , FTP_ROOT."/".$this->file_path);	
		else
			return unlink(ROOT_DIR."/".$this->file_path);
	}
	
	function open($key)
	{
		global $ftp_conn_id;
		
		if($key == "w")
		{	
			if(!file_exists(ROOT_DIR."/".$this->file_path))
				return false;
			
			//volle Zugriffsrechte vergeben
			if (FTP_USED == 1)
			{
				$this->chmod = $this->getchmod();
				if(!@ftp_chmod($ftp_conn_id, 33206, FTP_ROOT."/".$this->file_path))
					return false;
			}
			$this->tmp_file = SCRIPT_PATH."/work/temp/".$user->user_name."_".md5($this->file_path);
			
			//Datei zum bearbeiten an einen sicheren Ort bringen -> workt/temp/. Vermeidet race-conditions
			if(FTP_USED == 1)
			{
				if(!@ftp_rename($ftp_conn_id, FTP_ROOT."/".$this->file_path, FTP_ROOT."/".$this->tmp_file))
					return false;
			}
			else
			{
				if(!rename(ROOT_DIR."/".$this->file_path,ROOT_DIR."/".$this->tmp_file))
					return false;
			}		
			if(!($this->file_resource = @fopen(ROOT_DIR."/".$this->tmp_file, $key)))
				return false;
		}
		elseif($key == "r")
		{
			if(!($this->file_resource = @fopen(ROOT_DIR."/".$this->file_path, $key)))
				return false;
		}
		return true;
	}
	
	function close()
	{
		global $ftp_conn_id;
		
		//die Datei sollte schon wieder an ihren alten Platz gebracht werden, also kein Abbruch
		//if(!fclose($this->file_resource))
			//return false;
		@fclose($this->file_resource);
		if($this->tmp_file != "")
		{	
			//Datie wieder zurückbringen ins ursprüngliche Verzeichnis
			if(FTP_USED == 1)
			{
				if(!@ftp_rename($ftp_conn_id, FTP_ROOT."/".$this->tmp_file, FTP_ROOT."/".$this->file_path))
					return false;
			}
			else
			{
				if(!@rename(ROOT_DIR."/".$this->tmp_file,ROOT_DIR."/".$this->file_path))
					return false;		
			}		
			if (FTP_USED == 1)
			{
				if(!@ftp_chmod($ftp_conn_id, $this->chmod, FTP_ROOT."/".$this->file_path))
					return false;
			}
			$this->tmp_file == "";
		}
		return true;
	}
	function getContent()
	{
		if(!($fcontent = @file_get_contents(ROOT_DIR."/".$this->file_path)))
			return false;
		else
			return $fcontent;
	}
	function copy($newpath)
	{
		global $ftp_conn_id;
		
		$newfilename = $this->file_name;
		while(file_exists(ROOT_DIR."/".$newpath.$newfilename))
			$newfilename = "Copy_of_".$newfilename;
		
		$newpath = $newpath.$newfilename;
		
		if(FTP_USED == 1)
			return @ftp_put($ftp_conn_id, FTP_ROOT."/".$newpath, ROOT_DIR."/".$this->file_path, FTP_BINARY);
		else	
			return @copy(ROOT_DIR."/".$this->file_path, ROOT_DIR."/".$newpath);
	}
	function move_to($newpath)
	{
		global $ftp_conn_id;
		
		if(FTP_USED == 1)
			return ftp_put($ftp_conn_id, FTP_ROOT."/".$newpath, ROOT_DIR."/".$this->file_path, FTP_BINARY);
		else	
			return copy(ROOT_DIR."/".$this->file_path, ROOT_DIR."/".$newpath);
	}
	function isScript()
	{
		$path_info = pathinfo(ROOT_DIR."/".$this->file_path);
		$extension = strtoupper($path_info["extension"]);
		
		return ($extension == "PHP" || $extension == "PHP3" || $extension == "PHP4" || $extension == "PHP5" || $extension == "CGI" || $extension == "PL" || $extension == "ASP" || $extension == "JSP")?true:false;
	}
	function readonly()
	{
		$extensions = explode(",",READ_ONLY);
		
		return in_array($this->extension,$extensions);
	}
	function rename($newname)
	{
		global $ftp_conn_id;
		//history files neu bennenen
		if(file_exists(ROOT_DIR."/".SCRIPT_PATH."/work/data/finfo_".md5($this->file_path).".php"))
		{
			$finfo = new file(SCRIPT_PATH."/work/data/finfo_".md5($this->file_path).".php");
			$finfo->rename(SCRIPT_PATH."/work/data/finfo_".md5($newname).".php");
		}
		if(file_exists(ROOT_DIR."/".SCRIPT_PATH."/work/data/fcomments_".md5($this->file_path).".php"))
		{
			$fcomments = new file(SCRIPT_PATH."/work/data/fcomments_".md5($this->file_path).".php");
			$fcomments->rename(SCRIPT_PATH."/work/data/fcomments_".md5($newname).".php");
		}
		
		//newname muss voller Pfad sein
		if(FTP_USED == 1)
		{
			if(!ftp_rename($ftp_conn_id, FTP_ROOT."/".$this->file_path, FTP_ROOT."/".$newname))
				return false;
		}
		else
		{
			if(!rename(ROOT_DIR."/".$this->file_path,ROOT_DIR."/".$newname))
				return false;
		}			
		$is_locked = is_locked($this->file_path);
		
		if(gettype($is_locked) != "boolean")	
		{
			unlock_file($this->file_path);
			lock_file($newname);
		}
		return true;
	}
	function symbol()
	{
		global $template;
		switch(strtoupper($this->extension))
		{
			case "JPG":	
				return "template/".$template->layoutname."/images/image.gif";
				break;
			case "GIF":
				return "template/".$template->layoutname."/images/image.gif";
				break;
			case "JPEG":
				return "template/".$template->layoutname."/images/image.gif";
			case "ICO":
				return "template/".$template->layoutname."/images/image.gif";
			case "BMP":
				return "template/".$template->layoutname."/images/image.gif";
				break;
			case "PNG":
				return "template/".$template->layoutname."/images/image.gif";
				break;
			case "PHP":
				return "template/".$template->layoutname."/images/script.gif";
				break;
			case "PHP3":
				return "template/".$template->layoutname."/images/script.gif";
				break;
			case "PHP4":
				return "template/".$template->layoutname."/images/script.gif";
				break;
			case "PHP5":
				return "template/".$template->layoutname."/images/script.gif";
				break;
			case "JS":
				return "template/".$template->layoutname."/images/script.gif";
				Break;
			case "HTML":
				return "template/".$template->layoutname."/images/html.gif";
				break;
			case "HTM":
				return "template/".$template->layoutname."/images/html.gif";
				break;
			case "TXT":
				return "template/".$template->layoutname."/images/text.gif";
				break;
			case "CSS":
				return "template/".$template->layoutname."/images/css.gif";
				break;
			case "ZIP":
				return "template/".$template->layoutname."/images/zip.gif";
				break;
			case "PDF":
				return "template/".$template->layoutname."/images/pdf.gif";
				break;
			case "MP3":
				return "template/".$template->layoutname."/images/sound.gif";
				break;
			case "WMA":
				return "template/".$template->layoutname."/images/sound.gif";
				break;
			case "WAV":
				return "template/".$template->layoutname."/images/sound.gif";
				break;
			case "MIDI":
				return "template/".$template->layoutname."/images/sound.gif";
				break;
			default:
				return "template/".$template->layoutname."/images/unknown.gif";
				
		}
	}	
}
?>