<?php
session_start();
include("Connessione.php");
if (isset ($_SESSION['loggato'])){
$nomeutente = $_SESSION['loggato'];
}
if(isset($_GET["blog"])){
$blog = $_GET['blog'];
setcookie ("cookieblog", $blog, time()+3600, "/");
} else {
$blog = $_COOKIE["cookieblog"];
}
$error="";
$query = "SELECT * FROM `blog` WHERE titoloBlog = '$blog'";
$result = mysqli_query($mysqli, $query);
$row= mysqli_fetch_assoc($result);
$autoreblog = $row["nomeUtente"];
if(isset ($_POST['creapost'])){
$uploadDir = __DIR__.'\immagini';
$allowedextension = array("png", "jpg", "jpeg", "gif" );
if (!empty($_FILES)){
foreach ($_FILES as $file) {
if (UPLOAD_ERR_OK === $file['error']) {
$fileName = basename($blog.$file['name']);
// prendo l'estensione del file
$fileextension = pathinfo($fileName, PATHINFO_EXTENSION);
if (! in_array($fileextension, $allowedextension)) {
$error= "il file che hai caricato non ha un' estensione valida!<br>Per favore, seleziona un immagine in formato png, jpg, jpeg o una gif" ;
$fileName= "";
}
else{
move_uploaded_file($file['tmp_name'], $uploadDir.DIRECTORY_SEPARATOR.$fileName);
}
}
else{
if ($file['error']==4){
$fileName= NULL;
}
else{
$error=" errore nel caricamento del file";
}
}
}
}
else{
$fileName= NULL;
}
if (($fileName != "") or (is_null($fileName))){
$titolopost = $_POST['titolopost'];
$testo = $_POST['areapost'];
$img = $_POST['img'];
$query2 = "INSERT INTO post(titoloPost,data,ora,testo,nomeUtente,titoloBlog,img) VALUES ('$titolopost', SYSDATE(), NOW(), '$testo',(SELECT nomeUtente FROM utentiregistrati WHERE nomeUtente = '$nomeutente'),(SELECT titoloBlog FROM blog WHERE titoloBlog = '$blog'),'$img')";
$result2 = mysqli_query($mysqli, $query2);
if(!$result2){
echo 'errore';
}
}
}
if(isset($_COOKIE['cookiesfondo'])) {
$sfondo = $_COOKIE['cookiesfondo'];
switch ($sfondo) {
case '1':
$class = 'uno';
break;
case '2':
$class = 'due';
break;
case '3':
$class = 'tre';
break;
case '4':
$class = 'quattro';
break;
default:
$class = 'default';
break;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title> Blog </title>
<link rel = "stylesheet" href = "blog.css">
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<style type="text/css">
.uno {
background-color: red;
}
.due {
background-color: yellow;
}
.tre {
background-color: white;
}
.quattro {
background-color: black;
}
.default {
background-color: pink;
}
</style>
</head>
<script>
<?php if ($autoreblog == $nomeutente){ ?>
$(document).ready(function(){
$("#post").show();
});
<?php } ?>
</script>
<body class="<?php echo $class; ?>">
<input type="checkbox" id="nav-toggle" hidden />
<nav class="nav">
<label for="nav-toggle" class="nav-toggle" onclick></label>
<ul>
<li class = "" ><a href="home.html"> Home </a> </li>
<li class ="" ><a href="tuttiiblog.php">Tutti i blog</a> </li>
</ul>
</nav>
<p id = "nomeutente"> Autore Blog: <?php echo $autoreblog ?></p>
<h1 style = "color:black"> <?php echo $blog ?> </h1>
<form method = "post" action = "blog.php">
<div id = "post" style = "display:none">
<input type = "text" placeholder="Scegli un titolo" id = "titolopost" name = "titolopost"/> <br />
<label for "immagine">Inserisci un'immagine (GIF, JPEG, JPG, PNG): </label>
<input type="file" value="scegli immagine" name="img" />
<input type = "text" placeholder="Scrivi un post" id = "areapost" name = "areapost"/> <br />
<br />
<input type = "submit" value = "Crea Post" id = "creapost" name = "creapost" />
</div>
</form>
<?php
$query3 = "SELECT * FROM post WHERE titoloBlog = '$blog'";
$result3 = mysqli_query($mysqli, $query3);
while($row3= mysqli_fetch_assoc($result3)){
$postdititolo = $row3["titoloPost"];
$testopost = $row3["testo"];
$imgpost = $row3["img"];
$datapost = $row3["data"];
$orapost = $row3["ora"];
$idPost = $row3["IDpost"];
?>
<div id ='visualizzapost'>
<p id = "dataora"> <?php echo $datapost ?> - <?php echo $orapost?> </p>
<h3 id = "Tpost"> <?php echo $postdititolo ?> </h3>
<?php
if (empty($imgpost)){ ?>
<script>
$(document).ready(function(){
$('#img<?php echo $idPost; ?>').hide();
});
</script>
<?php } else{ ?>
<script>
$(document).ready(function(){
$('#img<?php echo $idPost; ?>').show();
});
</script>
<?php }; ?>
<img id="img<?php echo $idPost; ?>" src="immagini/<?php echo $imgpost ?>" style="display:block; margin:10% auto; text-align:center; width:40%; ">
</img>
<p id = "Testopost"> <?php echo $testopost ?> </p>
<a><?php echo "<a href=\"modificapost.php\">";?> <input type="submit" name = "modificapost" id = "modificapost" value="Modifica Post"></a> 
<!--<a><?php echo "<a href=\"eliminapost.php?IDpost=$idPost\">";?> <input type="submit" name = "eliminapost" id = "eliminapost" value="Elimina Post"></a> -->
<?php echo "<a href=\"eliminapost.php?IDpost=$idPost\">Elimina</a>";?>
</div>
<?php } ?>
<p><a href="personalizzablog.php">Personalizza Blog</a> </p>
</body>
</html>
<?php
mysqli_close($mysqli);
?>