#!/usr/bin/perl

use strict;

# LIRC CGI button press generator
# Version 1.0 - February 17th, 2008
# Developed by Jason Pimble.  Copyright 2008, licensed as public domain.

# This code is really ugly and is not something you should probably use.  
# It does performs minimal input validation, and does require a password, 
# but since it makes a system call it is still pretty risky code to have 
# publicly facing and access should be restricted to your local subnet.

sub scrub_input  {
  my $inValue = shift;

# valid buttons list, used for input scrubbing

  my %buttons = (
          Home                     => "0x14D5000000",
          Playlist                 => "0x14D6010000",
          Album                    => "0x14EE190000",
          Artist                   => "0x14F01B0000",
          Genre                    => "0x14F6210000",
          Track                    => "0x14F8230000",
          LiveTV                   => "0x14D8030000",
          DVD                      => "0x14D9040000",
          WEB                      => "0x14DA050000",
          Guide                    => "0x14DB060000",
          TV                       => "0x14DC070000",
          Power                    => "0x14D7020000",
          MOUSE_LEFT_BTN           => "0x144D780000",
          MOUSE_RIGHT_BTN          => "0x14517C0000",
          MOUSE_UP                 => "0x1447720000",
          MOUSE_DOWN               => "0x1448730000",
          MOUSE_LEFT               => "0x144C770000",
          MOUSE_RIGHT              => "0x1446710000",
          VolDown                  => "0x14DD080000",
          VolUp                    => "0x14DE090000",
          Mute                     => "0x14DF0A0000",
          ChanUp                   => "0x14E00B0000",
          ChanDown                 => "0x14E10C0000",
          1                        => "0x14E20D0000",
          2                        => "0x14E30E0000",
          3                        => "0x14E40F0000",
          4                        => "0x14E5100000",
          5                        => "0x14E6110000",
          6                        => "0x14E7120000",
          7                        => "0x14E8130000",
          8                        => "0x14E9140000",
          9                        => "0x14EA150000",
          0                        => "0x14EC170000",
          Add_Delete               => "0x14EB160000",
          A_B                      => "0x14ED180000",
          Up                       => "0x14EF1A0000",
          Down                     => "0x14F7220000",
          Left                     => "0x14F21D0000",
          Right                    => "0x14F41F0000",
          Enter                    => "0x14F31E0000",
          PageUp                   => "0x14F11C0000",
          PageDown                 => "0x14F5200000",
          Rewind                   => "0x14F9240000",
          Play                     => "0x14FA250000",
          Forward                  => "0x14FB260000",
          Record                   => "0x14FC270000",
          Stop                     => "0x14FD280000",
          Pause                    => "0x14FE290000",
  );

  if (exists $buttons{$inValue}) {
     return $inValue;
  }

  return undef;
}

use CGI;

my $query = new CGI;

my $buttonPressed = $query->param('RemoteSubmit');
my $password = $query->param('password');

print "Content-type: text/html\n\n";

print "<html>\n";
print "  <head><title>LIRC CGI gateway</title></head>\n";

# validate the password

if($password eq '1234') {

  # handle the button push

  if(scrub_input($buttonPressed)) {
    system("/usr/local/bin/lirc-udp.pl $buttonPressed");
  }
}
else {
  print "<p>Incorrect password</p>\n";
  $password = "";
}

# output the page

print "  <form method=\"post\" action=\"#\">\n";
print "    <input type=\"password\" name=\"password\" size=5 value=\"${password}\">Password<br>\n";
print "    <table><tr><td>\n";

print "    <tr><td width=33%></td></td><td width=33% align=center><input type=\"submit\" name=\"RemoteSubmit\" value=\"ChanUp\"></td><td width=33%></td></tr>\n";

print "    <tr><td align=center><input type=\"submit\" name=\"RemoteSubmit\" value=\"VolDown\"></td>\n";
print "    <td align=center><input type=\"submit\" name=\"RemoteSubmit\" value=\"Mute\">\n";
print "    <td align=center><input type=\"submit\" name=\"RemoteSubmit\" value=\"VolUp\"></td></tr>\n";

print "    <tr><td></td></td><td align=center><input type=\"submit\" name=\"RemoteSubmit\" value=\"ChanDown\"></td><td></td></tr>\n"; 

print "    <tr><td align=center><input type=\"submit\" name=\"RemoteSubmit\" value=\"Play\"></td>\n";
print "    <td align=center><input type=\"submit\" name=\"RemoteSubmit\" value=\"Stop\">\n";
print "    <td align=center><input type=\"submit\" name=\"RemoteSubmit\" value=\"Pause\"></td></tr>\n";

print "    </table>\n";
print "  </form>\n";
print "</html>\n";
