<?php
require_once("../../maincore.php");
header("Expires: Sat, 05 Nov 2005 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

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

if(IsSet($sp_id) && strlen($sp_id) == 1 && IsNum($sp_id)) {
	$id = ($sp_id*1);
}
if(!IsSet($sp_id) || $sp_id=='0') { $id = ""; }

$sendeplan_setting = dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_settings']."` WHERE `profile` = 'default'"));

// Check if locale file is available matching the current site locale setting.
if (file_exists($sp_install_folder."locale/".$settings['locale'].".php")) {
	// Load the locale file matching the current site locale setting.
	include $sp_install_folder."locale/".$settings['locale'].".php";
} else {
	// Load the infusion's default locale file.
	include $sp_install_folder."locale/English.php";
}

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

$newday = $requestday;
for ($s=0;$s<=14;$s++) {	
  	if(!tblexists_day($newday+($s*(3600*24)))) create_day($newday+($s*(3600*24)));
}

// request available entries and store them in array
$result = dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` WHERE `day` = ".$newday);
$rows = dbrows($result);
if ($rows != 0) {
	$entry = dbarray($result);
	$showstart = array();
	$showend = array();
	$showid = array();
	$djid = array();
	$current_show = "-";
	
	for($i=0; $i<=23; $i++) {
		if($i==0 && $entry[$i]=="") {
			// no entry = autostream
			$djid[] = 0;
			$showid[] = 1;
			$current_show = "";
			$showstart[] = $i;
			$showend[] = $i;
		}
		if($entry[$i]!=$current_show) { 
			if($entry[$i]!="") {
				$showdata = explode(".",$entry[$i]);
				$djid[] = $showdata[0];
				$showid[] = $showdata[1];
				$current_show = $entry[$i];$showstart[] = $i;
			}
			else {
				// no entry = autostream
				$djid[] = 0;
				$showid[] = 1;
				$current_show = "";
				$showstart[] = $i;
			}						
		}					
	}
	
	$showstart[] = 0;
	for($i=0; $i<(count($showstart)-1); $i++) {
		//$next=$i+1;
		if($showstart[$i]<=date("G",$timestamp) && ($showstart[$i+1]>date("G",$timestamp) || $showstart[$i+1]==0)) {
			
			if($djid[$i]==0) { $showid[$i] = 1; }
				$result = dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".$showid[$i]."'");
				$rows = dbrows($result);
				$z=0;
				if ($rows != 0) {
					$show = dbarray($result);
					$result2 = dbquery("SELECT * FROM `".$sendeplan_db['table_showdata']."` WHERE `ID` = '".$show['showid']."'");
					$rows = dbrows($result2);
					if ($rows != 0) {
						$showdata = dbarray($result2);
						$z++;
						// Dj ermitteln, der WIRKLICH online ist (Sendeplan ignorieren)
						$realonline = false; unset($realdj);
						if(file_exists(INFUSIONS."sw_streamstatus1_panel/realdj_info.php")) {						
							$realonline = stripinput(@file_get_contents($settings['site_protocol']."://".$settings['site_host']."/infusions/sw_streamstatus1_panel/realdj_info.php"));							
						}
						if($realonline) {
							if(IsNum($realonline) && ($realonline*1) == $realonline) { $result = dbquery("SELECT `user_id` FROM `".DB_USERS."` WHERE `user_id` = '".$realonline."' LIMIT 1"); }
							else $result = dbquery("SELECT `user_id` FROM `".DB_USERS."` WHERE `user_name` LIKE '".$realonline."' LIMIT 1");
							$rows = dbrows($result);
							if ($rows != 0) {
								$realdj = dbarray($result);
								$djid[$i] = $realdj['user_id'];
							}
						}
						// Ende Dj ermitteln
						$sql_q = "SELECT * FROM `".DB_USERS."` WHERE `user_id` = '".$djid[$i]."' LIMIT 1"; 
						$result3 = dbquery($sql_q);
						$rows = dbrows($result3);
						if ($rows != 0) { $djdata = dbarray($result3); }
						else { 
							$djdata['user_name'] = $sendeplan_setting['autodj_name'];
							$show['showid'] = 1;
						}									
						$result4 = dbquery("SELECT * FROM `".$sendeplan_db['table_djinfos']."` WHERE `user_id` = '".$djid[$i]."'");
						$rows = dbrows($result4);
						if ($rows != 0) { $djinfo = dbarray($result4); }
					}
				}
				echo "<table width='100%' align='center' border='0'><tr>\n";
				echo "<td width='1' align='center'>
				
				<div style='position: relative; float: left; top: 0px; left: 0px; height: 125px; width: 120px; padding: 0px; border: 0px none;'>";
				if(IsSet($djinfo['user_avatar']) && $djinfo['user_avatar']!="" && file_exists(INFUSIONS.$sp_url."/images/dj-avatars/".$djinfo['user_avatar'])) {
					echo ($djinfo['user_id']!='0' ? "<a href='".$settings['siteurl']."profile.php?lookup=".$djinfo['user_id']."' title='Profil'><img src='".$settings['siteurl']."infusions/".$sp_url."/images/dj-avatars/".$djinfo['user_avatar']."' alt='now on air' style='max-width: 120px; max-height: 120px; border: 0px none;' /></a>" : "<img src='".$settings['siteurl']."infusions/".$sp_url."/images/dj-avatars/".$djinfo['user_avatar']."' alt='now on air' style='max-width: 120px; max-height: 120px; border: 0px none;' />");
				}
				else {
					echo "<img src='".$settings['siteurl']."infusions/".$sp_url."/images/dj-avatars/nopic.png' alt='now on air' style='max-width: 120px; max-height: 120px; border: 0px none;' />";
				}
				echo "<br /><small>".(IsSet($djinfo) && $djid[$i]!=0 ? "<a href='".$settings['siteurl']."profile.php?lookup=".$djinfo['user_id']."' title='Benutzerprofil anzeigen'><strong>".$djdata['user_name']."</strong></a>" : $sendeplan_setting['autodj_name'])."</small>\n";
				// Link fuers Team, nur wenn Superadmin, Sendeplan-Admin oder DJ:
				if(IsSet($_GET['toolbox']) && $_GET['toolbox']=='yes') {
					echo "<br /><small>[<a href='javascript:;' onclick='popup=window.open(\"http://".$_SERVER['HTTP_HOST'].$ss_url."/dj_toolbox.php\",\"DJTools\",\"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=350,left=50,top=50\"); return false;' title='zur DJ-Toolbox' target='_blank'>DJ-Toolbox</a>]</small>";
				}
				echo "
				</div></td>\n";
				echo "<td align='left' valign='top'>
				<small>Aktuell bis ".($showstart[$i+1]<10 ? "0" : "").$showstart[$i+1]." Uhr:</small><br /><a href='".$settings['siteurl']."infusions/".$sp_url."/sendeplan.php#onair' title='' onmouseover='Tip(\"<div style=padding:10px;width:".($sendeplan_setting['showbox_width']-20)."px;height:".($sendeplan_setting['showbox_height']-20)."px;background-image:url(".$settings['siteurl']."infusions/".$sp_url."/images/shows/".$showdata['image'].");background-repeat:no-repeat;background-position:center;background-color:transparent;color:".$showdata['font-color'].";><div style=margin:0px;padding:5px;padding-top:3px;".($showdata['textbg']!="" && file_exists(INFUSIONS.$sp_url."/images/bg_trans/".$showdata['textbg']) ? "background-image:url(".$settings['siteurl']."infusions/".$sp_url."/images/bg_trans/".$showdata['textbg'].");" : "")."border:0px;height:".($sendeplan_setting['showbox_height']-30)."px;overflow:hidden;><div align=".$sendeplan_setting['align_showtitle']." class=spshowtitle style=color:".$showdata['font-color'].";><table width=100% border=0 align=left cellpadding=2 cellspacing=0 class=spshowtitle style=color:".$showdata['font-color'].";><tr><td><strong>".($showstart[$i]<10 ? "0" : "").$showstart[$i].":00".($sendeplan_setting['view_endtime']=="1" ? "-".($showstart[$i+1]<10 ? "0" : "").$showstart[$i+1].":00" : "")."</strong></td>".($sendeplan_setting['use_nsv']==true && $show['is_nsv']==false ? "<td width=1><img src=".$settings['siteurl']."infusions/".$sp_url."/images/camera_off.png width=25 height=25 border=0 alt=Cam_off! /></td>" : "").($sendeplan_setting['use_nsv']==true && $show['is_nsv']==true ? "<td width=1><img src=".$settings['siteurl']."infusions/".$sp_url."/images/camera_on.png width=25 height=25 border=0 alt=Cam_on! /></td>" : "")."</tr></table>".string2js($show['title'])."</div><div align=".$sendeplan_setting['align_showdescription']." class=spshowdescription style=color:".$showdata['font-color'].";>".str_replace("\r\n","",nl2br(string2js($show['description'])))."</div></div></div>\");'><strong>".htmlentities(utf8_cleantext(html_entity_decode($show['title'])))."</strong></a><br />";
				
				// Studiobox anzeigen?
				if($sb_url!="") {
					echo "<div style='height: auto; border: 0px none; margin-top: 8px; margin-bottom: 14px;'>\n";
					if($show['studiobox']==0) {
						if(IsSet($_GET['toolbox']) && $_GET['toolbox']=='yes') {
							echo "[<a href='javascript:;' onclick='popup=window.open(\"".$settings['siteurl']."infusions/".$sb_url."/studiobox_dj.php\",\"Studiobox\",\"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=500,left=50,top=50\"); return false;' title='Studiobox' target='_blank'>DJ: Studiobox aktuell inaktiv</a>]";
						}
						else {
							echo "<small><em>(Sendung ohne Gr&uuml;&szlig;e / W&uuml;nsche)</em></small>\n";
						}
					}
					elseif($show['studiobox']=='1') {
						echo "<a href='javascript:;' class='button' style='width: auto; padding: 3px; text-decoration:none; font-weight: bold;' onclick='popup=window.open(\"".$settings['siteurl']."infusions/".$sb_url."/studiobox_popup.php\?studio=".$id."\",\"Studiobox\",\"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=500,left=50,top=50\"); return false;' title='Studiobox' target='_blank'>Gru&szlig; ins Studio</a>\n";
					}
					elseif($show['studiobox']=='2') {
						echo "<a href='javascript:;' class='button' style='width: auto; padding: 3px; text-decoration:none; font-weight: bold;' onclick='popup=window.open(\"".$settings['siteurl']."infusions/".$sb_url."/studiobox_popup.php\?studio=".$id."\",\"Studiobox\",\"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=500,left=50,top=50\"); return false;' title='Studiobox' target='_blank'>Musikwunsch ins Studio</a>\n";
					}
					elseif($show['studiobox']=='3') {
						echo "<a href='javascript:;' class='button' style='width: auto; padding: 3px; text-decoration:none; font-weight: bold;' onclick='popup=window.open(\"".$settings['siteurl']."infusions/".$sb_url."/studiobox_popup.php\?studio=".$id."\",\"Studiobox\",\"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=500,left=50,top=50\"); return false;' title='Studiobox' target='_blank'>Musikwunsch/Gru&szlig; ins Studio</a>\n";
					}
					//
					echo "\n</div>\n";
				}
				else { echo "<br /><br />\n"; }

				//nachfolgende Sendung:
				if(count($showstart)>1 && IsSet($showstart[$i+1]) && $showstart[$i+1]>0) {									
					if($djid[$i+1]==0) { $showid[$i+1] = 1; }
					$result5 = dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".$showid[$i+1]."'");
					$rows = dbrows($result5);
					$z=0;
					if ($rows != 0) {
						$show = dbarray($result5);
						$result6 = dbquery("SELECT * FROM `".$sendeplan_db['table_showdata']."` WHERE `ID` = '".$show['showid']."'");
						$rows = dbrows($result6);
						if ($rows != 0) {
							$showdata = dbarray($result6);
							$z++; 
							if ($djid[$i+1]!='0') {
								$result7 = dbquery("SELECT * FROM `".DB_USERS."` WHERE `user_id` = '".$djid[$i+1]."'");
								$rows = dbrows($result7);
								if ($rows != 0) { $djdata = dbarray($result7); }
							}
							if($rows==0 || $djid[$i+1]=='0') {
								$djdata['user_name'] = $sendeplan_setting['autodj_name'];
								$show['showid'] = 1;
							}									
							$result8 = dbquery("SELECT * FROM `".$sendeplan_db['table_djinfos']."` WHERE `user_id` = '".$djid[$i+1]."'");
							$rows = dbrows($result8);
							if ($rows != 0) { $djinfo = dbarray($result8); }
								
							echo "<small>Danach:</small><br />\n";
							echo "<a href='".$settings['siteurl']."infusions/".$sp_url."/sendeplan.php' title='zum Sendeplan' onmouseover='Tip(\"<div style=padding:10px;width:".($sendeplan_setting['showbox_width']-20)."px;height:".($sendeplan_setting['showbox_height']-20)."px;background-image:url(".$settings['siteurl']."infusions/".$sp_url."/images/shows/".$showdata['image'].");background-repeat:no-repeat;background-position:center;background-color:transparent;color:".$showdata['font-color'].";><div style=margin:0px;padding:5px;padding-top:3px;".($showdata['textbg']!="" && file_exists($sp_install_folder."images/bg_trans/".$showdata['textbg']) ? "background-image:url(".$settings['siteurl']."infusions/".$sp_url."/images/bg_trans/".$showdata['textbg'].");" : "")."border:0px;height:".($sendeplan_setting['showbox_height']-30)."px;overflow:hidden;><div align=".$sendeplan_setting['align_showtitle']." class=spshowtitle style=color:".$showdata['font-color'].";><table width=100% border=0 align=left cellpadding=2 cellspacing=0 class=spshowtitle style=color:".$showdata['font-color'].";><tr><td><strong>".($showstart[$i+1]<10 ? "0" : "").$showstart[$i+1].":00".($sendeplan_setting['view_endtime']=="1" ? "-".($showstart[$i+2]<10 ? "0" : "").$showstart[$i+2].":00" : "")."</strong></td>".($sendeplan_setting['use_nsv']==true && $show['is_nsv']==false ? "<td width=1><img src=".$settings['siteurl']."infusions/".$sp_url."/images/camera_off.png width=25 height=25 border=0 alt=Cam_off! /></td>" : "").($sendeplan_setting['use_nsv']==true && $show['is_nsv']==true ? "<td width=1><img src=".$settings['siteurl']."infusions/".$sp_url."/images/camera_on.png width=25 height=25 border=0 alt=Cam_on! /></td>" : "")."</tr></table>".htmlentities(utf8_cleantext(html_entity_decode($show['title'])))."</div><div align=".$sendeplan_setting['align_showdescription']." class=spshowdescription style=color:".$showdata['font-color'].";>".str_replace("\r\n","",nl2br(htmlentities(utf8_cleantext(html_entity_decode($show['description'])))))."</div></div></div>\");'><strong>".htmlentities(utf8_cleantext(html_entity_decode($show['title'])))."</strong></a> <small>mit ".(IsSet($djdata) && $djid[$i+1]!=0 ? "<a href='".$settings['siteurl']."profile.php?lookup=".$djinfo['user_id']."' title='Benutzerprofil anzeigen'><strong>".$djdata['user_name']."</strong></a>" : $sendeplan_setting['autodj_name'])."</small>\n";
						}
					}
				}
			}
		}
	}
echo "</td></tr></table>";
?>
