-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
110 lines (90 loc) · 3.96 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
require_once('./conf/database.php');
require_once('./conf/session.php');
// Back-end de classement des posts
$request = $database->prepare("SELECT * FROM post ORDER BY post_date DESC");
$request->execute();
$posts = $request->fetchAll(PDO::FETCH_ASSOC);
// Back-end de recherche d'un post
if ($_SERVER['REQUEST_METHOD'] == "GET") {
if (!empty($_GET['recherche'])) {
$data = [
"recherche" => "%" . $_GET['recherche'] . "%",
];
$request_search = $database->prepare("SELECT * FROM post WHERE post_content LIKE :recherche");
$request_search->execute($data);
$posts = $request_search->fetchAll(PDO::FETCH_ASSOC);
}
}
// Back-end de création d'un post
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($_USER != null) {
if (isset($_POST['form']) && $_POST['form'] === "formulaire_ajout_post") {
if (!empty($_POST['post_content'] && !empty($_POST['post_tag']))) {
$post_content = $_POST['post_content'];
$post_tag = $_POST['post_tag'];
$post_pic = $_FILES['post_pic'];
if (filesize($post_pic["tmp_name"]) != 0):
// Vérification de la taille du fichier
$maxSize = 2097152; // 2 Mo
if (filesize($post_pic["tmp_name"]) > $maxSize) {
$fileSizeError = "Fichier dépassant la limite autorisée (2mo).";
// echo "Fichier dépassant la limite autorisée (2mo).";
return;
}
$uniqueName = uniqid() . '_' . $post_pic['name'];
// Déplacement de l'image dans un dossier
move_uploaded_file(
// Localisation temporaire de l'image
$post_pic["tmp_name"],
// Nouvelle localisation de l'image
__DIR__ . "/imagespost/" . $uniqueName
);
else:
// Donne une valeur nulle à la donnée post_pic si aucune image n'est postée
$uniqueName = null;
endif;
$data = [
"user_id" => $_SESSION['user_id'],
"post_content" => $post_content,
"post_tag" => $post_tag
];
$request_insert = $database->prepare("INSERT INTO post (post_author_id, post_content, post_tag, post_pic, post_date) VALUES (:user_id, :post_content, :post_tag, '$uniqueName', NOW())");
$post_inserted = $request_insert->execute($data);
header("Location: ./index.template.php");
die();
}
} else {
echo "Veuillez compléter tous les champs...";
}
} else
$notconnected = true;
}
// Back-end de suppression d'un post
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['form']) && $_POST['form'] === "formulaire_suppression_post") {
if (!empty($_POST['post_id'])) {
$data = [
"post_id" => $_POST['post_id']
];
$request_delete = $database->prepare("DELETE FROM post WHERE post_id = :post_id");
$post_deleted = $request_delete->execute($data);
header('Location: ./index.template.php');
}
}
}
// // Back-end de classement des users
// $request = $database->prepare("SELECT * FROM user ORDER BY user_nickname DESC");
// $request->execute();
// $users = $request->fetchAll(PDO::FETCH_ASSOC);
// // Back-end de recherche d'un user
// if ($_SERVER['REQUEST_METHOD'] == "GET") {
// if (!empty($_GET['recherche'])) {
// $data = [
// "recherche" => "%" . $_GET['recherche'] . "%",
// ];
// $request_search = $database->prepare("SELECT * FROM user WHERE user_nickname LIKE :recherche");
// $request_search->execute($data);
// $users = $request_search->fetchAll(PDO::FETCH_ASSOC);
// }
// }