9e0b86f9bce907aaed988915c27173bc622e04a0
[koha_ffzg] / 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 use Data::Dump qw(dump);
13 warn "# query ", dump( $query );
14
15 my $hash = {
16         remote_host => $query->remote_host,
17 };
18
19 my $dir = $FindBin::Bin;
20 my $path = "$dir/ip/" . $hash->{remote_host};
21
22
23 if ( my $ip = $query->param('local_ip') ) {
24
25         $hash->{local_ip} = $ip;
26         open(my $fh, '>', $path);
27         print $fh $hash->{local_ip};
28         close($fh);
29         warn "# $path ", -s $path, "\n";
30
31 } elsif ( -e $path ) {
32         open(my $fh, '<', $path);
33         my $ip = <$fh>;
34         chomp $ip;
35         $hash->{local_ip} = $ip;
36         close($fh);
37
38 } elsif ( my $koha_login = $query->param('koha_login') ) {
39         my $path = "$dir/user/$koha_login";
40         if ( -e $path ) {
41                 open(my $fh, '<', $path);
42                 $hash->{local_ip} = <$fh>;
43                 close $fh;
44         } else {
45                 warn "# no $path";
46         }
47
48 } else {
49         warn $hash->{_error} = "ERROR: ", $hash->{remote_host}, " don't have RFID reader assigned";
50 }
51
52 if ( $query->param('intranet-js') ) {
53         print "Content-type: application/javascript\r\n\r\n";
54
55         if ( my $local_ip = $hash->{local_ip} ) {
56                         chomp $local_ip;
57                         my $url = "/rfid/to/$local_ip";
58                         open(my $js, '<', 'koha-rfid.js');
59                         while(<$js>) {
60                                 s/local_ip/$local_ip/g;
61                                 s/localhost/$url/g;
62                                 s{///$url}{$url}g; # relative urls
63                                 print;
64                         }
65                         close($js);
66         } else {
67                         print 'console.log("no RFID reader for client "'. $hash->{remote_host}, ");\n";
68
69         }
70 } else {
71         print "Content-type: application/json; charset=utf-8\r\n\r\n";
72         print encode_json $hash;
73         warn "# hash = ",dump($hash);
74 }