f56c465984cc4a4d59dc4ad92ad3ef3857072ce4
[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         chomp $ip;
31         $hash->{local_ip} = $ip;
32         close($fh);
33 } else {
34         warn $hash->{_error} = "ERROR: ", $hash->{remote_host}, " don't have RFID reader assigned";
35 }
36
37 if ( $query->param('intranet-js') ) {
38         print "Content-type: application/javascript\r\n\r\n";
39         open(my $js, '<', 'koha-rfid.js');
40         while(<$js>) {
41                 s/localhost/$hash->{local_ip}/g;
42                 print;
43         }
44         close($js);
45 } else {
46         print "Content-type: application/json; charset=utf-8\r\n\r\n";
47         print encode_json $hash;
48 }