home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2004 February / INTERNET114.ISO / pc / projects / feedback.cgi < prev    next >
Encoding:
Text File  |  2003-12-02  |  4.3 KB  |  160 lines

  1. #!/usr/bin/perl
  2. # Change above so it points to perl. Next line controls output buffering.
  3. $|=1;
  4.  
  5.  
  6. ########
  7. #
  8. # Reader feedback listing script, (c) Alex Cruickshank 2003 (alex@itreviews.co.uk)
  9. #
  10. # This code is released under the GNU Public Licence (see www.gnu.org for details) and is supplied
  11. # as is with no warranty either expressed or implied. No liability will be accepted for loss or damage of any kind.
  12. #
  13. # Always back up your data before using new software.
  14. #
  15. # You are free to use this script to manage and serve data on your site.
  16. # Please notify the author of any such use, as well as bug reports, modifications, etc.
  17. #
  18. # chmod this file to 755.
  19. #
  20. $maintitle = "Readers' feedback";
  21. # title for all pages
  22. #
  23. $database_name = "feedback.txt";
  24. # the path and name of the file containing the feedback; a plain text file containing one entry per line
  25. #
  26. $per_page = 10; 
  27. # number of entries to display per page
  28. #
  29. $script_name = "/cgi-bin/feedback.cgi";
  30. # relative URL of this script
  31. #
  32. $HeaderFile = "head.htm";
  33. # HTML header file path
  34. #
  35. $FooterFile = "foot.htm";
  36. # HTML footer file path
  37. #
  38. $contact_script = "/cgi-bin/contact.cgi";
  39. # URL location of 'contact us' page
  40. #
  41. ########
  42.  
  43.  
  44. &parse;
  45. print "Content-type: text/html\n\n";
  46. if ($ENV{'QUERY_STRING'} eq "") {&main;}
  47. else {&show_feedback;}
  48. exit;
  49.  
  50.  
  51. sub parse { # handle form data
  52.  
  53.     read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  54.     @pairs = split(/&/, $buffer);
  55.     foreach $pair (@pairs) {
  56.         ($name, $value) = split(/=/, $pair);
  57.         $value =~ tr/+/ /;
  58.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  59.         if ($FORM{$name}) { $fields{$name} = $fields{$name}.",".$value; }
  60.         else { $FORM{$name} = $value; }
  61.     }
  62. }
  63.  
  64.  
  65. sub header { # load and use a formatted page header
  66.  
  67.     open (HEADER,"$HeaderFile");
  68.     @header = <HEADER>;
  69.     close (HEADER);
  70.     foreach $headerline (@header) {
  71.         print "$headerline";
  72.     }
  73.     print "<H1>$maintitle</H1>\n";
  74. }
  75.  
  76.  
  77. sub footer { # load and use a formatted page footer
  78.  
  79.     open (FOOTER,"$FooterFile");
  80.     @footer = <FOOTER>;
  81.     close (FOOTER);
  82.     foreach $footerline (@footer) {
  83.         print "$footerline";
  84.     }
  85. }
  86.  
  87.  
  88. sub main { # show the intro page
  89.     &header;
  90.  
  91.     print<<"OUTPUT";
  92.     <P>
  93.     Whenever a customer buys one of our products, we ask them to tell us what they think of our site, good or bad.
  94.     <A HREF="$script_name?$database_name&1">Click here</A> to read their comments and suggestions.
  95.     </P>
  96.     <P>
  97.     If you have your own comment or suggestion to make, <A HREF="$contact_script">click here</A>.
  98.     </P>
  99. OUTPUT
  100.  
  101.     &footer;
  102.     exit;
  103. }
  104.  
  105.  
  106. sub show_feedback { # display the feedback, breaking it up into pages
  107.     &header;
  108.  
  109.     $line_count = 0;
  110.     $counter = 0;
  111.     $printed_lines = 0;
  112.  
  113.     $query = $ENV{'QUERY_STRING'};
  114.     @query_terms = split (/&/, $query);
  115.     ($database_name, $range) = @query_terms;
  116.  
  117.     open (FBDB, "$database_name");
  118.     @open_comments = <FBDB>;
  119.     close (FBDB);
  120.  
  121.     foreach (@open_comments) {
  122.         $line_count++;
  123.     }
  124.  
  125.     $page_total_int = int($line_count / $per_page);
  126.     if ($page_total_int < ($line_count / $per_page)) {$page_total_int++;}
  127.     print "<P>Page $range of $page_total_int</P>\n";
  128.  
  129.     $next_page = $range + 1;
  130.     $prev_page = $range - 1;
  131.  
  132.     print "<UL>\n";
  133.     foreach $rline (@open_comments) {
  134.         chomp($rline);
  135.         if ($counter / $per_page >= $range - 1) {
  136.             if ($counter / $per_page < $range) {
  137.                 print "<LI>$rline  </LI>\n";
  138.                 $printed_lines += 1;
  139.             }
  140.         }
  141.         $counter++;
  142.     }
  143.     print "</UL>\n<BR><TABLE WIDTH=\"80%\" ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\" BORDER=\"0\"><TR>\n";
  144.     print "<TD ALIGN=\"LEFT\" WIDTH=\"33%\">\n";
  145.     if ($prev_page > 0) {
  146.         print "<A HREF=\"$script_name?$database_name&$prev_page\">< PREVIOUS</A>\n";
  147.     }
  148.     print "</TD><TD ALIGN=\"CENTER\" WIDTH=\"34%\">\n";
  149.     print "<A HREF=\"$script_name\">FEEDBACK INDEX</A>\n";
  150.  
  151.     print "</TD><TD ALIGN=\"RIGHT\" WIDTH=\"33%\">\n";
  152.     if ($next_page < $page_total_int + 1) {
  153.         print "<A HREF=\"$script_name?$database_name&$next_page\">NEXT ></A>\n";
  154.     }
  155.     print "</TD></TR></TABLE>\n";
  156.  
  157.     &footer;
  158.     exit;
  159. }
  160.