###RUN_PERL: #!/usr/bin/perl

# /bsta/2words
# 2words is generated from 2words.1.pl.
#
# The wordgame interface
#
# Copyright (C) 2016 - 2017, 2023  Balthasar Szczepański
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
#use warnings;
###PERL_LIB: use lib /botm/lib/bsta
use bsta_lib qw(failpage gethttpheader getcgi entityencode readdatafile writedatafile urlencode bb2ht);
use  File::Copy;

###PERL_CGI_PATH:           CGI_PATH           = /bsta/
###PERL_CGI_2WORDS_PATH:    CGI_2WORDS_PATH    = /bsta/2words
###PERL_CGI_CSS_PATH:       CGI_CSS_PATH       = /bsta/bsta.css
###PERL_CGI_LOGO_PATH:      CGI_LOGO_PATH      = /bsta/botmlogo.png
###PERL_CGI_VIEWER_PATH:    CGI_VIEWER_PATH    = /bsta/v

###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta/
###PERL_DATA_COIN_PATH:     DATA_COIN_PATH     = /botm/data/bsta/coincidence
###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
###PERL_DATA_LIST_PATH:     DATA_LIST_PATH     = /botm/data/bsta/list
###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
###PERL_DATA_STORY_PATH:    DATA_STORY_PATH    = /botm/data/bsta/story

###PERL_WWW_PATH:           WWW_PATH           = /botm/www/1190/bsta/
###PERL_WWW_INDEX_PATH:     WWW_INDEX_PATH     = /botm/www/1190/bsta/index.htm

###PERL_WEBSITE:            WEBSITE            = 1190.bicyclesonthemoon.info
###PERL_WEBSITE_NAME:       WEBSITE_NAME       = Bicycles on the Moon
###PERL_FAVICON_PATH:       FAVICON_PATH       = /img/favicon.png

###PERL_COIN_DATE:          COIN_DATE          = 13-Nov-2016 22:15
###PERL_INTF_DATE:          INTF_DATE          = 28-Sep-2016 20:34

###PERL_STORY_LENGTH:       STORY_LENGTH       = 16
###PERL_PAGE_LENGTH:        PAGE_LENGTH        = 16
###PERL_FIRSTPAGE_LENGTH:   FIRSTPAGE_LENGTH   = 4


my %http;
my %cgi;
my %story;
my %newstory;
my %settings;
my %state;
my %gotolist;

my $time = time();
srand ($time-$$);

my $method;
my $IP;
my $words;
my $color2;
my $lastip;
my $storyid;
my $turn;
my $message;
my $firstletter;
my $secondletter;
my $intfstate;
my $intfpass;
my $intfpause;
my $intfmode;
my $storypath;
my $storyfile;
my $storylock;
my @storylines;
my $ongstate;
my $page;


delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;

if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
	$method=$1;
}
else{
	exit failpage("Status: 405 Method Not Allowed\nAllow: GET, POST, HEAD\n","405 Method Not Allowed","The interface does not support the $ENV{'REQUEST_METHOD'} method.",$method);
}

%http = gethttpheader (\%ENV);
%cgi = getcgi($ENV{'QUERY_STRING'});

if ($method eq 'POST') {
	if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
		my %cgipost=getcgi( <STDIN> );
		foreach my $ind (keys %cgipost) {
			$cgi{$ind}=$cgipost{$ind};
		}
	}
	# multipart not supported
	else{
		exit failpage("Status: 415 Unsupported Media Type\n","415 Unsupported Media Type","Unsupported Content-type: $http{'content-type'}.");
	}
}

if ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
	$page=int($1);
}
else {
	$page=0;
}

if ($ENV{'HTTP_X_FORWARDED_FOR'} =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/) {
	$IP=$1;
}
elsif ($ENV{'REMOTE_ADDR'} =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/) {
	$IP=$1;
}
else {
	$IP='0.0.0.0';
}

if ($cgi{'words'} ne '') {
	$words=$cgi{'words'};
}

%settings=readdatafile(DATA_SETTINGS_PATH);
%state=readdatafile(DATA_STATE_PATH);
$ongstate=int($state{'state'});

$storylock=0;
if (open ($storyfile,"+<",DATA_STORY_PATH)){
	$storylock=1;
	if (flock($storyfile,2)) {
		$storylock=2;
	}
	%story=readdatafile($storyfile);
	
	if ($story{'lastip'} =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/) {
		$lastip=$1;
	}
	else {
		$lastip='0.0.0.0';
	}
	
	$storyid = int($story{'id'});
	$intfpass = int($story{'pass'});
	$intfstate = int($story{'state'});
	$intfmode = $intfstate;
	$intfpause = $intfstate & 1;
	if ($intfpause) {
		$intfmode -= 1;
	}
	
	if ($IP ne $lastip) {
		$turn = 1;
	}
	else {
		$turn = 0	;
	}
	
	if($words =~ /^bstaaaclear(all)?$/ || $intfstate < 0) {
		if($words eq 'bstaaaclearall' || $intfstate < -1) {
			$story{'id'}=0;
		}
		$story{'content'}='';
		$story{'lastip'}='0.0.0.0';
		$story{'letter'}='';
		$story{'pass'}='0';
		$story{'state'}='0';
		$turn=0;
		if($ongstate == 0) {
			writeindex(WWW_INDEX_PATH,0,0,0);
		}
		writedatafile($storyfile,%story);
	}
	
	if ($words ne '') {
		if (!$turn) {
			$message = "It's not your turn.";
		}
		else {
			if ($words =~ /^([!"\(\),\.:;\?][ \t]*)?([A-Za-z][A-Za-z'\-]*[A-Za-z']?)([!"\(\),\.:;\? \t][ \t]*)([A-Za-z][A-Za-z'\-]*[A-Za-z']?)([!"\(\),\.:;\?]?[ \t]*)$/) {
				
				$firstletter = lc(substr($2,0,1));
				$secondletter = lc(substr($4,0,1));
				if (($firstletter ne $story{'letter'})&&($story{'letter'} ne '')) {
					$message = 'The first word must start with '.uc($story{'letter'}).'.';
				}
				elsif ($firstletter eq $secondletter) {
					$message = 'The second word can\'t also start with '.uc($firstletter).'.';
				}
				else {
					$story{'content'} = $story{'content'} . $words."\n";
					$turn = 0;
					$story{'lastip'} = $IP;
					$story{'letter'} = $secondletter;
					
					if ($cgi{'next'} ne '') {
						if (split(/\r?\n/,$story{'content'}) >= (STORY_LENGTH-1)) {
							$storypath = DATA_STORY_PATH.$storyid;
							writedatafile($storypath,%story);
							$newstory{'id'} = $storyid + 1;
							$newstory{'letter'}='';
							$newstory{'lastip'}=$IP;
							$newstory{'content'}='';
							$newstory{'pass'}='0';
							$newstory{'state'}='0';
							$intfstate=0;
							$intfpass=0;
							$intfmode=0;
							$intfpause=0;
							if($ongstate == 0) {
								writeindex(WWW_INDEX_PATH,0,0,0);
							}
							writedatafile($storyfile,%newstory);
						}
						else {
							$message = 'To early to finish this wordgame.';
							writedatafile($storyfile,%story);
						}
					}
					else {
						if ($intfpass == 1){
							$intfpass = 2;
							$story{'pass'} = '2';
							if($ongstate == 0) {
								writeindex(WWW_INDEX_PATH,2,0,0);
							}
						}
						elsif(lc($2).' '.lc($4) eq $settings{'unlock'}) {
							if ($intfpass == 0) {
								if($ongstate == 0) {
									my %framedata = readdatafile(DATA_PATH.0);
									my %default = readdatafile(DATA_DEFAULT_PATH);
									my $inpath;
									my $outpath;
									
									$framedata{'ongtime'} = $time;
									$gotolist{'title-0'} = $framedata{'title'};
									$gotolist{'ongtime-0'} = $framedata{'ongtime'};
									writedatafile(DATA_PATH.0,%framedata);
									writedatafile(DATA_LIST_PATH,%gotolist);
									
									foreach my $ind (keys %default) {
										unless(defined($framedata{$ind})){
											$framedata{$ind}=$default{$ind};
										}
									}
									
									$inpath = DATA_PATH.sprintf($settings{'frame'},0,$framedata{'ext'});
									$outpath = WWW_PATH.sprintf($settings{'frame'},0,$framedata{'ext'});
									
									if(copy ($inpath, $outpath)) {
										$intfpass = 1;
										$intfstate = 0;
										$intfmode=0;
										$intfpause=0;
										$story{'pass'} = '1';
										$story{'state'} = '0';
										writeindex(WWW_INDEX_PATH,1,0,0);
									}
								}
								else {
									$message = "???";
								}
							}
							else {
								$message = 'The password has already been used in this story.';
							}
						}
						writedatafile($storyfile,%story);
					}
				}
			}
			else {
				$message = 'Please, two words, not more, not less (some punctuation is allowed).';
			}
		}
	}
	elsif (($cgi{'s'} ne '') && ($intfpass==1) && ($ongstate == 0)) {
		$intfstate = int($cgi{'s'});
		if($intfstate > 63 || $intfstate <0) {
			$intfstate = 0;
		}
		$intfmode = $intfstate;
		$intfpause = $intfstate & 1;
		if ($intfpause) {
			$intfmode -= 1;
		}
		$story{'state'} = $intfstate;
		writeindex(WWW_INDEX_PATH,1,$intfmode,$intfpause);
		writedatafile($storyfile,%story);
	}
	@storylines = split(/\r?\n/,$story{'content'});
	if(@storylines & 1) {
		$turn = !$turn;
	}
	
	close($storyfile);
}

print "Content-type: text/html\n\n";
if($method eq 'HEAD') {
	exit;
}

print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">'."\n";
print '<html lang="en"><head>'."\n";
print '<title>Two words &bull; '.WEBSITE_NAME.'</title>'."\n";
print '<meta http-equiv="Content-type" content="text/html; charset=UTF-8">'."\n";
print '<link rel="icon" type="image/png" href="'.FAVICON_PATH.'">'."\n";
print '<link rel="stylesheet" href="'.CGI_CSS_PATH.'">'."\n";
print '</head><body>'."\n";
print '<a href="/"><img id="botmlogo" src="'.CGI_LOGO_PATH.'" alt="'.WEBSITE.'"></a>'."\n";
print '<div id="all">'."\n";

print '<div id="inst" class="ins">'."\n";

print '<div id="title">'."\n";
print '<H1 id="titletext">Two words</H1>'."\n";
print '</div>'."\n";

if ($page == 0) {
	print '<div id="storypuzzle">'."\n";
	for (my $i = 0; $i < @storylines; ++$i){
		print '<span class="'.($turn?'ni':'br').'">'.entityencode($storylines[$i]).'</span>'."\n";
		$turn = !$turn;
	}
	print '</div>'."\n";

	print '<div id="command">'."\n";
	if ($message ne '') {
		print '<span class="br">'.entityencode($message).'</span>'."\n";
	}
	
	if ($turn) {
		print '<form method="post" action="'.CGI_2WORDS_PATH.'">'."\n";
		if ($message eq '') {
			if ($story{"content"} eq '') {
				print 'Two words, please:<br>'."\n";
			}
			else {
				print 'Please continue, two words:<br>'."\n";
			}
		}
		print '<input class="intx" type="text" name="words">'."\n";
		print '<input class="inbt" type="submit" value="enter">'."\n";
		if(@storylines >= (STORY_LENGTH-1)) {
			print '<input class="inbt" type="submit" name="next" value="enter and then start a new one">'."\n";
		}
		print '</form>'."\n";
	}
	else {
		if ($message eq '') {
			print 'Wait for it.'."\n";
		}
	}
	print '</div>'."\n";
}
elsif ($message ne '') {
	print '<div id="command">'."\n";
	print '<span class="br">'.entityencode($message).'</span>'."\n";
	print '</div>'."\n";
}

if(($intfpass == 1) && ($ongstate == 0)) {
	print '</div><div id="framespace">'."\n";
	print '<table id="intftable" cellspacing="0" cellpadding="0"><tr class="intf">'."\n";
	
	print '<td colspan="6" class="intf"><img src="'.CGI_PATH.'intf-00';
	if ($intfmode == 4) {
		print '_04';
	}
	elsif ($intfmode == 8) {
		print '_08';
	}
	elsif ($intfstate == 16) {
		print '_10';
	}	
	print'.gif" alt="" class="intf"></td>'."\n";
	
	print '</tr><tr class="intf">'."\n";
	print '<td class="intf"><img src="'.CGI_PATH.'intf-20.gif" alt="o" class="intf"></td>'."\n";
	print '<td class="intf"><a href="'.CGI_PATH.'2words?s='.($intfpause?17:16).'"><img src="'.CGI_PATH.'intf-10'.(($intfmode==16)?'_':'').'.gif" class="intf" alt="&gt"></td>'."\n";
	print '<td class="intf"><a href="'.CGI_PATH.'2words?s='.($intfpause?9:8).'"><img src="'.CGI_PATH.'intf-08'.(($intfmode==8)?'_':'').'.gif" class="intf" alt="&lt;&lt;"></td>'."\n";
	print '<td class="intf"><a href="'.CGI_PATH.'2words?s='.($intfpause?5:4).'"><img src="'.CGI_PATH.'intf-04'.(($intfmode==4)?'_':'').'.gif" class="intf" alt="&gt;&gt;"></td>'."\n";
	print '<td class="intf"><a href="'.CGI_PATH.'2words?s='.($intfpause?0:0).'"><img src="'.CGI_PATH.'intf-02.gif" class="intf" alt="^"></td>'."\n";
	print '<td class="intf"><a href="'.CGI_PATH.'2words?s='.($intfpause?$intfmode:$intfmode+1).'"><img src="'.CGI_PATH.'intf-01'.($intfpause?'_':'').'.gif" class="intf" alt="||"></td>'."\n";
	print '</tr></table>'."\n";
}

print '</div><div id="insb" class="ins">'."\n";
print '<div id="undertext">'."\n";
for (my $i = $storyid-1-(($page!=0)?((($page-1)*PAGE_LENGTH)+FIRSTPAGE_LENGTH):0); $i > ($storyid-1-($page*PAGE_LENGTH)- FIRSTPAGE_LENGTH) && $i >= 0; --$i) {
	$storypath = DATA_STORY_PATH.$i;
	%newstory = readdatafile($storypath);
	print '<p class="'.(($i&1)?'br':'ni').'" id="s'.$i.'">'.entityencode($newstory{'content'}).'</p>'."\n";
}

print '</div>'."\n";
print '<div id="underlinks">'."\n";
print '<a href="'.CGI_PATH.'">BSTA</a> | <a href="'.CGI_2WORDS_PATH.'">Once again</a>';
if(($storyid - ($page*PAGE_LENGTH)) - FIRSTPAGE_LENGTH > 0) {
	print ' | <a href="'.CGI_2WORDS_PATH.'/'.($page+1).'">Before</a>';
}
if($page > 0) {
	print ' | <a href="'.CGI_2WORDS_PATH.'/'.($page-1).'">Unbefore</a>';
}
if(($storyid - ($page*PAGE_LENGTH)) - FIRSTPAGE_LENGTH > 0) {
	print ' | <a href="'.CGI_2WORDS_PATH.'/'.(int(($storyid - FIRSTPAGE_LENGTH - 1) / PAGE_LENGTH) + 1).'">Initially</a>';
}
if($turn) {
	print ' | (Entering words here is irreversible. Your actions might be remembered forever. So please be reasonable.)';
}
print "\n";
print '</div>'."\n";

print '</div>'."\n";
print '</div>'."\n";
print '<a href="/" class="cz">'.WEBSITE.'</a>'."\n";
print '</body></html>'."\n";

sub writeindex {
	(my $indexpath, my $pass, my $mode, my $pause) = @_;
	my $indexfile;
	my $indexof;
		
	if(ref($indexpath)) {
		$indexfile=$indexpath;
		unless (seek($indexfile, 0, 0)) {
			return 0;
		}
	}
	else {
		unless (open ($indexfile, ">", $indexpath)) {
			return 0;
		}
	}
	
	$indexof = CGI_PATH;
	$indexof =~ s/\/$//g;
	
	my %coin = readdatafile(DATA_COIN_PATH);
	
	if ($pass != 1) {
		print $indexfile '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'."\n";
		print $indexfile '<html>'."\n";
		print $indexfile ' <head>'."\n";
		print $indexfile '  <title>Index of '.$indexof.'</title>'."\n";
		print $indexfile ' </head>'."\n";
		print $indexfile ' <body>'."\n";
		print $indexfile '<h1>Index of '.$indexof.'</h1>'."\n";
		print $indexfile '<table><tr><th><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr><tr><th colspan="5"><hr></th></tr>'."\n";
		print $indexfile '<tr><td valign="top"><img src="/icons/back.gif" alt="[DIR]"></td><td><a href="/">Parent Directory</a></td><td>&nbsp;</td><td align="right">  - </td><td>&nbsp;</td></tr>'."\n";
		print $indexfile '<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="2words/">2words/</a></td><td align="right">'.INTF_DATE.'  </td><td align="right">  - </td><td>&nbsp;</td></tr>'."\n";
		print $indexfile '<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="coin/">coin/</a></td><td align="right">'.COIN_DATE.'  </td><td align="right">  - </td><td> Coincidence </td></tr>'."\n";
		print $indexfile '<tr><th colspan="5"><hr></th></tr>'."\n";
		print $indexfile '</table>'."\n";
		print $indexfile '<address>Apache/2.2.22 (Debian) Server at '.WEBSITE.' Port 80</address>'."\n";
		print $indexfile '</body></html>'."\n";
	}
	else {
		my %framedata;
		my %nextframedata;
		my %default;
		
		%framedata = readdatafile(DATA_PATH.0);
		%nextframedata = readdatafile(DATA_PATH.1);
		%default=readdatafile(DATA_DEFAULT_PATH);
		
		# if($mode == 16 && $pause) {
			# $framedata{'ongtime'} = $time;
			# writedatafile(DATA_PATH.0,%framedata);
		# }
		
		foreach my $ind (keys %default) {
			unless(defined($framedata{$ind})){
				$framedata{$ind}=$default{$ind};
			}
			unless(defined($nextframedata{$ind})){
				$nextframedata{$ind}=$default{$ind};
			}
		}
		
		
		print $indexfile '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">'."\n";
		print $indexfile '<html lang="en"><head>'."\n";
		print $indexfile '<title>';
		if($mode == 8 || $mode == 4) {
			print $indexfile 'Index of';
		}
		elsif($mode == 16) {
			print $indexfile entityencode($settings{'story'});
			if($pause) {
				print $indexfile ' &bull; '.WEBSITE_NAME;
			}
		}
		else {
			print $indexfile 'Index of '.$indexof;
		}
		print $indexfile '</title>'."\n";
		print $indexfile '<meta http-equiv="Content-type" content="text/html; charset=UTF-8">'."\n";
		print $indexfile '<link rel="icon" type="image/png" href="'.FAVICON_PATH.'">'."\n";
		print $indexfile '<link rel="stylesheet" href="'.CGI_CSS_PATH.'">'."\n";
		print $indexfile '</head><body>'."\n";
		print $indexfile '<a href="/"><img id="botmlogo" src="'.CGI_LOGO_PATH.'" alt="'.WEBSITE.'"></a>'."\n";
		print $indexfile '<div id="all">'."\n";
		
		print $indexfile '<div id="inst" class="ins">'."\n";
		
		print $indexfile '<div id="title">'."\n";
		print $indexfile '<H1 id="titletext">';
		if($mode == 8 || $mode == 4) {
			print $indexfile 'Index of';
		}
		elsif($mode == 16) {
			print $indexfile entityencode($settings{'story'});
		}
		else {
			print $indexfile 'Index of '.$indexof;
		}
		print $indexfile '</H1>'."\n";
		print $indexfile '</div>'."\n";
		
		print $indexfile '</div><div id="framespace">'."\n";
		print $indexfile '<img src="'.CGI_PATH;
		if($mode == 8) {
			print $indexfile 'intf-ll.gif'
		}
		elsif($mode == 4) {
			print $indexfile 'intf-pp.gif'
		}
		elsif($mode == 16) {
			if($pause) {
				print $indexfile sprintf($settings{'frame'},0,$framedata{'ext'}).'" title="'.entityencode($framedata{'title'});
			}
			else {
				print $indexfile 'intf-tr.gif'
			}
		}
		else {
			print $indexfile 'intf-kw.gif'
		}
		print $indexfile '" alt="0" id="frame">'."\n";
		
		print $indexfile '</div><div id="insb" class="ins">'."\n";
		print $indexfile '<div id="undertext">'."\n";
		if($mode == 8 || $mode == 4) {
			print $indexfile '<img src="/icons/back.gif" alt="[DIR]"> <a href="/">Parent Directory</a><br><img src="/icons/folder.gif" alt="[DIR]"> <a href="#">yyyyb/</a>'."\n";
		}
		elsif ($mode == 16) {
			if($pause) {
				print $indexfile bb2ht($framedata{'content'})."\n";
			}
			else {
				print $indexfile "...\n";
			}
		}
		else {
			print $indexfile '<img src="/icons/back.gif" alt="[DIR]"> <a href="/">Parent Directory</a><br>'."\n";
			print $indexfile '<img src="/icons/folder.gif" alt="[DIR]"> <a href="2words/">2words/</a> '.INTF_DATE.' - <br>'."\n";
			print $indexfile '<img src="/icons/folder.gif" alt="[DIR]"> <a href="coin/">coin/</a> '.COIN_DATE.' - '.entityencode($coin{'server'})."\n";
		}
		print $indexfile '</div>'."\n";
		
		print $indexfile '<div id="command">'."\n";
		if ($mode == 8) {
			print $indexfile '[<span class="br">EE</span>:<span class="br">EE</span>:<span class="br">EE</span>]'."\n";
		}
		elsif ($mode == 4) {
			print $indexfile '[<span class="ni">EE</span>:<span class="ni">EE</span>:<span class="ni">EE</span>]'."\n";
		}
		elsif ($mode == 16) {
			if($pause) {
				print $indexfile '[<span class="br">00</span>:<span class="br">00</span>:<span class="br">00</span>]<br>'."\n";
				print $indexfile '&gt;<a href="'.CGI_VIEWER_PATH.'/1">'.entityencode($nextframedata{'title'}).'</a>'."\n";
			}
			else {
				print $indexfile '[<span class="ni">--</span>:<span class="ni">--</span>:<span class="ni">--</span>]<br>'."\n";
				print $indexfile '&gt;...<span class="inp">_</span>'."\n";
			}
		}
		else {
		}
		print $indexfile '</div>'."\n";
		
		print $indexfile '</div>'."\n";
		
		print $indexfile '</div>'."\n";
		print $indexfile '<a href="/" class="cz">'.WEBSITE.'</a>'."\n";
		
		print $indexfile '</body></html>'."\n";
	}
	
	unless (ref($indexpath)) {
		close ($indexfile);
	}
	else {
		truncate ($indexfile , tell($indexfile));
	}
	
	return 1;
}
