3 # Copyright Dobrica Pavlinusic 2024
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.
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
23 # perl -I/srv/koha_ffzg/ ./borrower.pl 'uid=0492188A741590'
30 use LWP::UserAgent qw();
31 use Data::Dump qw(dump); # FIXME
38 open(my $fh, '<', '/home/dpavlin/certifikat_za_pristup_apiv2/lozinka.txt');
40 $cert_password = <$fh>;
45 print "Content-type: text/plain; charset=utf-8\r\n\r\n";
47 my $dbh = C4::Context->dbh;
52 if ( my $val = $query->param('uid') ) {
53 $json->{param}->{RFID_SID} = uc $val;
54 push @where, "( code = 'RFID_SID' and attribute = ? )";
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{)};
71 my $sql = generate_sql @where;
74 my $sth = $dbh->prepare( $sql );
75 $sth->execute( @execute );
77 $json->{rows} = $sth->rows;
79 if ( $sth->rows < 1 ) {
80 if ( exists $json->{param}->{RFID_SID} ) {
81 my $sid = $json->{param}->{RFID_SID};
83 if ( $l == 19 or $l == 14 or $l = 8 ) {
86 my $ua = LWP::UserAgent->new (
88 SSL_cert_file => '/home/dpavlin/certifikat_za_pristup_apiv2/certifikat.pfx',
89 SSL_passwd_cb => sub { $cert_password },
92 my $response = $ua->get ("https://isspapi.issp.srce.hr/Kartice/oibjmbag/$sid", Accept => "application/json");
94 if ($response->is_success) {
95 my $json_text = $response->decoded_content;
96 my $h = decode_json $json_text;
103 foreach my $f ( keys %$h ) {
105 push @where, "( code = '$name' and attribute = ? )";
106 push @execute, $h->{$f};
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";
119 warn "ISSP $sid not found in koha $json_text";
120 $json->{error} = "borrower not found";
123 warn "ERROR ISSP ", $response->status_line;
124 $json->{error} = "borrower not found";
127 $json->{error} = "borrower not found";
130 $json->{error} = "borrower not found";
132 } elsif ( $sth->rows > 1 ) {
133 $json->{error} = "more than one borrower found";
135 $json->{borrower} = $sth->fetchrow_hashref;
138 $json = encode_json( $json );