School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,94 @@
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: ../Users/login.php');
}
if (isset($_POST['returnButton'])) {
header('Location: ../Users/admin.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title>Add a news</title>
</head>
<body>
<div class="container" id="addFormDiv">
<div class="row">
<div class="col-sm">
<div class="container text-left">
<form id="addForm">
<h3>Add a news:</h3>
<div class="mb-1"><label for="titleField" class="form-label">Title: </label><input type="text"
id="titleField" class="form-control" required pattern="[0-9A-Za-z-.\:;]{5,30}"></div>
<div class="mb-1"><label for="contentField" class="form-label">Content: </label><input
type="text" id="contentField" class="form-control" required
pattern="[0-9A-Za-z-.\:;]{10,256}"></div>
<div class="mb-1"><label for="dateField" class="form-label">Date: </label><input type="date"
id="dateField" class="form-control" required></div>
<div class="mb-1"><label for="producerField" class="form-label">Producer: </label><input
type="text" id="producerField" class="form-control" required
pattern="[0-9A-Za-z-]{5,30}"></div>
<div class="mb-1"><label for="categoryField" class="form-label">Category: </label><input
type="text" id="categoryField" class="form-control" required
pattern="[0-9A-Za-z-]{5,30}"></div>
<button id="insertNewsButton" type="submit" class="btn btn-success mb-1">Add
News</button>
</form>
<form method="post">
<input type="submit" class="btn btn-secondary mb-1" name="returnButton"
value="Return to profile">
</form>
</div>
</div>
</div>
</div>
<script>
const insertNews = (e) => {
e.preventDefault();
const title = $('#titleField').val();
const content = $('#contentField').val();
const date = $('#dateField').val();
const producer = $('#producerField').val();
const category = $('#categoryField').val();
$.ajax({
url: 'http://localhost/web/Lab 7/Repositories/Repository.php',
type: 'POST',
data: {
title: title,
news_text: content,
date: date,
producer: producer,
category: category,
action: 'insertNews'
},
success: function (response) {
alert('Log added successfully!');
},
error: function (response) {
alert('Error adding log!');
}
});
}
$('#addForm').submit(insertNews);
</script>
</body>
</html>
@@ -0,0 +1,106 @@
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: ../Users/login.php');
}
if (isset($_POST['returnButton'])) {
header('Location: ../Users/admin.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title>Edit a news report</title>
</head>
<body>
<div class="container" id="addFormDiv">
<div class="row">
<div class="col-sm">
<div class="container text-left">
<form id="editForm">
<h3>Edit a news:</h3>
<?php
require_once '../../Repositories/Repository.php';
$connection = new DBConnection();
if (!isset($_GET['id'])) {
header('Location: ../Users/admin.php');
}
$id = $_GET['id'];
$result = $connection->getNewsById($id);
if (!isset($result) || empty($result)) {
header('Location: ../Users/admin.php');
}
echo '<input type="hidden" id="idField" value="' . $result['id'] . '">';
echo '<div class="mb-1"><label for="titleField" class="form-label">Title: </label><input type="text" id="titleField" class="form-control" required pattern="[0-9A-Za-z-.\:;]{5,30}" value="' . $result['title'] . '"></div>';
echo '<div class="mb-1"><label for="contentField" class="form-label">Content: </label><input
type="text" id="contentField" class="form-control" required
pattern="[0-9A-Za-z-.\:;]{10,256}" value="' . $result['news_text'] . '"></div>';
echo '<div class="mb-1"><label for="dateField" class="form-label">Date: </label><input type="date"
id="dateField" class="form-control" required value=' . $result["date"] . '></div>';
echo '<div class="mb-1"><label for="producerField" class="form-label">Producer: </label><input
type="text" id="producerField" class="form-control" required
pattern="[0-9A-Za-z-]{5,30}" value="' . $result['producer'] . '"></div>';
echo '<div class="mb-1"><label for="categoryField" class="form-label">Category: </label><input
type="text" id="categoryField" class="form-control" required
pattern="[0-9A-Za-z-]{5,30}" value="' . $result['category'] . '"></div>';
?>
<button id="updateNewsButton" type="submit" class="btn btn-success mb-1">Update
News</button>
</form>
<form method="post">
<input type="submit" class="btn btn-secondary mb-1" name="returnButton"
value="Return to profile">
</form>
</div>
</div>
</div>
</div>
<script>
const updateNews = (e) => {
e.preventDefault();
const id = $('#idField').val();
const title = $('#titleField').val();
const content = $('#contentField').val();
const date = $('#dateField').val();
const producer = $('#producerField').val();
const category = $('#categoryField').val();
$.ajax({
url: 'http://localhost/web/Lab 7/Repositories/Repository.php',
type: 'POST',
data: {
id: id,
title: title,
news_text: content,
date: date,
producer: producer,
category: category,
action: 'updateNews'
},
success: function (response) {
alert('Log added successfully!');
},
error: function (response) {
alert('Error adding log!');
}
});
}
$('#editForm').submit(updateNews);
</script>
</body>
</html>
@@ -0,0 +1,99 @@
<?php
if (isset($_POST['backButton'])) {
header('Location: ../../main.html');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<title>News</title>
</head>
<body>
<div class="container text-center">
<h3>Welcome</h3>
<div class='dropdown'>
<button class='btn btn-secondary dropdown-toggle' type='button' id='dropdownMenuButton'
data-bs-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>
Filter
</button>
<div class='dropdown-menu' aria-labelledby='dropdownMenuButton'>
<input class='dropdown-item' type="date" id="dateField" class="form-control" required>
<input class='dropdown-item' placeholder="Category" type="text" id="categoryField" class="form-control"
required>
<input class='dropdown-item' type="button" class="btn btn-primary" id="filterButton" value="Filter">
</div>
</div>
<table class="container" id="newsTable"
style="margin: 30px; border: 1px solid;border-color: black; border-collapse: collapse;">
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Category</th>
<th>Date</th>
<th>Producer</th>
</tr>
</thead>
<tbody id='tableBody'>
<?php
require_once '../../Repositories/Repository.php';
$connection = new DBConnection();
$result = $connection->getAllNews();
foreach ($result as $key => $value) {
echo "<tr>";
echo "<td>" . $value['title'] . "</td>";
echo "<td>" . $value['news_text'] . "</td>";
echo "<td>" . $value['category'] . "</td>";
echo "<td>" . $value['date'] . "</td>";
echo "<td>" . $value['producer'] . "</td>";
echo "</tr>";
}
?>
</tbody>
<form method="post">
<input type="submit" class="btn btn-dark" name="backButton" value="Back" style=" margin: 30px">
</form>
</div>
<script>
$(document).ready(function () {
$('#filterButton').click(function () {
var date = $('#dateField').val();
var category = $('#categoryField').val();
$.ajax({
url: '../../Repositories/Repository.php',
type: 'GET',
data: {
date: date,
category: category,
action: 'getAllNewsByCategoryAndDate'
},
success: function (response) {
response = JSON.parse(response);
$('#tableBody').empty();
for (var i = 0; i < response.length; i++) {
var newRow = "<tr><td>" + response[i].title + "</td><td>" + response[i].news_text +
"</td><td>" + response[i].category + "</td><td>" + response[i].date +
"</td><td>" + response[i].producer + "</td></tr>";
$('#tableBody').append(newRow);
}
}
});
});
});
</script>
</body>
</html>
@@ -0,0 +1,78 @@
<?php
session_start();
if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
} else {
header('Location: login.php');
die();
}
if (isset($_POST['logoutButton'])) {
session_unset();
session_destroy();
header('Location: login.php');
}
if (isset($_POST['addNewsButton'])) {
header('Location: ../News/addNews.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title>Admin page</title>
</head>
<body>
<div class="container text-center">
<h3>Welcome, <?php echo $username; ?>!</h3>
<table class="container" id="newsTable"
style="margin: 30px; border: 1px solid;border-color: black; border-collapse: collapse;">
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Category</th>
<th>Date</th>
<th>Producer</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
require_once '../../Repositories/Repository.php';
$connection = new DBConnection();
$result = $connection->getAllNews();
foreach ($result as $key => $value) {
echo "<tr>";
echo "<td>" . $value['title'] . "</td>";
echo "<td>" . $value['news_text'] . "</td>";
echo "<td>" . $value['category'] . "</td>";
echo "<td>" . $value['date'] . "</td>";
echo "<td>" . $value['producer'] . "</td>";
echo "<td><a href='../News/editNews.php?id=" . $value['id'] . "'>Edit</a></td>";
echo "</tr>";
}
?>
</tbody>
<form method="post">
<input type="submit" class="btn btn-primary" name="addNewsButton" value="Add news" style="margin: 30px">
<input type="submit" class="btn btn-dark" name="logoutButton" value="Log out" style="margin: 30px">
</form>
</div>
</body>
</html>
@@ -0,0 +1,92 @@
<?php
header('Cache-Control: no-cache, must-revalidate');
require_once '../../Repositories/Repository.php';
session_start();
if (isset($_SESSION['username'])) {
unset($_SESSION['username']);
}
function checkValidUser(string $username, string $password): bool
{
$connection = new DBConnection();
$result = $connection->validateUser($username, $password);
return !(count($result) == 0);
}
if (isset($_POST['loginButton'])) {
$errors = 0;
$fields = array("username", "password");
foreach ($fields as $key => $field) {
if (!isset($_POST[$field]) || empty($_POST[$field])) {
$errors++;
break;
}
}
if ($errors <= 0) {
$username = $_POST['username'];
$password = $_POST['password'];
if (checkValidUser($username, $password)) {
$_SESSION['username'] = $username;
header('Location: admin.php');
} else {
$_SESSION['login-error'] = "Invalid username and/or password! Try again!";
}
} else {
$_SESSION['login-error'] = "Invalid username and/or password! Try again!";
}
}
if (isset($_POST['indexPage'])) {
header('Location: ../../main.html');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<title>Login</title>
</head>
<body>
<div class="container">
<h2>Login</h2>
<form id="login-form" method="post" action="login.php">
<div class="mb-3 mt-3">
<label for="username" class="form-label">Username:</label>
<input type="text" class="form-control" id="username" placeholder="Enter username" name="username">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password:</label>
<input type="password" class="form-control" id="password" placeholder="Enter password" name="password">
</div>
<input type="submit" class="btn btn-primary" name="loginButton" value="Login">
<input type="submit" class="btn btn-secondary" name="indexPage" value="Cancel">
<?php
if (isset($_SESSION['login-error'])) {
$error = $_SESSION['login-error'];
echo '<script type="text/javascript">';
echo 'window.alert("Invalid username and/or password")';
echo '</script>';
}
?>
</form>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</html>
<?php
unset($_SESSION['login-error']);
?>
@@ -0,0 +1,204 @@
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
class DBConnection
{
private string $host = '127.0.0.1';
private string $database = 'NewsDb';
private string $user = 'root';
private string $password = '';
private string $charset = 'UTF8';
# used php data objects (PDO) to connect to database
private PDO $pdo;
private string $error;
public function __construct()
{
$dsn = "mysql:host=$this->host;dbname=$this->database;charset=$this->charset";
# array of options that configures the PDO object
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, # if a database error occurs, PDO will throw a PDOException instead of just returning false or null
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, # when you fetch rows from a query result set, PDO will return an associative array where the keys are the column names and the values are the column values
PDO::ATTR_EMULATE_PREPARES => false
); # emulation is disabled, PDO uses real prepared statements instead (no substituting parameters into the SQL statement.)
try {
$this->pdo = new PDO($dsn, $this->user, $this->password, $opt);
} catch (PDOException $e) {
$this->error = $e->getMessage();
echo "Error connecting to database: " . $this->error;
}
}
public function getAllNews()
{
// $statement = $this->pdo->query("SELECT * FROM logs");
$statement = $this->pdo->prepare("SELECT * FROM News");
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
public function getAllNewsByDate($date)
{
if (!empty($date)) {
$statement = $this->pdo->prepare("SELECT * FROM News WHERE date = ?");
$statement->execute([$date]);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
http_response_code(400);
}
public function getAllNewsByCategory($category)
{
if (!empty($category)) {
$statement = $this->pdo->prepare("SELECT * FROM News WHERE category = ?");
$statement->execute([$category]);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
http_response_code(400);
}
public function getAllNewsByCategoryAndDate($category, $date)
{
if (!empty($category) && !empty($date)) {
$statement = $this->pdo->prepare("SELECT * FROM News WHERE category = ? AND date = ?");
$statement->execute([$category, $date]);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
if (!empty($category) && empty($date)) {
return $this->getAllNewsByCategory($category);
}
if (empty($category) && !empty($date)) {
return $this->getAllNewsByDate($date);
}
http_response_code(400);
}
public function validateUser($username, $password)
{
if (!empty($username) && !empty($password)) {
$statement = $this->pdo->prepare("SELECT 1 FROM Users WHERE username = ? AND password = ?");
$statement->execute([$username, $password]);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
http_response_code(400);
}
public function insertNews($title, $news_text, $category, $date, $producer)
{
if (!empty($title) && !empty($news_text) && !empty($category) && !empty($date) && !empty($producer)) {
$statement = $this->pdo->prepare("INSERT INTO News (title, news_text, category, date, producer) VALUES (?, ?, ?, ?, ?)");
$statement->execute([$title, $news_text, $category, $date, $producer]);
return $statement->rowCount();
}
http_response_code(400);
}
public function updateNews($id, $title, $news_text, $category, $date, $producer)
{
if (!empty($id) && !empty($title) && !empty($news_text) && !empty($category) && !empty($date)) {
$statement = $this->pdo->prepare("UPDATE News SET title = ?, news_text = ?, category = ?, date = ?, producer = ? WHERE id = ?");
$statement->execute([$title, $news_text, $category, $date, $producer, $id]);
return $statement->rowCount();
}
http_response_code(400);
}
public function getNewsById($id)
{
if (!empty($id)) {
$statement = $this->pdo->prepare("SELECT * FROM News WHERE id = ?");
$statement->execute([$id]);
return $statement->fetch(PDO::FETCH_ASSOC);
}
http_response_code(400);
}
public function show($value)
{
echo json_encode($value);
}
public function handleGet()
{
if (isset($_GET['action']) && !empty($_GET['action'])) {
switch ($_GET['action']) {
case 'getAllNews':
session_start();
$result = $this->getAllNews();
$this->show($result);
break;
case 'getAllNewsByCategory':
session_start();
$category = $_GET['category'];
$result = $this->getAllNewsByCategory($category);
$this->show($result);
break;
case 'getAllNewsByCategoryAndDate':
session_start();
$category = $_GET['category'];
$date = $_GET['date'];
$result = $this->getAllNewsByCategoryAndDate($category, $date);
$this->show($result);
break;
case 'getAllNewsByDate':
session_start();
$date = $_GET['date'];
$result = $this->getAllNewsByDate($date);
$this->show($result);
break;
case 'validateUser':
session_start();
$username = $_GET['username'];
$password = $_GET['password'];
$result = $this->validateUser($username, $password);
$this->show($result);
break;
case 'getNewsById':
session_start();
$id = $_GET['id'];
$result = $this->getNewsById($id);
$this->show($result);
break;
}
}
}
public function handlePost()
{
if (isset($_POST['action']) && !empty($_POST['action'])) {
switch ($_POST['action']) {
case 'insertNews':
session_start();
$title = $_POST['title'];
$news_text = $_POST['news_text'];
$category = $_POST['category'];
$producer = $_POST['producer'];
$date = $_POST['date'];
$result = $this->insertNews($title, $news_text, $category, $date, $producer);
$this->show($result);
break;
case 'updateNews':
session_start();
$id = $_POST['id'];
$title = $_POST['title'];
$news_text = $_POST['news_text'];
$category = $_POST['category'];
$date = $_POST['date'];
$producer = $_POST['producer'];
$result = $this->updateNews($id, $title, $news_text, $category, $date, $producer);
$this->show($result);
break;
}
}
}
}
$conn = new DBConnection();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$conn->handlePost();
} else {
$conn->handleGet();
}
?>
+34
View File
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div class="container text-center" id="index-page">
<h2>News</h2>
<button
type="button"
class="btn btn-primary btn-block"
onclick="location.href='Controllers/Users/login.php'"
>
Login
</button>
<button
type="button"
class="btn btn-primary btn-block"
onclick="location.href='Controllers/News/viewNews.php'"
>
View
</button>
</div>
</body>
</html>