<?php
if (file_exists("includes/toolcheck.php")) {
	require("includes/toolcheck.php");
} else {
	require("../includes/toolcheck.php");
}
?>
<table width="980" height="20" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 1px solid #DDDDDD; border-right: 1px solid #DDDDDD; text-align: left;">
<?php
$conn = @mysql_connect($dbhost,$dbuser,$dbpass) or sqlerror();
mysql_select_db($dbname, $conn);
$usql = mysql_query("SELECT * FROM cmum_udb ORDER BY id ASC");

$csvoutput = "";

while($line=mysql_fetch_array($usql)) {
	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 = trim($profvalues);
			$dbprof = "";
			$printprof = "";
			$profvalues = "";
		} else {
			$profiles = "";
		}
	}
	
	$comment = str_replace( "\n", " ", $line["comment"]);
	$comment = str_replace( "\r", " ", $comment);
	$comment = preg_replace("/\s+/", " ", $comment);
	
	$addname = getadminname($line["addedby"],0);
		if ($addname == "<i>Deleted admin!</i>") {
			$addname = "";
		}
	$chgname = getadminname($line["changedby"],0);
		if ($chgname == "<i>Deleted admin!</i>") {
			$chgname = "";
		}
		
	$csvoutput = $csvoutput.$line["name"].";".$line["password"].";".$line["displayname"].";".$line["ipmask"].";".$profiles.";".$line["maxconnections"].";".$line["admin"].";".$line["enabled"].";".$line["mapexclude"].";".$line["debug"].";".$comment.";".$line["email"].";".$line["usrgroup"].";".$line["customvalues"].";".$line["ecmrate"].";".$line["startdate"].";".$line["expiredate"].";".$line["added"].";".$addname.";".$line["changed"].";".$chgname.";\n";
}

mysql_close($conn);
?>
  <tr bgcolor="#FFFFFF">
    <td width="15" height="20">&nbsp;</td>
    <td height="20" colspan="4">
    <form name="exportuserscsv">
    <fieldset>
	<legend>Usuarios exportar para CSV</legend>
    <table width="100%" valign="top">
    <td width="100%" valign="top">
    <textarea name="csvusers" cols="87" rows="15" tabindex="1" ondblclick="select()"><?php print(trim($csvoutput)); ?></textarea><br>
    <input type="button" value="Select all" id="submit" onclick="csvusers.select()">
    </td>
    </table>
    </fieldset>
    </form>
    </td>
    <td width="15" height="20">&nbsp;</td>
  </tr>
</table>
