<?
////////////////////////////////////////////////////////////////////////
/*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.*/
////////////////////////////////////////////////////////////////////////
include('header.php');
$db = dbConnect();
$config = settings();


	///////////////////////////////////////////////////////////////
        /*
	If the user has submitted files to be added to a playlist process them
	1) Get the max ID on the currect playlist
	2) Insert files above that max position, loop over query until completed
        */
        ////////////////////////////////////////////////////////////////
	if(isset($_POST['mediaupdate'])){
	$pid = $_POST['playlists'];
	$files = $_POST['files'];
		foreach($files as $media){
			$p = $db->getRow('SELECT MAX(position) FROM playlist_content WHERE pid="'.$pid.'"');
			if($p[0] == ""){
			$pos = "1";
			}else{
			$pos = $p[0] + 1;
			}
			$db->insert('playlist_content', array ('fid' => $media, 'pid' => $pid, 'position' => $pos));
		}

	// Rebuild the playlist
	rebuildPlaylist($pid);
	echo "Files added!";
	
	
	}
?>
<table border="0">
<tr>
	<td valign="top">
<form method="get" enctype="multipart/form-data"><input type="submit" name="reload" value="Reload Media"></form> 
	</td><td valign="top">
	<?echo "<a href=\"#\" onClick=\"window.open('playlists.php','Playlist Management','resizable,height=600,width=800,scrollbars=yes,toolbar=no'); return false;\">Manage Playlists</a><br>";
	?></td>
</tr>
</table>
<?
	if(isset($_GET['reload'])){
	reloadmedia($config['media_path']);
	echo "Media list reloaded.";
	}
	
        if(isset($_GET['sortfield'])){
        $sortfield = $_GET['sortfield'];
        } else {
        $sortfield = 'Artist';
        }

	?>
	<table border="0" width="80%">
		<tr><td> 
	<a href="media.php?page=%">All</a>&nbsp;|&nbsp;
	<a href="media.php?page=a">A</a>&nbsp;
	<a href="media.php?page=b">B</a>&nbsp;
	<a href="media.php?page=c">C</a>&nbsp;
        <a href="media.php?page=d">D</a>&nbsp;
        <a href="media.php?page=e">E</a>&nbsp;
        <a href="media.php?page=f">F</a>&nbsp;
        <a href="media.php?page=g">G</a>&nbsp;
        <a href="media.php?page=h">H</a>&nbsp;
        <a href="media.php?page=i">I</a>&nbsp;
        <a href="media.php?page=j">J</a>&nbsp;
        <a href="media.php?page=k">K</a>&nbsp;
        <a href="media.php?page=l">L</a>&nbsp;
        <a href="media.php?page=m">M</a>&nbsp;
        <a href="media.php?page=n">N</a>&nbsp;
        <a href="media.php?page=o">O</a>&nbsp;
        <a href="media.php?page=p">P</a>&nbsp;
        <a href="media.php?page=q">Q</a>&nbsp;
        <a href="media.php?page=r">R</a>&nbsp;
        <a href="media.php?page=s">S</a>&nbsp;
        <a href="media.php?page=t">T</a>&nbsp;
        <a href="media.php?page=u">U</a>&nbsp;
        <a href="media.php?page=v">V</a>&nbsp;
        <a href="media.php?page=w">W</a>&nbsp;
        <a href="media.php?page=x">X</a>&nbsp;
        <a href="media.php?page=y">Y</a>&nbsp;
        <a href="media.php?page=z">Z</a>&nbsp;
		</td></tr>
	</table>
	<?
	if(isset($_GET['page'])){
        $letter = $_GET['page'];
        }else{
        $letter = '%';
        }
	?>
	<table id="rowclick" border="0" cellspacing="0" class="medialist" width="80%">
		<tr>
			<td>
			<strong><a href="media.php?page=<? echo $letter; ?>&sortfield=Artist">Artist</a></strong>
			</td><td>
			<strong><a href="media.php?page=<? echo $letter; ?>&sortfield=Album">Album</a></strong>
			</td><td>
			<strong><a href="media.php?page=<? echo $letter; ?>&sortfield=Song">Song</a></strong>
			</td><td align="left">
			<strong><a href="media.php?page=<? echo $letter; ?>&sortfield=Genre">Genre</a></strong>
			</td>
			<?if(isset($_GET['check'])){?>
			</td><td align="right">Add(<a class="tablebarlink" href="media.php?page=<? echo $letter;?>">Uncheck all</a>)</td></tr>
			<tr>
			<td class="tablebar" align="center"><? if ($config['flashplayer'] == "on") { ?><strong>Play</strong><? } ?></td>
			<?}else{?>
        		</td><td align="right">Add(<a href="media.php?page=<? echo $letter;?>&check=all">Check all</a>)</td></tr>
			<?}?>
			
		<form method="post" enctype="multipart/form-data">
	<?

	///////////////////////////////////////////////////////////////
	/*
	Set our default query, and alter the query so that we can sort the files
	as per the users request
	*/
	////////////////////////////////////////////////////////////////
	$i = "0";
	$sql = 'SELECT media.id,media.files,media.artist,media.album,media.song,media.genre,genres.genreTitle FROM media, genres ';
	$sql .= 'WHERE media.genre = genres.genreID AND Artist LIKE "'.$letter.'%" ORDER BY ';
	if ($sortfield == 'Artist') { $sql .= 'media.artist,media.album,media.song, genres.genreTitle'; }
	else if ($sortfield == 'Album') { $sql .= 'media.album, media.song, genres.genreTitle, media.artist'; }
	else if ($sortfield == 'Song') { $sql .= 'media.song, genres.genreTitle, media.artist, media.album'; }
	else { $sql .= "genres.genreTitle, media.artist, media.album, media.song"; }
	$query = $db->query($sql);
	 if(mysql_num_rows($query) < 1){
		if ($letter == '%') { echo "</table>No media files.<br><br>"; }
		else { echo "</table>No files available where Artist begins with `$letter`<br><br>"; }
         } else {
		while($row = mysql_fetch_row($query)){
		$bgcolor = ($i++ & 1) ? '#FFFFFF' : '#cfcfcf';
	

			///////////////////////////////////////////////////////////////
		        /*
		        If the flash MP3 player is enabled in our config file display
			the flash player and set config appropriatly.
		        */
		        ////////////////////////////////////////////////////////////////
			if ($config['flashplayer'] == "on") {
			 // Extract mp3 file name from path
			 $mp3path = explode('/',$row[1]);
			 $mp3file = $config['media_url'].rawurlencode($mp3path[sizeof($mp3path) - 1]);
 			 if (file_exists($config['media_path']."/".$mp3path[sizeof($mp3path) - 1])) {
   			   // Embed Flash player object
	       		   $mp3player = '<object type="application/x-shockwave-flash" data="include/player_mp3_maxi.swf" width="100" height="20">';
			   $mp3player .= '<param name="movie" value="include/player_mp3_maxi.swf" />';
			   // Parameters. For full list, check: http://flash-mp3-player.net/players/maxi/
			   // TODO: Include settings file for easier customization
			   $mp3player .= '<param name="FlashVars" value="mp3='.$mp3file.'&amp;';
			   $mp3player .= 'width=100&amp;autoload=0&amp;showvolume=1&amp;showloading=always&amp;sliderwidth=5&amp;volumewidth=25" />';
	   		   $mp3player .= '<param name="showslider" value="0"></object>';
   			 } else {
 			   $mp3player = '<strong>Invalid URL. Check AutoDJ settings/media.</strong>';
			 }
			 echo "<tr bgcolor='$bgcolor'>\n";
			 echo "<td>".$row[2]."</td><td>".$row[3]."</td><td>".$row[4]."</td><td>".$row[6]."</td><td align=\"center\">".$mp3player."</td>";

			///////////////////////////////////////////////////////////////
		        /*
		        If the flash player is not enabled do not show it and do not 
			set the extra table column where it would be.
		        */
		        ////////////////////////////////////////////////////////////////
			 }else{
			 echo "<tr bgcolor='$bgcolor'>\n";
                	 echo "<td>".$row[2]."</td><td>".$row[3]."</td><td>".$row[4]."</td><td>".$row[6]."</td>";
		}


		///////////////////////////////////////////////////////////////
	        /*
        	Check what our check box settings are and configure the boxes
		to display checked or not checked.
       		*/
	        ////////////////////////////////////////////////////////////////
		if (isset($_GET['check'])){
		echo "<td align=\"right\"><input type=\"checkbox\" name=\"files[]\" value=\"".$row[0]."\" CHECKED></td>";
	 } else {
		echo "<td align=\"right\"><input type=\"checkbox\" name=\"files[]\" value=\"".$row[0]."\"></td></tr>";
	 }
	}
	echo "</table>";
	}
	
	///////////////////////////////////////////////////////////////
        /*
        Drop down list to select what playlist to add the fiels to.
        */
        ////////////////////////////////////////////////////////////////
	?><table border="0" width="80%"><tr><td align="right"><?
	echo "Add to playlist: <select name=\"playlists\">";
	$rows = $db->getRows('playlist','','id, name', 'name', 'ASC');
	foreach ($rows as $row) {
		echo "<option value=\"".$row[0]."\">".$row[1]."</option>";
		}
	echo "</select>";
	echo "&nbsp;<input type=\"submit\" name=\"mediaupdate\" value=\"Add\"></form>";
	echo "</td></tr></table><br><br>";
	
?>
