Bug 30813: Update TransformMarcToKoha to accept a hashref
[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 qw( output_with_http_headers );
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 = CGI->new;
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 $od_username;
52                 if ( C4::Context->preference('OverDriveUsername') eq 'cardnumber' ){
53                     $od_username = $patron ? $patron->cardnumber : undef;
54                 } else {
55                     $od_username = $user;
56                 }
57                 my $branch_authname = $branch_info ? $branch_info->authname : undef;
58                 my $authname = $branch_authname || C4::Context->preference('OverDriveAuthname');
59                 $od->auth_by_userid($od_username, $password,C4::Context->preference('OverDriveWebsiteID'),$authname);
60                 $data{login_success} = 1;
61                 last;
62             };
63
64             if ($od->is_logged_in) {
65                 $data{is_logged_in} = JSON::true;
66
67                 $action eq 'logout' && do {
68                     $od->forget();
69                     $data{login_url} = $od->auth_url($page_url);
70                     $data{is_logged_in} = JSON::false;
71                     last;
72                 };
73
74                 $action eq 'account' && do {
75                     $data{account} = $od->patron;
76                     $data{checkouts} = $od->checkouts;
77                     $data{holds} = $od->holds;
78                     last;
79                 };
80
81                 $action eq 'checkout' && do {
82                     my $id = $cgi->param('id')
83                       or response_bad_request("No 'id' specified");
84                     my $format = $cgi->param('format');
85                     $data{action} = $od->checkout($id, $format);
86                     $data{checkouts} = $od->checkouts;
87                     $data{holds} = $od->holds;
88                     last;
89                 };
90
91                 $action eq 'checkout-format' && do {
92                     my $id = $cgi->param('id')
93                       or response_bad_request("No 'id' specified");
94                     my $format = $cgi->param('format')
95                       or response_bad_request("No 'format' specified");
96                     $data{action} = $od->lock_format($id, $format);
97                     $data{checkouts} = $od->checkouts;
98                     last;
99                 };
100
101                 $action eq 'download-url' && do {
102                     my $id = $cgi->param('id')
103                       or response_bad_request("No 'id' specified");
104                     $data{action} = $od->checkout_download_url($id);
105                     last;
106                 };
107
108                 $action eq 'return' && do {
109                     my $id = $cgi->param('id')
110                       or response_bad_request("No 'id' specified");
111                     local $@;
112                     $data{action} = eval { $od->return($id) };
113                     $data{action} = $@ if $@;
114                     $data{checkouts} = $od->checkouts;
115                     last;
116                 };
117
118                 $action eq 'place-hold' && do {
119                     my $id = $cgi->param('id')
120                       or response_bad_request("No 'id' specified");
121                     $data{action} = $od->place_hold($id);
122                     $data{holds} = $od->holds;
123                     last;
124                 };
125
126                 $action eq 'remove-hold' && do {
127                     my $id = $cgi->param('id')
128                       or response_bad_request("No 'id' specified");
129                     local $@;
130                     $data{action} = eval { $od->remove_hold($id) };
131                     $data{action} = $@ if $@;
132                     $data{holds} = $od->holds;
133                     last;
134                 };
135
136                 response_bad_request("Invalid 'action': $action");
137             }
138         }
139 };
140 if ($@) {
141     if ($od->is_not_authenticated_error("$@")) {
142         $logger->debug("OverDrive session timeout");
143         $data{is_logged_in} = JSON::false;
144     } else {
145         $logger->error($@);
146         $data{error} = $od->error_message("$@");
147     }
148 }
149
150 response(\%data);
151
152
153 sub response_bad_request {
154     my ($error) = @_;
155     response({error => $error}, "400 $error");
156 }
157 sub response {
158     my ($data, $status_line) = @_;
159     $status_line ||= "200 OK";
160     output_with_http_headers $cgi, undef, encode_json($data), 'json', $status_line;
161     exit;
162 }