Fixes access permissions to ILS-DI
[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::Services, 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 ILS-DI module is disabled in System->Preferences, redirect to 404
116 unless ( C4::Context->preference('ILS-DI') ) {
117     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
118     exit 1;
119 }
120
121 # If the remote address is not allowed, redirect to 403
122 if ( C4::Context->preference('ILS-DI:AuthorizedIPs') # If no filter set, allow access to everybody
123     and $cgi->param('service') and $cgi->param('service') ne 'Describe' # Allow access to online documentation
124     and not any { $ENV{'REMOTE_ADDR'} eq $_ } split(/,/, C4::Context->preference('ILS-DI:AuthorizedIPs')) # IP Check
125     ) {
126     print $cgi->redirect("/cgi-bin/koha/errors/403.pl");
127     exit 1;
128 }
129
130 # If no service is requested, display the online documentation
131 unless ( $cgi->param('service') ) {
132     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
133         {   template_name   => "ilsdi.tmpl",
134             query           => $cgi,
135             type            => "opac",
136             authnotrequired => 1,
137             debug           => 1,
138         }
139     );
140     output_html_with_http_headers $cgi, $cookie, $template->output;
141     exit 0;
142 }
143
144 # If user requested a service description, then display it
145 if ( $cgi->param('service') eq "Describe" and grep { $cgi->param('verb') eq $_ } @services ) {
146     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
147         {   template_name   => "ilsdi.tmpl",
148             query           => $cgi,
149             type            => "opac",
150             authnotrequired => 1,
151             debug           => 1,
152         }
153     );
154     $template->param( $cgi->param('verb') => 1 );
155     output_html_with_http_headers $cgi, $cookie, $template->output;
156     exit 0;
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 grep { $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     foreach my $name (@names) {
172         $paramhash{$name} = 1;
173     }
174
175     # check for missing parameters
176     foreach my $name (@parmsrequired) {
177         if ( ( !exists $paramhash{$name} ) ) {
178             $out->{'message'} = "missing $name parameter";
179         }
180     }
181
182     # check for illegal parameters
183     foreach my $name (@names) {
184         my $found = 0;
185         foreach my $name2 (@parmsall) {
186             if ( $name eq $name2 ) {
187                 $found = 1;
188             }
189         }
190         if ( ( $found == 0 ) && ( $name ne 'service' ) ) {
191             $out->{'message'} = "$name is an illegal parameter";
192         }
193     }
194
195     # check for multiple parameters
196     foreach my $name (@names) {
197         my @values = $cgi->param($name);
198         if ( $#values != 0 ) {
199             $out->{'message'} = "multiple values are not allowed for the $name parameter";
200         }
201     }
202
203     if ( !$out->{'message'} ) {
204
205         # GetAvailability is a special case, as it cannot use XML::Simple
206         if ( $service eq "GetAvailability" ) {
207             print CGI::header('text/xml');
208             print C4::ILSDI::Services::GetAvailability($cgi);
209             exit 0;
210         } else {
211
212             # Variable functions
213             my $sub = do {
214                 no strict 'refs';
215                 my $symbol = 'C4::ILSDI::Services::' . $service;
216                 \&{"$symbol"};
217             };
218
219             # Call the requested service, and get its return value
220             $out = &$sub($cgi);
221         }
222     }
223 } else {
224     $out->{'message'} = "NotSupported";
225 }
226
227 # Output XML by passing the hashref to XMLOut
228 print CGI::header('-type'=>'text/xml', '-charset'=>'utf-8');
229 print XMLout(
230     $out,
231     noattr        => 1,
232     noescape      => 1,
233     nosort        => 1,
234     xmldecl       => '<?xml version="1.0" encoding="UTF-8" ?>',
235     RootName      => $service,
236     SuppressEmpty => 1
237 );
238