<?php
//================================================================
// KOOBI INSTALLER-CLASS
// Version: 2.0
// www.koobi-erweiterungen.de
//================================================================
define("COPYRIGHT",'<div align="center" style="color: #BBB;" >Koobi-Installer 2.01 | &copy; 2007-2008 <a href="http://www.koobi-erweiterungen.de" style="color: #BBB;" target="_blank">Koobi Erweiterungen</a></div>');

if(defined('KOOBIDEMO')) $GLOBALS['tmpl']->assign('emsg', $GLOBALS['config_vars']['notindemo']);

include_once(BASE_DIR. "/functions/Func.Installer.php");

define(ADMINTHEME, (isset($_SESSION['admin_theme']) && is_dir(BASE_DIR . 'admin/' . $_SESSION['admin_theme'])) ? $_SESSION['admin_theme'] : 'standard');
if(ADMINTHEME != "standard") define("INS_ATEMP", 1);

# STARTE KLASSE
class installer
{

	# ÜBERSICHT
	function showSetting()
	{
	
		$modules = array();
		$num = 1;
		
		# ACCOUNT SPEICHERN
		if($_REQUEST['save'] == 1)
		{
			unlink(BASE_DIR . "/addons/installer/config.php");
			
			$content = '<?php $idcode = "'.sha1($_POST['email'] . md5($_POST['licence'])).'"; ?>';
			
			$write = @fopen(BASE_DIR . "/addons/installer/config.php", "wb");
			@fwrite($write, $content);
			@fclose($write);
			
			$_SESSION['check_licence'] = 0;
			
			header("Location: index.php?do=installer");
			exit();
		
		}
		
		# ERWEITERUNGEN LADEN
		if(isset($_REQUEST['load']))
		{
			@include_once(BASE_DIR . "/addons/installer/config.php");
			@$comp = file_get_contents("http://www.koobi-erweiterungen.de/install/".$idcode."/". $_REQUEST['load'] ."/");
			
			$write = @fopen(BASE_DIR . "/addons/installer/".$_REQUEST['load'].".tgz", "wb");
			@fwrite($write, $comp);
			@fclose($write);
			
			$archive = BASE_DIR . "/addons/installer/".$_REQUEST['load'].".tgz"; 
			$type = getarchivetype($archive); 
			if ($type != "unknown") 
			{ 
				if ($type != "tar") 
				{ 
					if ($type == "gzip") ungzip($archive);
					if ($type == "bzip2") unbzip2($archive);

					$uncompressedname = getuncompressedfilename($archive); 
					if(getarchivetype($uncompressedname) == "tar") 
					{ 
						untar($uncompressedname); 
						unlink($uncompressedname); 
					}
				} 
			} 
	
			else untar($archive);
			
			unlink(BASE_DIR . "/addons/installer/".$_REQUEST['load'].".tgz");
			
			$_SESSION['check_licence'] = 0;
			
			header("Location: index.php?do=installer");
			exit();
		
		}
		
		$config_vars = $GLOBALS['tmpl']->get_config_vars();
		
		# EINBINDEN DER MODULE		
		$addons = array();
		
		$ordner = BASE_DIR . "/addons/";
		{ 
		$handle = opendir($ordner); 
		while ($file = readdir ($handle)) { 
				if($file{0} != '.') {
						if(is_dir($ordner.'/'.$file)) { 
								$folderArray[] = $file; 
						} 
				} 
		} 
		closedir($handle); 
	
		if(isset($folderArray)) { 
				asort($folderArray); 
				foreach($folderArray as $row) { 
					unset($folderArray2);
					if(is_file(BASE_DIR . "/addons/" . $row . "/addon.". $row .".php")) array_push($addons, $row);
				} 
			} 
		}
			
		$_SESSION['addons'] = implode(",", $addons);

		foreach($addons as $add)
		{
			include_once(BASE_DIR . "/addons/" . $add . "/addon.". $add .".php");
				
			$row_f = $GLOBALS['db']->ArrQuery("SELECT * FROM " . PREFIX . "_installer WHERE Ordner='".$add."'");
			$num_f = is_array($row_f) ? "1" : "0";
			
			# Auf Updates ueberpruefen
			if($row_f['Build'] < $modul['Build'])
			{
				for ($s = $row_f['Build']; $s <= $modul['Build']; $s++)
				{
					$count = $count + count($modul_system_update[$s]);
					$count = $count + count($modul_template_update[$s]);
					$count = $count + count($modul_database_update[$s]);
				}
				
				if($count == 0)
				{
					# In Datenbank registrieren
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_installer 
						SET Build = '" . $modul['Build'] . "'
						WHERE Ordner = '" . $modul['Ordner'] . "'");
				}
				$update = ($count != 0) ? 1 : 0;
			}
			
			$domain = strpos($_SERVER['SCRIPT_NAME'],"index.php") ? str_replace("admin/index.php","",$_SERVER['SCRIPT_NAME']) : "";
			
			# HANDBUCH UND CHANGELOG UEBERPRUEFEN
			if(is_file(BASE_DIR . "/addons/" . $add . "/docs/Handbuch.pdf"))
			{		
				$handbuch = 1;
				$handbuch_download = $domain ."addons/".$add."/docs/Handbuch.pdf";
			}
			
			if(is_file(BASE_DIR . "/addons/" . $add . "/docs/Changelog.pdf"))
			{		
				$changelog = 1;
				$changelog_download = $domain ."addons/".$add."/docs/Changelog.pdf";
			}
			
			
			
			# NACH UPDATES SUCHEN
			if(isset($modul['pcode']) and !isset($_SESSION[$modul['pcode']]))
			{
				@$xml = file_get_contents("http://www.koobi-erweiterungen.de/updates/".$modul['pcode'] ."/");
				@$build = str_replace(" ", "", strip_content(preg_replace("/(.*?)<build>(.*?)<\/build>(.*?)/is", "\\2", $xml)));
						
				if($build > $modul['Build']) 
				{
					$description = strip_content(preg_replace("/(.*?)<description>(.*?)<\/description>(.*?)/is", "\\2", $xml));
					$download = "index.php?do=installer&load=".$modul['pcode'];
					$version = strip_content(preg_replace("/(.*?)<version>(.*?)<\/version>(.*?)/is", "\\2", $xml));
					$new_update = "1";
	
				}
				
				$_SESSION[$modul['pcode']] = 1;
				
			}
		
			array_push($modules, array("id" => $num, "folder" => $add, "name" => $modul['Name'], "version" => $modul['Version'], "build" => $modul['Build'], "build_db" => $row_f['Build'], "update" => $update, "installed" => $num_f, "desc" => $modul['Beschreibung'], "owner" => '<a href="'.$modul['Webseite'].'" target="_blank" style="color: #646464;">'.$modul['Autor'].'</a>', "img" => $modul['Bild'], "new_update" => $new_update,"update_desc" => "<b>".$config_vars['Installer_NewVersion']." ".$version."</b> <br />".$description, "update_download" => $download, "handbuch" => $handbuch, "handbuch_download" => $handbuch_download, "changelog" => $changelog, "changelog_download" => $changelog_download));
		
			$pcodes[] = $modul['pcode'];
			
			
			unset($modul['Name'], $modul['Build'], $modul['Beschreibung'], $modul['Webseite'], $modul['Autor'], $modul['Bild'],$modul['Check_Updates'],$modul['Version'],$modul['Update_Link'],$modul['pcode'],$update,$new_update,$handbuch,$changelog);
			$num = $num + 1;
			
		}
		
		# Account ueberpruefen
		if(is_file(BASE_DIR . "/addons/installer/config.php") and ($_SESSION['check_licence'] == 0 or !isset($_SESSION['check_licence'])))
		{
			include_once(BASE_DIR . "/addons/installer/config.php");
			@$xml = file_get_contents("http://www.koobi-erweiterungen.de/install/".$idcode."/");
			
			if(!empty($xml))
			{
				$licences = str_replace(" ", "", strip_content(preg_replace("/(.*?)<licences>(.*?)<\/licences>(.*?)/is", "\\2", $xml)));
				$licence = explode(";", $licences);
				
				$pre_modules = array();
				
				foreach($licence as $l)
				{
					$p = explode(",", $l);
					if(!in_array($p['0'], $pcodes) and !empty($p['0'])) array_push($pre_modules, array("pcode" => $p['0'], "name" => $p['1'], "version" => $p['2']));
				}
				
				$account = "accept";
			
			}
			else  $account = "false";
			
			$_SESSION['check_licence'] = $xml;
		
		} 
		else $account = "false";
		
		if($_SESSION['check_licence'] != 0 and isset($_SESSION['check_licence']))
		{
			if(!empty($_SESSION['check_licence']))
			{
				$licences = str_replace(" ", "", strip_content(preg_replace("/(.*?)<licences>(.*?)<\/licences>(.*?)/is", "\\2", $xml)));
				$licence = explode(";", $licences);
				
				$pre_modules = array();
				
				foreach($licence as $l)
				{
					$p = explode(",", $l);
					if(!in_array($p['0'], $pcodes) and !empty($p['0'])) array_push($pre_modules, array("pcode" => $p['0'], "name" => $p['1'], "version" => $p['2']));
				}
				
				$account = "accept";
			
			}
			else  $account = "false";
		
		}
		
		$filled = (is_file(BASE_DIR . "/addons/installer/config.php")) ? 1 : 0;
		
		$_SESSION["modules"] = $modules;
		
		$GLOBALS['tmpl']->assign("filled", $filled);
		$GLOBALS['tmpl']->assign("pre_modules", $pre_modules);
		$GLOBALS['tmpl']->assign("formaction", "index.php?do=installer");
		$GLOBALS['tmpl']->assign("account_stat", $account);
		$GLOBALS['tmpl']->assign("mods_num", count($pre_modules));
		$GLOBALS['tmpl']->assign("account", $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/installer/admin_account.tpl'));
		$GLOBALS['tmpl']->assign("licences", $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/installer/admin_licence.tpl'));	
		$GLOBALS['tmpl']->assign("items", $modules);
		$GLOBALS['tmpl']->assign("copy", COPYRIGHT);
		$GLOBALS['tmpl']->assign("theme", ADMINTHEME);
		$GLOBALS['tmpl']->assign('title', "Koobi-Installer");
		
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/installer/admin_start.tpl'));
	}
	
	# TEMPLATES ÜBERPRÜFEN
	function checkTemplate()
	{
		$modules = $_SESSION["modules"];

		if($_REQUEST['modul'] == "all")
		{
			foreach($modules as $m)
			{
				include_once(BASE_DIR . "/addons/" .  $m['folder'] ."/addon.".  $m['folder'] .".php");
				
				if(count($modul_template_install) != 0) $num = 1;
			}
		
		} else {
	
			$mod = $_REQUEST['modul'] - 1;
			include_once(BASE_DIR . "/addons/" .  $modules[$mod]['folder'] ."/addon.".  $modules[$mod]['folder'] .".php");
			
			switch($_REQUEST['method'])
			{
				case 'install':
					if(count($modul_template_install) != 0) $num = 1;
				break;
					
				case 'deinstall':
					if(count($modul_template_install) != 0) $num = 1;
				break;
					
				case 'update':
					for ($s = $modules[$mod]['build_db'] + 1; $s <= $modules[$mod]['build']; $s++) $count = $count + count($modul_template_update[$s]);
					if($count != 0) $num = 1;
				break;
			}
			
				
	
		}
		
		$_SESSION["method"] = $_REQUEST['method'];
		
		if($num != 1) header("location: index.php?do=installer&sub=perm&modul=".$_REQUEST['modul']); 
		else 
		{
	
		
			$templates = array();
		
			$ordner = BASE_DIR . "/theme/";
			{ 
			$handle = opendir($ordner); 
			while ($file = readdir ($handle)) { 
					if($file{0} != '.') { //Versteckte Dateien nicht anzeigen 
							if(is_dir($ordner.'/'.$file)) { 
									$folderArray[] = $file; 
							} 
					} 
			} 
			closedir($handle); 
	
			if(isset($folderArray)) { 
					asort($folderArray); 
					foreach($folderArray as $row) { 
						unset($folderArray2);
						array_push($templates, $row);
					} 
				} 
			}
				
			$GLOBALS['tmpl']->assign("items", $templates);
			$GLOBALS['tmpl']->assign("formaction", "index.php?do=installer&sub=perm&modul=".$_REQUEST['modul']);
			$GLOBALS['tmpl']->assign("copy", COPYRIGHT);
			$GLOBALS['tmpl']->assign("theme", ADMINTHEME);
			$config_vars = $GLOBALS['tmpl']->get_config_vars();
			$GLOBALS['tmpl']->assign('title', "Koobi-Installer - ".$config_vars['Installer_Templates']);
		
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/installer/admin_template.tpl'));
		
		}
	
	}
	
	# RECHTE ÜBERPRÜFEN
	function checkPerm()
	{
		$modules = $_SESSION["modules"];
		
		if($_POST['temp'] == 1 and empty($_POST['tpl'])) header("location: index.php?do=installer&sub=check&modul=".$_REQUEST['modul']."&error=1");
		
		isset($_POST['tpl']) ? $_SESSION['ins_tpl'] = implode("§", $_POST['tpl']) : $_POST['tpl'] = explode("§", $_SESSION['ins_tpl']) ;
		
		$config_vars = $GLOBALS['tmpl']->get_config_vars();
		$create = ' <font color="#ff9600">('.$config_vars['Installer_Create'].')</font>';
		
		$system = array();
		$template = array();

		if($_REQUEST['modul'] == "all")
		{
			foreach($modules as $m)
			{
				include_once(BASE_DIR . "/addons/" .  $m['folder'] ."/addon.".  $m['folder'] .".php");
				
					
						if(isset($modul_system_install))
						{
							foreach($modul_system_install as $m)
							{
								$i = explode("___", $m);
								if(INST_ATEMP == 1) str_replace("theme/standard","theme/".ADMINTHEME, $i[0]);
								if(!is_file(BASE_DIR."/".$i[0])) array_push($system,$i[0] . $create);
								else if(!is_writeable(BASE_DIR."/".$i[0])) array_push($system,$i[0]);	

							}
						}
						
						if(isset($modul_template_install))
						{
							foreach($_POST['tpl'] as $tpl)
							{
								foreach($modul_template_install as $m)
								{
									$i = explode("___", $m);
									if(!is_file(BASE_DIR."/".$i[0])) array_push($template,$i[0] . $create);
									else if(!is_writeable(BASE_DIR."/theme/".$tpl."/".$i[0])) array_push($template,"theme/".$tpl."/".$i[0]);
								}
							}
						}
						
						unset($modul_system_install,$modul_template_install);

			}
		
		} else {
		
			$mod = $_REQUEST['modul']-1;
			include_once(BASE_DIR . "/addons/" .  $modules[$mod]['folder'] ."/addon.".  $modules[$mod]['folder'] .".php");
			
				switch($_SESSION["method"])
				{
					case 'install':
					
						if(isset($modul_system_install))
						{
							foreach($modul_system_install as $m)
							{
								$i = explode("___", $m);
								if(INST_ATEMP == 1) str_replace("theme/standard","theme/".ADMINTHEME, $i[0]);
								if(!is_file(BASE_DIR."/".$i[0])) array_push($system,$i[0] . $create);
								else if(!is_writeable(BASE_DIR."/".$i[0])) array_push($system,$i[0]);	
							}
						}
						
						
						if(isset($modul_template_install))
						{
						
							foreach($_POST['tpl'] as $tpl)
							{
								foreach($modul_template_install as $m)
								{
									$i = explode("___", $m);
									if(!is_file(BASE_DIR."/".$i[0])) array_push($template,$i[0] . $create);
									else if(!is_writeable(BASE_DIR."/theme/".$tpl."/".$i[0])) array_push($template,"theme/".$tpl."/".$i[0]);
								}
							}
						}
						
						unset($modul_system_install,$modul_template_install);
						
					break;
					
					case 'deinstall':
					
						if(isset($modul_system_install))
						{
							foreach($modul_system_install as $m)
							{
								$i = explode("___", $m);
								if(INST_ATEMP == 1) str_replace("theme/standard","theme/".ADMINTHEME, $i[0]);
								if(!is_writeable(BASE_DIR."/".$i[0])) array_push($system,$i[0]);	
							}
						}
							
						if(isset($modul_template_install))
						{
							foreach($_POST['tpl'] as $tpl)
							{
								foreach($modul_template_install as $m)
								{
									$i = explode("___", $m);
									if(!is_writeable(BASE_DIR."/theme/".$tpl."/".$i[0])) array_push($template,"theme/".$tpl."/".$i[0]);
								}
							}
						}
						
						unset($modul_system_install,$modul_template_install);
						
					break;
					
					case 'update':
					
						for ($s = $modules[$mod]['build_db']; $s <= $modules[$mod]['build']; $s++)
						{
					
							foreach($modul_system_update[$s] as $m)
							{
								$i = explode("___", $m);
								if(INST_ATEMP == 1) str_replace("theme/standard","theme/".ADMINTHEME, $i[0]);
								if(!is_file(BASE_DIR."/".$i[0])) array_push($system,$i[0] . $create);
								else if(!is_writeable(BASE_DIR."/".$i[0])) array_push($system,$i[0]);	
							}
							
							foreach($_POST['tpl'] as $tpl)
							{
								foreach($modul_template_update[$s] as $m)
								{
									$i = explode("___", $m);
									if(!is_file(BASE_DIR."/".$i[0])) array_push($template,$i[0] . $create);
									else if(!is_writeable(BASE_DIR."/theme/".$tpl."/".$i[0])) array_push($template,"theme/".$tpl."/".$i[0]);
								}
										
							}
						
						}
						
						unset($modul_system_install,$modul_template_update);
						
					break;
				
				}
	
		}
		
		$system = array_unique($system);
		$template = array_unique($template);
		
		if(count($system) == 0 and count($template) == 0 ) header("location: index.php?do=installer&sub=install&modul=".$_REQUEST['modul']); 
		else 
		{
		
			$GLOBALS['tmpl']->assign("tpl", $template);
			$GLOBALS['tmpl']->assign("sys", $system);
			
			$GLOBALS['tmpl']->assign("tplc", count($template));
			$GLOBALS['tmpl']->assign("sysc", count($system));
			$GLOBALS['tmpl']->assign("post", $_POST['tpl']);
			
			$GLOBALS['tmpl']->assign("reaction", "index.php?do=installer&sub=perm&modul=".$_REQUEST['modul']);
			$GLOBALS['tmpl']->assign("copy", COPYRIGHT);
			$GLOBALS['tmpl']->assign("theme", ADMINTHEME);
			$GLOBALS['tmpl']->assign('title', "Koobi-Installer -  ".$config_vars['Installer_Rights']);
			
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/installer/admin_permission.tpl'));
		
		}

	}

	# INSTALLIEREN
	function install()
	{
		$modules = $_SESSION["modules"];
		$_POST['tpl'] = explode("§", $_SESSION['ins_tpl']);
		
		$log = array();

		if($_REQUEST['modul'] == "all")
		{
			foreach($modules as $m)
			{
				include_once(BASE_DIR . "/addons/" . $m['folder'] ."/addon.". $m['folder'] .".php");
				
				
						if(isset($modul_database_install) AND !(table_exists($modul['DB_MARK'])))
						{
							foreach($modul_database_install as $d)
							{
								
								$sql = $GLOBALS['db']->Query($d);
							}
							
							$db_installed = 1;
						}
					
						if(isset($modul_system_install))
						{
							foreach($modul_system_install as $s)
							{
								$i = explode("___", $s);
								if(INST_ATEMP == 1) str_replace("theme/standard","theme/".ADMINTHEME, $i[0]);
								$content = file_get_contents(BASE_DIR."/".$i['0']);
					
								if((strpos($content, $modul['SYS_MARK']) == false) or (in_array($i['0'], $log)))
								{
									if($i['3'] == "file")
									{
										$content_new = file_get_contents(BASE_DIR."/".$i['1']);
										$write = @fopen(BASE_DIR."/".$i['0'], "wb");
										@fwrite($write, $content_new);
										@fclose($write);
										
										$write = @fopen(BASE_DIR."/".$i['2'], "wb");
										@fwrite($write, $content);
										@fclose($write);
									
									}
									else
									{
									
										$content = str_replace_count ($i['1'],$i['2'], $content,$i['3']);
										$write = @fopen(BASE_DIR."/".$i['0'], "wb");
										@fwrite($write, $content);
										@fclose($write);
									}
									
									array_push($log, $i['0']);
								}
							}
							
							$sys_installed = 1;
						}
						
						if(isset($modul_template_install))
						{
							foreach($_POST['tpl'] as $tpl)
							{
								foreach($modul_template_install as $t)
								{
									$i = explode("___", $t);
									$content = file_get_contents(BASE_DIR."/theme/".$tpl."/".$i['0']);
					
									if((strpos($content, $modul['TPL_MARK']) == false) or (in_array($i['0'], $log)))
									{
										if($i['3'] == "file")
										{
											$content_new = file_get_contents(BASE_DIR."/theme/".$tpl."/".$i['1']);
											$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['0'], "wb");
											@fwrite($write, $content_new);
											@fclose($write);
											
											$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['2'], "wb");
											@fwrite($write, $content);
											@fclose($write);
										
										}
										else
										{
										
											$content = str_replace_count ($i['1'],$i['2'], $content,$i['3']);
											$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['0'], "wb");
											@fwrite($write, $content);
											@fclose($write);
										}
										
										array_push($log, $i['0']);
									}
								}
							}
							
							$temp_installed = 1;
						}
						
						# In Datenbank registrieren
						$row_f = $GLOBALS['db']->ArrQuery("SELECT * FROM " . PREFIX . "_installer WHERE Ordner='".$m['folder']."'");
						$num_f = is_array($row_f) ? "1" : "0";
						
						
						if($num_f == 0)
						{		
							$GLOBALS['db']->Query("INSERT " . PREFIX . "_installer 
							VALUES ('','" . $m['name'] . "', '" . $m['folder'] ."', '" . $m['build'] . "')
							");
						
						}
						else if($m['version'] > $m['version_db'])
						{
						
							$GLOBALS['db']->Query("UPDATE " . PREFIX . "_installer 
							SET Build = '" . $m['build'] . "'
							WHERE Ordner = '" . $m['folder'] . "'");
						
						}
						
						
						unset($modul_system_install,$modul_template_update,$modul_database_install,$modul['DB_MARK'],$modul['SYS_MARK'],$modul['TPL_MARK'],$_SESSION['addons']);
					
				
				
			}
		
		} else {
	
			$mod = $_REQUEST['modul']-1;
			include_once(BASE_DIR . "/addons/" .  $modules[$mod]['folder'] ."/addon.".  $modules[$mod]['folder'] .".php");
			
				switch($_SESSION["method"])
				{
					case 'install':
					
						if(isset($modul_database_install) AND !(table_exists($modul['DB_MARK'])))
						{
							foreach($modul_database_install as $d)
							{
								
								$sql = $GLOBALS['db']->Query($d);
							}
							
							$db_installed = 1;
						}
					
						if(isset($modul_system_install))
						{
							foreach($modul_system_install as $s)
							{
								$i = explode("___", $s);
								if(INST_ATEMP == 1) str_replace("theme/standard","theme/".ADMINTHEME, $i[0]);
								$content = file_get_contents(BASE_DIR."/".$i['0']);
				
								if((strpos($content, $modul['SYS_MARK']) == false) or (in_array($i['0'], $log)))
								{
									if($i['3'] == "file")
									{
										$content_new = file_get_contents(BASE_DIR."/".$i['1']);
										$write = @fopen(BASE_DIR."/".$i['0'], "wb");
										@fwrite($write, $content_new);
										@fclose($write);
										
										$write = @fopen(BASE_DIR."/".$i['2'], "wb");
										@fwrite($write, $content);
										@fclose($write);
									
									}
									else
									{
									
										$content = str_replace_count ($i['1'],$i['2'], $content,$i['3']);
										$write = @fopen(BASE_DIR."/".$i['0'], "wb");
										@fwrite($write, $content);
										@fclose($write);
									}
									
									array_push($log, $i['0']);
								}
							}
							
							$sys_installed = 1;
						}
						
						if(isset($modul_template_install))
						{
							foreach($_POST['tpl'] as $tpl)
							{
								foreach($modul_template_install as $t)
								{
									$i = explode("___", $t);
									$content = file_get_contents(BASE_DIR."/theme/".$tpl."/".$i['0']);
					
									if((strpos($content, $modul['TPL_MARK']) == false) or (in_array($i['0'], $log)))
									{
										if($i['3'] == "file")
										{
											$content_new = file_get_contents(BASE_DIR."/theme/".$tpl."/".$i['1']);
											$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['0'], "wb");
											@fwrite($write, $content_new);
											@fclose($write);
											
											$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['2'], "wb");
											@fwrite($write, $content);
											@fclose($write);
										
										}
										else
										{
										
											$content = str_replace_count ($i['1'],$i['2'], $content,$i['3']);
											$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['0'], "wb");
											@fwrite($write, $content);
											@fclose($write);
										}
										
										array_push($log, $i['0']);
									}
								}
							}
							
							$temp_installed = 1;
						}
						
						# In Datenbank registrieren
						$row_f = $GLOBALS['db']->ArrQuery("SELECT * FROM " . PREFIX . "_installer WHERE Ordner='".$modules[$mod]['folder']."'");
						$num_f = is_array($row_f) ? "1" : "0";
						
						if($num_f == 0)
						{		
							$GLOBALS['db']->Query("INSERT " . PREFIX . "_installer 
							VALUES ('','" . $modules[$mod]['name'] . "', '" . $modules[$mod]['folder'] ."', '" . $modules[$mod]['build'] . "')
							");
						
						}
						else if($modules[$mod]['version'] > $modules[$mod]['version_db'])
						{
							
							$GLOBALS['db']->Query("UPDATE " . PREFIX . "_installer 
							SET Build = '" . $modules[$mod]['build'] . "'
							WHERE Ordner = '" . $modules[$mod]['folder'] . "'");
						
						}
						
						
						unset($modul_system_install,$modul_template_update,$modul_database_install,$modul['DB_MARK'],$modul['SYS_MARK'],$modul['TPL_MARK'],$_SESSION['addons']);
						
					break;
					
					case 'deinstall':
					
					
						if(isset($modul_system_install))
						{
							foreach($modul_system_install as $m)
							{
								$i = explode("___", $m);
								if(INST_ATEMP == 1) str_replace("theme/standard","theme/".ADMINTHEME, $i[0]);
								$content = file_get_contents(BASE_DIR."/".$i['0']);
				
								if((strpos($content, $modul['SYS_MARK']) == true) or (in_array($i['0'], $log)))
								{
									if($i['3'] == "file")
									{
										$content_old = file_get_contents(BASE_DIR."/".$i['2']);
										$write = @fopen(BASE_DIR."/".$i['0'], "wb");
										@fwrite($write, $content_old);
										@fclose($write);
									}
									else
									{
									
										$content = str_replace_count ($i['2'],$i['1'], $content,$i['3']);
										$write = @fopen(BASE_DIR."/".$i['0'], "wb");
										@fwrite($write, $content);
										@fclose($write);
									}
									
									array_push($log, $i['0']);
								}
							}
							
							$sys_installed = 1;
						}
						
						if(isset($modul_template_install))
						{
							foreach($_POST['tpl'] as $tpl)
							{
								foreach($modul_template_install as $m)
								{
									$i = explode("___", $m);
									$content = file_get_contents(BASE_DIR."/theme/".$tpl."/".$i['0']);
					
									if((strpos($content, $modul['TPL_MARK']) == true) or (in_array($i['0'], $log)))
									{
										if($i['3'] == "file")
										{
											$content_old = file_get_contents(BASE_DIR."/theme/".$tpl."/".$i['2']);
											$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['0'], "wb");
											@fwrite($write, $content_old);
											@fclose($write);
										
										}
										else
										{
										
											$content = str_replace_count ($i['2'],$i['1'], $content,$i['3']);
											$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['0'], "wb");
											@fwrite($write, $content);
											@fclose($write);
										}
										
										array_push($log, $i['0']);
									}
								}
							}
							
							$temp_installed = 1;
						}
						
						# In Datenbank registrieren
						$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_installer WHERE Ordner = '".$modules[$mod]['folder']."'");
	
						unset($modul_system_install,$modul_template_update,$modul_database_install,$modul['DB_MARK'],$modul['SYS_MARK'],$modul['TPL_MARK'],$_SESSION['addons']);
						
					break;
					
					case 'update':
					
							
						for ($s = $modules[$mod]['build_db']; $s <= $modules[$mod]['build']; $s++)
						{
						
							foreach($modul_database_update[$s] as $d)
							{
								
								$sql = $GLOBALS['db']->Query($d);
								
								$db_installed = 1;
							}
						
							foreach($modul_system_update[$s] as $m)
							{
								$i = explode("___", $m);
								if(INST_ATEMP == 1) str_replace("theme/standard","theme/".ADMINTHEME, $i[0]);
								$content = file_get_contents(BASE_DIR."/".$i['0']);
				
								if($i['3'] == "file")
								{
									$content_new = file_get_contents(BASE_DIR."/".$i['1']);
									$write = @fopen(BASE_DIR."/".$i['0'], "wb");
									@fwrite($write, $content_new);
									@fclose($write);
									
									$write = @fopen(BASE_DIR."/".$i['2'], "wb");
									@fwrite($write, $content);
									@fclose($write);
								
								}
								else
								{
								
									$content = str_replace_count ($i['1'],$i['2'], $content,$i['3']);
									$write = @fopen(BASE_DIR."/".$i['0'], "wb");
									@fwrite($write, $content);
									@fclose($write);
								}
								
								$sys_installed = 1;
							}

							foreach($_POST['tpl'] as $tpl)
							{
								foreach($modul_template_update[$s] as $m)
								{
									$i = explode("___", $m);
									$content = file_get_contents(BASE_DIR."/theme/".$tpl."/".$i['0']);
					
									if($i['3'] == "file")
									{
										$content_new = file_get_contents(BASE_DIR."/theme/".$tpl."/".$i['1']);
										$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['0'], "wb");
										@fwrite($write, $content_new);
										@fclose($write);
										
										$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['2'], "wb");
										@fwrite($write, $content);
										@fclose($write);
									
									}
									else
									{
									
										$content = str_replace_count ($i['1'],$i['2'], $content,$i['3']);
										$write = @fopen(BASE_DIR."/theme/".$tpl."/".$i['0'], "wb");
										@fwrite($write, $content);
										@fclose($write);
									}
									
									$temp_installed = 1;
								}
							}
						
						}
						# In Datenbank registrieren
							$GLOBALS['db']->Query("UPDATE " . PREFIX . "_installer 
							SET Build = '" . $modules[$mod]['build'] . "'
							WHERE Ordner = '" . $modules[$mod]['folder'] . "'");
						
						
						
						unset($modul_system_install,$modul_template_update,$modul_database_install,$modul,$_SESSION['addons']);
						
					break;
				
				}
	
		}
		
		$GLOBALS['tmpl']->assign("db", $db_installed);
		$GLOBALS['tmpl']->assign("sys", $sys_installed);
		$GLOBALS['tmpl']->assign("temp", $temp_installed);

		$GLOBALS['tmpl']->assign("copy", COPYRIGHT);
		$GLOBALS['tmpl']->assign("theme", ADMINTHEME);
		
		$config_vars = $GLOBALS['tmpl']->get_config_vars();
		$GLOBALS['tmpl']->assign('title', "Koobi-Installer - ".$config_vars['Installer_Closed']);
		
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/installer/admin_finish.tpl'));
		

	}

}
?>