-
Notifications
You must be signed in to change notification settings - Fork 14
/
tr_attachment.cgi
executable file
·219 lines (180 loc) · 6.92 KB
/
tr_attachment.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
#!/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): Greg Hendricks <[email protected]>
use strict;
use lib qw(. lib);
use Bugzilla;
use Bugzilla::Util;
use Bugzilla::Constants;
use Bugzilla::Error;
BEGIN { Bugzilla->extensions }
use Bugzilla::Extension::Testopia::Util;
use Bugzilla::Extension::Testopia::Attachment;
use Bugzilla::Extension::Testopia::Search;
use Bugzilla::Extension::Testopia::Table;
use Bugzilla::Extension::Testopia::Constants;
my $vars = {};
my $template = Bugzilla->template;
my $cgi = Bugzilla->cgi;
my $dbh = Bugzilla->dbh;
Bugzilla->login(LOGIN_REQUIRED);
Bugzilla->error_mode(ERROR_MODE_AJAX) if $cgi->param('ctype') && $cgi->param('ctype') eq 'json';
my $action = $cgi->param('action') || '';
my $attach_id = $cgi->param('attach_id');
detaint_natural($attach_id) if $attach_id;
if (!$attach_id and $cgi->param('ctype') ne 'json'){
print $cgi->header();
$template->process("testopia/attachment/choose.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
##################
### Edit ###
##################
if ($action eq 'edit') {
print $cgi->header;
validate_test_id($attach_id,'attachment');
my $attachment = Bugzilla::Extension::Testopia::Attachment->new($attach_id);
ThrowUserError('testopia-permission-denied', {'object' => $attachment}) unless $attachment->canedit;
$attachment->set_description($cgi->param('description')) if $cgi->param('description');
$attachment->set_filename($cgi->param('filename')) if $cgi->param('filename');
$attachment->set_mime_type($cgi->param('mime_type')) if $cgi->param('mime_type');
$attachment->update();
print "{success: true}";
}
####################
### Unlink ###
####################
elsif ($action eq 'remove') {
print $cgi->header;
my $item = $cgi->param('object');
my $item_id = $cgi->param('object_id');
my $obj;
if ($item eq 'case'){
$obj = Bugzilla::Extension::Testopia::TestCase->new($item_id);
}
elsif ($item eq 'plan'){
$obj = Bugzilla::Extension::Testopia::TestPlan->new($item_id);
}
elsif ($item eq 'caserun'){
$obj = Bugzilla::Extension::Testopia::TestCaseRun->new($item_id);
$obj = $obj->case;
}
ThrowUserError('testopia-missing-parameter', {'param' => 'case_id or plan_id'}) unless $obj;
foreach my $attach_id (split(',', $cgi->param('attach_ids'))){
validate_test_id($attach_id,'attachment');
my $attachment = Bugzilla::Extension::Testopia::Attachment->new($attach_id);
ThrowUserError('testopia-no-delete', {'object' => $attachment}) unless $attachment->candelete;
if ($obj->type eq 'plan'){
$attachment->unlink_plan($obj->id);
}
elsif ($obj->type eq 'case'){
$attachment->unlink_case($obj->id);
}
}
print "{success: true}";
}
elsif ($action eq 'add'){
print $cgi->header;
my $item = $cgi->param('object');
my $item_id = $cgi->param('object_id');
my $obj;
my $att;
if ($item eq 'case'){
$obj = Bugzilla::Extension::Testopia::TestCase->new($item_id);
$att->{'case_id'} = $obj->id;
}
elsif ($item eq 'plan'){
$obj = Bugzilla::Extension::Testopia::TestPlan->new($item_id);
$att->{'plan_id'} = $obj->id;
}
elsif ($item eq 'caserun'){
$obj = Bugzilla::Extension::Testopia::TestCaseRun->new($item_id);
$att->{'caserun_id'} = $obj->id;
$att->{'case_id'} = $obj->case_id;
}
ThrowUserError("testopia-read-only", {'object' => $obj}) unless $obj->canedit;
defined $cgi->upload('data')
|| ThrowUserError("file_not_specified");
my $fh = $cgi->upload('data');
my $data;
# enable 'slurp' mode
local $/;
$data = <$fh>;
$data || ThrowUserError("zero_length_file");
$att->{'submitter_id'} = Bugzilla->user->id;
$att->{'description'} = $cgi->param("description") || 'Attachment';
$att->{'filename'} = $cgi->upload("data");
$att->{'mime_type'} = $cgi->uploadInfo($cgi->param("data"))->{'Content-Type'};
$att->{'contents'} = $data;
my $attachment = Bugzilla::Extension::Testopia::Attachment->create($att);
print "{success: true}";
}
################
### List ###
################
elsif ($action eq 'list') {
my $format = $template->get_format("testopia/attachment/list", scalar $cgi->param('format'), scalar $cgi->param('ctype'));
print $cgi->header;
my $item = $cgi->param('object');
my $item_id = $cgi->param('object_id');
my $obj;
if ($item eq 'case'){
$obj = Bugzilla::Extension::Testopia::TestCase->new($item_id);
}
elsif ($item eq 'plan'){
$obj = Bugzilla::Extension::Testopia::TestPlan->new($item_id);
}
elsif ($item eq 'caserun'){
$obj = Bugzilla::Extension::Testopia::TestCaseRun->new($item_id);
}
ThrowUserError("testopia-read-only", {'object' => $obj}) unless $obj->canview;
my @attachments = @{$obj->attachments};
my $out = '';
$out .= '{';
$out .= '"totalResultsAvailable":' . scalar @attachments .',';
$out .= '"attachment":[';
foreach my $i (@attachments){
$out .= $i->TO_JSON($cgi) . ',' if $i->canview;
}
chop($out) if scalar @attachments;
$out .= ']}';
$vars->{'json'} = $out;
$template->process($format->{'template'}, $vars)
|| ThrowTemplateError($template->error());
exit;
}
################
### View ###
################
else {
validate_test_id($attach_id,'attachment');
my $attachment = Bugzilla::Extension::Testopia::Attachment->new($attach_id);
ThrowUserError("attachment_removed") if $attachment->datasize == 0;
ThrowUserError('testopia-permission-denied', {'object' => $attachment}) unless $attachment->canview;
my $filename = $attachment->filename;
$filename =~ s/\\/\\\\/g; # escape backslashes
$filename =~ s/"/\\"/g; # escape quotes
print $cgi->header(-type => $attachment->mime_type . "; name=\"$filename\"",
-content_disposition => "inline; filename=\"$filename\"",
-content_length => $attachment->datasize);
disable_utf8();
print $attachment->contents;
}