No results found for \"$safe_search_text\".
"; } else { $list_html = "- " . join('', map {
"
- {path}\">$_->{title}
$_->{snippet} "
} @results) . "
#!/usr/bin/perl use strict; use warnings; use File::Spec; use Encode qw(decode_utf8); use URI::Escape qw(uri_unescape); use HTML::Escape qw(escape_html); binmode(STDOUT, ":utf8"); # --- Configuration --- my $max_parallel = 50; my $lock_timeout = 30; my $max_results = 20; my $lock_dir = '/tmp/search_locks'; my $directory = '../log/'; # --- Concurrency Control --- mkdir $lock_dir, 0777 unless -d $lock_dir; my $active_count = 0; my $now = time(); opendir(my $dh_lock, $lock_dir); while (my $file = readdir($dh_lock)) { next unless $file =~ /\.lock$/; my $path = "$lock_dir/$file"; my $mtime = (stat($path))[9] || 0; ($now - $mtime > $lock_timeout) ? unlink($path) : $active_count++; } closedir($dh_lock); if ($active_count >= $max_parallel) { render_html("
Server busy. Please try again in a few seconds.
", ""); exit; } my $lock_file = "$lock_dir/$$.lock"; open(my $fh_lock, '>', $lock_file) or die "Cannot create lock: $!"; # --- Query Decoding --- my $search_text = ''; if (($ENV{QUERY_STRING} || '') =~ /^q=([^&]*)/) { my $raw_q = $1; $raw_q =~ tr/+/ /; $search_text = uri_unescape($raw_q); $search_text = decode_utf8($search_text // ""); $search_text =~ s/\P{Print}//g; $search_text = substr($search_text, 0, 64); $search_text =~ s/^\s+|\s+$//g; } if ($search_text eq '') { final_output("Please enter a search term above.
"); } # --- Search --- my @results; my $files_read = 0; if ($search_text =~ /\S/) { my @stack = ($directory); while (@stack) { # Exit search immediately if limit reached last if scalar @results >= $max_results; my $current_path = pop @stack; if (-d $current_path) { if (opendir(my $dh, $current_path)) { while (my $entry = readdir($dh)) { next if $entry =~ /^\.\.?$/; push @stack, File::Spec->catfile($current_path, $entry); } closedir($dh); } } elsif (-f $current_path && $current_path =~ /index\.html$/) { if (open my $fh, '<:utf8', $current_path) { $files_read++; my $raw_content = do { local $/; <$fh> }; close $fh; my ($article) = $raw_content =~ /No results found for \"$safe_search_text\".
"; } else { $list_html = "