<?

// read the database settings from the file
include_once('./settings.php');

// DB Connection Info read from settings file
Global $DBServer, $DBUser, $DBPass, $DBDatabase;
			
//establish a connection to database (server, database, password)
mysql_connect($DBServer, $DBUser, $DBPass) or die ("Could not connect to database");
	
//select database
mysql_select_db($DBDatabase) or die ("Could not select database");


$Settings['DBPrefix'] = $DBPrefix;


$query = "select * from `".$Settings['DBPrefix']."core-Settings`";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
	$Settings[$row['Name']] = $row['Value'];
}



	if ($Settings['NewLKS']) {
		$LK = $Settings['NewLKS'];
		$Settings['lvl'] = (substr_count($LK,chr(88))+substr_count($LK,chr(89))+substr_count($LK,chr(90)));
		
		
		for ($i = 0; $i < strlen($LK); $i++) {
			if (ord(substr($LK,$i,1)) < 71 || ord(substr($LK,$i,1)) > 90) {
				$LK2 .= substr($LK,$i,1);
			}
		}
		
		for ($i = 0; $i < strlen($LK2); $i=$i+2) {
			$URL .= chr(hexdec(substr($LK2,$i,2)));
		}
		
		$Settings['BaseURL'] = $URL;
	}



if ($Settings['WindowsQSPatch'] == 'y') {
	$Settings['ScriptName'] .= '?';
}


$MainURL = $Settings['BaseURL'] . '/' . $Settings['ScriptName'];



$CookieData = $HTTP_COOKIE_VARS[$Settings['CookieName']];

$LoggedIn = 'n';
$UserLevel = 0;

// if the cookie exists and isn't empty
if ($CookieData != '' && $CookieData != 'not_logged_in') {

	// split the username and password out
	list ($CookieUsername, $CookiePassword) = split('==', $CookieData, 2);
	
	// if username and password are both not empty
	if ($CookieUsername != '' && $CookiePassword != '') {

		$query = "select RecordNumber,Level,Password from `".$Settings['DBPrefix']."core-Users` where Username='$CookieUsername' LIMIT 1";
		$result = mysql_query($query) or die (mysql_error());
		
		// if any rows are returned, the username exists
		if (mysql_num_rows($result) != 0) {
			
			$row = mysql_fetch_array($result);
			// check the password returned from the database against the one in the cookie
			if ($row['Password'] == $CookiePassword) {
				// passwords match
			    $LoggedIn = 'y';
				
				$UserLevel = $row['Level'];
				
			}
			
		}

	}

}



// remove slashes from language id to avoid any possible attempts to load other code in
$Settings['DefaultLanguage'] = str_replace('/','',$Settings['DefaultLanguage']);

if ($Settings['DefaultLanguage'] == '') {
	$Settings['DefaultLanguage'] = 'english';
}

// see if the file exists
if (file_exists('./languages/inc-core-lang-'.$Settings['DefaultLanguage'].'.php')) {
	$LanguageFileLocated = true;
}
else {
	$Settings['DefaultLanguage'] = 'english';
	// check again for default file
	if (file_exists('./languages/inc-core-lang-'.$Settings['DefaultLanguage'].'.php')) {
		$LanguageFileLocated = true;
	}
	else {
		print 'Error.  Language files could not be loaded.<br><br>Please ensure at least the English FlexCMS language pack is present in the <i>languages</i> folder under the main FlexCMS directory.';
	}
}

if ($LanguageFileLocated == true) {
	include_once('./languages/inc-core-lang-'.$Settings['DefaultLanguage'].'.php');
}



		$ModulesList = explode('-',$Settings['M1']);
		for ($i = 0; $i < count($ModulesList); $i++) {
			$IM[$ModulesList[$i]] = 'y';
		}
		



		$LinkDropdown = '<option value=""> '.$MsgText[0][191].':</option>';
		
		$LinkDropdown .= '<option value="'.$MainURL.'/index.html"> - '.$MsgText[0][1].'</option>';
		//$JSArray5 = '"index"';
		
		if ($IM['cl'] == 'y') {
			$LinkDropdown .= '<option value="'.$MainURL.'/calendar.html"> - '.$MsgText[1][1].'</option>';
			//$JSArray5 .= ',"calendar"';
		}
		if ($IM['mb'] == 'y') {
			$LinkDropdown .= '<option value="'.$MainURL.'/forum.html"> - '.$MsgText[2][1].'</option>';
			//$JSArray5 .= ',"forum"';
		}
		if ($IM['gb'] == 'y') {
			$LinkDropdown .= '<option value="'.$MainURL.'/guestbook.html"> - '.$MsgText[6][1].'</option>';
			//$JSArray5 .= ',"guestbook"';
		}
		
		if ($IM['nl'] == 'y') {
			$LinkDropdown .= '<option value="'.$MainURL.'/newsletter/subscribe.html"> - '.$MsgText[5][6].'</option>';
			//$JSArray5 .= ',"newsletter/subscribe"';
		
			$LinkDropdown .= '<option value="'.$MainURL.'/newsletter/unsubscribe.html"> - '.$MsgText[5][7].'</option>';
			//$JSArray5 .= ',"newsletter/unsubscribe"';
			
			$LinkDropdown .= '<option value="'.$MainURL.'/newsletter/messages.html"> - '.$MsgText[5][8].'</option>';
			//$JSArray5 .= ',"newsletter/messages"';
		}
		
		if ($IM['lm'] == 'y') {
			$LinkDropdown .= '<option value="'.$MainURL.'/links.html"> - '.$MsgText[8][4].'</option>';
			//$JSArray5 .= ',"links"';
		}
		
		if ($IM['na'] == 'y') {
			$LinkDropdown .= '<option value="'.$MainURL.'/articles.html"> - '.$MsgText[7][1].'</option>';
			
			$query2 = "select * from `".$Settings['DBPrefix']."na-Categories` ORDER BY CategoryName ASC, RecordNumber ASC";
			$result2 = mysql_query($query2) or die (mysql_error());
			while ($row2 = mysql_fetch_array($result2)) {
				$LinkDropdown .= '<option value="'.$MainURL.'/articles/'.$row2['RecordNumber'].'">&nbsp;&nbsp;&nbsp;&nbsp; - '.$row2['CategoryName'].' ('.$row2['RecordNumber'].')</option>';
			}
		}
		
		if ($IM['ec'] == 'y') {
			$LinkDropdown .= '<option value="'.$MainURL.'/store.html"> - '.$MsgText[9][271].'</option>';
		}
		
		$LinkDropdown .= '<option value="'.$MainURL.'/options.html"> - '.$MsgText[0][4].'</option>';
		//$JSArray5 .= ',"options"';
		
		$LinkDropdown .= '<option value="'.$MainURL.'/profile-edit.html"> - '.$MsgText[0][78].'</option>';
		//$JSArray5 .= ',"profile-edit"';
		
		$LinkDropdown .= '<option value="'.$MainURL.'/login.html"> - '.$MsgText[0][192].'</option>';
		//$JSArray5 .= ',"login"';
		
		$LinkDropdown .= '<option value="'.$MainURL.'/logout.html"> - '.$MsgText[0][193].'</option>';
		//$JSArray5 .= ',"logout"';
		
		$LinkDropdown .= '<option value="'.$MainURL.'/register.html"> - '.$MsgText[0][124].'</option>';
		//$JSArray5 .= ',"register"';
		
		if ($IM['pg'] == 'y') {
			$LinkDropdown .= '<option value="'.$MainURL.'/photos.html"> - '.$MsgText[3][1].'</option>';
			//$JSArray5 .= ',"photos"';
		
			$query2 = "select * from `".$Settings['DBPrefix']."pg-Galleries` where MinLevel<='".$UserLevel."' ORDER BY GalleryTitle ASC, RecordNumber ASC";
			$result2 = mysql_query($query2) or die (mysql_error());
			while ($row2 = mysql_fetch_array($result2)) {
				$LinkDropdown .= '<option value="'.$MainURL.'/photos/'.$row2['RecordNumber'].'.html">&nbsp;&nbsp;&nbsp;&nbsp; - '.$row2['GalleryTitle'].' ('.$row2['RecordNumber'].')</option>';
				//$JSArray5 .= ',"photos/'.$row2['RecordNumber'].'"';
			}
		}
		
		
		$LinkDropdown .= '<option value=""> '.$MsgText[0][194].':</option>';
		
		$query2 = "select * from `".$Settings['DBPrefix']."core-Pages` where MinLevel<='".$UserLevel."' ORDER BY PageTitle ASC";
		$result2 = mysql_query($query2) or die (mysql_error());
		while ($row2 = mysql_fetch_array($result2)) {
			if ($row2['PageLink'] == '') { $row2['PageLink'] == $row2['PageID']; }
			$LinkDropdown .= '<option value="'.$MainURL.'/pages/'.$row2['PageLink'].'.html">&nbsp;&nbsp;&nbsp;&nbsp; - '.$row2['PageTitle'].' ('.$row2['PageLink'].')</option>';
			//$JSArray5 .= ',"pages/'.$row2['PageLink'].'"';
		}





print 'document.write(\'<select name="LinkDropdown" id="LinkDropdown" onClick="OnPageChange();" onChange="OnPageChange();">'.addslashes($LinkDropdown).'</select>\');';

?>
