<?php //session_start();  ?>
<?php
if (!isset($_SESSION["adminid"]) || $_SESSION["adminid"] == "") {
    exit();
}
if (file_exists("includes/toolcheck.php")) {
    require("includes/toolcheck.php");
} else {
    require("../includes/toolcheck.php");
}
//var_dump($_SESSION);
?>
<table width="980" height="20" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 0px solid #DDDDDD; border-right: 0px solid #DDDDDD; text-align: left;">
    <?php
//include __DIR__ . '/../config.php';

    $aid = (isset($_GET['id']) && is_numeric($_GET['id']) && $_SESSION["adminlevel"] == "0") ? $_GET['id'] : $_SESSION["adminid"];

    $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
    mysql_select_db($dbname, $conn);
    $usql = mysql_query("SELECT * FROM cmum_udb WHERE addedby = {$aid} ORDER BY id ASC");
//var_dump(mysql_error());
    $cspxmloutput = "<xml-user-manager ver=\"1.0\">\n";

    while ($line = mysql_fetch_array($usql)) {
        if ($line["name"] == "") {
            $name = "";
        } else {
            $name = "name=\"" . $line["name"] . "\" ";
        }

        if ($line["password"] == "") {
            $password = "";
        } else {
            $password = "password=\"" . $line["password"] . "\" ";
        }

        if ($line["displayname"] == "") {
            $displayname = "";
        } else {
            /*if ($line["usrgroup"] == "") {
                $usrgroup = "";
            } else {
                $usrgroup = "[" . $line["usrgroup"] . "] ";
            }*/
            $displayname = "display-name=\"" .  $line["displayname"] . "\" ";
        }

        if ($line["ipmask"] == "") {
            $ipmask = "";
        } else {
            $ipmask = "ip-mask=\"" . $line["ipmask"] . "\" ";
        }

        if ($line["profiles"] == "") {
            $profiles = "";
        } else {
            $dbprof = unserialize($line["profiles"]);
            if ($dbprof <> "") {
                $profvalues = "";
                foreach ($dbprof as $printprof) {
                    $sql2 = mysql_query("SELECT * FROM cmum_profiles WHERE id='" . $printprof . "'");
                    $profname = mysql_fetch_array($sql2);
                    $profvalues = $profvalues . $profname["value"] . " ";
                }
                $profiles = "profiles=\"" . trim($profvalues) . "\" ";
                $dbprof = "";
                $printprof = "";
                $profvalues = "";
            } else {
                $profiles = "";
            }
        }

        if ($line["maxconnections"] == "") {
            $maxconnections = "";
        } else {
            $maxconnections = "max-connections=\"" . $line["maxconnections"] . "\" ";
        }

        if ($line["admin"] == "") {
            $admin = "";
        } else {
            $admin = "admin=\"" . $line["admin"] . "\" ";
        }

        if ($line["enabled"] == "") {
            $enabled = "";
        } else {
            $enabled = "enabled=\"" . $line["enabled"] . "\" ";
        }

        if ($line["mapexclude"] == "") {
            $mapexclude = "";
        } else {
            $mapexclude = "map-exclude=\"" . $line["mapexclude"] . "\" ";
        }

        if ($line["debug"] == "") {
            $debug = "";
        } else {
            $debug = "debug=\"" . $line["debug"] . "\" ";
        }

        if ($line["email"] == "") {
            $email = "";
        } else {
            $email = "email-address=\"" . $line["email"] . "\" ";
        }

        if ($line["ecmrate"] == "") {
            $ecmrate = "";
        } else {
            $ecmrate = "ecm-rate=\"" . $line["ecmrate"] . "\" ";
        }

        if ($line["startdate"] == "") {
            $startdate = "";
        } else {
            $startdate = "start-date=\"" . $line["startdate"] . "\" ";
        }

        if ($line["expiredate"] == "") {
            $expiredate = "";
        } else {
            $expiredate = "expire-date=\"" . $line["expiredate"] . "\" ";
        }

        if ($line["customvalues"] == "") {
            $customvalues = "";
        } else {
            $customvalues = $line["customvalues"] . " ";
        }

        $cspxmloutput = $cspxmloutput . "<user " . $name . $password  . $email . $displayname . $profiles . $maxconnections . $enabled . $startdate . $expiredate . "/>\n";
    }

    $cspxmloutput = $cspxmloutput . "</xml-user-manager>\n";
    mysql_close($conn);
    ?>
    <tr>
        <td height="20" colspan="4">
            <form name="exportusersxml">
                <fieldset>
                    <legend>Exportar Usuarios (CSP XML)</legend>
                    <table width="100%" valign="top">
                        <td width="100%" valign="top">
                            <textarea name="cspxmlusers" cols="87" rows="15" tabindex="1" class="form-control" ondblclick="select()"><?php print(trim($cspxmloutput)); ?></textarea><br>
                            <input type="button" value="Selecionar todos" id="submit" class="btn btn-default" onclick="cspxmlusers.select()">
                        </td>
                    </table>
                </fieldset>
            </form>
        </td>    
    </tr>
</table>
