<?
// <user-manager class="com.bowman.cardserv.XmlUserManager" allow-on-failure="true" log-failures="true" >
// <auth-config>
// <update-interval>3</update-interval>
// <user-source name="SKY">
//    <user-file-url>http://localhost/cspadm/cspadm.php?key=XXXXX</user-file-url>
// </user-source>
// </auth-config>
// </user-manager>


// Edit this 4 strings to match your system
require_once('adodb/adodb.inc.php'); //biblioteca necessaria para trabalhar com adodb
require_once('Amfphp/Services/conexao.php');
require_once('Amfphp/Services/funcoes.php');
if (empty($key)){
	echo "ERRO. Dados nao encontrado ";
} else {
	$key = anti_injection($key);
	$dados = explode(".", $key);
	$key = $dados[0];
	$profile = $dados[1];
	
	$con = new conexao();
	$sql = "select idempresa from empresa where kkey='$key' and situacao <> 'Inativo'";
	$resultado = $con->banco->Execute($sql);
	$registro = $resultado->FetchNextObject();
	$idempresa = $registro->IDEMPRESA;
	//$sql = "select csp_user.name, csp_user.password, csp_user.displayname, profile.descricao as profile, csp_user.maxconnections, csp_user.enabled, csp_user.expiredate, csp_user.expirehora, provedor.clienteteste from csp_user, profile, provedor where csp_user.idprofile = profile.idprofile and csp_user.idempresa = '$idempresa' and csp_user.situacao='Ativo' and csp_user.enabled='Ativo' and csp_user.del='NAO' and csp_user.id = provedor.idprovedor order by csp_user.name AS";
	$sql = "select csp_user.name, csp_user.password, csp_user.displayname, profile.descricao as profile, csp_user.maxconnections, csp_user.enabled, csp_user.expiredate, csp_user.expirehora, provedor.clienteteste from csp_user, profile, provedor where csp_user.idprofile = profile.idprofile and csp_user.idempresa = '$idempresa' and csp_user.enabled = 'Ativo' and csp_user.situacao='Ativo' and csp_user.id = provedor.idprovedor and csp_user.del='NAO'";
	if ($profile <> ""){
		$sql .= " and csp_user.idprofile in (select idprofile from profile where idempresa=csp_user.idempresa and descricao like '%$profile%')";
	}
	$sql .= " order by csp_user.name ASC";
	$resultado = $con->banco->Execute($sql);
	$xml = "<xml-user-manager ver=\"1.0\">\n";
	while($registro = $resultado->FetchNextObject()){
	 //  if($registro->EXPIREDATE >= date("Y-m-d")){
	//	if ($registro->ENABLED = "Ativo"){
	//		$situacao = "true";
	//	} else {
	//		$situacao = "false";
	//	}				
				
		if ($registro->NAME == "") :
			$name = "";
		else :
			$name = "name=\"".$registro->NAME."\" ";
		endif;
			
		if ($registro->PASSWORD == "") :
			$password = "";
		else :
			$password = "password=\"".$registro->PASSWORD."\" ";
		endif;
		
		if ($registro->DISPLAYNAME == "") :
			$displayname = "";
		else :
			$displayname = "display-name=\"".$registro->NAME."\" ";
		endif;
			
		if ($registro->PROFILE == "") :
			$profiles = "";
		else :
			$profiles = "profiles=\"".$registro->PROFILE."\" ";
		endif;
		
		if ($registro->MAXCONNECTIONS == "") :
			$maxconnections = "";
		else :
			$maxconnections = "max-connections=\"".$registro->MAXCONNECTIONS."\" ";
		endif;
		if ($registro->ENABLED == "Inativo" || $registro->ENABLED == "") :
		$enabled = "enabled=\"false\"";
		else :
			if ($registro->EXPIREDATE == "") :
				$enabled = "enabled=\"false\" "; 				
			else :
				if ($registro->EXPIREDATE < date("Y-m-d")) :
					$enabled = "enabled=\"false\" "; 			
				endif;
				if ($registro->EXPIREDATE >= date("Y-m-d")) :
					$enabled = "enabled=\"true\" ";					
				endif;
				if ($registro->EXPIREDATE == date("Y-m-d") && $registro->CLIENTETESTE=="1") :
					if ($registro->EXPIREHORA >= date("Hi")) :
						$enabled = "enabled=\"true\" "; 						
					endif;
					if ($registro->EXPIREHORA < date("Hi")) :
						$enabled = "enabled=\"false\" "; 											
					endif;
			endif;
		endif;
endif;		
		
		$xml .="<user ".$name.$password.$displayname.$profiles.$maxconnections.$enabled."/>\n";
	 }	
	
	$xml .= "</xml-user-manager>";
	echo "$xml";
}
?>
