-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
797 lines (623 loc) · 19.1 KB
/
api.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
<?php
require_once 'bootstrap.php';
require './lib/slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
//add db
require_once './lib/db.php'; //@psa todo refactor with autoloader and remove singleton pattern in favor of DI to make shit testable
//initalize Session
$session = new Session;
//initialize Timezone
date_default_timezone_set('Europe/Paris');
$app = new \Slim\Slim(array(
'debug' => true //@psa todo production
));
//@psa todo figure this login stuff out
$app->get('/getSession/:sessionId', function () {
$session = new Session;
$userData = $session->checkSession();
if($userData)
{
echo json_encode($userData);
}
else
{
echo 0;
}
});
/*
* check if usercredentials exist, if so, create session and save it to db
*/
$app->post('/postLogin/', function ()
{
$app = \Slim\Slim::getInstance();
$request = $app->request();
$body = $request->getBody();
$post = (array)json_decode($body);
return login($post);
});
function login ($post)
{
try
{
$session = new Session;
$select = "Select * from user where email = :email and password = :password";
$qresult = db::query($select, $post, false, false);
if (count($qresult) >= 1)
{
$hasSession = $session->checkSession();
if (!$hasSession)
{
$userId = $qresult[0]['id'];
$session->createSession($userId);
$result = array("status" => "success", "response" => "Login Success, new Session");
}
else if ($hasSession)
{
$session->renewSession();
// die(var_dump($update)); //@psa todo here is something wrong....continue work here!!
$result = array("status" => "success", "response" => "Login Success, renew Session");
}
}
else
{
$result = array("status" => "error", "response" => "Login Failed");
}
return json_encode($result);
}
catch (Exception $e)
{
$app->halt(400, $e);
}
}
$app->post('/postImage/:email/:userType/:file/:type', function ($email, $userType, $image, $type)
{
try
{
//@psa todo: make sure, the image only gets uploaded if no image exists already (security!)
$session = new Session;
$app = \Slim\Slim::getInstance();
$request = $app->request();
$body = $request->getBody();
$post = (array)json_decode($body);
$imageType = $image . '/' . $type;
$array = array('imageType' => $imageType, 'imageData' => $body, 'email' => $email, 'userType' => $userType);
$sql = "update user set imageType = :imageType, imageData = :imageData where email = :email and type = :userType";
$data = db::query($sql, $array, false, false);
var_dump($data);
}
catch (Exception $e)
{
$app->halt(400, $e);
}
});
//check if entered Emailaddress is unique in DB type 0 is student
$app->get('/getStudentEmailUnique/:email', function ($email)
{
echo checkEmailUnique($email, 0);
});
//check if entered Emailaddress is unique in DB, type 1 is company
$app->get('/getCompanyEmailUnique/:email', function ($email)
{
echo checkEmailUnique($email, 1);
});
function checkEmailUnique ($email, $userType = 0)
{
$select = "Select email from user where email = '$email' and type = $userType";
$data = db::query($select,null, false, false);
if (count($data) == 0)
{
$trueresponse = array("response" => true);
return json_encode($trueresponse);
}
else
{
$falseresponse = array("response" => false);
return json_encode($falseresponse);
}
}
$app->get('/hello/', function () //todo refactor this api function name
{
try
{
//todo continue here with this sql
$select = "Select user.id, user.firstname, user.lastname, dataStudy.name as study, dataRegion.name as region from user left join studentStudy on user.id=studentStudy.studentId left join dataStudy on studentStudy.study=dataStudy.id left join studentJobprofile on user.id = studentJobprofile.studentId left join dataRegion on studentJobprofile.region = dataRegion.id ";
//die(var_dump($select));
$data = db::query($select,null,false);
//psa todo experimental code to refactor at some point due to performance concerns
$count = 0;
/* removed image
foreach ($data as $entry)
{
$data[$count]['image'] = base64_encode($entry['image']);
$count++;
}
*/
$data = json_encode($data);
echo $data;
}
catch (Exception $e)
{
$app = \Slim\Slim::getInstance();
$app->halt(400, $e->getMessage());
}
});
$app->get('/regions/', function ()
{
$select = "Select * from dataRegion";
$data = db::query($select,null,true, false);
echo ($data);
});
$app->get('/study/', function ()
{
$select = "Select * from dataStudy";
$data = db::query($select,null,true, false);
echo ($data);
});
$app->get('/university/', function ()
{
$select = "Select * from dataUniversities";
$data = db::query($select,null,true, false);
echo ($data);
});
$app->get('/languageDiploma/:id', function ($id)
{
$select = "Select * from dataLanguageDiploma where languageId=".$id;
$data = db::query($select,null,true, false);
echo ($data);
});
$app->get('/employmenttypes/', function ()
{
$select = "Select * from dataEmploymenttypes";
$data = db::query($select,null,true, false);
echo ($data);
});
$app->get('/workloads/', function ()
{
$select = "Select * from dataWorkloads";
$data = db::query($select,null,true, false);
echo ($data);
});
$app->get('/mobility/', function ()
{
$select = "Select * from dataMobility";
$data = db::query($select,null,true, false);
echo ($data);
});
$app->get('/languages/', function ()
{
$select = "Select * from dataLanguages";
$data = db::query($select,null,true, false);
echo ($data);
});
$app->get('/industries/', function ()
{
$select = "Select * from dataIndustry";
$data = db::query($select,null,true, false);
echo ($data);
});
$app->post('/postStudent/', function () {
$app = \Slim\Slim::getInstance();
$request = $app->request();
$body = $request->getBody();
$post = json_decode($body);
try {
addStudent($post->student);
//var_dump($post->student);
$studentId = getStudentIdByEmail($post->student->email);
addAddress($studentId, $post->address);
addUniversityAndStudy($studentId, $post->university->id, $post->study->id, $post->minor->id, $post->semester);
//todo add languages
addLanguages($studentId, $post->language);
sendConfirmationMail($post->student->email, $studentId);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
$app->post('/postCompany/', function () {
$app = \Slim\Slim::getInstance();
//$app = new \Slim\Slim();
$request = $app->request();
$body = $request->getBody();
$post = json_decode($body);
try
{
addCompany($post->company);
$userId = getCompanyIdByEmail($post->company->email);
addAddress($userId, $post->address);
sendConfirmationMail($post->company->email, $userId);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
/*
* creates hash value for confirmation by user
*/
function sendConfirmationMail($email, $studentId)
{
$hash = md5($email.$studentId);
$array = array('email' => $email, 'studentId' => $studentId, 'hash' => $hash);
//var_dump($array);
$sql = "
insert into studentConfirmation (email, studentId, hashValue)
VALUES (:email, :studentId, :hash)
";
try
{
$response = db::query($sql, $array, false, false);
$confirmationUrl = $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"]."/api.php/confirmRegistration/$hash";
$text = "Hallo, bitte bestätige Deine Registrierung bei worq, indem Du folgendem Link folgst: <a href=\"$confirmationUrl\">$confirmationUrl</a>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster@worq.com>' . "\r\n";
mail('philou.savary@gmail.com', 'Bitte Registrierung bei Worq bestätigen', $text, $headers);
}
catch (Exception $e)
{
throw new Exception ($e);
}
}
/*
* Link the User follows to complete Registration
*
*/
$app->get('/confirmRegistration/:hash', function ($hash)
{
try
{
$select = "Select * from studentConfirmation where hashValue = '$hash'";
$data = db::query($select,null,false, false);
if (count($data) > 0)
{
$array = array('studentId' => (int)$data[0]['studentId']);
$update = "Update user set isConfirmed = 1, isActive = 1 where Id = :studentId";
$data = db::query($update,$array,false, false);
$selectUser = "Select email, password from user where id = :studentId";
$userCredentials = db::query($selectUser,$array,false, false);
$userCredentials = $userCredentials[0];
login($userCredentials);
$selectUser = "Select type from user where id = :studentId";
$userType = db::query($selectUser,$array,false, false);
$userType = (int) $userType[0]['type'];
$app = \Slim\Slim::getInstance();
if ($userType == 1)
{
$app->redirect('/#/');
}
else
{
$app->redirect('/#/jobprofile');
}
}
}
catch (Exception $e)
{
throw new Exception ($e);
}
});
//@psa refactor this into separate class
function addUniversityAndStudy ($studentId, $university, $study, $minor, $semester)
{
try
{
$array = array ('studentId' => $studentId, 'university' => $university, 'study' => $study, 'minor' => $minor, 'semester' => $semester);
$sql = "
insert into studentStudy (studentId, university, study, minor, semester)
VALUES (:studentId, :university, :study, :minor, :semester)
";
$response = db::query($sql, $array, false, false);
}
catch (Exception $e)
{
throw new Exception ($e);
}
}
function addLanguages($studentId, $languagesArray)
{
try{
foreach($languagesArray as $row)
{
//not really pretty.....
if(is_null($row->language) || is_null($row->languageDiploma) || $row->languageOral->id == "")
{
continue;
}
$idArray = array();
$idArray['studentId'] = $studentId;
/*
$idArray['language'] = $row['language']['id'];
$idArray['languageDiploma'] = $row['languageDiploma']['id'];
$idArray['languageOral'] = $row['languageOral']['id'];
*/
$idArray['language'] = (int)$row->language->id;
$idArray['languageDiploma'] = (int)$row->languageDiploma->id;
$idArray['languageOral'] = (int)$row->languageOral->id;
//var_dump($idArray);
$sql =
"
insert into studentLanguages (studentId, languageId, languageDiplomaId, languageOralId)
VALUES (:studentId, :language, :languageDiploma, :languageOral)
";
$response = db::query($sql, $idArray, false, false);
}
}
catch (Exception $e)
{
throw new Exception ($e);
}
}
function addStudent($student)
{
$sql = "
insert into user (firstname, lastname, gender, email, password, telephone, type)
VALUES (:firstname, :lastname, :gender, :email, :password, :telephone, :type)
"; //todo add birthdate
$studentArray = (array) $student;
$studentArray['type'] = 0;
try
{
$response = db::query($sql, $studentArray, false, false);
}
catch (Exception $e)
{
throw new Exception ($e);
}
}
function addCompany($company)
{
$sql = "
insert into user (company, firstname, lastname, gender, email, password, telephone, type)
VALUES (:company, :firstname, :lastname, :gender, :email, :password, :telephone, :type)
";
$companyArray = (array) $company;
$companyArray['type'] = 1;
try
{
$response = db::query($sql, $companyArray, false, false);
}
catch (Exception $e)
{
throw new Exception ($e);
}
}
function addAddress($userId, $address)
{ $address = (array)$address;
$address['userId'] = $userId;
$sql = "
insert into address (userId, street, streetno, zip, city)
VALUES (:userId, :street, :streetno, :zip, :city)";
try
{
$response = db::query($sql, $address, false, false);
//var_dump(($response)); //@psa todo make sure only one email is assigned. Maybe throw error or something
}
catch (Exception $e)
{
throw new Exception ($e);
}
}
function getStudentIdbyEmail($email)
{
return getUserIdByEmailAndType($email, 0);
}
function getCompanyIdbyEmail($email)
{
return getUserIdByEmailAndType($email, 1);
}
function getUserIdByEmailAndType ($email, $type)
{
$sql = "
select id from user where email='$email' and type = $type";
try
{
$response = db::query($sql, null, false, false);
// var_dump(($response[0]['id'])); //@psa todo make sure only one email is assigned. Maybe throw error or something
return $response[0]['id'];
}
catch (Exception $e)
{
throw new Exception ($e);
}
}
$app->post('/postJobprofile/', function () {
try
{
$app = \Slim\Slim::getInstance();
$session = new Session;
//$app = new \Slim\Slim();
$request = $app->request();
$body = $request->getBody();
$post = (array)json_decode($body);
//$post = json_decode(json_encode($post));
$availability = (array)$post['availability'];
//@psa todo check session and insert userId into studentJobprofile.....
$userData = $session->checkSession();
if (!$userData)
{
throw new Exception ('Bitte loggen Sie sich ein um Ihr Jobprofil speichern zu können!');
}
$userId = $userData['id'];
//die(var_dump($userData));
//todo continue here
addAvailability($availability,$userId);
unset($post['availability']);
$post['studentId'] = $userId;
$sql = "
insert into studentJobprofile (studentId, employmentType, workloadFrom, workloadTo, availableFrom, availableTo, commission, mobility, industry, promotion, region)
VALUES (:studentId, :employmentType, :workloadFrom, :workloadTo, :availableFrom, :availableTo, :commission, :mobility, :industry, :promotion, :region)";
$response = db::query($sql, $post, false, true);
// die(var_dump($response));
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
echo json_encode($response);
});
$app->post('/postContact/', function () {
try {
$app = \Slim\Slim::getInstance();
//$app = new \Slim\Slim();
$request = $app->request();
$body = $request->getBody();
$post = json_decode($body);
//$post = json_decode(json_encode($post));
$message = new Message;
$message->saveContact($post);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
$app->post('/postMessageTemplate/', function () {
try
{
$app = \Slim\Slim::getInstance();
//$app = new \Slim\Slim();
$request = $app->request();
$body = $request->getBody();
$post = (array)json_decode($body);
//$post = json_decode(json_encode($post));
$message = new Message;
if (array_key_exists('id', $post))
{
$messageId = $post['id'];
$messageId = $message->save($post, $messageId);
}
else
{
$messageId = $message->save($post);
}
echo trim($messageId);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
$app->post('/sendMessage/', function () {
try
{
$app = \Slim\Slim::getInstance();
//$app = new \Slim\Slim();
$request = $app->request();
$body = $request->getBody();
$post = (array)json_decode($body);
//$post = json_decode(json_encode($post));
$message = new Message;
$message->sendMessage($post);
//echo trim($messageId);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
$app->get('/getStudentJobprofile/:studentId', function($studentId)
{
try
{
$app = \Slim\Slim::getInstance();
$message = new Student;
$response = $message->getJobprofile($studentId);
echo json_encode($response);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
$app->get('/getDraftList/', function()
{
try
{
$app = \Slim\Slim::getInstance();
$message = new Message;
$response = $message->getDraftList();
echo json_encode($response);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
$app->get('/getOpenList/', function()
{
try
{
$app = \Slim\Slim::getInstance();
$message = new Message;
$response = $message->getOpenList();
echo json_encode($response);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
$app->get('/getMessage/:messageId', function ($messageId)
{
try
{
$app = \Slim\Slim::getInstance();
$message = new Message;
$response = $message->get($messageId);
echo json_encode($response);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
$app->get('/getUserContacts/', function ()
{
try
{
$app = \Slim\Slim::getInstance();
$message = new Message;
$response = $message->getUserContacts();
echo json_encode($response);
}
catch (Exception $e)
{
$app->halt(400, $e->getMessage());
}
});
/**
* @param $availability
* @param $userId
* @throws Exception
*/
function addAvailability ($availability, $userId)
{
//@psa todo save that stuff
foreach ($availability as $day => $row)
{
$sql = "insert into studentAvailability (studentId, day) values ($userId, '$day')";
$response = db::query($sql, null, false, true);
foreach ($row as $daytime => $isTrue)
{
// $sqlInsert['day']
if($isTrue)
{
$sqlUpdate = "update studentAvailability set $daytime = true where studentId = $userId and day = '$day' ";
$response = db::query($sqlUpdate, null, false, true);
var_dump($sqlUpdate);
}
}
}
}
/*
* @param: contains the result of the request
*/
function validate($sqlResult, $errorMessage = null)
{
$app->halt(400, "do tell the user why it is not working");
}
$app->run();
?>