Merge branch 'master' of git://git.koha-community.org/koha into 3.14-master
[koha.git] / opac / ilsdi.pl
index fa58296..419c0f2 100755 (executable)
@@ -20,6 +20,8 @@
 use strict;
 use warnings;
 
+use List::MoreUtils qw(any);
+
 use C4::ILSDI::Services;
 use C4::Auth;
 use C4::Output;
@@ -141,10 +143,12 @@ if ( $cgi->param('service') eq "Describe" and any { $cgi->param('verb') eq $_ }
     exit 0;
 }
 
+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,14 +156,12 @@ 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 ) {
 
@@ -173,7 +175,8 @@ if ( $service and any { $service eq $_ } @services ) {
     # check for missing parameters
     for ( @parmsrequired ) {
         unless ( exists $paramhash{$_} ) {
-            $out->{'message'} = "missing $_ parameter";
+            $out->{'code'} = "MissingParameter";
+            $out->{'message'} = "The required parameter ".$_." is missing.";
         }
     }
 
@@ -186,7 +189,8 @@ 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.";
         }
     }
 
@@ -194,7 +198,8 @@ if ( $service and any { $service eq $_ } @services ) {
     for ( @names ) {
         my @values = $cgi->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 ".$_.".";
         }
     }
 
@@ -203,7 +208,7 @@ 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 {
 
@@ -223,11 +228,11 @@ if ( $service and any { $service eq $_ } @services ) {
 }
 
 # Output XML by passing the hashref to XMLOut
+binmode STDOUT, ':encoding(UTF-8)';
 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,