<?PHP
	require_once('appconfig.php');

	ini_set('date.timezone','Asia/Manila');
	
	#region validate inactive account
	function check_activity($usrcde)
	{
		global $pdo,$xg_appkey;
		$xbool = true;
		$xholdunusedacc = getpar('baselinesecurityfile','holdunusedacc');
		$xdate = strtotime( '-'.$xholdunusedacc.' day', strtotime('now') );
		
		$xqry = "SELECT * FROM users WHERE usrstat = 'inactive' AND usrcde = ?";
		$stmt = $pdo->prepare($xqry);
		$stmt->execute(array($usrcde));
		
		while ($rs = $stmt->fetch())
		{
			$xadddte = strtotime($rs['adddte']);
			if( $xadddte < $xdate )
			{
				$xqry_upd = "UPDATE users SET usrstat='deactivated' WHERE id=?";
				$stmt_upd = $pdo->prepare($xqry_upd);
				$stmt_upd->execute(array($rs['id']));
				UserActivityLog("Bank User","Deactivate user ID:".$rs['id']." USER CODE : ".$rs['usrcde'],$PHP_SELF);
				msgbox('Your Account has been deactivated. Please contact your system administrator.');
				$_SESSION[$xg_appkey]['usrcde'] = '';
				$xbool = false;
			}
		}
		
		return $xbool;
	}
	#end
	
	#region initial login / Successful Log
	if (validate_initial_log())
	{
		$xqry_chkreset = "SELECT * FROM user_activities WHERE remarks like ?";
		$stmt_chkreset = $link_id->prepare($xqry_chkreset);
		$stmt_chkreset->execute(array('Reset Password%'.$_SESSION[$xg_appkey]['usrcde']));
		
		if ($rs_chkrest = $stmt_chkreset->fetch())
		{
			msgbox('Your password has been reset. You are required to change your password.');
		}
		else
		{
			msgbox('Initial Login. You are required to change your password.');
		}
		echo "<script type='text/javascript'>window.open('v_expired_changepass.php','_self')</script>";
	}
	function validate_initial_log()
	{
		global $link_id_main,$xg_appkey;
		$xqry_initlog = "SELECT * FROM users WHERE usrcde = ?";
		$stmt_initlog = $link_id_main->prepare($xqry_initlog);
		$stmt_initlog->execute(array($_SESSION[$xg_appkey]['usrcde']));
		$xretbool = false;
		
		if (basename($_SERVER['REQUEST_URI']) != 'v_expired_changepass.php')
		{
			if ($rs_initlog = $stmt_initlog->fetch())
			{
				if (trim($rs_initlog['initlog']) == 1)
				{
					$xretbool = true;
				}
			}
		}
		
		return $xretbool;
	}
	#end
	
	#region count numeric
	function numeric_count($str)
	{
		$words = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
		$i = 0;
		
		foreach ($words as $word)
		{
			if (is_numeric($word))
			{
				$i++;
			}
		}

		return $i;
	}
	#end
	
	#region count uppercase
	function upper_count($str)
	{
		$words = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
		$i = 0;
		
		foreach ($words as $word)
		{
			if (!is_numeric($word))
			{
				if (strtoupper($word) === $word)
				{
					$i++;
				}
			}
		}

		return $i;
	}
	
	#end
	
	#region count lowercase
	function lower_count($str)
	{
		$words = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
		$i = 0;
		
		foreach ($words as $word)
		{
			if (!is_numeric($word))
			{
				if (strtolower($word) === $word)
				{
					$i++;
				}
			}
		}

		return $i;
	}
	#end
	
	#region check password
	function validate_password($xpw)
	{
		
		global $xg_appkey,$link_id_main;
		$xsql_sec = "SELECT * FROM baselinesecurityfile";
		$stmt_sec = $link_id_main->prepare($xsql_sec);
		$stmt_sec->execute();
		$xrow_sec=$stmt_sec->fetch();
		
		$xmsg = '';
		$xpw_count = strlen($xpw);
		$xminlenpw = $xrow_sec['minlenpw'];
		$xmaxlenpw = $xrow_sec['maxlenpw'];
		$xminnumlcl = $xrow_sec['minnumlcl'];
		$xminnumucl = $xrow_sec['minnumucl'];
		$xminnumnc = $xrow_sec['minnumnc'];
		$xallowpu = $xrow_sec['allowpu'];
		$xbool = false;
		$msg = "Required length password not acquired";
		
		$xmsg = '';
		if ( $xminlenpw <= $xpw_count && $xpw_count <= $xmaxlenpw )
		{
			$xbool = true;
		}

		if ($xbool)
		{
			$xupper_count = upper_count($xpw);
			$xbool = false;
				$msg = "Required Uppercase character not acquired";
			if ( $xminnumucl <= $xupper_count )
			{
				$xbool = true;
			}
			
		}
		if ($xbool)
		{
			$xlower_count = lower_count($xpw);
			$xbool = false;
			$msg = "Required Lowercase character not acquired";
			if ( $xminnumlcl <= $xlower_count )
			{
				$xbool = true;
				
			}
		}
		if ($xbool)
		{
			$xnumeric_count = numeric_count($xpw);
			$xbool = false;
			$msg = "Required numeric character not acquired";
			if ( $xminnumnc <= $xnumeric_count )
			{
				$xbool = true;
			}
		}
		if ($xbool)
		{
			if ( $xallowpu == 0)
			{
				$xbool = false;
				$msg = "Same username and password is not allowed.";
				if ( $xpw != $_SESSION[$xg_appkey]['usrcde'] )
				{
					$xbool = true;
				}
			}
		}
		
		if(!$xbool)
		{
			$xmsg = $msg;
		}
		
		return $xmsg;
	}
	#end
	
	#region password lifetime
	function get_pwlifetime()
	{
		global $link_id_main;
		$xsql_sec = "SELECT * FROM baselinesecurityfile";
		$stmt_sec = $link_id_main->prepare($xsql_sec);
		$stmt_sec->execute();
		$xrow_sec=$stmt_sec->fetch();
		$xdate_now = date('Y-m-d',strtotime('now'));
		$xdate_generated = date('Y-m-d',strtotime( '+'.$xrow_sec['pwlifetime'].' day', strtotime($xdate_now) ));
		
		return $xdate_generated;
	}
	#end
	
	#region password reminders
	if ( $_SESSION[$xg_appkey]['expired_password'] && trim($_SESSION[$xg_appkey]['usrcde']) != '' )
	{
		password_reminders();
	}
	function password_reminders()
	{	
		global $xg_appkey;
		$xdate_now = date('Y-m-d',strtotime('now'));
		$xdate_expired = $_SESSION[$xg_appkey]['date_expired'];
		
		$xdate_validated = strtotime( '-'.getpar('baselinesecurityfile','rpcbe').' day', strtotime($xdate_expired) );
		if (strtotime('now') >= $xdate_validated)
		{
			if ( strtotime($xdate_expired) <= strtotime('now') )
			{
				if (basename($_SERVER['REQUEST_URI']) != 'v_expired_changepass.php')
				{
					// echo "<script type='text/javascript'>window.open('stdUserPassword.php','_self')</script>";
					echo "<script type='text/javascript'>window.open('v_expired_changepass.php','_self')</script>";
					$_SESSION[$xg_appkey]['expired_password'] = true;
				}
			}
			else
			{
				if (basename($_SERVER['REQUEST_URI']) != 'v_expired_pass.php' )
				{
				// echo "<script type='text/javascript'>alert('Your Password will expired within 5 Days.');</script>";
					echo "<script type='text/javascript'>window.open('v_expired_pass.php','_self')</script>";
				}
			}
		}
		else
		{
			echo "<script type='text/javascript'>";
			echo "window.open('sysMenu.php','_self','scrollbars=yes,location=yes, menubar=yes, toolbar=yes,height=600,width=1020,top=0')";
			echo '</script>';
		}
		// exit(0);
	}
	#end
	
	#region for time out
	function process_timeout()
	{
		global $link_id_main,$xg_appkey;
		$xsql_sec = "SELECT * FROM baselinesecurityfile";
		$stmt_sec = $link_id_main->prepare( $xsql_sec );
		$stmt_sec->execute();
		$xrow_sec=$stmt_sec->fetch();
		$timeout =$xrow_sec['autologout']*60;
		// *60
		// $timeout = 60; // Number of&nbsp;seconds until it times out.

		// Check if the timeout fieldexists.

		if(isset($_SESSION[$xg_appkey]['timeout'])) {
			// See if the&nbsp;number of seconds since the&nbsp;last

			// visit is larger than the timeout period.

			$duration = time() - (int)$_SESSION[$xg_appkey]['timeout'];

			if($duration > $timeout) {

				//&nbsp;Destroy the session and restart&nbsp;it.
				if (trim($_SERVER['SCRIPT_NAME']) !=  dirname($_SERVER['REQUEST_URI'])."/sysLogin.php")
				{
					echo "<script type='text/javascript'>";
					echo "alert('The system has been idle for too long, you need to Login again.')";
					echo '</script>';
				}
				$_SESSION[$xg_appkey]['usrcde'] = '';
			}
		}
		 
		// Update the timout field with the current time.
		$_SESSION[$xg_appkey]['timeout'] = time();
	}
	#end
?>
