<?
session_start();
include("config.php");
if($_POST["u"]) {
if(empty($_POST["navn"]) OR empty($_POST["beskrivelse"]) OR empty($_FILES["billede"])){
header("location: galleri.php?section=upload&error=tom");
}else{
$kat = htmlspecialchars($_POST["kategori"]);
$navn = htmlspecialchars($_POST["navn"]);
$beskrivelse = htmlspecialchars($_POST["beskrivelse"]);
$dato = date("d/m/Y - H.i");

$tmp_name = $_FILES["billede"]["tmp_name"];
$name = $_FILES["billede"]["name"];
$type = $_FILES["billede"]["type"];
$size = $_FILES["billede"]["size"];

if($type != "image/gif" AND $type != "image/JPEG" AND $type != "image/jpg"){
header("location: galleri.php?section=upload&error=format");

$ny_size = filesize($size)/1024;

} elseif ($ny_size > $max) {
header("location: galleri.php?section=upload&error=forstor");

} else {
$ny_navn = time() . rand() . $name;
move_uploaded_file($tmp_name, $mappen . $ny_navn);
$navnet = "http://pulzer.dk/" . $mappen . $ny_navn;
mysql_query("INSERT INTO billeder (kategori,navn,beskrivelse,billede,dato) VALUES ('".$kat."','".$navn."','".$beskrivelse."','".$navnet."','".$dato."')") or die (mysql_error());
header("location: galleri.php?section=visgalleri&id=".$kat."");
}
}
}
if($_POST["lo"]){
if(empty($_POST["bruger"]) OR empty($_POST["pass"])) {
header("location: ?section=admin&error=tom");
}else{
if(md5($_POST["pass"]) == md5($pass) && $_POST["bruger"] == $bruger){
$_SESSION["admin"] = $bruger;
header("location: ?section=adminindex");
} else{
header("location: galleri.php");
}
}
}

if($_GET["section"] == "logud"){
unset($_SESSION["admin"]);
session_destroy();
header("location: galleri.php");
}
if($_POST["ny"]){
if(!$_SESSION["admin"]){
header("location: http://google.dk");
}else{
if(empty($_POST["navn"])){
header("location: ?section=nykategori&error=tom");
}else{
$navn = htmlspecialchars($_POST["navn"]);
mysql_query("INSERT INTO kategorier (navn) VALUES ('".$navn."')") or die (mysql_error());
header("location: galleri.php");
}
}
}
if($_GET["section"] == "sk"){
if(!$_SESSION["admin"]){
header("location: http://google.dk");
}else{
mysql_query("DELETE FROM kategorier WHERE id = '".$_GET["id"]."'") or die (mysql_error());
header("location: ?section=retkategori");
}
}
if($_POST["rk"]){
if(!$_SESSION["admin"]){
header("location: http://google.dk");
}else{
$titel = htmlspecialchars($_POST["titel"]);
mysql_query("UPDATE kategorier SET navn = '".$titel."' WHERE id = '".$_GET["id"]."'") or die (mysql_error());
header("location: galleri.php");
}
}
if($_GET["section"] == "sb"){
if(!$_SESSION["admin"]){
header("location: http://google.dk");
}else{
mysql_query("DELETE FROM billeder WHERE id = '".$_GET["id"]."'") or die (mysql_error());
header("location: ?section=sletbilleder");
}
}
?>

<html>
<head>
<title>NsZOom.dk</title>
<style type="text/css">
body {
font-family: verdana;
font-size: 12px;
color: #000;
background: #fff;
}
a:link, a:visited, a:active {
color: #000;
text-decoration: none;
}
a:hover {
color: #999999;
text-decoration: underline;
}
a.o:link, a.o:visited, a.o:active {
padding-left: 4px;
border-left: 5px solid #f2f2f2;
color: #000;
text-decoration: none;
}
a.o:hover {
padding-left: 4px;
border-left: 5px solid #a1a1a1;
color: #ee9900;
text-decoration: none;
}
.linje {
border-bottom: 1px solid #ee9900;
}
.stor {
font-size: 18px;
}
input, textarea {
padding:1px;
margin:1px;
font-family: verdana;
border: 1px solid #000;
font-size: 12px;
}
form {
display: inline;
}
select {
margin:1px;
font-family: verdana;
border: 1px solid #000;
font-size: 12px;
display: inline;
}
.pic {
border: 1px solid #ee9900;
margin: 3px;
}
</style>
</head>
</html>

<?
if($_SESSION["admin"]){
?>
<a href="?section=adminindex">Admin-Startsiden</a>
<?
}else{
?><a href="?section=admin">Admin</a>
<?
}
?>
<hr class="linje">
<?
if(!$_GET["section"]){
$kategorier = mysql_query("SELECT * FROM kategorier ORDER BY id DESC") or die (mysql_error());
if(mysql_num_rows($kategorier) == 0) {
echo "Ingen kategorier!";
} else {
while($row = mysql_fetch_array($kategorier)){
$gal = mysql_query("SELECT * FROM billeder WHERE kategori = '".$row["id"]."'") or die (mysql_error());
$antal = mysql_num_rows($gal);
echo "<a href=\"galleri.php?section=visgalleri&id=".$row["id"]."\"><b>".$row["navn"]."</b></a> (".$antal." billeder)";
echo "<hr class=\"linje\">";
}
}
}
if($_GET["section"] == "visgalleri"){
$billeder = mysql_query("SELECT * FROM billeder WHERE kategori = '".$_GET["id"]."'") or die (mysql_error());
if(mysql_num_rows($billeder) == 0) {
echo "Ingen billeder!";
} else {
while($row = mysql_fetch_array($billeder)){
echo "<a href=\"galleri.php?section=visbillede&kategori=".$row["kategori"]."&id=".$row["id"]."\"><img title=\"".$row["navn"]."\" alt=\"".$row["navn"]."\" class=\"pic\" src=\"".$row["billede"]."\" border=\"0\" width=\"75px\" height=\"75px\"></a>";
}
}
}
if($_GET["section"] == "visbillede") {
$kat = mysql_query("SELECT * FROM kategorier WHERE id = '".$_GET["kategori"]."'") or die (mysql_error());
if(mysql_num_rows($kat) == 0) {
echo "Der er sket en fejl!";
} else {
$bil = mysql_query("SELECT * FROM billeder WHERE id = '".$_GET["id"]."'") or die (mysql_error());
if(mysql_num_rows($bil) == 0) {
echo "Der er sket en fejl!";
}else{
while($row = mysql_fetch_array($bil)) {
echo "<b>".$row["navn"]."</b> (".$row["dato"].")<br>";
echo "<img class=\"pic\" src=\"".$row["billede"]."\" border=\"0\"><br>";
echo "<i>";
echo nl2br($row["beskrivelse"]);
echo "</i>";
}
}
}
}
?>


<?
if($_GET["section"] == "admin") {
?>
<?
if($_GET["error"] == "tom"){
echo "Udfyld alle felter!<br><br>";
}
?>
<form action="" method="post">
Brugernavn:<br>
<input type="text" name="bruger"><br>
Password:<br>
<input type="password" name="pass"><br>
<input type="submit" value="Login" name="lo"></form>
<?
}
?>


<?
if($_GET["section"] == "adminindex"){
if(!$_SESSION["admin"]){
echo "DOH!";
}else{
?>
<b>Admin</b><br>
<a href="?section=nykategori">Ny kategori</a><br>
<a href="?section=retkategori">Ret / slet kategori</a><br><br>
<a href="?section=upload">Nyt billede</a><br>
<a href="?section=sletbilleder">Slet billeder</a><br><br>
<a href="galleri.php?section=logud">Logud</a>
<?
}
}
?>

<?
if($_GET["section"] == "nykategori"){
if(!$_SESSION["admin"]){
echo "DOH!";
}else{
?>
<?
if($_GET["error"] == "tom"){
echo "Udfyld alle felter!<br><br>";
}
?>
<form action="" method="post">
Titel:<br>
<input type="text" name="navn"><br>
<input type="submit" value="Ny kategori" name="ny"></form>
<?
}
}
?>

<?
if($_GET["section"] == "retkategori"){
if(!$_SESSION["admin"]){
echo "DOH!";
}else{
$kategorier = mysql_query("SELECT * FROM kategorier ORDER BY id DESC") or die(mysql_error());
if(mysql_num_rows($kategorier) == 0){
echo "Ingen kategorier!";
}else{
while($row=mysql_fetch_array($kategorier)){
echo "<b>".$row["navn"]."</b> <a href=\"?section=sk&id=".$row["id"]."\">slet</a> | <a href=\"?section=rk&id=".$row["id"]."\">ret</a><br>";
}
}
}
}
?>
<?
if($_GET["section"] == "rk"){
if(!$_SESSION["admin"]){
echo "DOH!";
}else{
$ret = mysql_query("SELECT * FROM kategorier WHERE id = '".$_GET["id"]."'") or die (mysql_error());
if(mysql_num_rows($ret) == 0) {
echo "Der er sket en fejl!";
}else{
$row = mysql_fetch_array($ret);
?>
<form action="" method="post">
Titel:<br>
<input type="text" name="titel" value="<?=$row["navn"];?>"><br>
<input type="submit" value="Ret kategori" name="rk"></form>
<?
}
}
}
?>

<?
if($_GET["section"] == "sletbilleder"){
if(!$_SESSION["admin"]){
echo "DOH!";
}else{
$billeder = mysql_query("SELECT * FROM billeder ORDER BY id DESC") or die (mysql_error());
if(mysql_num_rows($billeder) == 0) {
echo "Ingen billeder!";
}else{
while($row=mysql_fetch_array($billeder)){
echo "<b>".$row["navn"]."</b> | <a href=\"?section=sb&id=".$row["id"]."\">slet</a><br>";
echo "<img title=\"".$row["navn"]."\" alt=\"".$row["navn"]."\" class=\"pic\" src=\"".$row["billede"]."\" border=\"0\" width=\"75px\" height=\"75px\">";
echo "<hr class=\"linje\">";
}
}
}
}
?>

<?
if($_GET["section"] == "upload"){
if(!$_SESSION["admin"]){
echo "DOH!";
}else{
$tjek = mysql_query("SELECT * FROM kategorier") or die (mysql_error());
if(mysql_num_rows($tjek) == 0){
echo "Du kan ikke uploade uden kategorier!";
}else{
?>
<form action="galleri.php" method="post" enctype="multipart/form-data">
<?
if($_GET["error"] == "tom") {
echo "Udfyld alle felter!<br><br>";
} elseif($_GET["error"] == "format") {
echo "Forkert format!<br><br>";
} elseif($_GET["error"] == "forstor") {
echo "Billedet er for stort!<br><br>";
}
?>
<select name="kategori">
<?
$k = mysql_query("SELECT * FROM kategorier ORDER BY id DESC") or die (mysql_error());
if(mysql_num_rows($k) == 0) {
echo "Ingen kategorier, derfor kan du ikke uploade!";
} else {
while($row = mysql_fetch_array($k)){
echo "<option value=\"".$row["id"]."\">".$row["navn"]."</option>";
}
?>
</select>
<br>
Navn:<br>
<input type="text" name="navn"><br>
Beskrivelse:<br>
<textarea name="beskrivelse"></textarea><br>
Billede:<br>
<input type="file" name="billede"><br>
<input type="submit" value="Upload!" name="u"></form>
<?
}
}
}
}
?>