<?php
require("config.php");
require("includes/debug.php");
require("includes/functions.php");
require("includes/logincheck.php");
require("includes/settings.php");
require("includes/admincheck.php");

$profileexists = "";
$nonamevalue = "";

if (isset($_GET["id"]) <> "") {
	$profileid = $_GET["id"];
} else {
	header( "Location: profiles.php" );
}

if (isset($_POST["baction"]) and $_POST["baction"] == "Save") {
	$profileid = $_POST["profileid"];
	$profilename = $_POST["profilename"];
	$pname = trim(stripslashes($_POST["pname"]));
	$pvalue = trim($_POST["pvalue"]);
	if ($pname == "" or $pvalue == "") { 
		$nonamevalue = "true"; 
	} else {
		$nonamevalue = "false";
			if ($profilename == $pname) {
				$conn = @mysql_connect($dbhost,$dbuser,$dbpass) or sqlerror();
				mysql_select_db($dbname, $conn);
				mysql_query("UPDATE cmum_profiles SET name='".$pname."', value='".$pvalue."' WHERE id='".$profileid."'") or sqlerror();
				mysql_close($conn);
				header("Location: profiles.php?action=update&profile=".$pname);
			} else {
				$conn = @mysql_connect($dbhost,$dbuser,$dbpass) or sqlerror();
				mysql_select_db($dbname, $conn);	
				$sql = mysql_query("SELECT * FROM cmum_profiles WHERE name='".$pname."'");
				$rowcheck = mysql_num_rows($sql);
				if($rowcheck == 1){
					$profileexists = "true";
					mysql_close($conn);
				} else {
					$profileexists = "false";
					mysql_query("UPDATE cmum_profiles SET name='".$pname."', value='".$pvalue."' WHERE id='".$profileid."'") or sqlerror();
					mysql_close($conn);
					header("Location: profiles.php?action=update&profile=".$pname);
				}			
			}
	}
}

if (isset($_POST["baction"]) and $_POST["baction"] == "Cancel") {
	header("Location: profiles.php");
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=<?php print($charset); ?>" />
<title><?php print($longtitle); ?></title>
<link rel="shortcut icon" href="favicon.ico">
<link rel="apple-touch-icon" href="favicon.png">
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link href="css/main.css" rel="stylesheet" type="text/css">
</head>

<body>
<!--<table width="780" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 1px solid #DDDDDD; border-right: 1px solid #DDDDDD; text-align: left;">
  <tr bgcolor="#FFFFFF">
    <td height="22" colspan="6">&nbsp;</td>
  </tr>
  <tr bgcolor="#0066FF">
    <td width="15" bgcolor="#808080">&nbsp;</td>
    <td width="751" height="120" bgcolor="#808080"><?php //require("includes/header.php"); ?></td>
    <td width="15" bgcolor="#808080">&nbsp;</td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td height="18">&nbsp;</td>
    <td height="18"><center><?php //require("includes/menu.php"); ?></center></td>
    <td height="18">&nbsp;</td>
  </tr>
    <tr bgcolor="#FFFFFF">
    <td height="18">&nbsp;</td>
    <td height="18">&nbsp;</td>
    <td height="18">&nbsp;</td>
  </tr>
</table>-->
<?php require_once 'topo.php';?>
<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;">
      <tr>
    <td width="15" height="20">&nbsp;</td>
    <td height="20" colspan="4">
    <form name="editprofile" action="editprofile.php?id=<?php print($line["id"]); ?>" method="post">
    <fieldset>
    <legend>Edit Profile</legend>
    <?php
    $conn = @mysql_connect($dbhost,$dbuser,$dbpass) or sqlerror();
	mysql_select_db($dbname, $conn);
	$sql = mysql_query("SELECT * FROM cmum_profiles WHERE id='".$profileid."'");
	$line=mysql_fetch_array($sql);
	mysql_close($conn);
	
	if ($profileexists == "true") {
		print("<font color=\"#FF0000\">Perfil ja existe!</font><br><br>");
	}
	if ($nonamevalue == "true") {
		print("<font color=\"#FF0000\">You must enter a profile name and a value!</font><br><br>");
	}
	?>
    <input type="hidden" name="profileid" value="<?php print($line["id"]); ?>">
    <input type="hidden" name="profilename" value="<?php print($line["name"]); ?>">
    Profile name:<br>
    <input type="text" name="pname" class="form-control" value="<?php if ($profileexists == "true" or $nonamevalue == "true") { print($pname); } else { print($line["name"]); } ?>" size="15" tabindex="1"><br><br>
    CSP value:<br>
    <input type="text" name="pvalue" class="form-control" value="<?php if ($profileexists == "true" or $nonamevalue == "true") { print($pvalue); } else { print($line["value"]); } ?>" size="15" tabindex="2"><br><br>
    <input type="submit" name="baction" class="btn btn-default" value="Save" id="submit" tabindex="2" accesskey="s" title="(ALT+S)">
    <input type="submit" name="baction" class="btn btn-default" value="Cancel" id="submit" tabindex="3" accesskey="q" title="(ALT+Q)">
    </fieldset>
    </form>
    </td>
    <td width="15" height="20">&nbsp;</td>
  </tr>
</table>
<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; border-bottom: 0px solid #DDDDDD; text-align: left;">
    <tr>
    <td height="20" width="780">&nbsp;</td>
  </tr>
</table>
<?php
require("includes/footer.php");
?>
</body>
</html>
