This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
175 lines (145 loc) · 6.01 KB
/
init.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
<?php
############################
# Const
############################
define("ROOT", str_replace('\\', '/', __DIR__));
/** Register, login errors code
*
* USR_NOT_VALID : Invalid/empty username
* EMAIL_NOT_VALID : Invalid/empty email
* PASSWORD_NOT_VALID : Invalid/empty password
*
* USR_ALREADY_USED : The username is already taken
* EMAIL_ALREADY_USED : The email is already taken
*
* PASSWORD_DONT_MATCH : The two passwords doesn't match
* PASSWORD_NOT_SECURE : The password is not secure (1 lower case char, 1 upper case char, 1 number, 1 special char, min 8 chars)
* BAD_PASSWORD : The password doesn't match the one is the database
*
* UNKNOWN_REGISTER_ERROR : Can't add the user in the database
* UNKNOWN_USER : Can't find the user
*
* CANNOT_DISCONNECT : Cannot destroy the user's session
*/
define("USR_NOT_VALID", 1);
define("EMAIL_NOT_VALID", 2);
define("PASSWORD_NOT_VALID", 3);
define("USR_ALREADY_USED", 4);
define("EMAIL_ALREADY_USED", 5);
define("PASSWORD_DONT_MATCH", 6);
define("PASSWORD_NOT_SECURE", 7);
define("BAD_PASSWORD", 8);
define("UNKNOWN_REGISTER_ERROR", 20);
define("UNKNOWN_USER", 21);
define("CANNOT_DISCONNECT", 40);
/** Register, login success code
*
* REGISTRATION_COMPLETE : User successfully registered
*/
define("REGISTRATION_COMPLETE", 100);
/** Todo lists errors code
*
* TODO_LIST_NOT_FOUND : Invalid/empty username
* LIST_NAME_NOT_VALID : Invalid/empty list name
* CANNOT_UPDATE_LIST : Unknown error while updating list
* CANNOT_ACCEPT_SHARE : Unknown error while accepting a share
*/
define("TODO_LIST_NOT_FOUND", 200);
define("LIST_NAME_NOT_VALID", 201);
define("CANNOT_UPDATE_LIST", 210);
define("CANNOT_ACCEPT_SHARE", 220);
############################
# Session
############################
if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); }
$is_connected = (isset($_SESSION["user_id"]) AND !empty($_SESSION["user_id"]));
############################
# Import config file
############################
$config = parse_ini_file(ROOT . "/config/config.ini", true);
############################
# Load classes
############################
function loadClasses($classname) { require_once ROOT . "/classes/$classname.php"; }
spl_autoload_register("loadClasses");
############################
# Connect to database
############################
$db = PDOFactory::mySql($config["DB"]["host"], $config["DB"]["dbname"], $config["DB"]["user"], $config["DB"]["password"]);
############################
# Functions
############################
/**
* Redirect to the specified php file.
*
* This function redirects using the header()
* function with the file path and GET variables
* if specified. Then the PHP script is stopped
* with exit().
*
* @author Alexis
* @version 1.0
*
* @function redirectTo
* @param string $filePath Path to the php file
* @return void
*/
function redirectTo($filePath) {
header("Location: $filePath");
exit;
}
/**
* Get error and success messages in link.
*
* This function get errors and success in
* link and build a box with the corresponding
* message.
*
* @author Alexis
* @version 1.0
*
* @function getErrorsSuccess
* @return string HTML code of messages box
*/
function getErrorsSuccess() {
$messages = [
USR_NOT_VALID => "Le nom d'utilisateur renseigné n'est pas valide <em>(max. 32 caractères)</em>.",
EMAIL_NOT_VALID => "L'email renseigné n'est pas valide. Veuillez respecter le format <em>\"locale@domaine.ext\"</em>.",
PASSWORD_NOT_VALID => "Le mot de passe renseigné n'est pas valide <em>(une minuscule, une majuscule, un chiffre, un caractère spécial, huit caractères)</em>.",
USR_ALREADY_USED => "Le nom d'utilisateur renseigné est déjà utilisé.",
EMAIL_ALREADY_USED => "L'email renseigné est déjà utilisé.",
PASSWORD_DONT_MATCH => "Les mots de passes ne correspondent pas.",
PASSWORD_NOT_SECURE => "Le mot de passe n'est pas sécurisé <em>(une minuscule, une majuscule, un chiffre, un caractère spécial, huit caractères)</em>.",
BAD_PASSWORD => "Le mot de passe renseigné est incorrect.",
UNKNOWN_REGISTER_ERROR => "Erreur inconnue, impossible de vous enregistrer. Veuillez réessayer plus tard ou contactez le <a href=\"#\">support</a>.",
UNKNOWN_USER => "Le nom d'utilisateur renseigné n'existe pas.",
CANNOT_DISCONNECT => "Erreur inconnue, impossible de finaliser la déconnexion. Veuillez réessayer plus tard ou contactez le <a href=\"#\">support</a>.",
REGISTRATION_COMPLETE => "Inscription réussie ! Vous pouvez désormais vous connecter.",
TODO_LIST_NOT_FOUND => "La todo list n'existe pas.<br><a href=\"./index.php\"><i class=\"fas fa-long-arrow-alt-left\"></i> Retour aux todo lists</a>",
LIST_NAME_NOT_VALID => "Le nom de la liste est trop long (maximum 32 caractères)",
CANNOT_UPDATE_LIST => "Impossible de modifier la todo list. Veuillez réessayer plus tard ou contactez le <a href=\"#\">support</a>.",
"none" => "<em>Un problème est survenu lors de l'affichage de ce message</em>"
];
// Turn on output buffering
ob_start();
// Process errors
if (isset($_GET["error"]) AND !empty($_GET["error"])) {
?>
<div class="errorBox">
<p><?= $messages[$_GET["error"]] ?? $messages["none"]; ?></p>
</div>
<?php
}
// Process success
if (isset($_GET["success"]) AND !empty($_GET["success"])) {
?>
<div class="successBox">
<p><?= $messages[$_GET["success"]] ?? $messages["none"] ?></p>
</div>
<?php
}
// Get the final HTML code and stop the output buffering
$errorsSuccessMsg = ob_get_contents();
ob_end_clean();
return $errorsSuccessMsg;
}