<?php

include 'icl/showuser.inc.php';

function adduser(){
	global $userroles;
	global $dbsalt;
	
	$user=userinfo();
	if (!$user['groups']['accounts']) die('Access denied');
	$gsid=$user['gsid']+0;
	
	checkgskey('adduser');
		
	$login=GETSTR('login');
	$dispname=strip_tags(GETSTR('dispname'));
	$active=GETVAL('active');
	$virtual=GETVAL('virtual');
	$passreset=GETSTR('passreset');
	
	$newpass=file_get_contents('php://input');
	$np=encstr(md5($dbsalt.$newpass),$newpass.$dbsalt);
		
	$groupnames=GETSTR('groupnames');	
	
	if ($virtual){
		$groupnames='users';
		$np=md5(time().rand(999,9999).'block');
		$passreset=0;
	}
	
	global $db;
	
	$query="select * from ".TABLENAME_USERS." where login like '$login'";
	$rs=sql_query($query,$db);
	if ($myrow=sql_fetch_assoc($rs)) apperror('User already exists. Use a different login.');
	
	$query="insert into ".TABLENAME_USERS." (gsid,login,dispname,active,virtualuser,passreset,groupnames,password) values ($gsid,'$login','$dispname',$active,$virtual,'$passreset','$groupnames','$np') ";
	$rs=sql_query($query,$db);
	$userid=sql_insert_id($db,$rs)+0;

	if (!$userid) {
		apperror('Error creating User record');
	}
	
	logaction("added ".($virtual?'Virtual':'')." User #$userid $login",array('userid'=>$userid,'login'=>"$login"));
	
	header('newrecid:'.$userid);
	header('newkey:user_'.$userid);
	header('newparams:showuser&userid='.$userid);
	header("newloadfunc: if (!document.smartcard) gid('cardsettings_".$userid."').style.display='none';reloadview('core.users','userlist');");
	
	showuser($userid);
}

