<?php
	class LoginComponent extends Component {
		function GetUUIDs() {
			$uuids[] = 'admin\/login$';
			return $uuids;
		}

		function ShowLoginForm() {
			if (isset($_SESSION['needlogin'])) {
?>
<div style="background-color: #F5F6BE; border: #96969D 1px solid; font-weight: bold; font-size: 75%; padding: 6px;">
	<img alt="info" src="/images/info.png" />&nbsp;&nbsp; To continue, you must log into the administration interface.
</div>
<?php
				unset($_SESSION['needlogin']);
			}
?>
<h1>Login</h1>
<p>To log in, enter your assigned username and password.</p>
<p>If you have a <a href="/rpckey_signup" title="Signup for a key">key</a> but no login, <a href="/admin/register">register here</a>.</p>
<form method="post" action="/admin/login">
	<input type="hidden" name="ref" value="<?=(isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'')?>" />
	<div>
		<p>
			<input type="text" name="account" placeholder="Enter your username" /><br />
			<input type="password" name="password" placeholder="Enter your password" />
		</p>
		<input type="submit" value="Login" />
	</div>
</form>
<?php
		}

		function Run($page, $matches) {
			if (isset($_POST['account']) && isset($_POST['password']))
			{
				if (_process_site_login($_POST['account'], $_POST['password']))
				{
					if (isset($_POST['ref']) && preg_match('/:\/\/'.$_SERVER['HTTP_HOST'].'/', $_POST['ref']))
					{
						if (!preg_match('/\/login$/',$_POST['ref']))
						{
							header("Location: ".$_POST['ref']);
							return;
						}
					}
                                       	header("Location: /admin");
				}
				else
				{
					_page_header(SITE_NAME." :: Admin :: Failed Login");
					$this->ShowLoginForm();
					_page_footer();
				}
			}
			else
			{
				_page_header(SITE_NAME." :: Admin :: Login");
				$this->ShowLoginForm();
				_page_footer();
			}
		}

	};

	new LoginComponent;
?>
