<?php
require_once "../../maincore.php";

require_once "infusion_db.php";
@include INFUSIONS."sw_streamstatus".$sw_streamstatus_number."_panel/listen/streamconfig.php";
@include INFUSIONS.str_replace('/infusions/','',$sp_url)."/infusion_db.php";
@include(INFUSIONS.str_replace('/infusions/','',$sp_url)."/sp_functions.php");

// Load default settings
$ssp_setting[$sw_streamstatus_number] = @dbarray(dbquery("SELECT * FROM `".$streamstatus_db[$sw_streamstatus_number]['table_settings']."` WHERE `profile` = 'default' LIMIT 1"));

// convert current timestamp using global offset settings
$timestamp = (time() + (3600*$settings['timeoffset']));
if(IsSet($settings['serveroffset'])) { $timestamp += (3600*$settings['serveroffset']); }
// IMPORTANT: generate dateformat in gmt for db tables (without MESZ changes!)
$requestday = gmmktime(12,0,0,date("m",$timestamp),date("d",$timestamp),date("Y",$timestamp));
require_once $sp_install_folder."sp_functions.php";

$sp_setting = dbarray(dbquery("SELECT * FROM `".DB_PREFIX."sendeplan".($ssp_setting[$sw_streamstatus_number]['sp_number']>0 ? $ssp_setting[$sw_streamstatus_number]['sp_number'] : "")."_settings` WHERE `profile` = 'default'"));
if(!iSUPERADMIN && !checkgroup($sp_setting['dj_admingroup']) && !checkgroup($sp_setting['dj_group'])) { redirect(BASEDIR."index.php"); }
$result = dbquery("SELECT * FROM `".DB_PREFIX."sendeplan".($ssp_setting[$sw_streamstatus_number]['sp_number']>0 ? $ssp_setting[$sw_streamstatus_number]['sp_number'] : "")."` WHERE `day` = ".$requestday);
$rows = dbrows($result);
if ($rows != 0) {
	$entry = dbarray($result);
	$dj_current_id = explode(".",$entry[date("G",$timestamp)]);
	if(date("G",$timestamp)<23) {
		$dj_next_id = explode(".",$entry[(date("G",$timestamp)+1)]);
	}
	if(date("G",$timestamp)>0) {
		$dj_gone_id = explode(".",$entry[(date("G",$timestamp)-1)]);
	}
}

if(IsSet($_POST['songupdate']) && $_POST['song']!="" && (iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || (checkgroup($sp_setting['dj_group'] && ($userdata['user_id'] == $dj_current_id[0] || (IsSet($dj_next_id[0]) && $userdata['user_id'] == $dj_next_id[0]) || (IsSet($dj_gone_id[0]) && $userdata['user_id'] == $dj_gone_id[0])))))) {
	for($i=1;$i<=3;$i++) {
		$song = stripslashes($_POST['song']);
		if($ssp_setting[$sw_streamstatus_number]['enable_songupdate_'.$i]=='1') {
			$uri  = "/admin.cgi?".(IsSet($sc_id[$i]) && $sc_id[$i]>=1 ? "sid=".$sc_id[$i]."&" : "")."mode=updinfo&song=".stripslashes(rawurlencode("$song"));
			$sock = @fsockopen($sc_server[$i], $sc_port[$i], $errno, $errstr, 2);
			if($sock && $errno == 0) {
				fputs ($sock, "GET ".$uri." HTTP/1.1\r\n");
				fputs ($sock, "Host: ".$sc_server[$i].":".$sc_port[$i]."\r\n");
				fputs ($sock, "User-Agent: XML Getter (Mozilla Compatible)\r\n");
				fputs ($sock, "Authorization: Basic ".base64_encode("admin:".$sc_adminpw[$i])."\r\n");
				fputs ($sock, "Connection: close\r\n\r\n");
				@fclose($sock);
			}
		}
  		
	}
	redirect(FUSION_SELF);
}

elseif(IsSet($_GET['do']) && $_GET['do']=="kick" && IsSet($_GET['stream']) && IsNum($_GET['stream']) && (iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || (checkgroup($sp_setting['dj_group'] && ($userdata['user_id'] == $dj_current_id[0] || (IsSet($dj_next_id[0]) && $userdata['user_id'] == $dj_next_id[0]) || (IsSet($dj_gone_id[0]) && $userdata['user_id'] == $dj_gone_id[0])))))) {
	if($ssp_setting[$sw_streamstatus_number]['enable_songupdate_'.$_GET['stream']]=='1') {
		$uri  = "/admin.cgi?".(IsSet($sc_id[$_GET['stream']]) && $sc_id[$_GET['stream']]>=1 ? "sid=".$sc_id[$_GET['stream']]."&" : "")."mode=kicksrc";
		$sock = @fsockopen($sc_server[$_GET['stream']], $sc_port[$_GET['stream']], $errno, $errstr, 2);
		if($sock && $errno == 0) {
			fputs ($sock, "GET ".$uri." HTTP/1.1\r\n");
			fputs ($sock, "Host: ".$sc_server[$_GET['stream']].":".$sc_port[$_GET['stream']]."\r\n");
			fputs ($sock, "User-Agent: XML Getter (Mozilla Compatible)\r\n");
			fputs ($sock, "Authorization: Basic ".base64_encode("admin:".$sc_adminpw[$_GET['stream']])."\r\n");
			fputs ($sock, "Connection: close\r\n\r\n");
			@fclose($sock);
		}
	redirect(FUSION_SELF);
	}
}

?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='de' lang='de'>
<head>
<title>DJ-Toolbox</title>
<link rel='stylesheet' href='<?php echo THEME; ?>styles.css' type='text/css' media='screen' />
<script type='text/javascript'>
function ConfirmKick(content) {
	return confirm("Willst du wirklich folgenden Stream kicken:\n'" + content + "' ?\nNach Klick auf OK erfolgt dieser sofort!\nFalls du das nicht willst klicke auf ABBRECHEN.");
}
</script>
</head>
<body>
<?php

echo "<div id='content' class='tbl-border'>\n";
echo "<div id='content' class='tbl2'>\n";
echo "<h3 align='center'>DJ Toolbox</h3><hr />\n";
$sk_enabled = false;
$su_enabled = false;
echo "<h4 align='center'>Streamkick:</strong></h4>\n";
if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || (checkgroup($sp_setting['dj_group']) && ($userdata['user_id'] == $dj_current_id[0] || (IsSet($dj_next_id[0]) && $userdata['user_id'] == $dj_next_id[0]) || (IsSet($dj_next_id[0]) && $userdata['user_id'] == $dj_next_id[0]) || (IsSet($dj_gone_id[0]) && $userdata['user_id'] == $dj_gone_id[0])))) {
	for($i=1;$i<=3;$i++) {
		if($ssp_setting[$sw_streamstatus_number]['enable_streamkick_'.$i]=='1') {
			$sk_enabled = true;
			echo "<center><a href='?do=kick&amp;stream=".$i."' class='button' target='_self' title='Stream ".$i." kicken' style='text-decoration: none;' onclick='return ConfirmKick(\"".$ssp_setting[$sw_streamstatus_number]['sc_name_'.$i]."\");'>Kicken: ".$ssp_setting[$sw_streamstatus_number]['sc_name_'.$i]."</a></center>\n";
		}
	}
	if(!$sk_enabled) { echo "<div align='center' class='alt' style='padding: 7px;'><em>Streamkick ist f&uuml;r keinen vorhandenen Stream freigegeben.</em></div>\n"; }
	echo "
	<h4 align='center'>Manuelles Titel-Update:</strong></h4>
	<div align='center'>
	";
	/*
	 Die Titelanzeige ist unabhaengig von der Panel-Voreinstellung hier inkl. der akt. Hoererzahl, da dies relevant fuer den akt. DJ ist.
	 Ausserdem ist die Refreshzeit mit 5 Sek. sehr niedrig, um Titelupdate zeitnah anzuzeigen.
	*/
	echo "<script type='text/javascript' src='sc_info_ajax.php?hoerer=1'></script>
	<script type='text/javascript'>
		loadSC".$sp_id."Data();
		setInterval('loadSC".$sp_id."Data()',10000);
	</script>
	<br />
	<small>Aktueller Titel:</small><br />
	<span id='sc".$sp_id."_info' style='padding: 0px; font-size: 12px; font-family: verdana, arial; font-weight: normal;'><em>Stream-Daten werden geladen, bitte warten...</em></span>
	
	";
	for($i=1;$i<=3;$i++) {
		if($ssp_setting[$sw_streamstatus_number]['enable_songupdate_'.$i]=='1') {
			$su_enabled = true;
		}
	}
	if($su_enabled) {
		echo "<br />
		<form name='songupdate' method='post' style='padding-top: 10px;'>
			Songtitel manuell updaten:<br />
			<input type='text' class='textbox' name='song' value='' style='width: 280px' /><input type='submit' class='button' name='songupdate' style='width: 26px;' value='OK' />
		</form>
		";
	}
	else { echo "<div align='center' class='alt' style='padding: 7px;'><em><small>Das Songupdate-Feature wurde vom Administrator deaktiviert.</small></em></div>\n"; }
}
else echo "<span class='alt' style='padding: 7px;'>Streamkick und Songupdate sind nur f&uuml;r DJs erlaubt, welche aktuell oder in K&uuml;rze senden (gem&auml;&szlig; Sendeplan)!</span>\n";



echo "</div>
<br /><br />
<div align='center' style='float: left; text-align: center; width: 100%; font-size: 9px;'>Streamstatus &amp; DJ-Toolbox &copy; by <a href='https://www.systemweb.de' target='_blank'>Systemweb.de</a></div>
<br /></div>
</div>
";

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