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

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

// Load studiobox settings
$sb_setting = @dbarray(dbquery("SELECT `default_sp`,`guests_allowed`,`infotext` FROM `".$sb_db['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']); }
if(IsSet($settings['timeoffset'])) { $timestamp += (3600*$settings['timeoffset']); }

// 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));

// Load scheduler settings
$sp_setting = @dbarray(dbquery("SELECT * FROM `".DB_PREFIX."sendeplan".($sb_setting['default_sp']>0 ? $sb_setting['default_sp'] : '')."_settings` WHERE `profile` = 'default' LIMIT 1"));

// Load scheduler entry for current hour
$result = dbquery("SELECT `".date("G",$timestamp).(date("G",$timestamp)<23 ? "`,`".(date("G",$timestamp)+1) : '')."` FROM `".DB_PREFIX."sendeplan".($sb_setting['default_sp']>0 ? $sb_setting['default_sp'] : '')."` WHERE `day` = ".$requestday);
$rows = dbrows($result);
if ($rows != 0) {
	$entry = dbarray($result);
	if($entry[date("G",$timestamp)]!="") {
		$dj_current_id = explode(".",$entry[date("G",$timestamp)]);
		if(date("G",$timestamp)<23) {
			$dj_next_id = explode(".",$entry[(date("G",$timestamp)+1)]);
		}
		// Load scheduler entry for current hour
		$query = "SELECT `studiobox` FROM `".DB_PREFIX."sendeplan".($sb_setting['default_sp']>0 ? $sb_setting['default_sp'] : '')."_shows` WHERE `ID` = ".$dj_current_id[1];
		$result = dbquery($query);
		$studiobox = dbarray($result);
	}
	else { $studiobox['studiobox'] = 0; }
}
else { $studiobox['studiobox'] = 0; }

$sb_error = "";

if(IsSet($_POST['sb_submit']) && $studiobox['studiobox'] != 0) {
	if(!iMEMBER) {
		$_CAPTCHA_IS_VALID = false;
		if($settings['version']=="7.02.05") {			
			include INCLUDES."captchas/".$settings['captcha']."/captcha_check.php";
		}
		else {
			$result = dbquery("SELECT `captcha_string` FROM `".DB_PREFIX."captcha` WHERE `captcha_string` = \"".stripinput($_POST['captcha_code'])."\" AND `captcha_ip` = \"".USER_IP."\" LIMIT 1;");
			$rows = dbrows($result);
			if ($rows != 0) { $_CAPTCHA_IS_VALID = true; }
		}
		if ($_CAPTCHA_IS_VALID == false) {
			$sb_error .= "- ".$sb_locale['securecode_was_wrong']."<br />\n";
		}
		// Alte Image-Codes loeschen
		$sql_q = "DELETE FROM `".DB_PREFIX."captcha` WHERE `captcha_datestamp` < '".(time()-3600)."'";
    		$sql_r=@mysql_query($sql_q);
	}
	if(IsSet($_POST['user_name']) && $_POST['user_name']=="") {
		$sb_error.= "- ".$sb_locale['no_name_given']."<br />\n";
	}
	if(IsSet($_POST['wishes']) && $_POST['wishes']!="" && $studiobox['studiobox'] == '1') {
		$sb_error.= "- ".$sb_locale['no_wishes_allowed']."<br />\n";
	}
	if(IsSet($_POST['greetings']) && $_POST['greetings']!="" && $studiobox['studiobox'] == '2') {
		$sb_error.= "- ".$sb_locale['no_greetings_allowed']."<br />\n";
	}		
	if((!IsSet($_POST['greetings']) || $_POST['greetings']=="") && (!IsSet($_POST['wishes']) || $_POST['wishes']=="")) {
		$sb_error.= "- ".$sb_locale['not_filled_out']."<br />\n";
	}

	// save entry in db
	if($sb_error=="") {
		$result = dbquery("INSERT INTO `".$sb_db['table_posts']."` 
		(`sp_number`, `user_id`, `user_name`, `user_ip`, `submit_time`, `greetings`, `wishes`)
		VALUES
		('".$sb_setting['sp_number']."', '".stripinput($_POST['user_id'])."', '".stripinput($_POST['user_name'])."', '".stripinput($_POST['user_ip'])."', '".time()."', '".stripinput($_POST['greetings'])."', '".stripinput($_POST['wishes'])."');");
		redirect(FUSION_SELF."?success=1");
		exit;
	}

}

echo "<!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>".$sb_locale['studiobox_name']."</title>
<link rel='stylesheet' href='".THEME."styles.css' type='text/css' media='screen' />
<meta http-equiv='Content-Type' content='text/html; charset=".$locale['charset']."'>
</head>
<body>

<div id='content' class='tbl-border'>
<div class='tbl2'>
<h3 align='center'>".$sb_locale['studiobox_name']."</h3><hr />
";
if($sb_setting['infotext']!="") {
	echo "<div>".keephtml(nl2br(stripslashes(htmlentities($sb_setting['infotext'],ENT_QUOTES, "ISO-8859-1"))))."</div>\n";
}

if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || (checkgroup($sp_setting['dj_group']))) { echo "<br />\n<center>[<a href='studiobox_dj.php' title=''>".$sb_locale['dj_studiobox_link']."</a>]</center><br />\n"; }

if($sb_error!="") {
	echo "<div class='alt'>".$sb_locale['error_occured'].":<br />\n".$sb_error."\n</div>\n";
}

if($studiobox['studiobox']!=0) {

	echo "<div style='padding-bottom: 10px;'>\n";
	echo "<div align='center'>\n";

	if(!iMEMBER && $sb_setting['guests_allowed']=='0') {
		echo "<div class='alt'>".$sb_locale['no_guests_allowed']."\n</div>\n";
	}
	elseif(IsSet($_GET['success']) && $_GET['success'] = '1') {
		echo "<br /><br /><div class='alt'>".$sb_locale['message_was_sent']."</div><br /><br />
		<center><input type='submit' value='".$sb_locale['close_window']."' onclick='self.close();' /></center>";
	}
	else {
	
	echo "
	<form action='".FUSION_SELF."' name='studiobox' method='post' style='padding-top: 10px;'>

		".$sb_locale['input_username'].":<br />
		<input type='text' class='textbox' name='user_name' value=".(iMEMBER ? "'".$userdata['user_name']."'  readonly" : (IsSet($_POST['user_name']) ? "'".$_POST['user_name']."'" : "''"))." style='width: 280px' autocomplete='off' />
		<input type='hidden' name='user_id' value=".(iMEMBER ? "'".$userdata['user_id']."'" : "'0'")." />
		<input type='hidden' name='user_ip' value='".USER_IP."' /><br />
		";
		echo "<br />".$sb_locale['input_wish'].":<br />\n";
		if($studiobox['studiobox']!=1) {
			echo "
			<input type='text' class='textbox' name='wishes' value='".(IsSet($_POST['wishes']) ? $_POST['wishes'] : "")."' style='width: 280px' autocomplete='off' /><br />
			";
		}
		else { echo "<small><em>- ".$sb_locale['input_no_wishes']." -</em></small><br />\n"; }

		echo "<br />".$sb_locale['input_greetings'].":<br />\n";
		if($studiobox['studiobox']!=2) {
			echo "
			<textarea class='textbox' name='greetings' style='width: 280px; height: 40px;' />".(IsSet($_POST['greetings']) ? $_POST['greetings'] : "")."</textarea><br />";
		}
		else { echo "<small><em>- ".$sb_locale['input_no_greetings']." -</em></small><br /><br />\n"; }
		if(!iMEMBER) {			
			echo "<br />\n";
			if($settings['version']=="7.02.05") {
				include INCLUDES."captchas/".$settings['captcha']."/captcha_display.php";
				if (!isset($_CAPTCHA_HIDE_INPUT) || (isset($_CAPTCHA_HIDE_INPUT) && !$_CAPTCHA_HIDE_INPUT)) {
					echo "<label for='captcha_code'>".$sb_locale['input_security_code'].":</label><br />
					<input type='text' id='captcha_code' name='captcha_code' class='textbox' autocomplete='off' style='width:100px' />
					<br />";
				}
			}
			else {
				srand((double)microtime()*1000000); 
				$temp_num = md5(rand(0,9999)); 
				$vcode_1 = substr($temp_num, 17, 5); 
				$vcode_2 = md5($vcode_1);
				unset($temp_num);
				$result = dbquery("INSERT INTO `".DB_PREFIX."captcha` (`captcha_datestamp`, `captcha_ip`, `captcha_encode`, `captcha_string`) VALUES('".time()."', '".USER_IP."', '$vcode_2', '$vcode_1')");
				echo "<label for='captcha_code'>".$sb_locale['input_security_code'].":</label><br />
				<img src='captcha_image.php?vimage=".$vcode_2."' alt='Code-Image' border='0' /><br />
				<input type='text' id='captcha_code' name='captcha_code' class='textbox' autocomplete='off' style='width:100px' />
				<br />
				";
			
			}
		}
		echo "
		<input type='submit' class='button' name='sb_submit' style='width: auto;' value='".$sb_locale['input_button_sent']."' />
	</form>
	";
	}
	echo "
	</div></div>
	<br />
	";
	}

else {
	echo "<br /><br /><div class='alt'><center><em>".$sb_locale['studiobox_not_active']."</em></center></div><br /><br />\n";
}

echo "
<div align='center' style='float: left; text-align: center; width: 100%; font-size: 9px;'>".$sb_locale['studiobox_name']." v2.0 &copy; by <a href='http://www.systemweb.de' target='_blank'>Systemweb.de</a></div>
<br />
</div>
</div>
";
?>
</body>
</html>

