3 # Copyright 2009 SARL Biblibre
5 # This file is part of Koha.
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
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.
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.
23 use C4::ILSDI::Services;
30 =head1 DLF ILS-DI for Koha
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.
39 # Instanciate the CGI request
42 # List of available services, sorted by level
44 'Describe', # Not part of ILS-DI, online API doc
46 # Level 1: Basic Discovery Interfaces
47 # 'HarvestBibliographicRecords', # OAI-PMH
48 # 'HarvestExpandedRecords', # OAI-PMH
49 'GetAvailability', # FIXME Add bibbliographic level
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
59 'GetAuthorityRecords',
61 # 'OutputRewritablePage', # I don't understant this one
62 # 'OutputIntermediateFormat', # I don't understant this one
63 # Level 3: Elementary OPAC alternative
68 'GetServices', # FIXME Loans
70 'HoldTitle', # FIXME Add dates support
71 'HoldItem', # FIXME Add dates support
74 # 'RecallItem', # Not supported by Koha
75 # 'CancelRecall', # Not supported by Koha
76 # Level 4: Robust/domain specific discovery platforms
77 # 'SearchCourseReserves', # TODO
81 # List of required arguments
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' ],
98 # List of optional arguments
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' => [],
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' ],
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",
121 authnotrequired => 1,
125 output_html_with_http_headers $cgi, $cookie, $template->output;
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",
135 authnotrequired => 1,
139 $template->param( $cgi->param('verb') => 1 );
140 output_html_with_http_headers $cgi, $cookie, $template->output;
146 # If ILS-DI module is disabled in System->Preferences, redirect to 404
147 unless ( C4::Context->preference('ILS-DI') ) {
148 $out->{'code'} = "NotAllowed";
149 $out->{'message'} = "ILS-DI is disabled.";
152 # If the remote address is not allowed, redirect to 403
153 my @AuthorizedIPs = split(/,/, C4::Context->preference('ILS-DI:AuthorizedIPs'));
154 if ( @AuthorizedIPs # If no filter set, allow access to everybody
155 and not any { $ENV{'REMOTE_ADDR'} eq $_ } @AuthorizedIPs # IP Check
157 $out->{'code'} = "NotAllowed";
158 $out->{'message'} = "Unauthorized IP address: ".$ENV{'REMOTE_ADDR'}.".";
161 my $service = $cgi->param('service') || "ilsdi";
163 # Check if the requested service is in the list
164 if ( $service and any { $service eq $_ } @services ) {
166 my @parmsrequired = @{ $required{$service} };
167 my @parmsoptional = @{ $optional{$service} };
168 my @parmsall = ( @parmsrequired, @parmsoptional );
169 my @names = $cgi->param;
171 $paramhash{$_} = 1 for @names;
173 # check for missing parameters
174 for ( @parmsrequired ) {
175 unless ( exists $paramhash{$_} ) {
176 $out->{'code'} = "MissingParameter";
177 $out->{'message'} = "The required parameter ".$_." is missing.";
181 # check for illegal parameters
182 for my $name ( @names ) {
184 for my $name2 (@parmsall) {
185 if ( $name eq $name2 ) {
189 if ( $found == 0 && $name ne 'service' ) {
190 $out->{'code'} = "IllegalParameter";
191 $out->{'message'} = "The parameter ".$name." is illegal.";
195 # check for multiple parameters
197 my @values = $cgi->param($_);
198 if ( $#values != 0 ) {
199 $out->{'code'} = "MultipleValuesNotAllowed";
200 $out->{'message'} = "Multiple values not allowed for the parameter ".$_.".";
204 if ( !$out->{'message'} ) {
206 # GetAvailability is a special case, as it cannot use XML::Simple
207 if ( $service eq "GetAvailability" ) {
208 print CGI::header('text/xml');
209 print C4::ILSDI::GetAvailability($cgi);
216 my $symbol = 'C4::ILSDI::Services::' . $service;
220 # Call the requested service, and get its return value
225 $out->{'message'} = "NotSupported";
228 # Output XML by passing the hashref to XMLOut
229 print CGI::header('-type'=>'text/xml', '-charset'=>'utf-8');
235 xmldecl => '<?xml version="1.0" encoding="UTF-8" ?>',
236 RootName => $service,