Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / share / html / REST / 1.0 / ticket / comment
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %#
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %#
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %#
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 %# REST/1.0/ticket/comment
49 %#
50 <%ARGS>
51 $content
52 </%ARGS>
53 <%INIT>
54 use MIME::Entity;
55 use LWP::MediaTypes;
56 use RT::Interface::REST;
57 use File::Temp qw(tempfile);
58 my @tmp_files;
59
60 my $ticket = RT::Ticket->new($session{CurrentUser});
61 my $object = $r->path_info;
62 my $status = "200 Ok";
63 my $output;
64 my $action;
65
66 # http://.../REST/1.0/ticket/1/comment
67 my ($c, $o, $k, $e) = @{ form_parse($content)->[0] };
68 if ($e || !$o) {
69     if (!$o) {
70         $output = "Empty form submitted.\n";
71     }
72     else {
73         $c = "# Syntax error.";
74         $output = form_compose([[$c, $o, $k, $e]]);
75     }
76     $status = "400 Bad Request";
77     goto OUTPUT;
78 }
79
80 $object =~ s#^/##;
81 $object ||= $k->{Ticket};
82 unless ($object =~ /^\d+/) {
83     $output = "Invalid ticket id: `$object'.\n";
84     $status = "400 Bad Request";
85     goto OUTPUT;
86 }
87 if ($k->{Ticket} && $object ne $k->{Ticket}) {
88     $output = "The submitted form and URL specify different tickets.\n";
89     $status = "400 Bad Request";
90     goto OUTPUT;
91 }
92
93 ($action = $k->{Action}) =~ s/^(.)(.*)$/\U$1\L$2\E/;
94 unless ($action =~ /^(?:Comment|Correspond)$/) {
95     $output = "Invalid action: `$action'.\n";
96     $status = "400 Bad Request";
97     goto OUTPUT;
98 }
99
100 my $text = $k->{Text};
101 my @atts = @{ vsplit($k->{Attachment}) };
102
103 if (!$k->{Text} && @atts == 0) {
104         $status = "400 Bad Request";
105         $output = "Empty comment with no attachments submitted.\n";
106         goto OUTPUT;
107 }
108
109 my $cgi = $m->cgi_object;
110 my $ent = MIME::Entity->build(
111     Type => "multipart/mixed",
112     'X-RT-Interface' => 'REST',
113 );
114 $ent->attach(Data => $k->{Text}) if $k->{Text};
115
116 my $i = 1;
117 foreach my $att (@atts) {
118     local $/=undef;
119     my $file = $att;
120     $file =~ s#^.*[\\/]##;
121
122     my $fh = $cgi->upload("attachment_$i");
123     if ($fh) {
124         my $buf;
125         my ($w, $tmp) = tempfile();
126         push @tmp_files, $tmp;
127         my $info = $cgi->uploadInfo();
128
129         while (sysread($fh, $buf, 8192)) {
130             syswrite($w, $buf);
131         }
132
133         $ent->attach(
134             Path => $tmp,
135             Type => $info->{'Content-Type'} || guess_media_type($tmp),
136             Filename => $file,
137             Disposition => "attachment"
138         );
139     }
140     else {
141         $status = "400 Bad Request";
142         $output = "No attachment for $att.\n";
143         goto OUTPUT;
144     }
145
146     $i++;
147 }
148
149 $ticket->Load($object);
150 unless ($ticket->Id) {
151     $output = "Couldn't load ticket id: `$object'.\n";
152     $status = "404 Ticket not found";
153     goto OUTPUT;
154 }
155 unless ($ticket->CurrentUserHasRight('ModifyTicket') ||
156         ($action eq "Comment" &&
157          $ticket->CurrentUserHasRight("CommentOnTicket")) ||
158         ($action eq "Correspond" &&
159          $ticket->CurrentUserHasRight("ReplyToTicket")))
160 {
161     $output = "You are not allowed to $action on ticket $object.\n";
162     $status = "403 Permission denied";
163     goto OUTPUT;
164 }
165
166 my $cc = join ", ", @{ vsplit($k->{Cc}) };
167 my $bcc = join ", ", @{ vsplit($k->{Bcc}) };
168 my ($n, $s) = $ticket->$action(MIMEObj => $ent,
169                                CcMessageTo => $cc,
170                                BccMessageTo => $bcc,
171                                TimeTaken => $k->{TimeWorked} || 0);
172 $output = $s;
173 if ($k->{Status}) {
174    my  ($status_n, $status_s) = $ticket->SetStatus($k->{'Status'} );   
175   $output .= "\n".$status_s;
176 }
177
178 OUTPUT:
179
180 unlink @tmp_files;
181 </%INIT>
182 RT/<% $RT::VERSION %> <% $status %>
183
184 <% $output |n %>