<?php
if(!function_exists("tblexists_day")) {

function tblexists_day($newday) {
	global $query_counter, $sendeplan_db;
	$result = @dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` WHERE `day` = $newday");
	$rows = @dbrows($result);
	$query_counter++;
	if ($rows == 0) { return FALSE; }	
	else { return TRUE; }
}
}

if(!function_exists("create_day")) {
function create_day($newday) {
	global $query_counter, $sendeplan_db;
	// check for old timestamps before sp update 3.01
	$result = dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` WHERE `day` = ".mktime(12,0,0,date("m",$newday),date("d",$newday),date("Y",$newday)));
	$rows = dbrows($result);
	$query_counter++;
		$preset = dbarray($result);

	// check for weekly shows
	$result1 = dbquery("SELECT * FROM `".$sendeplan_db['table_weekly']."` WHERE `weekday` = '".date("w",$newday)."'");
	$rows = dbrows($result1);
	$query_counter++;
	if ($rows != 0) {
		$tmp = dbarray($result1);
		for($i=0;$i<=23;$i++) {
			if($tmp[$i]!="") {
				$preset[$i] = $tmp[$i];
			}
		}
	}


	// check for planned events
	$result2 = dbquery("SELECT * FROM `".$sendeplan_db['table_events']."` WHERE `day` = '".$newday."'");
	$rows = dbrows($result2);
	$query_counter++;
	if ($rows != 0) {
		$tmp = dbarray($result2);
		for($i=0;$i<=23;$i++) {
			if($tmp[$i]!="") {
				$preset[$i] = $tmp[$i];
			}
		}
	}

	// finally: create new day with given presets
	$sql_q = "INSERT INTO `".$sendeplan_db['table_schedules']."` 
	(`day`, `0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`, `18`, `19`, `20`, `21`, `22`, `23`) 
	VALUES 
	('".$newday."', '".$preset['0']."', '".$preset['1']."', '".$preset['2']."', '".$preset['3']."', '".$preset['4']."', '".$preset['5']."', '".$preset['6']."', '".$preset['7']."', '".$preset['8']."', '".$preset['9']."', '".$preset['10']."', '".$preset['11']."', '".$preset['12']."', '".$preset['13']."', '".$preset['14']."', '".$preset['15']."', '".$preset['16']."', '".$preset['17']."', '".$preset['18']."', '".$preset['19']."', '".$preset['20']."', '".$preset['21']."', '".$preset['22']."', '".$preset['23']."')";
	$result3 = dbquery($sql_q);
	$query_counter++;
	return;
}
}

if(!function_exists("getusernick")) {
function getusernick($id) {
global $query_counter;
$query_counter++;
$result = @dbquery("SELECT * FROM `".DB_USERS."` WHERE user_id='$id'");
$tmp = @dbarray($result);
if($tmp) return $tmp['user_name'];
else return false;
}

function getDropDown($hour, $showinfo, $day) {
	global $userdata, $myshowtitle, $myshowid, $sp_setting, $settings, $timestamp, $splocale, $sendeplan_db;
	if($showinfo!="") {$tmp = explode(".", $showinfo);}
	else { $tmp[0] = ""; $tmp[1] = ""; }
	$output = ""; 
	if(count($myshowid)>0 || iSUPERADMIN || checkgroup($sp_setting['dj_admingroup'])) {
		if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || ($tmp[0]!=$userdata['user_id'] || ($tmp[0]==$userdata['user_id'] && ($timestamp+(3600*24*$sp_setting['signoff_forbid'])) < mktime($hour,0,0,date("m",$day),date("d",$day),date("Y",$day))))) { $output .= "<select name='".$hour."' class='textbox' style='width: 200px;'><option value=''>-keine Show-</option>"; }
		else { $output .= "<select name='".$hour."' class='textbox' style='width: 200px; border: 1px solid #f00; background-color: #fdc; color: #000;'>"; }
		for($i=0; $i<count($myshowid); $i++) {
			$output.= "<option value='".$userdata['user_id'].".".$myshowid[$i]."'".(IsSet($tmp) && $tmp[1] == $myshowid[$i] ? " selected" : "").">".$myshowtitle[$i]."</option>";		
		}
		// Admin Dropdown extended:
		if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup'])) {
			$result = @dbquery("SELECT * FROM `".DB_USERS."` WHERE  `user_id` != '".$userdata['user_id']."' AND `user_groups` REGEXP('^\\\.".$sp_setting['dj_group']."$|\\\.".$sp_setting['dj_group']."\\\.|\\\.".$sp_setting['dj_group']."$') ORDER BY `user_name` ASC");
			//$rows = dbrows($result);
			while($djdata = @dbarray($result)) {
			$result2 = @dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE  `user_id` = '".$djdata['user_id']."' ORDER BY `title` ASC");
			//$rows = dbrows($result);
				while($showdata = @dbarray($result2)) {
					$output.="<option value='".$djdata['user_id'].".".$showdata['ID']."'".($tmp[0] == $djdata['user_id'] && $tmp[1] == $showdata['ID'] ? " selected" : "").">".$djdata['user_name'].": ".$showdata['title']."</option>";
				}
			}
		}
		
		$output.= "</select>";
	}
	else {
		$output = "-FEHLER-";
	}
	return $output;
}
}

if(!function_exists("getShowDropDown")) {
function getShowDropDown() {
	global $sp_setting, $sendeplan_db, $show_id, $userdata;
	$output = "<select name='show_id' class='textbox' style='width: 200px;'>\n";
	$result = @dbquery("SELECT * FROM `".DB_USERS."` WHERE  `user_groups` REGEXP('^\\\.".$sp_setting['dj_group']."$|\\\.".$sp_setting['dj_group']."\\\.|\\\.".$sp_setting['dj_group']."$') ORDER BY `user_name` ASC");
	while($djdata = @dbarray($result)) {
		$result2 = @dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE  `user_id` = '".$djdata['user_id']."' ORDER BY `title` ASC");
		while($showdata = @dbarray($result2)) {
			$output.="<option value='".$showdata['ID']."'".($show_id == $showdata['ID'] ? " selected" : "").">".$djdata['user_name'].": ".$showdata['title']."</option>\n";
		}
	}
	$output.="</select>\n";
	return $output;
}
}

if(!function_exists("br2nl")) {
function br2nl($string){
	$return=eregi_replace('<br[[:space:]]*/?[[:space:]]*>','\r\n',$string);
	return $return;
}
}

if(!function_exists("is_weekly")) {
function is_weekly($stunde,$datum, $eintrag) {
        global $sendeplan_db;
	$tag = date("w", $datum);
	$result = @dbquery("SELECT * FROM `".$sendeplan_db['table_weekly']."` WHERE  `weekday` = '".$tag."' AND `".$stunde."` = '".$eintrag."'");
	$rows = @dbrows($result);
	if ($rows != 0) { return true; }
	else { return false; }		
}
}

if(!function_exists("getWeeklyDropDown")) {
function getWeeklyDropDown() {
	global $sp_setting, $sendeplan_db, $userdata;
	$output = "<select name='show' class='textbox' style='width: 200px;'>\n";
	if(checkgroup($sp_setting['dj_admingroup']) || iSUPERADMIN) {
		$result = @dbquery("SELECT * FROM `".DB_USERS."` WHERE  `user_groups` REGEXP('^\\\.".$sp_setting['dj_group']."$|\\\.".$sp_setting['dj_group']."\\\.|\\\.".$sp_setting['dj_group']."$') ORDER BY `user_name` ASC");
		while($djdata = @dbarray($result)) {
			$result2 = @dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE  `user_id` = '".$djdata['user_id']."' ORDER BY `title` ASC");
			while($showdata = @dbarray($result2)) {
				$output.="<option value='".$djdata['user_id'].".".$showdata['ID']."'>".$djdata['user_name'].": ".$showdata['title']."</option>\n";
			}
		}
	}
	elseif($sp_setting['showrepeats_dj']==1) {
	$result2 = @dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE  `user_id` = '".$userdata['user_id']."' ORDER BY `title` ASC");
		while($showdata = @dbarray($result2)) {
			$output.="<option value='".$userdata['user_id'].".".$showdata['ID']."'>".$showdata['title']."</option>\n";
		}
	}
	$output.="</select>\n";
	return $output;
}
}

if(!function_exists("get_sp_weekday")) {
function get_sp_weekday($weekday) {
	global $splocale;
	if($weekday == '0') {return $splocale['sunday'];}
	elseif($weekday == '1') {return $splocale['monday'];}
	elseif($weekday == '2') {return $splocale['tuesday'];}
	elseif($weekday == '3') {return $splocale['wednesday'];}
	elseif($weekday == '4') {return $splocale['thursday'];}
	elseif($weekday == '5') {return $splocale['friday'];}
	elseif($weekday == '6') {return $splocale['saturday'];}
	return;
}
}

// check for correct permissions
if(!function_exists('is_folder_writable')) {
	function is_folder_writable($path) {
    		// extended check for open_basedir/safemod restrictions. $path must contain trailing slash to work!!!
    		$fp = @fopen ($path."check.tmp", "a");
    		if ($fp===false) {
        		return false;
		}
    		fclose($fp);
    		@unlink($path."check.tmp");
    		return true;
	}
}


if(function_exists('add_to_head')) {
	global $sp_setting;
	add_to_head("
	<style type=\"text/css\" media=\"screen\">
	.spshowtitle {
		font-family: ".$sp_setting['font_showtitle'].";
		font-size: ".$sp_setting['fontsize_showtitle']."px;
		font-weight: ".($sp_setting['bold_showtitle']==true ? "bold" : "normal").";
	}
	.spshowdescription {
		font-family: ".$sp_setting['font_showdescription'].";
		font-size: ".$sp_setting['fontsize_showdescription']."px;
		font-weight: ".($sp_setting['bold_showdescription']==true ? "bold" : "normal").";
	}
	</style>
	");
}

if(!function_exists('string2js')) {
	function string2js($string) {
        $bad_characters = array("Ä", "Ö", "Ü", 'ä"', "ö", "ü", "ß", "\r", "\n");
	    $safe_characters = array('\u00c4','\u00d6','\u00dc','\u00e4','\u00f6','\u00fc','\u00df','<br />','');
		$string= str_replace($bad_characters, $safe_characters, $string);
	return $string;
    }
}
?>
