|
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 |
|
| 17 |
my $script = $c->stash('name'); |
| 18 |
|
| 19 |
# Special case for installer which can takes a long time |
| 20 |
$c->inactivity_timeout(300) if $script eq 'installer/install.pl'; |
| 21 |
|
| 22 |
# Remove trailing slash, if any (e.g. .../svc/config/systempreferences/) |
| 23 |
$script =~ s|/$||; |
| 24 |
|
| 25 |
# Guess if it's an OPAC script, or an Intranet script |
| 26 |
# FIXME: errors scripts (400.pl, 401.pl, ...) exist in both intranet and |
| 27 |
# opac. The intranet ones will never be executed |
| 28 |
# Same for svc/club/cancel_enrollment, svc/club/enroll and svc/report |
| 29 |
if (-e $c->app->home->rel_file("opac/$script")) { |
| 30 |
$script = "opac/$script"; |
| 31 |
} |
| 32 |
|
| 33 |
unless (-e $c->app->home->rel_file($script)) { |
| 34 |
return $c->reply->not_found; |
| 35 |
} |
| 36 |
|
| 37 |
my $sub = CGI::Compile->compile($script); |
| 38 |
my $app = CGI::Emulate::PSGI->handler($sub); |
| 39 |
my $response = $app->($self->_psgi_env($c)); |
| 40 |
|
| 41 |
$c->res->code($response->[0]); |
| 42 |
$c->res->headers->from_hash({ @{ $response->[1] } }); |
| 43 |
$c->res->body(join('', @{$response->[2]})); |
| 44 |
$c->rendered; |
| 45 |
})->name('cgi'); |
| 46 |
} |
| 47 |
|
| 48 |
sub _psgi_env { |
| 49 |
my ($self, $c) = @_; |
| 50 |
|
| 51 |
my $body = $c->req->body; |
| 52 |
my $env = { |
| 53 |
'psgi.input' => IO::Scalar->new(\$body), |
| 54 |
'psgi.errors' => *STDERR, |
| 55 |
REQUEST_METHOD => $c->req->method, |
| 56 |
QUERY_STRING => $c->req->url->query->to_string, |
| 57 |
SERVER_NAME => $c->req->url->to_abs->host, |
| 58 |
SERVER_PORT => $c->req->url->to_abs->port, |
| 59 |
SERVER_PROTOCOL => 'HTTP/1.1', |
| 60 |
CONTENT_LENGTH => $c->req->headers->content_length, |
| 61 |
CONTENT_TYPE => $c->req->headers->content_type, |
| 62 |
REMOTE_ADDR => $c->tx->remote_address, |
| 63 |
SCRIPT_NAME => $c->req->url->path->to_string, |
| 64 |
}; |
| 65 |
for my $name (@{ $c->req->headers->names }) { |
| 66 |
my $value = $c->req->headers->header($name); |
| 67 |
$name =~ s/-/_/g; |
| 68 |
$name = 'HTTP_' . uc($name); |
| 69 |
$env->{$name} = $value; |
| 70 |
} |
| 71 |
|
| 72 |
return $env; |
| 73 |
} |
| 74 |
|
| 75 |
1; |