90ca5dc00fad91170edeb1232a5357e3e930ce7c
[koha.git] / ffzg / rfid / register.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use CGI;
7 use JSON;
8 use FindBin;
9
10 my $query = new CGI;
11
12 my $hash = {
13         remote_host => $query->remote_host,
14 };
15
16 my $dir = $FindBin::Bin;
17 my $path = "$dir/ip/" . $hash->{remote_host};
18
19 if ( my $ip = $query->param('local_ip') ) {
20
21         $hash->{local_ip} = $ip;
22         open(my $fh, '>', $path);
23         print $fh $hash->{local_ip};
24         close($fh);
25         warn "# $path ", -s $path, "\n";
26
27 } elsif ( -e $path ) {
28         open(my $fh, '<', $path);
29         my $ip = <$fh>;
30         $hash->{local_ip} = $ip;
31         close($fh);
32 } else {
33         warn $hash->{_error} = "ERROR: ", $hash->{remote_host}, " don't have RFID reader assigned";
34 }
35
36 if ( $query->param('intranet-js') ) {
37         print "Content-type: application/javascript\r\n\r\n";
38         open(my $js, '<', 'koha-rfid.js');
39         while(<$js>) {
40                 s/localhost/$hash->{local_ip}/g;
41                 print;
42         }
43         close($js);
44 } else {
45         print "Content-type: application/json; charset=utf-8\r\n\r\n";
46         print encode_json $hash;
47 }