Line 0
Link Here
|
|
|
1 |
package Koha::App::Plugin::CGIBinKoha; |
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Mojo::Base 'Mojolicious::Plugin'; |
6 |
|
7 |
use CGI::Compile; |
8 |
use CGI::Emulate::PSGI; |
9 |
use IO::Scalar; |
10 |
|
11 |
sub register { |
12 |
my ($self, $app) = @_; |
13 |
|
14 |
$app->routes->any('/cgi-bin/koha/*name' => sub { |
15 |
my ($c) = @_; |
16 |
my $script = $c->stash('name'); |
17 |
|
18 |
# Remove trailing slash, if any (e.g. .../svc/config/systempreferences/) |
19 |
$script =~ s|/$||; |
20 |
|
21 |
# Guess if it's an OPAC script, or an Intranet script |
22 |
# FIXME: errors scripts (400.pl, 401.pl, ...) exist in both intranet and |
23 |
# opac. The intranet ones will never be executed |
24 |
if (-e $c->app->home->rel_file("opac/$script")) { |
25 |
$script = "opac/$script"; |
26 |
} |
27 |
|
28 |
unless (-e $c->app->home->rel_file($script)) { |
29 |
return $c->reply->not_found; |
30 |
} |
31 |
|
32 |
my $sub = CGI::Compile->compile($script); |
33 |
my $app = CGI::Emulate::PSGI->handler($sub); |
34 |
my $response = $app->($self->_psgi_env($c)); |
35 |
|
36 |
$c->res->code($response->[0]); |
37 |
$c->res->headers->from_hash({ @{ $response->[1] } }); |
38 |
$c->res->body($response->[2][0]); |
39 |
$c->rendered; |
40 |
})->name('cgi'); |
41 |
} |
42 |
|
43 |
sub _psgi_env { |
44 |
my ($self, $c) = @_; |
45 |
|
46 |
my $body = $c->req->body; |
47 |
my $env = { |
48 |
'psgi.input' => IO::Scalar->new(\$body), |
49 |
'psgi.errors' => *STDERR, |
50 |
REQUEST_METHOD => $c->req->method, |
51 |
QUERY_STRING => $c->req->url->query->to_string, |
52 |
SERVER_NAME => $c->req->url->to_abs->host, |
53 |
SERVER_PORT => $c->req->url->to_abs->port, |
54 |
SERVER_PROTOCOL => 'HTTP/1.1', |
55 |
CONTENT_LENGTH => $c->req->headers->content_length, |
56 |
CONTENT_TYPE => $c->req->headers->content_type, |
57 |
REMOTE_ADDR => $c->tx->remote_address, |
58 |
}; |
59 |
for my $name (@{ $c->req->headers->names }) { |
60 |
my $value = $c->req->headers->header($name); |
61 |
$name =~ s/-/_/g; |
62 |
$name = 'HTTP_' . uc($name); |
63 |
$env->{$name} = $value; |
64 |
} |
65 |
|
66 |
return $env; |
67 |
} |
68 |
|
69 |
1; |