From 0722d27e850f43b09ca9731d2e620425626bcc1b Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 20 Feb 2026 11:13:49 +0100 Subject: [PATCH] Bug 41892: Remove C4::Context from C4::Auth's BEGIN block --- C4/Auth.pm | 8 ++++-- C4/Context.pm | 20 ------------- Koha/Context/RemoteAddress.pm | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 Koha/Context/RemoteAddress.pm diff --git a/C4/Auth.pm b/C4/Auth.pm index b5308ae4a42..034b6b9a0fd 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -43,6 +43,8 @@ use C4::Languages; use C4::Search::History; use C4::Output qw( output_and_exit ); use Koha; +use Koha::Context::RemoteAddress; +use Koha::Context::Preferences; use Koha::Logger; use Koha::Caches; use Koha::AuthUtils qw( get_script_name hash_password ); @@ -71,10 +73,10 @@ use Koha::Session; use vars qw($ldap $cas $caslogout); BEGIN { - C4::Context->set_remote_address; + Koha::Context::RemoteAddress->set; - $cas = C4::Context->preference('casAuthentication'); - $caslogout = C4::Context->preference('casLogout'); + $cas = Koha::Context::Preferences->get_value('casAuthentication'); + $caslogout = Koha::Context::Preferences->get_value('casLogout'); if ($cas) { require C4::Auth_with_cas; # no import diff --git a/C4/Context.pm b/C4/Context.pm index 03b4da86910..3971236de53 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -878,26 +878,6 @@ sub temporary_directory { return C4::Context->config('tmp_path') || File::Spec->tmpdir; } -=head3 set_remote_address - -set_remote_address should be called at the beginning of every script -that is *not* running under plack in order to the REMOTE_ADDR environment -variable to be set correctly. - -=cut - -sub set_remote_address { - if ( C4::Context->config('koha_trusted_proxies') ) { - require CGI; - my $header = CGI->http('HTTP_X_FORWARDED_FOR'); - - if ($header) { - require Koha::Middleware::RealIP; - $ENV{REMOTE_ADDR} = Koha::Middleware::RealIP::get_real_ip( $ENV{REMOTE_ADDR}, $header ); - } - } -} - =head3 https_enabled https_enabled should be called when checking if a HTTPS connection diff --git a/Koha/Context/RemoteAddress.pm b/Koha/Context/RemoteAddress.pm new file mode 100644 index 00000000000..4b65f5991f1 --- /dev/null +++ b/Koha/Context/RemoteAddress.pm @@ -0,0 +1,54 @@ +package Koha::Context::RemoteAddress; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Koha::Config; + +=head1 NAME + +Koha::Context::RemoteAddress - Set the remote address + +=head1 SYNOPSIS + + Koha::Context::RemoteAddress->set; + +=head1 METHODS + +=cut + +=head3 set + +Koha::Context::RemoteAddress->set should be called at the beginning of every script +that is *not* running under plack in order to the REMOTE_ADDR environment +variable to be set correctly. + +=cut + +sub set { + if ( Koha::Config->get_instance->get('koha_trusted_proxies') ) { + require CGI; + my $header = CGI->http('HTTP_X_FORWARDED_FOR'); + + if ($header) { + require Koha::Middleware::RealIP; + $ENV{REMOTE_ADDR} = Koha::Middleware::RealIP::get_real_ip( $ENV{REMOTE_ADDR}, $header ); + } + } +} + +1; -- 2.43.0