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