<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
class Fileupload
{
	var $_allowed = array('image/pjpeg','image/jpeg','image/x-png','image/png','image/gif');
	
	function fileupload($updir,$fhidden='',$divid,$resize,$ending='',$files='')
	{
		$_CREATOR = '';
		
		if($ending!='')
		{
			switch($ending)
			{
				case 'png':
					$this->_allowed = array('image/x-png','image/png');
				break;
			}
		}
		
		if($files==1)
		{
			 $this->_allowed = 'all';
		}
		
		
		
		$error = "";
		$msg = "";
		
		$fileElementName = 'fileToUpload_' . $divid;
		
		if(!empty($_FILES[$fileElementName]['error']))
		{
			switch($_FILES[$fileElementName]['error'])
			{
	
				case '1':$error = $GLOBALS['config_vars']['Upload_ToBig1'];break;
				case '2':$error = $GLOBALS['config_vars']['Upload_ToBig2'];break;
				case '3':$error = $GLOBALS['config_vars']['Upload_The uploaded file was only partially uploaded'];break;
				case '4':$error = $GLOBALS['config_vars']['Upload_NoFile'];break;
				case '6':$error = $GLOBALS['config_vars']['Upload_Missing a temporary folder'];break;
				case '7':$error = $GLOBALS['config_vars']['Upload_Failed to write file to disk'];break;
				case '8':$error = $GLOBALS['config_vars']['Uplaod_File upload stopped by extension'];break;
				case '999':default:$error = 'No error code avaiable';break;
			}
		} elseif(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none') {
			$error = $GLOBALS['config_vars']['Upload_NoFile'];
		} else {
			switch($_FILES[$fileElementName]['type'])
			{
				case 'image/pjpeg' :
				case 'image/jpeg' :
					$new_name = md5(time() . mt_rand(10001, 19999) . $_FILES[$fileElementName]['tmp_name']) . '.jpg';
				break;
				
				case 'image/x-png' :
				case 'image/png' :
					$new_name = md5(time() . mt_rand(10001, 19999) . $_FILES[$fileElementName]['tmp_name']) . '.png';
				break;
				
				case 'image/gif' :
					$new_name = md5(time() . mt_rand(10001, 19999) . $_FILES[$fileElementName]['tmp_name']) . '.gif';
				break;
				
				default:
				case '' :
					$new_name = $_FILES[$fileElementName]['name'];
				break;
			}
			
			if($files==1) $new_name = $_FILES[$fileElementName]['name'];
			
			
			$f_check     = trim(strtolower($_FILES[$fileElementName]['name']));
			$forbidden_1 = strtolower(substr($f_check, strlen($f_check) - 4));
			$forbidden_2 = strtolower(substr($f_check, strlen($f_check) - 5));
			
			
			if($new_name!='.htaccess' &&  (($this->_allowed=='all') || (in_array($_FILES[$fileElementName]['type'], $this->_allowed)) && ($forbidden_1 != 'jpeg' && $forbidden_2 != 'pjpeg')))
			{
				$msg .= $GLOBALS['config_vars']['Upload_Succes'] . '(' . $_FILES[$fileElementName]['name'] . ') ';
				@move_uploaded_file($_FILES[$fileElementName]['tmp_name'], BASE_DIR . $updir . $new_name);
				
				switch($_FILES[$fileElementName]['type'])
				{
					case 'image/pjpeg': case 'image/jpeg':
						$_CREATOR = 'imagecreatefromjpeg';
						$_OUTPUT  = 'imagejpeg';
						$end = '.jpg';
					break;
					
					case 'image/x-png': case 'image/png':
						$_CREATOR = 'imagecreatefrompng';
						$_OUTPUT  = 'imagepng';
						$end = '.png';
					break;
					
					case 'image/x-gif': case 'image/gif':
						$_CREATOR = 'imagecreatefromgif';
						$_OUTPUT  = 'imagegif';
						$end = '.gif';
					break;
				}
				
				if($_CREATOR!='')
				{
					if(!@$_CREATOR(BASE_DIR . $updir . $new_name))
					{
						@unlink(BASE_DIR . $updir . $new_name);
						echo "{";
						echo				"error: '" . $GLOBALS['config_vars']['Global_Upload_FileError'] . "'";
						echo "}";
						exit;
					}
				}
				
				if($resize!='0')
				{
					$sz         = ($resize>200) ? 200 : $resize;
					$altes_bild = $_CREATOR(BASE_DIR . $updir . $new_name);
					$quelle     = $altes_bild;
					
					$im_width  = imageSX($altes_bild);
					$im_height = imageSY($altes_bild);
					$factor    = $sz / $im_width;
					$w         = $sz;
					$h         = $im_height * $factor;
					
					if($im_width > $sz)
					{
						$neues_bild = imagecreatetruecolor($w, $h);
						//
						$ziel = imagecreatetruecolor($w, $h);
						if($end=='.png' && function_exists('imagesavealpha'))
						{
							imagealphablending($ziel, false);
							$colorTransparent = imagecolorallocatealpha($ziel, 0, 0, 0, 127);
							imagefill($ziel, 0, 0, $colorTransparent);
							imagesavealpha($ziel, true);
						} elseif($end=='.gif') {
							$trnprt_indx = imagecolortransparent($quelle);
							if ($trnprt_indx >= 0)
							{
								$trnprt_color = imagecolorsforindex($quelle, $trnprt_indx);
								$trnprt_indx = imagecolorallocate($ziel, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
								imagefill($ziel, 0, 0, $trnprt_indx);
								imagecolortransparent($ziel, $trnprt_indx);
							}
						} else {
							$ziel_bg = imagecolorallocate($ziel,255,255,255);
							imagefill($ziel , 0, 0, $ziel_bg);
						}
						
						//
						
						imagecopyresampled($ziel, $altes_bild, 0, 0, 0, 0, imagesx($ziel), imagesy($ziel), imagesx($altes_bild), imagesy($altes_bild));
						@unlink(BASE_DIR . $updir . $new_name);
						$_OUTPUT($ziel, BASE_DIR . $updir . $new_name);
					}
				}
						
			} else {
				$error = $GLOBALS['config_vars']['Upload_ForbiddenFile'];
			}
		}
		 
		
		define('AJAX_OUTPUT',1);
		if($files!=1)
		{
			echo "{";
			echo				"error: '" . $error . "',\n";
			echo				"msg: '<img src=".HOME_PATH.@$updir."/" . @$new_name . " />',\n";
			echo				"html: '" . @$new_name . "'\n";
			echo "}";
		} else {
			echo "{";
			echo				"error: '" . $error . "',\n";
			echo				"msg: 'Upload erfolgreich: <b style=color:red>" . @$new_name . "</b>',\n";
			echo				"html: '" . @$new_name . "'\n";
			echo "}";
		}
		
	}
}
?>