-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.php
170 lines (167 loc) · 8.15 KB
/
signup.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
<?php
session_start();
if(isset($_SESSION['user_id'])){
header("location:./index.php");
}else{
$postMethod = $_SERVER['REQUEST_METHOD'] == 'POST';
if($_SERVER['REQUEST_METHOD'] == 'POST'){
require_once "./functions/database.php";
function validateUsername(string $str) {
return preg_match('/^[a-zA-Z]/', $str) && strlen($str) >= 4 && preg_match('/^[a-zA-Z][0-9a-zA-Z_-]*$/', $str);
}
function only_nums_and_chars(string $str) {
return preg_match('/^[0-9a-zA-Z_-]*$/', $str);
}
$username= $_POST['username'] ?? "";
$email= $_POST['email'] ?? "";
$password= $_POST['password'] ?? "";
$confirmer_password= $_POST['confirmer_password'] ?? "";
$erreursArr=[];
if (empty($username)) {
$erreursArr['username']="le champs username est obligatoire";
}elseif (! validateUsername($username) && ! only_nums_and_chars($username)) {
$erreursArr['username']="les caractéres spéciaux ne sont pas validés";
}elseif ( ! validateUsername($username) && only_nums_and_chars($username)) {
$erreursArr["username"]="username doit commencé par un charactère (et taille > 4)";
}else{
$user = $pdo->quote($username);
$query = $pdo->query("SELECT * FROM user WHERE username=$user");
$res = $query->fetch();
if($res){
$erreursArr['username']="username existe déja";
}
}
if ($email =="") {
$erreursArr['email']="le champs email est obligatoire";
}else if(filter_var($email, FILTER_VALIDATE_EMAIL) == false){
$erreursArr['email']="l'email est incorrecte \n exemples : [email protected]";
}else{
$mail = $pdo->quote($email);
$query = $pdo->query("SELECT * FROM user WHERE email=$mail");
$resultat = $query->fetch();
if($resultat){
$erreursArr['email']="email existe déja";
}
}
if(strlen($password)==0){
$erreursArr['password']="le champs password est obligatoire";
}elseif(preg_match('/[A-Z]/', $password) == false){
$erreursArr['password']= "Le mot de passe doit contenir au moins une lettre majuscule.";
}elseif(!preg_match('/[a-z]/', $password)){
$erreursArr['password']= "Le mot de passe doit contenir au moins une lettre miniscule.";
}elseif(!preg_match('/[0-9]/', $password)){
$erreursArr['password']= "Le mot de passe doit contenir au moins un chiffre.";
}elseif(!preg_match('/[!@#$%^&*()_+=-]/',$password)){
$erreursArr['password']= "Le mot de passe doit contenir au moins un caractère special.";
}elseif(strlen($password) < 8 ){
$erreursArr['password']="Le mot de passe doit comporter au moins 8 caractères.";
}
if(strlen($confirmer_password) == 0){
$erreursArr['confirmer_password']="le champs de confirmation mot de passe est obligatoire";
}elseif ($confirmer_password != $password) {
$erreursArr['confirmer_password']="les deux mots de passes ne sont pas identiques";
}
if(sizeof($erreursArr) > 0) {
function erreursTable($erreursArr){
echo "<table class=\"table w-100 table-striped table-light table-hover\">
<thead class=\"table-danger\">
<tr>
<th scope=\"col\">#</th>
<th scope=\"col\">First</th>
</tr>
</thead>
<tbody>";
foreach($erreursArr as $key => $value):
echo "<tr>
<th>$key</th>
<td class=\"text-danger \">$value</td>
</tr>";
endforeach;
echo " </tbody>
</table>";
}
}else{
$hash = password_hash($password, PASSWORD_DEFAULT);
// $username =$pdo->quote($username);
// $email = $pdo->quote($email);
// $hash =$pdo->quote($hash);
$addUser = $pdo->prepare("INSERT INTO user(userName, email, password) VALUES(?, ?, ?)");
$addUser->execute([$username,$email,$hash]);
if($addUser->rowCount() > 0){
$userAdded=true ;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/style.css">
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="icon" href="./logoCinema.png">
<title>s'enregistrer</title>
</head>
<body class="bg-img3">
<div class="centered py-3 px-5 rounded bg-light shadow-lg p-3 mb-5">
<h2 class="text-center m-1">inscription des utilisateurs</h2>
<?php
if (isset($userAdded) && $userAdded=true) {
echo" <div class=\"alert alert-dismissible alert-success\">
<button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\"></button>
<strong>vous êtes inscrit avec succés</strong> <a href=\"./signIn.php\" class=\"alert-link text-primary\">se connecter</a>.
</div>";
}
?>
<form action="./signup.php" method="post" id="user_signup" autocomplete="off" novalidate>
<fieldset>
<div class="form-group">
<label for="username" class="form-label mt-1">Username</label>
<input type="text" class="form-control" name="username" aria-describedby="usernamedesc" id="username"placeholder="entrez username" value="<?php if($postMethod): echo $username; else: echo ''; endif ?>">
<small id="usernamedesc" class="form-text"></small>
</div>
<div class="form-group">
<label for="email" class="form-label mt-1">Email address</label>
<input type="email" class="form-control" name="email" id="email" aria-describedby="emaildesc" placeholder="Entrer email" value="<?php if($postMethod): echo $email; else: echo ''; endif ?>">
<small id="emaildesc" class="form-text">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="password" class="form-label mt-1">Password</label>
<input type="password" class="form-control" name="password" aria-describedby="pwddesc" id="password" placeholder="Password" value="<?php if($postMethod): echo $password; else: echo 'a'; endif ?>">
<small id="pwddesc" class="form-text"></small>
</div>
<div class="form-group">
<label for="confirmer_password" class="form-label mt-1">confirmer password</label>
<input type="password" class="form-control" name="confirmer_password" aria-describedby="cpwddesc" id="confirmer_password" placeholder="confirmer password">
<small id="cpwddesc" class="form-text"></small>
</div>
<div class="d-flex justify-content-between align-items-cente mt-2">
<button type="submit" class="btn btn-primary" id="submit-button">s'inscrire</button>
<a href="./signIn.php" class="text-decoration-none pt-1">Se connecter</a>
</div>
</fieldset>
</form>
</div>
<?php
if(isset($erreursArr) && sizeof($erreursArr) > 0):
?>
<div class="toast show w-50 centered" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">vous avez des erreurs</strong>
<button type="button" class="btn-close ms-2 mb-1" data-bs-dismiss="toast" aria-label="Close">
<span aria-hidden="true"></span>
</button>
</div>
<div class="toast-body">
<?php erreursTable($erreursArr) ?>
</div>
</div>
<?php endif;?>
<script src="node_modules/jquery/dist/jquery.slim.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="./javascript/signup.js"></script>
</body>
</html>
<?php } ?>