<?php

	// we will show the login fields unless we are not installed yet
	$login_style = "";
	
	// check our database setup in case the user hasn't installed it yet
	if ((!$db_connected)||(!$db_selected)) {
		// if we can't connect to the database, display a message to fix it
		set_feedback(0, EDITOR_NAME." is unable to connect to your database. Please check your connection details in config/constants.php.");
		$login_style = "display: none;";
	} else {
		// if the database hasn't been installed yet, display a link to install it
		$query = "SHOW TABLES LIKE 'content'";
		$result = db_do_query($query);
		$count = db_num_rows($result);
		if (!$count) {
			set_feedback(1, "Welcome to ".EDITOR_NAME.". <a href=\"$editor_index?page=install\">Click here</a> to install the database tables.");
			$login_style = "display: none;";
		} else {
			// if the admin password hasn't been set yet, display a message to set it
			$query = "SELECT ID FROM user WHERE name='admin' AND password=md5('')";
			$result = db_do_query($query);
			$count = db_num_rows($result);
			if ($count) {
				set_feedback(1, "The database tables have been installed. You can now log in as \"admin\" with no password. However, please set a password for the \"admin\" user as soon as you log in.");
			}
		}
	}
	
	// if we have any feedback to show, get it now; but remove the ERROR text since we're probably just doing the initial setup here
	$feedback = str_replace("ERROR: ", "", get_feedback());

?>

<html>

	<head>
		<title><?php print EDITOR_NAME; ?></title>
		<?php eval(include_namespace(HEAD_FILE)); ?>
	</head>
	
	<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
	
		<?php eval(include_namespace(HEADER_FILE)); ?>

		<form name="login" method="post" action="<?=$editor_index?>?page=login_submit">
		
			<div align="center">
		
				<div id="login_box" align="left">
		
					<p class="title"><?php print EDITOR_NAME ?></p><br />
				
					<?php print $feedback; ?>
					
					<div style="<?php print $login_style; ?>">

						<p class="heading">Login</p>
					
						<p class="body stack_left">Username<br />
						<input type="text" name="username_input" size="16" class="form"></p>
						
						<p class="body stack_left">Password<br />
						<input type="password" name="password_input" size="16" class="form"></p>
						
						<p class="body stack_left"><br /><input type="submit" value="Submit" class="form"></p>
						
						<div class="reset"></div>
					
					</div>
						
				</div>
				
			</div>

		</form>

		<?php eval(include_namespace(FOOTER_FILE)); ?>

	</body>
	
</html>