#!/usr/bin/perl # This is a cgi script in perl. # # It displays the Peecho print button. # # The idea is that it should be possible to buy the printed # copies of the book from Peecho.com. # # The number of the requested volume is given to this program # as a query string, for example if this program is available # at http://www.GoogolplexWrittenOut.com/cgi-bin/peecho.pl then one can # get the Peecho print button for volume number 12345 # at http://www.GoogolplexWrittenOut.com/cgi-bin/peecho.pl?12345 # where "12345" is the query string. Setting the query string # to "source", this means visiting something # like http://www.GoogolplexWrittenOut.com/cgi-bin/peecho.pl?source # shows the source of this perl-script. # # (some of the code about the print-button is copied from the Pecho website, the rest is typed) # # Wolfgang Hartmut Nitsche # http://www.stanford.edu/~nitsche/ # http://nitsche.mobi # http://www.GoogolplexWrittenOut.com ########################################################### ########################################################### ## ## ## I N I T I A L P A R T O F T H I S S C R I P T ## ## ## ########################################################### ########################################################### use bytes; # Probably this is not necessary, but just to be on the # safe side, we use it anyway. # I want to make shure that length() gives the number # of bytes in a string, not the number of characters. # So Windows-linebreak should have length 2. # Special Unicode should have a length of more # than 2, but we don't use them anyway. $query = $ENV{'QUERY_STRING'}; # The query string should usually be the number of the requested volume. # For example $query = '123' will display the print button for book volume 123. # Volume numbers must only consist of digits 0-9 and may not have any leading zero. # And $query = 'source' will show the source code of this perl script. # Any other invalid value for query will give error message. $filename = $ENV{'SCRIPT_FILENAME'}; # This is the file-name of this perl script, for # example $filename = 'peecho.pl' # The number of the last volume of the books: $voltotal = "1" . ("0" x 94); # Get rid of unwanted special characters in $query $query =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $query =~ s/[^\!-\~]/\?/g; $query =~ s/[\<\>\&]/\?/g; # Show the source code of this perl script, if requested: if($query eq 'source') { $sourcefile = ''; open(FILE, "<$filename"); while() { $sourcefile .= $_; } close(FILE); print "Content-type: text/plain\n\n"; print $sourcefile; exit; } # Check if $query is a valid volume number: $errormessage = ''; if( $query eq '' ) { $errormessage = 'requested volume number is an empty string'; } elsif( $query =~ /[^0-9]/ ) { $errormessage = 'requested volume number contains non-numeric characters'; } elsif( substr($query, 0, 1) eq '0' ) { $errormessage = 'requested volume number starts with leading zero'; } elsif( length($query) > length($voltotal) ) { $errormessage = 'requested volume number is longer and therefore larger than '.$voltotal; } elsif( (length($query) == length($voltotal)) and ($query gt $voltotal) ) { $errormessage = 'requested volume number is of equal length but larger than '.$voltotal; } # If $query is a valid volume number, use its value; otherwise show an error message: if($errormessage eq '') { $volnow = "$query"; } else { print "Content-type: text/html\n"; print "Status: 400 Bad Request\n"; print "\n"; print ''."\n"; print "\n"; print "\n"; print "error (400 Bad Request)\n"; print ''."\n"; print "\n"; print "\n"; print "

error (400 Bad Request)

\n"; print "

The volume which you requested does not exist.

\n"; print "

\n"; print "

Valid volume numbers nust be a number from 1 to $voltotal\n"; print "and must be written without leading zeros\n"; print "and without any non-digit characters.

\n"; print "

\n"; print "Your requested volume number was
\n"; print "$query
\n"; print "which caused the following problem:
\n"; print "$errormessage\n"; print "

\n"; print "\n"; print "\n"; exit; } # If we reach this part of the program, we know # that $volnow is a valid volume number, and # $voltotal is the number of the last possible # volume number. # We want to display the Peecho button to order volume $volnow. ############################################################# ############################################################# ## ## ## S T A R T D I S P L A Y I N G T H E B U T T O N ## ## ## ############################################################# ############################################################# print "Content-type: text/html\n"; print "\n"; print ''."\n"; print "\n"; print "\n"; print "Peecho: Googolplex Written Out, volume $volnow\n"; print ''."\n"; print "\n"; print "\n"; print "

Buy printed book from Peecho:
Googolplex Written Out, volume $volnow

\n"; print "

Please click the order button below to buy the printed book Googolplex Written Out, volume $volnow from Peecho.

\n"; print "Notices\n"; print "\n"; print "\n"; print ''; print "\n"; print "\n"; print "
\n"; print 'Print'; print "\n"; print ''; print "
\n"; print "\n"; print "\n"; print ''; print "\n"; print "\n"; print qq{

[go back to overview page]

\n}; print "\n"; print "\n"; ################################# ################################# ## ## ## E N D O F P R O G R A M ## ## ## ################################# #################################