ISSP lookup for OIB and JMBAG, WIP
[share-koha-fer] / intranet / cgi-bin / rfid-uid / borrower.pl
1 #!/usr/bin/perl
2
3 # Copyright Dobrica Pavlinusic 2024
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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.
19
20 # designed to work with https://github.com/martinpaljak/nfc4pc
21 # java -jar build/libs/nfc4pc-230711-next.jar -d -c -u https://lib.fer.hr:8443/cgi-bin/koha/rfid-uid/borrower.pl
22
23 # perl -I/srv/koha_ffzg/ ./borrower.pl 'uid=0492188A741590'
24
25 use strict;
26 use warnings;
27
28 use CGI;
29 use C4::Context;
30 use LWP::UserAgent qw();
31 use Data::Dump qw(dump); # FIXME
32 use JSON;
33
34 my $json;
35
36 my $cert_password;
37 {
38         open(my $fh, '<', '/home/dpavlin/certifikat_za_pristup_apiv2/lozinka.txt');
39         local $/ = undef;
40         $cert_password = <$fh>;
41         close($fh);
42 }
43
44 my $query = new CGI;
45 print "Content-type: text/plain; charset=utf-8\r\n\r\n";
46
47 my $dbh = C4::Context->dbh;
48
49 my @where;
50 my @execute;
51
52 if ( my $val = $query->param('uid') ) {
53         $json->{param}->{RFID_SID} = uc $val;
54         push @where, "( code = 'RFID_SID' and attribute = ? )";
55         push @execute, $val;
56 }
57
58 sub generate_sql {
59         my @where = @_;
60 my $sql = qq{
61 select
62         distinct
63         b.borrowernumber, firstname, surname, email, userid, cardnumber
64 from borrower_attributes ba
65 join borrowers b on b.borrowernumber = ba.borrowernumber
66 where (} . join(') or (', @where) . qq{)};
67
68         return $sql;
69 }
70
71 my $sql = generate_sql @where;
72 warn "# sql $sql\n";
73
74 my $sth = $dbh->prepare( $sql );
75 $sth->execute( @execute );
76
77 $json->{rows} = $sth->rows;
78
79 if ( $sth->rows < 1 ) {
80         if ( exists $json->{param}->{RFID_SID} ) {
81                 my $sid = $json->{param}->{RFID_SID};
82                 my $l = length($sid);
83                 if ( $l == 19 or $l == 14 or $l = 8 ) {
84                         warn "ISSP $sid\n";
85
86                         my $ua = LWP::UserAgent->new (
87                         ssl_opts => {
88                           SSL_cert_file => '/home/dpavlin/certifikat_za_pristup_apiv2/certifikat.pfx',
89                           SSL_passwd_cb => sub { $cert_password },
90                         }
91                         );
92                         my $response = $ua->get ("https://isspapi.issp.srce.hr/Kartice/oibjmbag/$sid", Accept => "application/json");
93
94                         if ($response->is_success) {
95                                 my $json_text = $response->decoded_content;
96                                 my $h = decode_json $json_text;
97
98                                 $json->{issp} = $h;
99
100                                 @where = ();
101                                 @execute = ();
102
103                                 foreach my $f ( keys %$h ) {
104                                         my $name = uc($f);
105                                         push @where, "( code = '$name' and attribute = ? )";
106                                         push @execute, $h->{$f};
107                                 }
108                                 my $sql = generate_sql @where;
109                                 warn "ISSP sql: $sql ",dump(@execute);
110                                 my $sth = $dbh->prepare( $sql );
111                                 $sth->execute( @execute );
112                                 if ( $sth->rows == 1 ) {
113                                         $json->{borrower} = $sth->fetchrow_hashref;
114                                         warn "borrower = ",dump( $json->{borrower} );
115                                         my $sth2 = $dbh->prepare(qq{ insert into borrower_attributes (borrowernumber, code, attribute) values (?,?,?) });
116                                         $sth2->execute( $json->{borrower}->{borrowernumber}, 'RFID_SID', $sid);
117                                         warn "ISSP updated $json->{borrower}->{borrowernumber} $sid";
118                                 } else {
119                                         warn "ISSP $sid not found in koha $json_text";
120                                         $json->{error} = "borrower not found";
121                                 }
122                         } else {
123                                 warn "ERROR ISSP ", $response->status_line;
124                                 $json->{error} = "borrower not found";
125                         }
126                 } else {
127                         $json->{error} = "borrower not found";
128                 }
129         } else {
130                 $json->{error} = "borrower not found";
131         }
132 } elsif ( $sth->rows > 1 ) {
133         $json->{error} = "more than one borrower found";
134 } else {
135         $json->{borrower} = $sth->fetchrow_hashref;
136 }
137
138 $json = encode_json( $json );
139
140 print $json;