<?
////////////////////////////////////////////////////////////////////////
/*SMI - SHOUTcast Management Interface
A web based shoutcast server management program
Founding Author: Scott D. Harvanek <scotth@sourcemirrors.org>

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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
////////////////////////////////////////////////////////////////////////
session_start();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="include/smi.css">
</head>
<body link="#000000" alink="#000000" vlink="#000000">
<?
require('include/functions.inc.php');
if(!$_SESSION['username']){
echo "<center>Please <a href=\"login.php\">LOGIN</a></center>";
echo $_SESSION['username'];
}else{
$db = dbConnect();
$config = settings();
$uid = getuid($_SESSION['username']);

/////////////////////////////////////////////////////////////
//
// If we receive a request to create a new playlist
// execute the following:
//
/////////////////////////////////////////////////////////////
if($_POST['new']){
        $name = $_POST['plname'];
        $checkdups = $db->query('SELECT count(*) as numrecords FROM playlist WHERE name="'.$name.'" AND uid="'.$uid.'"');
        $checkdups = mysql_fetch_assoc($checkdups);
                if ($checkdups['numrecords'] >= 1){
                echo 'The playlist name already exists for your user!<br/>';
                } else {
        $db->insert('playlist', array( 'name' => $name, 'uid' => $uid, 'file' => $config['smi_path']."/playlists/".$uid.$name.".pls"));
        echo "Playlist Added!";
                }
        }



/////////////////////////////////////////////////////////////
//
// If we receive a request to delete a playlist
// execute the following
//
/////////////////////////////////////////////////////////////
if(isset($_POST['delete'])){
			$pid = $_POST['pid'];
			$db->delete('playlist', array('id' => $pid, 'uid' => $uid));
			deletePlaylist($pid,$uid);
			echo "Playlist Deleted";
			}

/////////////////////////////////////////////////////////////
//
// If we receive a request to delete a file from the playlist
// execute the following
//
/////////////////////////////////////////////////////////////
if(isset($_POST['files'])){
			$pid = $_POST['pid'];
			$checkuid = $db->getRow('playlist', array('id' => $pid), 'uid');
	                if ($checkuid['0'] !== $uid){
        	        echo 'Denied!<br/>';
                	} else {
			
			$files = $_POST['files'];
			foreach($files as $delfile){
					$db->delete('playlist_content', array('fid' => $delfile, 'pid' => $pid));
				}
			echo "Files removed from playlist";
			$edit = $pid;
			
					}
			}

//////////////////////////////////////////////////////////////
//
// If we have a request to update the files position we should
//
///////////////////////////////////////////////////////////////
if(isset($_POST['position'])) {
  $pid = $_POST['pid'];
  $pos = $_POST['position'];
  $fid = $_POST['fid'];
  $pcid = $_POST['pcid'];
  $checkuid = $db->getRow('playlist', array('id' => $pid), 'uid');
  if ($checkuid['0'] !== $uid) {
    echo 'Denied!<br/>';
  } else {
    reset($pos);
    reset($fid);
    reset($pcid);
    while(($val1 = each($pos)) && ($val2 = each($fid)) && ($val3 = each($pcid))) {
      $update = array('position' => $val1['value']);
      $cond = array('fid' => $val2['value'], 'pid' => $pid, 'id' => $val3['value']);
      $db->update('playlist_content', $update, $cond);			
    }
  }
}

/////////////////////////////////////////////////////////////
//
// Else display the normal form
//
/////////////////////////////////////////////////////////////

				
	?>
	
	<form method="post" enctype="multipart/form-data">
	<table border="0">
		<tr>
			<td>
				Edit playlist
			</td>
			<td>
			<?echo "<select name=\"playlists\">";
			$rows = $db->getRows('playlist', array('uid' => $uid), 'id, name', 'name', 'ASC');
			foreach($rows as $row) {
			  echo "<option value=\"".$row[0]."\">".$row[1]."</option>";
			}
			echo "</select>";
			?></td>
				<td valign="top">
					<input type="submit" name="edit" value="Go"></form>
				</td>
		</tr>
		<tr>
			<td valign="top">
				<form method="post" enctype="multipart/form-data">
				New playlist
			</td>
			<td valign="top">
				<input type="text" size="20" name="plname">
			</td>
				<td valign="top">
					<input type="submit" name="new" value="Add"></form>
				</td>
		</tr>
		</table>			
			<?
/////////////////////////////////////////////////////////////
//
// If we receive a request to edit a playlist
// execute the following
//
/////////////////////////////////////////////////////////////
		if($_POST['edit'] || isset($edit) || $_GET['action'] || isset($_GET['pid'])){
				   $i = "0";
					if (isset($_GET['page'])) { $page  = $_GET['page']; 
					} else { $page=0; };
					if(isset($_POST['playlists'])){
				   $pid = $_POST['playlists'];
				   }else{
					if(isset($edit)){
					 $pid = $edit;
					}
					if(isset($_GET['pid'])){
					$pid = $_GET['pid'];
					}
					if(isset($_GET['action'])){
					 $pid = $_GET['pid'];
					 $fid = $_GET['fid'];
					 $direction = $_GET['direction'];
					 $pos = $_GET['pos'];
					 $pcid = $_GET['pcid'];
					if($direction == "up"){
					 $newpos = $pos - 1;
					}else{
					 $newpos = $pos + 1;
					} 
					$db->update('playlist_content', array('position' => $newpos), array('pid' => $pid, 'fid' => $fid, 'id' => $pcid));
					$db->query('UPDATE playlist_content SET position="'.$pos.'" WHERE pid="'.$pid.'" AND fid!="'.$fid.'" AND id!="'.$pcid.'" AND position="'.$newpos.'"');

							}

						   }
						   
						$start_from = ($page) * 50;
						?>
						<table border="0" cellpadding="0" cellspacing="0">
						<form method="post" enctype="multipart/form-data">
								<tr>
									<td>Pos.</td>
									<td>Name</td>
									<td align="right">Remove</td>
								</tr>
						<?
						  $query = $db->query('SELECT playlist_content.fid,media.artist,media.song,playlist_content.position,playlist_content.id from playlist_content,media 	
							WHERE playlist_content.pid="'.$pid.'" AND playlist_content.fid=media.id ORDER BY playlist_content.position,media.song ASC LIMIT '.$start_from.', 50');
							while($row = mysql_fetch_row($query)){
							$bgcolor = ($i++ & 1) ? '#FFFFFF' : '#cfcfcf';
							echo "<tr bgcolor='$bgcolor'>\n";
							echo "<td valign=\"middle\"><input type=\"hidden\" name=\"fid[]\" value=\"".$row[0]."\">
							<a href=\"playlists.php?action=updatepos&fid=".$row[0]."&pid=".$pid."&direction=up&pos=".$row[3]."&pcid=".$row[4]."\"><img src=\"imgs/up.png\" border=0></a><a href=\"playlists.php?action=updatepos&fid=".$row[0]."&pid=".$pid."&direction=down&pos=".$row[3]."&pcid=".$row[4]."\"><img src=\"imgs/down.png\" border=0></a>
							<input type=\"text\" size=\"5\" value=\"".$row[3]."\" name=\"position[]\">	
							<input type=\"hidden\" name=\"pcid[]\" value=\"".$row[4]."\"></td>";
							echo "<td>&nbsp;&nbsp;".$row[1]."-".$row[2]."</td>";
							echo "<td align=\"right\"><input type=\"checkbox\" name=\"files[]\" value=\"".$row[0]."\"></td></tr>";
							}
							?>	
							<tr><td>
							<form method="post" enctype="multipart/form-data">
							<input type="hidden" name="pid" value="<? echo $pid;?>">
							<input type="submit" name="delete" value="Delete Playlist">
							</td><td></td>
							<td align="right">
                                                        <input type="hidden" name="pid" value="<? echo $pid;?>">
                                                        <input type="submit" name="update" value="Update Playlist"></form>
							</form>
							</td></tr></table><table border="0"><tr><td>
							<?
								echo "<a href=\"playlists.php?page=0&pid=".$pid."\">0</a> ";
								$sql = "SELECT COUNT(id) FROM playlist_content where pid=".$pid."";
								$rs_result = $db->query($sql);
								$row = mysql_fetch_row($rs_result);
								$total_records = $row[0];
								$total_pages = ceil($total_records / 100);
								for ($d=1; $d<=$total_pages; $d++) {
							        echo "<a href=\"playlists.php?page=".$d."&pid=".$pid."\">".$d."</a> ";
	}
				}
	
	
}
?>
