-
Notifications
You must be signed in to change notification settings - Fork 0
/
accesbd.class.php
327 lines (281 loc) · 11 KB
/
accesbd.class.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
include 'employedao.class.php';
include_once('constants.php');
class AccesBD
{
private $pdo;
private $employeDao;
public function __construct()
{
try {
$this->pdo = new PDO('sqlite:' . dirname(__FILE__) . BD_PATH);
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo "Impossible d'accéder à la base de données SQLite : " . $e->getMessage();
die();
}
$this->employeDao = new EmployeDao();
}
public function creerTableEmployes()
{
$this->pdo->query("CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
firstName VARCHAR(50),
lastName VARCHAR(50),
username VARCHAR (50),
password VARCHAR (50),
color VARCHAR (10),
picture BLOB,
isAdmin INTEGER,
email VARCHAR (100),
hasRpi INTEGER
);
");
}
public function creerConfigurations()
{
$this->pdo->query("CREATE TABLE IF NOT EXISTS config (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rpiNetwork VARCHAR(20),
mailgunDomain VARCHAR(100),
mailgunApiKey VARCHAR(100),
db_updated INTEGER
);
");
$this->pdo->query("INSERT INTO config (id, rpiNetwork, mailgunDomain, mailgunApiKey, db_updated)
VALUES (1, '192.168.0.0', 'dinf.qc.to', 'key-8gn080hwlfdud6ctmtm1d66ulwg4p0t9', 1)");
$this->pdo->query("CREATE TRIGGER update_db_insert AFTER INSERT ON users
BEGIN
UPDATE config SET db_updated = 1;
END;
");
$this->pdo->query("CREATE TRIGGER update_db_update AFTER UPDATE ON users
BEGIN
UPDATE config SET db_updated = 1;
END;
");
$this->pdo->query("CREATE TRIGGER update_db_delete AFTER DELETE ON users
BEGIN
UPDATE config SET db_updated = 1;
END;
");
}
public function modifierRpiNetwork($rpiNetwork)
{
$this->pdo->query("UPDATE config
SET rpiNetwork='".$rpiNetwork."'
WHERE id=1");
}
public function getRpiNetwork()
{
$stmt = $this->pdo->prepare("SELECT rpiNetwork FROM config WHERE id = 1");
$stmt->execute();
$result = $stmt->fetchAll();
return $result[0]['rpiNetwork'];
}
public function modifierMailgunDomain($mailgunDomain)
{
$this->pdo->query("UPDATE config
SET mailgunDomain='".$mailgunDomain."'
WHERE id=1");
}
public function getMailgunDomain()
{
$stmt = $this->pdo->prepare("SELECT mailgunDomain FROM config WHERE id = 1");
$stmt->execute();
$result = $stmt->fetchAll();
return $result[0]['mailgunDomain'];
}
public function modifierMailgunApiKey($mailgunApiKey)
{
$this->pdo->query("UPDATE config
SET mailgunApiKey='".$mailgunApiKey."'
WHERE id=1");
}
public function getMailgunApiKey()
{
$stmt = $this->pdo->prepare("SELECT mailgunApiKey FROM config WHERE id = 1");
$stmt->execute();
$result = $stmt->fetchAll();
return $result[0]['mailgunApiKey'];
}
public function utilisateurValide($username, $enteredPassword)
{
$storedPassword = $this->employeDao->getByUsername($username)->getPassword();
return password_verify($enteredPassword, $storedPassword);
}
public function genererImage($id) {
$stmt = $this->pdo->prepare("SELECT picture FROM users WHERE id = " . $id);
$stmt->execute();
$result = $stmt->fetchAll();
return $result[0]['picture'];
}
public function applicationNonInstallee()
{
try {
$this->pdo->prepare("SELECT 1 FROM users LIMIT 1");
} catch (Exception $e) {
return true;
}
return false;
}
public function pasDeConfig()
{
try {
$this->pdo->prepare("SELECT 1 FROM config LIMIT 1");
} catch (Exception $e) {
return true;
}
return false;
}
public function supprimerTables()
{
$this->pdo->query("DROP TABLE IF EXISTS users;");
$this->pdo->query("DROP TABLE IF EXISTS config;");
}
public function creerUsagersStandards()
{
$admin = (new EmployeBuilder())
->id(0)
->username("admin")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->isAdmin(true)
->build();
$olivier = (new EmployeBuilder())
->username("lafleuro")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#0074D9")
->email("[email protected]")
->firstName("Olivier")
->lastName("Lafleur")
->isAdmin(true)
->picture(file_get_contents("usagers_images/sauvage.png"))
->hasRpi(true)
->build();
$guillaume = (new EmployeBuilder())
->username("michaudg")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#7FDBFF")
->email("[email protected]")
->firstName("Guillaume")
->lastName("Michaud")
->isAdmin(true)
->picture(file_get_contents("usagers_images/gargamel.jpeg"))
->hasRpi(true)
->build();
$melissa = (new EmployeBuilder())
->username("clermontm")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#2ECC40")
->email("[email protected]")
->firstName("Mélissa")
->lastName("Clermont")
->picture(file_get_contents("usagers_images/schtroumpfette.png"))
->build();
$gilles = (new EmployeBuilder())
->username("champagneg")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#01FF70")
->email("[email protected]")
->firstName("Gilles")
->lastName("Champagne")
->picture(file_get_contents("usagers_images/grandschtroumpf.png"))
->build();
$josee = (new EmployeBuilder())
->username("lainessej")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#FFDC00")
->email("[email protected]")
->firstName("Josée")
->lastName("Lainesse")
->picture(file_get_contents("usagers_images/bebe.png"))
->build();
$marc = (new EmployeBuilder())
->username("deslandesm")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#FF851B")
->email("[email protected]")
->firstName("Marc")
->lastName("Deslandes")
->picture(file_get_contents("usagers_images/fleur.png"))
->build();
$lise = (new EmployeBuilder())
->username("provencherl")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#FF4136")
->email("[email protected]")
->firstName("Lise")
->lastName("Provencher")
->picture(file_get_contents("usagers_images/tada.png"))
->build();
$serge = (new EmployeBuilder())
->username("levesques")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#F012BE")
->email("[email protected]")
->firstName("Serge")
->lastName("Lévesque")
->picture(file_get_contents("usagers_images/vantard.png"))
->build();
$yvan = (new EmployeBuilder())
->username("morrisseyy")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#0074D9")
->email("[email protected]")
->firstName("Yvan")
->lastName("Morrissey")
->picture(file_get_contents("usagers_images/tada.png"))
->build();
$mers = (new EmployeBuilder())
->username("merciers")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#7FDBFF")
->email("[email protected]")
->firstName("Stéphane")
->lastName("Mercier")
->picture(file_get_contents("usagers_images/fleur.png"))
->build();
$christian = (new EmployeBuilder())
->username("asselinc")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#2ECC40")
->email("[email protected]")
->firstName("Christian")
->lastName("Asselin")
->picture(file_get_contents("usagers_images/vantard.png"))
->build();
$nelson = (new EmployeBuilder())
->username("marceaun")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#01FF70")
->email("[email protected]")
->firstName("Nelson")
->lastName("Marceau")
->picture(file_get_contents("usagers_images/bebe.png"))
->build();
$jocelyne = (new EmployeBuilder())
->username("lapointej")
->password(password_hash("admin#123", PASSWORD_DEFAULT))
->color("#FFDC00")
->email("[email protected]")
->firstName("Jocelyne")
->lastName("Lapointe")
->picture(file_get_contents("usagers_images/schtroumpfette.png"))
->build();
$this->employeDao->insert($admin);
$this->employeDao->put($olivier);
$this->employeDao->put($guillaume);
$this->employeDao->put($melissa);
$this->employeDao->put($gilles);
$this->employeDao->put($josee);
$this->employeDao->put($marc);
$this->employeDao->put($lise);
$this->employeDao->put($serge);
$this->employeDao->put($yvan);
$this->employeDao->put($mers);
$this->employeDao->put($christian);
$this->employeDao->put($nelson);
$this->employeDao->put($jocelyne);
}
}