1 package Koha::App::Plugin::CGIBinKoha;
3 # Copyright 2020 BibLibre
5 # This file is part of Koha.
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.
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.
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>.
22 use Mojo::Base 'Mojolicious::Plugin';
26 use CGI::Emulate::PSGI;
29 my ($self, $app, $conf) = @_;
31 # CGI::Compile calls CGI::initialize_globals before each request, which resets PARAM_UTF8 to 0
32 # We need to set it back to the correct value
34 no warnings 'redefine';
35 my $old_new = \&CGI::new;
38 return $old_new->(@_);
42 my $opac = $conf->{opac};
46 $r->any('/cgi-bin/koha/*script' => sub {
49 my $script = $c->stash('script');
51 # Special case for installer which can takes a long time
52 $c->inactivity_timeout(300) if $script eq 'installer/install.pl';
54 # Remove trailing slash, if any (e.g. .../svc/config/systempreferences/)
58 $script = "opac/$script";
61 unless (-e $c->app->home->rel_file($script)) {
62 return $c->reply->not_found;
65 my $sub = CGI::Compile->compile($script);
66 my $app = CGI::Emulate::PSGI->handler($sub);
67 my $response = $app->($self->_psgi_env($c));
69 $c->res->code($response->[0]);
70 $c->res->headers->from_hash({ @{ $response->[1] } });
71 $c->res->body(join('', @{$response->[2]}));
79 my $env = $c->req->env;
81 my $body = $c->req->build_body;
82 open my $input, '<', \$body or die "Can't open in-memory scalar: $!";
85 'psgi.input' => $input,
86 'psgi.errors' => *STDERR,
87 REQUEST_METHOD => $c->req->method,
88 QUERY_STRING => $c->req->url->query->to_string,
89 SERVER_NAME => $c->req->url->to_abs->host,
90 SERVER_PORT => $c->req->url->to_abs->port,
91 SERVER_PROTOCOL => 'HTTP/1.1',
92 CONTENT_LENGTH => $c->req->headers->content_length,
93 CONTENT_TYPE => $c->req->headers->content_type,
94 REMOTE_ADDR => $c->tx->remote_address,
95 SCRIPT_NAME => $c->req->url->path->to_string,
98 # Starman sets PATH_INFO to the same value of SCRIPT_NAME, which confuses
99 # CGI and causes the redirect after OPAC login to fail
100 delete $env->{PATH_INFO} if ($env->{PATH_INFO} && $env->{PATH_INFO} eq $env->{SCRIPT_NAME});
102 for my $name (@{ $c->req->headers->names }) {
103 my $value = $c->req->headers->header($name);
105 $name = 'HTTP_' . uc($name);
106 $env->{$name} = $value;
118 Koha::App::Plugin::CGIBinKoha
122 Koha App Plugin used to wrap Koha CGI scripts for backwards compatibility whilst we migrate from CGI to using the Mojolicious Web Application Framework.
128 Called at application startup; Sets up a catch-all router to identify CGI scripts and loads the found script using CGI::Compile before running it under CGI::Emulate::PSGI.