commit dec36882bc47579944f8c9583afac18c8c2c2fd2
parent 570c8f2c2e6aed96e23a546bd76394dd03043473
Author: Samir Parikh <noreply@samirparikh.com>
Date: Sat, 4 Feb 2023 03:16:18 +0000
add spsp.cgi as the initial version of what should have been a CGI
program from the very beginning
Diffstat:
A | cgi/spsp.cgi | | | 117 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 117 insertions(+), 0 deletions(-)
diff --git a/cgi/spsp.cgi b/cgi/spsp.cgi
@@ -0,0 +1,117 @@
+#!/usr/local/bin/perl
+
+# "Simplified Perl Status Poster"
+#
+# MIT License
+#
+# Copyright (c) 2023 Samir Parikh
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+use warnings;
+use strict;
+use lib "/home/compiler/perl5/lib/perl5";
+use JSON;
+use POSIX qw( strftime );
+use Data::Dumper;
+use CGI;
+use Text::Markdown;
+use feature qw( signatures );
+no warnings qw( experimental::signatures );
+
+### declare constants
+my $POST_DATA_FILENAME = "/home/compiler/programs/spsp/html/post_data";
+my $TEMP_FILE = "/home/compiler/programs/spsp/html/tmp.$$";
+my $ID_LENGTH = 4;
+my $ID = "id";
+my $TITLE = "title";
+my $STATUS = "status";
+my $TIMESTAMP = "timestamp";
+my $HTML_TITLE = "SPSP";
+my $AUTHOR = "Samir Parikh";
+my $H1_HEADING = "Samir's Updates";
+
+my $cgi = CGI->new;
+
+sub generate_post_id ( $id_length )
+{
+ my @characters = (0 .. 9, "a" .. "z");
+ return join '', map $characters[ rand @characters ], 1 .. $id_length;
+}
+
+### slurp the input into a scalar
+### https://www.perl.com/article/21/2013/4/21/Read-an-entire-file-into-a-string/
+#chomp( my $status = do { local $/; <> } );
+
+### check to see if post_data file exists. If not, create it
+unless ( -e $POST_DATA_FILENAME )
+{
+ open my $post_data_filehandle, '>>', $POST_DATA_FILENAME
+ or die "Cannot open $POST_DATA_FILENAME for appending: $!\n";
+
+ close $post_data_filehandle or die "Error closing $POST_DATA_FILENAME: $!\n";
+}
+
+### open post data file for reading
+open my $post_data_filehandle, '<', $POST_DATA_FILENAME
+ or die "Cannot open $POST_DATA_FILENAME for reading: $!\n";
+
+### read all current posts into @posts
+my $json_posts = JSON->new;
+my @posts = do {
+ local $/;
+ @{ $json_posts->decode( <$post_data_filehandle> ) };
+ };
+
+### close post data file
+close $post_data_filehandle or die "Cannot close $POST_DATA_FILENAME: $!\n";
+
+### assemble the new post into a hash
+my %post = (
+ $ID => generate_post_id( $ID_LENGTH ),
+ $TITLE => scalar $cgi->param( $TITLE ),
+ $STATUS => scalar $cgi->param( $STATUS ),
+ $TIMESTAMP => strftime "%a %d %b %Y %H:%M %Z", localtime,
+);
+
+### prepend new post to the array of posts
+unshift @posts => \%post;
+
+# write updated posts to temporary file
+### open temporary file for writing
+open my $temp_filehandle, '>', $TEMP_FILE
+ or die "Cannot open $TEMP_FILE for writing: $!\n";
+
+my $posts = $json_posts->pretty->encode( \@posts );
+print $temp_filehandle $posts;
+
+### close the temporary file
+close $temp_filehandle or die "Error closing $TEMP_FILE: $!\n";
+
+### delete original post data file
+unlink $POST_DATA_FILENAME
+ or die "Cannot delete original $POST_DATA_FILENAME: $!\n";
+
+### Rename temporary file to replace original post data file
+rename $TEMP_FILE, $POST_DATA_FILENAME
+ or die "Cannot rename $TEMP_FILE to $POST_DATA_FILENAME: $!\n";
+
+print $cgi->redirect( "/post_data" );
+
+exit 0;