<?php
header("Content-type: text/xml; charset=UTF-8");
// Enter your server info below before use
// Enter your server info below before use

require_once '../config.php';
require_once "../includes/functions.php";

$conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die("ERROR! Não Conectado ao Banco de Dados..");
mysql_select_db($dbname, $conn);
$sql = mysql_query("SELECT genxmlkey,mult_perf,ips_perm_acesso_key FROM cmum_settings WHERE id='1'");
$setup = mysql_fetch_array($sql);
//Validando ips que podem acessar o genxml diretamente.
$ips_permitidos = explode(';',$setup['ips_perm_acesso_key']);

//var_dump($ips_permitidos);
if(in_array((string) $_SERVER['REMOTE_ADDR'], $ips_permitidos) === false){
    exit('<vazio>0 Logins</vazio>');    
}
//verificando a chave 
if ($setup["genxmlkey"] <> "") {
    if (!isset($_GET["key"])) {
        mysql_close($conn);
        exit;
    }
    if ($_GET["key"] == "") {
        mysql_close($conn);
        exit;
    }
    if ($setup["genxmlkey"] <> $_GET["key"]) {
        mysql_close($conn);
        exit;
    }
}
if ($dbhost == "" or $dbname == "" or $dbuser == "") {
    print("Enter your server info before use");
    exit;
}
eval(base64_decode("aWYoIWZ1bmN0aW9uX2V4aXN0cygiaXNfcGFpbmVsIikpew0KICAgICAgICBleGl0KCJFcnJvIEludGVybm8iKTsgICAgICAgIA0KICAgIH0="));
print("<xml-user-manager ver=\"1.0\">\n");

$usql = mysql_query("SELECT u.*,a.profiles_pm,a.level FROM cmum_udb AS u INNER JOIN cmum_admins AS a ON u.addedby = a.id WHERE u.enabled='true' AND a.enabled='true'"); // Usuarios para o CSP

//Parametros xml-user-manager
$name = "";
$password = "";
$displayname = "";
$profiles = "";
$maxconnections = "";
$enabled = "";
$debug = "";
$email = "";
$ecmrate = "";
$startdate = "";
$expiredate = "";
$customvalues = "";
$addedby = "";
$profiles_pm_rev = "";
$level_rev = "";
eval(base64_decode("aWYoIWZ1bmN0aW9uX2V4aXN0cygidiIpKXsNCiAgICAgICAgZXhpdCgiRXJybyBJbnRlcm5vIik7ICAgDQogICAgICAgIH0="));
while ($line = mysql_fetch_array($usql)) {
    $agora = date("Y-m-d H:i:s");
    if ($line["enabled"] == 'true' && $agora <= $line["expiredate"]) {
        if ($line["name"] != "") {
            $name = "name='{$line["name"]}'";
        }
        if ($line["password"] != "") {
            $password = "password='{$line["password"]}'";
        }

        /*if ($line["displayname"] != "") {
            //$displayname = "display-name='{$line["displayname"]}'";
            $displayname = "display-name='{$line["name"]}'"; // Muda para o login 18/06/2014
        }*/


        if ($line["profiles"] != "") {
            $dbprof = unserialize($line["profiles"]);
            //perfis permitidos para revenda
            $profiles_pm_rev = unserialize($line["profiles_pm"]);
            $level_rev = $line["level"];
            if ($dbprof <> "") {
                $profvalues = "";
                foreach ($dbprof as $printprof) {
                    if ($level_rev == "0") {
                        $sql2 = mysql_query("SELECT * FROM cmum_profiles WHERE id='" . $printprof . "'");
                        $profname = mysql_fetch_array($sql2);
                        $profvalues = $profvalues . $profname["value"] . " ";
                    } else {
                        if (array_search($printprof, $profiles_pm_rev) !== false) {
                            $sql2 = mysql_query("SELECT * FROM cmum_profiles WHERE id='" . $printprof . "'");
                            $profname = mysql_fetch_array($sql2);
                            $profvalues = $profvalues . $profname["value"] . " ";
                        }
                    }
                }

                //não lista os profiles vazios.
                if ($profvalues == "" || $profvalues == null || strlen($profvalues) < 2) {
                    continue;
                }

                $profiles = "profiles=\"" . trim($profvalues) . "\" ";
                $dbprof = "";
                $printprof = "";
                $profvalues = "";
            } else {
                $profiles = "";
            }
        }

        if ($line["maxconnections"] != "") {
            $maxconnections = "max-connections='{$line["maxconnections"]}'";
        }
        if ($line["enabled"] != "") {
            $enabled = "enabled='{$line["enabled"]}'";
        }

        if ($line["debug"] != "") {
            $debug = "debug='{$line["debug"]}'";
        }
        if ($line["email"] != "") {
            $email = "email-address='{$line["email"]}'";
        }

        if ($line["ecmrate"] != "") {
            $ecmrate = "ecm-rate='{$line["ecmrate"]}'";
        }
        if ($line["startdate"] != "") {
            $startdate = "start-date='{$line["startdate"]}'";
        }
        if ($line["expiredate"] != "") {
            $expiredate = "expire-date=\"" . $line["expiredate"] . "\" ";
            $agora = date("Y-m-d H:i:s");
            if ($agora >= $line["expiredate"]) {
                $enabled = 'enabled="false"';
            }
        }
        if ($line["customvalues"] != "") {
            $customvalues = $line["customvalues"] . " ";
        }
        if ($line["addedby"] != "") {
            $addedby = "addedby='{$line["addedby"]}'";
        }
        //Campo Email Retirado - 12/10/
        //Mudança Login Passado Como Nome 18/06/2014
        print(reversechars(preg_replace("/&[A-Za-z\S]+;/", "", "<user $name $password $addedby $profiles $maxconnections $enabled $startdate $expiredate $customvalues />\n")));
        $name = "";
        $password = "";
        //$displayname = "";
        $addedby = "";
        $profiles = "";
        $maxconnections = "";
        $enabled = "";
        $ecmrate = "";
        $startdate = "";
        $expiredate = "";
        $customvalues = "";
    }
}
print("</xml-user-manager>");
mysql_close($conn);
?>
