source: other-projects/trunk/realistic-books/cgi-bin/rbCGI.pm@ 19731

Last change on this file since 19731 was 19731, checked in by davidb, 15 years ago

Addition of CGI-bin support

File size: 4.8 KB
Line 
1package rbCGI;
2
3
4use strict;
5no strict 'subs';
6no strict 'refs'; # allow filehandles to be variables and viceversa
7
8use CGI;
9use Cwd;
10use MIME::Base64;
11
12@rbCGI::ISA = ( 'CGI' );
13
14sub new {
15 my $class = shift @_;
16
17 my $self = new CGI;
18
19 $self->{'site_filename'} = "rbsite.cfg";
20
21 return bless $self, $class;
22}
23
24
25sub parse_cgi_args
26{
27 my $self = shift @_;
28 my $xml = (defined $self->param("xml")) ? 1 : 0;
29
30 $self->{'xml'} = $xml;
31
32 my @var_names = $self->param;
33 my @arg_list = ();
34 foreach my $n ( @var_names ) {
35 my $arg = "$n=";
36 my $val = $self->param($n);
37 $arg .= $val if (defined $val);
38 push(@arg_list,$arg);
39 }
40
41 $self->{'args'} = join("&",@arg_list);
42}
43
44
45sub clean_param
46{
47 my $self = shift @_;
48 my ($param) = @_;
49
50 my $val = $self->SUPER::param($param);
51 $val =~ s/[\r\n]+$// if (defined $val);
52
53 return $val;
54}
55
56sub safe_val
57{
58 my $self = shift @_;
59 my ($val) = @_;
60
61 # ensure only alpha-numeric plus a few other special chars remain
62
63 $val =~ s/[^[:alnum:]@\.\/\- :_]//g if (defined $val);
64
65 return $val;
66}
67
68sub generate_message
69{
70 my $self = shift @_;
71 my ($message) = @_;
72
73 print STDOUT "Content-type:text/plain\n\n$message";
74
75}
76
77sub generate_error
78{
79 my $self = shift @_;
80 my ($mess) = @_;
81
82 my $xml = $self->{'xml'};
83
84 my $full_mess;
85 my $args = $self->{'args'};
86
87 if ($xml) {
88 # Make $args XML safe
89 my $args_xml_safe = $args;
90 $args_xml_safe =~ s/&/&/g;
91
92 $full_mess = "<Error>\n";
93 $full_mess .= " $mess\n";
94 $full_mess .= " CGI args were: $args_xml_safe\n";
95 $full_mess .= "</Error>\n";
96 }
97 else {
98 $full_mess = "ERROR: $mess\n ($args)\n";
99 }
100
101 $self->generate_message($full_mess);
102
103 die $full_mess;
104}
105
106sub generate_warning
107{
108 my $self = shift @_;
109 my ($mess) = @_;
110
111 my $xml = $self->{'xml'};
112
113 my $full_mess;
114 my $args = $self->{'args'};
115
116 if ($xml) {
117 # Make $args XML safe
118 my $args_xml_safe = $args;
119 $args_xml_safe =~ s/&/&amp;/g;
120
121 $full_mess = "<Warning>\n";
122 $full_mess .= " $mess\n";
123 $full_mess .= " CGI args were: $args_xml_safe\n";
124 $full_mess .= "</Warning>\n";
125 }
126 else {
127 $full_mess = "Warning: $mess ($args)\n";
128 }
129
130 $self->generate_message($full_mess);
131
132 print STDERR $full_mess;
133}
134
135
136sub generate_ok_message
137{
138 my $self = shift @_;
139 my ($mess) = @_;
140
141 my $xml = $self->{'xml'};
142
143 my $full_mess;
144
145 if ($xml) {
146 $full_mess = "<Accepted>\n";
147 $full_mess .= " $mess\n";
148 $full_mess .= "</Accepted>\n";
149 }
150 else {
151 $full_mess = "$mess\n";
152 }
153
154 $self->generate_message($full_mess);
155}
156
157
158
159sub get_config_info {
160 my $self = shift @_;
161 my ($infotype, $optional) = @_;
162
163 my $site_filename = $self->{'site_filename'};
164 open (FILEIN, "<$site_filename")
165 || $self->generate_error("Could not open $site_filename");
166
167 my $config_content = "";
168 while(defined (my $line = <FILEIN>)) {
169 $config_content .= $line;
170 }
171 close(FILEIN);
172
173 my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
174 $loc =~ s/\"//g if defined $loc;
175
176 if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
177 if((!defined $optional) || (!$optional)) {
178 $self->generate_error("$infotype is not set in $site_filename");
179 }
180 }
181
182 return $loc;
183}
184
185
186sub get_rb_home {
187 my $self = shift @_;
188
189 if (defined $self->{'rbhome'}) {
190 return $self->{'rbhome'};
191 }
192
193 my $rbhome = $self->get_config_info("rbhome");
194
195 $rbhome =~ s/(\/|\\)$//; # remove trailing slash
196
197 $self->{'rbhome'} = $rbhome;
198
199 return $rbhome;
200}
201
202sub get_rb_os {
203 my $self = shift @_;
204
205 my $os = $^O;
206
207 if ($os =~ m/linux/i) {
208 return "linux";
209 }
210 elsif ($os =~ m/mswin/i) {
211 return "windows";
212 }
213 elsif ($os =~ m/macos/i) {
214 return "darwin";
215 }
216 else {
217 # return as is.
218 return $os;
219 }
220}
221
222
223sub setup_rb {
224 my $self = shift @_;
225 my $optional = 1; # ignore absence of specified properties in rbsite.cfg if not found
226
227 my $rbhome = $self->get_rb_home();
228 my $rbos = $self->get_rb_os();
229 $ENV{'REALISTIC_BOOKS_HOME'} = $rbhome;
230 $ENV{'RBOS'} = $rbos;
231 $ENV{'GSDLOS'} = $rbos;
232
233 require util;
234
235 my $rb_bin_script = &util::filename_cat($rbhome,"bin","script");
236 &util::envvar_append("PATH",$rb_bin_script);
237
238 my $rb_bin_os = &util::filename_cat($rbhome,"bin",$rbos);
239 &util::envvar_append("PATH",$rb_bin_os);
240
241
242 my $rb_bin_perl = &util::filename_cat($rbhome,"bin",$rbos,"perl","bin");
243 &util::envvar_append("PATH",$rb_bin_perl);
244
245}
246
247
248sub checked_chdir
249{
250 my $self = shift @_;
251 my ($dir) = @_;
252
253 if (!-e $dir) {
254 $self->generate_error("Directory '$dir' does not exist");
255 }
256
257 chdir $dir
258 || $self->generate_error("Unable to change to directory: $dir");
259}
260
261
262sub decode {
263 my ($self, $text) = @_;
264 $text =~ s/\+/ /g;
265 $text = &MIME::Base64::decode_base64($text);
266
267 return $text;
268}
269
2701;
271
Note: See TracBrowser for help on using the repository browser.