source: branches/stable/cgi-bin/CommonWeb.pm@ 550

Last change on this file since 550 was 507, checked in by Kris Deugau, 13 years ago

/branches/stable

Make the fixed web path at least configurable in one place rather
than completely hardcoded across many files.
Update initial database tabledef SQL
Bump version

  • Property svn:keywords set to Date Rev Author
File size: 2.9 KB
Line 
1# ipdb/cgi-bin/CommonWeb.pm
2###
3# SVN revision info
4# $Date: 2011-11-15 23:08:14 +0000 (Tue, 15 Nov 2011) $
5# SVN revision $Rev: 507 $
6# Last update by $Author: kdeugau $
7###
8
9package CommonWeb;
10
11use strict;
12use warnings;
13use Exporter;
14use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
15
16$VERSION = 1.00;
17@ISA = qw(Exporter);
18@EXPORT_OK = qw(&parse_post &printFooter &printHeader &printError &printAndExit &desanitize &cleanInput &desanitize);
19
20@EXPORT = (); #export nothing by default
21%EXPORT_TAGS = ( ALL => [qw( &parse_post &printFooter &printHeader &printError
22 &printAndExit &desanitize &cleanInput )],
23 lean => [qw( &parse_post &printFooter &printHeader &printError
24 &printAndExit &cleanInput )]
25 );
26
27sub parse_post {
28 my $buffer;
29 if ($ENV{'REQUEST_METHOD'} eq "GET") {
30 $buffer=$ENV{'QUERY_STRING'}
31 } elsif ($ENV{'REQUEST_METHOD'} eq 'POST' && $ENV{'CONTENT_TYPE'} eq "application/x-www-form-urlencoded") {
32 read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
33 } else {
34 $buffer = $ENV{'QUERY_STRING'};
35 $buffer || read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
36 }
37 my @pairs = split(/&/, $buffer);
38 my %webvarLocal;
39 foreach my $pair (@pairs) {
40 my ($name, $value) = split(/=/, $pair);
41 $name =~ tr/+/ /;
42 $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
43 $value =~ tr/+/ /;
44 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
45 $value =~ s/\'/\\\'/g;
46 $webvarLocal{$name} = $value;
47 }
48 return %webvarLocal;
49}
50
51
52sub printHeader {
53 my $title = shift;
54 my $webpath = shift;
55 print "Content-type: text/html\n\n";
56# This doesn't work well. Must investigate.
57# my $realm = shift;
58# print qq(WWW-Authenticate: Basic realm="$realm"\n) if $realm;
59 open FILE, "../header.inc"
60 or carp $!;
61 my $html = join('',<FILE>);
62 close FILE;
63
64 $html =~ s/\$\$TITLE\$\$/$title/;
65 $html =~ s/\$\$WEBPATH\$\$/$webpath/g;
66# Necessary for mangling arbitrary bits of the header
67 my $i=0;
68 while (defined(my $param = shift)) {
69 $html =~ s/\$\$EXTRA$i\$\$/$param/g;
70 $i++;
71 }
72 print $html;
73}
74
75sub printFooter
76{
77 open FILE, "../footer.inc"
78 or croak $!;
79 while (<FILE>)
80 {
81 print;
82 }
83 close FILE;
84}
85
86sub printError($)
87{
88 my $errStr = $_[0];
89 print qq(
90 <center><p class="regular"> $errStr </p>
91 <input type="button" value="Back" onclick="history.go(-1)">
92 </center>
93 );
94}
95
96sub printAndExit($)
97{
98 my $errStr = $_[0];
99 print qq(
100 <center><p class="regular"> $errStr </p>
101 <input type="button" value="Back" onclick="history.go(-1)">
102 </center>
103 );
104 printFooter();
105 exit(0);
106}
107
108# needs a reference to the webvar hash.
109# takes out backticks and single quotes
110sub cleanInput($)
111{
112 my $hashRef = $_[0];
113
114 foreach my $key (keys %$hashRef)
115 {
116 $hashRef->{$key} =~ s/`/\\`/g;
117 $hashRef->{$key} =~ s/'/\'/g;
118 }
119}
120
121# undoes clean input. takes a string as an arg.
122sub desanitize($)
123{
124 my $string = $_[0];
125 $string =~ s/\\`/`/g;
126 $string =~ s/\\'/'/g;
127 return $string;
128}
129
130# indicate that the module loaded okay.
1311;
Note: See TracBrowser for help on using the repository browser.