<?php
ob_start();
require_once("init.php");
require_once("../inc/config.php");
echo "<html><head><title>";
echo $langadmin['fileinsert']."</title>";
echo "<link href=\"templates/".ADMINTHEME."/mediapool.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "</head><body>"; 
if (!permission("adminpanel"))  { echo $langadmin['noperms'] ; exit(); }
if (!permission("mediapool")) { echo $langadmin['noperms'] ; exit(); }
$mediapath = "";

if ($_REQUEST['action']=="upload") {
	?>
	
	
	
	<form action="mediapool.php?action=upload2&pfad=<?=$_REQUEST['pfad'];?>" method="post" enctype="multipart/form-data" name="upform" id="upform" style="display:inline;">
	<center>
	<table width="100%" border="0" cellpadding="2" cellspacing="0">
	<tr>
	<td align="center" valign="middle">
	<table width="100%"  border="0" cellpadding="4" cellspacing="1" class="tableborder">
	<tr>
	<th class="firstrow">
	<?=$langadmin['filemaxupload'];?>
	<?=get_cfg_var(upload_max_filesize)."B"; ?>
	</th>
	</tr>
	<tr>
	<td class="secondrow">
	<? for($a=1; $a<=5; $a++){ ?>
		<input name="upfile[]" type="file" id="upfile[]" size="40" />
	<? } ?>
	</td>
	</tr>
	<tr class="secondrow">
	<td class="firstrow">
	<table width="100%"  border="0" cellspacing="0" cellpadding="0">
	<tr>
	<td>
	<input name="resize" type="checkbox" id="resize" value="1" />
	<b>
	<?=$langadmin['warningresize2'];?>
	</b><br />
	<?=$langadmin['warningresize'];?>
	<br />
	</td>
	</tr>
	<tr>
	<td height="20"><b>
	<?=$langadmin['newsizewh'];?>
	</b>
	<input onkeypress="if(event.keyCode < 48 || event.keyCode > 57) { event.returnValue = false; } else {if(event.which < 48 || event.which > 57) return false;}" name="w" type="text" id="w" size="5" maxlength="4" />
	x
	<input onkeypress="if(event.keyCode < 48 || event.keyCode > 57) { event.returnValue = false; } else {if(event.which < 48 || event.which > 57) return false;}" name="h" type="text" id="h" size="5" maxlength="4" />
	</td>
	</tr>
	</table>
	</td>
	</tr>
	<tr class="secondrow">
	<td>
	<input type="button" class="button" onclick="this.disabled=true;this.value='Bitte warten...';document.forms['upform'].submit();" value="<?=$langadmin['upload'];?>" />
	</td>
	</tr>
	</table>
	</td>
	</tr>
	</table>
	</center>
	</form>
	<?
	echo "</body></html>";
	exit();
}

if (permission("mediapoolupload")) { 
	if ($_REQUEST['action']=="upload2") {
		
		for($i=0;$i<count($_FILES['upfile']['tmp_name']);$i++){
			$d_name = strtolower(ltrim(rtrim($_FILES['upfile']['name'][$i])));
			
			$d_name = preg_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"){
				
				move_uploaded_file($d_tmp, "../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
				@chmod("../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name, 0777);
				if (isset($_REQUEST['resize'])) {
					$error = 0;
					
					
					if (gdversion() >= 2) { 
						$sowhat = "imagecreatetruecolor";
					} else { 
						$sowhat = "imagecreate";
					}
					
					if (function_exists("imagecreate")) {
						$neues_bild = $sowhat($_REQUEST['w'], $_REQUEST['h']);
						if($_FILES['upfile']['type'][$i]=="image/pjpeg" || $_FILES['upfile']['type'][$i]=="image/jpeg"){
							$altes_bild = imagecreatefromjpeg("../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
						}
						if($_FILES['upfile']['type'][$i]=="image/png" || $_FILES['upfile']['type'][$i]=="image/x-png"){
							$altes_bild = imagecreatefrompng("../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
						}
						if($_FILES['upfile']['type'][$i]=="image/gif"){
							$error = 1;
						}
						if (isset($altes_bild)) {
							imagecopyresized($neues_bild, $altes_bild, 0, 0, 0, 0, imagesx($neues_bild), imagesy($neues_bild), imagesx($altes_bild), imagesy($altes_bild));
							if($_FILES['upfile']['type'][$i]=="image/pjpeg" || $_FILES['upfile']['type'][$i]=="image/jpeg"){
								@unlink("../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
								imagejpeg($neues_bild, "../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
							}
							if($_FILES['upfile']['type'][$i]=="image/png" || $_FILES['upfile']['type'][$i]=="image/x-png"){
								@unlink("../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
								imagepng($neues_bild, "../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
								
							}
						}
					}
					
				} else {
					$d_tmp = $_FILES['upfile']['tmp_name'];
					move_uploaded_file($d_tmp, "../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
					@chmod("../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name, 0777);	
				}
				
			} else {
				
				// Wenn keine Bild-Datei...
				move_uploaded_file($d_tmp, "../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name);
				@chmod("../uploads".$mediapath."" . $_REQUEST['pfad'] . $d_name, 0777);
			}
		}
		echo"
		<script language=\"javascript\">
		<!--
		window.opener.parent.frames['zf'].location.href = window.opener.parent.frames['zf'].location.href;
		window.close();
		//-->
		</script>
		";
		exit();
	}
}
if (permission("mediapooldel")) { 
	if ($_REQUEST['action']=="delfile") {
		if (@unlink("../uploads".$mediapath."" . $_REQUEST['file'])) {
			$error = 0;
			echo '
			<script language="javascript">
			<!--
			alert(\''.$langadmin['delok'].'\');
			//-->
			</script>
			';
			$_REQUEST['file'] = "";
			$_REQUEST['action'] = "";
			/*
			echo "<meta http-equiv=\"refresh\" content=\"0;mediapool.php?typ=".$_REQUEST['typ']."&dir".$_REQUEST['dir']."&done=1\">";
			echo "<a href=mediapool.php?typ=".$_REQUEST['typ']."&dir".$_REQUEST['dir']."&done=1>".$langadmin['redirfalse']."</a>";
			*/
			$_REQUEST['action']="list";
		}else{
		}
		$_REQUEST['action']="list";
	}
}

if ($_REQUEST['action']=="list") {
	echo "<html><head>";
	?>
	<title>
	<?=$langadmin['MPTITLE'];?>
	</title>
	<link href="templates/<?=ADMINTHEME;?>/mediapool.css" rel="stylesheet" type="text/css" />
	<? echo '</head><body  topmargin="0" leftmargin="0">' ;?>
	<table width="100%"  border="0" cellpadding="4" cellspacing="1" class="tableborder">
	<tr>
	<td  class="firstrow">&nbsp;</td>
	<td  class="firstrow">   
	<?=$langadmin['filename'];?>
	</td>
	<td  class="firstrow">   
	<?=$langadmin['filesize'] ;?>
	</td>
	<td  class="firstrow">&nbsp;</td>
	</tr>
	<? 	 	
	$dir = $_REQUEST['dir'];
	
	if (substr($dir,strlen($dir)-4)=="/../") {
		$zerlegen = explode("/", $dir);
		$myf = count($zerlegen) - 3;
		$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=="/")) {
		?>
		<tr>
		<td width="5%" class="c2">
		<div align="center"><a href="mediapool.php?typ=<?=$_REQUEST['typ'];?>&sess=<?=$sessid;?>&dir=<?=$dir . "../";?>&action=list"><img   src="templates/<?=ADMINTHEME;?>/image/folder.gif" alt="" border="0" /></a></div>
		</td>
		<td width="45%" class="secondrow">&nbsp;<a href="mediapool.php?typ=<?=$_REQUEST['typ'];?>&amp;sess=<?=$sessid;?>&amp;dir=<?=$dir . "../";?>&amp;action=list">..</a>&nbsp;&nbsp;</td>
		<td class="secondrow">&nbsp;</td>
		<td class="secondrow">&nbsp;</td>
		</tr>
		<?
	}
	
	$resuld = @mkdir("../uploads".$mediapath."" . $dir . $_REQUEST['newdir']);
	
	$d = dir("../uploads".$mediapath."" . $dir);
	while (false !== ($entry = $d->read())) {
		if ($entry != "." && $entry != ".." && !($entry == "newsletter_attachments" && $dir == "/")) {
			if (is_dir("../uploads".$mediapath."" . $dir . $entry)) {
				$elem['dir'][] = $entry;
			} else {
				$elem['file'][] = $entry;
			}
		}
	}
	$d->close();
	$dir_array = $elem['dir'];
	@asort($dir_array);
	while (list($key,$val) = @each($dir_array)) {
		if($val != "attachment"  &&  $val != 'inlineshots'){
			
			?>
			<tr>
			<td width="5%"  class="c2">
			<div align="center"><img   src="templates/<?=ADMINTHEME;?>/image/folder.gif" alt="" border="0" /></div>
			</td>
			<td  colspan="3" class="c1"><a class="media" href="mediapool.php?typ=<?=$_REQUEST['typ'];?>&amp;sess=<?=$sessid;?>&amp;dir=<?=$dir . $val . "/";?>&amp;action=list"><?=$val;?></a></td>
			
			</tr>
			<?
		}
	}
	$file_array = $elem['file'];
	@asort($file_array);
	while (list($key,$val) = @each($file_array)) {
		$endg = substr($val, strlen($val) - 4);
		
		$end = str_replace(".","",$endg);
		if(file_exists("templates/".ADMINTHEME."/image/mediapool/".$end.".gif")){
			$gifend = $end;
		}else{
			$gifend = "attach";
		}
		
		if (($_REQUEST['typ']=="bild" && ($endg==".gif" || $endg==".jpg" || $endg==".bmp" || $endg==".png")) /* || */)
		
		{
			?>
			<tr>
			<td width="5%"  class="c2">
			<div align="center"><img   src="templates/<?=ADMINTHEME;?>/image/mediapool/<?=$gifend;?>.gif" alt="" border="0" /></div>
			</td>
			<td width="45%"  class="c1"><a class="media" href="javascript:selbild('<?=$val;?>');"><?=$val;?></a>&nbsp;&nbsp;</td>
			<td  class="c2">&nbsp;
			<?=round(filesize("../uploads".$mediapath."/" . $dir . $val)/1024,2);?>
			KB</td>
			<td  class="c1">
			<div align="center">
			<? if (permission("mediapooldel")) { ?>
				<input onclick="delfile('<?=$val;?>')" class="button" type="button" value="<?=$langadmin['delonce'];?>">
			<? } ?>
			
			</div>
			</td>
			</tr>
			<?
			
		}
		if (($_REQUEST['typ']=="bild" && ($endg==".zip" || $endg==".rar" || $endg==".ace" || $endg==".tar" || $endg==".exe" || $endg==".psd" || $endg==".tif" || $endg==".wav" || $endg==".avi" || $endg==".txt" || $endg==".doc" || $endg==".pdf" || $endg==".mp3")) /* || */)
		{
			?>
			<tr>
			<td width="5%"  class="c2">
			<div align="center"><img   src="templates/<?=ADMINTHEME;?>/image/mediapool/<?=$gifend;?>.gif" alt="" border="0" /></div>
			</td>
			<td width="45%"  class="c1"><a class="media" href="javascript:selfile('<?=$val;?>');"><?=$val;?></a>&nbsp;&nbsp;</td>
			<td  class="c2">&nbsp;
			<?=round(filesize("../uploads".$mediapath."/" . $dir . $val)/1024,2);?>
			KB</td>
			<td  class="c1">
			<div align="center">
			<input onclick="delfile('<?=$val;?>')" class="button" type="button" value="<?=$langadmin['delonce'];?>">
			</div>
			</td>
			</tr>
			<?
		}
	}
	?>
	</table>
	<script language="JavaScript" type="text/javascript">
	<!--
	function selbild(src) {
		parent.frames['vs'].location.href = '../uploads<?=$mediapath;?><?=$dir;?>' + src;
		parent.document.dat.fn.value = src;
	}
	function selfile(src) {
		parent.document.dat.fn.value = src;
	}
	function delfile(src) {
		if (window.confirm('<?=$langadmin['confirmdel'];?> : ' + src + ' ')) {
			parent.frames['zf'].location.href = 'mediapool.php?typ=<?=$_REQUEST['typ'];?>&action=delfile&dir=<?=$dir;?>&file=<?=$dir;?>' + src + '&sess=<?=$sessid;?>';
		}
	}
	parent.document.dat.dateiname.value='<?=$dir;?>';
	//-->
	</script>
	<?
	if ($_REQUEST['newdir']!="" && isset($_REQUEST['newdir'])) {
		if ($resuld) {
			@chmod("../uploads".$mediapath."/" . $dir . $_REQUEST['newdir'], 0777);
			?>
			<script language="JavaScript" type="text/javascript">
			<!--
			alert('<?=$langadmin['cdok'];?>');
			//-->
			</script>
			<?
		} else {
			?>
			<script language="JavaScript" type="text/javascript">
			<!--
			alert('<?=$langadmin['cdfailed'];?>');
			//-->
			</script>
			<?			
		}
	}
	
	echo "</body></html>";
	} else {
	
	echo "<html><head>";?>
	<link href="templates/<?=ADMINTHEME;?>/mediapool.css" rel="stylesheet" type="text/css" />
	<title><?=$langadmin['imagebrowser'];?></title>
	<? echo'</head><body  topmargin="5" leftmargin="5" marginheight="5"> '; ?>
	<form name="dat" id="dat" style="display:inline;" onsubmit="return false;">
	<table  border="0" cellspacing="1" cellpadding="1">
	<tr>
	<td>
	<?=$langadmin['MPPATH'];?>
	</td>
	<td>
	<input class="feld" type="text" name="dateiname" size="63" readonly="readonly" />
	</td>
	<td>
	<input onclick="javascript:UpDir();" type="button" value="..." class="button">
	</td>
	<td>
	<? 
	if(ini_get("safe_mode") == 1) { $disbutt = "disabled=\"disabled\""; } 
	if (permission("mediapooldir")) { ?>
		<input type="button" class="button" onclick="NewFolder();" value="<?=$langadmin['cnewfolder'];?>" <?=$disbutt;?> />
	<? } if (permission("mediapoolupload")) { ?>
		<input type="button" class="button" onclick="updlg();" value="<?=$langadmin['cuploadfiles'];?>" />
	<? } ?>
	</td>
	</tr>
	</table>
	<br />
	<? if(ini_get("safe_mode") == 1) { ?>
		<div style="padding:3px;background-color:#fff;color:#999;">Achtung: Auf Ihrem Server ist SAFE_MODE aktiviert. Bitte legen Sie neue Verzeichnisse per FTP an </div>
		
	<? } if($_REQUEST['typ']=="bild"){?>
		
		<table width="100%"  border="0" cellspacing="2" cellpadding="2">
		<tr valign="top">
		<td width="60%">
		<div style="background-color:#fff;border:1px inset #666;overflow:hidden;height:420px;width:100%">
		<iframe frameborder="0" name="zf" id="zf" width="100%" height="420" scrolling="Yes" src="mediapool.php?typ=<?=$_REQUEST['typ'];?>&d4sess=<?=$sessid;?>&dir=/&action=list"></iframe>
		</div>
		</td>
		<? if($_REQUEST['target']=="url"){ ?>
			<iframe border="0px" frameborder="0" width="1" height="1" name="vs" id="vs"></iframe>
		<? } else { ?>
			<td width="40%">
			<div style="background-color:#fff; border:1px inset #666;overflow:hidden;height:420px;width:100%">
			<iframe  frameborder="0" width="99%" height="420" name="vs" id="vs"></iframe>
			</div>
			</td>
			
		<? } ?>
		</tr>
		</table>
		
		
	<? }
	if($_REQUEST['typ']=="file"){?>
		<iframe  name="zf" id="zf" width="100%" height="470" scrolling="Yes" src="mediapool.php?typ=<?=$_REQUEST['typ'];?>&sess=<?=$sessid;?>&dir=/&action=list"></iframe>
	<? } ?>
	<br />
	
	<table  border="0" cellspacing="4" cellpadding="0">
	<tr>
	<td>
	<input type="button" class="button" value="<?=$langadmin['fileinsert'];?>" onclick="submitTheForm();" />
	</td>
	<td>
	
	&raquo; <input style="background-color:transparent;border:1px solid transparent;" type="text" id="fn" size="63" readonly="readonly" /></td>
	</tr>
	</table>
	</form>
	</body>
	
	
	
	
	
	<script language="JavaScript" type="text/javascript">
	<!--
	function submitTheForm() {
		if (document.dat.fn.value == '') {
			alert('<?=$langadmin['errornoname'];?>');
		} else {
			
			
			<? 
			if($_REQUEST[mode]=="fck") { 
				if($_REQUEST[typ]!="file") {
					if($_REQUEST[target]=="txtUrl") { 
						$var = "txtUrl"; 
					} else if($_REQUEST[target]=="link") { $var = "txtUrl";  } 
					if( ($_REQUEST[target]!="link") && ($_REQUEST[target]!="url") ) {
						?>
						window.opener.document.getElementById('<?=$var;?>').src = '../uploads<?=$mediapath;?>' + document.dat.dateiname.value + document.dat.fn.value;
					<? }
				} 
				//$host = "http://" . $_SERVER['HTTP_HOST'].str_replace("/admin/mediapool.php","",$_SERVER['PHP_SELF']);
				$host = str_replace("/admin/mediapool.php","",$_SERVER['PHP_SELF']);
				$newhost = $host . "/";
				if( $_REQUEST[target]=="link") { $newhost = str_replace("http://","",$newhost); }
				?>
				window.opener.document.getElementById('<?=$var;?>').value = '<?=$newhost;?>uploads<?=$mediapath;?>' + document.dat.dateiname.value + document.dat.fn.value;
				setTimeout("self.close();", 50);	
				
				
			<? } else { 
				// UNTERSCHEIDEN ZWISCHEN NORMALEM ODER TOPNEWS- BILD
				if($_REQUEST['target'] == 'images_top') {
					$target_p = 'pic_top';
				} else {
					$target_p = 'pic';
				}
				
				// UEBERGEBEN
				if($_REQUEST[typ]!="file"){ 
					if($_REQUEST[target] != "url" && $_REQUEST[target] != "url2"  && $_REQUEST[target] != "url3"  && $_REQUEST[target] != "f_url"  && $_REQUEST[target] != "url4") { ?> 
						window.opener.document.kform['<?=$target_p;?>'].src = '../uploads<?=$mediapath;?>' + document.dat.dateiname.value + document.dat.fn.value;
						
				<? } } 
				if($_REQUEST['target']=="url"){
					$host = str_replace("/admin/mediapool.php","",$_SERVER['PHP_SELF']);
					//$host = "http://" . $_SERVER['HTTP_HOST'].str_replace("/admin/mediapool.php","",$_SERVER['PHP_SELF']);
					$host = $host . "/";
				}
				?>
				
				window.opener.document.kform['<?=$_REQUEST['target'];?>'].value = '<?=$host.$newhost;?>uploads<?=$mediapath;?>' + document.dat.dateiname.value + document.dat.fn.value;
				setTimeout("self.close();", 50);	
				
			<? } ?>
			
		}
	}
	<? if (permission("mediapooldir")) { ?>
		function UpDir() {
			parent.frames['zf'].location.href='mediapool.php?typ=<?=$_REQUEST['typ'];?>&dir=' + document.dat.dateiname.value + '../&action=list';
		}
		function NewFolder() {
			var dname = window.prompt('<?=$langadmin['errornoname'];?>', '<?=$langadmin['newdirname'];?>');
			if (dname=='' || dname==null) {
				alert('<?=$langadmin['errornoname'];?>');
			} else {
				parent.frames['zf'].location.href='mediapool.php?typ=<?=$_REQUEST['typ'];?>&dir=' + document.dat.dateiname.value + '&action=list&newdir=' + dname;
			}
		}
	<? } if (permission("mediapoolupload")) { ?>
		function updlg() {
			var url = 'mediapool.php?action=upload&pfad=' + document.dat.dateiname.value;
			var winWidth = 450;
			var winHeight = 350;
			var w = (screen.width - winWidth)/2;
			var h = (screen.height - winHeight)/2 - 60;
			var name = 'upload2mp';
			var features = 'scrollbars=no,width='+winWidth+',height='+winHeight+',top='+h+',left='+w;
			window.open(url,name,features);
		}
	<? } ?>
	//-->
	</script>
	
	
	</html>
	
	<?
}
?>