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