<?php

//////////////////////////////////////////////////////////////
//===========================================================
// user_sessions_theme.php
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       8th Mar 2010
// Time:       23:00 hrs
// Site:       https://www.virtualizor.com/ (SOFTACULOUS VIRTUALIZOR)
// ----------------------------------------------------------
// Please Read the Terms of use at https://www.virtualizor.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Ltd.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('VIRTUALIZOR')){

	die('Hacking Attempt');

}

function user_sessions_theme(){

global $theme, $globals, $kernel, $user_sessions, $l, $error, $done, $SESS;

softheader($l['<title>']);
					
echo page_header(__('User Sessions'), 'fas fa-users');

$breadcrumbs = array( 'dashboard', 'lm_users', 'lms_user_sessions');

echo get_breadcrumb($breadcrumbs);

error_handle($error);

echo '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
var from_modal = 0;

function vpsmodal(sid){
	$("#conf_modal").modal("show");

	$("#del_user_list").html($("#"+sid).html());
	$("#saveset").attr("onclick", "from_modal=1;$(\"#conf_modal\").modal(\"hide\");action_chosen(\'delete\', \'"+sid+"\');");
};

function action_chosen(action, uid, ele){
	
	action = action || "";
	uid = uid || 0;
	
	if(action == ""){
		if($(ele).parent().prev().find("select").val() == 0){
				showalert("'.__J('No action selected !').'","1");
				return false;
		}else{
			action = $(ele).parent().prev().find("select").val();
		}
	}
	
	// List of ids to delete
	var usr_list = new Array();
	
	if(uid < 1){
		
		$(".ios:checked").each(function(){
			usr_list.push($(this).val());
		});
		
	}else{
		
		usr_list.push(uid);
		
	}
	
	if(usr_list.length < 1){
		showalert("'.__J('Nothing selected !').'","1");
		return false;
	}
	
	// Just check for delete action
	if(action == "delete" && from_modal == 0){
		vpsmodal();
		from_modal = 1;
		return true;
	}
	
	// To delete the user
	if(action == "delete"){
		//console.log(usr_list)
		showalert("'.__J('Are you sure, you want to delete the selected item(s) ?').'" ,"3" ,function(confirm){
			if(confirm == false){
				return false;
			}else{
				var finalData = new Object();
				finalData["delete"] = usr_list.join(",");
				perform_action(finalData);
			}
		});

	// Not a valid action 
	}else{
		showalert("'.__J('No action selected !').'","1");
		return false;
	}
	
	return false;
}

function perform_action(finalData, uid = 0){
	
	//alert(finalData);
	//return false;
	
	$("#progress_bar").show();
	$.ajax({
		type: "POST",
		url: "'.$globals['index'].'act=user_sessions&api=json",
		data : finalData,
		dataType : "json",
		success: function(data){
			$("#progress_bar").hide();
			if(!empty(uid)){
				$("#showload_"+uid).hide();
			}
			if("done" in data){
				setTimeout(function(){showalert("'.__J('The action was completed sucessfully').'");
				$("#alert-modal").on("hidden.bs.modal", function (e) {
				location.reload(true);
				});} ,800);
			}
			
			if("error" in data){
				setTimeout(function(){showalert(data["error"],"1");
				$("#alert-modal").on("hidden.bs.modal", function (e) {
				location.reload(true);
				});} ,800);
			}
		},
		error: function(data) {
			if(!empty(uid)){
				$("#showload_"+uid).hide();
			}
			$("#progress_bar").hide();
			return false;
		}
	});
	
	return false;
}

// ]]></script>
<div class="card p-2 mx-3">';

if($done){
	show_alert(__('User session(s) was deleted successfully'));
}

if(empty($user_sessions)){
	// Nothing to do here 
	// technically it is not possible to come here if ANY user is logged in!!!
}else{
	echo'<div class="row mx-auto w-100 mb-3">
	<div class="col-sm-8 col-lg-8 col-md-6 d-flex align-items-center p-0">';
	page_links($globals['num_res'], $globals['cur_page'], $globals['reslen']);
	echo get_num_records($globals['num_res']).'
	</div>
	<!-- 
	<div class="col-sm-4 col-lg-4 col-md-6 text-right d-flex align-items-center">
		<div class="bottom-menu float-right w-100">
			<div class="row my-auto">
				<div class="p-0">
					<label>'.__('With Selected').'</label>
				</div>
				<div class="col p-0">
					<select name="user_task_select" class="custom-select" id="user_task_select">
						<option value="0">---</option>
						<option value="delete">'.__('Delete').'</option>
					</select>
				</div>
				<div class="pl-2">
					<button type="submit" class="go_btn" id="user_submit" name="user_submit" onclick="action_chosen(\'\', 0, this); return false;">'.__('GO').'</button>
				</div>
			</div>
		</div>
	</div> 
	-->
</div>
<form accept-charset="'.$globals['charset'].'" name="multi_users" id="multi_users" method="post" action="" class="form-horizontal">
<div class="w-100 mx-auto sticky">
<table class="table table-hover tablesorter common-table">
	<tr class="table_heading">
		<!--<th class="z-index-1 min_width" align="center">
			'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
		</th>-->
		<!-- <th  class="min_width" align="center">'.__('ID').'</th> -->
		<th align="center">'.__('Email').'</th>
		<th align="center">'.__('Last Accessed time').'</th>
		<th align="center" width="100">'.__('User Type').'</th>
		<th align="center" width="150">'.__('Remote Address').'</th>
		<th align="center" width="300">'.__('User Agent').'</th>
		<th class="text-right pr-3" colspan="3">'.__('Manage').'</th>
	</tr>
	<tbody>';

$i = 1;
$user = array('-1'=> 'admin', 'user', 'admin', 'cloud_users');
$user_lang['user_-1'] = __('root');
$user_lang['user_1'] = __('Admin');
$user_lang['user_2'] = __('Cloud');
$user_lang['user_0'] = __('User');
// r_print($user_sessions);
foreach($user_sessions as $k => $v){

	echo '<tr>
			<!-- <td width="20" valign="middle" align="center">
				'.get_checkbox('user_list[]', '', '', array('chk_it' => 0), $k).'
			</td> -->
			<!-- <td align="center">'.$v['sid'].'</td> -->
			<td id="'.$v['sid'].'">'.$v['email'].'</td>
			<td id="'.$v['sid'].'">'.datify($v['last_updated'], 1, 1).'</td>
			<td align="left" class="user_table d-flex align-items-end">
				<img class="mr-2" src="'.$theme['svg'].''.$user[$v['type']].'.svg" title="'.$user_lang['user_'.$v['type']].'">
				<span class="usr_small">'.(empty($user_lang['user_'.$v['type']])?'null':$user_lang['user_'.$v['type']]).'</span>
			</td>
			<td>'.$v['data']['ip'].'</td>
			<td>'.$v['data']['user-agent'].'</td>
			<td width="20" valign="middle" align="center">
				<div class="badge badge_container">
					'.($SESS['sid'] === $v['sid'] ? '' : '<a class="badge badge-trash" href="javascript:void(0);" onclick="vpsmodal(\''.$k.'\');"  title="'.__('Delete Recipe').'"><i class="fas fa-trash"></i></a>').'
				</div>
			</td>
		</tr>';
	
	$i++;
}

echo '</tbody></table>
</div>';
echo'<div class="row mx-auto w-100 mt-5">
	<div class="col-sm-8 col-lg-8 col-md-6 mb-3 p-0">';
		page_links($globals['num_res'], $globals['cur_page'], $globals['reslen']);
	echo'
	</div>
	<!--
	<div class="col-sm-4 col-lg-4 col-md-6 text-right mb-3">
		<div class="bottom-menu float-right w-100">
			<div class="row my-auto">
				<div class="p-0">
					<label>'.__('With Selected').'</label>
				</div>
				<div class="col p-0">
					<select name="user_task_select" class="custom-select" id="user_task_select">
						<option value="0">---</option>
						<option value="delete">'.__('Delete').'</option>
					</select>
				</div>
			</div>
		</div>
	</div>
	-->
</div>
</form>
<div id="progress_bar" class="my-3" style="display:none;">
	<br />
	<center>
		<font id="progress_txt" size="4">'.__('Please wait, Action in Progress.').'</font>
	</center>
	<div class="my-2" align="center">
		<div class="progress w-50">
			<div class="progress-bar bg-primary progress-bar-striped progress-bar-animated w-100">
			</div>
		</div>
	</div>
	<center>
		'.__('You will be notified once the Action is Completed.').'
	</center>
</div>

<div class="modal fade" role="dialog" id="conf_modal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header" id="conf_modal_head">				
				<span class="fhead text-always-white">'.__('Delete Session').'</span>
				<button type="button" class="close text-always-white" data-dismiss="modal" aria-hidden="true">&times;</button>
			</div>
			<div class="modal-body" id="conf_modal_body">
				<div class="d-flex font-weight-bold">'.__('Following user will be logged out from current session:').'</div>
				<div class="d-flex"><span id="del_user_list"></span></div>
			</div>
			<div class="modal-footer">
				<button type="button" id="saveset" class="go_btn">'.__('Delete').'</button>
			</div>
		</div>
	</div>
</div>';  

}// End of else

echo '</div><br>';

softfooter();

}
?>