-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.php
702 lines (623 loc) · 22.4 KB
/
database.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<?php
require 'KLogger.php';
class Database{
private $dsn;
private $user;
private $password;
protected $logger;
public function __construct(){
$this->logger = new KLogger("logs/log.txt", KLogger::DEBUG);
$url = parse_url("mysql://b3ad4171a41e4e:000f8d66@us-cdbr-east-03.cleardb.com/heroku_67195a60087b2ce?reconnect=true");
$this->user = $url["user"];
$this->password = $url["pass"];
$db = substr($url["path"], 1);
$host = $url['host'];
$this->dsn = "mysql:dbname=heroku_67195a60087b2ce;host=us-cdbr-east-03.cleardb.com";
}
private function makeConnection(){
try{
return new PDO($this->dsn, $this->user, $this->password);
}
catch(PDOException $exception){
$this->logger->LogFatal("Database connection failed! " . $exception);
}
}
public function verifyLogin($email, $password){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT count(*) as total FROM Account WHERE email = :email AND password = :pass");
$q->bindParam(':email', $email);
$q->bindParam(':pass', $password);
$q->execute();
$row = $q->fetch();
if($row['total'] == 1){
$this->logger->LogInfo("User found!" . print_r($row, 1));
return true;
}
else{
return false;
}
}
catch(Exception $e){
$this->logger->LogWarn($e);
exit;
}
}
public function deleteUserVehicle($uid, $vID){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Delete FROM accountVehicle WHERE accountID = :uid and vehicleID = :vID");
$q->bindParam(':uid', $uid);
$q->bindParam(':vID', $vID);
$q->execute();
$this->logger->LogDebug('Deleting vehicle from user'. $uid);
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function deleteCart($uid){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Delete FROM Cart WHERE accountID = :uid");
$q->bindParam(':uid', $uid);
$q->execute();
$this->logger->LogDebug('Deleting entire cart for user'. $uid);
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function deletePartialCart($uid, $partID){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Delete FROM Cart WHERE accountID = :uid and partID = :partID");
$q->bindParam(':uid', $uid);
$q->bindParam(':partID', $partID);
$q->execute();
$this->logger->LogDebug('Deleting cart item '.$partID. ' for user'. $uid);
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function searchParts($searchString){
$conn = $this->makeConnection();
$searchString = "%{$searchString}%";
try{
$q = $conn->prepare("Select * From Part WHERE partID LIKE :partID or partName LIKE :partName");
$q->bindParam(':partID', $searchString);
$q->bindParam(':partName', $searchString);
$q->execute();
$row = $q->fetchAll();
return $row;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getAllMake(){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Select DISTINCT make From Vehicle ORDER BY make ASC");
$q->execute();
$row = $q->fetchAll();
return $row;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getModel($make){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Select DISTINCT model From Vehicle where make = :make ORDER BY model ASC");
$q->bindParam(":make", $make);
$q->execute();
$row = $q->fetchAll();
return $row;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getYear($make, $model){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Select year From Vehicle where model = :model and make = :make ORDER BY year ASC");
$q->bindParam(":model", $model);
$q->bindParam(":make", $make);
$q->execute();
$row = $q->fetchAll();
return $row;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function printAllParts(){
$parts = $this->getParts();
foreach($parts as $part){
echo'<tr>
<form method = "post" action = "ATC_handler.php">
<td>'.$part['partID'].'</td>
<td>'.$part['partName'].'</td>
<td>'.$part['price'].'</td>
<input type = "hidden" name = "id" value = '.$part['partID'].'></input>
<td><button type = "submit" class = "atc">Add to Cart</button></td>
</tr>
</form>';
}
}
public function getParts(){
$conn = $this->makeConnection();
try{
$sql = "SELECT * FROM Part";
$result = $conn->query($sql, PDO::FETCH_ASSOC);
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
return $result->fetchAll();
}
public function inCart($partID, $userID){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Select count(*) as total From Cart WHERE partID = :partID and accountID = :userID");
$q->bindParam(':partID', $partID);
$q->bindParam(':userID', $userID);
$q->execute();
$row = $q->fetch();
if($row['total']){
$this->logger->LogDebug("Found row in cart for " .$partID . "and user " .$userID);
return true;
}
return false;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getOrder($uid){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT * From Orders Where accountID = :uid");
$q->bindParam(':uid', $uid);
$q->execute();
$row = $q->fetchAll();
$this->logger->LogDebug("Found orders for user" .$uid);
return $row;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getVehicleID($make, $model, $year){
$conn = $this->makeConnection();
try{
$q = $q = $conn->prepare("Select vehicleID From Vehicle WHERE make = :make and model = :model and year = :year");
$q->bindParam(':make', $make);
$q->bindParam(':model', $model);
$q->bindParam(':year', $year);
$q->execute();
$row = $q->fetchAll();
$this->logger->LogDebug("Found orders for user" .$make);
return $row;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getOrderInfo($oNum){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT Part.partName, Part.imageSrc, Part.Price, Order_Manifest.quantity From Order_Manifest JOIN Part ON Order_Manifest.partID = Part.partID Where Order_Manifest.orderNumber = :oNum");
$q->bindParam(':oNum', $oNum);
$q->execute();
$row = $q->fetchAll();
$this->logger->LogDebug("Found orders for " .$oNum);
return $row;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function verifyPart($partID){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Select count(*) as total From Part WHERE partID = :partID");
$q->bindParam(':partID', $partID);
$q->execute();
$row = $q->fetch();
if($row['total']){
$this->logger->LogDebug("Found row for part " .$partID);
return true;
}
return false;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getVehiclesUser($userID){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT DISTINCT accountVehicle.vehicleID, vehicle.make, vehicle.model, vehicle.year From accountVehicle JOIN vehicle ON accountVehicle.vehicleID = vehicle.vehicleID Where accountVehicle.accountID = :accountID");
$q->bindParam(':accountID', $userID);
$q->execute();
$row = $q->fetchAll();
$this->logger->LogDebug("Found vehicles for " .$userID);
return $row;
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getPartInfo($id){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Select * From Part WHERE partID = :partID");
$q->bindParam(':partID', $id);
$q->execute();
$row = $q->fetch();
if($row){
$this->logger->LogDebug("Found row for part " . $id);
return $row;
}
else{
header('Location: index.php');
exit;
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
private function getFeaturedProducts(){
$conn = $this->makeConnection();
try{
$sql = "SELECT * FROM Part order by rand() limit 5";
$result = $conn->query($sql, PDO::FETCH_ASSOC);
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
return $result->fetchAll();
}
public function printFeatured(){
$products = $this->getFeaturedProducts();
foreach($products as $product){
echo'<div id ="product">
<div id = "desc">'.$product['partName'].'</div>
<img src = "'.$product["imageSrc"].'" class = "responsive">
<div id = "product_info">
<div id = "price">$'. $product['price'].'</div>
<div id = "info">'.$product['partDesc'].'</div>
<div id = "button">
<form method = "post" action = "ATC_handler.php">
<button class = "atc">Add to Cart</button>
<input type = "hidden" name = "id" value = "'.$product["partID"].'"></input>
</form>
</div>
</div>
</div>';
}
}
public function verifyVehicle($make, $model, $year){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("Select * From Vehicle WHERE make = :make and model = :model and year = :year");
$q->bindParam(':make', $make);
$q->bindParam(':model', $model);
$q->bindParam(':year', $year);
$q->execute();
$row = $q->fetch();
if($row){
return $row;
}
else{
return -1;
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function addVehicle($make, $model, $year, $accountID){
$conn = $this->makeConnection();
try{
$row = $this->verifyVehicle($make, $model, $year);
if($row != -1){
echo $row['vehicleID'];
$this->logger->LogDebug("Inserting Vehicle into account: " .$accountID);
$q = $conn->prepare("INSERT INTO accountVehicle(accountID, vehicleID) VALUES (:accountID, :vehicleID)");
$q->bindParam(':accountID', $accountID);
$q->bindParam(':vehicleID', $row['vehicleID']);
$q->execute();
}
else{
return -1;
}
}
catch(Exception $e){
$this->logger->LogWarn($e);
}
}
public function addToCart($accountID, $partID){
if(!($this->inCart($partID, $accountID))){
$conn = $this->makeConnection();
try{
$this->logger->LogDebug("Inserting Part: " . $partID . 'for account: ' . $accountID);
$q = $conn->prepare("INSERT INTO Cart (accountID, partID) VALUES (:accountID, :partID)");
$q->bindParam(':accountID', $accountID);
$q->bindParam(':partID', $partID);
$q->execute();
}
catch(Exception $e){
$this->logger->LogWarn($e);
}
}
}
public function getCart($accountID){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT partID From Cart WHERE accountID = :accountID");
$q->bindParam(':accountID', $accountID);
$q->execute();
$row = $q->fetchAll();
if($row){
$this->logger->LogDebug("Found cart for account " .$accountID);
return $row;
}
else{
$this->logger->LogDebug("No rows exist in the cart for account ". $accountID);
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function addUser($email, $password){
$conn = $this->makeConnection();
try{
$this->logger->LogDebug("Creating user with email: " . $email);
$q = $conn->prepare("INSERT INTO Account (email, password) VALUES (:email, :password)");
$q->bindParam(':email', $email);
$q->bindParam(':password', $password);
$q->execute();
}
catch(Exception $e){
$this->logger->LogWarn($e);
}
}
public function getStock($partID){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT stock FROM Part Where partID = :partID");
$q->bindParam(':partID', $partID);
$q->execute();
$row = $q->fetch();
return $row;
}
catch(Exception $e){
$this->logger->LogWarn($e);
exit;
}
}
public function updateStock($partID, $quantity){
$conn = $this->makeConnection();
$result = $this->getStock($partID);
try{
$this->logger->LogDebug("Updating stock for '.$partID.'");
$q = $conn->prepare("UPDATE Part SET stock = :newStock WHERE partID = :partID");
$q->bindParam(':partID', $partID);
$newStock = $result['stock'] - $quantity;
$q->bindParam(':newStock', $newStock);
$q->execute();
}
catch(Exception $e){
$this->logger->LogWarn($e);
}
}
public function addOrderManifest($orderNumber, $partID, $quantity){
$conn = $this->makeConnection();
try{
$this->logger->LogDebug("Adding order_manifest for '.$orderNumber.'");
$q = $conn->prepare("Insert Into Order_Manifest (orderNumber, partID, quantity) VALUES(:orderNumber, :partID, :quantity)");
$q->bindParam(':orderNumber', $orderNumber);
$q->bindParam(':partID', $partID);
$q->bindParam(':quantity', $quantity);
$q->execute();
}
catch(Exception $e){
$this->logger->LogWarn($e);
}
}
public function addOrder($orderID, $orderAmount, $accountID){
$conn = $this->makeConnection();
try{
$this->logger->LogDebug("Adding order for '.$accountID.'");
$q = $conn->prepare("Insert Into Orders (orderNumber, orderAmount, orderDate, accountID) VALUES(:orderID, :orderAmount, :date, :accountID)");
$date = date("Y-m-d");
$q->bindParam(':orderID', $orderID);
$q->bindParam(':orderAmount', $orderAmount);
$q->bindParam(':date', $date);
$q->bindParam(':accountID', $accountID);
$q->execute();
}
catch(Exception $e){
$this->logger->LogWarn($e);
}
}
public function updateUser($uID, $email, $address, $city, $state, $zip){
$conn = $this->makeConnection();
try{
$this->logger->LogDebug("Updating Account '.$uID.'");
$q = $conn->prepare("UPDATE Account SET email = :email, address = :address, city = :city, state = :state, zip = :zip WHERE accountID = :uid");
$q->bindParam(':uid', $uID);
$q->bindParam(':email', $email);
$q->bindParam(':address', $address);
$q->bindParam(':city', $city);
$q->bindParam(':state', $state);
$q->bindParam(':zip', $zip);
$q->execute();
}
catch(Exception $e){
$this->logger->LogWarn($e);
}
}
public function getUserInfo($uid){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT state, city, zip, address From Account WHERE accountID = :uid");
$q->bindParam(':uid', $uid);
$q->execute();
$row = $q->fetch();
if($row){
$this->logger->LogDebug("Found id for account " .$uid);
return $row;
}
else{
$this->logger->LogDebug("User does not exist for account ". $uid);
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getUserByEmail($email){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT accountID From Account WHERE email = :email");
$q->bindParam(':email', $email);
$q->execute();
$row = $q->fetch();
if($row){
$this->logger->LogDebug("Found id for account " .$email);
return $row;
}
else{
$this->logger->LogDebug("User does not exist for account ". $email);
return FALSE;
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getUserPassword($email){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT password From Account WHERE email = :email");
$q->bindParam(':email', $email);
$q->execute();
$row = $q->fetch();
if($row){
$this->logger->LogDebug("Found id for account " .$email);
return $row;
}
else{
$this->logger->LogDebug("User does not exist for account ". $email);
return FALSE;
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getUserPasswordByID($uID){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT password From Account WHERE accountID= :id");
$q->bindParam(':id', $uID);
$q->execute();
$row = $q->fetch();
if($row){
$this->logger->LogDebug("Found user info for account " .$uID);
return $row;
}
else{
$this->logger->LogDebug("User does not exist for account ". $uID);
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getUserID($email, $password){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT accountID From Account WHERE email = :email AND password = :pass");
$q->bindParam(':email', $email);
$q->bindParam(':pass', $password);
$q->execute();
$row = $q->fetch();
if($row){
$this->logger->LogDebug("Found id for account " .$email);
return $row;
}
else{
$this->logger->LogDebug("User does not exist for account ". $email);
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function getUserByID($id){
$conn = $this->makeConnection();
try{
$q = $conn->prepare("SELECT * From Account WHERE accountID= :id");
$q->bindParam(':id', $id);
$q->execute();
$row = $q->fetch();
if($row){
$this->logger->LogDebug("Found user info for account " .$id);
return $row;
}
else{
$this->logger->LogDebug("User does not exist for account ". $id);
}
}
catch(Exception $e){
$this->logger->LogWarn(print_r($e, 1));
exit;
}
}
public function updatePassword($uid, $password){
$conn = $this->makeConnection();
try{
$this->logger->LogDebug("Updating password for account '.$uid.'");
$q = $conn->prepare("UPDATE Account SET password = :password WHERE accountID = :uid");
$q->bindParam(':uid', $uid);
$q->bindParam(':password', $password);
$q->execute();
}
catch(Exception $e){
$this->logger->LogWarn($e);
}
}
}