summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Quantum/CGI.pm
blob: 4d43158cc3a3c6243d542b2cfc097883d4a48e1e (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
# 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::Quantum::CGI;
use Mojo::Base 'Mojolicious::Controller';

use CGI::Compile;
use Bugzilla::Constants qw(bz_locations);
use File::Slurper qw(read_text);
use File::Spec::Functions qw(catfile);
use Sub::Quote 2.005000;
use Taint::Util qw(untaint);

my %CGIS;
my %SKIP = ( 'xmlrpc.cgi' => 1, 'jsonrpc.cgi' => 1, 'rest.cgi' => 1);

_load_all();

sub expose_routes {
    my ($class, $r) = @_;
    foreach my $cgi (keys %CGIS) {
        $r->any("/$cgi")->to("CGI#$CGIS{$cgi}");
    }
}

sub _load_all {
    foreach my $script (glob '*.cgi') {
        next if $SKIP{$script};
        my $name = _load_cgi($script);
        $CGIS{ $script } = $name;
    }
}

sub _load_cgi {
    my ($file) = @_;
    my $name = $file;
    $name =~ s/\.cgi$//s;
    $name =~ s/\W+/_/gs;
    my $subname = "handle_$name";
    my $content = read_text(catfile(bz_locations->{cgi_path}, $file));
    untaint($content);
    $content = 'my ($self) = @_; ' . $content;
    my %options = (
        package => __PACKAGE__ . "::$name",
        file    => $file,
        line    => 1,
        no_defer => 1,
    );
    quote_sub $subname, $content, {}, \%options;
    return $subname;
}

1;