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

use strict;
use CGI qw/:standard/;
use APR::Request::Cookie;
use Pos::Webtop::Toolbox;
use Pos::Webtop::webtopFormBuilder;
use HTTP::Request;
use LWP::UserAgent;

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

# q is the server/path
my $q = $cgi->param('q');
die() unless( defined( $q ) && length( $q ) );

# c is corporate or personal fileshare
my $c = $cgi->param('c');
my $conf   = new Config::General("/usr/local/positive/conf/PosPerl.conf");
my %config = $conf->getall;
my $userId = $client->getUserIdDB( db=> $db );
my $referrer = $ENV{'HTTP_REFERER'};

sub showPanel
{
	my $rc = 1;

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

	#
	# Show tree
	my( $epoch ) = $db->query( sql=> "
					SELECT 
                                       		ROUND( EXTRACT(epoch FROM NOW()) / 60 )
					" );

	my @tempArr = split("/", $config{'webtopBackendUrl'});
    	my $hostName = $tempArr[2];

	my $url = $config{'webtopBackendUrl'} . "?m=" . $config{'webtopShowFileShareTree'} . "&c=$c&q=$q&U=$userId&i=root&ms=$epoch&ajax=0";
	my $request = HTTP::Request->new(GET => $url );

	my $ua = LWP::UserAgent->new(ssl_opts => { SSL_hostname=>'${hostName}', verify_hostname => 0, SSL_version => 'TLSv12'} );
    	$ua->timeout(60);
    	$ua->agent("Mozilla/8.0");
        my $response = $ua->request( $request );

	if( $response->is_success ) {
		print $response->content;
	}
	else {
		showErrorModal( error=>"Sorry, fileshare connection failed." );
		$rc = 0;
	}

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

	if( $rc ) {
	print "
		<div id='fspanelDiv'>
	";

		$url = $config{'webtopBackendUrl'} . "?m=" . $config{'webtopLoadFileSharePanel'} . "&c=$c&q=$q&U=$userId&i=file&ms=$epoch&ajax=0";
		$request = HTTP::Request->new(GET => $url );

		$ua = LWP::UserAgent->new(ssl_opts => { SSL_hostname=>'${hostName}', verify_hostname => 0, SSL_version => 'TLSv12'} );
    		$ua->timeout(60);
    		$ua->agent("Mozilla/8.0");
        	my $response = $ua->request( $request );

		if( $response->is_success ) {
			print $response->content;
		}
		else {
			showErrorModal( error=>"Sorry, fileshare connection failed." );
		}
	
	print "
		</div> <!-- panel -->
	";
	}
       
	my( $privateIpAddress ) = $db->query( sql=> "
                                        SELECT rawip32_to_dotted_quad(private_ip_address)
                                                FROM client_sessions
                                                WHERE user_id = $userId AND client_session_type_id = 4
                                        ");

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

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

	<p hidden>
		<input size='0' type='password' name='c' id='c' value='$c'>
		<input size='0' type='password' name='d' id='d' value=''>
		<input size='0' type='password' name='q' id='q' value='$q'>
		<input size='0' type='password' name='u' id='u' value='$userId'>
	</p>
";

}

sub showBackErrorModal
{
print "
	<div id='seDiv'>
	</div>
";
}

sub showErrorModal
{
	my( $error ) = @{{@_}}{qw/error/};

print "
	<div class='ot-modal ot-modal--is-active' id='ediv' 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' value='WARNING'>
				</p>
    			</div>
    			<div class='ot-modal__body'>
      				<div class='ot-m-1' style='background: #white; width: 450px; height: 150px;'>
					<div class='ot-form-control'>
						<div class='text'>
						$error
						</div>
					</div>
      				</div>
   			</div>
			<div class='ot-modal__footer'>
                        	<button class='ot-btn ot-btn--is-primary' onclick=javascript:doq('aclDiv');>
                                	OK
                                </button>
                       	</div>
  		</div>
	</div>
";
}

sub showDeleteConfirmModal
{
print "
	<div class='ot-modal' id='dcfdiv' 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' value='Delete file'>
				</p>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 300px; height: 200px;'>
					<div id='dcfmsgDiv0' style='display: block;' class='ot-form-control'>
						<div id='dcfmsgDiv' class='ot-form-control'>
							<div class='ot-form-control'>
								Yes, I would like to permanently delete...
								<div style='width: 1px; height: 2px; opacity: 1;'></div>
								<select id='dcfmsg' multiple readonly style='overflow-y:scroll;height:150px;width:600px;'></select>
								</div>
							</div>
						</div>
						<div id='dcfmsgDiv1' style='display: none;' class='ot-form-control'>
							<div class='ot-form-control'>
								Please select one more files to delete.
							</div>
						</div>
					</div>
				</div>
				<div class='ot-modal__footer'>
					<div class='ot-btn-group'>
						<button class='ot-btn ot-btn--is-primary' onclick=javascript:pdc(1,document.getElementById('r').value);>
							OK
						</button>
						<button class='ot-btn ot-btn--is-secondary' onclick=javascript:pdc(0,document.getElementById('r').value);>
							Cancel
						</button>
					</div>
					<p hidden>
						<input size='0' type='password' name='r' id='r'>
					</p>       
				</div>
			</div>
		</div>
	</div>
";
}

sub showCreateFolderModal
{
print "
		<div class='ot-modal' id='cffdiv' 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' value='Create folder'>
					</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='cfNameDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='cfName'>
								<div class='ot-form-control__required-indicator'>*</div>
								Folder name
							</label>
							<div class='ot-form-control__input-group'>
								<input placeholder='(e.g., Projects)' type='text' class='ot-input' name='cfName' id='cfName' style='width: 35em' maxlength='100' size='75'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								Folder name 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:pcfc(1);>
							OK
						</button>
						<button class='ot-btn ot-btn--is-secondary' onclick=javascript:pcfc(0);>
							Cancel
						</button>
					</div>
					<p hidden>
						<input size='0' type='password' name='r' id='r'>
					</p>       
				</div>
			</div>
		</div>
	";
}
	
sub showRenameFileModal
{
	print "
		<div class='ot-modal' id='rfdiv' 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' value='Rename file'>
					</p>
				</div>
				<div class='ot-modal__body'>
					<div class='ot-m-1' style='background: #white; width: 300px; height: 200px;'>
					<div class='ot-form-control'>
						<div id='rfNameDiv0' style='display: block;' class='ot-form-control'>
							<div id='rcfNameDiv' class='ot-form-control'>
								<label class='ot-form-control__label' for='rcfName'>
									Current filename
								</label>
								<div class='ot-form-control__input-group'>
									<input type='text' class='ot-input' readonly name='rcfName' id='rcfName' style='width: 35em' maxlength='100' size='75'>
								</div>
							</div>	
							<div id='rfNameDiv' class='ot-form-control'>
								<label class='ot-form-control__label' for='rfName'>
									<div class='ot-form-control__required-indicator'>*</div>
									New filename
								</label>
								<div class='ot-form-control__input-group'>
									<input type='text' class='ot-input' name='rfName' id='rfName' style='width: 35em' maxlength='100' size='75'>
									<span class='ot-form-control__error-icon'>
									</span>
								</div>
								<p class='ot-form-control__error-text'>
									Filename is a required field
								</p>
							</div>
						</div>
						<div id='rfNameDiv1' style='display: hidden;' class='ot-form-control'>	
							<div class='ot-form-control'>
								Please select one file to rename.
							</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:prfc(1,document.getElementById('r').value);>
							OK
						</button>
						<button class='ot-btn ot-btn--is-secondary' onclick=javascript:prfc(0,document.getElementById('r').value);>
							Cancel
						</button>
					</div>
					<p hidden>
						<input size='0' type='password' name='r' id='r'>
					</p>       
				</div>
			</div>
		</div>
	";
}

sub showMoveFileModal
{
print "
		<div class='ot-modal' id='mffdiv' style='z-index: 1501;'>
			<div class='ot-modal__background'>
			</div>
			<div class='ot-modal__content'>
				<div class='ot-modal__header'>
					 <div class='ot-level'>
						<div class='ot-level__center'>
							<div class='ot-level__item'>
								<p class='title'>
									<input type='text' disabled class='ot-input' value='Move file'>
								</p>
							</div>
						</div>
					</div>
				</div>
				<div class='ot-modal__body'>
					<div class='ot-m-1' style='background: #white; width: 300px; height: 400px;'>
					<div class='ot-form-control'>
						<input type='text' readonly class='ot-input' name='treeSD' id='treeSD' style='width: 35em' size='75'>
						<div id='tiles-dashboard-move' class='ot-tiles ot-section'>
							<div id='dcmfmsgDiv0' style='display: none; z-index: 1502; border:1px solid black; background-color: white; z-index:2; height:175px; width:425px; top: 70px; right: 0px; position: fixed; cursor: pointer;' class='ot-form-control'>
								<div id='dcmfmsgDiv' class='ot-form-control'>
									<div class='ot-form-control'>
										<a href='#' onclick='javascript:sml(0);'>Close file list</a>
										<select id='dcmfmsg' multiple readonly style='overflow-y:scroll;height:150px;width:400px;'></select>
									</div>
								</div>
							</div>
							<div id='mfsdiv' style='display: none;' class='ot-form-control'>
							<a href='#' onclick='javascript:sml(1);'>View file list</a>
	";
		#
		# Show tree
		my( $epoch ) = $db->query( sql=> "
						SELECT 
							ROUND( EXTRACT(epoch FROM NOW()) / 60 )
						" );

		my @tempArr = split("/", $config{'webtopBackendUrl'});
		my $hostName = $tempArr[2];

		my $url = $config{'webtopBackendUrl'} . "?m=" . $config{'webtopShowFileShareTree'} . "&c=$c&q=$q&U=$userId&i=root&ms=$epoch&ajax=0&M=1";
		my $request = HTTP::Request->new(GET => $url );

		my $ua = LWP::UserAgent->new(ssl_opts => { SSL_hostname=>'${hostName}', verify_hostname => 0, SSL_version => 'TLSv12'} );
		$ua->timeout(60);
		$ua->agent("Mozilla/8.0");
		my $response = $ua->request( $request );

		if( $response->is_success ) {
			print $response->content;
		}
		else {
			showErrorModal( error=>"Sorry, fileshare connection failed." );
		}

	print "
							</div> <!-- tree -->
							<div id='dcmfmsgDiv1' style='display: none;' class='ot-form-control'>
								<div class='ot-form-control'>
									Please select one more files to move.
								</div>
							</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:pmfc(1);>
						OK
					</button>
					<button class='ot-btn ot-btn--is-secondary' onclick=javascript:pmfc(0);>
			  			Cancel
					</button>
		      		</div>
    			</div>
  		</div>
	</div>
";
}

sub showDownloadFileModal
{
print "
	<div class='ot-modal' id='dlfdiv' 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' value='Download file'>
				</p>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 300px; height: 225px;'>
					<div id='dlfmsgDiv0' style='display: block;' class='ot-form-control'>
						<div id='dcfmsgDiv' class='ot-form-control'>
							<div class='ot-form-control'>
								Yes, I would like to download...
								<div style='width: 1px; height: 2px; opacity: 1;'></div>
								<select id='dlfmsg' multiple readonly style='overflow-y:scroll;height:150px;width:600px;'></select>
								</div>
							</div>
						</div>
						<div id='dlfmsgDiv1' style='display: none;' class='ot-form-control'>
							<div class='ot-form-control'>
								Please select one more files to download.
							</div>
						</div>
						<div id='dlfmsgDiv2' style='display: none;' class='ot-form-control'>
							<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'>Download in progress...</div>
									</div>
								</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:pdlc(1);>
							OK
						</button>
						<button class='ot-btn ot-btn--is-secondary' onclick=javascript:pdlc(0);>
							Cancel
						</button>
					</div>
				</div>
			</div>
		</div>
	</div>
";
}

sub showUploadFileModal
{
print "
	<div class='ot-modal' id='uffdiv' style='z-index: 1501;'>
		<div class='ot-modal__background'>
		</div>
		<div class='ot-modal__content'>
			<div class='ot-modal__header'>
				 <div class='ot-level'>
                                 	<div class='ot-level__center'>
                                                <div class='ot-level__item'>
							<p class='title'>
								<input type='text' disabled class='ot-input' value='File upload'>
							</p>
							<div style='width: 345px; height: 1px; opacity: 1;'></div>
							<button class='ot-btn ot-btn--is-secondary' onclick=javascript:pufs();>
								Select files...
							</button>
						</div>
					</div>
				</div>
			</div>
			<div class='ot-modal__body'>
				<div class='ot-m-1' style='background: #white; width: 300px; height: 225px;'>
				<div class='ot-form-control'>
					<div id='u0' style='display: block; text-align: center' class='ot-form-control'>
						<img src='LocalWebLib/ill_workspace_xecm220.svg' style='height: 125px; width: 125px'>
                				<div id='ddz' class='text' ondrop='dropHandler(event);' ondragover='dragOverHandler(event);'>
                        				<p style='color: black;'>
                                				<span>Drop your files here or click \"Select files...\"</span>
                        				</p>
						</div>
					</div>
					<div id='r1' style='display: none;' class='ot-form-control'>
						<div id='ufNameDiv' class='ot-form-control'>
							<label class='ot-form-control__label' for='ufName'>
								<div class='ot-form-control__required-indicator'>*</div>
								File name
							</label>
							<div class='ot-form-control__input-group'>
								<input type='text' readonly class='ot-input' name='ufName' id='ufName' style='width: 35em' maxlength='100' size='75'>
								<span class='ot-form-control__error-icon'>
								</span>
							</div>
							<p class='ot-form-control__error-text'>
								File name is a required field
							</p>
						</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:pufc(1);>
						OK
					</button>
					<button class='ot-btn ot-btn--is-secondary' onclick=javascript:pufc(0);>
			  			Cancel
					</button>
		      		</div>
				<p hidden>
                               		<input size='0' type='password' name='r' id='r'>
                               	</p>       
    			</div>
  		</div>
	</div>
";
}

sub showConnectModal
{
	my( $active ) = @{{@_}}{qw/active/};

        my $modalActive = "ot-modal";
        $modalActive = "ot-modal ot-modal--is-active" unless( $active == 0 );

print "
	<div class='$modalActive' id='cndiv' style='z-index: 1500;'>
		<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, connecting...</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
";
}

sub showRecent
{
print "
	<div class='ot-list-tile ot-list-tile--is-simple' style='height: calc( 100vh - 200px );'>   
		<div class='ot-list-tile__header'>
	      		<div class='ot-list-tile__title'>Recent</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'>

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

#
# Main
#
$wtfb->showHeader( request=>$config{'webtopShowFileShare'}, mode=>1, isCorporate=>$c);
showPanel();
showDownloadFileModal();
showBackErrorModal();
showDeleteConfirmModal();
showCreateFolderModal();
showUploadFileModal();
showRenameFileModal();
showMoveFileModal();
$wtfb->showFooter();

print "
        <script type='text/javascript'>
                wmsg(0);
	</script>
";

exit(0);
