<?php
function removeDir($path) {
	//vytvoreni instance adredare
	$dir = dir($path);
	
	while ($filename = $dir->read()) {
		//konotrla . a ..
		if ($filename == "." || $filename == "..") {
			continue;
		}
		
		//kontrola typu objektu
		if (is_file($path . "/" . $filename)) {
			//objekt je soubor
			unlink($path . "/" . $filename);
		} else {
			removeDir($path . "/" . $filename);
		}
	}
	
	rmdir($path);
}

function copyContent($directory, $basePath) {
	$dir = dir($directory);
	
	//zasobnich jmen zanoreni adredaru
	static $path = array();
	
	//sestaveni aktualni relativni cesty k adredari
	$currentPath = implode("/", $path);
	
	if ($currentPath) {
		$currentPath .= "/";
	}
	 
	$dirList = array();
	
	while ($name = $dir->read()) {
		if ($name != "." && $name != "..") {
			//kontorla jestli je cil adredarem
			if (is_dir($directory . "/" . $name)) {
				$dirList[] = $name;
				
				if (!is_dir($basePath . $currentPath . $name)) {
					mkdir($basePath . $currentPath . $name);
				}
			} else {
				if ($directory) {
					$directory .= "/";
				}
				
				$path = $basePath . $currentPath;
				
				if ($path) {
					$path .= "/";
				}
				
				copy($directory . $name,  $path . $name);
			}
		}
	}
	
	//zkopirovani adredaru
	foreach ($dirList as $name) {
		$path[] = $name;
		copyContent($directory . "/" . $name, $basePath);
		array_pop($path);
	}
}

//vygenerovani docasneho jmena
$zipPath = tempnam(dirname(__FILE__), "install_");

unlink($zipPath);

mkdir($zipPath);

//rozbaleni ZIP archivu
$zip = new ZipArchive();
$zip->open("./projectComprimed.zip");
$zip->extractTo($zipPath);

//zjisteni jmena korenoveho adredare projektu
$dir = dir($zipPath);

$found = false;

while (($name = $dir->read()) && !$found) {
	if ($name == "." || $name == "..") {
		continue;
	}
	
	$found = is_dir($zipPath . "/" . $name);
	$dirName = $name;
}

if (!$found) {
	removeDir($zipPath);
	return;
}

//priprava cesty
$contentPath = $zipPath . "/" . $dirName;

//zkopirovani
copyContent($contentPath, $_REQUEST["install_path"] || "");

//smazani adredare a souboru
removeDir($zipPath);
unlink("projectComprimed.zip");
unlink("projectComprimed.php");