Bug 20832: Fix opac user profile page when there is overdue fine and no rental charge
[koha.git] / opac / ilsdi.pl
index fa58296..b43ca74 100755 (executable)
@@ -4,28 +4,29 @@
 #
 # 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 2 of the License, or (at your option) any later
-# version.
+# 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.
+# 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, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use C4::ILSDI::Services;
 use C4::Auth;
 use C4::Output;
 use C4::Context;
+
+use List::MoreUtils qw(any);
 use XML::Simple;
-use CGI;
+use CGI qw ( -utf8 );
 
 =head1 DLF ILS-DI for Koha
 
@@ -46,7 +47,7 @@ my @services = (
     #  Level 1: Basic Discovery Interfaces
     #  'HarvestBibliographicRecords',       # OAI-PMH
     #  'HarvestExpandedRecords',            # OAI-PMH
-    'GetAvailability',    # FIXME Add bibbliographic level
+    'GetAvailability',    # FIXME Add bibliographic level
 
     #  'GoToBibliographicRequestPage'       # I don't understant this one
     #  Level 2: Elementary OPAC supplement
@@ -103,7 +104,7 @@ my %optional = (
     'GetAuthorityRecords' => ['schema'],
     'LookupPatron'        => ['id_type'],
     'AuthenticatePatron'  => [],
-    'GetPatronInfo'       => [ 'show_contact', 'show_fines', 'show_holds', 'show_loans' ],
+    'GetPatronInfo'       => [ 'show_contact', 'show_fines', 'show_holds', 'show_loans', 'show_attributes' ],
     'GetPatronStatus'     => [],
     'GetServices'         => [],
     'RenewLoan'           => ['desired_due_date'],
@@ -115,7 +116,7 @@ my %optional = (
 # If no service is requested, display the online documentation
 unless ( $cgi->param('service') ) {
     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
-        {   template_name   => "ilsdi.tmpl",
+        {   template_name   => "ilsdi.tt",
             query           => $cgi,
             type            => "opac",
             authnotrequired => 1,
@@ -127,24 +128,30 @@ unless ( $cgi->param('service') ) {
 }
 
 # If user requested a service description, then display it
-if ( $cgi->param('service') eq "Describe" and any { $cgi->param('verb') eq $_ } @services ) {
+if ( scalar $cgi->param('service') eq "Describe" and any { scalar $cgi->param('verb') eq $_ } @services ) {
     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
-        {   template_name   => "ilsdi.tmpl",
+        {   template_name   => "ilsdi.tt",
             query           => $cgi,
             type            => "opac",
             authnotrequired => 1,
             debug           => 1,
         }
     );
-    $template->param( $cgi->param('verb') => 1 );
+    $template->param( scalar $cgi->param('verb') => 1 );
     output_html_with_http_headers $cgi, $cookie, $template->output;
     exit 0;
 }
 
+# any output after this point will be UTF-8 XML
+binmode STDOUT, ':encoding(UTF-8)';
+print CGI::header('-type'=>'text/xml', '-charset'=>'utf-8');
+
+my $out;
+
 # If ILS-DI module is disabled in System->Preferences, redirect to 404
 unless ( C4::Context->preference('ILS-DI') ) {
-    print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
-    exit 1;
+    $out->{'code'} = "NotAllowed";
+    $out->{'message'} = "ILS-DI is disabled.";
 }
 
 # If the remote address is not allowed, redirect to 403
@@ -152,28 +159,27 @@ my @AuthorizedIPs = split(/,/, C4::Context->preference('ILS-DI:AuthorizedIPs'));
 if ( @AuthorizedIPs # If no filter set, allow access to everybody
     and not any { $ENV{'REMOTE_ADDR'} eq $_ } @AuthorizedIPs # IP Check
     ) {
-    print $cgi->redirect("/cgi-bin/koha/errors/403.pl");
-    exit 1;
+    $out->{'code'} = "NotAllowed";
+    $out->{'message'} = "Unauthorized IP address: ".$ENV{'REMOTE_ADDR'}.".";
 }
 
 my $service = $cgi->param('service') || "ilsdi";
 
-my $out;
-
 # Check if the requested service is in the list
 if ( $service and any { $service eq $_ } @services ) {
 
     my @parmsrequired = @{ $required{$service} };
     my @parmsoptional = @{ $optional{$service} };
     my @parmsall      = ( @parmsrequired, @parmsoptional );
-    my @names         = $cgi->param;
+    my @names         = $cgi->multi_param;
     my %paramhash;
     $paramhash{$_} = 1 for @names;
 
     # check for missing parameters
     for ( @parmsrequired ) {
         unless ( exists $paramhash{$_} ) {
-            $out->{'message'} = "missing $_ parameter";
+            $out->{'code'} = "MissingParameter";
+            $out->{'message'} = "The required parameter ".$_." is missing.";
         }
     }
 
@@ -186,15 +192,17 @@ if ( $service and any { $service eq $_ } @services ) {
             }
         }
         if ( $found == 0 && $name ne 'service' ) {
-            $out->{'message'} = "$name is an illegal parameter";
+            $out->{'code'} = "IllegalParameter";
+            $out->{'message'} = "The parameter ".$name." is illegal.";
         }
     }
 
     # check for multiple parameters
     for ( @names ) {
-        my @values = $cgi->param($_);
+        my @values = $cgi->multi_param($_);
         if ( $#values != 0 ) {
-            $out->{'message'} = "multiple values are not allowed for the $_ parameter";
+            $out->{'code'} = "MultipleValuesNotAllowed";
+            $out->{'message'} = "Multiple values not allowed for the parameter ".$_.".";
         }
     }
 
@@ -202,14 +210,13 @@ if ( $service and any { $service eq $_ } @services ) {
 
         # GetAvailability is a special case, as it cannot use XML::Simple
         if ( $service eq "GetAvailability" ) {
-            print CGI::header('text/xml');
-            print C4::ILSDI::GetAvailability($cgi);
+            print C4::ILSDI::Services::GetAvailability($cgi);
             exit 0;
         } else {
 
             # Variable functions
             my $sub = do {
-                no strict 'refs';
+#                no strict 'refs';
                 my $symbol = 'C4::ILSDI::Services::' . $service;
                 \&{"$symbol"};
             };
@@ -223,11 +230,9 @@ if ( $service and any { $service eq $_ } @services ) {
 }
 
 # Output XML by passing the hashref to XMLOut
-print CGI::header('-type'=>'text/xml', '-charset'=>'utf-8');
 print XMLout(
     $out,
     noattr        => 1,
-    noescape      => 1,
     nosort        => 1,
     xmldecl       => '<?xml version="1.0" encoding="UTF-8" ?>',
     RootName      => $service,