1 |
|
---|
2 | package gsdlCGI;
|
---|
3 |
|
---|
4 | use CGI;
|
---|
5 |
|
---|
6 | @ISA = ('CGI');
|
---|
7 |
|
---|
8 | sub new {
|
---|
9 | my $class = shift @_;
|
---|
10 | my ($mode) = @_;
|
---|
11 |
|
---|
12 |
|
---|
13 | my $self;
|
---|
14 | if ((defined $mode) && ($mode eq "+cmdline")) {
|
---|
15 | my $line = <STDIN>;
|
---|
16 | if ((defined $line) && ($line ne "")) {
|
---|
17 | $self = new CGI($line);
|
---|
18 | }
|
---|
19 | else {
|
---|
20 | $self = new CGI();
|
---|
21 | }
|
---|
22 | }
|
---|
23 | else {
|
---|
24 | $self = new CGI();
|
---|
25 | }
|
---|
26 |
|
---|
27 | my $xml = (defined $self->param("xml")) ? 1 : 0;
|
---|
28 |
|
---|
29 | $self->{'xml'} = $xml;
|
---|
30 |
|
---|
31 | my @var_names = $self->param;
|
---|
32 | my @arg_list = ();
|
---|
33 | foreach my $n ( @var_names ) {
|
---|
34 | my $arg = "$n=";
|
---|
35 | my $val = $self->param($n);
|
---|
36 | $arg .= $val if (defined $val);
|
---|
37 | push(@arg_list,$arg);
|
---|
38 | }
|
---|
39 |
|
---|
40 | $self->{'args'} = join("&",@arg_list);
|
---|
41 |
|
---|
42 | return bless $self, $class;
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | sub clean_param
|
---|
47 | {
|
---|
48 | my $self = shift @_;
|
---|
49 | my ($param) = @_;
|
---|
50 |
|
---|
51 | my $val = $self->SUPER::param($param);
|
---|
52 | $val =~ s/[\r\n]+$// if (defined $val);
|
---|
53 |
|
---|
54 | return $val;
|
---|
55 | }
|
---|
56 |
|
---|
57 | sub safe_val
|
---|
58 | {
|
---|
59 | my $self = shift @_;
|
---|
60 | my ($val) = @_;
|
---|
61 |
|
---|
62 | # ensure only alpha-numeric plus a few other special chars remain
|
---|
63 |
|
---|
64 | $val =~ s/[^[:alnum:]@\.\/\- :]//g if (defined $val);
|
---|
65 |
|
---|
66 | return $val;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | sub generate_error
|
---|
71 | {
|
---|
72 | my $self = shift @_;
|
---|
73 | my ($mess) = @_;
|
---|
74 |
|
---|
75 | my $xml = $self->{'xml'};
|
---|
76 |
|
---|
77 | my $full_mess;
|
---|
78 | my $args = $self->{'args'};
|
---|
79 |
|
---|
80 | if ($xml) {
|
---|
81 | $full_mess = "<Error>\n";
|
---|
82 | $full_mess .= " $mess\n";
|
---|
83 | $full_mess .= " CGI args were: $args\n";
|
---|
84 | $full_mess .= "</Error>\n";
|
---|
85 | }
|
---|
86 | else {
|
---|
87 | $full_mess = "ERROR: $mess ($args)\n";
|
---|
88 | }
|
---|
89 |
|
---|
90 | print STDOUT "Content-type:text/plain\n\n";
|
---|
91 | print STDOUT $full_mess;
|
---|
92 |
|
---|
93 | die $full_mess;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | sub generate_ok_message
|
---|
98 | {
|
---|
99 | my $self = shift @_;
|
---|
100 | my ($mess) = @_;
|
---|
101 |
|
---|
102 | my $xml = $self->{'xml'};
|
---|
103 |
|
---|
104 | my $full_mess;
|
---|
105 |
|
---|
106 | if ($xml) {
|
---|
107 | $full_mess = "<Accepted>\n";
|
---|
108 | $full_mess .= " $mess\n";
|
---|
109 | $full_mess .= "</Accepted>\n";
|
---|
110 | }
|
---|
111 | else {
|
---|
112 | $full_mess = "$mess\n";
|
---|
113 | }
|
---|
114 |
|
---|
115 | print STDOUT "Content-type:text/plain\n\n";
|
---|
116 | print STDOUT $full_mess;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 |
|
---|
121 | sub get_config_info {
|
---|
122 | my $self = shift @_;
|
---|
123 | my ($infotype) = @_;
|
---|
124 |
|
---|
125 | my $site_filename = "gsdlsite.cfg";
|
---|
126 | open (FILEIN, "<$site_filename")
|
---|
127 | || $self->generate_error("Could not open gsdlsite.cfg");
|
---|
128 |
|
---|
129 | my $config_content = "";
|
---|
130 | while(defined (my $line = <FILEIN>)) {
|
---|
131 | $config_content .= $line;
|
---|
132 | }
|
---|
133 | close(FILEIN);
|
---|
134 |
|
---|
135 |
|
---|
136 | my ($loc) = ($config_content =~ m/^$infotype\s+(\S+)\s*\n/m);
|
---|
137 | $loc =~ s/\"//g;
|
---|
138 |
|
---|
139 | if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
|
---|
140 | $self->generate_error("$infotype is not set in gsdlsite.cfg");
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | return $loc;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 | sub rm_rf
|
---|
150 | {
|
---|
151 | my $self = shift @_;
|
---|
152 | my ($dir) = @_;
|
---|
153 |
|
---|
154 | # Delete recursively with force flag on
|
---|
155 |
|
---|
156 | # Currently, Unix specific, need to generalise to Windows etc.
|
---|
157 | # => hook in with util.pm ?
|
---|
158 |
|
---|
159 | my $cmd = "/bin/rm -rf $dir 2>&1"; # Unix specific
|
---|
160 |
|
---|
161 | my $output = `$cmd`;
|
---|
162 |
|
---|
163 | if ($?>0) {
|
---|
164 | $self->generate_error("Failed to delete directory: $dir\n\n$output");
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | sub unix_cmd
|
---|
169 | {
|
---|
170 | my $self = shift @_;
|
---|
171 | my ($cmd) = @_;
|
---|
172 |
|
---|
173 | $cmd = "( $cmd ) 2>&1";
|
---|
174 |
|
---|
175 | my $output = `$cmd`;
|
---|
176 |
|
---|
177 | if ($?>0) {
|
---|
178 | $self->generate_error("Failed to run command: $cmd\n\n$output");
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | sub checked_chdir
|
---|
183 | {
|
---|
184 | my $self = shift @_;
|
---|
185 | my ($dir) = @_;
|
---|
186 |
|
---|
187 | if (!-e $dir) {
|
---|
188 | $self->generate_error("Directory '$dir' does not exist");
|
---|
189 | }
|
---|
190 |
|
---|
191 | chdir $dir
|
---|
192 | || $self->generate_error("Unable to change to directory: $dir");
|
---|
193 | }
|
---|
194 |
|
---|
195 | 1;
|
---|