first commit

This commit is contained in:
Bildcraft1 2024-10-31 10:47:12 +01:00
commit d975e212f9
35 changed files with 813 additions and 0 deletions

36
login/amici.php Normal file
View file

@ -0,0 +1,36 @@
<?php
include('includes/security.php');
global $users;
if (!isset($_SESSION["loggedIn"])) {
$_SESSION["error"] = true;
header("Location: index.php");
exit();
}
?>
<!DOCTYPE HTML>
<html lang="it">
<head>
<title>Login</title>
<?php require "includes/head.php" ?>
</head>
<body>
<?php require "includes/navbar.php" ?>
<div class="container">
<h1>Lista Amici</h1>
<h2>Ecco i tuoi amici</h2>
<?php foreach ($users as $user => $x) { ?>
<p><?php if (strcmp($user, $_SESSION["nome"]) !== 0) echo $user ?></p>
<?php } ?>
</div>
<?php require "includes/footer.php" ?>
</body>
</html>

42
login/foto.php Normal file
View file

@ -0,0 +1,42 @@
<?php
include('includes/security.php');
global $users;
if (!isset($_SESSION["loggedIn"])) {
$_SESSION["error"] = true;
header("Location: index.php");
exit();
}
$cartella_foto = "./uploads";
$files = array_slice(scandir($cartella_foto), 2);
?>
<!DOCTYPE HTML>
<html lang="it">
<head>
<title>Login</title>
<?php require "includes/head.php" ?>
</head>
<body>
<?php require "includes/navbar.php" ?>
<div class="container">
<h1>Foto dell'amministratore</h1>
<div>
<?php
foreach ($files as $file) {
echo '<a href="' . $cartella_foto . '/' . $file . '" data-fancybox="gallery" data-caption="Caption #1">';
echo '<img src="' . $cartella_foto . '/' . $file . '" alt="' . $file . '" style="width:200px;height:200px;">';
echo '</a>';
}
?>
</div>
</div>
<?php require "includes/footer.php" ?>
</body>
</html>

66
login/home.php Normal file
View file

@ -0,0 +1,66 @@
<?php
include "includes/security.php";
global $users;
if ((!isset($_POST["nome"]) || !isset($_POST["password"]) and !isset($_SESSION["loggedIn"]))) {
$_SESSION["error"] = true;
header("Location: index.php");
exit();
}
if (!isset($_SESSION["loggedIn"]) || $_SESSION["loggedIn"] !== true) {
foreach ($users as $nome => $password) {
if (strcmp($nome, $_POST["nome"]) == 0 and strcmp($password, $_POST["password"]) == 0) {
$_SESSION["nome"] = $nome;
$_SESSION["loggedIn"] = true;
}
}
if (in_array($_POST["nome"], array_keys($users)) == 0) {
$users[$_POST["nome"]] = $_POST["password"];
$json = json_encode($users);
file_put_contents("users.json", $json);
$_SESSION["nome"] = $_POST['nome'];
$_SESSION["loggedIn"] = true;
}
if (!isset($_SESSION["loggedIn"])) {
$_SESSION["error"] = true;
header("Location: index.php");
exit();
}
if (strcmp($_SESSION["nome"], "admin") == 0) {
$_SESSION["admin"] = true;
}
}
if (!isset($_COOKIE["lastSeen"])) {
setcookie("lastSeen", date("d/m/Y H:i:s"), time() + 60, "/");
}
if ($_SESSION["loggedIn"] === true) {
$_COOKIE["lastSeen"] = date("d/m/Y H:i:s");
}
?>
<!DOCTYPE HTML>
<html lang="it">
<head>
<title>Login</title>
<?php require "includes/head.php" ?>
</head>
<body>
<?php require "includes/navbar.php" ?>
<div class="container">
<h1>Home Page</h1>
<h2>Benvenuto <?= $_SESSION["nome"] ?></h2>
<h2>L'ultima volta in cui hai fatto l'accesso: <?= $_COOKIE["lastSeen"] ?></h2>
</div>
<?php require "includes/footer.php" ?>
</body>
</html>

View file

@ -0,0 +1,8 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script>
Fancybox.bind("[data-fancybox]", {
// Your custom options
});
</script>

7
login/includes/head.php Normal file
View file

@ -0,0 +1,7 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0/dist/fancybox/fancybox.umd.js"></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0/dist/fancybox/fancybox.css"
/>

32
login/includes/navbar.php Normal file
View file

@ -0,0 +1,32 @@
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Facebook 2</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="home.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="amici.php">Amici</a>
</li>
<li class="nav-item">
<a class="nav-link" href="foto.php">Foto</a>
</li>
<li class="nav-item d-flex me-2">
<a class="nav-link" href="logout.php">Logout</a>
</li>
<?php
if (isset($_SESSION['admin']) && $_SESSION['admin']) {
echo '<li class="nav-item">
<a class="nav-link" href="upload.php">Admin</a>
</li>';
}
?>
</ul>
</div>
</div>
</nav>

View file

@ -0,0 +1,4 @@
<?php
session_start();
$users = json_decode(file_get_contents("users.json"),true);

39
login/index.php Normal file
View file

@ -0,0 +1,39 @@
<?php require "includes/security.php" ?>
<!DOCTYPE HTML>
<html lang="it">
<head>
<title>Login</title>
<?php require "includes/head.php" ?>
</head>
<body>
<div class="container">
<h1>Login</h1>
<?php
if (isset($_SESSION["error"])) {
?>
<div class="alert alert-danger" role="alert">
Errore! Riprova
</div>
<?php }
session_unset();
?>
<form action="home.php" method="post">
<div class="mb-3">
<label for="nome" class="form-label">Nome Utente</label>
<input type="text" class="form-control" id="nome" name="nome">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
<?php require "includes/footer.php" ?>
</body>
</html>

6
login/logout.php Normal file
View file

@ -0,0 +1,6 @@
<?php
session_start();
session_destroy();
header('Location: index.php');
exit();

46
login/register.php Normal file
View file

@ -0,0 +1,46 @@
<?php
include "includes/security.php";
global $users;
if ((!isset($_POST["nome"]) || !isset($_POST["password"]) and !isset($_SESSION["loggedIn"]))) {
$_SESSION["error"] = true;
header("Location: index.php");
exit();
}
if (!isset($_SESSION["loggedIn"]) || $_SESSION["loggedIn"] !== true) {
foreach ($users as $nome => $password) {
if (strcmp($nome, $_POST["nome"]) == 0 and strcmp($password, $_POST["password"]) == 0) {
$_SESSION["nome"] = $nome;
$_SESSION["loggedIn"] = true;
}
}
if (!isset($_SESSION["loggedIn"])) {
$_SESSION["error"] = true;
header("Location: index.php");
exit();
}
}
?>
<!DOCTYPE HTML>
<html lang="it">
<head>
<title>Login</title>
<?php require "includes/head.php" ?>
</head>
<body>
<?php require "includes/navbar.php" ?>
<div class="container">
<h1>Home Page</h1>
<h2>Benvenuto <?= $_SESSION["nome"] ?></h2>
</div>
<?php require "includes/footer.php" ?>
</body>
</html>

94
login/upload.php Normal file
View file

@ -0,0 +1,94 @@
<?php
include('includes/security.php');
global $users;
if (!isset($_SESSION["loggedIn"]) and !isset($_SESSION["admin"])) {
$_SESSION["error"] = true;
header("Location: index.php");
exit();
}
if (!$_SESSION["admin"]) {
header("Location: index.php");
exit();
} else if ($_SERVER["REQUEST_METHOD"] == "POST") {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if (isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "mp4") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
<!DOCTYPE HTML>
<html lang="it">
<head>
<title>Login</title>
<?php require "includes/head.php" ?>
</head>
<body>
<?php require "includes/navbar.php" ?>
<div class="container">
<h1>Upload di Foto</h1>
<div class="mb-3">
<form method="post" action="upload.php" enctype="multipart/form-data">
<label for="formFile" class="form-label">Default file input example</label>
<input class="form-control" type="file" name="fileToUpload" id="fileToUpload">
<button type="submit" class="btn btn-primary">Carica il File</button>
</form>
</div>
</div>
<?php require "includes/footer.php" ?>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

1
login/users.json Normal file
View file

@ -0,0 +1 @@
{"admin":"admin","mario":"sturniolo"}