sendfeedback

  1. #!/usr/bin/perl -T
  2. ##
  3. ## feedback generator for W3C Markup Validation Service
  4.  
  5. ## Pragmas.
  6. use strict;
  7. use warnings;
  8.  
  9. ## Modules. See also the BEGIN block further down below.
  10.  
  11. use CGI qw();
  12. use File::Spec::Functions qw(catfile);
  13. use HTML::Template qw(); # Need 2.6 but can't say, rt.cpan.org#70190
  14. use Config::General 2.32 qw(); # Need 2.32 for <msg 0>, rt.cpan.org#17852
  15.  
  16. use vars qw($DEBUG $CFG %RSRC $VERSION);
  17.  
  18. # Define global constants
  19. use constant TRUE => 1;
  20. use constant FALSE => 0;
  21.  
  22. # Things inside BEGIN don't happen on every request in persistent
  23. # environments, such as mod_perl. So let's do globals, eg. read config here.
  24. BEGIN {
  25.  
  26. my $base = $ENV{W3C_VALIDATOR_HOME} || '/usr/local/validator';
  27.  
  28. # Launder data for -T; -AutoLaunder doesn't catch this one.
  29. if ($base =~ /^(.*)$/) {
  30. $base = $1;
  31. }
  32.  
  33. #
  34. # Read Config Files.
  35. eval {
  36. my %config_opts = (
  37. -ConfigFile =>
  38. ($ENV{W3C_VALIDATOR_CFG} || '/etc/w3c/validator.conf'),
  39. -MergeDuplicateOptions => TRUE,
  40. -MergeDuplicateBlocks => TRUE,
  41. -SplitPolicy => 'equalsign',
  42. -UseApacheInclude => TRUE,
  43. -IncludeRelative => TRUE,
  44. -InterPolateVars => TRUE,
  45. -AutoLaunder => TRUE,
  46. -AutoTrue => TRUE,
  47. -CComments => FALSE,
  48. -DefaultConfig => {Paths => {Base => $base,},},
  49. );
  50. my %cfg = Config::General->new(%config_opts)->getall();
  51. $CFG = \%cfg;
  52. };
  53. if ($@) {
  54. die <<"EOF";
  55. Could not read configuration. Set the W3C_VALIDATOR_CFG environment variable
  56. or copy conf/* to /etc/w3c/. Make sure that the configuration file and all
  57. included files are readable by the web server user. The error was:\n'$@'
  58. EOF
  59. }
  60. } # end of BEGIN block.
  61.  
  62. #
  63. # Get rid of (possibly insecure) $PATH.
  64. delete $ENV{PATH};
  65.  
  66. our $q = CGI->new();
  67. our $lang = 'en_US'; # @@@ TODO: conneg
  68.  
  69. # Read error message + explanations file
  70. %RSRC = Config::General->new(
  71. -MergeDuplicateBlocks => 1,
  72. -ConfigFile =>
  73. catfile($CFG->{Paths}->{Templates}, $lang, 'error_messages.cfg'),
  74. )->getall();
  75.  
  76. our $T = HTML::Template->new(
  77. filename => catfile($CFG->{Paths}->{Templates}, $lang, 'feedback.tmpl'),
  78. die_on_bad_params => FALSE,
  79. );
  80.  
  81. our $errlist = "";
  82. our $errmsg_text;
  83. our $validated_uri;
  84. our $errmsg_id;
  85.  
  86. sub process_query
  87. {
  88. $validated_uri = $q->param('uri');
  89. $errmsg_id = $q->param('errmsg_id');
  90. if ($errmsg_id) {
  91. $errmsg_text = $RSRC{msg}->{$errmsg_id}->{original};
  92. $errmsg_text = de_template_explanation($errmsg_text);
  93. }
  94.  
  95. # Trigger "thanks for your message. If your query requires an answer,..." ack paragraph
  96. my $sent = $q->param('send');
  97. if ($sent) {
  98. print "hello";
  99. if ($sent eq "yes") {
  100. $T->param(ack_ok => TRUE);
  101. }
  102. }
  103. }
  104.  
  105. sub send_message
  106. {
  107.  
  108. # sends message to www-validator list @@ TODO @@
  109. }
  110.  
  111. sub error_choices
  112. {
  113.  
  114. # creates drop-down menu with all possible error messages to send feedback about
  115. my @msgnumbers = keys(%{$RSRC{msg}});
  116. @msgnumbers = sort { $a <=> $b } @msgnumbers;
  117. my $errlabel;
  118.  
  119. for my $errnum (@msgnumbers) {
  120. $errlabel = $RSRC{msg}->{$errnum}->{original};
  121. $errlabel = de_template_explanation($errlabel);
  122. if (length($errlabel) > 70) {
  123. $errlabel = substr($errlabel, 0, 67) . "...";
  124. }
  125. $errlist = $errlist . '<option value="' . $errnum . '"';
  126. if ($errmsg_id) {
  127. if ($errnum == $errmsg_id) {
  128. $errlist = $errlist . 'selected="selected" ';
  129. }
  130. }
  131. $errlist = $errlist . "> $errnum $errlabel</option>\n";
  132. }
  133. }
  134.  
  135. sub de_template_explanation
  136. {
  137.  
  138. # takes the error message template, and replace "template keywords" with real life keywords
  139. my $explanation = shift;
  140. if ($explanation) {
  141. $explanation =~ s/\%1/X/;
  142. $explanation =~ s/\%2/Y/;
  143. $explanation =~ s/\%3/Z/;
  144. $explanation =~ s/\%4/a/;
  145. $explanation =~ s/\%5/b/;
  146. $explanation =~ s/\%6/c/;
  147. }
  148. return $explanation;
  149. }
  150.  
  151. sub prepare_error_message
  152. {
  153.  
  154. # if the form sent contains errors (what kind exactly?)
  155. # @@ TODO @@
  156. }
  157.  
  158. sub print_prefilled_form
  159. {
  160. $T->param(page_title => "Feedback");
  161. $T->param(is_feedback => 1);
  162. $T->param(uri => $validated_uri);
  163. $T->param(errmsg_id => $errmsg_id);
  164.  
  165. # $T->param(errlist => $errlist);
  166. $T->param(explanation => $errmsg_text);
  167. print $T->output;
  168. }
  169.  
  170. process_query;
  171.  
  172. #error_choices;
  173. print_prefilled_form;
  174.  
  175. # Local Variables:
  176. # mode: perl
  177. # indent-tabs-mode: nil
  178. # cperl-indent-level: 4
  179. # cperl-continued-statement-offset: 4
  180. # cperl-brace-offset: -4
  181. # perl-indent-level: 4
  182. # End:
  183. # ex: ts=4 sw=4 et
Programming language: 
Perl