<?php
	class KeyRequestComponent extends Component {

		function GetUUIDs() {
		        $uuids[] = 'rpckey_(request|signup)(\.do){0,1}$';
			return $uuids;
		}

		function fillform($field) {
			return (isset($_SESSION[$field])?$_SESSION[$field]:'');
		}

		function validateForm() {
			$_SESSION['firstname'] = db_escape(strip_tags($_POST['firstname']));
			$_SESSION['lastname'] = db_escape(strip_tags($_POST['lastname']));
			$_SESSION['email'] = db_escape(strip_tags($_POST['email']));
			$_SESSION['justification'] = stripslashes(strip_tags($_POST['justification']));
			switch (true)
			{
				case empty($_SESSION['firstname']):
				case empty($_SESSION['lastname']):
				case empty($_SESSION['email']):
				case empty($_SESSION['justification']):
				case ($_POST['TOS'] != 'agree'):
					return false;
					break;
				case !filter_var($_SESSION['email'], FILTER_VALIDATE_EMAIL):
					$_SESSION['email'] = "INVALID ADDRESS";
					return false;
					break;
			}
			$reCaptcha = new reCaptcha();
			return $reCaptcha->verify();
		}

		function displayForm($page) {
			$captcha = new reCaptcha();
			$labelstyle="display:block;float:left;width:150px;margin:2px;";
			$inputstyle="margin:2px;width:450px";
?>
<form name="signup" method="post" action="<?= $page ?>">
	<fieldset>
		<legend>RPC Key Requestform</legend>
		<label for="firstname" style="<?=$labelstyle?>">First Name:</label>
		<input type="text" id="firstname" name="firstname" value="<?= $this->fillform('firstname') ?>" style="<?=$inputstyle?>" /><br/>

		<label for="lastname" style="<?=$labelstyle?>">Last Name:</label>
		<input type="text" id="lastname" name="lastname" value="<?= $this->fillform('lastname') ?>" style="<?=$inputstyle?>" /><br/>

		<label for="email" style="<?=$labelstyle?>">E-mail Address</label>
		<input type="text" id="email" name="email" value="<?= $this->fillform('email') ?>" style="<?=$inputstyle?>" /><br/>

		<label for="justification">Please enter a reason for requesting:</label><br/>
		<textarea id="justification" name="justification" cols="57" rows="10" style="width:600px;margin:2px"><?= $this->fillform('justification') ?></textarea><br/>
		<small>
			If applicable, specify the following details: network name, your nickname, irc:// address and website.
			Also, if you are AS admin, please specify the AS numbers, ipowners can also state their ranges in here.
		</small><br/><br/>

		<p style="text-align: justify;">
			By requesting a key, you certify that the hosts you submit are compromised, or are otherwise a
			verified source of nefarious activity at the time of submission.  Real-time automated submissions
			provide the most reliable reports with the least probability of reporting false positives.  If
			real-time submission is not practical for your application, batch reports shall be submitted no
			more than 24 hours after the aggregation of compromised hosts.  The <?=SITE_NAME?> service MUST NOT
			be used as a method of retribution against trolls or other annoyances, or for any other personal,
			non-security related reasons.
		</p>
		<input type="radio" name="TOS" value="agree" /> I Agree &nbsp; &nbsp;<input type="radio" name="TOS" value="disagree" checked="checked" /> I Disagree<br/><br/>
		<?= $captcha->create(); ?>

		<input type="submit" name="ok" value="Submit request" />
	</fieldset>
</form>
<script type="text/javascript">document.signup.firstname.focus();</script>
<?php
		}

		function Run($page, $matches) {
			global $frameworks_path;
			_page_header(SITE_NAME." :: RPC Key Request");
?>
<h2>RPC Key Request</h2>
<p>
	Please read the <a href="/doc/rpc2">RPC2 documentation</a> and understand it before requesting a key.
	Also note that this is not a contact form. It is a form for requesting RPC keys.
</p>
<?php
			if (isset($_POST['ok']))
			{
				if ($this->validateForm())
				{
					$query = "SELECT id FROM keyrequests WHERE (email='" . $_SESSION['email'] . "') OR (firstname='" . 
						 $_SESSION['firstname'] . "' AND lastname='" . $_SESSION['lastname'] . "' AND requester_ip='" . 
						 USERIP . "') LIMIT 1";
					$dupeCheck = db_num_rows(get_db_rows($query));
					if ($dupeCheck > 0)
					{
?>
<p style="color:orange">You have already submitted a request for a key. Please be patient. If there is a problem, please contact us.</p>
<?php
					}
					else
					{
						$query = "INSERT INTO keyrequests (firstname, lastname, email, justification, requester_ip) VALUES ('" . 
							 $_SESSION['firstname'] . "', '" . $_SESSION['lastname'] . "', '" . $_SESSION['email'] . "', '" . 
							 db_escape($_SESSION['justification']) . "', '" . USERIP . "')";
						get_db_rows($query);
?>
<p style="color:green">Your request has been recorded. Please be patient while we review your request.</p>
<?php
					}
				}
				else
				{
?>
<p style="color:red">Fail. Please complete all fields!</p>
<?php
					$this->displayForm($page);
				}
			}
			else
			{
?>
<p>All fields are required. Feel free to report any issues.</p>
<?php
				$this->displayForm($page);
			}
			_page_footer();
		}
	};

	new KeyRequestComponent;

?>
