Bug 21115: Add multi_param call and add divider in cache key in svc/report and opac...
[koha.git] / opac / svc / overdrive
1 #!/usr/bin/perl
2
3 # script to action OverDrive API calls
4
5 # Copyright 2015 Catalyst IT
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use JSON qw(encode_json);
24 use C4::Auth qw(checkauth);
25 use C4::Output;
26 use Koha::Logger;
27 use Koha::Patrons;
28 use Koha::Library::OverDriveInfos;
29 use Koha::ExternalContent::OverDrive;
30
31 my $logger = Koha::Logger->get({ interface => 'opac' });
32 our $cgi = new CGI;
33 my $page_url = $cgi->referer();
34
35 my ( $user, $cookie, $sessionID, $flags ) = checkauth( $cgi, 1, {}, 'opac' );
36 $user && $sessionID or response_bad_request("User not logged in");
37
38 my $action = $cgi->param('action') or response_bad_request("No 'action' specified");
39
40 my $od = Koha::ExternalContent::OverDrive->new({ koha_session_id => $sessionID });
41 my %data = (
42    is_logged_in => JSON::false,
43 );
44 local $@;
45 eval {
46         {
47             $action eq 'login' && do {
48                 my $password = $cgi->param("password") // q{} ;
49                 my $patron = Koha::Patrons->find({ userid => $user });
50                 my $branch_info = $patron ? Koha::Library::OverDriveInfos->find( $patron->branchcode ) : undef;
51                 my $branch_authname = $branch_info ? $branch_info->authname : undef;
52                 my $authname = $branch_authname || C4::Context->preference('OverDriveAuthname');
53                 $od->auth_by_userid($user, $password,C4::Context->preference('OverDriveWebsiteID'),$authname);
54                 $data{login_success} = 1;
55                 last;
56             };
57
58             if ($od->is_logged_in) {
59                 $data{is_logged_in} = JSON::true;
60
61                 $action eq 'logout' && do {
62                     $od->forget();
63                     $data{login_url} = $od->auth_url($page_url);
64                     $data{is_logged_in} = JSON::false;
65                     last;
66                 };
67
68                 $action eq 'account' && do {
69                     $data{account} = $od->patron;
70                     $data{checkouts} = $od->checkouts;
71                     $data{holds} = $od->holds;
72                     last;
73                 };
74
75                 $action eq 'checkout' && do {
76                     my $id = $cgi->param('id')
77                       or response_bad_request("No 'id' specified");
78                     my $format = $cgi->param('format');
79                     $data{action} = $od->checkout($id, $format);
80                     $data{checkouts} = $od->checkouts;
81                     $data{holds} = $od->holds;
82                     last;
83                 };
84
85                 $action eq 'checkout-format' && do {
86                     my $id = $cgi->param('id')
87                       or response_bad_request("No 'id' specified");
88                     my $format = $cgi->param('format')
89                       or response_bad_request("No 'format' specified");
90                     $data{action} = $od->lock_format($id, $format);
91                     $data{checkouts} = $od->checkouts;
92                     last;
93                 };
94
95                 $action eq 'download-url' && do {
96                     my $id = $cgi->param('id')
97                       or response_bad_request("No 'id' specified");
98                     my $format = $cgi->param('format')
99                       or response_bad_request("No 'format' specified");
100                     $data{action} = $od->checkout_download_url($id, $format, $page_url, $page_url);
101                     last;
102                 };
103
104                 $action eq 'return' && do {
105                     my $id = $cgi->param('id')
106                       or response_bad_request("No 'id' specified");
107                     local $@;
108                     $data{action} = eval { $od->return($id) };
109                     $data{action} = $@ if $@;
110                     $data{checkouts} = $od->checkouts;
111                     last;
112                 };
113
114                 $action eq 'place-hold' && do {
115                     my $id = $cgi->param('id')
116                       or response_bad_request("No 'id' specified");
117                     $data{action} = $od->place_hold($id);
118                     $data{holds} = $od->holds;
119                     last;
120                 };
121
122                 $action eq 'remove-hold' && do {
123                     my $id = $cgi->param('id')
124                       or response_bad_request("No 'id' specified");
125                     local $@;
126                     $data{action} = eval { $od->remove_hold($id) };
127                     $data{action} = $@ if $@;
128                     $data{holds} = $od->holds;
129                     last;
130                 };
131
132                 response_bad_request("Invalid 'action': $action");
133             }
134         }
135 };
136 if ($@) {
137     if ($od->is_not_authenticated_error("$@")) {
138         $logger->debug("OverDrive session timeout");
139         $data{is_logged_in} = JSON::false;
140     } else {
141         $logger->error($@);
142         $data{error} = $od->error_message("$@");
143     }
144 }
145
146 response(\%data);
147
148
149 sub response_bad_request {
150     my ($error) = @_;
151     response({error => $error}, "400 $error");
152 }
153 sub response {
154     my ($data, $status_line) = @_;
155     $status_line ||= "200 OK";
156     output_with_http_headers $cgi, undef, encode_json($data), 'json', $status_line;
157     exit;
158 }