--- a
+++ b/omgm/admin.php
@@ -0,0 +1,278 @@
+<?php
+include_once "define.php";
+include "db.php";
+
+$db = new Db();
+
+?>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+    <head>
+        <title>OMGM Admin</title>
+        <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
+        <link href="resources/main.css" rel="stylesheet" type="text/css" />
+
+    </head>
+    <body>
+<div id="navigation">
+<?
+	include "menu.inc";
+?>
+</div>
+        <h1>Administration</h1>
+
+<?php
+getZoomLevels();
+if( $_POST["deleteZoom"] ){
+	if( strcmp($_POST["password"], ADMINPASSWORD) != 0)
+		echo '<font color="red">Invalid password</font>';
+	else {
+  echo "<p><textarea cols=\"50\" rows=\"5\">";
+
+  $zoom = $_POST["zoom"];
+  if( strcmp("all", $zoom) == 0){
+    deleteFilesInDir("tilesdir/");
+    $db->deleteAllTiles();
+  }
+  else{
+    deleteFilesInDir("tilesdir/".$zoom."/");
+    $db->deleteTilesAtZoom($zoom);
+  }
+  echo "</textarea></p>";
+	}
+}
+if( $_POST["deleteMap"] ){
+	if( strcmp($_POST["password"], ADMINPASSWORD) != 0)
+		echo '<font color="red">Invalid password</font>';
+	else {
+  
+  if(is_array($_POST["mapid"])){
+    foreach($_POST["mapid"] as $mapId){
+      deleteMap($mapId);
+    }
+  }
+  else{
+    echo "<p class=\"info\">No map(s) selected</p>"; 
+  }
+ 	}
+}
+
+$maps = $db->getMaps();
+
+$db->setNumTiles($maps);
+
+echo "<form action=\"admin.php\" method=\"post\"><table class=\"horizontaldata\">";
+echo "<tr><th></th><th>Name</th><th>Epsg</th><th>Tiles</th><th>Contact 
+name</th><th>Contact 
+info</th><th>Added</th><th>IP</th><th>Host</th></tr>";
+
+foreach($maps as $map){  
+  echo "<tr>";
+  echo "<td><input type=\"checkbox\" name=\"mapid[]\" value=\"".$map["id"]."\" /></td>";
+  echo "<td>".$map["name"]."</td>";
+  echo "<td>".$map["epsg"]."</td>";
+  echo "<td>".$map["totalTiles"]."</td>";  
+  echo "<td>".$map["contactName"]."</td>";
+  echo "<td>".$map["contactInfo"]."</td>";
+  echo "<td>".$map["addedDate"]."</td>";
+  echo "<td>".$map["addedIp"]."</td>";
+  echo "<td>".gethostbyaddr($map["addedIp"])."</td>";
+  echo "</tr>";
+}
+?>
+</table>
+<p>
+Password: <input type="password" name="password" />
+<input type="submit" name="deleteMap" value="Delete selected map(s)" />
+</p>
+</form>
+
+<form action="admin.php" method="post">
+<p>
+Password: <input type="password" name="password" />
+  Delete tiles at zoom level: 
+<select name="zoom">
+  <option value="all">All</option>
+<?php
+  $zoomLevels = getZoomLevels();
+  //foreach($zoomLevels as $level){
+  for($level=6;$level<16;$level++){
+    echo "<option value=\"$level\">$level</option>\n";
+  }
+?>
+</select>
+<input type="submit" name="deleteZoom" value="Delete" />
+</p>
+</form>
+    </body>
+</html>
+
+<?php
+
+function deleteMap($mapId){
+  global $db;
+  // ta bort tiles, karta och filer. om en thar slagits ihop ska detta justeras
+
+  $tiles = $db->getTiles($mapId);
+
+  foreach($tiles as $tile){
+     deleteTileFile($tile);
+  }
+  $db->deleteTilesByMap($mapId);
+  $db->deleteMap($mapId);
+}
+
+function deleteTileFile($tile){
+  global $db;
+  if( $tile["duplicate"] ){
+    $mergedTiles = $db->getTilesByName($tile["tileX"], $tile["tileY"]);
+    unlink( createFileName($tile, $tile["mapId"]) );
+    unlink( createFileName($tile, false) );
+
+    recreateTileFile($tile, $mergedTiles);   
+
+    if( count($mergedTiles) == 2){
+       // if count was 2 it should soon be 1 so the remainder is not a duplicate
+
+       $reminder = $mergedTiles[0];
+
+       if( $reminder["mapId"] == $tile["mapId"] ){
+         $reminder = $mergedTiles[1];
+       }
+
+       $db->setUnmerged($reminder["mapId"], $reminder["tileX"], $reminder["tileY"]);
+
+    }
+  }
+  else{
+    unlink( createFileName($tile, false));
+  }
+}
+
+function recreateTileFile($excludeTile, $mergedTiles){
+  $destIm = false;
+  $im = false;
+  foreach($mergedTiles as $tile){
+    if( $tile["mapId"] != $excludeTile["mapId"]){
+
+      $n = createFileName($tile, $tile["mapId"]);
+
+      if( $destIm ){
+        $im = imagecreatefrompng($n);
+        imagecopymerge_alpha($destIm, $im, 0,0, 0,0, 256,256,100);
+      }
+      else{
+        $destIm = imagecreatefrompng( $n );
+      }
+    }
+    if($im){
+      imagedestroy($im);
+    }
+  }
+
+  $f = createFileName($excludeTile, false);
+  imagepng($destIm, $f);
+  imagedestroy($destIm);
+}
+
+function createFileName($tile, $mapId){
+  if($mapId){
+    return "tilesdir/".$tile["zoom"]."/x".$tile["tileX"]."_y".$tile["tileY"].".png_".$mapId;
+  }
+  else{
+    return "tilesdir/".$tile["zoom"]."/x".$tile["tileX"]."_y".$tile["tileY"].".png";
+  }
+}
+
+function getZoomLevels(){
+  $baseDir = "tilesdir/";
+  $dirs = scandir($baseDir);
+  $zoomLevels = array();
+   foreach($dirs as $dir){
+     if( !(strcmp(".", $dir) == 0 || strcmp("..", $dir) == 0) ){
+       if(dirContainFiles($baseDir.$dir)){
+         $zoomLevels[] = $dir;
+       }
+     }
+   }
+  sort($zoomLevels);
+  return $zoomLevels;
+}
+
+function dirContainFiles($dir){
+  if(is_dir($dir)){
+    $content = scandir($dir);
+    if( count($content) > 2 ){
+      return true;
+    }
+  }
+  return false;
+}
+
+function deleteFilesInDir($dir){
+   echo "** Delete files in dir ".$dir."\n";
+
+   $files = scandir($dir);
+
+   foreach($files as $file){
+     if( !(strcmp(".", $file) == 0 || strcmp("..", $file) == 0) ){
+       $path = $dir.$file;
+       if( is_dir($path) ){
+         echo "Is dir: ".$path."\n";
+         deleteFilesInDir($path."/");
+         echo "Delete dir: ".$path."\n";
+         rmdir($path);
+       }
+       else{
+         echo "Is file: ".$path."\n";
+         unlink($path);
+       }
+     }
+   }
+}
+
+function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
+    if(!isset($pct)){
+        return false;
+    }
+    $pct /= 100;
+    // Get image width and height
+    $w = imagesx( $src_im );
+    $h = imagesy( $src_im );
+    // Turn alpha blending off
+    imagealphablending( $src_im, false );
+    // Find the most opaque pixel in the image (the one with the smallest alpha value)
+    $minalpha = 127;
+    for( $x = 0; $x < $w; $x++ )
+    for( $y = 0; $y < $h; $y++ ){
+        $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF;
+        if( $alpha < $minalpha ){
+            $minalpha = $alpha;
+        }
+    }
+    //loop through image pixels and modify alpha for each
+    for( $x = 0; $x < $w; $x++ ){
+        for( $y = 0; $y < $h; $y++ ){
+            //get current alpha value (represents the TANSPARENCY!)
+            $colorxy = imagecolorat( $src_im, $x, $y );
+            $alpha = ( $colorxy >> 24 ) & 0xFF;
+            //calculate new alpha
+            if( $minalpha !== 127 ){
+                $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha );
+            } else {
+                $alpha += 127 * $pct;
+            }
+            //get the color index with new alpha
+            $alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
+            //set pixel with the new color + opacity
+            if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){
+                return false;
+            }
+        }
+    }
+    // The image copy
+    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
+} 
+?>