#!/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;
use Ogg::Vorbis::Header;
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...
 
 %shows = ();

 #get show list
 $sqry = "SELECT ID,showname FROM shows WHERE channelid = ? AND enabled = 1";
 my $sout = $dbh->prepare($sqry);
 $sout->execute($channel);

 while (my($showid,$showname)=$sout->fetchrow_array)
 {
  $shows{$showid} = $showname;
 }

 #deal with any action

 if ($action ne "")
 {
  if ($action eq "remove")
  {
   $sql = "DELETE FROM channeltunes WHERE channelid = ? AND tuneid = ?";
   my $out = $dbh->prepare($sql);
   $out->execute($channel,$tid); 
  }
  elsif ($action eq "removerp")
  {
   $sql = "UPDATE channeltunes SET randomplay = 0 WHERE channelid = ? AND tuneid = ?";
   my $out = $dbh->prepare($sql);
   $out->execute($channel,$tid);
  }
  elsif ($action eq "add")
  {
   #no dups
   $sql1 = "SELECT tuneid FROM channeltunes WHERE channelid = ? AND tuneid = ?";
   my $out1 = $dbh->prepare($sql1);
   $out1->execute($channel,$tid);

   unless (my($crap)=$out1->fetchrow_array)
   {

    $sql = "INSERT INTO channeltunes (added,channelid,tuneid) VALUES (NOW(),?,?)";
    my $out = $dbh->prepare($sql);
    $out->execute($channel,$tid);
   }
  }
  elsif ($action eq "addrp")
  {
   $sql = "UPDATE channeltunes SET randomplay = 1 WHERE channelid = ? AND tuneid = ?";
   my $out = $dbh->prepare($sql);
   $out->execute($channel,$tid);
  }
  elsif ($action eq "intoshow")
  {
   $sql = "INSERT INTO playlists (TuneID,ShowID) VALUES (?,?)";
   my $out = $dbh->prepare($sql);
   $out->execute($tid,$ssid);
  }
  elsif ($action eq "outofshow")
  {
   $sql = "DELETE FROM playlists WHERE TuneID = ? AND ShowID = ?";
   my $out = $dbh->prepare($sql);
   $out->execute($tid,$ssid);
  }
 }


 $qry = "SELECT ID,title,artist,album,time,filename FROM tunes 
         ORDER BY artist,album,title";
 
 my $out = $dbh->prepare($qry);
 $out->execute;

 $bsrow = $srow - 30;
 $fsrow = $srow + 30;

 $bbsrow = $srow - 300;
 $ffsrow = $srow + 300;

 if ($bsrow < 0) { $bsrow = 0; }
 if ($bbsrow < 0) { $bbsrow = 0; }

 print "<B><A HREF=/cgi-bin/songman.pl?srow=$bbsrow><<-- </A>&nbsp;&nbsp;";
 print "<A HREF=/cgi-bin/songman.pl?srow=$bsrow><-- BACK</A>&nbsp;&nbsp;&nbsp;&nbsp;";
 print "<A HREF=/cgi-bin/songman.pl?srow=$fsrow>FORWARD --></A>";
 print "<A HREF=/cgi-bin/songman.pl?srow=$ffsrow>&nbsp;&nbsp;-->></A><P>";

 print "<TABLE RULES=ROWS>";
 print "<TR><TD><B>TuneID</TD><TD><B>Artist</TD><TD><B>Album</td><TD><B>Title</TD><TD><B>Length</TD>";

 print "<TD><B>Play in channel?</TD><TD><B>Actions</TD><TD>&nbsp;</TD>";

 while ( my ($showid, $showname) = each(%shows) ) {
        print "<TD> $showname &nbsp;&nbsp;</TD>";
 }

 print "</TR>";
 
 $rr = 0;

 while (my($tuneid,$title,$artist,$album,$stime,$filename)=$out->fetchrow_array)
 {
  $rr++;
  if (($rr > $srow) and ($rr < ($srow + 31)))
  {
   $sql = "SELECT channelid,randomplay,disabled FROM channeltunes WHERE tuneid = ? ORDER BY channelid";
   my $out2 = $dbh->prepare($sql);
   $out2->execute($tuneid);

   $c = 0;
   $cr = 0;

   while (my($chanid,$randomplay,$disabled)=$out2->fetchrow_array)
   {
    if ($chanid eq $channel)
    {
     $c = 1;
     if ($randomplay) { $cr = 1;}
    }
   }
 
   if ($c) { $ct = "Yes"; } else {$ct = "No"; }
   if ($cr) { $crt = ", random play &nbsp;&nbsp;&nbsp;"; } else { $crt = ""; }

   $sfn = $filename;
   $sfn =~ s/\/usr\/local\/tunes\///;

   print "<TR><TD>$tuneid</TD><TD>$artist &nbsp;&nbsp;&nbsp;</TD><TD>$album &nbsp;&nbsp;&nbsp;</TD><TD>";
   print "<A HREF=\"http://www.spogbiper.com/tunes/$sfn?stream\">$title</A> &nbsp;&nbsp;&nbsp;</TD><td>$stime &nbsp;&nbsp;&nbsp;</TD>";
   print "<TD>$ct$crt</TD>";

   print "<TD>";
  
   if ($c)
   {
    print "<A HREF=\"/cgi-bin/songman.pl?srow=$srow&tid=$tuneid&action=remove\">Remove</A>&nbsp;&nbsp;</TD><TD>"; 
    if ($cr)
    {
     print "<A HREF=\"/cgi-bin/songman.pl?srow=$srow&tid=$tuneid&action=removerp\">Remove RP</A>";
    }
    else
    {
     print "<A HREF=\"/cgi-bin/songman.pl?srow=$srow&tid=$tuneid&action=addrp\">Add to RP</A>";
    }
   }
   else
   {
    print "<A HREF=\"/cgi-bin/songman.pl?srow=$srow&tid=$tuneid&action=add\">Add</A></TD><TD> ";
#   print "<A HREF=\"/cgi-bin/songman.pl?srow=$srow&tid=$tuneid&action=addrp\">Add to RP</A></TD><TD> ";  
   }

   print "</TD>";


   while ( my ($showid, $showname) = each(%shows) ) {
        print "<TD><CENTER>";

	$shqry = "SELECT ID FROM playlists WHERE TuneID = ? AND ShowID = ?";
        my $shout = $dbh->prepare($shqry);
        $shout->execute($tuneid,$showid);

	if (my($playid)=$shout->fetchrow_array)
        {
         print "<A HREF=\"/cgi-bin/songman.pl?srow=$srow&tid=$tuneid&action=outofshow&showid=$showid\">Yes</A>";
        }
        else
        {
         print "<A HREF=\"/cgi-bin/songman.pl?srow=$srow&tid=$tuneid&action=intoshow&showid=$showid\">No</A>";
        }

        print "</TD>";
   }

   print "</TR>";
  }
 }
 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>";

