function doResetPWEmail($email, $answer = "") {
global $CONFIG;
global $_LANG;
global $securityquestion;
if (!$email) {
return $_LANG['pwresetemailrequired'];
}
$result = select_query("tblclients", "id,password,securityqid,securityqans", array("email" => $email, "status" => array("sqltype" => "NEQ", "value" => "Closed")));
$data = mysql_fetch_array($result);
$userid = $data['id'];
$password = $data['password'];
$securityqid = $data['securityqid'];
$securityqans = $data['securityqans'];
if (!$userid) {
$result = select_query("tblcontacts", "tblcontacts.id,tblcontacts.userid,tblcontacts.password", array("tblcontacts.email" => $email, "tblcontacts.subaccount" => "1", "tblclients.status" => array("sqltype" => "NEQ", "value" => "Closed")), "", "", "", "tblclients ON tblclients.id=tblcontacts.userid");
$data = mysql_fetch_array($result);
$contactid = $data['id'];
$userid = $data['userid'];
$password = $data['password'];
}
if (!$userid) {
return $_LANG['pwresetemailnotfound'];
}
if ($securityqid) {
$result = select_query("tbladminsecurityquestions", "", array("id" => $securityqid));
$data = mysql_fetch_array($result);
$securityquestion = decrypt($data['question']);
if (!$answer) {
return "";
}
if ($answer != decrypt($securityqans)) {
return $_LANG['pwresetsecurityquestionincorrect'];
}
}
$resetkey = md5($userid . rand(100000, 999999) . $password);
if ($contactid) {
update_query("tblcontacts", array("pwresetkey" => $resetkey, "pwresetexpiry" => time() + 2 * 60 * 60), array("id" => $contactid));
}
else {
update_query("tblclients", array("pwresetkey" => $resetkey, "pwresetexpiry" => time() + 2 * 60 * 60), array("id" => $userid));
}
$reseturl = ($CONFIG['SystemSSLURL'] ? $CONFIG['SystemSSLURL'] : $CONFIG['SystemURL']);
$reseturl .= "/pwreset.php?key=" . $resetkey;
sendMessage("Password Reset Validation", $userid, array("pw_reset_url" => $reseturl, "contactid" => $contactid));
logActivity("Password Reset Requested", $userid);
}