-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
251 lines (211 loc) · 5.94 KB
/
functions.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
date_default_timezone_set('Europe/Paris');
session_start();
//Constants
const FLASH = 'FLASH_MESSAGES';
const FORM = 'FORM_INFO';
//flash type
const FLASH_ERROR = 'error';
const FLASH_WARNING = 'warning';
const FLASH_INFO = 'info';
const FLASH_SUCCESS = 'success';
//flash name
const ERROR_LOGIN = 'error_login';
const ERROR_HANDLER = 'error_handler';
const SUCCESS_LOGIN = 'success_login';
function dd($var) //function for debug
{
echo "<pre style='font-size: 18px'>";
print_r($var);
echo "</pre>";
die();
}
function dump($var) //function for debug
{
echo "<pre>";
print_r($var);
echo "</pre>";
}
function get_user_lang() {
$lang = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$lang = strtolower(substr(chop($lang[0]), 0, 2));
if(isset($lang) && ($lang == 'en' || $lang == 'fr')) {
return $lang;
} else {
return 'en';
}
}
function init_php_session(): bool //init php session
{
if (!session_id()) {
session_start();
session_regenerate_id();
return true;
}
return false;
}
function clean_php_session(): void //clean the php session
{
session_unset();
session_destroy();
}
function is_logged(): bool
{
if (isset($_SESSION["profile"])) {
return true;
}
return false;
}
function errorHandler($errno, $errstr, $errfile, $errline) //error
{
/* $error = [$errno, $errstr, $errfile, $errline]; //information about the error (for debug) */
create_flash_message(ERROR_HANDLER, 'An error has occured, please try again later.', FLASH_ERROR);
/* Don't execute PHP internal error handler */
return true;
}
function fatalErrorHandler() //fatal error
{
if(!is_logged()) {
create_flash_message(ERROR_HANDLER, 'An error has occured, please try again later.', FLASH_ERROR);
}
/* Don't execute PHP internal error handler */
return true;
}
//create a flash message
function create_flash_message(string $name, string $message, string $type): void
{
if (isset($_SESSION[FLASH][$name])) {
unset($_SESSION[FLASH][$name]);
}
$_SESSION[FLASH][$name] = ['message' => $message, 'type' => $type];
}
//check if flash message is define by name
function isset_flash_message_by_name(string $name): bool
{
if (isset($_SESSION[FLASH][$name])) {
return true;
} else {
return false;
}
}
//check if flash message is define by type
function isset_flash_message_by_type(string $type): bool
{
if (isset($_SESSION[FLASH])) {
foreach ($_SESSION[FLASH] as $key => $value) {
if ($value['type'] == $type) {
return true;
} else {
return false;
}
}
}
return false;
}
//Delete flash message by name
function delete_flash_message_by_name(string $name): bool
{
if (isset($_SESSION[FLASH][$name])) {
unset($_SESSION[FLASH][$name]);
} else {
return false;
}
}
//delete flash message by type
function delete_flash_message_by_type(string $type): bool
{
if (isset($_SESSION[FLASH])) {
foreach ($_SESSION[FLASH] as $key => $value) {
if ($value['type'] == $type) {
unset($_SESSION[FLASH][$key]);
} else {
return false;
}
}
}
return false;
}
//Display flash message by type
function display_flash_message_by_name(string $name): void
{
if (!isset($_SESSION[FLASH][$name])) {
return;
}
$flash_message[$name] = $_SESSION[FLASH][$name];
unset($_SESSION[FLASH][$name]);
echo $flash_message[$name]['message'];
}
//Display flash message by type
function display_flash_message_by_type(string $type): void
{
if (isset($_SESSION[FLASH])) {
foreach ($_SESSION[FLASH] as $key => $value) {
if ($value['type'] == $type) {
$flash_message = $value['message'];
unset($_SESSION[FLASH][$key]);
echo $flash_message;
}
}
}
}
// Check if a value is a integer (
// "23" return true, 23 return true, 23.4 return false
function isInteger($input)
{
return (ctype_digit(strval($input)));
}
function getAverageFromGrades($grades) {
$coefs = 0;
$additionnalAverage = 0;
if(isset($grades) && !empty($grades)) {
foreach($grades as $grade) {
if($grade->coef != 'N.C.') {
$coef = null;
if(!empty($grade->grades)) {
$coef = intval($grade->coef);
$additionnalAverage = $additionnalAverage + floatval($grade->ccaverage) * intval($grade->coef);
}
if(isset($grade->average)) {
$coef = intval($grade->coef);
$additionnalAverage = $additionnalAverage + floatval($grade->average) * intval($grade->coef);
}
if(isset($coef)) {
$coefs = $coefs + $coef;
}
}
}
}
return $additionnalAverage / $coefs;
}
// Sort the agenda multiple times
function sortAgenda(){
//to sort the agenda
if(isset($_SESSION['agenda'])) {
usort($_SESSION['agenda'], "cmp");
}
}
//to sort the agenda array by starting date
function cmp($a, $b)
{
return $a->start_date - $b->start_date;
}
//generate an unique token
function guidv4($data = null)
{
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);
// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
function redirectToReferer(string $path) {
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) {
header('location: '.$_SERVER['HTTP_REFERER']);
} else {
header('location: '.$path);
}
}