<?

/* This should be in a kind of prepend.php or something like it. */
$skindir="skins/"; // The URL to the skins (as: $docroot/"$skindir").
session_start();
if (!$skin) { $skin="default"; } // DEVEL ONLY!
if ($setskin) { $skin=$setskin; }
session_register("skin");
if (is_readable($htmldocroot."/".$skindir.$skin."/settings.php")) { include $skindir.$skin."/settings.php"; }


/* Database connection function. Voor het snellere tiepwerk :) */
function connectdb() {
	global $mysqluser,$mysqlpass, $mysqlhost, $mysqldatabase;
	$conn=mysql_connect($mysqlhost, $mysqluser, $mysqlpass);
	mysql_select_db($mysqldatabase, $conn);
	return $conn;
}

/* genre fetch */
function get_genre($type="formselect", $selected="", $selectname="songgenre", $extravalue=false, $onchange="") {

	global $conn;

	if (!isset($conn)) { $conn=connectdb(); }

	$query="select * from genres";
	$result=mysql_query($query, $conn);
	if ($extravalue && $type=="formselect") { $var.="<option value=\"\">$extravalue</option>\n"; }

	while ($row=mysql_fetch_array($result)) {
		if ($type=="formselect") {
			if ($selected==$row["id"]) { $sel=" selected"; } else { unset($sel); }
			$var.="<option value=\"".$row["id"]."\"$sel>".$row["description"]."</option>\n";
		} elseif ($type=="array") {
			$i=$row["id"];
			$var[$i]["description"]=$row["description"];
		}
	}

	if ($onchange) { $selectaction=" onChange='$onchange'"; }
	if ($type=="formselect") {
		$var="\n<!-- get_cat -->\n<select name=\"$selectname\"$selectaction>\n$var</select>\n<!-- get_cat -->\n";
	}
	return $var;

}

/* HTML header en footer. */
function html_header($title='') {
	global $skindir, $skin, $skininfo;
	if ($title=='') { $title="Webbases Music Jukebox"; }
	echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">".
		"<html>\n".
		"<head>\n".
		"  <title>".$title."</title>\n".
		"  <link rel=\"stylesheet\" href=\"".$skindir.$skin."/main.css\">\n".
		"</head>\n".
		"<body topmargin=\"0\" bottommargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" margin=\"0\" marginheight=\"0\" marginwidth=\"0\">\n".
		"<table width=\"100%\" border=\"0\" class=\"godtable\">\n".
  		"  <tr>\n".
    	"    <td height=\"38\" align=\"center\"><img src=\"".$skindir.$skin."/top-logo.jpg\"></td>\n".
		"  </tr>\n";
	if ($skininfo['menulocation']=='top') { navmenu(); }
	echo "  <tr>\n".
		"    <td>\n".
		"<!-- content cell -->\n";
}

function html_footer() {
	global $skindir, $skin, $skininfo;
	echo "<!-- /content cell -->\n    </td>\n  </tr>\n";
	if ($skininfo['menulocation']!='top') { navmenu(); }
	echo "</table>\n".
		"</body>\n</html>";
}

function navmenu() {
	global $skindir, $skin, $skininfo;
	echo "  <tr>\n    <td>".
		"<!-- navmenu table -->\n".
		"<table class=\"navmenu\" cellspacing=\"".$skininfo['cellspacing']['navmenu']."\">".// border=\"0\">\n".
		"  <tr class=\"navmenu\">\n". 
		"    <td class=\"navmenu\" align=\"right\"><img src=\"".$skindir.$skin."/navmenu-left.jpg\"></td>\n".
		"    <td class=\"navmenu\" width=\"60%\" nowrap>".
		"<a href=\"admin.php\"><img src=\"".$skindir.$skin."/menu-admin.jpg\" border=\"0\"></a><img src=\"".$skindir.$skin."/menu-sepperator.jpg\" border=\"0\">".
		"<a href=\"playlist.php\"><img src=\"".$skindir.$skin."/menu-playlist.jpg\" border=\"0\"></a><img src=\"".$skindir.$skin."/menu-sepperator.jpg\" border=\"0\">".
		"<a href=\"filechecker.php\"><img src=\"".$skindir.$skin."/menu-addsongs.jpg\" border=\"0\"></a></a><img src=\"".$skindir.$skin."/menu-sepperator.jpg\" border=\"0\">".
		"<a href=\"index.php\"><img src=\"".$skindir.$skin."/menu-index.jpg\" border=\"0\"></a><img src=\"".$skindir.$skin."/menu-sepperator.jpg\" border=\"0\"></td>\n".
		"    <td class=\"navmenu\" width=\"40%\" align=\"right\"><a href=\"http://www.marell.net\"><img src=\"".$skindir.$skin."/menu-copylefts.jpg\" border=\"0\"></a></td>\n".
		"    <td class=\"navmenu\" align=\"left\"><img src=\"".$skindir.$skin."/navmenu-right.jpg\"></td>\n".
		"  </tr>\n".
		"</table>\n".
		"<!-- /navmenu table -->\n".
		"    </td>\n".
		"  </tr>\n";
}

/*
Begin en eind van de hoofdtabel. Zit het menu bij in.
*/
function start_maintable() {
    global $skindir, $skin, $skininfo, $errormes, $noticemes, $PHP_SELF;

	echo "
		<!-- main table -->
        <table class=\"maintable\" cellspacing=\"".$skininfo['cellspacing']['maintable']."\" align=\"center\">
          <tr>
            <td class=\"maintable\"><img src=\"".$skindir.$skin."/table-topleft.jpg\"></td>
            <td class=\"maintable\" width=\"99%\" background=\"".$skindir.$skin."/table-top.jpg\"><img src=\"".$skindir.$skin."/table-top.jpg\"></td>
            <td class=\"maintable\"><img src=\"".$skindir.$skin."/table-topright.jpg\"></td>
          </tr>
          <tr>
            <td class=\"maintable\" background=\"".$skindir.$skin."/table-centerleft.jpg\"><img src=\"".$skindir.$skin."/table-centerleft.jpg\"></td>
	        <td class=\"maintable\" align=\"center\">
	";

    if ($errormes) { echo "<p align=\"center\"><b>ERROR:<br>\n".nl2br($errormes)."</b></p>"; }
    if ($noticemes) { echo "<p align=\"center\"><b>Attention:</b><br>\n".nl2br($noticemes)."</p>"; }
}

function end_maintable() {
	global $skindir, $skin;
	echo "
            </td>
            <td class=\"maintable\" background=\"".$skindir.$skin."/table-centerright.jpg\"><img src=\"".$skindir.$skin."/table-centerright.jpg\"></td>
          </tr>
          <tr>
            <td class=\"maintable\"><img src=\"".$skindir.$skin."/table-bottomleft.jpg\"></td>
            <td class=\"maintable\" background=\"".$skindir.$skin."/table-bottom.jpg\"><img src=\"".$skindir.$skin."/table-bottom.jpg\"></td>
            <td class=\"maintable\"><img src=\"".$skindir.$skin."/table-bottomright.jpg\"></td>
          </tr>
        </table>
		<!-- /main table -->
	";
}

/* Ftp connectie maken om de file uploaden. */
/* NOT YET USED */
function ftpstore ($origfile, $destfile) {
	global $ftpconn, $ftpuser, $ftppass, $errormes, $noticemes;
	if ($origfile=='none') { 
		$errormes.="Het te uploaden plaatje is waarschijnlijk qua bestandsomvang te groot.";
		return false;
	}
	if (!$ftpconn) {
		$ftpconn = ftp_connect('localhost');
		$ftplogin = ftp_login($ftpconn, $ftpuser, $ftppass);
		if (!$ftpconn || !$ftplogin) {
	    	$errormes.="De verbinding met de ftp-server 'localhost' met gebruikersnaam ".$ftpuser." kan ik niet maken...\n";
			return false;
		}
	}

    $upload = ftp_put($ftpconn, $destfile, $origfile, FTP_BINARY);
   	if (!$upload) { 
		$errormes.="Het uploaden van bestand $origfile naar $destfile is niet gelukt...\n"; 
		return false;
	} else {
		$noticemes.="Het uploaden lijkt mij ok.\n";
		return true;
	}

}

/* PID functions */
function writepid($pid) {
	if (is_readable("/tmp/wmj.pid")) { return false; }
	else
		$fp=fopen("/tmp/wmj.pid",'w');
		fwrite($fp, $pid);
		fclose($fp);
		return $pid;
}
function getpid() {
	global $tempdir;
	if (is_readable($tempdir."/wmj.pid")) {
		$fp=fopen($tempdir."/wmj.pid",'r');
		$pid=fread($fp, filesize($tempdir."/wmj.pid"));
		fclose($fp);
		return $pid;
	} 
	return false;
}

/* START, SKIP 'n STOP functions */
function startthebox($i=0) {
	global $htmldocroot, $tempdir;
	$i++;
	if ($i>2) { echo "Cannot start the box!"; die(); }
	if (!getpid()) {
		exec ("php $htmldocroot/shell.php > /dev/null &");
	} else {
		killthebox();
		startthebox($i);
	}
}
function skipthebox() {
	exec ("killall amp");
}
function killthebox() {
	global $tempdir;
	if (getpid()) {
		exec ("kill ".getpid());
		exec ("killall amp");
		unlink ($tempdir."/wmj.pid");
	}
}

function get_playlist($return='array') {

	global $connection;
	if (!$connection) { $connection=connectdb(); }

	$querypl="select * from playlist";
	$resultpl=mysql_query($querypl, $connection);
	while ($rowpl=mysql_fetch_array($resultpl)) {
		if ($return=='array') {
			$arr[]=$rowpl[filename];
		} elseif ($return='table') {
			$arr.="<tr><td>$row[filename]</td></tr>\n";
		}
	}
	if ($return=='table') {
		return "<table>$arr</table>";
	} elseif($return=='array') {
		return $arr;
	}
}


function zoektabel ($query) {

	global $connection, $skindir, $skin, $skininfo;
	$playarray=get_playlist();
	
	$result=mysql_query($query, $connection);
	echo	"<table class=\"searchlist\" cellspacing=\"".$skininfo['cellspacing']['searchlist']."\">\n".
			"  <tr>\n".
			"    <th width=\"20%\" class=\"searchlist\">Artist</th>\n".
			"    <th width=\"20%\" class=\"searchlist\">Song</th>\n".
			"    <th width=\"20%\" class=\"searchlist\">Album</th>\n".
			"    <th width=\"20%\" class=\"searchlist\">Bitrate</th>\n".
			"    <th width=\"20%\" class=\"searchlist\">Add to playlist</th>\n".
			"    <th width=\"20%\" class=\"searchlist\">Edit file info</th>\n".
			"  </tr>\n";

	while ($row=mysql_fetch_array($result)){

		if (!$row[album]){$album="unknown album";} else { $album=($row[album]);}
		if (!$row[title]){$title="unknown title";} else { $title=($row[title]);}
		if (!$row[artist]){$artist="unknown artist";} else { $artist=($row[artist]);}
		if (!$row[bitrate]){$bitrate="unknown bitrate";} else { $bitrate=($row[bitrate]);}

		echo "  <tr>\n". 
			"    <td class=\"searchlist\">".stripslashes($artist)."</td>\n".
			"    <td class=\"searchlist\">".stripslashes($title)."</td>\n".
			"    <td class=\"searchlist\">".stripslashes($album)."</td>\n".
			"    <td class=\"searchlist\">".$bitrate."</td>\n";

		if (@in_array($row[filename], $playarray)){
			echo "    <td class=\"searchlist\"><font color=\"red\">Already in the playlist</font></td>\n";
		} else {
			echo "    <td class=\"searchlist\"><input type=\"checkbox\" name=\"add_$row[id]\" value=\"checkbox\"><br></a></td>\n";
		}

		echo "    <td class=\"searchlist\"><a href=edit.php?id=$row[id]><img src=\"".$skindir.$skin."/button-editfileinfo.jpg\" border=\"0\"></td>\n";

	}

	echo	"  <tr>\n".
			"    <td class=\"searchlist\" colspan=\"6\" align=\"center\"><br><input type=\"submit\" name=\"playlistadd\" value=\"Add to the playlist\"><br><br></td>".
			"  </tr>\n".
	    	"</table>\n";
}

?>