#!/usr/bin/perl # SM-Comic Version 1.0 # Copyright 2007-2008 James Lehmann and Scriptmonkeys.us # redirect errors to file 'errors.txt' # in BEGIN block, so it runs BEFORE the rest of the program is compiled -- so it catches any compile errors (usually) BEGIN { open (STDERR, '>errors.txt'); # redirect error msgs to file } # User-defined set-up -- edit these values for your site # Name of program - so program can call itself # If you change the name of the program, change this to match $self_url = 'smcomic.cgi'; # Name of the folder you're going to keep all the comics in. Must have a slash at the end. # Usually, $comic_folder_unix and $comic_folder_html will be the same. # If smcomic.cgi will be in the same folder as your comic files, set $comic_folder_unix to './' (period slash) # and $comic_folder_html to '' (nothing). $comic_folder_unix = '../comics/'; $comic_folder_html = '../comics/'; # Filenames (with paths) of your navifgation buttons. The "alt" versions are displayed when a button is # not available (such as the "next" button on the current comic). Not used if the next two variables are # set to 'no'. $firstimage = 'first.jpg'; $lastimage = 'current.jpg'; $previmage = 'previous.jpg'; $nextimage = 'next.jpg'; $altfirstimage = 'blank.jpg'; $altlastimage = 'blank.jpg'; $altprevimage = 'blank.jpg'; $altnextimage = 'blank.jpg'; # Set this to 'yes' or 'no' -- if 'yes' then "first" button will stop working when reader is on # first comic and "last" button will stop working when reader is on last comic. If 'no' then # first and last buttons will always be active. $disable_first_last = 'yes'; # Set this to 'yes' or 'no' -- if 'yes' then "previous" button will stop working when reader is on # first comic and "next" button will stop working when reader is on last comic. If 'no' then # previous and next buttons will always be active. $disable_next_prev = 'yes'; # This is the order the buttons will be displayed in. Set it to: # 1 for PREVIOUS - FIRST - LAST - NEXT # 2 for FIRST - PREVIOUS - NEXT - LAST $buttonorder = 2; # Filename of page template: $pagetemplatename = 'template.htm'; # You should not need to edit below this line ############################################################################## $debugg = 0; # set to 1 to send debug info to errors.txt $|=1; #req'd for cgi scripts # REQUIRE LIBRARY FILES require ("cgi-lib.pl") || die ("Can't require cgi-lib.pl"); # Read-in data items &ReadParse(*form_data); # Sets %form_data from "form" on previous screen # enter with the following possibilities for form_data: # # 1) a = (nothing) # display current comic # # 2) a = (number) # display comic number (number) # if invalid, display current comic # # Everything is handled by a single routine, display_page. Yeah, I could have eliminated the function definition and # just made it all part of the main routine, but having it in a seperate function makes it easier to add new # functionality in the future. &display_page; exit; # FUNCTIONS ############################################################################## sub display_page { if (! -d $comic_folder_unix) { die "Directory $comic_folder_unix not found"; } # first, gather list of files available # to publish a comic, just drop it into your comics folder @allfiles = glob($comic_folder_unix . '*'); # gather a list of all files in comics directory if ($debugg) {local $, = "\n"; print STDERR '@allfiles =', @allfiles, '(END @allfiles)' . "\n\n";} # remove path (everything before final '/') foreach (@allfiles) {s!^.*/!!;} # For each element of @allfiles, replace... # ^ from start of file # .* everything (greedy mode) # / to final / # ! !! replace with nothing # make array of **just the ones with a number in the name (must not be zero)** # will be @allfiles2 @allfiles2 = (); # define for use later $k = 0; #for $i = 0 to $#allfiles for ($i = 0; $i <= $#allfiles; ++$i) { # create parallel array # perl purists will insist on using a hash at this point # nuts to them # extract number from filename and set @number to that $allfiles[$i] =~ m/([0-9]+)/; # look for number $n = $1; # stuff that was in parenthesis in the line above if ($n > 0) { $number[$k] = $n; $allfiles2[$k] = $allfiles[$i]; ++$k; } } # end for loop if ($debugg) {local $, = "\n"; print STDERR '@allfiles2 =', @allfiles2, '(END @allfiles2)' . "\n\n";} if ($debugg) { #for $i = 0 to number of elements in @number for ($i = 0; $i <= $#number; $i++) { print STDERR "\$number[$i] = $number[$i]\n"; } # end for print STDERR "\n"; } # the number passed to us in $form_data{'a'} is the number stamped on the file, not the index of the array # we don't need to do a full sort, just get us the indexes of the current, previous, next, first, and last $wanted = $form_data{'a'}; # these all contain index values -- tres confusing, I know # the worst part is that for $current, $previous, and $next, -1 means undefined, but for $first, $last, and $wanted, 0 means undefined $current = -1; $previous = -1; $next = -1; $first = 0; $last = 0; #for $j = 0 to $#allfiles2 for ($j = 0; $j <= $#allfiles2; $j++) { if ($number[$j] < $number[$first]) {$first = $j;} if ($number[$j] > $number[$last]) {$last = $j;} if ($wanted > 0) { if ($number[$j] == $wanted) {$current = $j;} } # end if } # end for $j loop if ($current == -1) { # if 'current' undefined $current = $last; } # second pass - find previous and next: #for $j = 0 to $#allfiles2 for ($j = 0; $j <= $#allfiles2; $j++) { if ($previous == -1) { if ($number[$j] < $number[$current]) {$previous = $j;} } else { if ($number[$j] < $number[$current] and $number[$j] > $number[$previous]) {$previous = $j;} } if ($next == -1) { if ($number[$j] > $number[$current]) {$next = $j;} } else { if ($number[$j] > $number[$current] and $number[$j] < $number[$next]) {$next = $j;} } } # end for $j loop if ($debugg) {print STDERR "\$wanted = $wanted\n\$current = $current\n\$previous = $previous\n\$next = $next\n\$first = $first\n\$last = $last\n\n";} # WE NOW HAVE EVERYTHING WE NEED # poke certain things into variables to make the html below cleaner $comicurl = $comic_folder_html . $allfiles2[$current]; $comichtml = qq~~; $disable_first_last_x = 0; if ($disable_first_last =~ /yes/i) { $disable_first_last_x = 1; } if ( $disable_first_last_x and ( $current == $first ) ) { $firsthtml = qq~~; } else { $firsthtml = qq~~; } if ( $disable_first_last_x and ( $current == $last ) ) { $lasthtml = qq~~; } else { $lasthtml = qq~~; } $disable_next_prev_x = 0; if ($disable_next_prev =~ /yes/i) { $disable_next_prev_x = 1; } if ( $disable_next_prev_x == 0 and $previous == -1 ) { $previous = $first; } if ( $disable_next_prev_x == 0 and $next == -1 ) { $next = $last; } if ($previous == -1) { $previoushtml = qq~~; } else { $previoushtml = qq~~; } if ($next == -1) { $nexthtml = qq~~; } else { $nexthtml = qq~~; } if ($buttonorder == 1) { $navbar = qq~
$previoushtml $firsthtml $lasthtml $nexthtml
~; } else { $navbar = qq~
$firsthtml $previoushtml $nexthtml $lasthtml
~; } # substitute, then print to screen ($pageoutput) = &read_entire_text_file ($pagetemplatename); $pageoutput =~ s/%NAVBAR%/$navbar/ig; $pageoutput =~ s/%IMAGE%/$comichtml/ig; print "Content-type: text/html\n\n"; # page header - required print $pageoutput; } # end sub display_page ############################################################################## sub read_entire_text_file { my $text; open (RETF_FILE, '<' . $_[0]) or die "Error opening $_[0] for reading: $!"; { local $/ = ''; $text = ; } close RETF_FILE; my $pagesize = length($text); return $text, $pagesize; } # end sub read_template ############################################################################## ## END OF PROGRAM ##