#!/usr/bin/perl


# Copyright 2005 Aaron Wolfe, Jacint Balint
#
#    This file is part of tunequeue.
#
#    tunequeue is free software; you can redistribute it and/or modify      
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    tunequeue is distributed in the hope that it will be useful,      
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with tunequeue; if not, write to the Free Software      
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

use CGI qw(:standard);
use DBI;
require "setup.pl";

my $dbh = DBI->connect($config{'DB_STRING'},$config{'DB_USER'},$config{'DB_PASS'});

$CGI::DISABLE_UPLOADS = 1;
$CGI::POST_MAX        = 102_400; # 100 KB

my $q = new CGI;

my $auser = $q->param( 'user' );
my $apass = $q->param( 'pass' );
my $action = $q->param( 'action' );
my $tid = $q->param( 'tid' );
my $srow = $q->param( 'srow' );
my $ssid = $q->param( 'showid' );

my %cookie_val = $q->cookie(-name => 'iruleyou');

$admin = 0;

if ($cookie_val{'admin'} == 1)
{
 $admin = 1;
 $user = $cookie_val{'user'};
 print $q->header("text/html");

 #load up some shit for later
 $sql = "SELECT ID,name,incomingpath,defaultplaylist FROM channels WHERE admin = ?";
 $out = $dbh->prepare($sql);
 $out->execute($user);
 
 ($channel,$channelname,$incomingpath,$defaultplaylist)=$out->fetchrow_array;
}
elsif ($auser ne "")
{
#log the bitch in
 $sql = "SELECT password FROM channels WHERE admin = ?";
 $out = $dbh->prepare($sql);
 $out->execute($auser);

 if (my($tpass)=$out->fetchrow_array)
 {
  if ($tpass eq $apass)
  {
   $admin = 1;
   $user = $auser;
   #set cookie
   
   %cookie_val = ();
   $cookie_val{ 'admin' } = 1;
   $cookie_val{ 'user' } = $auser;

   my $cookie = $q->cookie (
                -name    => 'iruleyou',
                -value   => \%cookie_val,
                -path    => '/',
                -expires => '+24h'
   );

   print $q->header(-cookie => [$cookie]);
  }
  else
  { 
   print $q->header("text/html");
  }
 }
}
else
{
 print $q->header("text/html");
}

print "<HTML><HEAD>";
print "<TITLE>SPOGBIPER - Administration</TITLE>";

print "<LINK HREF=\"/admin.css\" rel=\"stylesheet\" type=\"text/css\">";

print "</HEAD><BODY BGCOLOR=#004466 ALINK=#FFFFFF LINK=#FFFFFF VLINK=#FFFFFF>";

print "<IMG SRC=/spogbiper.gif>";
print "<h2><A HREF=/cgi-bin/admin.pl>[Main menu]</A> &nbsp;&nbsp;Channel: $channelname</h2><P>";  
if ($admin)
{
 # LOGGED IN...

 print "<TABLE CELLPADDING=10><TR><TD VALIGN=TOP>";
 print "<h2>Current listeners:<h2>";
 print "<TABLE CELLPADDING=3>";
 print "<TR><TD><B>IP:</TD><TD><B>Hostname:</TD><TD><B>Country:</TD><TD><B>Since:</TD><TD><B>Type:</TD></TR>";

 $qry = "SELECT ip,start,type FROM listeners WHERE channelid = ? AND end = ?";
 $out = $dbh->prepare($qry);
 $out->execute($channel,"0000-00-00 00:00:00");

 while (my($ip,$start,$type)=$out->fetchrow_array)
 {
  print "<TR><TD>$ip</TD> ";

  $host = `host $ip`;
  @hosts = split(/ /,$host);

  print "<TD>$hosts[@hosts-1]</TD>";

  $geo = `geoiplookup $ip`;
  @geos = split(/:/,$geo);  

  print "<TD>$geos[1]</TD><TD>$start</TD><TD>$type</TD>";  
 }

 print "</TABLE><P>";
 
 print "<IMG SRC=/channel$channel.gif><P>";
 print "<IMG SRC=/channel$channel-lt.gif><P>";

 print "</TD><TD VALIGN=TOP>";

 #list recent listeners
 print "<H2>Recent listeners:</H2><P>";

 $qry = "SELECT ip,start,end,type,start_song,end_song FROM listeners WHERE channelid = ? AND end != ? ORDER BY end DESC LIMIT 15";
 $out = $dbh->prepare($qry);
 $out->execute($channel,"0000-00-00 00:00:00");

 print "<TABLE CELLPADDING=3>";
 print "<TR><TD><B>Hostname:</TD><TD><B>Country:</TD><TD><B>Started:</TD><TD><B>Ended:</TD><TD><B>Type:</TD></TR>";

 while (my($ip,$start,$end,$type,$start_song,$end_song)=$out->fetchrow_array)
 {
  print "<TR>";

  $host = `host $ip`;
  @hosts = split(/ /,$host);

  print "<TD>$hosts[@hosts-1]<BR>$ip</TD>";

  $geo = `geoiplookup $ip`;
  @geos = split(/:/,$geo);

  print "<TD>$geos[1]</TD>";

  $qry2 = "SELECT artist,title FROM tunes WHERE ID = ?";
  $out2 = $dbh->prepare($qry2);
  $out2->execute($start_song);
  
  my($artist,$title)=$out2->fetchrow_array;
  
  print "<TD>$start<BR>During \"$title\" by $artist</TD>";

  $qry2 = "SELECT artist,title FROM tunes WHERE ID = ?"; 
  $out2 = $dbh->prepare($qry2);
  $out2->execute($end_song);

  my($artist,$title)=$out2->fetchrow_array;

  print "<TD>$end<BR>During \"$title\" by $artist</TD>";

  print "<TD>$type</TD>";
 }

 print "</TABLE>";

}
else
{
 print "<FONT COLOR=#FFFFFF><H3>What it is</H3>";
 print "<FORM>";
 print "<TABLE><TR><TD><FONT COLOR=#FFFFFF>Name: </TD><TD><INPUT TYPE=TEXT NAME=user></TD></TR>";
 print "<TR><TD><FONT COLOR=#FFFFFF>Password: </TD><TD><INPUT TYPE=PASSWORD NAME=pass></TD></TR>";
 print "<TR><TD COLSPAN=2><CENTER><INPUT TYPE=Submit VALUE=Login></TD></TR></TABLE>";
 print "</FORM>";
}

print "</BODY></HTML>";

