Line 0
Link Here
|
|
|
1 |
package Koha::App::Plugin::CGIBinKoha; |
2 |
|
3 |
# Copyright 2020 BibLibre |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Mojo::Base 'Mojolicious::Plugin'; |
23 |
|
24 |
use CGI::Compile; |
25 |
use CGI::Emulate::PSGI; |
26 |
use IO::Scalar; |
27 |
|
28 |
sub register { |
29 |
my ($self, $app, $conf) = @_; |
30 |
|
31 |
my $opac = $conf->{opac}; |
32 |
|
33 |
my $r = $app->routes; |
34 |
|
35 |
$r->any('/cgi-bin/koha/*script' => sub { |
36 |
my ($c) = @_; |
37 |
|
38 |
my $script = $c->stash('script'); |
39 |
|
40 |
# Special case for installer which can takes a long time |
41 |
$c->inactivity_timeout(300) if $script eq 'installer/install.pl'; |
42 |
|
43 |
# Remove trailing slash, if any (e.g. .../svc/config/systempreferences/) |
44 |
$script =~ s|/$||; |
45 |
|
46 |
if ($opac) { |
47 |
$script = "opac/$script"; |
48 |
} |
49 |
|
50 |
unless (-e $c->app->home->rel_file($script)) { |
51 |
return $c->reply->not_found; |
52 |
} |
53 |
|
54 |
my $sub = CGI::Compile->compile($script); |
55 |
my $app = CGI::Emulate::PSGI->handler($sub); |
56 |
my $response = $app->($self->_psgi_env($c)); |
57 |
|
58 |
$c->res->code($response->[0]); |
59 |
$c->res->headers->from_hash({ @{ $response->[1] } }); |
60 |
$c->res->body(join('', @{$response->[2]})); |
61 |
$c->rendered; |
62 |
})->name('cgi'); |
63 |
} |
64 |
|
65 |
sub _psgi_env { |
66 |
my ($self, $c) = @_; |
67 |
|
68 |
my $env = $c->req->env; |
69 |
|
70 |
my $body = $c->req->build_body; |
71 |
$env = { |
72 |
%$env, |
73 |
'psgi.input' => IO::Scalar->new(\$body), |
74 |
'psgi.errors' => *STDERR, |
75 |
REQUEST_METHOD => $c->req->method, |
76 |
QUERY_STRING => $c->req->url->query->to_string, |
77 |
SERVER_NAME => $c->req->url->to_abs->host, |
78 |
SERVER_PORT => $c->req->url->to_abs->port, |
79 |
SERVER_PROTOCOL => 'HTTP/1.1', |
80 |
CONTENT_LENGTH => $c->req->headers->content_length, |
81 |
CONTENT_TYPE => $c->req->headers->content_type, |
82 |
REMOTE_ADDR => $c->tx->remote_address, |
83 |
SCRIPT_NAME => $c->req->url->path->to_string, |
84 |
}; |
85 |
|
86 |
# Starman sets PATH_INFO to the same value of SCRIPT_NAME, which confuses |
87 |
# CGI and causes the redirect after OPAC login to fail |
88 |
delete $env->{PATH_INFO} if ($env->{PATH_INFO} eq $env->{SCRIPT_NAME}); |
89 |
|
90 |
for my $name (@{ $c->req->headers->names }) { |
91 |
my $value = $c->req->headers->header($name); |
92 |
$name =~ s/-/_/g; |
93 |
$name = 'HTTP_' . uc($name); |
94 |
$env->{$name} = $value; |
95 |
} |
96 |
|
97 |
return $env; |
98 |
} |
99 |
|
100 |
1; |