#!/usr/bin/perl -Tw -I/usr/local/positive/Perl -I. 

use strict;
use CGI qw/:standard/;
use APR::Request::Cookie;
use MIME::Base64;
use POSIX ();
use Pos::Webtop::webtopFormBuilder;
use Digest::MD5::File qw(file_md5_hex);
use Pos::Webtop::OcxLauncher;

my $wtfb = webtopFormBuilder->new();
my $tb = Pos::Webtop::Toolbox->new();
my $client = $tb->getClient();

my $cgi = CGI->new;

my $conf   = new Config::General("/usr/local/positive/conf/PosPerl.conf");
my %config = $conf->getall;

my $vpnConf = new Config::General('/usr/local/positive/conf/VpnDownload.conf');
my %vpnConfig = $vpnConf->getall;
my $thisUrl   = $config{thisUrl};

my $q = $cgi->param('q');
$q = "" unless( defined( $q ) && length( $q ) );

my $mode = $cgi->param('m');
$mode = "" unless( defined( $mode ) && length( $mode ) );

my $si = $wtfb->getClientSessionId();
if( length( $mode ) && $si == 0 || length( $si ) == 0 ) {
	exitNow();
}

$client->setRestrictionSetId( tb=>$tb );

#if( defined( $q ) && ! defined( $f ) ) {
#	print "Content-Type: text/html\nCache-Control: no-cache\n\n
#
#               <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
##               <head>
#               <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
#               <meta http-equiv=refresh content='0; URL=core.pl?q=$q'>
#               </head>
#               </html>
#	";
#
#	exit();
#}

my $remoteAddr = $ENV{'REMOTE_ADDR'};

sub showSignonMessage
{
	my $db = $tb->getWebtopDbWriter();

	my $restrictionSetId = $client->setRestrictionSetId(tb=>$tb);
	my $userId = $client->getUserIdDB( db=> $db );

	my( @msg ) = $db->query( sql => "
					SELECT
						title_text, message_text
					FROM
						pol_signon_messages AS psm
						JOIN user_signon_message AS usm ON psm.pol_signon_message_id = usm.pol_signon_message_id
					WHERE user_id = $userId AND restrict_set_id = $restrictionSetId
					" );

	my( $cnt ) = $db->query( sql=> " 
					SELECT COUNT(1)
						FROM client_sessions 
						WHERE captive_portalized = 1 AND client_session_id = $si
					" );

	if( scalar( @msg ) && $cnt > 0 ) {
		my( $title, $body) = @{ $msg[0] };

		$body = HTML::Entities::encode($body);
		$body =~ s/_BR_/<br>/g;

print "
		<div class='ot-modal ot-modal--is-active' id='mfdiv' style='z-index: 1501;'>
			<div class='ot-modal__background'>
		  	</div>
		  	<div class='ot-modal__content'>
		    		<div class='ot-modal__header'>
		      			<p class='title'>
						<input type='text' disabled class='ot-input' name='mt7' id='mt7' value='${title}'>
					</p>
		    		</div>
		    		<div class='ot-modal__body'>
		      			<div class='ot-m-1' style='background: #white; width: 600px; height: 300px;'>
						<div class='ot-form-control'>
							<div id='msgDiv' class='ot-form-control'>
								<div class='text'>
								$body
								</div>
							</div>
						</div>
		      			</div>
		    		</div>
		    		<div class='ot-modal__footer'>
		      			<div class='ot-btn-group'>
						<button class='ot-btn ot-btn--is-primary' onclick='javascript:uff(4);'>
							OK
						</button>
						<button class='ot-btn ot-btn--is-secondary' onclick='javascript:sdc(1);q();'>
			  				Sign out
						</button>
		      			</div>
					<p hidden>
						<input size='0' type='password' name='q6' id='q6' value=$config{'webtopSaveCaptivePortalized'}>
						<!--<input size='0' type='password' name='q100' id='q100' value=$config{'webtopSignoff'}>-->
					</p>
    				</div>
  			</div>
		</div>
";
	}

	if( ! scalar( @msg ) ) {
		$db->do( sql=> "UPDATE client_sessions SET captive_portalized = 0 WHERE client_session_id = $si" );
		$db->commit();
	}
}

sub checkCaptivePortalized
{
	showSignonMessage();
}

sub touchSession
{
	my $db = $tb->getWebtopDbWriter();

	$db->do( sql=> "
			UPDATE client_sessions SET touch_time = NOW()
				WHERE user_id = " . $client->getUserIdDB( db=> $db ) 
			);
	$db->commit();
}
	
sub exitNow
{
	my($issue) = @{{@_}}{qw/issue/};

	warn "$config{'webtopRelease'}:$remoteAddr:exitNow():No session\n";
	my $q = $config{'webtopTimedout'};
	my $s = $config{'webtopLanding'};

	print "Content-Type: text/html\nCache-Control: no-cache\n\n
 
		<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
               	<head>
               	<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
               	<meta http-equiv=refresh content='0; URL=loginwt.pl?q=$q&s=$s'>
               	</head>
               	</html>
        ";
               
	exit();
}

sub strim
{
	my($str) = @{{@_}}{qw/str/};

	!defined $str and return undef;
	$str =~ s/^\s+//;
	$str =~ s/\s+$//;

	return $str;
}

sub deleteSession()
{
	my $db = $tb->getWebtopDbWriter();

	if( length( $si ) ) {
		$db->do( sql=> "DELETE FROM client_sessions WHERE client_session_id = $si" );
		$db->commit();
	}
}

#
# End utilities
#

sub showRecent
{
        my $db = $tb->getWebtopDbWriter();

print "
	<div class='ot-list-tile ot-list-tile--is-simple' style='height: calc( 100vh - 150px );'>   
		<div class='ot-list-tile__header'>
	      		<div class='ot-list-tile__title'>Recent</div>
			<div id='c1' style='display: block; bottom: 10;' class='ot-form-control'>
				<!--
				<img src='LocalWebLib/ipsecDevice.svg' style='height: 24px; width: 24px' onclick='javascript:sf(4,0,4);' title='Click to view loaded content'> 
				-->
			</div>
		</div>
	
		<div class='ot-list-tile__body'>
		        <div class='ot-panel__body'>
				<div id='r0' style='display: block; text-align: center' class='ot-form-control'>
					<div style='width: 100%; height: 50px; opacity: 1;'></div>

					<img src='LocalWebLib/ill_workspace_xecm220.svg' style='height: 152px; width: 152px'>
                			<div class='text'>
                        			<p style='color: black;'>
                                			<span>Nothing recent to show</span>
                        			</p>
					</div>
				</div>
				<div id='r1' style='display: none;' class='ot-form-control'>
                			<div class='text'>
						<ul id='r1ul' class='ot-list ot-list--is-very-high-density'>

						</ul>
					</div>
				</div>
                	</div>
		</div>
	</div>
";
}

sub showBookmark
{
	my( $restrictionSetId ) = @{{@_}}{qw/restrictionSetId/};

print "
	<div class='ot-list-tile ot-list-tile--is-simple' style='height: calc( 100vh - 150px );'>   
		<div class='ot-list-tile__header'>
	      		<div class='ot-list-tile__title'>Bookmarks</div>
		</div>
		
		<div class='ot-list-tile__body'>
			<div id='basic-panel' class='ot-panel'>
        			<div class='ot-panel__header'>
					<ul class='ot-tabs'>
						<li id='tab0' class='ot-tabs__item ot-tabs__item--is-active'>
							<a onclick='javascript:btab(0);'>Corporate</a>
  						</li>
  						<li id='tab1' class='ot-tabs__item'>
							<a onclick='javascript:btab(1);'>Personal</a>
  						</li>
					</ul>
				</div>
			</div>
		        <div class='ot-panel__body'>
				<div id='cbm' style='display: block;' class='ot-form-control'>

				</div>
				<div id='pbm' style='display: none;' class='ot-form-control'>

				</div>
			</div>
		</div>
	</div>
";

	my $cbm = $config{'webtopGetCorporateBookmark'};
	my $pbm = $config{'webtopGetPersonalBookmark'};

print "
	<script type='text/javascript'>
		ws('$pbm',1,$restrictionSetId,0);
		ws('$cbm',3,$restrictionSetId,0);
	</script>
";

	showBookmarkGroupModal();
	showBookmarkModal();
}

sub showFileshare
{
	my( $restrictionSetId ) = @{{@_}}{qw/restrictionSetId/};

print "
	<div class='ot-list-tile ot-list-tile--is-simple' style='height: calc( 100vh - 150px );'>   
		<div class='ot-list-tile__header'>
	      		<div class='ot-list-tile__title'>Fileshares</div>
		</div>
		
		<div class='ot-list-tile__body'>
			<div id='basic-panel' class='ot-panel'>
        			<div class='ot-panel__header'>
					<ul class='ot-tabs'>
  						<li id='tab2' class='ot-tabs__item ot-tabs__item--is-active'>
							<a onclick='javascript:ftab(0);'>Corporate</a>
  						</li>
  						<li id='tab3' class='ot-tabs__item'>
							<a onclick='javascript:ftab(1);'>Personal</a>
  						</li>
					</ul>
				</div>
			</div>
		        <div class='ot-panel__body'>
				<div id='cfs' style='display: block;' class='ot-form-control'>

				</div>
				<div id='pfs' style='display: none;' class='ot-form-control'>

				</div>
			</div>
		</div>
	</div>
";

	my $cfs = $config{'webtopGetCorporateFileshare'};
	my $pfs = $config{'webtopGetPersonalFileshare'};

print "
	<script type='text/javascript'>
		ws('$pfs',2,$restrictionSetId,0);
		ws('$cfs',4,$restrictionSetId,0);
	</script>
";

	showFileshareModal();
}

sub showStopModal
{
print "
	<div class='ot-modal' id='hdiv' style='z-index: 1500; width: 500px; height: 400px;'>
		<div class='ot-modal__content'>
			<div class='ot-modal__header'>
				<p class='title'>
					<input type='text' disabled class='ot-input' value='WARNING'>
				</p>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 400px; height: 300px;'>
					<div class='ot-form-control'>
						<div class='text'>
							You are signed in elsewhere, please close this tab.
						</div>
					</div>
				</div>
			</div>
  		</div>
	</div>
";
}

sub showDisconnectModal
{
print "
	<div class='ot-modal' id='dcdiv' style='z-index: 2500;'>
		<div class='ot-modal__body'>
			<div class='ot-m-1' style='background: #white; width: 400px; height: 325px;'>
				<div class='ot-section ot--has-bg-grey-lightest'>
					<div class='ot-progress-container'>
						<div id='ot-spinner' class='ot-progress ot-progress--is-spinner ot-progress--is-spinner-has-text'>
							<div class='ot-progress__outer'>
								<div class='ot-progress__meter'>
								</div>
							</div>
							<div class='ot-progress__text'>One moment, disconnecting...</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
";
}

sub showPreDiagnostic
{
print "
	<div class='ot-modal' id='pdiagdiv' style='z-index: 4000'>
		<div class='ot-modal__body'>
			<div class='ot-m-1' style='background: #white; width: 400px; height: 300px;'>
				<div class='ot-section ot--has-bg-grey-lightest'>
					<div class='ot-progress-container'>
						<div id='ot-spinner' class='ot-progress ot-progress--is-spinner ot-progress--is-spinner-has-text'>
							<div class='ot-progress__outer'>
								<div class='ot-progress__meter'>
								</div>
							</div>
							<div class='ot-progress__text'>One moment, checking...</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
";
}

sub showDiagnostic
{
	showPreDiagnostic();

print "
	<div id='diagDiv'>
	</div>
";
}

sub showAclModal
{
print "
	<div id='aclDiv'>
	</div>
";
}

sub showIdleTimoutModal
{
print "
	<div id='itoDiv'>
	</div>
";
}

sub showBookmarkGroupModal 
{
print "
	<div class='ot-modal' id='bgfdiv' style='z-index: 1500;'>
		<div class='ot-modal__background'>
		</div>
		<div class='ot-modal__content'>
			<div class='ot-modal__header'>
				<div class='ot-level'>
					<div class='ot-level__left'>
						<div class='ot-level__item'>
							<p class='title'>
								<input type='text' disabled class='ot-input' name='mt3' id='mt3'>
							</p>
						</div>
					</div>
					<div class='ot-level__center'>
						<div class='ot-level__item'>
							<div style='width: 450px; height: 1px; opacity: 1;'></div>
						</div>
					</div>
					<div class='ot-level__right'>
						<div class='ot-level__item'>
							<div id='dgbm' style='display: none;' onclick='javascript:rff(2);'>
								<img src='LocalWebLib/action_delete32.svg' style='height: 19px; width: 19px;' title='Delete bookmark group'>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 300px; height: 150px;'>
					<div class='ot-form-control'>
						<div id='bgNameDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='bgName'>
								<div class='ot-form-control__required-indicator'>*</div>
								Group 
							</label>

							<div class='ot-form-control__input-group'>
								<input placeholder='(e.g., Training)' type='text' class='ot-input' name='bgName' id='bgName' style='width: 15em' maxlength='100' size='50'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Group is a required field
							</p>
						</div>
					</div>
				</div>
			</div>
			<div class='ot-modal__footer'>
				<div class='ot-btn-group'>
					<button class='ot-btn ot-btn--is-primary' onclick='javascript:okay(2);'>
						Save
					</button>
					<button class='ot-btn ot-btn--is-secondary' onclick='javascript:cmsg();sf(0,2,3);'>
						Cancel
					</button>

					<p hidden>
						<input size='0' type='password' name='z' id='z' value='0'>
						<input size='0' type='password' name='zz' id='zz' value='0'>
						<input size='0' type='password' name='q3' id='q3' value=$config{'webtopSaveBookmarkGroup'}>
						<input size='0' type='password' name='qq3' id='qq3' value=$config{'webtopDeleteBookmarkGroup'}>
						<input size='0' type='password' name='q5' id='q5' value=$config{'webtopGetBookmarkGroup'}>
					</p>
				</div>
			</div>
		</div>
	</div>
";
}

sub showBookmarkModal
{
print "
	<div class='ot-modal' id='bfdiv'>
		<div class='ot-modal__background'>
		</div>
		<div class='ot-modal__content'>
			<div class='ot-modal__header'>
				<div class='ot-level'>
					<div class='ot-level__left'>
						<div class='ot-level__item'>
							<p class='title'>
								<input type='text' disabled class='ot-input' name='mt1' id='mt1'>
							</p>
						</div>
					</div>
					<div class='ot-level__center'>
						<div class='ot-level__item'>
							<div style='width: 450px; height: 1px; opacity: 1;'></div>
						</div>
					</div>
					<div class='ot-level__right'>
						<div class='ot-level__item'>
							<div id='dbm' style='display: none;' onclick='javascript:rff(0);'>
								<img src='LocalWebLib/action_delete32.svg' style='height: 19px; width: 19px' title='Delete bookmark'>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 400px; height: 270px;'>
					<div class='ot-form-control'>
						<div id='bmGroupDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='bmGroup'>
								<div class='ot-form-control__required-indicator'>*</div>
								Group
							</label>

							<div class='ot-form-control__input-group'>
								<div id='gbdd' style='display: block;' class='ot-form-control'>

								</div>

								<span class='ot-form-control__error-icon'>
								</span>

								<div style='width: 1em; height: 1em; opacity: 1;'></div>
		
								<div id='' role='group' class='ot-btn-group' onclick='javascript:sf(1,0,3);'>
								<img src='LocalWebLib/action_add32.svg' style='height: 19px; width: 19px' title='Create bookmark group'>
								</div>
							</div>
							<p class='ot-form-control__error-text'>
								Group is a required field
							</p>
						</div>
						<div id='bmNameDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='bmName'>
								<div class='ot-form-control__required-indicator'>*</div>
								Name
							</label>

							<div class='ot-form-control__input-group'>
								<input placeholder='(e.g., Compliance)' type='text' class='ot-input' name='bmName' id='bmName' style='width: 15em' maxlength='30' size='50'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Name is a required field
							</p>
						</div>

						<div id='bmUrlDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='bmUrl'>
								<div class='ot-form-control__required-indicator'>*</div>
								URL
							</label>		
							<div class='ot-form-control__input-group'>
								<input placeholder='https://<website>' type='text' class='ot-input' name='bmUrl' id='bmUrl' style='width: 100em;' maxlength='100' size='50'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								URL is a required field
							</p>
						</div>
					</div>
				</div>
			</div>
			<div class='ot-modal__footer'>
				<div class='ot-btn-group'>
					<button class='ot-btn ot-btn--is-primary' onclick='javascript:okay(0);'>
						Save
					</button>
					<button class='ot-btn ot-btn--is-secondary' onclick='javascript:cmsg();sf(0,0,1);'>
						Cancel
					</button>

					<p hidden>
						<input size='0' type='password' name='z' id='z' value='0'>
						<input size='0' type='password' name='zz' id='zz' value='0'>
						<input size='0' type='password' name='q1' id='q1' value=$config{'webtopSaveBookmark'}>
						<input size='0' type='password' name='qq1' id='qq1' value=$config{'webtopDeleteBookmark'}>
					</p>
				</div>
			</div>
		</div>
	</div>
";

	my $gbdd = $config{'webtopGetBookmarkGroup'};

print "
	<script type='text/javascript'>
		ws('$gbdd',5,0,0);
	</script>
";
}

sub showFileshareModal
{
print "
	<div class='ot-modal' id='ffdiv'>
		<div class='ot-modal__background'>
		</div>
		<div class='ot-modal__content'>
			<div class='ot-modal__header'>
				<div class='ot-level'>
					<div class='ot-level__left'>
						<div class='ot-level__item'>
							<p class='title'>
								<input type='text' disabled class='ot-input' name='mt2' id='mt2'>
							</p>
						</div>
					</div>
					<div class='ot-level__center'>
						<div class='ot-level__item'>
							<div style='width: 450px; height: 1px; opacity: 1;'></div>
						</div>
					</div>
					<div class='ot-level__right'>
						<div class='ot-level__item'>
							<div id='dpfs' style='display: none;' onclick='javascript:rff(1);'>
								<img src='LocalWebLib/action_delete32.svg' style='height: 19px; width: 19px' title='Delete fileshare'>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 400px; height: 270px;'>
					<div class='ot-form-control'>
						<div id='fsRemoteLocationDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='fsRemoteLocation'>
								<div class='ot-form-control__required-indicator'>*</div>
								Remote Location
							</label>

							<div class='ot-form-control__input-group'>
								<input placeholder='\\\\server\\shared-name' type='text' class='ot-input' name='fsRemoteLocation' id='fsRemoteLocation' style='width: 15em' maxlength='100'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Remote Location is a required field
							</p>
						</div>

						<div id='fsUsernameDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='fsUsername'>
								<!-- <div class='ot-form-control__required-indicator'>*</div>-->
								Username
							</label>		
							<div class='ot-form-control__input-group'>
								<input placeholder='Username' type='text' class='ot-input' name='fsUsername' id='fsUsername' style='width: 15em' maxlength='50'>
							</div>
						</div>

						<div id='fsPasswordDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='fsPassword'>
								<!-- <div class='ot-form-control__required-indicator'>*</div> -->
								Password
							</label>		
							<div class='ot-form-control__input-group'>
								<input placeholder='Password' type='password' class='ot-input' name='fsPassword' id='fsPassword' style='width: 15em' maxlength='50'>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class='ot-modal__footer'>
				<div class='ot-btn-group'>
					<button class='ot-btn ot-btn--is-primary' onclick='javascript:okay(1);'>
						Save	
					</button>
					<button class='ot-btn ot-btn--is-secondary' onclick='javascript:cmsg();sf(0,1,2);'>
						Cancel
					</button>
					<p hidden>
						<input size='0' type='password' name='z' id='z' value='0'>
						<input size='0' type='password' name='zz' id='zz' value='0'>
						<input size='0' type='password' name='q2' id='q2' value=$config{'webtopSaveFileshare'}>
						<input size='0' type='password' name='qq2' id='qq2' value=$config{'webtopDeleteFileshare'}>
					</p>
				</div>
			</div>
		</div>
	</div>
";
}

sub showTunnelModal
{
print "
	<div class='ot-modal' id='tfdiv' style='z-index: 1500; width: 1024px; height: 100vh; position: absolute; left:-100'>
		<div class='ot-modal__background'>
		</div>
		<div class='ot-modal__content'>
			<div class='ot-modal__header'>
				<p class='title'>
					<input type='text' disabled class='ot-input' name='mt4' id='mt4'>
				</p>
				<ul id='tm' class='ot-tabs'>
					<!-- <li id='tm100' class='ot-tabs__item ot-tabs__item--is-active'> -->
					<li id='tm100' class='ot-tabs__item'> 
						<a onclick=javascript:tbtab('tm100');>
							<img src='LocalWebLib/ipsecDevice.svg' style='height: 19px; width: 19px'>
							Summary
						</a>
					</li>
				</ul>
			</div>
			<div class='ot-modal__body'>
				<div id='tmDiv' class='ot-m-1' style='background: #white;'> 
					<div id='td100' style='display: block;' class='ot-form-control'>

					</div>
				</div>
			</div>
			<div class='ot-modal__footer'>
				<button class='ot-btn ot-btn--is-primary' onclick='javascript:sf(0,0,4);'>	
					Hide
				</button>
			</div>
		</div>
	</div>
";
}

sub showConfirmModal
{
print "
	<div class='ot-modal' id='cfdiv' style='z-index: 1501;'>
		<div class='ot-modal__background'>
		</div>
		<div class='ot-modal__content'>
			<div class='ot-modal__header'>
				<p class='title'>
					<input type='text' disabled class='ot-input' name='mt5' id='mt5'>
				</p>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 300px; height: 150px;'>
					<div class='ot-form-control'>
						<div id='ynDiv' class='ot-form-control'>
							<div class='text'>
							Disconnect tunnel and close this window?
							</div>
						</div>
					</div>
				</div>
			</div>
		    	<div class='ot-modal__footer'>
		      		<div class='ot-btn-group'>
					<button class='ot-btn ot-btn--is-primary' onclick='javascript:scf(1);'>
						OK
					</button>
					<button class='ot-btn ot-btn--is-secondary' onclick='javascript:scf(0);'>
			  			Cancel
					</button>
		      		</div>
    			</div>
  		</div>
	</div>
";
}

sub showPreferenceModal
{
print "
	<div class='ot-modal' id='pfdiv' style='z-index: 1501;'>
		<div class='ot-modal__background'>
		</div>
		<div class='ot-modal__content'>
			<div class='ot-modal__header'>
				<p class='title'>
					<input type='text' disabled class='ot-input' name='mt6' id='mt6'>
				</p>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 300px; height: 350px;'>
					<div class='ot-form-control'>
						<!--
						<h6>Contact Information</h6>	
						<div id='fNameDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='fName'>
								<div class='ot-form-control__required-indicator'>*</div>
								Given name	
							</label>
							<div class='ot-form-control__input-group'>
								<input placeholder='' type='text' class='ot-input' name='fName' id='fName' style='width: 20rem'  maxlength='25'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Given name is a required field
							</p>
						</div>
						<div id='lNameDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='lName'>
								<div class='ot-form-control__required-indicator'>*</div>
								Surname	
							</label>
							<div class='ot-form-control__input-group'>
								<input placeholder='' type='text' class='ot-input' name='lName' id='lName' style='width: 20rem'  maxlength='25'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Surname is a required field
							</p>
						</div>
						<div id='wEmailDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='wEmail'>
								<div class='ot-form-control__required-indicator'>*</div>
								Work email
							</label>
							<div class='ot-form-control__input-group'>
								<input placeholder='' type='text' class='ot-input' name='wEmail' id='wEmail' style='width: 20rem'  maxlength='25'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Work email is a required field
							</p>
						</div>
						<p>
						-->
						<h6>Security</h6>	
						<div id='oPwdDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='oPwd'>
								<div class='ot-form-control__required-indicator'>*</div>
								Current password
							</label>
							<div class='ot-form-control__input-group'>
								<input placeholder='' type='password' class='ot-input' name='oPwd' id='oPwd' style='width: 20rem'  maxlength='25'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Current password is a required field
							</p>
						</div>
						<div id='nPwdDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='nPwd'>
								<div class='ot-form-control__required-indicator'>*</div>
								New password
							</label>
							<div class='ot-form-control__input-group'>
								<input placeholder='' type='password' class='ot-input' name='nPwd' id='nPwd' style='width: 20rem'  maxlength='25'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								New password is a required field
							</p>
						</div>
						<div id='cPwdDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='cPwd'>
								<div class='ot-form-control__required-indicator'>*</div>
								Confirm new password
							</label>
							<div class='ot-form-control__input-group'>
								<input placeholder='' type='password' class='ot-input' name='cPwd' id='cPwd' style='width: 20rem'  maxlength='25'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Confirm password is a required field
							</p>
						</div>
					</div>
				</div>
			</div>
			<div class='ot-modal__footer'>
				<div class='ot-btn-group'>
					<button class='ot-btn ot-btn--is-primary' onclick='javascript:okay(5,1);'>
						Save
					</button>
					<button class='ot-btn ot-btn--is-secondary' onclick='javascript:sf(0,5,6);'>
						Cancel
					</button>
				</div>
				<p hidden>
					<input size='0' type='password' name='q7' id='q7' value=$config{'webtopSavePassword'}>
				</p>
			</div>
		</div>
	</div>
";
}

sub showDashboard
{
	my $restrictionSetId = $client->getRestrictionSetId();
	my $db = $tb->getWebtopDbWriter();

	my $cmi = $client->getUserContentModules();
	my @sortedKeys =  sort { $cmi->{$a}->{'linkOrder'} cmp $cmi->{$b}->{'linkOrder'} } keys %{ $cmi };

print "
	<script type='text/javascript' src=\"LocalWebLib/webtopToolBox.js\"></script>
	<div style='height: calc( 100vh - 205px ); position: relative;'>
	<div id='tiles-dashboard' class='ot-tiles ot-section ot-section--has-gradient-bg'>
";

	touchSession();
	showRecent();

        foreach ( @sortedKeys ) {
                my $class = $_;
                my $moduleName = $cmi->{$_}->{'moduleName'};

		if( $moduleName eq "File Shares" ) {
			showFileshare( restrictionSetId=> $restrictionSetId );	
		}
		elsif( $moduleName eq "Intranet" ) {
			showBookmark( restrictionSetId=> $restrictionSetId );
		}
	}

	my( $privateIpAddress ) = $db->query( sql=> "
                                        SELECT rawip32_to_dotted_quad(private_ip_address)
                                                FROM client_sessions
                                                WHERE user_id = " . $client->getUserIdDB( db=> $db ) . " AND client_session_type_id = 4
                                        ");

	my $msg = "Core security";
	$msg = "Secure+ tunnel on $privateIpAddress" unless( ! length( $privateIpAddress ) );

print "
	<div id='c0' style='display: block;' class='ot-form-control'>
		<div style='width: 100%; bottom: 0; position: absolute;'>
			<div class='text'>
				<p style='color: white;'>
					<span>$msg</span>
				</p>
			</div>
		</div>
	</div>
";

print "
	</div> <!-- tiles -->
	</div>
";

	showTunnelModal();
	showConfirmModal();
	showPreferenceModal();
}

sub showHome
{
	my $greeting = "";
        my $db = $tb->getWebtopDbWriter();

	my( $firstName ) = $db->query( sql=> "SELECT first_name FROM users WHERE user_id = " . $client->getUserIdDB( db=> $db ) );
	if( length( $firstName ) ) {
		$firstName = " " . ucfirst( $firstName ) . ",";
	}
	else {
		$firstName = ",";
	}

	my( $hour ) = $db->query( sql=> "SELECT TO_CHAR(NOW(), 'hh24')" );
	if( $hour > 0 && $hour < 12 ) {
		$greeting = "Good morning";
	}
	elsif( $hour >= 12 && $hour < 18 ) {
		$greeting = "Good afternoon";
	}
	else {
		$greeting = "Good evening";
	}

	$greeting .= $firstName;

print "
	<div class='ot-list-tile ot-list-tile--is-simple' style='background: #a9aeb9'>
		<div class='ot-list-tile__header'>
			<div class='ot-list-tile__title'>$greeting
			</div>
		</div>
		<div style='height: 200px; position: relative;'>
			I would like to...	
			<div style='width: 100%; height: 20px; opacity: 1;'></div>
			<input placeholder='Find a bookmark, file share' type='text' class='ot-input' name='f' id='f' style='width: 15em; style='width: 25em' maxlength='100'>
			<div style='width: 100%; height: 20px; opacity: 1;'></div>
			<input placeholder='Browse https://' type='text' class='ot-input' name='b' id='b' style='width: 15em' maxlength='255'>

			<div style='width: 100%; height: 20px; opacity: 1;'></div>
			<!--
			<div>
				<div style='width: 50px; height: 1px; opacity: 1;'></div>
				<button class='ot-btn ot-btn--is-primary' onclick='javascript:okayy(2);'>
				Go	
				</button>
			</div>
			-->
		</div>
</div>
";
}

sub showVpnDownloadPage
{
	my $db = $tb->getWebtopDbWriter();

	my( @vpnDownloads ) = $db->query( sql => "
					SELECT
						can_download_vpn,
						can_download_l2tp_ios,
						can_download_l2tp_macosx
					FROM
						user_vpn_access
					WHERE
						user_id = " . $client->getUserIdDB( db=> $db ) . "
					LIMIT 1
					");
        my( $canDownloadVpn, $canDownloadL2tpIos, $canDownloadL2tpMacosx ) = @{ $vpnDownloads[0] };

	#
	# Disabling macOS and iOS until we modernize
	#
	$canDownloadL2tpIos = 0; 
	$canDownloadL2tpMacosx = 0;

	if( $canDownloadVpn == 1 ) {
		my $macClientDmg = $config{macClientDmg};
		my $modeText = "vpn";
		my $windowsOS = "";
		my $oem = $tb->getOem();
		my $productName = $oem->getSetting(setting => 'vpnProductName');
		my $productInstallPath = $oem->getSetting(setting => 'installerPath');
		my $filePath = "";
		my $fileSizeUnit = "";
		my $site = "";
		my $softwareBranch = 0;
		my $digest = 0;
		my $fileSize = 0;
		my @stat = ();

		my( @userSoftwareVersions ) = $db->query( sql=> "
					SELECT
						major_release_number,
						minor_release_number
						FROM user_software_versions AS usv
						JOIN software_products AS sp ON sp.software_product_id = usv.software_product_id
						JOIN software_releases AS sr ON sr.software_release_id = usv.software_release_id
							 WHERE user_id = " . $client->getUserIdDB( db=> $db ) . " AND sp.distribution_identifier = '$modeText'" 
						);
		my( $majorReleaseNumber, $minorReleaseNumber ) = @{ $userSoftwareVersions[0] };
		
		if( $majorReleaseNumber == $vpnConfig{showWin10MajorVersion} && $minorReleaseNumber >= $vpnConfig{showWin10MinorVersion} ) {
			$windowsOS = "Windows&reg; 10 and Windows&reg; 11";
		}
			else {
			$windowsOS = "Windows&reg; 7 and Windows&reg; 8.1";
		}

		if( $productInstallPath =~ /vpn\-(.+)\// ) {
			$site = $0;
			$softwareBranch = $1;
		}

		my $pos = index( $productInstallPath, "downloads" );
		if( $pos > 0 ) {
			$filePath = substr( $productInstallPath, $pos );
		}
		else {
			$pos = index( $productInstallPath, "install" );
			$filePath = substr( $productInstallPath, $pos );
		}

		if( -e $filePath )  {
			@stat = stat $filePath;
			$fileSize = $stat[7];

			my $i = 0;
			my @units = ("B", "KB", "MB", "GB");
		
			for( @units ) {
				last if $fileSize < 1024;

				$fileSize /= 1024;
				$i++;
			}

			$fileSizeUnit = sprintf("%.2f %s", $fileSize, $units[$i]);
			$digest = file_md5_hex( $filePath );
		}

		my( @version ) = $db->query(sql => "
                                	SELECT
                                        	sr.major_release_number,
                                        	sr.minor_release_number
                                	FROM user_software_versions AS usv,
                                		software_product_branches AS spb,
                                		software_releases AS sr 
                                	WHERE spb.software_product_id = usv.software_product_id AND spb.software_branch_type_id = usv.software_branch_type_id
                                		AND sr.software_release_id = spb.software_release_id
                                		AND usv.software_product_id = 1
                                		AND usv.user_id = " . $client->getUserIdDB( db=> $db ) . "
                        ");


		my $initialDownload = "";
		my( $downloadCount ) = $db->query( sql=> "
						SELECT COUNT(1) 
						FROM user_enrollment_metrics
						WHERE user_id = " . $client->getUserIdDB( db=> $db ) . "
				");

		if( $downloadCount > 0  )
		{
			( $initialDownload ) = $db->query( sql=> "
						SELECT TO_CHAR(client_download_date, 'DD/MM/YYYY hh12:mi:ss AM')
						FROM user_enrollment_metrics
						WHERE user_id = " . $client->getUserIdDB( db=> $db ) . "
				");
		}
		$initialDownload = "Unknown" unless( length( $initialDownload ) > 0 );

print "
<div id='ddiv1' style='display: block;' class='ot-form-control'>
	<form>
	<div style='height: calc( 100vh - 220px ); position: relative;'>   
	<div class='ot-table ot-table--is-bordered'>
	<div class='ot-table-wrapper'>
        <div class='ot-table-container'>
		 <div role='table header' class='ot-table__header'>
                        <div class='ot-table--is-center'>
                                <h6>Compatible with $windowsOS</h6>
                        </div>
                </div>
		<div class='ot-table-content'>
          		<div role='table body' class='ot-table__body'>
            			<div class='ot-container'>
              				<div class='ot-table-header-group'>
                				<div class='ot-table__subheader'>
							<div class='ot-table__column' style='width: 15%; text-align: left;'>
								<p>Product</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>Version</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>File Size</p>
							</div>
							<div class='ot-table__column' style='width: 25%; text-align: left;'>
								<p>Checksum</p>
							</div>
							<div class='ot-table__column' style='width: 15%; text-align: left;'>
								<p>Initial Download</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>Download Count</p>
							</div>
							<div class='ot-table__column' style='width: *%; text-align: left;'>
								<p></p>
							</div>
						</div> 
					</div>
                                        <div class='ot-table-row-group' id='winTable'>
						<div class='ot-table__row'>
							<div class='ot-table__column' style='width: 15%; text-align: left;'>
								<p>$productName</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>$softwareBranch</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>$fileSizeUnit</p>
							</div>
							<div class='ot-table__column' style='width: 25%; text-align: left;'>
								<p>$digest</p>
							</div>
							<div class='ot-table__column' style='width: 15%; text-align: left;'>
								<p>$initialDownload</p>
							</div>
							<div class='ot-table__column' style='width: 11%; text-align: right;'>
								<p>$downloadCount</p>
							</div>
							<div class='ot-table__column' style='width: *%; text-align: left text-valign: top;'>
						 		<button id='db' name='db' class='ot-btn ot-btn--is-primary' onclick=javascript:document.getElementById('dl').click();>
                       						Download 
                        					</button>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	</div>
	</div>
	</div>
		
	<div position: relative;'>   
	<div class='ot-table'>
	<div class='ot-table-wrapper'>
        <div class='ot-table-container'>
		<div class='ot-table-content'>
          		<div role='table body' class='ot-table__body'>
            			<div class='ot-container'>
                                        <div class='ot-table-row-group' id='footerTable'>
						<div class='ot-table__row'>
							<div class='ot-table__column' style='width: 30%'; text-align: left;' colspan='3'>
								<div style='width: 100%; height: 1px; opacity: 1;'></div>
							</div>
							<!--
							<div class='ot-table__column' style='width: 50%'; text-align: right;' colspan='3'>
								<button id='bc' name='bc' class='ot-btn ot-btn--is-secondary' onclick='javascript:window.opener.close();'>
                       							Close Window
                        					</button>
							</div>
							-->
						</div>
					</div>
					<div style='width: 100%; height: 50px; opacity: 1;'></div>
                                        <div class='ot-table-row-group' id='tmTable'>
						<div class='ot-table__row'>
							<div class='ot-table__column' style='width: 100%; text-align: left;' colspan='7'>
								<i>Windows&reg; is a registered trademark of Microsoft Corporation</i>
								<a href='$productInstallPath' target='_blank' id='dl'></a>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	</div>
	</div>
	</div>
	</form>
</div>
";
	}
#<i>macOS&reg; is a registered trademark of Apple Inc.</i>

	if( $canDownloadVpn == 0 && $canDownloadL2tpIos == 0 && $canDownloadL2tpMacosx == 0 ) {
	print "
				<div style='display: block;' class='ot-list-tile ot-list-tile--is-detail'>
					<div class='ot-list-tile__header'>
						<div class='ot-list-tile__title'>
						</div>
					</div>
					<div class='ot-list-tile__body'>
						<ul id='rrsul1' class='ot-list'>
							<li class='ot-list__item'><span style='width: 50rem;'>It appears your download access has not been granted.</span></li>
						</ul>
					</div>
				</div>
		";
	}
	
	deleteSession();
}

sub showNetconDownloadPage
{
	my $db = $tb->getWebtopDbWriter();

	my( $canDownloadVpn ) = $db->query( sql => "
					SELECT
						can_download_vpn
					FROM
						user_vpn_access
					WHERE
						user_id = " . $client->getUserIdDB( db=> $db ) . "
					LIMIT 1
					");

	if( $canDownloadVpn == 1 ) {
		my $modeText = "netcon";
		my $windowsOS = "";

		my( @userSoftwareVersions ) = $db->query( sql=> "
					SELECT
						major_release_number,
						minor_release_number
						FROM user_software_versions AS usv
						JOIN software_products AS sp ON sp.software_product_id = usv.software_product_id
						JOIN software_releases AS sr ON sr.software_release_id = usv.software_release_id
						      WHERE user_id = " . $client->getUserIdDB( db=> $db ) . " AND sp.distribution_identifier = '$modeText'" 
						);
		my( $majorReleaseNumber, $minorReleaseNumber ) = @{ $userSoftwareVersions[0] };

		if( $majorReleaseNumber == $vpnConfig{showWin10NetconMajorVersion} && $minorReleaseNumber >= $vpnConfig{showWin10NetconMinorVersion} ) {
			$windowsOS = "Windows&reg; 10, Windows&reg; 11, Windows Server&reg; 2016 and Windows Server&reg; 2019";
		}
		else {	
			$windowsOS = "Windows&reg; 7, Windows&reg; 8.1, Windows Server&reg; 2012 R2";
		}

		my $oem = $tb->getOem();
		my $productName = $oem->getSetting(setting => 'networkConnectorName');
		my $productInstallPath = $oem->getSetting(setting => 'netconInstallerPath');
		my $oemNetconInstallerFileName = $oem->getSetting(setting => 'oemNetconInstallerFileName');
		my $filePath = "";
		my $fileSizeUnit = "";
		my $site = "";
		my $digest = 0;
		my $fileSize = 0;
		my @stat = ();

		my $pos = index( $productInstallPath, "downloads" );
		if( $pos > 0 ) {
			$filePath = substr( $productInstallPath, $pos );
		}
		else {
			$pos = index( $productInstallPath, "install" );
			$filePath = substr( $productInstallPath, $pos );
		}

		if( -e $filePath )  {
			@stat = stat $filePath;
			$fileSize = $stat[7];

			my $i = 0;
			my @units = ("B", "KB", "MB", "GB");
		
			for( @units ) {
				last if $fileSize < 1024;

				$fileSize /= 1024;
				$i++;
			}

			$fileSizeUnit = sprintf("%.2f %s", $fileSize, $units[$i]);
			$fileSizeUnit = strim( str=> $fileSizeUnit );

			$digest = file_md5_hex( $filePath );
			$digest = strim( str=> $digest );
		}

		my( @version ) = $db->query(sql => "
                                	SELECT
                                        	sr.major_release_number,
                                        	sr.minor_release_number
                                	FROM user_software_versions AS usv,
                                		software_product_branches AS spb,
                                		software_releases AS sr 
                                	WHERE spb.software_product_id = usv.software_product_id AND spb.software_branch_type_id = usv.software_branch_type_id
                                		AND sr.software_release_id = spb.software_release_id
                                		AND usv.software_product_id = 1
                                		AND usv.user_id = " . $client->getUserIdDB( db=> $db ) . "
                       	 ");

		my $initialDownload = "";
		my( $downloadCount ) = $db->query( sql=> "
						SELECT COUNT(1) 
						FROM user_enrollment_metrics
						WHERE user_id = " . $client->getUserIdDB( db=> $db ) . "
				");

		if( $downloadCount > 0  )
		{
			( $initialDownload ) = $db->query( sql=> "
						SELECT TO_CHAR(client_download_date, 'DD/MM/YYYY hh12:mi:ss AM')
						FROM user_enrollment_metrics
						WHERE user_id = " . $client->getUserIdDB( db=> $db ) . "
				");
		}
		$initialDownload = "Unknown" unless( length( $initialDownload ) > 0 );

print "
<div id='ddiv1' style='display: block;' class='ot-form-control'>
	<form>
	<div style='height: calc( 100vh - 220px ); position: relative;'>   
	<div class='ot-table ot-table--is-bordered'>
	<div class='ot-table-wrapper'>
        <div class='ot-table-container'>
		 <div role='table header' class='ot-table__header'>
                        <div class='ot-table--is-center'>
                                <h6>Compatible with $windowsOS</h6>
                        </div>
                </div>
		<div class='ot-table-content'>
          		<div role='table body' class='ot-table__body'>
            			<div class='ot-container'>
              				<div class='ot-table-header-group'>
                				<div class='ot-table__subheader'>
							<div class='ot-table__column' style='width: 15%; text-align: left;'>
								<p>Product</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>Version</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>File Size</p>
							</div>
							<div class='ot-table__column' style='width: 25%; text-align: left;'>
								<p>Checksum</p>
							</div>
							<div class='ot-table__column' style='width: 15%; text-align: left;'>
								<p>Initial Download</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>Download Count</p>
							</div>
							<div class='ot-table__column' style='width: *%; text-align: left;'>
								<p></p>
							</div>
						</div> 
					</div>
                                        <div class='ot-table-row-group' id='winTable'>
						<div class='ot-table__row'>
							<div class='ot-table__column' style='width: 15%; text-align: left;'>
								<p>$productName</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>$majorReleaseNumber . $minorReleaseNumber</p>
							</div>
							<div class='ot-table__column' style='width: 10%; text-align: left;'>
								<p>$fileSizeUnit</p>
							</div>
							<div class='ot-table__column' style='width: 25%; text-align: left;'>
								<p>$digest</p>
							</div>
							<div class='ot-table__column' style='width: 15%; text-align: left;'>
								<p>$initialDownload</p>
							</div>
							<div class='ot-table__column' style='width: 9%; text-align: right;'>
								<p>$downloadCount</p>
							</div>
							<div class='ot-table__column' style='width: *%; text-align: left text-valign: top;'>
						 		<button id='db' name='db' class='ot-btn ot-btn--is-primary' onclick=javascript:document.getElementById('dl').click();>
                       						Download
                        					</button>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	</div>
	</div>
	</div>
		
	<div position: relative;'>   
	<div class='ot-table'>
	<div class='ot-table-wrapper'>
        <div class='ot-table-container'>
		<div class='ot-table-content'>
          		<div role='table body' class='ot-table__body'>
            			<div class='ot-container'>
                                        <div class='ot-table-row-group' id='footerTable'>
						<div class='ot-table__row'>
							<div class='ot-table__column' style='width: 30%'; text-align: left;' colspan='3'>
								<div style='width: 100%; height: 1px; opacity: 1;'></div>
							</div>
							<!--
							<div class='ot-table__column' style='width: 50%'; text-align: right;' colspan='3'>
								<button id='bc' name='bc' class='ot-btn ot-btn--is-secondary' onclick='javascript:window.opener.close();'>
                       							Close Window
                        					</button>
							</div>
							-->
						</div>
					</div>
					<div style='width: 100%; height: 50px; opacity: 1;'></div>
                                        <div class='ot-table-row-group' id='tmTable'>
						<div class='ot-table__row'>
							<div class='ot-table__column' style='width: 100%; text-align: left;' colspan='7'>
								<i>Windows&reg; is a registered trademark of Microsoft Corporation</i>
								<a href='$productInstallPath' target='_blank' id='dl'></a>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	</div>
	</div>
	</div>
	</form>
</div>
";

warn "DEBUG: productInstallPath=$productInstallPath\n";

	}

	deleteSession();
}

$wtfb->showHeader( request=> $q, mode=> $mode );

if( $mode eq $config{'webtopNetconDownload'} ) {
	showNetconDownloadPage();
}
elsif( $mode eq $config{'webtopVPNDownload'} ) {
	showVpnDownloadPage();
}
else {
	my $referrer = $ENV{'HTTP_REFERER'};

	showStopModal();
	showDashboard();
	checkCaptivePortalized();
	showDisconnectModal();
	showIdleTimoutModal();
	showAclModal();
	showDiagnostic();
}

$wtfb->showFooter();
exit(0);
