-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.php
215 lines (198 loc) · 6.67 KB
/
create.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
session_start();
require 'autoload.php';
use App\Entity\User;
use App\Repository\UserRepository;
if (!isset($_SESSION['user_id'])) {
$connected = false;
} else {
if (!isset($_SESSION['admin']) || $_SESSION['admin'] !== true) {
header('Location: index.php');
exit;
}
$connected = true;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$mail = $_POST['mail'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$errors = [];
function contientUniquementDesChiffres($chaine) {
return ctype_digit($chaine);
}
if (empty($name)) {
$errors[] = "Le nom d'utilisateur est obligatoire.";
} elseif (contientUniquementDesChiffres($name)) {
$errors[] = "Le nom d'utilisateur ne doit pas contenir uniquement des chiffres.";
}
// Validation de l'email
if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
$errors[] = "L'email n'est pas valide.";
}
//Verification si l'email est déjà utilisé
$userRepo = new UserRepository();
if ($userRepo->findByEmail($mail) !== null) {
$errors[] = "L'email est déjà utilisé.";
}
if (strlen($password) < 8) {
$errors[] = "Le mot de passe doit contenir au moins 8 caractères.";
}
if ($password !== $confirm_password) {
$errors[] = "Les mots de passe ne correspondent pas.";
}
// Gestion de l'upload de la photo
if (isset($_FILES['photo']) && $_FILES['photo']['error'] === 0) {
$photo = $_FILES['photo']['name'];
$tailleFichier = $_FILES['photo']['size'];
$typeFichier = $_FILES['photo']['type'];
$target_dir = "uploads/";
if ($tailleFichier > 25 * 1024 * 1024) {
$errors[] = "Le fichier est trop grand. Maximum 25 Mo.";
exit;
}
// Autoriser uniquement certains types de fichiers (ex. images)
$typesAutorises = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($typeFichier, $typesAutorises)) {
$errors[] = "Seuls les fichiers JPG, PNG et GIF sont autorisés.";
exit;
}
// Créer un nom unique pour éviter les conflits de fichiers
$nouveauNomFichier = uniqid() . '-' . $photo;
$target_file = $target_dir . $nouveauNomFichier;
// Déplacer le fichier téléchargé du dossier temporaire vers sa destination
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target_file)) {
if (empty($errors)) {
// Hachage du mot de passe
$password_hash = password_hash($password, PASSWORD_BCRYPT);
$user = new User($name, $mail, $password_hash, $target_file);
$userRepo = new UserRepository();
try {
$userRepo->create($user);
header('Location: index.php');
exit;
} catch(Exception $e) {
echo "Erreur: " . $e->getMessage();
}
} else {
echo "<h2>Des erreurs ont été trouvées :</h2>";
echo "<ul>";
foreach ($errors as $error) {
echo "<li>" . htmlspecialchars($error) . "</li>";
}
echo "</ul>";
}
} else {
echo "Erreur lors du téléchargement du fichier.";
}
} else {
echo "Erreur sur le fichier.";
}
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php
if($connected) {
echo "<title>Créer un Utilisateur</title>";
} else {
echo "<title>Créer un compte</title>";
}
?>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100vh;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input[type="text"],
input[type="email"],
input[type="password"],
input[type="file"] {
width: 90%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
width: 100%;
padding: 10px;
background-color: #28a745;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
a {
display: block;
text-align: center;
margin-top: 20px;
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<?php
if($connected) {
echo "<h1>Créer un Utilisateur</h1>";
} else {
echo "<h1>Créer un compte</h1>";
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<label for="name">Nom:</label>
<input type="text" id="name" name="name" required>
<br>
<label for="mail">E-mail:</label>
<input type="email" id="mail" name="mail" required>
<br>
<label>Mot de passe:</label>
<input type="password" name="password" required>
<br>
<label>Confirmez votre mot de passe:</label>
<input type="password" name="confirm_password" required>
<br>
<label>Photo de profil:</label>
<input type="file" name="photo" required>
<br>
<button type="submit" name="inscription">Ajouter l'utilisateur</button>
</form>
<br/><br/>
<?php
if($connected) {
echo '<a href="index.php">Retour à la liste des utilisateurs</a>';
} else {
echo '<a href="login.php">Retour à la page de login</a>';
}
?>
</body>
</html>