-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseFunctions.php
1241 lines (1141 loc) · 50.1 KB
/
DatabaseFunctions.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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Created by IntelliJ IDEA.
* User: Elias
* Date: 2017-04-12
* Time: 15:44
*/
spl_autoload_register(function ($class_name) {
/** @noinspection PhpIncludeInspection */
include $class_name . '.php';
});
class DatabaseFunctions
{
/**
* @var PDO
*/
private $dbc;
function __construct()
{
$dsn = 'mysql:host=localhost;dbname=confdroid_test';
$username = 'confdroid_test';
$password = 'tutus';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$this->dbc = new PDO($dsn, $username, $password, $options);
}
/**
* Login function for admin from the webbInterface
* @param $username
* @param $password
* @return mixed
*/
public function login($username, $password)
{
$stmt = $this->dbc->prepare("SELECT id FROM admin WHERE username=:username AND password=:password");
$salt = $this->dbc->prepare("SELECT salt FROM admin WHERE username=:username");
$salt->bindParam(":username", $username);
$salt->execute();
$salt = $salt->fetch()["salt"];
$passwordHash = hash("sha512", $password + $salt);
$stmt->bindParam(":username", $username);
$stmt->bindParam(":password", $passwordHash);
$stmt->execute();
$user = $stmt->fetchAll(PDO::FETCH_ASSOC);
$adminSession["id"] = null;
$adminSession["Token"] = null;
if (!isset($user[0]["id"])) {
return null;
} else {
$adminSession["id"] = $user[0]["id"];
$token = bin2hex(openssl_random_pseudo_bytes(16)); //Creates random hex Token
$insertAuth = $this->dbc->prepare("UPDATE admin SET authToken=:authToken, logintime=NOW(), ipaddr=:ipaddr WHERE username=:username AND password=:password ");
$insertAuth->bindParam(":authToken", $token);
$insertAuth->bindParam(":username", $username);
$insertAuth->bindParam(":password", $password);
$ip = $this->getRealIpAddr();
$insertAuth->bindParam(":ipaddr", $ip);
$insertAuth->execute();
$adminSession["Token"] = $token;
return $adminSession;
}
}
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* Authorize the admin with the session variables created on login.
* @param $authToken
* @param $adminId
* @return bool
*/
public function authorizeAdmin($authToken, $adminId)
{
$stmt = $this->dbc->prepare("SELECT * FROM admin WHERE id=:adminID AND authToken=:authToken AND TIME_TO_SEC(TIMEDIFF(NOW(),logintime)) < 600;");
$stmt->bindParam(":adminID", $adminId);
$stmt->bindParam(":authToken", $authToken);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (sizeof($result) == 1) {
$stmt = $this->dbc->prepare("UPDATE admin SET logintime=NOW() WHERE id=:adminID AND authToken=:authToken;");
$stmt->bindParam(":adminID", $adminId);
$stmt->bindParam(":authToken", $authToken);
$stmt->execute();
return true;
}
return false;
}
public function logout($authToken, $adminId)
{
$stmt = $this->dbc->prepare("UPDATE admin SET authToken=NULL WHERE id=:adminID AND authToken=:authToken;");
$stmt->bindParam(":adminID", $adminId);
$stmt->bindParam(":authToken", $authToken);
$stmt->execute();
}
/**
* Search on user
* @param string $name
* @return Mixed
*/
public function searchUsers($name = "")
{
$stmt = $this->dbc->prepare("SELECT * FROM user WHERE name LIKE :name"); //OR mail LIKE :mail
$name = "%" . $name . "%";
$stmt->bindParam(":name", $name);
$stmt->execute();
$queriedUsers = $stmt->fetchAll(PDO::FETCH_ASSOC);
$i = 0;
$users = array(User::class);
if (isset($queriedUsers[0])) {
foreach ($queriedUsers as $user) {
$users[$i] = new User($user["id"], $user["name"], $user["mail"], $user["auth_token"], $user["date_created"]);
$users[$i]->addDevices($this->getDevices($user["id"]));
$users[$i]->addGroups($this->getGroupsByUserId($user["id"]));
$users[$i] = $users[$i]->getObject();
$i++;
}
return $users;
}
return null;
}
/**
* Returns array with devices
* @param $userId
* @return Device[]
*/
private function getDevices($userId)
{
$stmt = $this->dbc->prepare("SELECT id, name, imei, date_created FROM device, user_device WHERE device.id = user_device.device_id AND user_device.user_id=:userId");
$stmt->bindParam(":userId", $userId);
$stmt->execute();
$stmtAnswer = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (isset($stmtAnswer[0])) {
$devices = null;
for ($i = 0; $i < count($stmtAnswer); $i++) {
$devices[$i] = new Device($stmtAnswer[$i]["id"], $stmtAnswer[$i]["name"], $stmtAnswer[$i]["imei"], $stmtAnswer[$i]["date_created"]);
$devices[$i] = $this->getApplications($devices[$i]);
}
return $devices;
}
return null;
}
/**
* Returns all the applications on a device.
* @param $device
* @param $user
* @return Device
*/
private function getApplications(Device $device = null, User $user = null)
{
if ($user == null && $device != null) {
$stmt = $this->dbc->prepare("SELECT application.* FROM application, application_device WHERE application.id = application_device.application_id AND application_device.device_id =:deviceID");
$deviceId = $device->getId();
$stmt->bindParam(":deviceID", $deviceId);
$addTo = $device;
} else if ($user != null && $device != null) {
$stmt = $this->dbc->prepare("SELECT DISTINCT application.* FROM `application`, device, application_device, user, user_device WHERE `application`.id=application_device.application_id AND application_device.device_id=device.id AND device.id=:deviceID AND user.id = user_device.user_id AND application_device.device_id = user_device.device_id AND user.id=:userID UNION DISTINCT SELECT DISTINCT application.* FROM `application`, `user`, application_user WHERE `application`.id=application_user.application_id AND application_user.user_id=`user`.id AND user.id=:userID UNION DISTINCT SELECT DISTINCT application.* FROM `application`, `group`, application_group, user, user_group WHERE `application`.id=application_group.application_id AND application_group.group_id=`group`.id AND user.id = user_group.user_id AND application_group.group_id = user_group.group_id AND user.id=:userID");
$stmt->bindParam(":userID", $user->getId());
$deviceId = $device->getId();
$stmt->bindParam(":deviceID", $deviceId);
$addTo = $device;
} else if ($device == null && $user != null) {
$stmt = $this->dbc->prepare("SELECT application.* FROM application, application_user WHERE application.id = application_user.application_id AND application_user.user_id =:userID");
$stmt->bindParam(":userID", $user->getId());
$addTo = $user;
} else {
return $device;
}
$stmt->execute();
$applications = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sqlSettingStmt = $this->dbc->prepare("SELECT sql_setting.* FROM application, sql_setting, application_sql_setting WHERE application.id = application_sql_setting.application_id AND sql_setting.id = application_sql_setting.sql_setting_id AND application.id=:appID");
$xmlSettingStmt = $this->dbc->prepare("SELECT xml_setting.* FROM application, xml_setting, application_xml_setting WHERE application.id = application_xml_setting.application_id AND xml_setting.id = application_xml_setting.xml_setting_id AND application.id=:appID");
foreach ($applications as $application) {
$sqlSettingStmt->bindParam(":appID", $application["id"]);
$sqlSettingStmt->execute();
$sqlSettings = $sqlSettingStmt->fetchAll(PDO::FETCH_ASSOC);
$xmlSettingStmt->bindParam(":appID", $application["id"]);
$xmlSettingStmt->execute();
$xmlSettings = $xmlSettingStmt->fetchAll(PDO::FETCH_ASSOC);
$app = new Application($application["id"], $application["data_dir"], $application["apk_name"], $application["apk_url"], $application["friendly_name"], $application["force_install"], $application["package_name"]);
foreach ($sqlSettings as $sqlSetting) {
$sqlSetting = new SqlSetting($sqlSetting["id"], $sqlSetting["sql_location"], $sqlSetting["sql_setting"], $sqlSetting["friendly_name"]);
if ($user != null) {
$this->evaluateSqlSettingVariables($sqlSetting, $user);
}
$app->addSQL_setting($sqlSetting);
}
foreach ($xmlSettings as $xmlSetting) {
$xmlSetting = new XmlSetting($xmlSetting["id"], $xmlSetting["file_location"], $xmlSetting["regularexp"], $xmlSetting["replacewith"], $xmlSetting["friendly_name"]);
if ($user != null) {
$this->evaluateXmlSettingVariables($xmlSetting, $user);
}
$app->addXML_setting($xmlSetting);
}
$addTo->addApplication($app);
}
return $addTo;
}
/**
* @param $userId
* @return Group[]
*/
private function getGroupsByUserId($userId)
{
$stmt = $this->dbc->prepare("SELECT id, prio, name FROM `group`, user_group WHERE `group`.`id` = user_group.group_id AND user_group.user_id=:userId");
$stmt->bindParam(":userId", $userId);
$stmt->execute();
$stmtAnswer = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (isset($stmtAnswer[0])) {
$groups = array(Group::class);
for ($i = 0; $i < count($stmtAnswer); $i++) {
$groups[$i] = new Group($stmtAnswer[$i]["id"], $stmtAnswer[$i]["prio"], $stmtAnswer[$i]["name"]);
}
return $groups;
}
return null;
}
public function getUserWithAuthtoken($authToken, $imei)
{
$stmt = $this->dbc->prepare("SELECT * FROM user WHERE BINARY auth_token=:authToken");
$stmt->bindParam(":authToken", $authToken);
$stmt->execute();
$queriedUsers = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (isset($queriedUsers[0])) {
$user = new User($queriedUsers[0]["id"], $queriedUsers[0]["name"], $queriedUsers[0]["mail"], $queriedUsers[0]["auth_token"], $queriedUsers[0]["date_created"]);
$device = $this->getDevice($imei, $user, true);
if ($device) $user->addDevice($device); else http_response_code(400);
$user->addGroups($this->getGroupsByUserId($user->getId()));
$user = $this->getApplications(null, $user);
return $user;
}
return null;
}
public function getDevice($identifier, User $user = null, $isImei = false)
{
if ($isImei) {
$stmt = $this->dbc->prepare("SELECT id FROM device WHERE imei=:imei");
$stmt->bindParam(":imei", $identifier);
$stmt->execute();
$id = $stmt->fetchAll(PDO::FETCH_ASSOC)[0]["id"];
} else {
$id = $identifier;
}
if ($user == null) {
$stmt = $this->dbc->prepare("SELECT `device`.* FROM device WHERE device.id=:id");
$stmt->bindParam(":id", $id);
$userStmt = $this->dbc->prepare("SELECT `user`.* FROM device, user, user_device WHERE user_device.device_id=:id AND user_device.device_id=device.id AND user_device.user_id=user.id");
$userStmt->bindParam(":id", $id);
$userStmt->execute();
$userStmtResult = $userStmt->fetchAll(PDO::FETCH_ASSOC);
if (isset($userStmtResult[0])) $deviceOwner = new User($userStmtResult[0]["id"], $userStmtResult[0]["name"], $userStmtResult[0]["mail"], $userStmtResult[0]["auth_token"], $userStmtResult[0]["date_created"]);
} else {
$stmt = $this->dbc->prepare("SELECT device.* FROM device, user_device WHERE device.id=:id AND user_device.device_id=device.id AND user_device.user_id=:userID");
$stmt->bindParam(":id", $id);
$stmt->bindParam(":userID", $user->getID());
}
$stmt->execute();
$stmtAnswer = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (isset($stmtAnswer[0])) {
$device = new Device($stmtAnswer[0]["id"], $stmtAnswer[0]["name"], $stmtAnswer[0]["imei"], $stmtAnswer[0]["date_created"]);
$device = $this->getApplications($device, $user);
if (isset($deviceOwner)) $device->setUser($deviceOwner->getObject());
return $device;
}
return null;
}
public function getUserWithID($id)
{
$stmt = $this->dbc->prepare("SELECT * FROM user WHERE id=:id");
$stmt->bindParam(":id", $id);
$stmt->execute();
$queriedUsers = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (isset($queriedUsers[0])) {
$user = new User($queriedUsers[0]["id"], $queriedUsers[0]["name"], $queriedUsers[0]["mail"], $queriedUsers[0]["auth_token"], $queriedUsers[0]["date_created"]);
$user->addDevices($this->getDevices($user->getId()));
$user->addGroups($this->getGroupsByUserId($user->getId()));
$user = $this->getApplications(null, $user);
return $user;
}
return null;
}
public function createDevice($json)
{
$device = json_decode($json, true);
if (!isset($device["name"]) || !isset($device["imei"])) {
http_response_code(400);
return false;
}
if ($device["name"] == "" || $device["imei"] == "") {
http_response_code(400);
return false;
}
$stmt = $this->dbc->prepare("INSERT INTO device(name, imei) VALUES(:name, :imei)");
$stmt->bindParam(":name", $device["name"]);
$stmt->bindParam(":imei", $device["imei"]);
$stmt->execute();
if ($stmt->errorCode() == 00000) {
return true;
}
return false;
}
public function updateDevice($deviceID, $json)
{
$device = json_decode($json, true);
$query = "";
if (isset($device["imei"])) {
$query .= "imei=:imei ";
}
if (isset($device["imei"]) && isset($device["name"])) {
$query .= ", ";
}
if (isset($device["name"])) {
$query .= "name=:name ";
}
$check = $this->dbc->prepare("SELECT * FROM device WHERE id=:deviceID");
$check->bindParam(":deviceID", $deviceID);
$check->execute();
if ($check->rowCount() < 1) {
echo "No device with that id!";
http_response_code(404);
return true;
}
$stmt = $this->dbc->prepare("UPDATE device SET $query WHERE id=:deviceID");
$stmt->bindParam(":deviceID", $deviceID);
if (isset($device["imei"])) {
$stmt->bindParam(":imei", $device["imei"]);
}
if (isset($device["name"])) {
$stmt->bindParam(":name", $device["name"]);
}
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function addDeviceToUser($userID, $deviceID)
{
$stmt = $this->dbc->prepare("INSERT INTO user_device(user_id, device_id) VALUES(:userID, :deviceID)");
$stmt->bindParam(":userID", $userID);
$stmt->bindParam(":deviceID", $deviceID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function removeDeviceFromUser($userID, $deviceID)
{
$stmt = $this->dbc->prepare("DELETE FROM user_device WHERE device_id=:deviceID AND user_id=:userID");
$stmt->bindParam(":userID", $userID);
$stmt->bindParam(":deviceID", $deviceID);
$stmt->execute();
return true;
}
public function searchDevices($searchValue)
{
$stmt = $this->dbc->prepare("SELECT id, name, imei, date_created FROM device WHERE device.name LIKE :searchValue OR device.imei LIKE :searchValue");
$searchValue = "%" . $searchValue . "%";
$stmt->bindParam(":searchValue", $searchValue);
$stmt->execute();
$stmtAnswer = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (isset($stmtAnswer[0])) {
$devices = array(Device::class);
for ($i = 0; $i < count($stmtAnswer); $i++) {
$devices[$i] = new Device($stmtAnswer[$i]["id"], $stmtAnswer[$i]["name"], $stmtAnswer[$i]["imei"], $stmtAnswer[$i]["date_created"]);
$devices[$i] = $this->getApplications($devices[$i]);
$devices[$i] = $devices[$i]->getObject();
}
return $devices;
}
return null;
}
public function addApplicationToUser($userID, $applicationID)
{
$stmt = $this->dbc->prepare("INSERT INTO application_user(user_id, application_id) VALUES(:userID, :applicationID)");
$stmt->bindParam(":userID", $userID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function addApplicationToGroup($groupID, $applicationID)
{
$stmt = $this->dbc->prepare("INSERT INTO application_group(group_id, application_id) VALUES(:groupID, :applicationID)");
$stmt->bindParam(":groupID", $groupID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function addApplicationToDevice($deviceID, $applicationID)
{
$stmt = $this->dbc->prepare("INSERT INTO application_device(device_id, application_id) VALUES(:deviceID, :applicationID)");
$stmt->bindParam(":deviceID", $deviceID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function addApplicationToSqlSetting($sqlSettingID, $applicationID)
{
$stmt = $this->dbc->prepare("INSERT INTO application_sql_setting(sql_setting_id, application_id) VALUES(:sqlSettingID, :applicationID)");
$stmt->bindParam(":sqlSettingID", $sqlSettingID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function addApplicationToXmlSetting($xmlSettingID, $applicationID)
{
$stmt = $this->dbc->prepare("INSERT INTO application_xml_setting(xml_setting_id, application_id) VALUES(:xmlSettingID, :applicationID)");
$stmt->bindParam(":xmlSettingID", $xmlSettingID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function removeApplicationFromUser($userID, $applicationID)
{
$stmt = $this->dbc->prepare("DELETE FROM application_user WHERE application_id=:applicationID AND user_id=:userID");
$stmt->bindParam(":userID", $userID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
return true;
}
public function removeApplicationFromGroup($groupID, $applicationID)
{
$stmt = $this->dbc->prepare("DELETE FROM application_group WHERE group_id=:groupID AND application_id=:applicationID");
$stmt->bindParam(":groupID", $groupID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function removeApplicationFromDevice($deviceID, $applicationID)
{
$stmt = $this->dbc->prepare("DELETE FROM application_device WHERE device_id=:deviceID AND application_id=:applicationID");
$stmt->bindParam(":deviceID", $deviceID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function removeApplicationFromSqlSetting($sqlSettingID, $applicationID)
{
$stmt = $this->dbc->prepare("DELETE FROM application_sql_setting WHERE sql_setting_id=:sqlSettingID AND application_id=:applicationID");
$stmt->bindParam(":sqlSettingID", $sqlSettingID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function removeApplicationFromXmlSetting($xmlSettingID, $applicationID)
{
$stmt = $this->dbc->prepare("DELETE FROM application_xml_setting WHERE xml_setting_id=:xmlSettingID AND application_id=:applicationID");
$stmt->bindParam(":xmlSettingID", $xmlSettingID);
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function createApplication($json)
{
$application = json_decode($json, true);
if (!isset($application["name"]) || !isset($application["packageName"])) {
http_response_code(400);
return false;
}
if ($application["name"] == "" || $application["packageName"] == "") {
http_response_code(400);
return false;
}
if (!isset($application["apkName"])) $application["apkName"] = "";
if (!isset($application["forceInstall"])) $application["forceInstall"] = "";
if (!isset($application["dataDir"])) $application["dataDir"] = "";
if (!isset($application["apkURL"])) $application["apkURL"] = "";
$stmt = $this->dbc->prepare("INSERT INTO application(apk_name, apk_url, force_install, package_name, data_dir, friendly_name) VALUES(:apk_name, :apk_url, :force_install, :package_name, :data_dir, :friendly_name)");
$stmt->bindParam(":apk_name", $application["apkName"]);
$stmt->bindParam(":apk_url", $application["apkURL"]);
$stmt->bindParam(":force_install", $application["forceInstall"]);
$stmt->bindParam(":package_name", $application["packageName"]);
$stmt->bindParam(":data_dir", $application["dataDir"]);
$stmt->bindParam(":friendly_name", $application["name"]);
$stmt->execute();
if ($stmt->errorCode() == 00000) {
return true;
} else {
return false;
}
}
public function searchApplications($searchValue = null, $appID = null)
{
if (!$appID) {
$stmt = $this->dbc->prepare("SELECT * FROM application WHERE friendly_name LIKE :searchValue OR package_name LIKE :searchValue OR apk_name LIKE :searchValue");
$searchValue = "%" . $searchValue . "%";
$stmt->bindParam(":searchValue", $searchValue);
} else {
$stmt = $this->dbc->prepare("SELECT * FROM application WHERE id=:appID");
$stmt->bindParam(":appID", $appID);
}
$stmt->execute();
$applications = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sqlSettingStmt = $this->dbc->prepare("SELECT sql_setting.* FROM application, sql_setting, application_sql_setting WHERE application.id = application_sql_setting.application_id AND sql_setting.id = application_sql_setting.sql_setting_id AND application.id=:appID");
$xmlSettingStmt = $this->dbc->prepare("SELECT xml_setting.* FROM application, xml_setting, application_xml_setting WHERE application.id = application_xml_setting.application_id AND xml_setting.id = application_xml_setting.xml_setting_id AND application.id=:appID");
$usersStmt = $this->dbc->prepare("SELECT user.* FROM user, application_user WHERE application_user.user_id=user.id AND application_user.application_id=:appID");
$groupsStmt = $this->dbc->prepare("SELECT `group`.* FROM `group`, application_group WHERE application_group.group_id=`group`.id AND application_group.application_id=:appID");
$devicesStmt = $this->dbc->prepare("SELECT device.* FROM device, application_device WHERE application_device.device_id=device.id AND application_device.application_id=:appID");
$appArray = array();
foreach ($applications as $application) {
$usersStmt->bindParam(":appID", $application["id"]);
$usersStmt->execute();
$groupsStmt->bindParam(":appID", $application["id"]);
$groupsStmt->execute();
$devicesStmt->bindParam(":appID", $application["id"]);
$devicesStmt->execute();
$sqlSettingStmt->bindParam(":appID", $application["id"]);
$sqlSettingStmt->execute();
$sqlSettings = $sqlSettingStmt->fetchAll(PDO::FETCH_ASSOC);
$xmlSettingStmt->bindParam(":appID", $application["id"]);
$xmlSettingStmt->execute();
$xmlSettings = $xmlSettingStmt->fetchAll(PDO::FETCH_ASSOC);
$app = new Application($application["id"], $application["data_dir"], $application["apk_name"], $application["apk_url"], $application["friendly_name"], $application["force_install"], $application["package_name"]);
foreach ($sqlSettings as $sqlSetting) {
$app->addSQL_setting(new SqlSetting($sqlSetting["id"], $sqlSetting["sql_location"], $sqlSetting["sql_setting"], $sqlSetting["friendly_name"]));
}
foreach ($xmlSettings as $xmlSetting) {
$app->addXML_setting(new XmlSetting($xmlSetting["id"], $xmlSetting["file_location"], $xmlSetting["regularexp"], $xmlSetting["replacewith"], $xmlSetting["friendly_name"]));
}
$app->setUsers($usersStmt->fetchAll(PDO::FETCH_ASSOC));
$app->setGroups($groupsStmt->fetchAll(PDO::FETCH_ASSOC));
$app->setDevices($devicesStmt->fetchAll(PDO::FETCH_ASSOC));
array_push($appArray, $app->getObject());
}
return $appArray;
}
public function searchSqlSettings($searchValue = null, $sqlSettingID = null)
{
if (!$sqlSettingID) {
$stmt = $this->dbc->prepare("SELECT * FROM sql_setting WHERE friendly_name LIKE :searchValue");
$searchValue = "%" . $searchValue . "%";
$stmt->bindParam(":searchValue", $searchValue);
} else {
$stmt = $this->dbc->prepare("SELECT * FROM sql_setting WHERE id=:sqlSettingID");
$stmt->bindParam(":sqlSettingID", $sqlSettingID);
}
$stmt->execute();
$sqlSettingArray = array();
$sqlSettings = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($sqlSettings as $sqlSetting) {
array_push($sqlSettingArray, (new SqlSetting($sqlSetting["id"], $sqlSetting["sql_location"], $sqlSetting["sql_setting"], $sqlSetting["friendly_name"]))->getObject());
}
return $sqlSettingArray;
}
public function searchXmlSettings($searchValue = null, $XmlSettingID = null)
{
if (!$XmlSettingID) {
$stmt = $this->dbc->prepare("SELECT * FROM xml_setting WHERE friendly_name LIKE :searchValue");
$searchValue = "%" . $searchValue . "%";
$stmt->bindParam(":searchValue", $searchValue);
} else {
$stmt = $this->dbc->prepare("SELECT * FROM xml_setting WHERE id=:XmlSettingID");
$stmt->bindParam(":XmlSettingID", $XmlSettingID);
}
$stmt->execute();
$xmlSettingArray = array();
$XmlSettings = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($XmlSettings as $xmlSetting) {
array_push($xmlSettingArray, (new XmlSetting($xmlSetting["id"], $xmlSetting["file_location"], $xmlSetting["regularexp"], $xmlSetting["replacewith"], $xmlSetting["friendly_name"]))->getObject());
}
return $xmlSettingArray;
}
public function removeGroupFromUser($userID, $groupID)
{
$stmt = $this->dbc->prepare("DELETE FROM user_group WHERE group_id=:groupID AND user_id=:userID");
$stmt->bindParam(":userID", $userID);
$stmt->bindParam(":groupID", $groupID);
$stmt->execute();
return true;
}
public function addGroupToUser($userID, $groupID)
{
$stmt = $this->dbc->prepare("INSERT INTO user_group(user_id, group_id) VALUES(:userID, :groupID)");
$stmt->bindParam(":userID", $userID);
$stmt->bindParam(":groupID", $groupID);
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function createGroup($json)
{
$group = json_decode($json, true);
if (!isset($group["prio"])) $group["prio"] = 50;
if (!isset($group["name"])) {
http_response_code(400);
return false;
}
if ($group["name"] == "") {
http_response_code(400);
return false;
}
$stmt = $this->dbc->prepare("INSERT INTO `group`(prio, name) VALUES(:prio, :name)");
$stmt->bindParam(":prio", $group["prio"]);
$stmt->bindParam(":name", $group["name"]);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
public function addUser($json)
{
$user = json_decode($json, true);
if (isset($user["email"]) && isset($user["name"])) {
if ($user["email"] != "" && $user["name"] != "") {
$token = bin2hex(openssl_random_pseudo_bytes(16)); //Creates random hex Token
$stmt = $this->dbc->prepare("INSERT INTO user(name, mail, auth_token) VALUES(:name, :mail, :authToken)");
$stmt->bindParam(":name", $user["name"]);
$stmt->bindParam(":mail", $user["email"]);
$stmt->bindParam(":authToken", $token);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
}
http_response_code(400);
return false;
}
public function deleteUser($id)
{
$stmt = $this->dbc->prepare("DELETE FROM user WHERE id=:id;");
$stmt->bindParam(":id", $id);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
public function deleteDevice($deviceID)
{
$stmt = $this->dbc->prepare("DELETE FROM `device` WHERE id=:deviceID");
$stmt->bindParam(":deviceID", $deviceID);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
public function deleteApplication($applicationID)
{
$stmt = $this->dbc->prepare("DELETE FROM `application` WHERE id=:applicationID");
$stmt->bindParam(":applicationID", $applicationID);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
public function deleteSqlSetting($sqlSettingID)
{
$stmt = $this->dbc->prepare("DELETE FROM `sql_setting` WHERE id=:sqlSettingID");
$stmt->bindParam(":sqlSettingID", $sqlSettingID);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
public function deleteXmlSetting($xmlSettingID)
{
$stmt = $this->dbc->prepare("DELETE FROM `xml_setting` WHERE id=:xmlSettingID");
$stmt->bindParam(":xmlSettingID", $xmlSettingID);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
public function createSqlSetting($json)
{
$sqlSetting = json_decode($json, true);
if (!isset($sqlSetting["query"]) || !isset($sqlSetting["dblocation"]) || !isset($sqlSetting["name"])) {
http_response_code(400);
return false;
}
if ($sqlSetting["name"] == "" || $sqlSetting["dblocation"] == "" || $sqlSetting["name"] == null) {
http_response_code(400);
return false;
}
$stmt = $this->dbc->prepare("INSERT INTO `sql_setting`(sql_setting, sql_location, friendly_name) VALUES(:sqlquery, :dblocation, :name)");
$stmt->bindParam(":sqlquery", $sqlSetting["query"]);
$stmt->bindParam(":dblocation", $sqlSetting["dblocation"]);
$stmt->bindParam(":name", $sqlSetting["name"]);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
public function createXmlSetting($json)
{
$xmlSetting = json_decode($json, true);
if (!isset($xmlSetting["fileLocation"]) || !isset($xmlSetting["regexp"]) || !isset($xmlSetting["replaceWith"]) || !isset($xmlSetting["name"])) {
http_response_code(400);
return false;
}
if ($xmlSetting["name"] == "" || $xmlSetting["regexp"] == "" || $xmlSetting["replaceWith"] == "" || $xmlSetting["name"] == null) {
http_response_code(400);
return false;
}
$stmt = $this->dbc->prepare("INSERT INTO `xml_setting`(regularexp,replacewith, file_location, friendly_name) VALUES(:regularexp, :replaceWith, :fileLocation, :name)");
$stmt->bindParam(":regularexp", $xmlSetting["regexp"]);
$stmt->bindParam(":replaceWith", $xmlSetting["replaceWith"]);
$stmt->bindParam(":fileLocation", $xmlSetting["fileLocation"]);
$stmt->bindParam(":name", $xmlSetting["name"]);
$stmt->execute();
if ($stmt->errorCode() == 00000) return true;
return false;
}
public function updateApplication($applicationID, $putJSON)
{
$application = json_decode($putJSON, true);
$query = "";
if (isset($application["name"])) {//friendly_name
$query .= "friendly_name=:name ";
}
if (isset($application["apkName"])) {
if (substr($query, -1) != ',') $query .= ",";
$query .= "apk_name=:apkName ";
}
if (isset($application["forceInstall"])) {
if (substr($query, -1) != ',') $query .= ",";
$query .= "force_install=:forceInstall ";
}
if (isset($application["packageName"])) {
if (substr($query, -1) != ',') $query .= ",";
$query .= "package_name=:packageName ";
}
if (isset($application["dataDir"])) {
if (substr($query, -1) != ',') $query .= ",";
$query .= "data_dir=:dataDir ";
}
if (isset($application["apkURL"])) {
if (substr($query, -1) != ',') $query .= ",";
$query .= "apk_url=:apkURL ";
}
$check = $this->dbc->prepare("SELECT * FROM application WHERE id=:id");
$check->bindParam(":id", $applicationID);
$check->execute();
if ($check->rowCount() < 1) {
echo "No application with that id!";
http_response_code(404);
return true;
}
$stmt = $this->dbc->prepare("UPDATE application SET $query WHERE id=:id");
$stmt->bindParam(":id", $applicationID);
if (isset($application["name"])) {
$stmt->bindParam(":name", $application["name"]);
}
if (isset($application["apkName"])) {
$stmt->bindParam(":apkName", $application["apkName"]);
}
if (isset($application["forceInstall"])) {
$stmt->bindParam(":forceInstall", $application["forceInstall"]);
}
if (isset($application["packageName"])) {
$stmt->bindParam(":packageName", $application["packageName"]);
}
if (isset($application["dataDir"])) {
$stmt->bindParam(":dataDir", $application["dataDir"]);
}
if (isset($application["apkURL"])) {
$stmt->bindParam(":apkURL", $application["apkURL"]);
}
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
echo $stmt->queryString;
print_r($stmt->errorInfo());
return false;
}
}
public function updateUser($userID, $putJSON)
{
$user = json_decode($putJSON, true);
$query = "";
if (isset($user["name"])) {
$query .= "name=:name ";
}
if (isset($user["email"])) {
if (substr($query, -1) != ',') $query .= ",";
$query .= "mail=:email ";
}
if (isset($user["authToken"])) {
if (substr($query, -1) != ',') $query .= ",";
$query .= "auth_token=:authToken ";
}
$check = $this->dbc->prepare("SELECT * FROM user WHERE id=:id");
$check->bindParam(":id", $userID);
$check->execute();
if ($check->rowCount() < 1) {
echo "No user with that id!";
http_response_code(404);
return true;
}
$stmt = $this->dbc->prepare("UPDATE user SET $query WHERE id=:id");
$stmt->bindParam(":id", $userID);
if (isset($user["name"])) {
$stmt->bindParam(":name", $user["name"]);
}
if (isset($user["email"])) {
$stmt->bindParam(":email", $user["email"]);
}
if (isset($user["authToken"])) {
$stmt->bindParam(":authToken", $user["authToken"]);
}
$stmt->execute();
if ($stmt->errorCode() == "00000") {
return true;
} else {
return false;
}
}
public function searchGroups($groupName)
{
$stmt = $this->dbc->prepare("SELECT id, prio, name FROM `group` WHERE name LIKE :groupName");
$groupName = "%" . $groupName . "%";
$stmt->bindParam(":groupName", $groupName);
$stmt->execute();
$stmtAnswer = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (isset($stmtAnswer[0])) {
$groups = array(Group::class);
for ($i = 0; $i < count($stmtAnswer); $i++) {
$groups[$i] = new Group($stmtAnswer[$i]["id"], $stmtAnswer[$i]["prio"], $stmtAnswer[$i]["name"]);
$groups[$i] = $groups[$i]->getObject();
}
return $groups;
}
return null;
}
public function getGroup($groupID)
{
$groupstmt = $this->dbc->prepare("SELECT id, prio, name FROM `group` WHERE id=:groupID");
$usersstmt = $this->dbc->prepare("SELECT * FROM user, user_group WHERE user.id = user_group.user_id AND user_group.group_id=:groupID");
$applicationsstmt = $this->dbc->prepare("SELECT * FROM application, application_group WHERE application.id=application_group.application_id AND application_group.group_id=:groupID");
$groupstmt->bindParam(":groupID", $groupID);
$groupstmt->execute();
$groupstmtAnswer = $groupstmt->fetchAll(PDO::FETCH_ASSOC);
$usersstmt->bindParam(":groupID", $groupID);
$usersstmt->execute();
$userstmtAnswer = $usersstmt->fetchAll(PDO::FETCH_ASSOC);
$applicationsstmt->bindParam(":groupID", $groupID);
$applicationsstmt->execute();
$applicationsstmtAnswer = $applicationsstmt->fetchAll(PDO::FETCH_ASSOC);
$group = new Group($groupstmtAnswer[0]["id"], $groupstmtAnswer[0]["prio"], $groupstmtAnswer[0]["name"]);
foreach ($userstmtAnswer as $uservalues) {
$group->addUser(new User($uservalues["id"], $uservalues["name"], $uservalues["mail"], $uservalues["auth_token"], $uservalues["date_created"]));
}
foreach ($applicationsstmtAnswer as $applicationValues) {
$group->addApplication(new Application($applicationValues["id"], $applicationValues["data_dir"], $applicationValues["apk_name"], $applicationValues["apk_url"], $applicationValues["friendly_name"], $applicationValues["force_install"], $applicationValues["package_name"]));
}
return $group->getObject();
}
public function updateGroup($groupID, $putJSON)
{
$group = json_decode($putJSON, true);
$query = "";
if (isset($group["prio"])) {
$query .= "prio=:prio ";
}
if (isset($group["name"])) {
if (substr($query, -1) != ',') $query .= ",";
$query .= "name=:name ";
}
$check = $this->dbc->prepare("SELECT * FROM `group` WHERE id=:id");
$check->bindParam(":id", $groupID);
$check->execute();
if ($check->rowCount() < 1) {
echo "No group with that id!";
http_response_code(404);
return true;
}
$stmt = $this->dbc->prepare("UPDATE `group` SET $query WHERE id=:id");
$stmt->bindParam(":id", $groupID);
if (isset($group["prio"])) {