<?php 
ini_set('display_errors', 0);
error_reporting(E_ALL & ~E_DEPRECATED);
$form_name = "";
$table_name = "custom_participant_registration";
$primary_key = "reg_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";

include_once("includes/sha256.php");
include_once("includes/config.php");
include_once("includes/common_functions.php");
require_once("includes/class.chip_password_generator.php");
require_once("includes/phpmailer/class.phpmailer.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";
?>
<style>
	.content_bg
	{
		border-radius: 0px;
		background-color: #ffffff;
	}
	#frm_display
	{
		background: url("includes/images/loginbackground.jpg") no-repeat scroll 240px 40px transparent; 
		height:400px;
		text-align:center;
	}
	.form
	{
		margin-left: 38%;
		/*margin-top: 55px;
		position: inherit;*/
		padding-top: 95px;
		text-align: right;
		width: 35%;
	}
</style>
<?php
switch($mode)
{
	case "submit":
		$qry = "SELECT * FROM lime_users WHERE users_name = '".$_REQUEST['username']."' AND status = 'Y'";
		$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		$msg = "";
		
		if(count($rec) != 0)
		{
			if($_REQUEST['email'] == $rec[0]['email'])
			{
				$length = 8;
				$alpha_upper_include = TRUE;
				$alpha_lower_include = TRUE;
				$number_include = TRUE;
				$symbol_include = FALSE;
				
				$args = array(
							'length'				=>	$length,
							'alpha_upper_include'	=>	$alpha_upper_include,
							'alpha_lower_include'	=>	$alpha_lower_include,						
							'number_include'		=>	$number_include,
							'symbol_include'		=>	$symbol_include,	
				);
				$object = new chip_password_generator($args);
				$password = $object->get_password();
				
				$query = "SELECT * FROM custom_settings";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$smtp_rec = $common->sql->record;
				
				$host = $smtp_rec[0]['smtp_server_name'];
				$port = $smtp_rec[0]['smtp_server_port'];
				$user = $smtp_rec[0]['smtp_server_usrname'];
				$from = $smtp_rec[0]['smtp_frm_mail'];
				$ssl = $smtp_rec[0]['smtp_server_ssl'];
				$auth = $smtp_rec[0]['smtp_server_auth'];
				$pswd = base64_decode($smtp_rec[0]['smtp_server_pwd']);
				$rprtr_mail = $_REQUEST['email'];
				$username = $_REQUEST['username'];
				
				$emailmethod = "smtp";
				$emailsmtphost = $host;
				$emailsmtpport = $port;
				$emailsmtpuser = $user;
				$emailsmtppassword = $pswd;
				$emailsmtpssl = $ssl;
				$emailsmtpauth = $auth;
				$defaultlang = "";
				$emailsmtpdebug = 2;
				
				$path = "http://".$_SERVER['HTTP_HOST']."/".DOC_ROOT;
				$path = WWW_DOC_ROOT;
				
				$modmessage = "Dear ".$username.",<br /><br /> The Generated Password for Your ID is <b>".$password."</b><br /><br />\n With Warm Regrds, <br />Portal Admin<br /><a href='".$path."'>Portal Link</a>";
				$modsubject = 'Forgot Password Mail';
				$to = $rprtr_mail;
				$sitename = "";
				$ishtml = true;
				$customheaders = "";
				
				$oMail = new PHPMailer();
				$mail_sts = $common->SendEmailMessage($oMail, $modmessage, $modsubject, $to , $from, $sitename, $ishtml, null, null, $customheaders);
				
				if($mail_sts)
				{
					$password = md5($password);
					$upd_qry = "UPDATE lime_users SET password = '".$password."' WHERE users_name = '".$_REQUEST['username']."' AND status = 'Y'";
					//$common->sql->exeQuery($upd_qry);
					
					$_SESSION['msg'] = 'Password has been sent to your Email';
				}
				else
					$_SESSION['msg'] = $oMail->ErrorInfo;
				
				header("location:".$_SERVER['HTTP_REFERER']);
			}
			else
			{
				echo '<div class="content_bg">';
					$msg = "Email does not exists";
					include("add.php");
				echo '</div>';
			}
		}
		else
		{
			echo '<div class="content_bg">';
				$msg = "Username does not exists";
				include("add.php");
			echo '</div>';
		}
	break;
	
	default:
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>