summaryrefslogtreecommitdiffstats
path: root/extensions/TellUsMore/lib/Process.pm
blob: a7386646866e317718d5917246174708b85ce9b8 (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::TellUsMore::Process;

use strict;
use warnings;

use Bugzilla::Bug;
use Bugzilla::Component;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Hook;
use Bugzilla::Product;
use Bugzilla::User;
use Bugzilla::Util;
use Bugzilla::Version;

use Bugzilla::Extension::TellUsMore::Constants;

use Data::Dumper;
use File::Basename;
use MIME::Base64;
use Safe;

sub new {
    my $invocant = shift;
    my $class = ref($invocant) || $invocant;
    my $object = {};
    bless($object, $class);
    return $object;
}

sub execute {
    my ($self, $token) = @_;
    my $dbh = Bugzilla->dbh;

    my ($bug, $user, $is_new_user);
    Bugzilla->error_mode(ERROR_MODE_DIE);
    eval {
        $self->_delete_stale_issues();
        my ($mail, $params) = $self->_deserialise_token($token);

        $dbh->bz_start_transaction();

        $self->_fix_invalid_params($params);

        ($user, $is_new_user) = $self->_get_user($mail, $params);

        $bug = $self->_create_bug($user, $params);
        $self->_post_bug_hook($bug);

        $self->_delete_token($token);
        $dbh->bz_commit_transaction();

        $self->_send_mail($bug, $user);
    };
    $self->{error} = $@;
    Bugzilla->error_mode(ERROR_MODE_WEBPAGE);
    return $self->{error} ? undef : ($bug, $is_new_user);
}

sub error {
    my ($self) = @_;
    return $self->{error};
}

sub _delete_stale_issues {
    my ($self) = @_;
    my $dbh = Bugzilla->dbh;

    # delete issues older than TOKEN_EXPIRY_DAYS

    $dbh->do("
        DELETE FROM tell_us_more
         WHERE creation_ts < NOW() - " .
               $dbh->sql_interval(TOKEN_EXPIRY_DAYS, 'DAY')
    );
}

sub _deserialise_token {
    my ($self, $token) = @_;
    my $dbh = Bugzilla->dbh;

    # validate token

    trick_taint($token);
    my ($mail, $params) = $dbh->selectrow_array(
        "SELECT mail,content FROM tell_us_more WHERE token=?",
        undef, $token
    );
    ThrowUserError('token_does_not_exist') unless $mail;

    # deserialise, return ($mail, $params)
   
    my $compartment = Safe->new();
    $compartment->reval($params)
        || ThrowUserError('token_does_not_exist');
    $params = ${$compartment->varglob('VAR1')};

    return ($mail, $params);
}

sub _fix_invalid_params {
    my ($self, $params) = @_;

    # silently adjust any params which are no longer valid
    # so we don't lose the submission

    my $product = Bugzilla::Product->new({ name => TARGET_PRODUCT })
        || ThrowUserError('invalid_product_name', { product => TARGET_PRODUCT });

    # component --> general

    my $component = Bugzilla::Component->new({ product => $product, name => $params->{component} })
        || Bugzilla::Component->new({ product => $product, name => DEFAULT_COMPONENT })
        || ThrowUserError('tum_invalid_component', { product => TARGET_PRODUCT, name => DEFAULT_COMPONENT });
    $params->{component} = $component->name;
    
    # version --> unspecified

    my $version = Bugzilla::Version->new({ product => $product, name => $params->{version} })
        || Bugzilla::Version->new({ product => $product, name => DEFAULT_VERSION });
    $params->{version} = $version->name;
}

sub _get_user {
    my ($self, $mail, $params) = @_;

    # return existing bmo user

    my $user = Bugzilla::User->new({ name => $mail });
    return ($user, 0) if $user;

    # or create new user

    $user = Bugzilla::User->create({
        login_name => $mail,
        cryptpassword => '*',
        realname => $params->{creator_name},
    });
    return ($user, 1);
}

sub _create_bug {
    my ($self, $user, $params) = @_;
    my $template = Bugzilla->template;
    my $vars = {};

    # login as the user

    Bugzilla->set_user($user);

    # create the bug

    my $create = {
        product => $params->{product},
        component => $params->{component},
        short_desc => $params->{summary},
        comment => $params->{description},
        version => $params->{version},
        rep_platform => $params->{rep_platform},
        op_sys => $params->{op_sys},
        bug_severity => $params->{bug_severity},
        priority => $params->{priority},
        bug_file_loc => $params->{bug_file_loc},
    };
    if ($params->{group}) {
        $create->{groups} = [ $params->{group} ];
    };

    my $bug = Bugzilla::Bug->create($create);

    # add attachments

    foreach my $attachment (@{$params->{attachments}}) {
        $self->_add_attachment($bug, $attachment);
    }
    if (scalar @{$params->{attachments}}) {
        $bug->update();
    }

    return $bug;
}

sub _add_attachment {
    my ($self, $bug, $params) = @_;
    my $dbh = Bugzilla->dbh;

    # init

    my $timestamp = $dbh->selectrow_array('SELECT creation_ts FROM bugs WHERE bug_id=?', undef, $bug->bug_id);
    my $data = decode_base64($params->{content});

    my $description;
    if ($params->{description}) {
        $description = $params->{description};
    } else {
        $description = $params->{filename};
        $description =~ s/\\/\//g;
        $description = basename($description);
    }

    # trigger content-type auto detection

    Bugzilla->input_params->{'contenttypemethod'} = 'autodetect';

    # add attachment

    my $attachment = Bugzilla::Attachment->create({
        bug => $bug,
        creation_ts => $timestamp,
        data => $data,
        description => $description,
        filename => $params->{filename},
        mimetype => $params->{content_type},
    });

    # add comment

    $bug->add_comment('', {
        isprivate => 0,
        type => CMT_ATTACHMENT_CREATED,
        extra_data => $attachment->id,
    });
}

sub _post_bug_hook {
    my ($self, $bug) = @_;

    # trigger post_bug_after_creation hook

    my $vars = {
        id => $bug->bug_id,
        bug => $bug,
    };
    Bugzilla::Hook::process('post_bug_after_creation', { vars => $vars });
}

sub _send_mail {
    my ($self, $bug, $user) = @_;

    # send new-bug email

    Bugzilla::BugMail::Send($bug->bug_id, { changer => $user });
}

sub _delete_token {
    my ($self, $token) = @_;
    my $dbh = Bugzilla->dbh;

    # delete token

    trick_taint($token);
    $dbh->do('DELETE FROM tell_us_more WHERE token=?', undef, $token);
}

1;