<?php
include 'icl/showkeyfilepad.inc.php';
include 'icl/showuserhelptopics.inc.php';

function showuser($userid=null){
	global $smskey;
	global $userrolelocks;
	
	if (!isset($userid)) $userid=GETVAL('userid');
	
	$user=userinfo();
	if (!$user['groups']['accounts']) die('Access denied');
	$gsid=$user['gsid']+0;
	
	$myuserid=$user['userid']+0;
	
	global $db;
	global $userroles;
	
	$query="select * from ".TABLENAME_USERS." where userid=? and gsid=? ";
	$rs=sql_prep($query,$db,array($userid,$gsid));
	
	if (!$myrow=sql_fetch_array($rs)) die('This user record has been removed');
	
	$login=$myrow['login'];
	$dispname=$myrow['dispname'];
	$active=$myrow['active'];
	$virtual=$myrow['virtualuser'];
	$passreset=$myrow['passreset'];
	$groupnames=$myrow['groupnames'];
	$groups=explode('|',$groupnames);
	$needcert=$myrow['needcert'];
	$certname=$myrow['certname'];
	if ($certname=='') $certname='<em>not set</em>';
	$needkeyfile=$myrow['needkeyfile'];
	
	$usesms=$myrow['usesms'];
	$smscell=$myrow['smscell'];
	
	if ($smskey=='') $usesms=0;
		
	header('newtitle: '.tabtitle('<img src="imgs/t.gif" class="ico-user">'.htmlspecialchars($login)));
	
	makechangebar('user_'.$userid,"updateuser($userid,'".makegskey('updateuser_'.$userid)."');");
?>



<div class="section">
	<div class="sectiontitle"><?echo htmlspecialchars($login);?></div>

	<div class="col">


	<div class="inputrow">
		<div class="formlabel"><?tr('username');?>:</div>
		<input class="inpmed" id="login_<?echo $userid;?>" value="<?echo htmlspecialchars($login);?>" onchange="marktabchanged('user_<?echo $userid;?>');" onblur="if (gid('dispname_<?echo $userid;?>').value==''&&this.value!='') {var val=this.value.charAt(0).toUpperCase()+this.value.slice(1);gid('dispname_<?echo $userid;?>').value=val;}">
	</div>
	<div class="inputrow">
		<div class="formlabel"><?tr('dispname');?>:</div>
		<input class="inpmed" id="dispname_<?echo $userid;?>" value="<?echo htmlspecialchars($dispname);?>" onchange="marktabchanged('user_<?echo $userid;?>');" onfocus="this.select();document.hotspot=this;">
	</div>
	<div class="inputrow">
		<input type="checkbox" id="active_<?echo $userid;?>" <?if ($active) echo 'checked';?>  onchange="marktabchanged('user_<?echo $userid;?>');"> <label for="active_<?echo $userid;?>"><?tr('account_active');?></label>
		&nbsp;&nbsp;
		<input type="checkbox" id="virtual_<?echo $userid;?>" <?if ($virtual) echo 'checked';?>  onchange="marktabchanged('user_<?echo $userid;?>');" onclick="if (this.checked) gid('userpasses_<?echo $userid;?>').style.display='none'; else gid('userpasses_<?echo $userid;?>').style.display='block';"> <label for="virtual_<?echo $userid;?>"><?tr('account_virtual');?></label>
	</div>
	<div id="userpasses_<?echo $userid;?>" style="<?if ($virtual) echo 'display:none;';?>">
	<div class="inputrow">
		<div class="formlabel"><?tr('new_password');?>: &nbsp; &nbsp; <span style="font-weight:normal;color:#ab0200;" id="passwarn_<?echo $userid;?>"></span></div>
		<input class="inp" autocomplete="new-password" id="newpass_<?echo $userid;?>" onkeyup="ajxjs(self.checkpass,'accounts.js');_checkpass(this,'passwarn_<?echo $userid;?>');" type="password" onchange="marktabchanged('user_<?echo $userid;?>');ajxjs(self.checkpass,'accounts.js');checkpass(this,'passwarn_<?echo $userid;?>');">
	</div>
	<div class="inputrow">
		<div class="formlabel"><?tr('repeat_password');?>:</div>
		<input class="inp" id="newpass2_<?echo $userid;?>" type="password" onchange="marktabchanged('user_<?echo $userid;?>');">
	</div>
	
	<div class="inputrow">
		<input type="checkbox" onclick="marktabchanged('user_<?echo $userid;?>');" id="passreset_<?echo $userid;?>" <?if ($passreset) echo 'checked';?>> <label for="passreset_<?echo $userid;?>"><?tr('account_login_reset');?></label>
	</div>

	<div class="inputrow" id="cardsettings_<?echo $userid;?>">
		<div class="formlabel">ID Card: &nbsp; 
			<span style="font-weight:normal;" id="cardstatus_<?echo $userid;?>"><?echo $certname;?></span> <a class="labelbutton" onclick="loadsmartcard(<?echo $userid;?>);">load card</a>
			<span style="display:none;"><textarea id="cert_<?echo $userid;?>" value=""></textarea></span>
		</div>
		<input onchange="marktabchanged('user_<?echo $userid;?>');" type="checkbox" id="needcert_<?echo $userid;?>" <?if ($needcert) echo 'checked';?>> <label for="needcert_<?echo $userid;?>">card must be present at sign-in</label>

	</div>
	
	<div class="inputrow">
		<input type="checkbox" <?if ($smskey=='') echo 'disabled';?> id="usesms_<?echo $userid;?>" <?if ($usesms) echo 'checked';?> onclick="marktabchanged('user_<?echo $userid;?>');if (this.checked) {gid('smscellview_<?echo $userid;?>').style.display='block';gid('smscell_<?echo $userid;?>').focus();} else gid('smscellview_<?echo $userid;?>').style.display='none';">
		<label style="<?if ($smskey=='') echo 'color:#666666;';?>" for="usesms_<?echo $userid;?>">use SMS code to enhance login</label>
	</div>
	
	<div id="smscellview_<?echo $userid;?>" style="padding-left:30px;display:none<?if ($usesms) echo 'a';?>;">
		<div class="inputrow">
			Cell: <input class="inpmed" id="smscell_<?echo $userid;?>" value="<?echo htmlspecialchars($smscell);?>" onchange="marktabchanged('user_<?echo $userid;?>');">
		</div>
	</div>
	
	<div class="inputrow">
		<input onchange="marktabchanged('user_<?echo $userid;?>');if (this.checked) gid('keyfileview_<?echo $userid;?>').style.display='block'; else gid('keyfileview_<?echo $userid;?>').style.display='none';" type="checkbox" id="userneedkeyfile_<?echo $userid;?>" <?if ($needkeyfile) echo 'checked';?>> <label for="userneedkeyfile_<?echo $userid;?>">enhance login with a key file</label>
		<?makehelp('helpuserneedkeyfile_'.$userid,'once set, the generated key file has to be attached each time you sign in.');?>
	</div>
	
	<div class="inputrow">
		<div class="formlabel"><?tr('account_roles');?>:</div>
		<?
		$warning=0;
		foreach ($userroles as $role=>$label){
		?>
		<div style="padding-left:10px;margin-bottom:3px;<?if (in_array($role,$userrolelocks)&&!in_array($role,$groups)&&!$user['groups'][$role]) echo 'display:none;';?>">
			<input <?if (in_array($role,$userrolelocks)&&!$user['groups'][$role]) echo 'disabled';?> onclick="marktabchanged('user_<?echo $userid;?>');" type="checkbox" id="userrole_<?echo $role;?>_<?echo $userid;?>" <?if (in_array($role,$groups)) echo 'checked';?>> 
			<label for="userrole_<?echo $role;?>_<?echo $userid;?>"><?echo $label;?><?if ($myuserid==$userid&&in_array($role,$userrolelocks)&&$user['groups'][$role]) {$warning=1;echo ' <span style="color:#ab0200;">*</span>';}?></label>
		</div>
		<?	
		}
		?>
	</div>	
	</div><!-- userpasses -->
	
	<?if ($warning){
	?>
	<div class="warnbox">
		* You are editing your own access. Once you uncheck the items that are marked with asterisks, you cannot re-grant yourself.
	</div>
	<?	
	}?>
	
	<div class="inputrow">
		<button onclick="updateuser(<?echo $userid;?>,'<?emitgskey('updateuser_'.$userid,'accounts');?>');"><?tr('button_update');?></button>
		&nbsp; &nbsp;
		<button class="warn" onclick="deluser(<?echo $userid;?>,'<?emitgskey('deluser_'.$userid,'accounts');?>');"><?tr('button_delete');?></button>
	</div>


	</div>
	
	<div class="col">
		<div id="keyfileview_<?echo $userid;?>" style="display:none<?if ($needkeyfile) echo 'a';?>;">
			<div class="sectionheader">Key File</div>
			<?showkeyfilepad('keyfileeditor_'.$userid,$userid);?>
		</div>
	</div>

	<div class="clear"></div>

	<?if ($userid==$myuserid){?>
	<div id="muserhelptopics_<?echo $userid;?>">
		<?showuserhelptopics();?>
	</div>
	<?}?>
		
	
</div>
<?
}
