RFID: add title to page
[koha_ffzg] / ffzg / rfid / koha-reader.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use CGI qw ( -utf8 );
5 use lib '/srv/koha_ffzg';
6 use C4::Auth;
7 use C4::Output;
8 use Data::Dump qw(dump);
9
10 my $query = new CGI;
11
12 # fake koha login so we can get valid session
13 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
14     {
15         template_name   => "intranet-main.tt",
16         query           => $query,
17         type            => "intranet",
18         authnotrequired => 0,
19         flagsrequired   => { catalogue => 1, },
20     }
21 );
22
23 my $session = $query->cookie('CGISESSID');
24 my $url = $query->url;
25 # hungle URL to get Koha authorization and keep our reader identification in URL
26 # https://ffzg.koha-dev.rot13.org:8443/cgi-bin/koha/ffzg/rfid/reader/10.60.0.92:9000/mainpage.pl
27 my ( $redirect, $reader_ip_port ) = ( $1 . $3 , $2 ) if $url =~ s{(^.+)/ffzg/rfid/reader/([^/]+)(/.+)$}{$1};
28
29 warn "## $session $reader_ip_port";
30 my $session_file = "/dev/shm/rfid.$session";
31
32 if ( $reader_ip_port ) {
33
34 open(my $fh, '>', $session_file) || die "$session_file: $!";
35 print $fh $reader_ip_port;
36 close($fh);
37
38 } # $reader_ip_port
39
40 sub check_rfid_reader {
41         my $host_port = shift;
42         if ( my $sock = IO::Socket::INET->new($reader_ip_port) ) {
43                 print $sock "GET /scan HTTP/1.0\r\n\r\n";
44                 local $/ = undef;
45                 return "OK\n\n" . <$sock>;
46         } else {
47                 return "RFID ERROR $reader_ip_port : $!";
48         }
49
50 }
51
52 my $reader_url = $query->url( -path => 1 );
53 $reader_url =~ s{/[^/]+$}{}; # strip script name
54 $reader_url . '/ffzg/rfid/';
55
56 output_html_with_http_headers $query, $cookie, join('',qq{
57 <html>
58 <head>
59 <title>RFID reader selection</title>
60 </head>
61 <a id="redirect" href="$redirect"">$redirect</a>
62 <ol>
63 <li>Koha session: <pre>$session },(-e $session_file ? 'OK' : 'ERROR: MISSING'),qq{</pre></li>
64 }, ( $reader_ip_port
65         ? qq{<li>RFID reader: <pre>$reader_ip_port } . check_rfid_reader( $reader_ip_port ) . qq{</pre></li>}
66         : qq{RFID available:<ul><li>} . join('</li><li>',map { qq{<a href="$reader_url/$_" target="rfid">$_</a>} } glob('reader/*/*')) . qq{</li></ul>}
67 ), qq{
68 </ol>
69 <script>
70 var e = document.getElementById('redirect');
71 console.log(e);
72 //e.click();
73 </script>
74 </html>
75 });