forked from bugzilla/extensions-Testopia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tr_import_environment.cgi
executable file
·349 lines (278 loc) · 10.4 KB
/
tr_import_environment.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Testopia System.
#
# The Initial Developer of the Original Code is Greg Hendricks.
# Portions created by Greg Hendricks are Copyright (C) 2006
# Novell. All Rights Reserved.
#
# Contributor(s): Garrett Braden <garrett@tnbd.org>
# This script reads in an environment in xml and persists it to the database.
=head1 NAME
tr_import_environment.cgi - Imports an environment from a XML file into the database.
=head1 DESCRIPTION
Administrator's and testopia users can import an environment from XML into the database.
A tool is being designed to automatically scan a system and create such an XML file to
import.
=cut
#************************************************** Uses ****************************************************#
use strict;
use CGI;
use lib qw(. lib);
use Bugzilla;
use Bugzilla::Util;
use Bugzilla::Config;
use Bugzilla::Constants;
use Bugzilla::Error;
BEGIN { Bugzilla->extensions }
use Bugzilla::Extension::Testopia::Util;
use Bugzilla::Extension::Testopia::Environment;
use Bugzilla::Extension::Testopia::Environment::Xml;
use Data::Dumper;
#************************************ Variable Declarations/Initialization **********************************#
Bugzilla->login(LOGIN_REQUIRED);
local our $cgi = Bugzilla->cgi;
local our $template = Bugzilla->template;
local our $vars = {};
local our $upload_dir = "testopia/temp";
local our $env_filename = $cgi->param('env_file');
local our $env_fh = $cgi->upload('env_file');
local our $action = $cgi->param('action') || '';
local our $xml = $cgi->param('xml');
local our $submit = $cgi->param('submit');
local our $environment = Bugzilla::Extension::Testopia::Environment->new({'environment_id' => 0});
local our $user_can_edit = $environment->canedit;
#my $user_can_edit = 0; # Remove on production <- used to toggle user's rights.
our $message = '';
$CGI::POST_MAX = 1024 * 500; # max file size 500K
#***************************************** UI Logic ************************************************************#
print $cgi->header;
# Make sure the file isn't too big.
if (!$env_filename && $cgi->cgi_error()) {
$vars->{'tr_error'} .= "File size cannot exceed 500K.<BR/>";
}
# Upload the file and read it into a string if it's been posted.
if ($action eq 'import' && $env_filename) {
trick_taint($env_filename);
# Continue only if import exits without errors, otherwise display the errors.
if (slurp_env_file() && $xml && import()) {
# Check for new data
if (check_new_items()) {
if (write_env_file()) {
# If there's new data and the user can edit have them approve the changes.
if ($user_can_edit) {
admin_approve();
}
else {
$vars->{'tr_error'} .= "Please contact an admin who can import the above mentioned non-existing data.<BR/>";
}
}
}
# If there's no new Categories, Elements, or Properties then store the environment.
else {
store_environment(); # Stores the new Environment values based on existing Categories, Elements, Properties, and Valid Expressions.
}
}
}
# If the admin form posted
elsif ($action eq 'admin' && $xml) {
if ($user_can_edit) {
my $file = "$upload_dir/$xml";
trick_taint($file);
# If the admin clicked the 'Add Now' and not the 'Cancel' button
if ($submit eq 'Add Now') {
if (read_env_file($file)) {
import("admin"); # Creates Bugzilla::Extension::Testopia::Environment::Xml object and store's it to the database.
}
}
else {
$vars->{'tr_error'} .= "Import XML Environment Cancelled.<BR/>";
delete_env_file($file);
}
}
else {
$vars->{'tr_error'} .= "Error reading from file. Please try again.<BR/>If the problem persists please contact the site administrator.<BR/>";
}
}
display();
#*********************************************** EXISTS HERE *************************************************#
#************************************* Sub Routine Deffinitions Below ****************************************#
=head2 Create XML document
=head2 DESCRIPTION
Creates the Environment XML Document and node lists.
=cut
sub import {
# If the user is an admin and has approved to add the values pass 1 to parse().
my $admin = @_;
$environment = Bugzilla::Extension::Testopia::Environment::Xml->new($xml, $admin);
if ($environment->{'error'}) {
$vars->{'tr_error'} .= $environment->{'error'};
return 0;
}
return 1;
}
=head2 Checking Exists
=head2 DESCRIPTION
Checking if the Environment, Elements, Categories, and Properties already exist or not.
=cut
sub check_new_items {
return $environment->check_new_items();
}
=head2 Admin Approve
=head2 Description
Prepares the $vars being passed to the template to display the
Admin Approval Form first before importing an environment with
new data.
=cut
sub admin_approve {
$vars->{'action'} = "admin";
$vars->{'xml'} = $xml;
}
=head2 Store the Environment
=head2 Description
Stores the Environment based on existing Categories, Elements, Properties, and selectable values.
=cut
sub store_environment {
$environment->store();
}
=head2 Upload and read the XML file
=head2 Description
Uploads and reads the XML Environment file and prepares it into an array for parsing.
=cut
sub slurp_env_file {
my $untainted_filename;
my $post_max = $CGI::POST_MAX;
if (!$env_filename) {
$vars->{'tr_error'} .= "Please upload an Environment XML file.<BR/>";
return 0;
}
# untaint $env_file
if ($env_filename =~ /^([-\@:\/\\\w.]+)$/) {
$untainted_filename = $1;
}
else {
$vars->{'tr_error'} .= "The filename must contain numbers and letters only. Please try again.<BR/>";
return 0;
}
if ($untainted_filename =~ m/\.\./) {
$vars->{'tr_error'} .= "The filename cannot conain the sequence '..' Please try again.<BR/>";
return 0;
}
my $num_bytes = $CGI::POST_MAX;
my ($totalbytes, $byteswritten, $buffer);
while ($byteswritten = read($env_fh, $buffer, $num_bytes)) {
$xml .= $buffer;
$totalbytes += $num_bytes;
}
if (!($byteswritten && $totalbytes)) {
$vars->{'error'} .= "Problem uploading file " . $env_filename . ".<BR/>"
}
if (!$xml) {
$vars->{'tr_error'} .= "File is empty. Please upload a valid Environment XML file.<BR/>";
return 0;
}
if (length($xml) > $post_max) {
$vars->{'tr_error'} .= "File uploaded was too large. Please make sure the file size is no bigger than 500 KB.<BR/>";
return 0;
}
return 1;
}
=head2 Write the Environment XML File to the Server
=head Description
Writes the environment XML File to the $upload_dir directory where it will be read in later by the admin to be approved. Once approved it's deleted.
=cut
sub write_env_file {
# If a writable $upload_dir exists, log error details there.
if (-w "$upload_dir") {
my $timestamp = Bugzilla::Extension::Testopia::Util::get_time_stamp();
my $filename = $env_filename;
$filename =~ s/(.xml)//;
$filename .= "_" . $timestamp;
$filename =~ s/\:/./g;
$filename =~ s/[\: -]/_/g;
$filename .= ".xml";
my $untainted_filename;
if (!$filename) {
$vars->{'tr_error'} .= "Please upload an Environment XML file.<BR/>";
return 0;
}
# untaint $env_file
if ($filename =~ /^([-\@:\/\\\w.]+)$/) {
$untainted_filename = $1;
}
else {
$vars->{'tr_error'} .= "The filename must contain numbers and letters only. Please try again.<BR/>";
return 0;
}
if ($untainted_filename =~ m/\.\./) {
$vars->{'tr_error'} .= "The filename cannot conain the sequence '..' Please try again.<BR/>";
return 0;
}
open (my $fh, ">$upload_dir/$filename") || die "PROBLEM WRITING FILE.<BR/>";
print $fh $xml;
close $fh;
$xml = $filename;
}
else {
$vars->{'tr_error'} .= "Unable to write temporary environment file. Please try again.<BR/>If the problem persists please contact the site administrator.<BR/>";
return 0;
}
return 1;
}
=head2 Read the uploaded Environment XML file
=head2 Description
Uploads and reads the XML Environment file and prepares it into an array for parsing.
=cut
sub read_env_file {
my ($file) = @_;
$xml = '';
open (my $fh, "<$file") || do {
$vars->{'tr_error'} .= "Unable to read temporary environment file. Please to try again.<BR/>If the problem persists please contact the site administrator.<BR/>";
return 0;
};
binmode($fh);
while (<$fh>) {
$xml .= $_;
}
close $fh;
if (!$xml) {
$vars->{'tr_error'} .= "File is empty. Please upload a valid Environment XML file.<BR/>";
return 0;
}
if (length($xml) > $CGI::POST_MAX) {
$vars->{'tr_error'} .= "File uploaded was too large. Please make sure the file size is no bigger than 500 KB.<BR/>";
return 0;
}
return delete_env_file($file);
}
=head2 delete_env_file
=head2 Description
Deletes the temporary environment file.
=cut
sub delete_env_file {
my ($file) = @_;
if (!unlink($file)) {
$vars->{'tr_error'} .= "Unable to delete temporary environment file.<BR/>If the problem persists please contact the site administrator.<BR/>";
return 0;
}
return 1;
}
=head2 Display
=head2 Description
Displays the Import Environment template
=cut
sub display {
$vars->{'tr_message'} .= $message . $environment->{'message'};
$template->process("testopia/environment/import.xml.tmpl", $vars) || print $template->error();
}