<?PHP
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
                              Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de 
 Last update : 2005-11-24     - Bugfix for multibyte charsets    
 Part of     : Update to Version 01.00.02               
******************************************************************************/
// Browsern das cachen von Backendseiten verbieten
if ($cfg_dedi['backend_cache'] == '1') {
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Datum aus Vergangenheit
	header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // immer geändert
	header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
	header('Cache-Control: post-check=0, pre-check=0', false);
	header('Pragma: no-cache');
}
$tpl->loadTemplatefile('header.tpl');
if ($area == 'con_frameheader') $area='con_editframe';

// Selectbox für die Projekte
$sql = "SELECT DISTINCT * FROM ". $dedi_db['clients'] ." A LEFT JOIN ". $dedi_db['clients_lang']." B USING(idclient) ORDER BY A.name";
$db->query($sql);
$con_more_than_one_client = false;
$prev_client = '';
while($db->next_record()) {
	// darf User Projekt sehen?
	if ($perm -> have_perm('1', 'lang', $db->f('idlang')) && $prev_client != $db->f('idclient')) {
		if ($client == $db->f('idclient')) {
			$con_client_options .= "      <option value=\"".$db->f('idclient')."\" selected>".$db->f('name')."</option>\n";
			$con_act_client = $db->f('name');
		} else {
			$con_client_options .= "      <option value=\"".$db->f('idclient')."\">".$db->f('name')."</option>\n";
			$con_more_than_one_client = true;
		}
		// wenn mehrere sprachen in einem client sind, verhindern, das der client öfters als ein mal angezeigt wird
		$prev_client = $db->f('idclient');
	}
}
if ($con_more_than_one_client) {
	$tpl_in['CLIENT_FORM'] = "<form name=\"clientform\" method=\"post\" action=\"".$sess->url('main.php')."\" target=\"_top\">\n";
	if (!empty($idcatside)) $tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"idcatside\" value=\"$idcatside\">\n";
	if (!empty($idside)) $tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"idside\" value=\"$idside\">\n";
	if (!empty($idcat)) $tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"idcat\" value=\"$idcat\">\n";
	if (!empty($idlay)) $tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"idlay\" value=\"$idlay\">\n";
	if (!empty($idmod)) $tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"idmod\" value=\"$idmod\">\n";
	if (!empty($idtpl)) $tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"idtpl\" value=\"$idtpl\">\n";
	if (!empty($idclient)) $tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"idclient\" value=\"$idclient\">\n";
	if (!empty($idcatsidetpl)) $tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"idcatsidetpl\" value=\"$idcatsidetpl\">\n";
	$tpl_in['CLIENT_FORM'] .= "    <input type=\"hidden\" name=\"changeclient\" value=\"1\">
    <input type=\"hidden\" name=\"change_show_tree\" value=\"0\">
    <select name=\"client\" size=\"1\" onChange=\"clientform.submit()\">
".$con_client_options."    </select></form>";
}

// Selectbox für die Sprachen
$sql = "SELECT A.idlang, A.name FROM ".$dedi_db['lang']." A LEFT JOIN ".$dedi_db['clients_lang']." B USING(idlang) WHERE B.idclient='$client' ORDER BY idlang";
$db->query($sql);
$con_more_than_one_lang = false;
while($db->next_record()) {
	// darf User Sprache sehen?
	if($perm -> have_perm('1', 'lang', $db->f('idlang'))) {
		if ($lang == $db->f('idlang')) {
			$con_lang_options .= "      <option value=\"".$db->f('idlang')."\" selected>".$db->f('name')."</option>\n";
			$con_act_lang = $db->f('name');
		} else {
			$con_lang_options .= "      <option value=\"".$db->f('idlang')."\">".$db->f('name')."</option>\n";
			$con_more_than_one_lang = true;
		}
	}
}
if($con_more_than_one_lang) {
	$tpl_in['LANG_FORM'] ="<form name=\"languageform\" method=\"post\" action=\"".$sess->url('main.php?area='.$area)."\" target=\"_top\">\n";
	if (!empty($idside)) $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"idside\" value=\"$idside\">\n";
	if (!empty($idcat)) $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"idcat\" value=\"$idcat\">\n";
	if (!empty($idlay)) $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"idlay\" value=\"$idlay\">\n";
	if (!empty($idmod)) $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"idmod\" value=\"$idmod\">\n";
	if (!empty($idtpl)) $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"idtpl\" value=\"$idtpl\">\n";
	if (!empty($idclient)) $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"idclient\" value=\"$idclient\">\n";
	if (!empty($idcatsidetpl)) $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"idcatsidetpl\" value=\"$idcatsidetpl\">\n";
	if (isset($change_show_tree)) $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"change_show_tree\" value=\"$change_show_tree\">\n";
	else $tpl_in['LANG_FORM'] .= "    <input type=\"hidden\" name=\"change_show_tree\" value=\"$show_tree\">\n";
	$tpl_in['LANG_FORM'] .= "    <select name=\"lang\" size=\"1\" onChange=\"languageform.submit()\">\n";
	$tpl_in['LANG_FORM'] .= $con_lang_options."    </select></form>";
}
$sql = "SELECT idbackendmenu, parent, sortindex, entry_langstring, entry_url, url_target, entry_validate FROM ". $dedi_db['backendmenu'] ." WHERE idclient IN(0, $client) AND entry_langstring NOT IN('empty_dummy') ORDER BY parent, sortindex";
$db->query($sql);
for($i =0; $db->next_record(); $i++) {
	$parent_old = $parent_new;
	$parent_new = $db->f('parent');
	if($parent_new != $parent_old){$k=0;}
	$unsort_array[$parent_new][$k]['id'] = $db->f('idbackendmenu');
	$unsort_array[$parent_new][$k]['parent'] = $db->f('parent');
	$unsort_array[$parent_new][$k]['sort'] = $db->f('sortindex');
	$unsort_array[$parent_new][$k]['langstring'] = $db->f('entry_langstring');
	$unsort_array[$parent_new][$k]['url'] = $db->f('entry_url');
	$unsort_array[$parent_new][$k]['url_target'] = $db->f('url_target');
	$unsort_array[$parent_new][$k]['validate'] = $db->f('entry_validate');
	$k++;
}

/**
* rekursives Auslesen vom $unsort_array
* die Daten werden danach mit richtiger Reihenfolge in ein Array eingeordnet
*/
/**
* Menupunkte rekursiv in das Array tree einlesen
*
* @args startposition im baum
*
*/
$count =0;
$maxlevel =0;
menu_level_order(0);
function menu_level_order($node_id, $level = 0) {
	global $unsort_array, $tree, $count, $maxlevel;

	for($i =0; ! empty($unsort_array[$node_id][$i]['id']) ; $i++) {
		$tree[$count]['level'] = $level;
		$tree[$count]['id'] = $unsort_array[$node_id][$i]['id'];
		$tree[$count]['sort'] = $unsort_array[$node_id][$i]['sort'];
		$tree[$count]['langstring'] = $unsort_array[$node_id][$i]['langstring'];
		$tree[$count]['url'] = $unsort_array[$node_id][$i]['url'];
		$tree[$count]['url_target'] = $unsort_array[$node_id][$i]['url_target'];
		$tree[$count]['validate'] = $unsort_array[$node_id][$i]['validate'];
		$tree[$count]['parent'] = $unsort_array[$node_id][$i]['parent'];
		if ($tree[$count][0] > $maxlevel) $maxlevel = $tree[$count]['id'];
		$count++;
		/* Haengt ein Leaf am Leaf? einfach mal rekursiv runtergehen und nachkucken ... */
		menu_level_order($tree[$count-1]['id'], ($level + 1));
	}
	/* Fallback aus der Rekursion */
	return;
}
$main_index = -1;
$sub_index = -1;

// needed to figure out active sublaye
$pos = strpos($area, '_');
$layer_cutter = (!$pos) ? $area: substr( $area, 0, $pos );
for($i = 0; $i < $count; $i++) {
	// Hauptmenü bauen
	if($tree[$i]['level'] == '0'){
		$main_index++;
		$sub_index = -1;
        //CHANGE STAM
        if ($tree[$i]['url'] == 'root') {
            $surl = 'javascript:con_layer('. ($main_index+1) .')';
        } else {
            $dynamic = '$surl = "'.$tree[$i]['url']. '";';
        	// parse url, this is nessesary if the var includes dynamic content like arrays, vars, etc...
            eval($dynamic);
        }
        //CHANGE STAM
		$mouseover = 'onMouseOver="on(\''. $dedi_lang[$tree[$i]['langstring']] .'\');return true;"';
		$mouseout = 'onMouseOut="off();return true;"';
		$mainmenu[$main_index]['url'] = '<a href = "'.$surl. '" '. $mouseover .' '. $mouseout .' class="mainmenu">'. $dedi_lang[$tree[$i]['langstring']] .'</a>';
		$mainmenu[$main_index]['validate'] = $tree[$i]['validate'];
		$out = $mainmenu[$main_index]['url'];

	// Untermenü aufbauen
	} else {
		$sub_index++;
		$dynamic = '$surl = "'.$tree[$i]['url']. '";';

		// parse url, this is nessesary if the var includes dynamic content like arrays, vars, etc...
		eval($dynamic);

		// target
		if($tree[$i]['url_target'] == 'frame') $surl = $sess->url('main.php?area=con_editframe&idplugin='. $tree[$i]['id']);
		else $surl = $sess->url($surl);
		$mouseover = 'onMouseOver="on(`'. $dedi_lang[$tree[$i]['langstring']] .'`);return true;"';
		$mouseout = 'onMouseOut="off();return true;"';

		// fucking mouseover slashing
		$submenu[$sub_index] = str_replace( '"', '\"', '<a href = "'.$surl. '" '. $mouseover .' '. $mouseout .' class="submenu" target="_parent">'. $dedi_lang[$tree[$i]['langstring']] .'</a>');
		$mainmenu[$main_index]['permstring'] .= '( '. $tree[$i]['validate'] .')*';
		$dynamic = 'if('.$tree[$i]['validate'].')$out="'. $submenu[$sub_index] .'&nbsp;&nbsp;&nbsp;";else $out="";';

		// check perms
		eval($dynamic);

		// fucking mouseover slashing2
		$sub_final[$main_index] .= str_replace('`', "'", $out);
		$cat_is_nit_empty[$main_index] = true;

		// check for active menu layer
		//CHANGE STAM BB
		if ((preg_match('/area='.$layer_cutter.'\b/', $tree[$i]['url']) && empty($dedi_plugin)) 
			|| $idplugin == $tree[$i]['id'] 
			|| strstr($tree[$i]['url'], 'dedi_plugin='.$dedi_plugin) 
			&& !empty($dedi_plugin)){
		
			$active_submenu_layer = $main_index +1;
		}
		//CHANGE STAM BB
	}
	$out = '';
}
$tpl->setCurrentBlock('SUBMENU');

// throw out mainmenu
$maincount = count($mainmenu);
for($i=0; $i < $maincount; $i++) {
	$permstring = str_replace(')*(', ') || (', $mainmenu[$i]['permstring']);
	$permstring = str_replace(')*', ')', $permstring);
	if ($mainmenu[$i]['validate'] != 'root' && $permstring) $permstring = '('.$permstring.') && '.$mainmenu[$i]['validate'];

	// check perms for displaying maincat
	if(trim($permstring) != ''){
		$permurl = $mainmenu[$i]['url'];
		$dynamic = 'if('.$permstring.') $out.="$permurl&nbsp;&nbsp;&nbsp;";else $out.="";';
		eval($dynamic);

	// only output, no perms to check
	} else if($cat_is_nit_empty[$i]) $out .= $mainmenu[$i]['url'] .'&nbsp;&nbsp;&nbsp;';
	$sub_tmp['COUNT'] = $i+1;
	$sub_tmp['SUB_MENU_ENTRYS'] = $sub_final[$i];
	$sub_tmp['IMGPATH'] = 'tpl/'.$cfg_dedi['skin'].'/img/';
	$tpl->setVariable($sub_tmp);
	$tpl->parseCurrentBlock('SUBMENU');
}
// Robert Hartwig, 9.10.2004 -- charset-Variable eingefügt, bindet {CHARSET} in header.tlp
$tpl_in['CHARSET'] = $lang_charset;
$tpl_in['VERSION'] = $cfg_dedi['version'];
$tpl_in['IMGPATH'] = 'tpl/'.$cfg_dedi['skin'].'/img/';
$tpl_in['MAIN_MENU_ENTRYS'] = $out;
$tpl_in['MAX_SUBMENUS'] = $maincount;
$tpl_in['ACTIVE_SUBMENU_LAYER'] = $active_submenu_layer;
$tpl_in['LOGOUT_URL'] = $sess->url('main.php?area=logout');
$tpl_in['PATH_HELP'] = 'help/index_'.$cfg_dedi['backend_lang'] .'.php#'. $area ;
$tpl_in['LANG_TOOLTIP'] = addslashes($dedi_lang['gen_logout']);
$tpl_in['DELETE_MSG'] = $dedi_lang['gen_deletealert'];
$tpl_in['HELP_TOOLTIP'] = addslashes($dedi_lang['dedi_help']);
$tpl_in['LOGOUT_WIDTH'] = $dedi_lang['gen_logout_wide'];
$tpl_in['LOGGED_USER'] = $dedi_lang['gen_user'] . $auth->auth['uname'];

$tpl->setVariable($tpl_in);
$tpl->show();
unset($sub_tmp, $tpl_in);
unset($unsort_array, $tree, $count, $maxlevel);
unset($tpl);
$tpl = &new HTML_Template_IT($this_dir.'tpl/'.$cfg_dedi['skin'].'/');
?>
