<?php 
include("user_func.php");

function sendWelcomeEmail($userName, $email)
{
	$coyId 			= SableDBFunctions::getCompanyCodeFromEmail($email);
	$file 			= $coyId ? "email_employee.html" : "email_graduate.html";
	$emailContent 	= "<p>Dear ".$userName.",</p>" . file_get_contents(__DIR__.'/'.$file);
	SableSystem::sendEmail($email, "Welcome to the BITS-Integrated CSI-System", $emailContent);			
} /* function sendConfirmationLink() */	

$loginLink = SableSystem::getSableLinkPath('login/index.php');		
$msg = "Your registration could not be confirmed. ".
		"The link may possibly have been corrupted or this account may have already been activated. ".
		"Please try to <strong><a href='".$loginLink."'>login</a></strong> or contact support for assistance.";
$hdr = '<h3 class="title"><i class="fa fa-frown" aria-hidden="true"></i>&nbsp;Oops...</h3>';
$userHash = base64_decode($_SESSION['caller']['args'][1]);

$sql = 'select user_request_id, user_code from user__request where hash = "'.$userHash.'" and expires is not null';
$row = $sableDB->getRow($sql);
$userCode 	= $row['user_code'];
$requestId 	= $row['user_request_id'];
if ($userCode)
{
	$email		= SableDBFunctions::getUserEmailFromCode($userCode);
	$userName	= SableDBFunctions::getUserDisplayName($email);	
	$now = date('Y-m-d H:i:s');
	$sql = "update user set user_active = 1, activate_date = '".$now."' where user_code = '" . $userCode . "'";
	$result = $sableDB->updateQuery($sql);
	if ($result)
	{
		$hdr = '<h3 class="title"><i class="fa fa-smile-beam" aria-hidden="true"></i>&nbsp;Welcome</h3>';
		$msg = "Thank you for confirming your registration <strong>".$userName."</strong>.<p>".
		"Please <strong><a href='".$loginLink."'>login</a></strong> with your user name and password to access the system.</p>";
		$res = $sableDB->updateQuery('update user__request set expires = NULL where user_request_id = '.$requestId);
		//sendWelcomeEmail($userName, $email);
	}
}
?>
<!--// header -->
<form id="Login" name="Login">
	<div class="forms">
        <p></p>
        <div class="container forms">
            <div class="row forms-grids">
                <div class="col-md-12 forms-grid">
                <?php echo($hdr); ?>
                </div> 
            </div>

            <div class="row forms-grids">
                <div class="col-md-12 forms-grid">
                    <?php echo($msg); ?>
                    <p></p>
                </div>
            </div>
        </div>
	</div>
 </form>