Bug 19752: offline_circ/service.pl - Return HTTP status 401 when authentication faile...
[koha.git] / offline_circ / service.pl
1 #!/usr/bin/perl
2
3 # 2009 BibLibre <jeanandre.santoni@biblibre.com>
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
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Circulation;
26 use Koha::DateUtils;
27 use DateTime::TimeZone;
28
29 my $cgi = CGI->new;
30
31 # used by the KOCT firefox extension
32 # (or any third-party that doesn't want to rely on cookies for authentication)
33 my $nocookie = $cgi->param('nocookie') || 0;
34
35 # get the status of the user, this will check his credentials and rights
36 my ($status, $cookie, $sessionId) = C4::Auth::check_api_auth($cgi, undef);
37 ($status, $sessionId) = C4::Auth::check_cookie_auth($cgi, undef) if ($status ne 'ok' && !$nocookie);
38
39 my $result;
40
41 if ($status eq 'ok') { # if authentication is ok
42
43     my $userid     = $cgi->param('userid')     || '';
44     my $branchcode = $cgi->param('branchcode') || '';
45     my $timestamp  = $cgi->param('timestamp')  || '';
46     my $action     = $cgi->param('action')     || '';
47     my $barcode    = $cgi->param('barcode')    || '';
48     my $amount     = $cgi->param('amount')     || 0;
49     $barcode    =~ s/^\s+//;
50     $barcode    =~ s/\s+$//;
51     my $cardnumber = $cgi->param('cardnumber') || '';
52     $cardnumber =~ s/^\s+//;
53     $cardnumber =~ s/\s+$//;
54
55     # KOCT send UTC timestamp, it should be converted to local timezone
56     my $dt = dt_from_string($timestamp, 'iso', DateTime::TimeZone->new(name => 'UTC'));
57     $dt->set_time_zone(C4::Context->tz);
58     $timestamp = $dt->ymd('-') . ' ' . $dt->hms(':');
59
60     if ( $cgi->param('pending') eq 'true' ) { # if the 'pending' flag is true, we store the operation in the db instead of directly processing them
61         $result = AddOfflineOperation(
62             $userid,
63             $branchcode,
64             $timestamp,
65             $action,
66             $barcode,
67             $cardnumber,
68             $amount
69         );
70     } else {
71         $result = ProcessOfflineOperation(
72             {
73                 'userid'      => $userid,
74                 'branchcode'  => $branchcode,
75                 'timestamp'   => $timestamp,
76                 'action'      => $action,
77                 'barcode'     => $barcode,
78                 'cardnumber'  => $cardnumber,
79                 'amount'      => $amount
80             }
81         );
82     }
83
84     print CGI::header('-type'=>'text/plain', '-charset'=>'utf-8');
85     print $result;
86     exit;
87 }
88
89 print CGI::header('-type'=>'text/plain', '-charset'=>'utf-8', '-status' => '401 Unauthorized');
90 print $result;