-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
143 lines (121 loc) · 3.22 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
<?php
require_once(dirname(__FILE__) . "/config.php");
function connectToDb()
{
global $config;
try {
$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Échec lors de la connexion : ' . $e->getMessage();
$db = null;
}
return $db;
}
function accessForbidden()
{
echo "<div class='alert alert-danger'>Accès interdit</div>";
include_once(TEMPLATES_PATH . '/footer.php');
exit(403);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function convertDateToSql($date)
{
Try {
if (!is_a($date, "DateTime")) {
if (strpos($date, '/'))
$date = DateTime::createFromFormat("d/m/Y", $date);
else if (strpos($date, '-'))
$date = DateTime::createFromFormat("d-m-Y", $date);
}
$date = $date->format("Y-m-d");
} catch (Exception $e) {
echo $e->getMessage();
}
return $date;
}
function convertDateFromSql($date)
{
Try {
if (!is_a($date, "DateTime")) {
if (strpos($date, '/'))
$date = DateTime::createFromFormat("d/m/Y", $date);
else if (strpos($date, '-'))
$date = DateTime::createFromFormat("Y-m-d", $date);
}
$date = $date->format("d-m-Y");
} catch (Exception $e) {
echo $e->getMessage();
}
return $date;
}
function today()
{
return date("d-m-Y");
}
function compareDate($date1, $date2)
{
echo "<br />";
$d1ex = explode("-", $date1);
$d2ex = explode("-", $date2);
if ($d2ex[2] > $d1ex[2]) {
return true;
} else {
if ($d2ex[1] >= $d1ex[1]) {
return true;
} else {
if ($d2ex[0] >= $d1ex[0])
return true;
}
}
return false;
}
function isPostSet($var)
{
return (isset($_POST[$var]) && !empty($_POST[$var]));
}
function isGetSet($var)
{
return (isset($_GET[$var]) && !empty($_GET[$var]));
}
function json($json)
{
$json = json_encode($json);
$nb = strlen($json);
if ($nb > 0) {
$json[0] = " ";
$json[$nb - 1] = " ";
}
return $json;
}
function testDate($date)
{
try {
$d = DateTime::createFromFormat("d-m-Y", $date);
$d = $d->format("d-m-y");
} catch (Exception $e) {
return 1;
}
return $d;
}
error_reporting(0);
function shutDownFunction()
{
$error = error_get_last();
// fatal error, E_ERROR === 1
if ($error['type'] === E_ERROR) {
errorHandler(E_ERROR, $error['message'], $error['file'], $error['line']);
}
}
register_shutdown_function('shutDownFunction');
function errorHandler($code, $message, $file, $line)
{
echo '{"Code" : ' . $code . ', "Message" : "' . $message . '", "File" : "' . $file . '", "Line" : ' . $line . '}';
}
set_error_handler('errorHandler');