<?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 = "";
$status = "";
$nonamevalue = "";
$rowcolor = "#FFFFFF";

if (isset($_GET["action"]) and $_GET["action"] == "update") {
    $status = "Profile " . $_GET["profile"] . " atualizada com sucesso!";
}

if (isset($_POST["baction"]) and $_POST["baction"] == "Salvar Perfil") {
    //var_dump($_POST);
    //exit();


    $pname = trim($_POST["pname"]);
    $pvalue = trim($_POST["pvalue"]);
    $url_ip = trim($_POST['url_ip']);
    $porta = trim($_POST['porta']);
    $obs = trim($_POST['obs']);



    if ($pname == "" or $pvalue == "") {
        $nonamevalue = "true";
    } else {
        $nonamevalue = "false";
        $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 (isset($_POST['id']) && $_POST['id'] > 0) {
            $SQL = "UPDATE cmum_profiles SET name='{$pname}',value='{$pvalue}',url_ip='{$url_ip}',porta='{$porta}',obs='{$obs}' WHERE id = '{$_POST['id']}'";
            $rs = mysql_query($SQL);
            if ($rs === true) {
                $status = "Perfil " . $pname . " atualizado com sucesso.";
            }
        } elseif ($rowcheck == 1) {
            $profileexists = "true";
        } else {
            $profileexists = "false";
            mysql_query("INSERT INTO cmum_profiles (name,value,url_ip,porta,obs) VALUES ('{$pname}','{$pvalue}','{$url_ip}','{$porta}','{$obs}')") or sqlerror();
            mysql_close($conn);
            $status = "Perfil " . $pname . " cadastrado com sucesso";
        }
    }
}
if (isset($_GET['ac']) && $_GET['ac'] == 'edt' && is_numeric($_GET['pid'])) {
    $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
    mysql_select_db($dbname, $conn);
    $SQL = "SELECT * FROM cmum_profiles WHERE id = '{$_GET['pid']}'";
    $rs = mysql_query($SQL);
    $linha_edt = mysql_fetch_assoc($rs);
    //var_dump($linha_edt);
    //exit();
}
if (isset($_GET['ac']) && $_GET['ac'] == 'del' && is_numeric($_GET['pid'])) {
    //var_dump($_GET);
    //exit();
    $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
    mysql_select_db($dbname, $conn);
    mysql_query("DELETE FROM cmum_profiles WHERE id='{$_GET['pid']}'") or sqlerror();
    $status = "Dados removidos com sucesso.";
}
if (isset($_GET['ac']) && $_GET['ac'] == 'bloq' && is_numeric($_GET['pid'])) {
    $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
    mysql_select_db($dbname, $conn);
    mysql_query("UPDATE cmum_profiles SET bloqueado='1' WHERE id='{$_GET['pid']}'") or sqlerror();
    $status = "Perfil Bloqueado com sucesso";
}
if (isset($_GET['ac']) && $_GET['ac'] == 'desbloq' && is_numeric($_GET['pid'])) {
    $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
    mysql_select_db($dbname, $conn);
    mysql_query("UPDATE cmum_profiles SET bloqueado='0' WHERE id='{$_GET['pid']}'") or sqlerror();
    $status = "Perfil Desbloqueado com sucesso";
}


/* if (isset($_POST["baction"]) and $_POST["baction"] == "Yes") {
  $profileid = $_POST["profileid"];
  $profilename = $_POST["profilename"];
  $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
  mysql_select_db($dbname, $conn);
  $sql = mysql_query("SELECT * FROM cmum_udb") or sqlerror();
  while ($line = mysql_fetch_array($sql)) {
  if ($profileid == $line["profiles"]) {
  mysql_query("UPDATE cmum_udb SET profiles='' WHERE id='" . $line["id"] . "'") or sqlerror();
  }
  }
  mysql_query("DELETE FROM cmum_profiles WHERE id='" . $profileid . "'") or sqlerror();
  mysql_close($conn);
  $status = "Profile " . $profilename . " removido com sucesso!";
  } */
?>
<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"/>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
            $(document).on('input', '.numeros', function () {
                var v = $(this).val().replace(/[^0-9\-]/, "");
                $(this).val(v);
            });
        </script>
    </head>

    <body <?php
    if ($profileexists == "true") {
        print(" \"onLoad=\"focus();addprofile.pname.focus()\"");
    }
    ?>>

        <?php require_once 'topo.php'; ?>
        <table width="500px" height="18" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" >
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>               
                <td width="500px" height="18">
                    <form name="addprofile" action="profiles.php" method="post">
                        <input type="hidden" name="id" value="<?php echo isset($linha_edt['id']) ? $linha_edt['id'] : "0" ?>"/>
                        <fieldset> 
                            <legend>Adicionar perfil</legend>
                            <table width="100%" valign="top">
                                <tr>
                                    <td> Nome do perfil:</td>
                                    <td>
                                        <div class="col-sm-8">
                                            <input type="text" name="pname" size="15" tabindex="1" class="form-control" value="<?php echo isset($linha_edt['name']) ? $linha_edt['name'] : "" ?>">
                                        </div>
                                    </td>

                                    <td>
                                </tr>
                                <tr>
                                    <td>
                                        Valor no csp:
                                    </td>
                                    <td>
                                        <div class="col-sm-8">
                                            <input type="text" name="pvalue" size="15" tabindex="2" class="form-control" value="<?php echo isset($linha_edt['value']) ? $linha_edt['value'] : "" ?>">
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Url/Ip:</td>
                                    <td>
                                        <div class="col-sm-10">
                                            <input type="text" name="url_ip" size="15" tabindex="2" class="form-control" value="<?php echo isset($linha_edt['url_ip']) ? $linha_edt['url_ip'] : "" ?>">
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Porta:</td>
                                    <td>
                                        <div class="col-sm-4">
                                            <input type="text" name="porta" size="15" tabindex="2" class="form-control numeros" value="<?php echo isset($linha_edt['porta']) ? $linha_edt['porta'] : "" ?>"/>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Observações:</td>
                                    <td>
                                        <div class="col-sm-10">
                                            <textarea name="obs" class="form-control"><?php echo isset($linha_edt['obs']) ? $linha_edt['obs'] : "" ?></textarea>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td>

                                        <input type="submit" name="baction" value="Salvar Perfil" id="submit" tabindex="3" accesskey="s" title="(ALT+S)" class="btn btn-default">
                                    </td>
                                </tr>
                            </table>
                            <?php
                            if ($profileexists == "true") {
                                print("<font color=\"#FF0000\">Perfil já foi cadastrado.</font>");
                            }
                            ?>
                        </fieldset> 
                    </form>
                    <?php
                    if ($status <> "") {
                        print("<font color=\"#347C2C\">" . $status . "</font><br><br>");
                    }
                    if ($nonamevalue == "true") {
                        print("<font color=\"#FF0000\">Preencha o nome do perfil e/ou valor do csp.</font><br><br>");
                    }
                    ?>

            </tr>
        </table>
        <hr/>.
        <div style="width: 980px; margin: auto">
            <table width="980" height="18" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" class="table table-striped table-bordered table-condensed table-hover" style="font-size: 8pt">
                <?php
                $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror();
                mysql_select_db($dbname, $conn);
                $sql = mysql_query("SELECT * FROM cmum_profiles") or sqlerror();
                $profexists = mysql_num_rows($sql);
                $sql = "";
                ?>
                <tr>
                    <th >&nbsp;</th>
                    <th >Nome do perfil</th>
                    <th >Valor no CSP</th>
                    <th >Url/Ip</th>
                    <th >Porta</th>
                    <th >Observações</th>
                    <th >&nbsp;</th>
                </tr>
                <?php
                $sql = mysql_query("SELECT * FROM cmum_profiles ORDER BY name ASC") or sqlerror();
                while ($line = mysql_fetch_array($sql)) {
                    $status = "";
                    if ($line['bloqueado'] == "0") {
                        $status = '<a href="profiles.php?ac=bloq&pid=' . $line['id'] . '"><img src="images/desbloq.png" width="24px" title="Bloquear"/></a>';
                    } else {
                        $status = '<a href="profiles.php?ac=desbloq&pid=' . $line['id'] . '"><img src="images/bloq.png" width="24px" title="Desbloquear"/></a>';
                    }



                    $gerenciar = "";
                    $editar = '<a href="profiles.php?ac=edt&pid=' . $line['id'] . '"><img src="images/edit.png"/></a>';
                    $deletar = '<a href="profiles.php?ac=del&pid=' . $line['id'] . '" onclick="return window.confirm(\'Deseja Mesmo Deletar?\')"><img src="images/delete.png"/></a>';
                    $gerenciar .= $editar . " " . $deletar;
                    ?>
                    <tr>
                        <td><?php echo $status; ?></td>
                        <td><?php echo $line['name'] ?></td>
                        <td><?php echo $line['value'] ?></td>
                        <td><?php echo $line['url_ip'] ?></td>
                        <td><?php echo $line['porta'] ?></td>
                        <td><?php echo $line['obs'] ?></td>
                        <td><?php echo $gerenciar; ?></td>
                    </tr>                
                <?php } ?>
            </table>
            <table width="980" height="18" 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="18" width="980">&nbsp;</td>
                </tr>
            </table>
        </div>
        <?php
        require("includes/footer.php");
        ?>
    </body>
</html>
