$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::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/>";