Bug 29871: Remove marcflavour param in Koha::Biblio->get_marc_notes
[koha.git] / Koha / App / Plugin / CGIBinKoha.pm
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;
25 use CGI::Compile;
26 use CGI::Emulate::PSGI;
27
28 sub register {
29     my ($self, $app, $conf) = @_;
30
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
33     {
34         no warnings 'redefine';
35         my $old_new = \&CGI::new;
36         *CGI::new = sub {
37             $CGI::PARAM_UTF8 = 1;
38             return $old_new->(@_);
39         };
40     }
41
42     my $opac = $conf->{opac};
43
44     my $r = $app->routes;
45
46     $r->any('/cgi-bin/koha/*script' => sub {
47         my ($c) = @_;
48
49         my $script = $c->stash('script');
50
51         # Special case for installer which can takes a long time
52         $c->inactivity_timeout(300) if $script eq 'installer/install.pl';
53
54         # Remove trailing slash, if any (e.g. .../svc/config/systempreferences/)
55         $script =~ s|/$||;
56
57         if ($opac) {
58             $script = "opac/$script";
59         }
60
61         unless (-e $c->app->home->rel_file($script)) {
62             return $c->reply->not_found;
63         }
64
65         my $sub = CGI::Compile->compile($script);
66         my $app = CGI::Emulate::PSGI->handler($sub);
67         my $response = $app->($self->_psgi_env($c));
68
69         $c->res->code($response->[0]);
70         $c->res->headers->from_hash({ @{ $response->[1] } });
71         $c->res->body(join('', @{$response->[2]}));
72         $c->rendered;
73     })->name('cgi');
74 }
75
76 sub _psgi_env {
77     my ($self, $c) = @_;
78
79     my $env = $c->req->env;
80
81     my $body = $c->req->build_body;
82     open my $input, '<', \$body or die "Can't open in-memory scalar: $!";
83     $env = {
84         %$env,
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,
96     };
97
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});
101
102     for my $name (@{ $c->req->headers->names }) {
103         my $value = $c->req->headers->header($name);
104         $name =~ s/-/_/g;
105         $name = 'HTTP_' . uc($name);
106         $env->{$name} = $value;
107     }
108
109     return $env;
110 }
111
112 1;
113
114 =encoding utf8
115
116 =head1 NAME
117
118 Koha::App::Plugin::CGIBinKoha
119
120 =head1 DESCRIPTION
121
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.
123
124 =head1 METHODS
125
126 =head2 register
127
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.
129
130 =cut