Merge remote-tracking branch 'origin/new/bug_7729'
[koha.git] / opac / ilsdi.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 SARL Biblibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use List::MoreUtils qw(any);
24
25 use C4::ILSDI::Services;
26 use C4::Auth;
27 use C4::Output;
28 use C4::Context;
29 use XML::Simple;
30 use CGI;
31
32 =head1 DLF ILS-DI for Koha
33
34 This script is a basic implementation of ILS-DI protocol for Koha.
35 It acts like a dispatcher, that get the CGI request, check required and 
36 optionals arguments, call a function from C4::ILS-DI, and finaly
37 outputs the returned hashref as XML.
38
39 =cut
40
41 # Instanciate the CGI request
42 my $cgi = new CGI;
43
44 # List of available services, sorted by level
45 my @services = (
46     'Describe',    # Not part of ILS-DI, online API doc
47
48     #   Level 1: Basic Discovery Interfaces
49     #   'HarvestBibliographicRecords',       # OAI-PMH
50     #   'HarvestExpandedRecords',            # OAI-PMH
51     'GetAvailability',    # FIXME Add bibbliographic level
52
53     #   'GoToBibliographicRequestPage'       # I don't understant this one
54     #   Level 2: Elementary OPAC supplement
55     #   'HarvestAuthorityRecords',           # OAI-PMH
56     #   'HarvestHoldingsRecords',            # OAI-PMH
57     'GetRecords',         # Note that we can use OAI-PMH for this too
58
59     #   'Search',                            # TODO
60     #   'Scan',                              # TODO
61     'GetAuthorityRecords',
62
63     #   'OutputRewritablePage',              # I don't understant this one
64     #   'OutputIntermediateFormat',          # I don't understant this one
65     #   Level 3: Elementary OPAC alternative
66     'LookupPatron',
67     'AuthenticatePatron',
68     'GetPatronInfo',
69     'GetPatronStatus',
70     'GetServices',    # FIXME Loans
71     'RenewLoan',
72     'HoldTitle',      # FIXME Add dates support
73     'HoldItem',       # FIXME Add dates support
74     'CancelHold',
75
76     #   'RecallItem',                        # Not supported by Koha
77     #   'CancelRecall',                      # Not supported by Koha
78     #   Level 4: Robust/domain specific discovery platforms
79     #   'SearchCourseReserves',              # TODO
80     #   'Explain'                            # TODO
81 );
82
83 # List of required arguments
84 my %required = (
85     'Describe'            => ['verb'],
86     'GetAvailability'     => [ 'id', 'id_type' ],
87     'GetRecords'          => ['id'],
88     'GetAuthorityRecords' => ['id'],
89     'LookupPatron'        => ['id'],
90     'AuthenticatePatron'  => [ 'username', 'password' ],
91     'GetPatronInfo'       => ['patron_id'],
92     'GetPatronStatus'     => ['patron_id'],
93     'GetServices'         => [ 'patron_id', 'item_id' ],
94     'RenewLoan'           => [ 'patron_id', 'item_id' ],
95     'HoldTitle'           => [ 'patron_id', 'bib_id', 'request_location' ],
96     'HoldItem'            => [ 'patron_id', 'bib_id', 'item_id' ],
97     'CancelHold' => [ 'patron_id', 'item_id' ],
98 );
99
100 # List of optional arguments
101 my %optional = (
102     'Describe'            => [],
103     'GetAvailability'     => [ 'return_type', 'return_fmt' ],
104     'GetRecords'          => ['schema'],
105     'GetAuthorityRecords' => ['schema'],
106     'LookupPatron'        => ['id_type'],
107     'AuthenticatePatron'  => [],
108     'GetPatronInfo'       => [ 'show_contact', 'show_fines', 'show_holds', 'show_loans' ],
109     'GetPatronStatus'     => [],
110     'GetServices'         => [],
111     'RenewLoan'           => ['desired_due_date'],
112     'HoldTitle'  => [ 'pickup_location', 'needed_before_date', 'pickup_expiry_date' ],
113     'HoldItem'   => [ 'pickup_location', 'needed_before_date', 'pickup_expiry_date' ],
114     'CancelHold' => [],
115 );
116
117 # If no service is requested, display the online documentation
118 unless ( $cgi->param('service') ) {
119     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
120         {   template_name   => "ilsdi.tmpl",
121             query           => $cgi,
122             type            => "opac",
123             authnotrequired => 1,
124             debug           => 1,
125         }
126     );
127     output_html_with_http_headers $cgi, $cookie, $template->output;
128     exit 0;
129 }
130
131 # If user requested a service description, then display it
132 if ( $cgi->param('service') eq "Describe" and any { $cgi->param('verb') eq $_ } @services ) {
133     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
134         {   template_name   => "ilsdi.tmpl",
135             query           => $cgi,
136             type            => "opac",
137             authnotrequired => 1,
138             debug           => 1,
139         }
140     );
141     $template->param( $cgi->param('verb') => 1 );
142     output_html_with_http_headers $cgi, $cookie, $template->output;
143     exit 0;
144 }
145
146 my $out;
147
148 # If ILS-DI module is disabled in System->Preferences, redirect to 404
149 unless ( C4::Context->preference('ILS-DI') ) {
150     $out->{'code'} = "NotAllowed";
151     $out->{'message'} = "ILS-DI is disabled.";
152 }
153
154 # If the remote address is not allowed, redirect to 403
155 my @AuthorizedIPs = split(/,/, C4::Context->preference('ILS-DI:AuthorizedIPs'));
156 if ( @AuthorizedIPs # If no filter set, allow access to everybody
157     and not any { $ENV{'REMOTE_ADDR'} eq $_ } @AuthorizedIPs # IP Check
158     ) {
159     $out->{'code'} = "NotAllowed";
160     $out->{'message'} = "Unauthorized IP address: ".$ENV{'REMOTE_ADDR'}.".";
161 }
162
163 my $service = $cgi->param('service') || "ilsdi";
164
165 # Check if the requested service is in the list
166 if ( $service and any { $service eq $_ } @services ) {
167
168     my @parmsrequired = @{ $required{$service} };
169     my @parmsoptional = @{ $optional{$service} };
170     my @parmsall      = ( @parmsrequired, @parmsoptional );
171     my @names         = $cgi->param;
172     my %paramhash;
173     $paramhash{$_} = 1 for @names;
174
175     # check for missing parameters
176     for ( @parmsrequired ) {
177         unless ( exists $paramhash{$_} ) {
178             $out->{'code'} = "MissingParameter";
179             $out->{'message'} = "The required parameter ".$_." is missing.";
180         }
181     }
182
183     # check for illegal parameters
184     for my $name ( @names ) {
185         my $found = 0;
186         for my $name2 (@parmsall) {
187             if ( $name eq $name2 ) {
188                 $found = 1;
189             }
190         }
191         if ( $found == 0 && $name ne 'service' ) {
192             $out->{'code'} = "IllegalParameter";
193             $out->{'message'} = "The parameter ".$name." is illegal.";
194         }
195     }
196
197     # check for multiple parameters
198     for ( @names ) {
199         my @values = $cgi->param($_);
200         if ( $#values != 0 ) {
201             $out->{'code'} = "MultipleValuesNotAllowed";
202             $out->{'message'} = "Multiple values not allowed for the parameter ".$_.".";
203         }
204     }
205
206     if ( !$out->{'message'} ) {
207
208         # GetAvailability is a special case, as it cannot use XML::Simple
209         if ( $service eq "GetAvailability" ) {
210             print CGI::header('text/xml');
211             print C4::ILSDI::Services::GetAvailability($cgi);
212             exit 0;
213         } else {
214
215             # Variable functions
216             my $sub = do {
217                 no strict 'refs';
218                 my $symbol = 'C4::ILSDI::Services::' . $service;
219                 \&{"$symbol"};
220             };
221
222             # Call the requested service, and get its return value
223             $out = &$sub($cgi);
224         }
225     }
226 } else {
227     $out->{'message'} = "NotSupported";
228 }
229
230 # Output XML by passing the hashref to XMLOut
231 binmode STDOUT, ':encoding(UTF-8)';
232 print CGI::header('-type'=>'text/xml', '-charset'=>'utf-8');
233 print XMLout(
234     $out,
235     noattr        => 1,
236     nosort        => 1,
237     xmldecl       => '<?xml version="1.0" encoding="UTF-8" ?>',
238     RootName      => $service,
239     SuppressEmpty => 1
240 );
241 exit 0;
242