<?php 
//////////////////////////////////////////////////////////////////////////////
//   Imp3web.php:  Web Jukebox frontend to imp3sh, 
//   used for web control of a local computer jukebox
//Copyright (C) 2001 Jef Spaleta (jspaleta@linuxfreemail.com)
//
//This program 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.
//
//    This program 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 this program; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//////////////////////////////////////////////////////////////////////////////
//  Revision History
//  v0.1-09/24/2001  Initial public release meant for inclusion with 
//                   the imp3sh project
//////////////////////////////////////////////////////////////////////////////
//  Todo:
//  *Implement multiple user system with config'able user requests limits
//  *Implement operator user will full access to imp3sh controls from the web
//  *Add images for buttons 
////////////////////////////////////////////////////////////////////////////// 
//session_start();


/////////////////////////////////////////////////////////////////////////////
////////////////////////       Configs     //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//Script localation on the webserver
//$impVars["script"]     = substr($SCRIPT_NAME,1);
$impVars["script"]     = "/imp3web.php";

//Note:  net* variables use only if method = net
//Filesystem location of the network imp3sh client
$impVars["netclient"]  = "/var/tmp/imp3net";
//Hostname or ip address of imp3sh host
$impVars["nethost"]    = "test";
//Port number on imp3sh host
$impVars["netport"]    = "9998";

//Note:  local* variables use only if method = net
//Filesystem location of the local imp3sh client
$impVars["localclient"]= "/var/tmp/imp3client";

//Filesystem location of the input and output pipes for the local client
$impVars["inpipe"]     = "/var/tmp/tmpin";
$impVars["outpipe"]    = "/var/tmp/tmpout";

//Choose the client method: local or net
$impVars["method"]     ="net";


//Playlist Parsing Section----------------------------------------------
//number indicates segment of playlist name where variable info is encoded
//sections are separated by / 
// If the variables not encoded in the playlist are given a -1 
//example playlist name:  REM/reveal/rock/2001
// genre=2
// artist=0
// album=1
// year=3
// label=-1
//-------------------------------------------------------------------------
$impVars["genre"]      = -1;
$impVars["artist"]     = 0;
$impVars["title"]      = 1;
$impVars["year"]       = -1;
$impVars["label"]       = -1;

//Web Interface Formatting Section
// bgcolor: general page variables
$impVars["bgcolor"]   = "black";
$impVars["linkcolor"]   = "red";
$impVars["vlinkcolor"]   = "red";
$impVars["textcolor"]   = "white";
// header format variables
$impVars["headerwidth"]   = "100%";
$impVars["headerbgcolor"]   = "darkgrey";
$impVars["headertextcolor"]   = "white";
// footer format variables
$impVars["footerwidth"]   = "100%";
$impVars["footerbgcolor"]   = "darkgrey";
$impVars["footertextcolor"]   = "white";
// controlbar format variables
$impVars["ctrlwidth"]   = "80%";
$impVars["ctrlcolor"]   = "779977";
$impVars["ctrllinkcolor"]   = "white";
$impVars["ctrlvlinkcolor"]   = "white";
$impVars["ctrltextcolor"]   = "white";
// list format variables
$impVars["listwidth"]   = "80%";
$impVars["listbgcolor1"]   = "#AAFF55";
$impVars["listbgcolor2"]   = "#55FFAA";
$impVars["listtextcolor"]   = "red";
$impVars["listlinkcolor"]   = "black";
$impVars["listvlinkcolor"]   = "black";
// navigation bar format variables
$impVars["navwidth"]   = "80%";
$impVars["navcolor"]   = "#AAAA55";
$impVars["navlinkcolor"]   = "black";
$impVars["navvlinkcolor"]   = "black";
$impVars["navtextcolor"]   = "black";
// status page format variables
$impVars["statuswidth"]   = "80%";
$impVars["statuscolor1"]   = "yellow";
$impVars["statuscolor2"]   = "darksalmon";
$impVars["useimages"]       = 0;

//Internal command structure---you shouldn't have to change these
$impVars["localbase"]  =$impVars["client"]." ".$impVars["inpipe"]." ".$impVars["outpipe"]." ";
$impVars["netbase"]    =$impVars["netclient"]." ".$impVars["nethost"]." ".$impVars["netport"]." ";
////////////////////////////////////////////////////////////////////////////
//////////////////////////    End Of Configs     ///////////////////////////
////////////////////////////////////////////////////////////////////////////
function imp3($text)
{
  global $impVars;
  if($impVars["method"]=="net"){$command=$impVars["netbase"]."\"".$text."\"";}
  if($impVars["method"]=="local"){$command=$impVars["localbase"]."\"".$text."\"";}
  $output=`$command`;
  return $output;

}
function stop()
{
  global $impVars;
  $output=imp3("stop");
  $output=imp3("clearq");
  $output=imp3("uselist default");
}

function pause()
{
  global $impVars;
  $output=imp3("pause");
}

function resume()
{
  global $impVars;
  $output=imp3("resume");
}

function mute()
{
  global $impVars;
  $output=imp3("mute");
}

function vol_low()
{
  global $impVars;
  $output=imp3("volume 5 50 50");
}

function vol_normal()
{
  global $impVars;
  $output=imp3("volume 30 50 50");
}

function vol_up()
{
  global $impVars;
  $output=imp3("status");
  $lines = split("^.*Mixer.*:",$output);
  $level=substr($lines[1],8,3);
  $level=$level+5;
  if($level>100){ $level=100;}
  $command="volume ".$level." 50 50";
  $output=imp3($command);
}

function vol_down()
{
  global $impVars;
  $output=imp3("status");
  $lines = split("^.*Mixer.*:",$output);
  $level=substr($lines[1],8,3);
  $level=$level-5;
  if($level<0){ $level=0;}
  $command="volume ".$level." 50 50";
  $output=imp3($command);

}

function play()
{
  global $impVars;
  $output=imp3("play");
}

function currentsong()
{
  global $impVars;
  $output=imp3("status");
  $strings=explode("\n",str_replace("-->","",$output));
  $tablehead="<table frame=void cellspacing=0 align=center";
  $tablehead=$tablehead." width=".$impVars["statuswidth"];
  $tablehead=$tablehead.">";
  echo $tablehead;
  echo "<tr align=center bgcolor=".$impVars["statuscolor1"]."><td>";
  echo $strings[1];
  echo "</td></tr>";
  echo "<tr align=center bgcolor=".$impVars["statuscolor2"]."><td>";
  echo $strings[2];
  echo "</td></tr>";
  echo "<tr align=center bgcolor=".$impVars["statuscolor1"]."><td>";
  echo $strings[3];
  echo "</td></tr>";
  echo "<tr align=center bgcolor=".$impVars["statuscolor2"]."><td>";
  echo $strings[4];
  echo "</table>";
}


function nextsong()
{
  global $impVars;
  $output=imp3("next");
  frontpage();
}
function previoussong()
{
  global $impVars;
  $output=imp3("last");
  frontpage();
}

function help()
{
  global $impVars;
  $output=imp3("help");
  echo str_replace("\n","<br>",$output);
}
function frontpage($task="")
{
  if(strcmp($task,"")!=0){echo "The command $task is unimplemented<br>";}
  currentsong();
}

function button($label="",$class="",$image="",$a="",$b="",$c="",$d="")
{
  global $impVars;
  $bstring="<a ";
  if($class!="") $bstring=$bstring."class=\"".$class."\" ";
  $bstring=$bstring."href=\"";
  $bstring.=($impVars["script"])."?";
  if($a != "") { $bstring=$bstring."a=".$a."&";}
  if($b != "") { $bstring=$bstring."b=".$b."&";}
  if($c != "") { $bstring=$bstring."c=".$c."&";}
  if($d != "") { $bstring=$bstring."d=".$d;}
  $bstring=$bstring."\">".$label."</a>";
  echo "$bstring"; 
}

function albumlist($sorttype=-1,$sortitem="")
{
  global $impVars;
  $output=imp3("liststatus");
  $lines = split("\n",$output);
  $linecount=count($lines);
  $albumcount=0;
  $sortcount=0;
  $active=-1;
  $flag=0;
  //Sort the output lines
  for ($i=0; $i < $linecount ; $i++) 
  {
//find the active album
    if(ereg("^>>>",$lines[$i])) 
    { 
      $album=preg_replace("/[>>> ,--> ,-->\*]/","",$lines[$i]);
      $album=preg_replace("/\n/","",$album);
      if(!eregi("^queue|^default",$album))
      {
        $albums[$albumcount]=$album;
        $active=$albumcount;
        $albumcount++;
        $flag=2;
      }
    }
    //find the inactive albums
    if(ereg("^-->",$lines[$i])) 
    { 
      $album=preg_replace("/[>>> ,--> ,-->\*]/","",$lines[$i]);
      $album=preg_replace("/\n/","",$album);
      if(!eregi("^queue|^default",$album))
      {
        $albums[$albumcount]=$album;
        $albumcount++;
        $flag=1;
      }
    }
    if($flag>0)
    {
      //check to see if we are sorting by type(genre,artist,etc...)
      if($sorttype!=-1) 
      {
        //pick the corresponding field specified by sort type number
        $fields = split("/",$album);
        $field = $fields[$sorttype];
        //check sortitem to see if we are listing albums 
        if($flag==2) 
        {
          $active=$sortcount;
        }
        if($sortitem!="") 
        {
          //if there is a sortitem present list those items corresponding to sortitem
          if($sortitem==$field) 
          {
            $sortlist[$sortcount]=$album;
            $sortcount++;
          }
        } else {
          $sortlist[$sortcount]=$field;
          $sortcount++;
        }
      } 
      $flag=0;
    }
  }

  if($sorttype!=-1) 
  {
    if($sortitem!="") 
    {
      $sortcount=count($sortlist);
      echo "<table frame=void cellspacing=0 align=center width=".$impVars["listwidth"].">";
      for ($i=0; $i < $sortcount ; $i++) 
      {
        $album=$sortlist[$i];
        if(($i % 2)==0) {
          $color=$impVars["listbgcolor1"];
        } else {
          $color=$impVars["listbgcolor2"];
        }       
        echo "<tr align=center bgcolor=".$color." ><td align=left class=\"list\">";
        if($active==$i) echo "<b>";
        echo $album;
        if($active==$i) echo "</b>";
        echo "</td><td align=right>";
        button("songlist","list","","songlist",$album);
        echo "</td><td align=right>";
        button("playalbum ","list","","playalbum","$album");
        echo "</td><td align=right>";
        button("addalbum ","list","","addalbum","$album");
      }
      echo "</table>";
    } else {
      $newlist=array_unique($sortlist);    
      $sortcount=count($sortlist);
      echo "<table frame=void cellspacing=0 align=center width=".$impVars["listwidth"].">";
      $j=0;
      for ($i=0; $i < $sortcount ; $i++) 
      {
        $album=$newlist[$i];
          if($album!="")
          {
            if(($j % 2)==0) {
              $color=$impVars["listbgcolor1"];
            } else {
              $color=$impVars["listbgcolor2"];
            }       
            echo "<tr align=center bgcolor=".$color." ><td align=left class=\"list\">";
            if($active==$i) echo "<b>";
            echo $album;
            if($active==$i) echo "</b>";
            echo "</td><td align=right>";
            button("List","list","","sortlist","$sorttype",$album);
            $j++;
          }
      }
      echo "</table>";

    }
  } else {

    $albumcount=count($albums);
    echo "<table frame=void cellspacing=0 align=center width=".$impVars["listwidth"].">";
    for ($i=0; $i < $albumcount ; $i++) 
    {
      $album=$albums[$i];
          if(($i % 2)==0) {
            $color=$impVars["listbgcolor1"];
          } else {
            $color=$impVars["listbgcolor2"];
          }       
          echo "<tr align=center bgcolor=".$color." ><td align=left class=\"list\">";
          if($active==$i) echo "<b>";
          echo $album;
          if($active==$i) echo "</b>";
          echo "</td><td align=right>";
          button("songlist","list","","songlist",$album);
          echo "</td><td align=right>";
          button("playalbum","list","","playalbum","$album");
          echo "</td><td align=right>";
          button("addalbum","list","","addalbum","$album");
    }
    echo "</table>";
  }
}

function songlist($album)
{
  global $impVars;
  $command="editlist ".$album;
  $output=imp3($command);
  $command=$impVars["combase"]."printlist";
  $output=imp3($command);
  $lines = split("\n",$output);
  $linecount=count($lines);
  $songcount=0;
  $active=-1;
  for ($i=0; $i < $linecount ; $i++) 
  {
      if(ereg("^\|\|\|",$lines[$i])) 
      { 
        $songs[$songcount]=$lines[$i];
        $songcount++;
      }
      if(ereg("^>>>",$lines[$i])) 
      { 
        $songs[$songcount]=$lines[$i];
        $active=$songcount;
        $songcount++;
      }
  }
  $songcount=count($songs);
  echo "<table frame=void cellspacing=0 align=center width=".$impVars["listwidth"].">";
  for ($i=0; $i < $songcount ; $i++) 
  {
    if(($i % 2)==0) {
      $color=$impVars["listbgcolor1"];
    } else {
      $color=$impVars["listbgcolor2"];
    }       
    echo "<tr align=center bgcolor=".$color." ><td class=\"list\">";
    $song=preg_replace("/[>>>,|||].*> /","",$songs[$i]);
    if($active==$i) echo "<b>";
    echo $song;
    if($active==$i) echo "</b>";
    echo "</td><td align=right>";
    echo button("playsong","list","","playsong",$album,$song,"$i");
    echo "</td><td align=right>";
    echo button("addsong","list","","addsong",$album,$song,$i);
  }
  echo "</table>";
}

function quelist()
{
  global $impVars;
  $output=imp3("printq");
  $lines = split("\n",$output);
  $linecount=count($lines);
  $songcount=0;
  $active=-1;
  for ($i=0; $i < $linecount ; $i++) 
  {
      if(ereg("^\|\|\|",$lines[$i])) 
      { 
        $songs[$songcount]=$lines[$i];
        $songcount++;
      }
      if(ereg("^>>>",$lines[$i])) 
      { 
        $songs[$songcount]=$lines[$i];
        $active=$songcount;
        $songcount++;
      }
    
  }
  $songcount=count($songs);
  echo "<table frame=void cellspacing=0 align=center width=".$impVars["listwidth"].">";
  for ($i=0; $i < $songcount ; $i++) 
  {
    if(($i % 2)==0) {
      $color=$impVars["listbgcolor1"];
    } else {
      $color=$impVars["listbgcolor2"];
    }       
    echo "<tr align=center bgcolor=".$color." ><td align=left class=\"list\">";
  $song=preg_replace("/[>>>,|||].*> /","",$songs[$i]);
  if($active==$i) echo "<b>";
  echo $song;
  if($active==$i) echo "</b>";
  }
  echo "</table>";
}

function queclear()
{
  global $impVars;
  $output=imp3("clearq");

}

function addsong($album,$index)
{
  global $impVars;
  $command="uselist ".$album;
  $output=imp3($command);
  $command="addq ".$index;
  $output=imp3($command);
  $output=imp3("uselist default");
  quelist();
}

function playsong($album,$index)
{
  global $impVars;
  $command="uselist ".$album;
  $output=imp3($command);
  $command="playindex ".$index;
  $output=imp3($command);
  frontpage();
}

function playalbum($album)
{
  global $impVars;
  $output=imp3("stop");

  $output=imp3("clearq");
  $command="uselist ".$album;
  $output=imp3($command);
  $command="addq 0-";
  $output=imp3($command);
  $output=imp3("play");
  $output=imp3("uselist default");
  quelist();
}

function addalbum($album)
{
  global $impVars;
  $command="uselist ".$album; 
  $output=imp3($command);
  $command="addq 0-";
  $output=imp3($command);
  $output=imp3("uselist default");
  quelist();
}

function menubar()
{
echo "$script";
}

function navbar()
{
  global $impVars;
  echo "<DIV class=\"nav\">";
  $tablehead="<table frame=void cellspacing=0 align=center";
  $tablehead=$tablehead." width=".$impVars["navwidth"];
  $tablehead=$tablehead." bgcolor=".$impVars["navcolor"];
  $tablehead=$tablehead."><tr align=center><td>";
  echo $tablehead;
  button("Home","nav","","");echo "::"; 
  button("albums","nav","","albums");echo "::"; 
  $test=$impVars["artist"];
  if($impVars["artist"] > -1) {button("artists","nav","","artists");echo "::";} 
  if($impVars["genre"] > -1) {button("genre","nav","","genre");echo "::";} 
  if($impVars["label"]>-1) {button("label","nav","","label");echo "::";} 
  if($impVars["year"]>-1) {button("year","nav","","year");echo "::";} 
  button("que","nav","","que");echo ":"; 
  echo "</table></div>";

}
function controlbar()
{
  global $impVars;
  $tablehead="<table frame=void cellspacing=0 align=center";
  $tablehead=$tablehead." width=".$impVars["ctrlwidth"];
  $tablehead=$tablehead." bgcolor=".$impVars["ctrlcolor"];
  $tablehead=$tablehead."><tr align=center ><td>";
  echo $tablehead;
  button("queclear","ctrl","","queclear");echo "::"; 
  button("stop","ctrl","","stop");echo":";
  button("play","ctrl","","play");echo":";
  button("pause","ctrl","","pause");echo":";
  button("resume","ctrl","","resume");echo":";
  button("next","ctrl","","next");echo":";
  button("previous","ctrl","","previous");echo"::";
  button("vol_up","ctrl","","vol_up");echo":";
  button("vol_down","ctrl","","vol_down");echo ":";
  button("mute","ctrl","","mute");
  echo "</td></tr></table>";
}
function Head()
{
  global $impVars;
  $headstring="<html><head><title>PHP Test</title>";
  $headstring=$headstring."<STYLE TYPE-\"type/css\"><!-- "; 
  $headstring=$headstring.".list {color:".$impVars["listtextcolor"]."}";
  $headstring=$headstring.".header {color:".$impVars["headertextcolor"]."}";
  $headstring=$headstring.".footer {color:".$impVars["footertextcolor"]."}";
  $headstring=$headstring.".nav {color:".$impVars["navtextcolor"]."}";
  $headstring=$headstring.".ctrl {color:".$impVars["ctrltextcolor"]."}";
  $headstring=$headstring."A:link.nav {color:".$impVars["navlinkcolor"]."}";
  $headstring=$headstring."A:visited.nav {color:".$impVars["navvlinkcolor"]."}";
  $headstring=$headstring."A:link.ctrl {color:".$impVars["ctrllinkcolor"]."}";
  $headstring=$headstring."A:visited.ctrl {color:".$impVars["ctrlvlinkcolor"]."}";
  $headstring=$headstring."A:link.list {color:".$impVars["listlinkcolor"]."}";
  $headstring=$headstring."A:visited.list {color:".$impVars["listvlinkcolor"]."}";

  $headstring=$headstring." --></STYLE>";
  $headstring=$headstring."</head><body";
  $headstring=$headstring." bgcolor=".$impVars["bgcolor"];
  $headstring=$headstring.">";
  echo $headstring;
  $tablehead="<table frame=void cellspacing=0 align=center";
  $tablehead=$tablehead." width=".$impVars["headerwidth"];
  $tablehead=$tablehead." bgcolor=".$impVars["headerbgcolor"];
  $tablehead=$tablehead."><tr><td class=\"header\">";
  echo $tablehead;
  echo "Welcome to Imp3web (V0.1): the Imp3sh powered WebJukebox";
  echo "</table>";
  
//  menubar();
  navbar();
  controlbar();
}

function Foot()
{
  global $impVars;
  $tablehead="<table frame=void cellspacing=0 align=center";
  $tablehead=$tablehead." width=".$impVars["footerwidth"];
  $tablehead=$tablehead." bgcolor=".$impVars["footerbgcolor"];
  $tablehead=$tablehead."><tr><td class=\"footer\">";
  echo $tablehead;
  echo "Copyright (2) 2001 Jef Spaleta (spatula@linuxfreemail.com)";
  echo "</table>";

 echo "</body></html>";
}



Head();
switch($a)
{
  case "albums":
    albumlist();
    break;
  case "artists":
    albumlist($impVars["artist"]);
    break;
  case "genre":
    albumlist($impVars["genre"]);
    break;
  case "year":
    albumlist($impVars["year"]);
    break;
  case "label":
    albumlist($impVars["label"]);
    break;
  case "sortlist":
    albumlist($b,$c);
    break;
  case "playalbum":
    playalbum($b);
    break;
  case "addalbum":
    addalbum($b);
    break;
  case "que":
    quelist();
    break;
  case "queclear":
    queclear();
    break;
  case "next":
    nextsong();
    break;
  case "previous":
    previoussong();
    break;
  case "play":
    play();
    break;
  case "stop":
    stop();
    break;
  case "pause":
    pause();
    break;
  case "resume":
    resume();
    break;
  case "help":
    help();
    break;
  case "songlist":
    songlist($b);
    break;
  case "addsong":
    addsong($b,$c);
    break;
  case "playsong":
    playsong($b,$d);
    break;
  case "vol_up":
    vol_up();
    break;
  case "vol_down":
    vol_down();
    break;
  case "mute":
    mute();
    break;
  default:
    frontpage($a);
}   
Foot();
?>
