<?php

// +----------------------------------------------------------------------+
// | OpenConf                                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2008 Zakon Group LLC.  All Rights Reserved.       |
// +----------------------------------------------------------------------+
// | This source file is subject to the OpenConf License, available on    |
// | the OpenConf web site: www.OpenConf.com                              |
// +----------------------------------------------------------------------+

require_once "../include.php";

printHeader("Reset Password",3);

if (isset($_POST['submit']) && ($_POST['submit'] == "Reset Password") && preg_match("/^\d+$/",$_POST['pid']) && !empty($_POST['email'])) {
	// check for valid email
	if (!validEmail($_POST['email'])) {
		print '<center><span class="warn">Email address entered is invalid</span></center>';
		printFooter();
		exit;
	}
	$q = "SELECT " . OCC_TABLE_AUTHOR . ".email FROM " . OCC_TABLE_PAPER . ", " . OCC_TABLE_AUTHOR . " WHERE " . OCC_TABLE_PAPER . ".paperid='".$_POST['pid']."' AND " . OCC_TABLE_AUTHOR . ".paperid=" . OCC_TABLE_PAPER . ".paperid AND " . OCC_TABLE_AUTHOR . ".position=" . OCC_TABLE_PAPER . ".contactid AND " . OCC_TABLE_AUTHOR . ".email='" . safeSQLstr(oc_strtolower($_POST['email'])) . "'";
	$r = ocsql_query($q) or err("Error checking submission id ".mysql_errno());
	if (mysql_num_rows($r) != 1) { 
		print '<center><span class="warn"><i>We were unable to match the submission id to the contact author\'s email.  Please contact the <a href="mailto:' . $OC_configAR['OC_pcemail'] . '?subject=Unable to Reset Password">Program Chair</a>.</span></center>'; 
		printFooter();
		exit;
	}
	else {
		$newpwd = newPassword();
		$q2 = "UPDATE " . OCC_TABLE_PAPER . " SET password='" . hashPassword($newpwd) . "' WHERE paperid='" . $_POST['pid'] . "'";
		$r2 = ocsql_query($q2) or err("unable to update password");
		$msg = '
Per your request, we have issued you a new password for accessing the ' . $OC_configAR['OC_confName'] . ' OpenConf system.  The new password is:

	' . $newpwd . '

You may change this password by signing in to the OpenConf system and editing your submission.

		';
		sendEmail($_POST['email'], "Author Password Reset", $msg, $OC_configAR['OC_notifyAuthorReset']);
		print 'We have emailed you a new password.<p>';
		printFooter();
		exit;
	}
}
else {
	print "<center><b>Please enter your submission id and the contact author's email below</b></center><p>\n";
}

print '
<center>
<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
<table border=0>
<tr><td><b>Submission ID:</b></td><td><input size=20 name="pid" value="' . varValue('pid', $_POST) . '"></td></tr>
<tr><td><b>Email:</b></td><td><input size=20 name="email" value="' . varValue('email', $_POST) . '"></td></tr>
<tr><th align="center" colspan=2><br><input type="submit" name="submit" value="Reset Password"></th></tr>
</table>
</form>
</center>
<p>
';

printFooter();

?>
