From a152c4e7e4c15be60d9d5118db2a83199fb6dbaf Mon Sep 17 00:00:00 2001
From: b <rowerynaksiezycu@gmail.com>
Date: Thu, 9 Nov 2023 22:50:02 +0000
Subject: [PATCH] generating fake index

---
 bsta_lib.1.pm | 74 ++++++++++++++++++++++++++++++++++++++++++++++-----
 viewer.1.pl   |  2 +-
 2 files changed, 68 insertions(+), 8 deletions(-)

diff --git a/bsta_lib.1.pm b/bsta_lib.1.pm
index 50eea1a..7ae1183 100644
--- a/bsta_lib.1.pm
+++ b/bsta_lib.1.pm
@@ -407,7 +407,8 @@ sub urlencode {
 # to consider:
 # a BBcode library?
 
-#analyse bbcode text to build tag tree #TODO make [/*] optional!
+#analyse bbcode text to build tag tree
+#TODO make [/*] optional!
 sub bbtree {
 	(my $bb, my $printdebug) = @_;
 	my %bbtree;
@@ -1201,12 +1202,21 @@ sub print_viewer_page {
 
 sub write_index {
 	(
-		my $mode,
 		my $state,
-		my $settings
+		my $settings,
+		my $pass,
+		my $mode,
+		my $index
 	) = @_;
+	my $fh;
+	my $r;
+	
+	unless (open ($fh, ">:encoding(UTF-8)", encode('locale_fs', WWW_INDEX_PATH()))) {
+		return 0;
+	}
 	
-	if ($mode eq 'viewer') {
+	# normal running story
+	if ($state > STATE->{'inactive'}) {
 		my %frame_data     = read_data_file(join_path(PATH_SEPARATOR(), DATA_PATH(), 0));
 		my %next_frame_data= read_data_file(join_path(PATH_SEPARATOR(), DATA_PATH(), 1));
 		my %default        = read_data_file(DATA_DEFAULT_PATH());
@@ -1214,8 +1224,8 @@ sub write_index {
 		%frame_data     = merge_settings(\%default,      \%frame_data);
 		%next_frame_data= merge_settings(\%default, \%next_frame_data);
 		
-		print_viewer_page(
-			WWW_INDEX_PATH(),
+		$r = print_viewer_page(
+			$fh,
 			{
 				'frame' => 0,
 				'access' => 1,
@@ -1231,9 +1241,59 @@ sub write_index {
 			\%next_frame_data
 		);
 	}
+	# no conditions met, pretend a normal Apache2 index
+	elsif ($pass != 1) { 
+		my $index_of = CGI_PATH;
+		$index_of =~ s/\/$//g;
+		
+		my $_index_of = html_entity_encode_dec($index_of, 1);
+		
+		
+		print_html_start ($fh);
+		print $fh ' <head>'."\n";
+		print $fh '  <meta http-equiv="Content-type" content="text/html; charset=UTF-8">'."\n";
+		print $fh '  <title>Index of '.$_index_of.'</title>'."\n";
+		print $fh ' </head>'."\n";
+		print $fh ' <body>'."\n";
+		print $fh '  <h1>Index of '.$_index_of.'</h1>'."\n";
+		print $fh '  <table>'."\n";
+		print $fh '   <tr>'."\n";
+		print $fh '    <th><img src="/icons/blank.gif" alt="[ICO]"></th>'."\n";
+		print $fh '    <th><a href="?C=N;O=D">Name</a></th>'."\n";
+		print $fh '    <th><a href="?C=M;O=A">Last modified</a></th>'."\n";
+		print $fh '    <th><a href="?C=S;O=A">Size</a></th>'."\n";
+		print $fh '    <th><a href="?C=D;O=A">Description</a></th>'."\n";
+		print $fh '   </tr><tr>'."\n";
+		print $fh '    <th colspan="5"><hr></th>'."\n";
+		print $fh '   </tr><tr>'."\n";
+		print $fh '    <td valign="top"><img src="/icons/back.gif" alt="[DIR]"></td>'."\n";
+		print $fh '    <td><a href="/">Parent Directory</a></td>'."\n";
+		print $fh '    <td>&nbsp;</td>'."\n";
+		print $fh '    <td align="right">  - </td>'."\n";
+		print $fh '    <td>&nbsp;</td>'."\n";
+		print $fh '   </tr><tr>'."\n";
+		print $fh '    <td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td>'."\n";
+		print $fh '    <td><a href="2words/">2words/</a></td>'."\n";
+		print $fh '    <td align="right">'.INTF_DATE.'  </td>'."\n";
+		print $fh '    <td align="right">  - </td><td>&nbsp;</td>'."\n";
+		print $fh '   </tr><tr>'."\n";
+		print $fh '    <td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td>'."\n";
+		print $fh '    <td><a href="coin/">coin/</a></td>'."\n";
+		print $fh '    <td align="right">'.COIN_DATE.'  </td>'."\n";
+		print $fh '    <td align="right">  - </td><td> Coincidence </td>'."\n";
+		print $fh '   </tr><tr>'."\n";
+		print $fh '    <th colspan="5"><hr></th>'."\n";
+		print $fh '   </tr>'."\n";
+		print $fh '  </table>'."\n";
+		print $fh '  <address>Apache/2.2.22 (Debian) Server at '.WEBSITE.' Port 80</address>'."\n";
+		print $fh '  /body>'."\n";
+		print_html_end ($fh);
+	}
 	else {
-		# TO DO !
+		
 	}
+	close ($fh);
+	return $r
 }
 
 
diff --git a/viewer.1.pl b/viewer.1.pl
index 356e46c..68db13c 100644
--- a/viewer.1.pl
+++ b/viewer.1.pl
@@ -214,7 +214,7 @@ if (open ($state_file, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
 				$goto_list{'ongtime-1'} = $frame_data{'ongtime'};
 				
 				if (copy ($in_path, $out_path)) {
-					write_index('viewer', \%state, \%settings);
+					write_index(\%state, \%settings);
 					write_data_file($state_file,     '','', \%state);
 					write_data_file(DATA_LIST_PATH(),'','', \%goto_list);
 				}
-- 
2.30.2