-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.php
865 lines (803 loc) · 29.5 KB
/
operations.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
<?php
require_once('db.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);
ValidateAjaxRequest();
function ValidateAjaxRequest() {
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
ValidateAction();
}
}
function ValidateAction(){
if (isset($_POST["Action"]) && !empty($_POST["Action"])) {
$Action = $_POST["Action"];
PHPOperation($Action);
}
}
function PHPOperation($Action){
switch($Action) {
case "UnitTest": UnitTest($Action);
break;
case "AddAccount": AddAccount($Action);
break;
case "SignIn": SignIn($Action);
break;
case "SignInPet": SignInPet($Action);
break;
case "SignOut": SignOut($Action);
break;
case "FetchErrors": FetchErrors($Action);
break;
case "AddError": AddError($Action);
break;
case "FetchActivities": FetchActivities($Action);
break;
case "FetchSignInPet": FetchSignInPet($Action);
break;
case "FetchBreeds": FetchBreeds($Action);
break;
case "ValidateSession": ValidateSession($Action);
break;
case "ResetPassword": ResetPassword($Action);
break;
case "UpdatePassword": UpdatePassword($Action);
break;
case "AddPet": AddPet($Action);
break;
case "AddBreed": AddBreed($Action);
break;
case "FetchUsers": FetchUsers($Action);
break;
case "FetchUserPets": FetchUserPets($Action);
break;
case "FetchUserStatus": FetchUserStatus($Action);
break;
case "FetchPetStatus": FetchPetStatus($Action);
break;
case "FetchPet": FetchPet($Action);
break;
case "UpdateAccountStatus": UpdateAccountStatus($Action);
break;
case "UpdatePetStatus": UpdatePetStatus($Action);
break;
case "UpdatePetName": UpdatePetName($Action);
break;
case "UpdatePetBreed": UpdatePetBreed($Action);
break;
case "UpdatePetGender": UpdatePetGender($Action);
break;
case "UpdateBreed": UpdateBreed($Action);
break;
case "FetchPetNameCount": FetchPetNameCount($Action);
break;
case "FetchUserEmail": FetchUserEmail($Action);
break;
}
}
function Execute($Action,$Statement){
try {
$Statement->execute();
} catch (PDOException $e) {
$ErrorMSG = 'Error 0: ' . $e->getMessage() . "\n";
$PHP = 1;
AddError($Action,$ErrorMSG,$PHP);
}
}
function AdminRole($Action,$Email){
$Role = FetchAccountRole($Action,$Email);
if($Role != 2){
echo json_encode("expired");
exit;
}
}
function UserRole($Action,$Email){
$Role = FetchAccountRole($Action,$Email);
if($Role != 1){
echo json_encode("expired");
exit;
}
}
function AddActivity($Action,$Email,$ActivityMSG){
global $PDOconn;
$Query = 'CALL AddActivity (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
$Statement->bindParam(2, $ActivityMSG, PDO::PARAM_STR, 255);
Execute($Action,$Statement);
}
function HashIt($Password){
$HashedPassword = password_hash($Password, PASSWORD_DEFAULT);
return $HashedPassword;
}
function FetchUser($Action,$Email){
global $PDOconn;
$Query = 'CALL FetchUser (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetch(PDO::FETCH_ASSOC);
return $Response;
}
function FetchAdmins($Action){
global $PDOconn;
$Query = 'CALL FetchAdmins';
$Statement = $PDOconn->prepare($Query);
Execute($Action,$Statement);
$Response = $Statement->fetchAll();
return $Response;
}
function ValidatePassword($Password,$HashedPassword){
if (password_verify($Password, $HashedPassword)) {
return 1;
} else {
return 0;
}
}
function FetchAccountRole($Action,$Email){
global $PDOconn;
$Query = 'CALL FetchAccountRole (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetch(PDO::FETCH_ASSOC);
if($Response['Disabled'] == 0 && $Response['Attempt'] == 0){
if($Response['AdminCode'] == 2){
$AccountRole = 2;//admin
}else{
$AccountRole = 1;//registered user
}
}else{
session_unset();
session_destroy();
$AccountRole = 0;//user
}
return $AccountRole;
}
function FetchSession($Action,$AliID){
global $PDOconn;
$Query = 'CALL FetchSession (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $AliID, PDO::PARAM_STR, 64);
Execute($Action,$Statement);
$Response = $Statement->fetch(PDO::FETCH_ASSOC);
return $Response;
}
function PasswordGenerator(){
//https://www.catchstudio.com/labs/password-generator/
//$Password = hash('sha256', uniqid(rand(), true));
// Characters to use for the password
$Strings = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+=_,!@$#*%<>[]{}";
// Desired length of the password
$PasswordLength = 12;
// Length of the string to take characters from
$StringLength = strlen($Strings);
// RANDOM.ORG - We are pulling our list of random numbers as a
// single request, instead of iterating over each character individually
$uri = "http://www.random.org/integers/?";
$Random = file_get_contents(
$uri ."num=$PasswordLength&min=0&max=".($StringLength-1)."&col=1&base=10&format=plain&rnd=new"
);
$Indexes = explode("\n", $Random);
array_pop($Indexes);
// We now have an array of random indexes which we will use to build our password
$Password = '';
foreach ($Indexes as $int){
$Password .= substr($Strings, $int, 1);
}
return $Password;
}
function GetBrowserData(){
$SessionIP = $_SERVER['REMOTE_ADDR'];
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$BrowserName = 'Unknown';
$Platform = 'Unknown';
if (preg_match('/linux/i', $u_agent)) {
$Platform = 'Linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$Platform = 'Mac';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$Platform = 'Windows';
}
// Next get the name of the useragent yes seperately and for refresh reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$BrowserName = 'Internet Explorer';
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$BrowserName = 'Mozilla Firefox';
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$BrowserName = 'Google Chrome';
}
elseif(preg_match('/Safari/i',$u_agent))
{
$BrowserName = 'Apple Safari';
}
elseif(preg_match('/Opera/i',$u_agent))
{
$BrowserName = 'Opera';
}
elseif(preg_match('/Netscape/i',$u_agent))
{
$BrowserName = 'Netscape';
}
return array(
'IP' => $SessionIP,
'Browser' => $BrowserName,
'Platform' => $Platform
);
}
//Single use
function UnitTest($Action){
global $PDOconn;
$Query = 'CALL UTCreate';
$Statement = $PDOconn->prepare($Query);
Execute($Action,$Statement);
$UTValue = "1";
$Query = 'CALL UTInsert (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $UTValue, PDO::PARAM_INT);
Execute($Action,$Statement);
$UpdatedUTValue = "2";
$Query = 'CALL UTUpdate (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $UpdatedUTValue, PDO::PARAM_INT);
$Statement->bindParam(2, $UTValue, PDO::PARAM_INT);
Execute($Action,$Statement);
$Query = 'CALL UTDelete (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $UpdatedValue, PDO::PARAM_INT);
Execute($Action,$Statement);
$Query = 'CALL UTDrop';
$Statement = $PDOconn->prepare($Query);
Execute($Action,$Statement);
echo json_encode("Unit Test successful");//do I need try and catch for unit test? do I need to fetch?
$PDOconn = null;
}
function AddError($Action,$ErrorMSG,$PHP){
$Email = ValidateSession($Action);
if (!isset($Email)) {
$Email = NULL;
}
if (!isset($PHP)) {
$FailedAction = stripslashes($_POST["FailedAction"]);
$ErrorMSG = stripslashes($_POST["ErrorMSG"]);
}else{
$FailedAction = $Action;
}
global $PDOconn;
$Query = 'CALL AddError (?, ?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
$Statement->bindParam(2, $FailedAction, PDO::PARAM_STR, 45);
$Statement->bindParam(3, $ErrorMSG, PDO::PARAM_STR, 255);
$Statement->execute();
$PDOconn = null;
}
function UpdateAccountStatus($Action){
$AdminEmail = ValidateSession($Action);
AdminRole($Action,$AdminEmail);
$Disabled = stripslashes($_POST["D1"]);
$Email = stripslashes($_POST["D2"]);
global $PDOconn;
$Query = 'CALL UpdateAccountStatus (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Disabled, PDO::PARAM_INT, 1);
$Statement->bindParam(2, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
if($Disabled == 0){
$ActivityMSG = "Your account was activated by an Admin.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Account activated","Your account was activated by an Admin.");
$ActivityMSG = "You activated " . $Email . "'s account.";
AddActivity($Action,$AdminEmail,$ActivityMSG);
}else{
$ActivityMSG = "Your account was dis-activated by an Admin.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Account dis-activated","Your account was dis-activated by an Admin.");
$ActivityMSG = "You dis-activated " . $Email . "'s account";
AddActivity($Action,$AdminEmail,$ActivityMSG);
}
echo json_encode("refresh");
$PDOconn = null;
}
function UpdatePetStatus($Action){
$AdminEmail = ValidateSession($Action);
AdminRole($Action,$AdminEmail);
$Disabled = stripslashes($_POST["D1"]);
$PetName = stripslashes($_POST["D2"]);
$PetID = stripslashes($_POST["D3"]);
$Email = stripslashes($_POST["D4"]);
global $PDOconn;
$Query = 'CALL UpdatePetStatus (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Disabled, PDO::PARAM_INT, 1);
$Statement->bindParam(2, $PetID, PDO::PARAM_INT);
Execute($Action,$Statement);
if($Disabled == 0){
$ActivityMSG = $PetName . " has been activated by an Admin.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Pet activated","Your pet " . $PetName . " has been activated by an Admin.");
$ActivityMSG = "You activated " . $Email . "'s pet " . $PetName . ".";
AddActivity($Action,$AdminEmail,$ActivityMSG);
}else{
$ActivityMSG = $PetName . " has been dis-activated by an Admin.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Pet dis-activated","Your pet " . $PetName . " has been dis-activated by an Admin.");
$ActivityMSG = "You dis-activated " . $Email . "'s pet " . $PetName . ".";
AddActivity($Action,$AdminEmail,$ActivityMSG);
}
echo json_encode("refresh");
$PDOconn = null;
}
function UpdatePetName($Action){
$AdminEmail = ValidateSession($Action);
AdminRole($Action,$AdminEmail);
$PetName = stripslashes($_POST["D1"]);
$OldPetName = stripslashes($_POST["D2"]);
$Email = stripslashes($_POST["D3"]);
global $PDOconn;
$Query = 'CALL UpdatePetName (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $PetName, PDO::PARAM_STR, 45);
$Statement->bindParam(2, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$ActivityMSG = $OldPetName . "'s name was changed to " . $PetName . " by an Admin.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Pet name change","Your pet " . $OldPetName . "'s name was changed to " . $PetName . " by an Admin.");
$ActivityMSG = "You changed " . $Email . "'s pet name to " . $PetName . " from " . $OldPetName . ".";
AddActivity($Action,$AdminEmail,$ActivityMSG);
echo json_encode("refresh");
$PDOconn = null;
}
function UpdatePetBreed($Action){
$AdminEmail = ValidateSession($Action);
AdminRole($Action,$AdminEmail);
$BreedID = stripslashes($_POST["D1"]);
$PetName = stripslashes($_POST["D2"]);
$Email = stripslashes($_POST["D3"]);
global $PDOconn;
$Query = 'CALL UpdatePetBreed (?, ?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $BreedID, PDO::PARAM_INT);
$Statement->bindParam(2, $PetName, PDO::PARAM_STR, 45);
$Statement->bindParam(3, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$ActivityMSG = $PetName . "'s breed was changed by an Admin.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Pet breed change", $PetName . "'s breed was changed by an Admin.");
$ActivityMSG = "You've changed the breed of " . $Email . "'s pet name " . $PetName . ".";
AddActivity($Action,$AdminEmail,$ActivityMSG);
echo json_encode("refresh");
$PDOconn = null;
}
function UpdatePetGender($Action){
$AdminEmail = ValidateSession($Action);
AdminRole($Action,$AdminEmail);
$Gender = stripslashes($_POST["D1"]);
$PetName = stripslashes($_POST["D2"]);
$Email = stripslashes($_POST["D3"]);
global $PDOconn;
$Query = 'CALL UpdatePetGender (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Gender, PDO::PARAM_STR, 4);
$Statement->bindParam(2, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$ActivityMSG = $PetName . "'s gender was changed to a " . $Gender . " by an Admin.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Pet gender change","Your pet " . $PetName . "'s gender was changed to " . $Gender . " by an Admin.");
$ActivityMSG = "You changed the gender of " . $Email . "'s pet name " . $PetName . ".";
AddActivity($Action,$AdminEmail,$ActivityMSG);
echo json_encode("refresh");
$PDOconn = null;
}
function UpdateBreed($Action){
$Email = ValidateSession($Action);
AdminRole($Action,$Email);
$BreedName = stripslashes($_POST["D1"]);
$OldBreedName = stripslashes($_POST["D2"]);
$BreedID = stripslashes($_POST["D3"]);
global $PDOconn;
$Query = 'CALL UpdateBreed (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $BreedName, PDO::PARAM_INT);
$Statement->bindParam(2, $BreedID, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$ActivityMSG = "Breed changed to " . $BreedName . " from " . $OldBreedName . " by an Admin.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("refresh");
$PDOconn = null;
}
function AddBreed($Action){
$Email = ValidateSession($Action);
AdminRole($Action,$Email);
$Name = stripslashes($_POST["D1"]);
global $PDOconn;
$Query = 'CALL FetchBreedNameCount (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Name, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetch(PDO::FETCH_ASSOC);
$Statement->closeCursor();
if($Response['Count'] == 0){
$Query = 'CALL AddBreed (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Name, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$ActivityMSG = "You added " . $Name . " as a new breed.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("refresh");
exit;
}else{
echo json_encode("breedexist");
exit;
}
}
function AddAttempt($Action,$UserData,$Email){
$NewAttempt = $UserData['Attempt'];
$NewAttempt++;
global $PDOconn;
$Query = 'CALL AddAttempt (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $NewAttempt, PDO::PARAM_INT, 1);
$Statement->bindParam(2, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
}
function SignIn($Action){
$Email = stripslashes($_POST["D1"]);
$Password = stripslashes($_POST["D2"]);
$UserData = FetchUser($Action,$Email);
if(!empty($UserData['Email'])){
$HashedPassword = $UserData['Password'];
$PasswordResponse = ValidatePassword($Password,$HashedPassword);
if($Email == $UserData['Email'] && $PasswordResponse == 1 && $UserData['Attempt'] < 5){
if($UserData['Disabled'] == 0) {
ResetAttempt($Action,$Email);
DeleteSession($Action,$Email);
AddSession($Action,$Email);
$ActivityMSG = "You signed in.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("refresh");
$PDOconn = null;
}else{
$ActivityMSG = "You attempted to sign in but your account wasn't activated by an admin yet.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("notactive");
$PDOconn = null;
}
}else{
if($UserData['Attempt'] < 5){
AddAttempt($Action,$UserData,$Email);
$ActivityMSG = "Account to be locked due to multipel sign in attempts.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("notlocked");
$PDOconn = null;
}else{
$ActivityMSG = "Account was locked out due to multiple sign in attempts.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("locked");
$PDOconn = null;
}
}
}else{
echo json_encode("none");
$PDOconn = null;
}
}
function UpdatePassword($Action){
$Email = ValidateSession($Action);
$OldPassword = stripslashes($_POST["D1"]);
$NewPassword = stripslashes($_POST["D2"]);
$UserData = FetchUser($Action,$Email);
$HashedPassword = $UserData['Password'];
$PasswordResponse = ValidatePassword($OldPassword,$HashedPassword);
if($Email == $UserData['Email'] && $PasswordResponse == 1){
$NewHashedPassword = HashIt($NewPassword);
global $PDOconn;
$Query = 'CALL UpdatePassword (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $NewHashedPassword, PDO::PARAM_STR, 64);
$Statement->bindParam(2, $Email, PDO::PARAM_STR, 45);
Execute($Action, $Statement);
$ActivityMSG = "Your password was changed.";
AddActivity($Action, $Email, $ActivityMSG);
mail($Email, "Password was changed", "Your password was changed.");
echo json_encode("pupdated");
$PDOconn = null;
}else{
echo json_encode("xupdated");
}
}
function ResetPassword($Action){
$Email = stripslashes($_POST["D1"]);
$UserData = FetchUser($Action,$Email);
if(!empty($UserData['Email'])){
$Password = PasswordGenerator();
$HashedPassword = HashIt($Password);
global $PDOconn;
$Query = 'CALL UpdatePassword (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $HashedPassword, PDO::PARAM_STR, 64);
$Statement->bindParam(2, $Email, PDO::PARAM_STR, 45);
Execute($Action, $Statement);
$ActivityMSG = "Your password was changed.";
AddActivity($Action, $Email, $ActivityMSG);
mail($Email, "Password was reset", "Your password was reset to " . $Password . ".");
echo json_encode("refresh");
$PDOconn = null;
}else{
echo json_encode("none");
}
}
function SignInPet($Action){
$Email = ValidateSession($Action);
$Name = stripslashes($_POST["D1"]);
$ActivityMSG = "Your pet " . $Name . " has been signed in.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("refresh");
}
function DeleteSession($Action,$Email){
global $PDOconn;
$Query = 'CALL DeleteSession (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
}
function ResetAttempt($Action,$Email){
global $PDOconn;
$Query = 'CALL ResetAttempt (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
}
function AddAccount($Action){
$Email = stripslashes($_POST["D1"]);
$UserData = FetchUser($Action,$Email);
if($Email == $UserData['Email']){
if($UserData['Attempt'] < 5){
AddAttempt($Action,$UserData,$Email);
$ActivityMSG = "Account to be locked due to multiple registration attempts.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("notlocked");
exit;
}else{
$ActivityMSG = "Account was locked out due to multiple registration attempts.";
AddActivity($Action,$Email,$ActivityMSG);
echo json_encode("locked");
exit;
}
}
$Password = stripslashes($_POST["D2"]);
$HashedPassword = HashIt($Password);
$Disabled = 1;
$Attempt = 0;
$AdminCode = 1;
global $PDOconn;
$Query = 'CALL AddAccount (?, ?, ?, ?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
$Statement->bindParam(2, $HashedPassword, PDO::PARAM_STR, 64);
$Statement->bindParam(3, $Disabled, PDO::PARAM_INT, 1);
$Statement->bindParam(4, $Attempt, PDO::PARAM_INT, 1);
$Statement->bindParam(5, $AdminCode, PDO::PARAM_INT, 1);
Execute($Action,$Statement);
$ActivityMSG = "Your account was created.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Your account was created","The following email: " . $Email . " has been created. The account will be activated by an Admin. In the meantime, familiarize yourself with the pet policy. https://petsignin.alibkaba.com/petsignin/petpolicy.pdf");
$AdminAccounts = FetchAdmins($Action);
foreach ($AdminAccounts as $AdminEmail) {
mail($AdminEmail['Email'],"New account created","The following email: " . $Email . " has been created. Account is awaiting your approval.");
}
echo json_encode("refresh");
$PDOconn = null;
}
function AddPet($Action){
$Email = ValidateSession($Action);
$Name = stripslashes($_POST["D1"]);
$BreedID = stripslashes($_POST["D2"]);
$Gender = stripslashes($_POST["D3"]);
$Disabled = 1;
global $PDOconn;
$Query = 'CALL AddPet (?, ?, ?, ?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
$Statement->bindParam(2, $Name, PDO::PARAM_STR, 45);
$Statement->bindParam(3, $BreedID, PDO::PARAM_INT);
$Statement->bindParam(4, $Gender, PDO::PARAM_STR, 4);
$Statement->bindParam(5, $Disabled, PDO::PARAM_INT, 1);
Execute($Action,$Statement);
$ActivityMSG = "Your new pet " . $Name . " has been added.";
AddActivity($Action,$Email,$ActivityMSG);
mail($Email,"Your pet was added","The following pet: " . $Name . " has been added. Please go to this link (https://petsignin.alibkaba.com/petsignin/upload.html) URL to upload the necessary documentation of your pet requested from the pet policy: Pet Policy https://petsignin.alibkaba.com/petsignin/petpolicy.pdf");
echo json_encode("refresh");
$PDOconn = null;
}
function FetchActivities($Action){
$Email = ValidateSession($Action);
global $PDOconn;
$Query = 'CALL FetchActivities (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetchAll();
echo json_encode($Response);
$PDOconn = null;
}
function FetchErrors($Action){
$Email = ValidateSession($Action);
AdminRole($Action,$Email);
global $PDOconn;
$Query = 'CALL FetchErrors';
$Statement = $PDOconn->prepare($Query);
Execute($Action,$Statement);
$Response = $Statement->fetchAll();
echo json_encode($Response);
$PDOconn = null;
}
function FetchSignInPet($Action){
$Email = ValidateSession($Action);
UserRole($Action,$Email);
global $PDOconn;
$Query = 'CALL FetchSignInPet (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetchAll();
echo json_encode($Response);
$PDOconn = null;
}
function FetchUserStatus($Action){
$Email = ValidateSession($Action);
AdminRole($Action,$Email);
$Email = stripslashes($_POST["D1"]);
global $PDOconn;
$Query = 'CALL FetchUserStatus (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetch(PDO::FETCH_ASSOC);
echo json_encode($Response);
$PDOconn = null;
}
function FetchPetStatus($Action){
$Email = ValidateSession($Action);
AdminRole($Action,$Email);
$PetID = stripslashes($_POST["D1"]);
global $PDOconn;
$Query = 'CALL FetchPetStatus (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $PetID, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetch(PDO::FETCH_ASSOC);
echo json_encode($Response);
$PDOconn = null;
}
function FetchPet($Action){
$Email = ValidateSession($Action);
AdminRole($Action,$Email);
$PetID = stripslashes($_POST["D1"]);
global $PDOconn;
$Query = 'CALL FetchPet (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $PetID, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetch(PDO::FETCH_ASSOC);
echo json_encode($Response);
$PDOconn = null;
}
function FetchUserPets($Action){
$Email = ValidateSession($Action);
AdminRole($Action,$Email);
$AccountEmail = stripslashes($_POST["D1"]);
global $PDOconn;
$Query = 'CALL FetchUserPets (?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $AccountEmail, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetchAll();
echo json_encode($Response);
$PDOconn = null;
}
function FetchPetNameCount($Action){
$Email = ValidateSession($Action);
$Name = stripslashes($_POST["D1"]);
global $PDOconn;
$Query = 'CALL FetchPetNameCount (?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $Email, PDO::PARAM_STR, 45);
$Statement->bindParam(2, $Name, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
$Response = $Statement->fetch(PDO::FETCH_ASSOC);
$Statement->closeCursor();
echo json_encode($Response);
$PDOconn = null;
}
function FetchUsers($Action){
$Email = ValidateSession($Action);
AdminRole($Action,$Email);
global $PDOconn;
$Query = 'CALL FetchUsers';
$Statement = $PDOconn->prepare($Query);
Execute($Action,$Statement);
$Response = $Statement->fetchAll();
echo json_encode($Response);
$PDOconn = null;
}
function FetchBreeds($Action){
global $PDOconn;
$Query = 'CALL FetchBreeds';
$Statement = $PDOconn->prepare($Query);
Execute($Action,$Statement);
$Response = $Statement->fetchAll();
echo json_encode($Response);
$PDOconn = null;
}
function SignOut($Action){
$Email = ValidateSession($Action);
DeleteSession($Action,$Email);
$ActivityMSG = "You signed out.";
AddActivity($Action,$Email,$ActivityMSG);
session_unset();
session_destroy();
echo json_encode("refresh");
}
function ValidateSession($Action){
StartSession();
if(isset($_SESSION['AliID'])){
$AliID = $_SESSION["AliID"];
$SessionData = FetchSession($Action,$AliID);
$Email = $SessionData['Email'];
$BrowserData = GetBrowserData();
$AccountRole = FetchAccountRole($Action,$Email);
if($SessionData['IP'] == $BrowserData['IP'] && $SessionData['Browser'] == $BrowserData['Browser'] && $SessionData['Platform'] == $BrowserData['Platform']){
if($Action == "ValidateSession"){
echo json_encode($AccountRole);
}else{
return $Email;
}
}else{
session_unset();
session_destroy();
echo json_encode("expired");
$PDOconn = null;
}
}else{
if($Action == "ValidateSession"){
echo json_encode(0);
}else{
echo json_encode("expired");
exit;
}
}
}
function FetchUserEmail($Action){
$Email = ValidateSession($Action);
echo json_encode($Email);
}
function AddSession($Action,$Email){
StartSession();
$BrowserData = GetBrowserData();
$AliID = hash('sha256', uniqid(rand(), true));
$_SESSION["AliID"] = $AliID;
$SessionIP = $BrowserData['IP'];
$SessionBrowser = $BrowserData['Browser'];
$SessionPlatform = $BrowserData['Platform'];
global $PDOconn;
$Query = 'CALL AddSession (?, ?, ?, ?, ?)';
$Statement = $PDOconn->prepare($Query);
$Statement->bindParam(1, $AliID, PDO::PARAM_STR, 64);
$Statement->bindParam(2, $Email, PDO::PARAM_STR, 45);
$Statement->bindParam(3, $SessionIP, PDO::PARAM_STR, 45);
$Statement->bindParam(4, $SessionBrowser, PDO::PARAM_STR, 45);
$Statement->bindParam(5, $SessionPlatform, PDO::PARAM_STR, 45);
Execute($Action,$Statement);
}
function StartSession(){
ini_set('session.cookie_lifetime', 1800);
ini_set('session.gc_maxlifetime', 1800);
session_start();
}