<?php
if(!defined('BASE_DIR')) exit;
if(!perm('mediapool'))
{
	no_perm();
	
} else {
	$mediapath = "";
	
	
	$file_allowed = array('.rar','.zip','.wav','.jpg','.gif','.png','.pdf','.avi','.swf','.txt');
	
	$GLOBALS['tmpl']->assign('file_allowed', $file_allowed);
	
	define('UPDIR', BASE_DIR . 'uploads/media');
	define('TRASHDIR', BASE_DIR . 'uploads/trash');
	if(isset($_REQUEST['dir']) && !empty($_REQUEST['dir']))
	{
		$check = @$_REQUEST['dir'];
		$check2 = @explode('/',$check);
		if((startsWith('...',$check2[1])) || startsWith('../',$check) || startsWith('/../',$check) || startsWith('http://',$check)) $_REQUEST['dir'] = '/';
	}
	
	//=======================================================
	// Bild erzeugen
	//=======================================================
	if(isset($_REQUEST['thumb']) && $_REQUEST['thumb']==1)
	{
		$bild = str_replace(array('../','..','\'','//','./'),'',$_REQUEST['bild']);
		
		switch($_REQUEST['imgtype'])
		{
			case 'gif':
				header("Content-type:image/gif"); 
				$img_method = 'imagegif'; 
				$quelle = imagecreatefromgif(UPDIR . $bild); 
			break;
			
			case 'jpg':
				header("Content-type:image/jpeg"); 
				$img_method = 'imagejpeg'; 
				$quelle = imagecreatefromjpeg(UPDIR . $bild); 
			break;
			
			case 'png':
				header("Content-type:image/png"); 
				$img_method = 'imagepng'; 
				$quelle = imagecreatefrompng(UPDIR . $bild); 
				
			break;
		}
		
		
		$w = (int)$_REQUEST['w'];
		$h = (int)$_REQUEST['h'];
		
		$im_width = imageSX($quelle);
		$im_height = imageSY($quelle);
		
		// wenn Bild breiter als 200 Pixel ist, verkleinern!
		if($w > 80)
		{
			$sz = 80;
			$factor = $sz / $im_width;
			$w = $sz;
			$h = $im_height * $factor;
		}
		
		
		$ziel = imagecreatetruecolor($w, $h);
		$ziel_bg = imagecolorallocate($ziel,255,255,255);
		imagefill($ziel , 0, 0, $ziel_bg);
		
		imagecopyresampled($ziel, $quelle, 0, 0, 0, 0, $w, $h, imagesx($quelle), imagesy($quelle));
		
		$img_method($ziel);
	
	}
	
	//=======================================================
	// Mediapool
	//=======================================================
	$_REQUEST['action'] = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	
	if ($_REQUEST['action']=="upload")
	{
		if(true)
		{
			if(!perm('mediapool_upload'))
			{
				$GLOBALS['tmpl']->display(TPL_FOLDER . 'error/no_perm.tpl');
			} else {
				$GLOBALS['tmpl']->assign('sess', SESSION);
				$GLOBALS['tmpl']->display(TPL_FOLDER . 'browser/browser_upload.tpl');
			}
		} 
		else 
		{
			echo '
			<script language="JavaScript" type="text/javascript">
			window.close();
			</script>
			';
		}
		exit;
	}
	
	if ($_REQUEST['action']=="upload2")
	{
		//=======================================================
		// multiupload
		//=======================================================
		if(perm('mediapool_upload'))
		{
			@$_REQUEST['pfad'] = str_replace('.', '',$_REQUEST['pfad']);
					
			for($i=0;$i<count($_FILES['upfile']['tmp_name']);$i++)
			{
				$d_name = strtolower(ltrim(rtrim($_FILES['upfile']['name'][$i])));
				$d_name = str_replace(" ","", $d_name);
				$d_tmp = $_FILES['upfile']['tmp_name'][$i];
				$endg = strtolower(substr($d_name, strlen($d_name) - 4));
				
				if($_FILES['upfile']['type'][$i]=="image/pjpeg" || $_FILES['upfile']['type'][$i]=="image/jpeg" || $_FILES['upfile']['type'][$i]=="image/x-png" || $_FILES['upfile']['type'][$i]=="image/png")
				{
					//=======================================================
					// rename if exists
					//=======================================================
					if(file_exists(UPDIR . $_REQUEST['pfad'] . $d_name ))
					{
						$expl = explode(".", $d_name);
						$d_name = $expl[0] . date("dhi"). "." . $expl[1];
					}
					
					
					Logger('User ' . $_SESSION['user_name'] . ' uploads new File: ' .  $d_name, '3', $_SESSION['benutzer_id']);
					@move_uploaded_file($d_tmp, UPDIR . $_REQUEST['pfad'] . $d_name);
					@chmod(UPDIR . $_REQUEST['pfad'] . $d_name, 0777);
					if (isset($_REQUEST['resize']) && $_REQUEST['resize']==1 && isset($_REQUEST['w']) && !empty($_REQUEST['w']) && $_REQUEST['w'] > 10 && $_REQUEST['w'] <= 1600)
					{
						$error = 0;
						if (function_exists("imagecreatetruecolor"))
						{
							$sowhat = "imagecreatetruecolor";
						} else {
							$sowhat = "imagecreate";
						}
						
						
						
						//$neues_bild = $sowhat($_REQUEST['w'], $_REQUEST['h']);
						
						if($_FILES['upfile']['type'][$i]=="image/pjpeg" || $_FILES['upfile']['type'][$i]=="image/jpeg")
						{
							$altes_bild = imagecreatefromjpeg(UPDIR . $_REQUEST['pfad'] . $d_name);
						}
						
						
						if($_FILES['upfile']['type'][$i]=="image/png" || $_FILES['upfile']['type'][$i]=="x/png")
						{
							$altes_bild = imagecreatefrompng(UPDIR . $_REQUEST['pfad'] . $d_name);
						}
						
						if($_FILES['upfile']['type'][$i]=="image/gif")
						{
							$altes_bild = imagecreatefromgif(UPDIR . $_REQUEST['pfad'] . $d_name);
						}
						
						
						$sz = cleanInt($_REQUEST['w']);
						$im_width = imageSX($altes_bild);
						$im_height = imageSY($altes_bild);
						
						$factor = $sz / $im_width;
						
						$size_orig = @getimagesize(UPDIR . $_REQUEST['pfad'] . $d_name);
						if(!empty($size_orig[0]) && ($size_orig[0] > $sz))
						{
							$sz = $size_orig[0];
							$im_width = $size_orig[0];
							$im_height = $size_orig[1];
							$factor = $sz / $im_width;
						}

						$w = $sz;
						$h = $im_height * $factor;
						
						$neues_bild = $sowhat($w, $h);
						
						
						if (isset($altes_bild))
						{
						 // Bild erzeugen
							imagecopyresampled($neues_bild, $altes_bild, 0, 0, 0, 0, imagesx($neues_bild), imagesy($neues_bild), imagesx($altes_bild), imagesy($altes_bild));
							
							// JPG verkleinern
							if($_FILES['upfile']['type'][$i]=="image/pjpeg" || $_FILES['upfile']['type'][$i]=="image/jpeg")
							{
								unlink(UPDIR . $_REQUEST['pfad'] . $d_name);
								imagejpeg($neues_bild, UPDIR . $_REQUEST['pfad'] . $d_name, 100);
								
							}
							// PNG verkleinern
							if($_FILES['upfile']['type'][$i]=="image/png" || $_FILES['upfile']['type'][$i]=="x/png")
							{
								
							}
							
							if($_FILES['upfile']['type'][$i]=="image/gif")
							{
								unlink(UPDIR . $_REQUEST['pfad'] . $d_name);
								imagegif($neues_bild, UPDIR . $_REQUEST['pfad'] . $d_name, 95);
							}
						}
					} 
					else
					{
						if(!empty($d_name))
						{
							// Wenn keine Bild-Datei...
							$check = strtolower(substr($d_name,-4));if(in_array($check,$file_allowed)){Logger('User ' . $_SESSION['user_name'] . ' uploads new File: ' .  $d_name, '3', $_SESSION['benutzer_id']);$d_tmp = $_FILES['upfile']['tmp_name'][$i];@move_uploaded_file($d_tmp, UPDIR . $_REQUEST['pfad'] . $d_name);@chmod(UPDIR. $_REQUEST['pfad'] . $d_name, 0777);} else {Logger('User ' . $_SESSION['user_name'] . ' uploads illegal File: ' .  $d_name . '. Deleted!', '3', $_SESSION['benutzer_id']);}
						}
					}
				} 
				else
				{
					// Wenn keine Bild-Datei... 
					if(!empty($d_name)){if(file_exists(UPDIR . $_REQUEST['pfad'] . $d_name )){$expl = @explode(".", $d_name);$d_name = $expl[0] . date("dhi"). "." . $expl[1];}$check = strtolower(substr($d_name,-4));if(in_array($check,$file_allowed)){Logger('User ' . $_SESSION['user_name'] . ' uploads new File: ' .  $d_name, '3', $_SESSION['benutzer_id']);@move_uploaded_file($d_tmp, UPDIR . $_REQUEST['pfad'] . $d_name);@chmod(UPDIR . $_REQUEST['pfad'] . $d_name, 0777);} else {Logger('User ' . $_SESSION['user_name'] . ' uploads illegal File: ' .  $d_name . '. Deleted!', '3', $_SESSION['benutzer_id']);}}
				}
			}
		}
		#header("Location:".redir());
		echo"
		<script language=\"javascript\">
		<!--
		window.opener.parent.frames['zf'].location.href = window.opener.parent.frames['zf'].location.href;
		window.close();
		//-->
		</script>
		"; 
		exit();
	}
	
	
	
	if ($_REQUEST['action']=="delfile")
	{
		if(perm('mediapool_del'))
		{
			@copy(UPDIR . $_REQUEST['file'], TRASHDIR . '/' . $_REQUEST['df'] );
			if(@unlink(UPDIR . $_REQUEST['file']))
			{
				$error = 0;
				Logger('User ' . $_SESSION['user_name'] . ' deleted an image: ' .  $_REQUEST['file'], '3', $_SESSION['benutzer_id']);
				
				$_REQUEST['file'] = "";
				$_REQUEST['action'] = "";
				echo "<script language=\"javascript\"><!--";
				echo "parent.frames['zf'].location.href='?do=browser&pop=1&typ=" . $_REQUEST['typ'] . "&dir=" . $_REQUEST['dir'] . "&cp=" . SESSION . "&done=1";
				echo "--></script>";
				$_REQUEST['action']="list";
			}
		}
		else 
		{
			echo "<script language=\"javascript\"><!--";
			echo "parent.frames['zf'].location.href='?do=browser&pop=1&typ=" . $_REQUEST['typ'] . "&dir=" . $_REQUEST['dir'] . "&cp=" . SESSION . "&done=1";
			echo "--></script>";
		}
		$_REQUEST['action']="list";
	}
	
	
	$_REQUEST['done'] = (isset($_REQUEST['done']) && $_REQUEST['done'] == 1) ? 1 : "";
	$dir = (isset($_REQUEST['dir']) && $_REQUEST['dir'] != '') ? $_REQUEST['dir'] : "";
	$dir  = (strpos($dir, "//")!==false || substr($dir,0,4)=='/../'  ) ? '' : $dir;
	
	if ($_REQUEST['action']=="list" || $_REQUEST['done']==1)
	{
		if (substr($dir,strlen($dir)-4)=="/../")
		{
			$zerlegen = explode("/", $dir);
			$myf = count($zerlegen) - 4;
			$myd = "";
			for ($i=0; $i<$myf; $i++)
			{
				if ($zerlegen[$i]!="")
				{
					$myd .= "/" . $zerlegen[$i];
				}
			}
			if (substr($myd, strlen($myd)-1)=="/")
				$dir = $myd;
			else
				$dir = $myd . "/";
		}
	
		if (!($dir=="/"))
		{
			$GLOBALS['tmpl']->assign('dir', $dir);
			$GLOBALS['tmpl']->assign('sess', SESSION);
			$GLOBALS['tmpl']->assign('dirup', 1);
		}
	
		//=======================================================
		// included directories
		// ======================================================
		if(isset($_REQUEST['newdir']) && !empty($_REQUEST['newdir']))
		{
			$_REQUEST['newdir'] = trim(strtolower(eregi_replace('[^A-Za-zÀ-ÖØ-öø-ÿ0-9_]', '', $_REQUEST['newdir'])));
			$resuld = @mkdir(UPDIR . $dir . $_REQUEST['newdir']);
		}
		$d = @dir(UPDIR . $dir);
		while (false !== ($entry = @$d->read()))
		{
			if ($entry != "." && $entry != ".." && !($entry == "koobi" && $dir == "/"))
			{
				if (is_dir(UPDIR . $mediapath. "" . $dir . $entry)) 
				{
					$elem['dir'][] = $entry;
				} else {
					$elem['file'][] = $entry;
				}
			}
		}
		$d->close();
	
		@asort($elem['dir']);
		$bfiles = array();
		while (list($key,$val) = @each($elem['dir']))
		{
			unset($row);
			$row->fileopen = $_REQUEST['typ'] . "&amp;dir=" . $dir . $val . "/&amp;action=list";
			$row->val = $val;
			array_push($bfiles,$row);
			
		}
		//=======================================================
		// sort files by alpha...
		//=======================================================
		
		@asort($elem['file']);
		$unable_delete = 0;
		$dats = array();
		
		$allowed_images =  array('.jpg','.jpe','.png','.gif');
		
		while (list($key,$val) = @each($elem['file']))
		{
			unset($row);
			$endg = strtolower(substr($val, strlen($val) - 4));
			$file_allowed = (isset($_REQUEST['typ']) && $_REQUEST['typ']=='bild') ? $allowed_images : $file_allowed;
			
			if(in_array($endg,$file_allowed))
			{
				if(startswith('.',$endg)) $endg = substr($endg,1);
		
				$endg_r = strtolower(substr($val, strlen($val) - 4));
				$end = $endg;
		
				if(isset($_REQUEST['target']) && $_REQUEST['target']=='link')
				{
					$allowed = $file_allowed;
				}
				
				$val_allowed = substr($val,-4);
				
				
				$row->gifends = (file_exists(TPL_FOLDER . "/images/mediapool/".$endg. ".gif")) ? $endg : "attach";
				$row->gifend = $row->gifends;
				$row->datsize = @round(@filesize(UPDIR . $dir . $val)/1024,2);
				$row->val = $val;
				$row->moddate = date("d.m.y, H:i", @filemtime(UPDIR . $dir . $val));
				$row->rowval = $dir . $val;
					
				if(in_array($val_allowed,$allowed_images))
				{
					
					if(function_exists("getimagesize") && function_exists("imagecreatetruecolor"))
					{
						switch($endg_r)
						{
							case '.jpg':
							case '.jpe':
								
								$img_size = getimagesize(UPDIR . $dir . $val);
								$row->bild = "<img border=\"0\" src=\"?noout=1&do=browser&pop=1&imgtype=jpg&thumb=1&w=".$img_size[0]. "&h=".$img_size[1]. "&bild=". $dir . $val. "\"><br />";
							break;
							
							case '.png':
								$img_size = getimagesize(UPDIR . $dir . $val);
								$row->bild = "<img border=\"0\" src=\"?noout=1&do=browser&pop=1&imgtype=png&thumb=1&w=".$img_size[0]. "&h=".$img_size[1]. "&bild=". $dir . $val. "\"><br />";
							break;
							
							case '.gif':
								if(function_exists("imagecreatefromgif"))
								{
									$img_size = getimagesize(UPDIR . $dir . $val);
									$row->bild = "<img border=\"0\" src=\"?noout=1&do=browser&pop=1&imgtype=gif&thumb=1&w=".$img_size[0]. "&h=".$img_size[1]. "&bild=". $dir . $val. "\"><br />";
								} else {
									$row->bild = "<img src=\"../uploads/media". $dir . $val. "\" border=\"0\" /><br />";
								}
							break;
							
						}
					} else {
						// KEINE GD!	
					}
					
				} else {
						
				}
					
				// Wenn Datei im Papierkorb liegt, löschen nicht möglich!
				$unable_delete = (strpos($dir, 'trash')!==false) ? 1 : 0;
				array_push($dats,$row);
				unset($row);
			} else {
				//@unlink(UPDIR . '/' . $val);
			}
		}
		
		
		$GLOBALS['tmpl']->assign('unable_delete',$unable_delete);
		$GLOBALS['tmpl']->assign('dats',$dats);
		$GLOBALS['tmpl']->assign('bfiles',$bfiles);
		$GLOBALS['tmpl']->assign('dir', $dir);
		
		//=======================================================
		// create new dir
		//=======================================================
		$_REQUEST['newdir'] = (isset($_REQUEST['newdir'])) ? $_REQUEST['newdir'] : "";
		if ($_REQUEST['newdir']!="" && isset($_REQUEST['newdir']))
		{
			if ($resuld)
			{
				@chmod(UPDIR . $dir . $_REQUEST['newdir'], 0777);
			} 
			else 
			{
				echo '
				<script language="JavaScript" type="text/javascript">
				alert("'.$GLOBALS['config_vars']['NewFolderInf_E'].'");
				</script>
				';
			}
		}
		//=======================================================
		// display inline content
		//=======================================================
		$GLOBALS['tmpl']->display(TPL_FOLDER . 'browser/browser.tpl');
		
		} else {
		
		//=======================================================
		// display main browser-window
		//=======================================================
		$self = substr($_SERVER['PHP_SELF'],0,-15);
		$sub_target = @explode("__", $_REQUEST['target']);
		if(is_array($sub_target)) $sub = @$sub_target[1];
	
		$GLOBALS['tmpl']->assign('target_img', $sub_target[0]);
		$GLOBALS['tmpl']->assign('pop_id', $sub);
		$GLOBALS['tmpl']->assign('koobipath', $self);
		$GLOBALS['tmpl']->assign('sess', SESSION);
		if(isset($_REQUEST['noout']) && $_REQUEST['noout'] == 1)
		{
		} else {
			$GLOBALS['tmpl']->display(TPL_FOLDER . 'browser/browser_2frames.tpl');
		}
		exit;
	}
}
?>