<?

error_reporting (E_ALL ^ E_NOTICE);

// 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'];
}



$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="index"> - '.$MsgText[0][1].'</option>';
		$JSArray5 = '"index"';
		
		if ($IM['cl'] == 'y') {
			$LinkDropdown .= '<option value="calendar"> - '.$MsgText[1][1].'</option>';
			$JSArray5 .= ',"calendar"';
		}
		if ($IM['mb'] == 'y') {
			$LinkDropdown .= '<option value="forum"> - '.$MsgText[2][1].'</option>';
			$JSArray5 .= ',"forum"';
		}
		if ($IM['gb'] == 'y') {
			$LinkDropdown .= '<option value="guestbook"> - '.$MsgText[6][1].'</option>';
			$JSArray5 .= ',"guestbook"';
		}
		
		if ($IM['nl'] == 'y') {
			$LinkDropdown .= '<option value="newsletter/subscribe"> - '.$MsgText[5][6].'</option>';
			$JSArray5 .= ',"newsletter/subscribe"';
		
			$LinkDropdown .= '<option value="newsletter/unsubscribe"> - '.$MsgText[5][7].'</option>';
			$JSArray5 .= ',"newsletter/unsubscribe"';
			
			$LinkDropdown .= '<option value="newsletter/messages"> - '.$MsgText[5][8].'</option>';
			$JSArray5 .= ',"newsletter/messages"';
		}
		
		if ($IM['lm'] == 'y') {
			$LinkDropdown .= '<option value="links"> - '.$MsgText[8][4].'</option>';
			$JSArray5 .= ',"links"';
		}
		
		$LinkDropdown .= '<option value="options"> - '.$MsgText[0][4].'</option>';
		$JSArray5 .= ',"options"';
		
		$LinkDropdown .= '<option value="profile-edit"> - '.$MsgText[0][78].'</option>';
		$JSArray5 .= ',"profile-edit"';
		
		$LinkDropdown .= '<option value="login"> - '.$MsgText[0][192].'</option>';
		$JSArray5 .= ',"login"';
		
		$LinkDropdown .= '<option value="logout"> - '.$MsgText[0][193].'</option>';
		$JSArray5 .= ',"logout"';
		
		$LinkDropdown .= '<option value="register"> - '.$MsgText[0][124].'</option>';
		$JSArray5 .= ',"register"';
		
		if ($IM['pg'] == 'y') {
			$LinkDropdown .= '<option value="photos"> - '.$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="photos/'.$row2['RecordNumber'].'">&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="pages/'.$row2['PageLink'].'">&nbsp;&nbsp;&nbsp;&nbsp; - '.$row2['PageTitle'].' ('.$row2['PageLink'].')</option>';
			$JSArray5 .= ',"pages/'.$row2['PageLink'].'"';
		}





print '<html><head>
<title>Link</title>
<meta name="Description" content="">
<meta name="Keywords" content="">
<meta name="Author" content="DCSun Internet Technologies">
<meta name="Generator" content="DCSun PageBuilder">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
</head>
<body bgcolor="#eeeeee" text="#000000" link="#000000" vlink="#000000" alink="#000000" leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" rightmargin="0" style="margin:0">

<table width="100%" border="0" cellpadding="10" cellspacing="0"><tr><form name="LinkForm" onSubmit="return false;">
<td width="100%" valign="middle">


<img src="spacer.gif" border="0" width="1" height="30" alt=""><br>

<font size="2">

Link Destination:<br><input type="text" name="LinkHREF" size="55" value="http://"><br><img src="spacer.gif" border="0" width="1" height="10" alt="">

<br>Or Select From Existing FlexCMS Pages and Functions:<br><select name="LinkDropdown" onChange="LinkDropdownChanged();">'.$LinkDropdown.'</select>

<br><img src="spacer.gif" border="0" width="1" height="20" alt=""><br>

<br>Link Target:<br><select name="LinkTarget">
	<option value=""> Same Window</option>
	<option value="_blank"> _blank (New Window)</option>
	<option value="_top"> _top</option>
	<option value="_parent"> _parent</option>
	<option value="frame"> Frame - Enter Name -&gt;</option>
</select>
<input type="text" name="LinkTargetFrame" size="20" value=""><br><img src="spacer.gif" border="0" width="1" height="10" alt="">

</font>

<br>



</td>
</tr></table>
<img src="spacer.gif" width="1" height="15" alt="" border="0"><br>
<div align="center"><input type="button" id="ContinueButton" value="Continue" onClick="ContinueClicked();"></div>
</form>



<script>

var LinkMode = window.opener.LinkMode;
var LinkProperties = window.opener.LinkProperties;

if (LinkMode != \'new_html\' && LinkMode != \'new_image\') {
	// existing link

	if (LinkProperties[0].indexOf(\'{FlexMainURL}\') != -1) {
		// if this is a relative FlexCMS link, take out the http://domain that IE forces back into the url value
		document.forms[\'LinkForm\'].elements[\'LinkHREF\'].value = LinkProperties[0].substring(LinkProperties[0].indexOf(\'{FlexMainURL}\'));
	}
	else {
		document.forms[\'LinkForm\'].elements[\'LinkHREF\'].value = LinkProperties[0];
	}
	
	if (LinkProperties[1] != \'\') {
		if (LinkProperties[1] != \'_blank\' && LinkProperties[1] != \'_top\' && LinkProperties[1] != \'_parent\') {
			document.forms[\'LinkForm\'].elements[\'LinkTarget\'].value = \'frame\';
			document.forms[\'LinkForm\'].elements[\'LinkTargetFrame\'].value = LinkProperties[1];
		}
		else {
			document.forms[\'LinkForm\'].elements[\'LinkTarget\'].value = LinkProperties[1];
		}
	}
	

}


function LinkDropdownChanged() {

	if (document.forms[\'LinkForm\'].elements[\'LinkDropdown\'].value != \'\') {
		
		document.forms[\'LinkForm\'].elements[\'LinkHREF\'].value = \'{FlexMainURL}/\' + document.forms[\'LinkForm\'].elements[\'LinkDropdown\'].value + \'.html\';
	
	}

}


function ContinueClicked() {

	// perform error checking on color code
	
	var ErrorFound = true;
	
	ErrorFound = false;	
	

	
	if (ErrorFound) {
		alert (\'Error Message\');	
	}
	else {
		var LinkTarget = \'\';
		if (document.forms[\'LinkForm\'].elements[\'LinkTarget\'].value == \'frame\') {
			LinkTarget = document.forms[\'LinkForm\'].elements[\'LinkTargetFrame\'].value;
		}
		else {
			LinkTarget = document.forms[\'LinkForm\'].elements[\'LinkTarget\'].value;
		}
	
		window.opener.ApplyStyle(\'link\', LinkMode, document.forms[\'LinkForm\'].elements[\'LinkHREF\'].value, LinkTarget);
	}

}

</script>

</body>
</html>';

?>
