#!/usr/bin/perl

################ (C) COPYRIGHT ##########################################
#		Programmer: 	Tri Dung Nguyen				#
#		Website:	www.vitinh.de				#
#		Mirrors:	www.freeperlscripts.de			#
#				www.tridung.de				#
#########################################################################
	
################ MULTIPLE USAGE OF FPG ON ONE SERVER ####################
# Change the two varialbles below if you want to install more than one  #
# FPG on the same server. For example:					#
#	my $dir = "fpg_files_2";					#
#	my $fpg_public = "fpg_public_2";				#
# for the second album and so on... Do the same with the file fpg.cgi	#
# and asimg.cgi								#
#########################################################################

################ FPG on a Windows Machine with IIS ######################
# Use absolute path for the 4 varialbles below if you want to use FPG   #
# on a Windows Machine with IIS. For example:				#
#	my $dir = "c\:\\myaccount\\mydir\\fpg_files";			#
#	my $fpg_public = "c\:\\myaccount\\mydir\\fpg_public";		#
#	my $libdir = "c\:\\myaccount\\mydir\\fpg_ascii";		#
#	my $bindir = "c\:\\myaccount\\mydir\\fpg_binary";		#
#########################################################################

my $dir = "fpg_files";
my $fpg_public = "fpg_public";
my $libdir = "fpg_ascii";
my $bindir = "fpg_binary";

####################### BEGIN OF PROGRAM CODE ###########################
						
use CGI::Carp qw(fatalsToBrowser);
use CGI; # This modul must be installed on your machine
use CGI qw(:standard);
use Time::Local; # This modul must be installed on your machine
#use strict;

my $file_copy = 0;
eval {use File::Copy;}; $file_copy = 1 if(!$@); # Comment this line if you get a File::Copy-Error

my $net_smtp = 0;
eval {use Net::SMTP;}; $net_smtp = 1 if(!$@); # Comment this line if you get a Net::SMTP-Error

my $gd = 0;
my $asmethod = 2;
eval {require GD;}; unless($@){$gd = 1; $asmethod = 1; import GD;} # Comment this line if you get a GD-Error
   
my $cgi = new CGI;
my $version = "2.03";
my $mytag = "19";
my $mymon = "04";
my $myjahr = "2007";
my $date = timelocal(0,0,0,$mytag,$mymon-1,$myjahr-1900);
my $update_and_del = 1;
my $myself = $ENV{'SCRIPT_NAME'};
my $docroot = $ENV{'DOCUMENT_ROOT'};
my $server_url = $ENV{'SERVER_URL'};
   $server_url =~ s/\/$//;
my $guestbook = "fpg.cgi";
my $publicdir = "$docroot/$fpg_public";
my $asdir = "$dir/antispam";
my $skindir = "$dir/skins";
my $langdir = "$dir/languages";
my $restrictions = "$dir/restrictions";
my $configs = "$dir/configs";
my $session = "$dir/session";
my $ilang = "$libdir/install.fpg";
my $license = "$libdir/license.fpg";
my $default_pass = crypt("vitinh.de", "td");
my $olddir = "tdpn_v_10_dir";
my $conttype = "Content-type: text/html\n\n";
my $default_sendmail = "/usr/lib/sendmail -t";
my $default_smtp = "localhost";
my @alldirs = ();

my $info_title = "FPG - Error!";
my $info = "Internal error! Please go to the <a href=\"http://www.vitinh.de\">FPG Homepage</a> to get help!<br><br>\n";
my $info_color = "red";
my $tdpn = "<br><br><a style=\"text-decoration:none; color:gray\" href=\"http://www.vitinh.de/cgi-bin/download_guestbook.cgi\">Free Perl Guestbook Version $version</a><br><br>";
my $css = qq(<style type="text/css">
body{ 
	scrollbar-base-color:#7083c7;
	scrollbar-arrow-color:red;
}
a{
	color: black;
	text-decoration: none;
	}
.text{
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 11px;
	}
.navi{
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 12px;
	}
.text-white{
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 11px;
	color: white;
	}
.mail_text{
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 12px;
	}
.title{
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	}
.title_7083c7{
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	color: #7083c7;
	}
.small{
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 10px;
	}
.red{
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 11px;
	color: red;
	}
.red-bold{
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	color: red;
	}
.error{
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	color: red;
	}
.form-text{
	color: black;
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 11px;
	}
.form-text-bold{
	color: black;
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	}
.form-textarea{
	color: black;
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 11px;
	}
.form-submit{
	color: red;
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	}
.form-file{
	color: blue;
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 11px;
	}
.form-reset{
	color: blue;
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	}
.form-button{
	color: black;
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	}
.form-select{
	color: black;
	background: white;
	font-family: verdana, arial;
	font-weight: normal;
	font-size: 11px;
	}
.form-select-bold{
	color: black;
	background: white;
	font-family: verdana, arial;
	font-weight: bold;
	font-size: 11px;
	}
.edit-textarea{
	color: white;
	background: black;
	font-family: fixedsys, courier new, verdana, arial;
	font-weight: normal;
	font-size: 12px;
	width: 630px;
	height:	250px;
	}
.thumb{
	width: 120px;
	height:	110px;
}
</style>);

########## Check folders ################################################
if(!-e $libdir){
	$info_title = "Installation failed!";
	$info .= "&raquo; Folder not found: <b>fpg_ascii</b><br>";
	$info .= "&raquo; Upload this folder and all files inside and try again!<br><br>";
	$info .= "&raquo; View the <b>readme</b> file or click [<a href=\"http://www.vitinh.de\">here</a>] for help!<br>";
	&info;
}
if(!-e $bindir){
	$info_title = "Installation failed!";
	$info .= "&raquo; Folder not found: <b>fpg_binary</b><br>";
	$info .= "&raquo; Upload this folder and all files inside and try again!<br><br>";
	$info .= "&raquo; View the <b>readme</b> file or click [<a href=\"http://www.vitinh.de\">here</a>] for help!<br>";
	&info;
}	
########## Load language ################################################
my $mylang = $cgi->param('lang');
   $mylang = "" if(!defined $mylang);
   $mylang = lc $mylang;
   $mylang = "" if($mylang eq "english");
my %alllangs = (
	'bulgarian' => '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080; &#1077;&#1079;&#1080;&#1082;',
	'croatian' => 'Hrvatski',
	'czech' => 'Ce&scaron;tina',
	'danish' => 'Dansk',
	'dutch' => 'Nederlands',
	'english' => 'English',
	'french' => 'Fran&ccedil;ais',
	'german' => 'Deutsch',
	'greek' => '&#917;&#955;&#955;&#951;&#957;&#953;&#954;&#940;',
	'maohi' => 'Maohi',
	'norwegian' => 'Norsk',
	'polish' => 'J&#281;zyk Polski',
	'russian' => '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;',
	'serbian' => 'Srpski',
	'spanish' => 'Espa&ntilde;ol',
	'swedish' => 'Svenska',
	'vietnamese' => 'Ti&#7871;ng Vi&#7879;t'
	);
my @other_languages = ();
if(-d "$libdir/other_languages"){
	opendir(DH, "$libdir/other_languages") or die "Cannot open directory $libdir/other_languages! $!";
	@other_languages = sort grep !/^\.\.?$/, readdir(DH);
	closedir(DH);
	if(exists $alllangs{$mylang}){
		$ilang = "$libdir/other_languages/$mylang/install.fpg";
		$license = "$libdir/other_languages/$mylang/license.fpg";
	}
}
my %ilang = &language($ilang);
########## Action Control ###############################################	
my %actions =(
	'install_now' => \&install_now,
);

my $action = $cgi->param('action');
if(defined $action){
	if($actions{$action}){
		$actions{$action}->();
		}
	else{
		&install;
	}
}
else{&install;}
########### Installation - Restore ######################################
sub install{
	my $conditions = "";
	opendir(DH, $libdir) or die "Cannot open directory $libdir! $!";
	my @allfiles = readdir(DH);
	closedir(DH);
	my @langs = sort(grep/\.lang/, @allfiles);
	my @tpls = grep/\.tpl/, @allfiles;
	if(!-e $ilang){
		$info_title = "Installation failed!";
		$info  = "&raquo; Error code: <b>100</b><br>";
		$info .= "&raquo; File not found: <b>$ilang</b><br>";
		$info .= "&raquo; Upload the file and try again!<br><br>";
		$info .= "&raquo; View the <b>readme</b> file or click [<a href=\"http://www.vitinh.de\">here</a>] for help!<br>";
		&info;
	}
	if(!-e $license){
		$info_title = "Installation failed!";
		$info  = "&raquo; Error code: <b>101</b><br>";
		$info .= "&raquo; File not found: <b>$license</b><br>";
		$info .= "&raquo; Upload the file and try again!<br><br>";
		$info .= "&raquo; View the <b>readme</b> file or click [<a href=\"http://www.vitinh.de\">here</a>] for help!<br>";
		&info;
	}
	else{
		open(DH, "< $license") or die "Cannot open $license! $!";
		my @temp = <DH>;
		close(DH);
		foreach(@temp){
			$conditions .= $_;
		}
		my $thisyear = &getdate("year");
		my $mydate = "$myjahr.$mymon.$mytag";
		$conditions =~ s/\$\$version\$\$/$version/g;
		$conditions =~ s/\$\$date\$\$/$mydate/g;
		$conditions =~ s/\$\$thisyear\$\$/$thisyear/g;
	}
	if(scalar @langs == 0){
		$info_title = "Installation failed!";
		$info  = "&raquo; Error code: <b>102</b><br>";
		$info .= "&raquo; File(s) not found: <b>language file</b><br>";
		$info .= "&raquo; Upload the file(s) and try again!<br><br>";
		$info .= "&raquo; View the <b>readme</b> file or click [<a href=\"http://www.vitinh.de\">here</a>] for help!<br>";
		&info;
	}
	my @alltpls = ("admin","between","entry","message","preview","reply","sign","style","view");
	my @errs;
	my $nofile = "<ul>";
	foreach(@alltpls){
		if(! -e "$libdir/$_.tpl"){
			push @errs, $_;
			$nofile .= "<li><b>$_.tpl</b>\n";
		}
	}
	if(scalar @errs){
		$nofile .= "</ul>";
		$info_title = "Installation failed!";
		$info  = "&raquo; Error code: <b>103</b><br>";
		$info .= "&raquo; File(s) not found: $nofile";
		$info .= "&raquo; Upload the file(s) and try again!<br><br>";
		$info .= "&raquo; View the <b>readme</b> file or click [<a href=\"http://www.vitinh.de\">here</a>] for help!<br>";
		&info;
	}
		
	if(!-e $license){
		$info_title = "Installation failed!";
		$info  = "&raquo; Error code: <b>104</b><br>";
		$info .= "&raquo; File not found: <b>$license</b><br>";
		$info .= "&raquo; Upload the file and try again!<br><br>";
		$info .= "&raquo; View the <b>readme</b> file or click [<a href=\"http://www.vitinh.de\">here</a>] for help!<br>";
		&info;
	}
	if((-e $dir) and (-d $dir)){
		&upgrade;
		exit;
	}
	print $conttype;
	print qq~
	<html>
	<head>
	$css
	<title>Free Perl Guestbook Version $version - Installation</title>
	</head>
	<body class="text" topmargin="20" leftmargin="0" bottommargin="0" rightmargin="0">
	<div align="center" class="title_7083c7"><b>FREE PERL GUESTBOOK $version - INSTALLATION</b></div><br>
	<form action="$myself" method="post" name="mylang">
	<table width="700" align="center" border="0" cellpadding="0" cellspacing="0" class="text">
	<tr>
	<td valign="top" align="right">
		<select name="lang" class="form-select-bold" onChange="this.form.submit();">
		<option value="">English
	~;
	foreach(@other_languages){
		my $selected = "";
		   $selected = "selected" if($mylang eq $_);
		print qq~
		<option value="$_" $selected>$alllangs{$_}
		~;
	}
	print qq~
		</select>
		<input type="submit" value=" &raquo; " class="form-submit">
	</td>
	</tr>
	</table>
	</form>
	<form action="$myself" method="post" name="frm" onSubmit="this.sbm.value=\'$ilang{'INSTALLING'}\'; this.sbm.disabled=true;">
	<table width="700" align="center" border="0" cellpadding="0" cellspacing="0" class="text">
	<tr>
	<td valign="top">
	~;
	my $old_owner = "";
	my $old_owner_email = "";
	my $old_owner_home = "";
	my $passblank = "";
	if((-e $olddir) and (-d $olddir)){
		$passblank = "<br><span class=\"small\">$ilang{'PASSWORD_BLANK'}</span>";
		print qq~
		<table width="100%" cellpadding="5" cellspacing="1" border="0" bgcolor="#7083c7" class="text">
		<tr><td valign="top" class="text-white"><b>$ilang{'OLDER_VERSION_DETECTED'}</b></td></tr>
		<tr>
		<td valign="top" bgcolor="white">
		~;
		if(-e "$olddir/guestbook.datei"){
			print qq~
			<input type="checkbox" name="copy_entries" id="copy_entries" checked> <label for="copy_entries" style="cursor: hand;">$ilang{'COPY_ENTRIES'}</label><br>
			~;
		}
		if(-e "$olddir/guestbook_bad.words"){
			print qq~
			<input type="checkbox" name="use_badwords" id="use_badwords" checked> <label for="use_badwords" style="cursor: hand;">$ilang{'USE_BADWORDS'}</label><br>
			~;
		}
		if(-e "$olddir/guestbook.lang"){
			open(DH, "<$olddir/guestbook.lang") or die "Cannot open $olddir/guestbook.lang! $!";
			my @temp = <DH>;
			close(DH);
			foreach(@temp){
				chomp;
				$old_owner = $1 if($_ =~ /^Owner_Name=\"(.*?)\"=$/);
				$old_owner_email = $1 if($_ =~ /^Owner_Email=\"(.*?)\"=$/);
			}
		}
		if(-e "$olddir/guestbook.style"){
			open(DH, "<$olddir/guestbook.style") or die "Cannot open $olddir/guestbook.style! $!";
			my @temp = <DH>;
			close(DH);
			foreach(@temp){
				chomp;
				$old_owner_home = $1 if($_ =~ /^Owner_Homepage=\"(.*?)\"=$/);
			}
		}
		print qq~
		<input type="checkbox" name="uninstall" id="uninstall"> <label for="uninstall" style="cursor: hand;">$ilang{'UNINSTALL_OLDGB'}</label><br>
		</td>
		</tr>
		</table>
		<br><br>
		~;
	}
	print qq~
	<table width="100%" cellpadding="5" cellspacing="1" border="0" bgcolor="#7083c7" class="text">
	<tr><td valign="top" class="text-white"><b>
	~;
	if(scalar @langs > 1){
		print $ilang{'MULTIPLE_LANGUAGE'};
	}
	else{
		print $ilang{'SINGLE_LANGUAGE'};
	}
	print qq~
	</b></td>
	</tr>
	<tr>
	<td valign="top" bgcolor="white">
	~;
	if(scalar @langs > 1){
		print "$ilang{'MULTIPLE_LANGUAGE_INFO'}<br><br>\n";
		print qq~
		<table cellpadding="2" cellspacing="1" border="0" width="100%" class="text" bgcolor="#e0e0e0">
		~;
		my $num = 0;
		my $cols = 4;
		my $rows = int((scalar @langs) / $cols);
		$rows++ if((scalar @langs) % $cols != 0);
		for(my $a = 0; $a < $rows; $a++){
			print "<tr>\n";
			for(my $b = 0; $b < $cols; $b++){
				my $langname = "\&nbsp;";
				my $lang = "";
				$lang = $langs[$num] if($langs[$num]);
				$lang =~ s/\.lang//i;
				$langname = $langs[$num] if($langs[$num]);
				$langname =~ s/\.lang//i;
				$langname = $alllangs{$langname};
				my $checkbox = "";
				$checkbox = "<input type=\"checkbox\" name=\"fpg_lang_$lang\" id=\"fpg_lang_$lang\" checked>" if($langs[$num]);
				print qq~
				<td valign="top" bgcolor="white">$checkbox <label for="fpg_lang_$lang" style="cursor: hand;">$langname</label></td>
				~;
				$num++;
			}
			print "</tr>\n";
		}
		print qq~
		</table>
		<br>
		<script type="text/javascript">
		function checklang(){
			var lang = document.frm.default_language.options[document.frm.default_language.selectedIndex].value;
			eval("document.frm.fpg_lang_"+lang+".checked = 1");
		}
		</script>
		$ilang{'DEFAULT_LANGUAGE'}
		<select name="default_language" class="form-select" onChange="checklang()">
		~;
		foreach (@langs){
			$_ =~ s/\.lang$//i;
			my $selected = "";
			$selected = "selected" if(lc $_ eq lc $ilang{'ID'});
			my $langname = $_;
			   $langname = $alllangs{$langname};
			print qq~
			<option value="$_" $selected>$langname
			~;
		}
		print qq~
		</select>
		~;
	}
	else{
		$langs[0] =~ s/\.lang//;
		my @temp;
		foreach(split(/_/, $langs[0])){
			push @temp, ucfirst $_;
		}
		$langs[0] = join(" ", @temp);
		print "$ilang{'SINGLE_LANGUAGE_INFO'} <b>$langs[0]</b><br><br>";
	}
	my $check_sendmail = "";
	my $check_smtp = "";
	if($^O =~ /win/i){
		$check_smtp = "checked";
	}
	else{
		$check_sendmail = "checked";
	}
	my $smtp_installed = "";
	   $smtp_installed = "(not installed)" if(!$net_smtp);
	   
	print qq~
		</td>
		</tr>
		</table>
		<br><br>
		<table width="100%" cellpadding="5" cellspacing="1" border="0" bgcolor="#7083c7" class="text">
		<tr><td valign="top" class="text-white"><b>$ilang{'CONFIGURATION'}</b></td></tr>
		<tr>
		<td valign="top" class="text" bgcolor="white">
			<table cellpadding="2" cellspacing="1" border="0" width="100%" class="text" bgcolor="#e0e0e0">
			<tr>
			<td valign="top" align="right" bgcolor="white"><span class="red-bold">&raquo;</span> $ilang{'YOUR_NAME'}</td>
			<td valign="top" bgcolor="white"><input type="text" name="owner" class="form-text" size="40" value="$old_owner"></td>
			</tr>
			<tr>
			<td valign="top" align="right" bgcolor="white"><span class="red-bold">&raquo;</span> $ilang{'YOUR_EMAIL'}</td>
			<td valign="top" bgcolor="white"><input type="text" name="owner_email" class="form-text" size="40" value="$old_owner_email"></td>
			</tr>
			<tr>
			<td valign="top" align="right" bgcolor="white">$ilang{'YOUR_HOME'}</td>
			<td valign="top" bgcolor="white"><input type="text" name="owner_home" class="form-text" size="40" value="$old_owner_home"></td>
			</tr>
			<tr>
			<td valign="top" align="right" bgcolor="white"><span class="red-bold">&raquo;</span> $ilang{'PASSWORD'}</td>
			<td valign="top" bgcolor="white">
				<input type="password" name="password" class="form-text" size="20">
			</td>
			</tr>
			<tr>
			<td valign="top" align="right" bgcolor="white">$ilang{'SENDMAIL_PATH'}</td>
			<td valign="top" bgcolor="white"><input type="text" name="sendmail_path" class="form-text" size="40" value="$default_sendmail"></td>
			</tr>
			<tr>
			<td valign="top" align="right" bgcolor="white">$ilang{'SMTP_SERVER'}</td>
			<td valign="top" bgcolor="white"><input type="text" name="smtp_server" class="form-text" size="40" value="$default_smtp"></td>
			</tr>
			<tr>
			<td valign="top" align="right" bgcolor="white">$ilang{'MAIL_METHOD'}</td>
			<td valign="top" bgcolor="white">
				<input type="radio" name="mail_method" value="sendmail" id="fpg_sendmail" $check_sendmail> <label for="fpg_sendmail" style="cursor: hand;">$ilang{'SENDMAIL'}</label>
				<input type="radio" name="mail_method" value="smtp" id="fpg_smtp" $check_smtp> <label for="fpg_smtp" style="cursor: hand;">$ilang{'SMTP'} $smtp_installed</label>
			</td>
			</tr>
			</table>
			<br>
			$ilang{'CONFIGURATION_MSG'}
		</td>
		</tr>
		</table>
		<br><br>
		<table width="100%" cellpadding="5" cellspacing="1" border="0" bgcolor="#7083c7" class="text">
		<tr><td valign="top" class="text-white"><b>$ilang{'CONDITION'}</b></td></tr>
		<tr>
		<td valign="top" class="text" bgcolor="white">
			<table cellpadding="2" cellspacing="1" border="0" width="100%" class="text" bgcolor="#e0e0e0">
			<tr>
			<td valign="top" bgcolor="white">
				$conditions
			</td>
			</tr>
			</table>
			<br>
			<input type="checkbox" name="agreement" id="agreement"> <label for="agreement" style="cursor: hand;">$ilang{'ACCEPT'}</label>
		</td>
		</tr>
		</table>
		<br><br>
		<table width="100%" cellpadding="5" cellspacing="1" border="0" bgcolor="#7083c7" class="text">
		<tr><td valign="top" class="text-white"><b>$ilang{'DELETE_FILES'}</b></td></tr>
		<tr>
		<td valign="top" class="text" bgcolor="white">
			<table cellpadding="2" cellspacing="1" border="0" width="100%" class="text" bgcolor="#e0e0e0">
			<tr>
			<td valign="top" bgcolor="white">
				$ilang{'FILES_TO_DELETE'}<br><br>
				<input type="checkbox" name="del_ascii" checked><b>fpg_ascii</b> $ilang{'AND_FILES_INSIDE'}<br>
				<input type="checkbox" name="del_binary" checked><b>fpg_binary</b> $ilang{'AND_FILES_INSIDE'}<br>
				<input type="checkbox" name="del_installer" checked><b>$ilang{'THE_INSTALLER'}</b><br>
			</td>
			</tr>
			</table>
		</td>
		</tr>
		</table>
		<br><br>
	<div align="center">
		<input type="hidden" name="action" value="install_now">
		<input type="hidden" name="lang" value="$mylang">
		<input type="submit" name="sbm" value="$ilang{'CONTINUE'}" class="form-submit">
		<input type="reset" name="res" value="$ilang{'RESET'}" class="form-reset">
	</div>
	</td>
	</tr>
	</table>
	</form>
	<center>$tdpn</center>
	</body>
	</html>
	~;
}
########### Install now #################################################
sub install_now{
	my @params = $cgi->param();
	my $owner = $cgi->param('owner');
	my $owner_home = $cgi->param('owner_home');
	my $owner_email = $cgi->param('owner_email');
	my $password = $cgi->param('password');
	my $sendmail_path = $cgi->param('sendmail_path');
	my $smtp_server = $cgi->param('smtp_server');
	my $mail_method = $cgi->param('mail_method');
	my $agreement = $cgi->param('agreement');
	my $del_ascii = $cgi->param('del_ascii');
	my $del_binary = $cgi->param('del_binary');
	my $del_installer = $cgi->param('del_installer');
	if(!$agreement){	
		my %ilang;
		open(DH, "<$ilang") or die "Cannot open the file $ilang! $!";
		while(<DH>){
			chomp;
			my($key, $value) = split(/\<==\>/, $_);
			$ilang{$key} = $value;
		}
		$info_title = "$ilang{'INSTALLATION_FAILED'}";
		$info = "$ilang{'NOT_ACCEPTED'}";
		$info .= "<br><br>[<a href=\"$myself?lang=$mylang\">$ilang{'GOBACK'}</a>]<br>";
		&info;
	}
	if(!$password){
		$info_title = "$ilang{'INSTALLATION_FAILED'}";
		$info = "$ilang{'ENTER_PASSWORD'}";
		$info .= "<br><br>[<a href=\"$myself?lang=$mylang\">$ilang{'GOBACK'}</a>]<br>";
		&info;
	}
	if(!$owner){
		$info_title = "$ilang{'INSTALLATION_FAILED'}";
		$info = "$ilang{'ENTER_OWNER'}";
		$info .= "<br><br>[<a href=\"$myself?lang=$mylang\">$ilang{'GOBACK'}</a>]<br>";
		&info;
	}
	if(!$owner_email){
		$info_title = "$ilang{'INSTALLATION_FAILED'}";
		$info = "$ilang{'ENTER_EMAIL'}";
		$info .= "<br><br>[<a href=\"$myself?lang=$mylang\">$ilang{'GOBACK'}</a>]<br>";
		&info;
	}
	my $copy_entries = $cgi->param('copy_entries');
	my $use_pass = $cgi->param('use_pass');
	
	my $use_badwords = $cgi->param('use_badwords');
	my $uninstall = $cgi->param('uninstall');
	my $default_language = $cgi->param('default_language');
	my @langs = grep /^fpg_lang_/, @params;
	my $langnum = @langs;
	foreach my $langtest (@langs){
		last if !$default_language;
		$langtest =~ s/^fpa_lang_//;
		$langnum-- if($langtest ne $default_language);
	}
	push @langs, $default_language if(!$langnum);
	
	mkdir($dir, 0755) or die "Cannot create guestbook folder $dir! $!";
	mkdir($asdir, 0755) or die "Cannot create language folder $asdir! $!";
	mkdir($langdir, 0755) or die "Cannot create language folder $langdir! $!";
	mkdir($skindir, 0755) or die "Cannot create skin folder $skindir! $!";
	mkdir($configs, 0755) or die "Cannot create configs folder $configs! $!";
	mkdir($restrictions, 0755) or die "Cannot create restrictions folder $restrictions! $!";
	if(!mkdir($publicdir, 0755)){
		$server_url = "http://www.vitinh.de";
	}
	mkdir($session, 0755) or die "Cannot create session folder $session! $!";
	
	######### Version information ###############
	open(DH, ">$dir/version.fpg") or die "Cannot open $dir/version.fpg! $!";
	print DH "$version\n";
	print DH $date;
	close(DH);
	
	######### Make config file ##################
	if(!$password){
		if($use_pass){
			open(DH, "< $olddir/guestbook.pass") or die "Cannot open $olddir/guestbook.pass! $!";
			my $oldpass = <DH>;
			close(DH);
			$password = $oldpass;
		}
		else{
			$password = $default_pass;
		}
	}
	else{
		$password = crypt($password, "td");
	}
	my $fpg_configs = qq(OWNER<==>$owner
		OWNER_EMAIL<==>$owner_email
		HOME<==>$owner_home
		PASSWORD<==>$password
		DEFAULT_LANGUAGE<==>$default_language
		LANGUAGE_BAR<==>yes
		MAIL_OWNER<==>no
		MAIL_GUEST<==>no
		SENDMAIL_PATH<==>$sendmail_path
		SMTP_SERVER<==>$smtp_server
		MAIL_METHOD<==>$mail_method<==> 'smtp' or 'sendmail'
		CENSOR<==>no
		ALLOW_HTML<==>no<==>'no' is strongly recommended!
		ICONS_URL<==>$server_url/fpg_public
		SMILEYS<==>yes
		FPG_CODE<==>yes
		FPG_SCRIPT<==>$server_url/fpg_public/fpg.js
		URL_FILTER<==>msg<==> 'no' or 'msg' or 'replace'
		WORD_FILTER<==>msg<==> 'no' or 'msg' or 'replace'
		IP_FILTER<==>msg<==> 'no' or 'msg' or 'ignore'
		FILTER_MODE<==>strict<==> 'normal' or 'strict'
		ANTISPAM_METHOD<==>$asmethod<==> 1: use GD-Captcha; 2: use my own pics
		VIETUNI<==>yes
		TIMEZONE<==>0
		MAX_ENTRIES_PP<==>10
		MAX_WORD_LENGTH<==>50
		MAX_ENTRY_LENGTH<==>3000
		NAVILENGTH<==>15
		UIP_LIFE<==>120<==> in seconds
		TIC_LIFE<==>120<==> in seconds
		ASC_LIFE<==>1200<==> in seconds
		SID_LIFE<==>3600<==> in seconds
		COOKIE_LIFE<==>+1y<==> (s,m,h,d,M,y)
		THANK_MSG<==>yes<==> 'yes' or 'no'
		LOGIN_CMD<==>login
		READONLY_MODE<==>off<==> 'on' or 'off'
	);
	$fpg_configs =~ s/\t//g;
	open(DH, ">$configs/general.fpg") or die "Cannot open $configs/general.fpg! $!";
	print DH $fpg_configs;
	close(DH);
	######### Move language files ###############
	foreach(@langs){
		$_ =~ s/^fpg_lang_//;
		# File::Copy works faster
		if($file_copy){
			copy("$libdir/$_.lang", "$langdir/$_.lang") or die "Cannot copy $libdir/$_.lang to $langdir! $!";
			if(-e "$libdir/$_.cap"){
				copy("$libdir/$_.cap", "$langdir/$_.cap") or die "Cannot copy $libdir/$_.cap to $langdir! $!";
			}
		}
		else{
			my $temp = "";
			open(DH, "< $libdir/$_.lang") or die "Cannot open $libdir/$_.lang! $!";
			my @lines = <DH>;
			close(DH);
			foreach(@lines){
				$temp .= $_;
			}
			open(DH, "> $langdir/$_.lang") or die "Cannot open $langdir/$_.lang! $!";
			print DH $temp;
			close(DH);
			if(-e "$libdir/$_.cap"){
				my $temp = "";
				open(DH, "< $libdir/$_.cap") or die "Cannot open $libdir/$_.cap! $!";
				my @lines = <DH>;
				close(DH);
				foreach(@lines){
					$temp .= $_;
				}
				open(DH, "> $langdir/$_.cap") or die "Cannot open $langdir/$_.cap! $!";
				print DH $temp;
				close(DH);
			}
		}
	}
	#&copyadminlang;
	######### Move Binary Files #################
	opendir(DH, $bindir) or die "Cannot opendir $bindir! $!";
	my @temp = grep !/^\.\.?$/, readdir(DH);
	closedir(DH);
	foreach(@temp){
		if($file_copy){
			if($_ !~ /^antispam_/){
				if((-e $publicdir) and (-d $publicdir)){
					copy("$bindir/$_", "$publicdir/$_") or die "Cannot copy $bindir/$_ to $publicdir! $!";
				}
			}
			else{
				copy("$bindir/$_", "$asdir/$_") or die "Cannot copy $bindir/$_ to $asdir! $!";
			}
		}
		else{
			my $targetdir = $asdir;
			   $targetdir = $publicdir if($_ !~ /^antispam_/);
			if(($_ =~ /^antispam_/) or (($_ !~ /^antispam_/) and (-e $publicdir) and (-d $publicdir))){
				open(DH,"<$bindir/$_") or die "Cannot open $bindir/$_! $!";
				open(FH,">$targetdir/$_") or die "Cannot open $targetdir/$_! $!";
				binmode(DH);
				binmode(FH);
				while(<DH>){
					print FH;
				}
				close(DH);
				close(FH);
			}
		}
	}
	# Rename all anti-spam codes from antispam_code.gif to code.gif
	opendir(DH, $asdir) or die "Cannot opendir $asdir! $!";
	my @astemp = grep !/^\.\.?$/, readdir(DH);
	closedir(DH);
	foreach(@astemp){
		my $newname = $_;
		   $newname =~ s/^antispam_//;
		rename("$asdir/$_","$asdir/$newname") or die "Cannot rename $asdir/$_ to $asdir/$newname! $!";
	}
	######### Move ASCII Files ##################
	opendir(DH, $libdir) or die "Cannot opendir $libdir! $!";
	my @tempa = readdir(DH);
	closedir(DH);
	my @tpls = grep /\.tpl$/, @tempa;
	my @js = grep /\.js$/, @tempa;
	my @fpgs = grep /\.fpg$/, @tempa;
	# Templates -> Skin folder
	foreach(@tpls){
		if($file_copy){
			copy("$libdir/$_", "$skindir/$_") or die "Cannot copy $libdir/$_ to $skindir! $!";
		}
		else{
			my $temp = "";
			open(DH, "< $libdir/$_") or die "Cannot open $libdir/$_! $!";
			my @lines = <DH>;
			close(DH);
			foreach(@lines){
				$temp .= $_;
			}
			open(DH, "> $skindir/$_") or die "Cannot open $skindir/$_! $!";
			print DH $temp;
			close(DH);
		}
	}
	# Javascripts -> Public folder
	if((-e $publicdir) and (-d $publicdir)){
		foreach(@js){
			if($file_copy){
				copy("$libdir/$_", "$publicdir/$_") or die "Cannot copy $libdir/$_ to $publicdir! $!";
			}
			else{
				my $temp = "";
				open(DH, "< $libdir/$_") or die "Cannot open $libdir/$_! $!";
				my @lines = <DH>;
				close(DH);
				foreach(@lines){
					$temp .= $_;
				}
				open(DH, "> $publicdir/$_") or die "Cannot open $publicdir/$_! $!";
				print DH $temp;
				close(DH);
			}
		}
	}
	# Configs files -> Configs folder
	foreach(@fpgs){
		my $thefile = "$libdir/$_";
		   $thefile = "$libdir/other_languages/$mylang/$_" if($mylang and (($_ eq "install.fpg") or ($_ eq "license.fpg")));
		if($file_copy){
			copy($thefile, "$configs/$_") or die "Cannot copy $thefile to $configs! $!";
		}
		else{
			my $temp = "";
			open(DH, "< $thefile") or die "Cannot open $thefile! $!";
			my @lines = <DH>;
			close(DH);
			foreach(@lines){
				$temp .= $_;
			}
			open(DH, "> $configs/$_") or die "Cannot open $configs/$_! $!";
			print DH $temp;
			close(DH);
		}
	}
	
	######### Copy old entries ##################
	if($copy_entries){
		open(DH, "< $olddir/guestbook.datei") or die "Cannot open $olddir/guestbook.datei! $!";
		my @lines = <DH>;
		close(DH);
		my $temp = "";
		foreach(@lines){
			my @nums = ("a"	.. "z",	"1" .. "9");
			my $random_num = join ("", @nums[map{rand @nums}(1 .. 8)]);
			chomp;
			my @fields = split(/\|/, $_);
			foreach(@fields){
				$_ =~ s/--b-r-e-a-k--/\[newline\]/g;
				$_ =~ s/\%\&\&2/\&\#34\;/g;
				$_ =~ s/\%\&\&3/\&\#124\;/g;
			}
			$temp .= "ID<==>$fields[3]-$random_num\n";
			$temp .= "IP<==>$fields[7]\n";
			$temp .= "FPG_SHOW<==>yes\n";
			$temp .= "FPG_NAME<==>$fields[1]\n";
			$temp .= "FPG_EMAIL<==>$fields[4]\n" if($fields[4]);
			$temp .= "FPG_HOMEPAGE<==>$fields[11]\n" if($fields[11]);
			$temp .= "FPG_LOCATION<==>$fields[5]\n" if($fields[5]);
			$temp .= "FPG_MESSAGE<==>$fields[2]\n";
			$temp .= "FPG_REPLY<==>$fields[6]\n" if($fields[6]);
			$temp .= "\n";
		}
		open(DH, "> $dir/entries.fpg") or die "Cannot open $dir/entries.fpg! $!";
		print DH $temp;
		close(DH);
	}
	else{
		open(DH, "> $dir/entries.fpg") or die "Cannot open $dir/entries.fpg! $!";
		print DH "";
		close(DH);
	}
	######### Keep old bad words list ###########
	my $badwords = "$libdir/badwords.fpg";
	if($use_badwords){
		$badwords = "$olddir/guestbook_bad.words";
	}
	if(-e $badwords){
		if($file_copy){
			copy("$badwords", "$restrictions/badwords.fpg") or die "Cannot copy $badwords $!";
		}
		else{
			open(DH, "< $badwords") or die "Cannot open $badwords! $!";
			my @words = <DH>;
			close(DH);
			open(DH, "> $restrictions/badwords.fpg") or die "Cannot open $restrictions/badwords.fpg! $!";
			my $temp = "# FPG won't accept the words below:\n";
			foreach(@words){
				$temp .= $_;
			}
			print DH $temp;
			close(DH);
		}
	}
	else{
		open(DH, "> $restrictions/badwords.fpg") or die "Cannot open $restrictions/badwords.fpg! $!";
		my $temp = "# FPG won't accept the words below:\n";
		print DH $temp;
		close(DH);
	}
	######### Create Banned IPs file ############
	open(DH, ">$restrictions/badips.fpg") or die "Cannot open $restrictions/badips.fpg! $!";
	print DH "# FPG will ignore all users whose IP begins with:\n";
	close(DH);
	######### Create Banned URLs file ###########
	open(DH, ">$restrictions/badurls.fpg") or die "Cannot open $restrictions/badurls.fpg! $!";
	print DH "# FPG will ignore all URLs that contain one of the key words below:\n";
	close(DH);
	######### Create Smileys List ###############
	open(DH, ">$configs/smileys.fpg") or die "Cannot open $configs/smileys.fpg! $!";
	my $smileys_list = qq(smile<==>\$\$ICONS_URL\$\$/smileys_smile.gif
		wink<==>\$\$ICONS_URL\$\$/smileys_wink.gif
		sad<==>\$\$ICONS_URL\$\$/smileys_sad.gif
		mad<==>\$\$ICONS_URL\$\$/smileys_mad.gif
		biggrin<==>\$\$ICONS_URL\$\$/smileys_biggrin.gif
		confused<==>\$\$ICONS_URL\$\$/smileys_confused.gif
		tongue<==>\$\$ICONS_URL\$\$/smileys_tongue.gif
		redface<==>\$\$ICONS_URL\$\$/smileys_redface.gif
		rolleyes<==>\$\$ICONS_URL\$\$/smileys_rolleyes.gif
		eek<==>\$\$ICONS_URL\$\$/smileys_eek.gif
		thumbup<==>\$\$ICONS_URL\$\$/smileys_thumbup.gif
		thumbdown<==>\$\$ICONS_URL\$\$/smileys_thumbdown.gif
		lamp<==>\$\$ICONS_URL\$\$/smileys_lamp.gif
		question<==>\$\$ICONS_URL\$\$/smileys_question.gif);
	$smileys_list =~ s/\t//g;
	print DH "$smileys_list\n";
	close(DH);
	######### Chmod all files ###################
	my @dirs = ($publicdir, $configs, $langdir, $restrictions, $session, $skindir);
	my $chmoded = 0;
	foreach(@dirs){
		my $temp = $_;
		next if(!(-e $temp and -d $temp));
		opendir(DH, $_) or die "Cannot opendir $_! $!";
		my @all = grep !/^\.\.?$/, readdir(DH);
		closedir(DH);
		foreach(@all){
			chmod(0755, "$temp/$_") or die "Cannot chmod $temp/$_! $!";
			$chmoded++;
		}
	}
	######### Uninstall old GB ##################
	if($uninstall){
		opendir(DH, $olddir) or die "Cannot opendir $olddir! $!";
		my @all = grep !/^\.\.?$/, readdir(DH);
		closedir(DH);
		foreach(@all){
			unlink "$olddir/$_" or die "Cannot delete $olddir/$_! $!";
		}
		rmdir $olddir or die "Cannot remove dir $olddir! $!";
	}
	
	if($del_ascii){
		&delme("folder","fpg_ascii");
	}
	if($del_binary){
		&delme("folder","fpg_binary");
	}
	if($del_installer){
		my $name = $myself;
		   $name =~ s/\\/\//g;
		my @array = split(/\//, $name);
		my $myname = pop @array;
		open(DH,">$dir/delete_installer.txt") or die "Cannot open delete_installer.txt! $!";
		print DH $myname;
		close(DH);
	}
		
		
	$info_title = "FREE PERL GUESTBOOK $version - INSTALLATION";
	$info_color = "#7083c7";
	$info = "$ilang{'INSTALLED'}<br><br>\n";
	$info .= "&raquo; <a href=\"$guestbook\">$ilang{'VIEW_GUESTBOOK'}</a>\n";
	&info;
}
########### Upgrade #####################################################
sub upgrade{
	my $upgrade = 0;
	my $oversion = "";
	my $odate = "";
	if(!-e "$dir/version.fpg"){
		$upgrade = 1;
	}
	else{
		open(DH, "<$dir/version.fpg") or die "Cannot open $dir/version.fpg! $!";
		my @info = <DH>;
		close(DH);
		$oversion = $info[0];
		$oversion =~ s/\n//g;
		$oversion =~ s/\r//g;
		$odate = $info[1];
		$odate =~ s/\n//g;
		$odate =~ s/\r//g;
		if($odate < $date){
			$upgrade = 1;
		}
	}
	if($upgrade){
		my @nums = ("1"	.. "9");
		my $num = join ("", @nums[map{rand @nums}(1 .. 4)]);
		my $odir = $dir . "_" . $num;
		rename($dir,$odir) or die "Cannot rename $dir to $odir! $!";
		
		mkdir($dir, 0755) or die "Cannot create guestbook folder $dir! $!";
		mkdir($asdir, 0755) or die "Cannot create language folder $asdir! $!";
		mkdir($langdir, 0755) or die "Cannot create language folder $langdir! $!";
		mkdir($skindir, 0755) or die "Cannot create skin folder $skindir! $!";
		mkdir($configs, 0755) or die "Cannot create configs folder $configs! $!";
		mkdir($restrictions, 0755) or die "Cannot create restrictions folder $restrictions! $!";
		mkdir($session, 0755) or die "Cannot create session folder $session! $!";
		
		######### Version information ###############
		open(DH, ">$dir/version.fpg") or die "Cannot open $dir/version.fpg! $!";
		print DH "$version\n";
		print DH $date;
		close(DH);
		
		######### Copy old files ####################
		my @dirlist = ("skins","configs","restrictions","session");
		push @dirlist, "antispam" if(-d "$odir/antispam");
		foreach my $subdir (@dirlist){
			opendir(DH,"$odir/$subdir") or die "Cannot open $odir/$subdir! $!";
			my @files = grep !/^\.\.?$/, readdir(DH);
			closedir(DH);
			foreach my $file (@files){
				if($file_copy){
					copy("$odir/$subdir/$file","$dir/$subdir/$file") or die "Cannot copy $file from $odir/$subdir to $dir/$subdir! $!";
				}
				else{
					my $temp = "";
					open(DH, "< $odir/$subdir/$file") or die "Cannot open $odir/$subdir/$file! $!";
					my @lines = <DH>;
					close(DH);
					foreach(@lines){
						$temp .= $_;
					}
					open(DH, "> $dir/$subdir/$file") or die "Cannot open $dir/$subdir/$file! $!";
					print DH $temp;
					close(DH);
				}
			}
		}
		# entries
		if($file_copy){
			copy("$odir/entries.fpg","$dir/entries.fpg") or die "Cannot copy entries.fpg from $odir to $dir! $!";
		}
		else{
			my $temp = "";
			open(DH, "< $odir/entries.fpg") or die "Cannot open $odir/entries.fpg! $!";
			my @lines = <DH>;
			close(DH);
			foreach(@lines){
				$temp .= $_;
			}
			open(DH, "> $dir/entries.fpg") or die "Cannot open $dir/entries.fpg! $!";
			print DH $temp;
			close(DH);
		}
		######### Copy new files ##################
		if((!$oversion) or ($oversion eq "200")){
			unlink "$skindir/style.tpl" or die "Cannot delete $skindir/style.tpl! $!";
			unlink "$skindir/sign.tpl" or die "Cannot delete $skindir/sign.tpl! $!";
			unlink "$configs/inputs.fpg" or die "Cannot delete $configs/inputs.fpg! $!";
			
			if($file_copy){
				copy("$libdir/style.tpl","$skindir/style.tpl");
				copy("$libdir/sign.tpl","$skindir/sign.tpl");
				copy("$libdir/inputs.fpg","$configs/inputs.fpg");
			}
			else{
				my $temp1 = "";
				open(DH, "< $libdir/style.tpl") or die "Cannot open $libdir/style.tpl! $!";
				my @lines1 = <DH>;
				close(DH);
				foreach(@lines1){
					$temp1 .= $_;
				}
				open(DH, "> $skindir/style.tpl") or die "Cannot open $skindir/style.tpl! $!";
				print DH $temp1;
				close(DH);
				
				my $temp2 = "";
				open(DH, "< $libdir/sign.tpl") or die "Cannot open $libdir/sign.tpl! $!";
				my @lines2 = <DH>;
				close(DH);
				foreach(@lines2){
					$temp2 .= $_;
				}
				open(DH, "> $skindir/sign.tpl") or die "Cannot open $skindir/sign.tpl! $!";
				print DH $temp2;
				close(DH);
				
				my $temp3 = "";
				open(DH, "< $libdir/inputs.fpg") or die "Cannot open $libdir/inputs.fpg! $!";
				my @lines3 = <DH>;
				close(DH);
				foreach(@lines3){
					$temp3 .= $_;
				}
				open(DH, "> $configs/inputs.fpg") or die "Cannot open $configs/inputs.fpg! $!";
				print DH $temp3;
				close(DH);
			}
		}
		# Copy language files
		opendir(DH,$libdir) or die "Cannot open $libdir! $!";
		my @langs = grep /\.lang$/, readdir(DH);
		closedir(DH);
		foreach my $lang (@langs){
			if($file_copy){
				copy("$libdir/$lang","$langdir/$lang") or die "Cannot copy $lang from $libdir to $langdir! $!";
			}
			else{
				my $temp = "";
				open(DH, "< $libdir/$lang") or die "Cannot open $libdir/$lang! $!";
				my @lines = <DH>;
				close(DH);
				foreach(@lines){
					$temp .= $_;
				}
				open(DH, "> $langdir/$lang") or die "Cannot open $langdir/$lang! $!";
				print DH $temp;
				close(DH);
			}
		}
		# Copy the right admin-language
		my %adminlang = &language("$configs/install.fpg");
		&copyadminlang($adminlang{'ID'});
		# Copy anti-spam codes
		if(!-d "$odir/antispam"){
			if($file_copy){
				opendir(DH, $bindir) or die "Cannot opendir $bindir! $!";
				my @temp = grep /^antispam_/, readdir(DH);
				closedir(DH);
				foreach(@temp){
					copy("$bindir/$_", "$asdir/$_") or die "Cannot copy $bindir/$_ to $asdir! $!";
				}
			}
			else{
				opendir(DH, $bindir) or die "Cannot opendir $bindir! $!";
				my @temp = grep /^antispam_/, readdir(DH);
				closedir(DH);
				foreach(@temp){
					open(DH,"<$bindir/$_") or die "Cannot open $bindir/$_! $!";
					open(FH,">$asdir/$_") or die "Cannot open $asdir/$_! $!";
					binmode(DH);
					binmode(FH);
					while(<DH>){
						print FH;
					}
					close(DH);
					close(FH);
				}
			}
			# Rename all anti-spam codes from antispam_code.gif to code.gif
			opendir(DH, $asdir) or die "Cannot opendir $asdir! $!";
			my @astemp = grep !/^\.\.?$/, readdir(DH);
			closedir(DH);
			foreach(@astemp){
				my $newname = $_;
				   $newname =~ s/^antispam_//;
				rename("$asdir/$_","$asdir/$newname") or die "Cannot rename $asdir/$_ to $asdir/$newname! $!";
			}
		}
		# Edit the config file
		open(DH, "< $configs/general.fpg") or die "Cannot open $configs/general.fpg! $!";
		my @general = <DH>;
		close(DH);
		my $generalfile = "";
		my $uip = 0;
		my $asc = 0;
		my $rom = 0;
		my $cmd = 0;
		my $asm = 0;
		foreach(@general){
			$_ =~ s/\r?\n?//g;
			$generalfile .= "$_\n";
			if($_ =~ /^ASC_LIFE\<\=\=\>/){
				$asc++;
			}
			if($_ =~ /^UIP_LIFE\<\=\=\>/){
				$uip++;
			}
			if($_ =~ /^READONLY_MODE\<\=\=\>/){
				$rom++;
			}
			if($_ =~ /^LOGIN_CMD\<\=\=\>/){
				$cmd++;
			}
			if($_ =~ /^ANTISPAM_METHOD\<\=\=\>/){
				$asm++;
			}
		}
		if(!$asc){
			$generalfile .= "ASC_LIFE<==>1200<==>in seconds\n";
		}
		if(!$uip){
			$generalfile .= "UIP_LIFE<==>0<==>in seconds\n";
		}
		if(!$rom){
			$generalfile .= "READONLY_MODE<==>off<==> 'on' or 'off'\n";
		}
		if(!$cmd){
			$generalfile .= "LOGIN_CMD<==>login\n";
		}
		if(!$asm){
			if($gd){
				$generalfile .= "ANTISPAM_METHOD<==>1<==> 1: use GD-Captcha; 2: use my own pics\n";
			}
			else{
				$generalfile .= "ANTISPAM_METHOD<==>2<==> 1: use GD-Captcha; 2: use my own pics\n";
			}
		}
		if(!$asc or !$rom or !$cmd or !$uip or !$asm){
			open(DH, "> $configs/general.fpg") or die "Cannot open $configs/general.fpg! $!";
			print DH $generalfile;
			close(DH);
		}
		# Delele installation files
		if($update_and_del){
			&delme("folder","fpg_ascii");
			&delme("folder","fpg_binary");
			my $name = $myself;
			   $name =~ s/\\/\//g;
			my @array = split(/\//, $name);
			my $myname = pop @array;
			open(DH,">$dir/delete_installer.txt") or die "Cannot open delete_installer.txt! $!";
			print DH $myname;
			close(DH);
		}
		my %newlang = &language("$configs/install.fpg");
		$info_title = "FREE PERL GUESTBOOK $version - INSTALLATION";
		$info_color = "#7083c7";
		$info = "$newlang{'UPGRADED'} <b>$version</b><br><br>\n";
		$info .= "&raquo; <a href=\"$guestbook\">$newlang{'VIEW_GUESTBOOK'}</a>\n";
		&info;
	}
	else{
		print $conttype;
		print qq~
		<html>
		<head>
		$css
		<title>Free Perl Guestbook Version $version</title>
		</head>
		<body topmargin="20" leftmargin="20" class="text">
		FPG already installed!
		~;
		if(-e "fpg.cgi"){
			print qq~
				<script type="text/javascript">
					setTimeout("location.href = 'fpg.cgi'", 3000);
				</script>
			~;
		}
		print qq~
		</body>
		</html>
		~;
		
		if($update_and_del){
			&delme("folder","fpg_ascii");
			&delme("folder","fpg_binary");
			my $name = $myself;
			   $name =~ s/\\/\//g;
			my @array = split(/\//, $name);
			my $myname = pop @array;
			open(DH,">$dir/delete_installer.txt") or die "Cannot open delete_installer.txt! $!";
			print DH $myname;
			close(DH);
		}
	}
}
########### Get language ################################################
sub language{
	open(DH, "<$_[0]") or die "Cannot open $_[0]! $!";
	my @temp = <DH>;
	close(DH);
	my %language;
	foreach(@temp){
		$_ =~ s/\n//g;
		$_ =~ s/\r//g;
		next if(($_ =~ /^\#/) or (!$_));
		my($key,$value) = split(/\<==\>/, $_);
		$language{$key} = $value if(defined $key);
	}
	return %language;
}
########### Get Date ####################################################
sub getdate{
	my ($sec, $min, $std, $tag, $mon, $jahr) = localtime(time);
	$sec  = &formatdate($sec);
	$min  = &formatdate($min);
	$std  = &formatdate($std);
	$tag  = &formatdate($tag);
	$mon  = &formatdate(++$mon);
	$jahr = $jahr + 1900;
	my $timeinsec = timelocal($sec,$min,$std,$tag,$mon-1,$jahr-1900);
	return $jahr if($_[0] eq "year");
}
sub formatdate{
	if(length ($_[0]) <2){return "0".$_[0];}
	else{return $_[0];}
}

########### Delete files ################################################
sub delme{
	my $tmp = "";
	my $mode = shift;
	my $todel = shift;
	if($mode eq "file"){
		unlink $todel or die "Cannot delete $todel! $!";
	}
	else{
		my @mydirs = reverse sort &getdirs($todel);
		foreach my $mydir (@mydirs){
			opendir(DH, $mydir) or die "Cannot open $mydir! $!";
			my @files = grep !/^\.\.?$/, readdir(DH);
			foreach my $myfile (@files){
				unlink "$mydir/$myfile" or die "Cannot delete $mydir/$myfile! $!";
			}
			closedir(DH);
			rmdir $mydir or die "Cannot delete $mydir! $!";
		}
		opendir(DH, $todel) or die "Cannot open $todel! $!";
		my @xfiles = grep !/^\.\.?$/, readdir(DH);
		foreach my $myxfile (@xfiles){
			unlink "$todel/$myxfile" or die "Cannot delete $todel/$myxfile! $!";
		}
		closedir(DH);
		rmdir $todel or die "Cannot delete $todel! $!";
	}
	@alldirs = ();
}
########### List Dirs ###################################################
sub getdirs{
	my $dir = shift;
	   $dir =~ s/\\/\//g;
	opendir(DH, $dir);
	my @all = grep !/^\.\.?$/, readdir(DH);
	closedir(DH);
	foreach(@all){
		if(-d "$dir/$_"){
			push @alldirs, "$dir/$_";
			&getdirs("$dir/$_");
		}
	}
	return @alldirs;
}
########### install.fpg #################################################
sub copyadminlang{
	my $myid = shift;
	my $thefile = "$libdir/install.fpg";
	   $thefile = "$libdir/other_languages/$myid/install.fpg" if($myid and (-e "$libdir/other_languages/$myid/install.fpg"));
	if($file_copy){
		copy($thefile, "$configs/$_") or die "Cannot copy $thefile to $configs! $!";
	}
	else{
		my $temp = "";
		open(DH, "< $thefile") or die "Cannot open $thefile! $!";
		my @lines = <DH>;
		close(DH);
		foreach(@lines){
			$temp .= $_;
		}
		open(DH, "> $configs/$_") or die "Cannot open $configs/$_! $!";
		print DH $temp;
		close(DH);
	}
}
########### Inform user #################################################
sub info{
	print $conttype;
	print qq~
	<html>
	<head>
	$css
	<title>$info_title</title>
	</head>
	<body topmargin="0" leftmargin="0" class="text">
	<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0" class="text">
	<tr>
	<td valign="middle" align="center">
		<table bgcolor="$info_color" width="600" cellpadding="5" cellspacing="1" border="0" class="text">
		<tr><td valign="top" class="title">$info_title</td></tr>
		<tr bgcolor="white"><td valign="top">$info</td></tr>
		</table>
	</td>
	</tr>
	<tr>
	<td valign="bottom" align="center">$tdpn</td>
	</tr>
	</table>
	</body>
	</html>
	~;
	exit;
}

