<?php
if ( $_SESSION['IsAdmin'] )
	{
		set_time_limit(0);
		ini_set("memory_limit", "128M");

		if ( isset ( $_POST['edit'] ) )
			{
			
				if ( "loeschen" == $_POST['edit'] )
					{
						unlink( $filename );
					}
				if ( "speichern" == $_POST['edit'] )
					{
						if ( '' != $_FILES['Foto']['name'] )
							{
								$uploadfile				= $uploaddir. $_FILES['Foto']['name'];
								move_uploaded_file 		( $_FILES['Foto']['tmp_name'], $uploadfile );
								$src					= imagecreatefromjpeg ( $uploadfile );
								list( $width,$height )	= getimagesize( $uploadfile );
								$newheight				= ( $height / $width ) * $newwidth;
								$tmp					= imagecreatetruecolor( $newwidth, $newheight );
								imagecopyresampled		( $tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
								if ( file_exists( $filename ) )
									{
										unlink( $filename );
									}
								if ( file_exists( $uploadfile ) )
									{
										unlink( $uploadfile );
									}
								imagejpeg				( $tmp, $filename, 80 );
								imagedestroy			($src);
								imagedestroy			($tmp);
								chmod					( $filename, 0755 );
							}
					}
				if ( "waehlen" == $_POST['edit'] )
					{
						echo "<form action=\"index.php?".$_SERVER['QUERY_STRING']."\" method=\"post\" enctype=\"multipart/form-data\">\n";
						echo "	<input type=\"hidden\" name=\"edit\" value=\"speichern\" />\n";
						if ( isset ( $_POST['ID'] ) )
							{
								echo "	<input type=\"hidden\" name=\"ID\" value=\"".$_POST['ID']."\" />\n";
							}
						echo "	<input type=\"hidden\" name=\"test\" value=\"".microtime()."\" />\n";
						echo "	<input type=\"hidden\" name=\"Filename\" value=\"".$filename."\" />\n";
						echo "	<input type=\"hidden\" name=\"action\" value=\"Foto\" />\n";
						echo "	<label>Foto auswählen</label>\n";
						echo "	<input type=\"file\" name=\"Foto\" />\n";
						echo "	<br />\n";
						echo "	<input type=\"submit\" name=\"Speichern\" value=\"Hochladen\" />\n";
						echo "	<br />\n";
						echo "</form>\n";
					} 
			}
		else
			{
				if ( "" == $filename )
					{
					}
				elseif ( file_exists( $filename ) )
					{
						echo "<p>Auf das Bild klicken, um es zu löschen! <span class=\"hinweis\">Achtung!</span> Es gibt keine weitere Abfrage mehr.</p>\n";
						echo "<form action=\"index.php?".$_SERVER['QUERY_STRING']."\" method=\"post\" enctype=\"multipart/form-data\">\n";
						echo "	<input type=\"hidden\" name=\"edit\" value=\"loeschen\" />\n";
						echo "	<input type=\"hidden\" name=\"test\" value=\"".microtime()."\" />\n";
						echo "	<input type=\"hidden\" name=\"DELname\" value=\"".$filename."\" />\n";
						if ( isset ( $_POST['ID'] ) )
							{
								echo "	<input type=\"hidden\" name=\"ID\" value=\"".$_POST['ID']."\" />\n";
							}
						echo "	<input style=\"width: auto; max-width: 750px;\" type=\"image\" src=\"".$filename."\" name=\"action\" value=\"Foto\" />\n";
						echo "	<br />\n";
						echo "</form>\n";
					}
				else
					{
						echo "<p>Auf den <span class=\"hinweis\">Platzhalter</span> klicken, um ein Bild hochzuladen.</p>\n";
						echo "<form action=\"index.php?".$_SERVER['QUERY_STRING']."\" method=\"post\" enctype=\"multipart/form-data\">\n";
						echo "	<input type=\"hidden\" name=\"width\" value=\"".$foto_width."\" />\n";
						echo "	<input type=\"hidden\" name=\"test\" value=\"".microtime()."\" />\n";
						echo "	<input type=\"hidden\" name=\"edit\" value=\"waehlen\" />\n";
						if ( isset ( $_POST['ID'] ) )
							{
								echo "	<input type=\"hidden\" name=\"ID\" value=\"".$_POST['ID']."\" />\n";
							}
						echo "	<input style=\"width: auto;\" type=\"image\" src=\"../img/no.jpg\" name=\"action\" value=\"Foto\" />\n";
						echo "	<br />\n";
						echo "</form>\n";
					}
			}
	}
?>