<?php 
  include_once "./audiostreamerlib.php";

  //  AudioStreamer, www.audiostreamer.org
  //  Copyright (C) <2013>  <Lieven Rottiers>
  //
  //  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 3 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, see <http://www.gnu.org/licenses/>.
	
  $output = '';
  $id = $_GET["id"];
  //in case of resampling the current song, this takes a little longer
  set_time_limit(60);
  
  try {
    $dbh = new PDO("sqlite:".$sdb);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);     
    
    $sql = "select b.id, b.filename, b.artist, b.album, b.song, b.length 
            from music a,
                 music b
            where a.id=".$id." 
					 ";

    if ($_SESSION["ind_genre_radio"] == "1") {
 	  $sql = $sql."
              and ifnull(a.genre,'0') = ifnull(b.genre,'0')";
		}
					 
 	  $sql = $sql."
          		and ifnull(b.extension,'dir') in ('mp3','wma','wav','ogg')
						order by random()
            limit 1";
    
    foreach ($dbh->query($sql) as $row) {
      if (!empty($row['song'])) {
        $playlist = $row['artist']." - ".$row['album']." - ".$row['song']." (".$row['length'].")";
      }
      else {
        $playlist = utf8_encode($row['filename']);
      }
      $output = $output.'<li><div onClick="deleteSong(this)" class="jp-playlist-delete"></div><div title="move up-down" class="jp-playlist-move"></div><a href="#" onClick="playSong(this)" songId="'.$row['id'].'" mp3="./streammp3.php?id='.$row['id'].'" title="'.$playlist.'">'.$playlist.'</a></li>';      
    }   
    //close connection
    $dbh = null;    
  }
  catch(PDOException $e)
  {
    $output = $output.'<br/>'.$e->getMessage();
  }  
  
  echo $output;
  //echo utf8_encode($output);
  ?> 
