pn

the website
git clone https://git.pastanoggin.com/pn.git
Log | Files | Refs | README | LICENSE

perl.cgi (1593B)


      1 #!/usr/bin/perl
      2 use strict;
      3 use warnings;
      4 use open qw< :encoding(UTF-8) >;
      5 
      6 my $BASEDIR = "/htdocs/pastanoggin.com";
      7 my $base_filename = "$BASEDIR/docs/base";
      8 my $status_filename = "$BASEDIR/docs/status";
      9 
     10 sub print_file {
     11 	my $filename = shift;
     12 	my $file = undef;
     13 	open($file, "<", $filename)
     14 		or die "$0: can't open $filename for reading: $!";
     15 	print while (<$file>);
     16 	die "unexpected error while reading from $filename: $!" if $!
     17 }
     18 
     19 sub print_header {
     20 	my $code = shift;
     21 	print <<~END;
     22 	Status: $code
     23 	Content-Type: text/html
     24 
     25 	END
     26 }
     27 
     28 sub render_page {
     29 	my ($docname, $doctitle) = @_;
     30 	my $doc_filename = "$BASEDIR/docs/$docname";
     31 
     32 	my $base_file = undef;
     33 	open($base_file, "<", $base_filename)
     34 		or die "$0: can't open $base_filename for reading: $!";
     35 
     36 	# print header and contents
     37 	print_header 200;
     38 	while (<$base_file>) {
     39 		if (/\[%title%\]/) {
     40 			print "$doctitle";
     41 		} elsif (/\[%content%\]/) {
     42 			print_file $doc_filename;
     43 		} elsif (/\[%status%\]/) {
     44 			print_file "$status_filename";
     45 		} else {
     46 			print;
     47 		}
     48 	}
     49 	die "unexpected error while reading from $base_filename: $!" if $!
     50 }
     51 
     52 sub render_error {
     53 	my $errcode = shift;
     54 	print_header $errcode;
     55 	print_file "$BASEDIR/docs/${errcode}.html";
     56 }
     57 
     58 my $path = $ENV{'PATH_INFO'} || '/';
     59 
     60 if ($path =~ /^\/?$/) {
     61 	render_page 'root', 'pastanoggin!!';
     62 } elsif ($path =~ /^\/contact\/?$/) {
     63 	render_page 'contact', 'hello, is it me you\'re looking for?';
     64 } elsif ($path =~ /^\/blog\/?$/) {
     65 	render_page 'blog', 'yapyapyapyap :D';
     66 } elsif ($path =~ /^\/links\/?$/) {
     67 	render_page 'links', 'the interwebs';
     68 } else {
     69 	render_error '404';
     70 }