<?php require_once('Connections/receitas.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "login/login_admin.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
  $MM_dupKeyRedirect="repetido.php";
  $loginUsername = $_POST['titulo'];
  $LoginRS__query = sprintf("SELECT titulo FROM receitas WHERE titulo=%s", GetSQLValueString($loginUsername, "text"));
  mysql_select_db($database_receitas, $receitas);
  $LoginRS=mysql_query($LoginRS__query, $receitas) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);

  //if there is a row in the database, the username was found - can not add the requested username
  if($loginFoundUser){
    $MM_qsChar = "?";
    //append the username to the redirect page
    if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
    $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
    header ("Location: $MM_dupKeyRedirect");
    exit;
  }
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO receitas (categoria, titulo, autor, ingredientes, preparo) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['categoria'], "text"),
                       GetSQLValueString($_POST['titulo'], "text"),
                       GetSQLValueString($_POST['sub'], "text"),
                       GetSQLValueString($_POST['ingredientes'], "text"),
                       GetSQLValueString($_POST['preparo'], "text"));

  mysql_select_db($database_receitas, $receitas);
  $Result1 = mysql_query($insertSQL, $receitas) or die(mysql_error());

  $insertGoTo = "lista_receitas.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_receitas, $receitas);
$query_receitas = "SELECT * FROM receitas ORDER BY titulo ASC";
$receitas = mysql_query($query_receitas, $receitas) or die(mysql_error());
$row_receitas = mysql_fetch_assoc($receitas);
$totalRows_receitas = mysql_num_rows($receitas);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>INSERIR RECEITAS</title>
<link href="css/estilosreceitas.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center">&nbsp;
      <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
        <table align="center" cellspacing="10">
          <tr valign="baseline">
            <td align="right" nowrap="nowrap" class="stilo4"><strong>Categoria:</strong></td>
            <td align="left" valign="top" class="stilo2"><input name="categoria" type="text" id="categoria" placeholder="Categorias"size="80" /></td>
          </tr>
          <tr valign="baseline">
            <td align="right" nowrap="nowrap" class="stilo4"><strong>Titulo:</strong></td>
            <td align="left" valign="top" class="stilo2"><input type="text" name="titulo" placeholder="Titulo" value="" size="80" /></td>
          </tr>
          <tr valign="baseline">
            <td align="right" valign="top" nowrap="nowrap" class="stilo4"><strong>Sub-título:</strong></td>
            <td align="left" valign="top" class="stilo2"><input name="sub" type="text" id="sub" placeholder="Sub-Titulo"  size="80" /></td>
          </tr>
          <tr valign="baseline">
            <td align="right" valign="top" nowrap="nowrap" class="stilo4"><strong>Ingredientes</strong>:</td>
            <td align="left" valign="top" class="stilo2"><textarea name="ingredientes" placeholder="Ingredientes" cols="80" rows="10"></textarea></td>
          </tr>
          <tr valign="baseline">
            <td align="right" valign="top" nowrap="nowrap" class="stilo4"><strong>Preparo:</strong></td>
            <td align="left" valign="top" class="stilo2"><textarea name="preparo" placeholder="Preparo" cols="80" rows="20"></textarea></td>
          </tr>
          <tr valign="baseline" class="stilo2">
            <td colspan="2" align="center" nowrap="nowrap"><input type="submit" value="INSERIR" /></td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="form1" />
      </form></td>
  </tr>
  <tr>
    <td align="center" class="stilo1"><a href="inicio.php" target="_parent">VOLTAR</a></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($receitas);
?>
