-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.php
1207 lines (1134 loc) · 38.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
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
/*
* Project Name: FusionBells for FusionPBX
*
* Description: Multicast Bell Notification for
* education environments. Uses FFMPEG for transcoding
* and network delivery.
*
* Requirements: FusionPBX, SQLite and Multicast network
* configuration.
*
* Developer: FikesMedia.com
* Contact: [email protected]
* Copyright: Copyright 2017 FikesMedia All Right Reserved
*/
/*
* Add a non default day and return the ID for
* FullCalendar.io to update view.
*
* Christopher Fikes
* 03/03/2017
*/
function insertCalendarDay(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Grab POST Values
$day = $_POST['day'];
$schedule = $_POST['schedule'];
//Check if values are null, return error if so.
if( is_null($schedule) || is_null($day) || $schedule == "" || $day == "" ){
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Empty Values Sent DAY $day SCHEDULE $schedule"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Check Null and Empty
//Check for invalid Lengths, return error if so
elseif ( strlen($day) != 10 ) {
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Invalid Date Length"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Check Day Length
//Compelete the transaction
else {
try {
//Connect to DB and insert posted Values
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Prepare Statement for insert
$Insert = $fusionBellDB->prepare('INSERT INTO calendar (Date,Schedule) VALUES (?,?)');
//Bind Parameters for Insert and Execute
$Insert->execute(array($day,$schedule));
//Fetch back new record information
//Prepare Statement for Query
$Return = $fusionBellDB->prepare('SELECT * FROM calendar WHERE Date = ? AND Schedule = ? LIMIT 1');
//Bind Parameters for Query and Execute
$Return->execute(array($day,$schedule));
//Set Variables of Returned Data
foreach($Return as $row) {
$RecordID = $row['ID'];
$RecordDate = $row['Date'];
$RecordSchedule = $row['Schedule'];
}
//Create JSON Message with Record Values
$returnValue = ['msg' => 'complete', "recordid" => $RecordID, "recorddate" => $RecordDate, "recordschedule" => $RecordSchedule];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
}//End of Else of Bad POST Data
}//End of insertCalendarDay Function
/*
* Remove a non default day and return status.
*
* Christopher Fikes
* 03/03/2017
*/
function removeCalendarDay(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Grab POST Values
$day = $_POST['day'];
$schedule = $_POST['schedule'];
//Check if values are null, return error if so.
if( is_null($schedule) || is_null($day) || $schedule == "" || $day == "" ){
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Empty Values Sent"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Check Null and Empty
//Check for invalid Lengths, return error if so
elseif ( strlen($day) != 10 ) {
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Invalid Date Length"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Check Day Length
//Compelete the transaction
else {
try {
//Connect to DB and insert posted Values
$fusionBellDB = new PDO("sqlite:$UUID.db");
$Return = $fusionBellDB->prepare('DELETE FROM calendar WHERE Date = ? AND Schedule = ?');
//Bind Parameters for Query and Execute
$Return->execute(array($day,$schedule));
//Create JSON Message with Record Values
$returnValue = ['msg' => 'complete'];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
}//End of Else of Bad POST Data
}//End of removeCalendarDay Function
/*
* Update a non default day and return status.
*
* Christopher Fikes
* 03/03/2017
*/
function updateCalendarDay(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Grab POST Values
$day = $_POST['day'];
$ID = $_POST['ID'];
//Check if values are null, return error if so.
if( is_null($ID) || is_null($day) || $day == "" || $ID == "" ){
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Empty Values Sent"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Check Null and Empty
//Check for invalid Lengths, return error if so
elseif ( strlen($day) != 10 ) {
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Invalid Date Length"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Check Day Length
//Compelete the transaction
else {
try {
//Connect to DB and insert posted Values
$fusionBellDB = new PDO("sqlite:$UUID.db");
$Return = $fusionBellDB->prepare('UPDATE calendar SET Date = ? WHERE ID = ?');
//Bind Parameters for Query and Execute
$Return->execute(array($day,$ID));
//Create JSON Message with Record Values
$returnValue = ['msg' => 'complete'];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
}//End of Else of Bad POST Data
}//End of updateCalendarDay Function
/*
* Returns All Scheduled Calendar Days in JSON
* for FullCalendar.io to update view.
*
* Christopher Fikes
* 03/03/2017
*/
function returnCalendarDays(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
try {
//Connect to DB and insert posted Values
$fusionBellDB = new PDO("sqlite:$UUID.db");
$Return = $fusionBellDB->prepare('SELECT * FROM calendar');
//Execute
$Return->execute();
//Setup Counter
$i = 0;
//Loop Through each row creating a separate associate array
foreach($Return as $row){
//Format for FullCalendar.io
$returnJSON[$i] = array("title" => $row['Schedule'], "start" => $row['Date'], "id" => $row['ID'], "allDay" => true);
$i++;
}
//Create JSON Message with Record Values
header('Content-Type: application/json');
echo json_encode($returnJSON);
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
}//End of returnCalendarDays Function
/*
* Plays desired tone from the dashboard
*
* Christopher Fikes
* 03/03/2017
*/
function manualBell() {
//Check if valid POST Type
if(is_null($_POST['tone'] || $_POST['tone'] == "")){ die("NoType"); }
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Set ring type from POST
switch ($_POST['tone']) {
case 'Normal' :
$toneType = "\"Bell Tone\"";
break;
case 'FireDrill' :
$toneType = "\"Fire Drill Tone\"";
break;
case 'WeatherDrill' :
$toneType = "\"Weather Drill Tone\"";
break;
default :
$toneType = "\"Bell Tone\"";
break;
}
//Query WAV file used for specified tone
$Result = $fusionBellDB->query("SELECT SettingValue FROM settings WHERE SettingName = $toneType LIMIT 1;");
//Set value is any return given
if(count($Result,0) != 0) {
foreach($Result as $row){
$tone = $row['SettingValue'];
}
}
//Query if this is the ZoneBOX
$ActingZoneBox = $fusionBellDB->prepare('SELECT SettingValue FROM settings WHERE SettingName = ? LIMIT 1');
$ActingZoneBox->execute(array("Acting ZoneBOX"));
//Set value is any return given
foreach ($ActingZoneBox as $row) {
$Setting = $row['SettingValue'];
}
//I am the ZoneBox
if ($Setting == "Disabled") {
sendManualBell($tone);
}
//This is the ZoneBox
elseif ($Setting == "Enabled") {
//Query Multicast address to use
$Result = $fusionBellDB->query("SELECT ZoneValue FROM zones WHERE ZoneName = 'All Tone' LIMIT 1;");
//Set value is any return given
if(count($Result,0) != 0) {
foreach($Result as $row){
$address = $row['ZoneValue'];
}
}
//Call System command with specified parameters
$exec="./ffmpeg -re -i ./tones/$UUID/$tone -filter_complex 'aresample=16000,asetnsamples=n=160' -acodec g722 -ac 1 -vn -f rtp udp://$address &";
exec($exec);
}
}
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}
}//End manualBell Function
/*
* Get bell schedule for specified getSchedules
* and return as formated JSON
*
* Christopher Fikes
* 03/03/2017
*/
function getSchedule() {
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Set desired schedule from POST
$schedule = $_POST['schedule'];
//Error if Null or blank and send back error message
if(is_null($schedule) || $schedule == ""){
$returnValue = ['error' => 'no values input for GET Schedule'];
header('Content-Type: application/json');
echo json_encode($returnValue);
} else {
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Query for specified schedule
$Result = $fusionBellDB->prepare('SELECT Schedule,Time,Tone,Zone FROM schedules WHERE Schedule = ? ORDER BY Time');
$Result->execute(array($schedule));
//If any results continue
if(count($Result,0) != 0) {
//Setup Counter
$i = 0;
//For each returned result append array
foreach($Result as $row){
$returnJSON[$i] = $row;
$i++;
}
//Return array as formated JSON
header('Content-Type: application/json');
echo json_encode($returnJSON);
}//End if any results continue
} //End Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
} //End capture errors
} //End Else no Errors
} //End getSchedule function
/*
* Get all zones and return as formated JSON
*
* Christopher Fikes
* 05/09/2017
*/
function getZones(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Ask for just unique values from schedules table
$Result = $fusionBellDB->query("SELECT distinct ZoneName FROM zones ORDER BY Schedule ASC;");
//If any results continue
if(count($Result,0) != 0) {
//Setup Counter
$i = 0;
//For each returned result append array
foreach($Result as $row){
$returnJSON[$i] = $row;
$i++;
}
//Return array as formated JSON
header('Content-Type: application/json');
echo json_encode($returnJSON);
}//End if any results continue
}//End Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}
}
/*
* Deletes zone and attached schedules
*
* Christopher Fikes
* 05/09/2017
*/
function delZone(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Sets name from POST
$name = $_POST['zonename'];
if(is_null($name) || $name == ""){
$returnValue = ['error' => 'no values input for delZone'];
header('Content-Type: application/json');
echo json_encode($returnValue);
} else {
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Del zone with post vars
$delZone = $fusionBellDB->prepare('DELETE FROM zones WHERE ZoneName = ?');
//Bind Parameters for Query and Execute
$delZone->execute(array($name));
$delSchedule = $fusionBellDB->prepare('DELETE FROM schedules WHERE Zone = ?');
//Bind Parameters for Query and Execute
$delSchedule->execute(array($name));
//Return results as formated JSON
$returnValue = ['Status' => "complete"];
echo json_encode($returnValue);
}//End Try DB code
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End catch
}//End Else no errors
}//End delZone
/*
* Get all schedules and return as formated JSON
*
* Christopher Fikes
* 03/03/2017
*/
function getSchedules(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Ask for just unique values from schedules table
$Result = $fusionBellDB->query("SELECT distinct Schedule FROM schedules ORDER BY Schedule ASC;");
//If any results continue
if(count($Result,0) != 0) {
//Setup Counter
$i = 0;
//For each returned result append array
foreach($Result as $row){
$returnJSON[$i] = $row;
$i++;
}
//Return array as formated JSON
header('Content-Type: application/json');
echo json_encode($returnJSON);
}//End if any results continue
}//End Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}
}
/*
* Creates a new schedule by adding a new
* schedule name with a midnight ring.
*
* Christopher Fikes
* 03/03/2017
*/
function createNewSchedule(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Sets name from POST
$name = $_POST['schedulename'];
//Set first bell to midnight
$time = "23:59";
//Assign All Tone Zone
$zone = "All Tone";
//Check is name is null or blank if true error
if(is_null($name) || $name == ""){
$returnValue = ['error' => 'no values input for Create New Schedule'];
header('Content-Type: application/json');
echo json_encode($returnValue);
} else {
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Get default tone from Settings
$Result = $fusionBellDB->query("SELECT SettingValue FROM settings WHERE SettingName = 'Bell Tone' Limit 1;");
foreach($Result as $row){
$defaultTone = $row['SettingValue'];
}
//Insert new bell at midnight using name from post with default tone
$Result = $fusionBellDB->prepare('INSERT INTO schedules (Schedule,Time,Tone,Zone) VALUES (?,?,?,?)');
$Result->execute(array($name,$time,$defaultTone,$zone));
//Return array as formated JSON
$returnValue = ['Status' => 'Completed'];
echo json_encode($returnValue);
}//End try DB code
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End Catch
}//End Else no errors
}//End createNewSchedule function
/*
* Deletes all references to schedule
* including special dates.
*
* Christopher Fikes
* 03/03/2017
*/
function deleteSchedule(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Set name from POST
$schedule = $_POST['schedulename'];
//If null or blank error and send back error
if(is_null($schedule) || $schedule == ""){
$returnValue = ['error' => 'no values input for Delete Schedule'];
header('Content-Type: application/json');
echo json_encode($returnValue);
} else {
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Delete all from schedules
$Return = $fusionBellDB->prepare('DELETE FROM schedules WHERE Schedule = ?');
//Bind Parameters for Query and Execute
$Return->execute(array($schedule));
//Delete all from calendar
$ReturnCal = $fusionBellDB->prepare('DELETE FROM calendar WHERE Schedule = ?');
//Bind Parameters for Query and Execute
$ReturnCal->execute(array($schedule));
//Return results as formated JSON
$returnValue = ['Status' => "Completed Deletion $schedule"];
echo json_encode($returnValue);
}//End Try DB code
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End catch
}//End Else no errors
}//End deleteSchedule function
/*
* Deletes bell from schedule.
*
* Christopher Fikes
* 03/03/2017
*/
function deleteBell(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Get Schedule name and time form POST
$name = $_POST['schedulename'];
$time = $_POST['time'];
$zone = $_POST['zone'];
//Check for empty values and send back error
if(is_null($name) || is_null($time) || $name == "" || $time == ""){
$returnValue = ['error' => 'no values input for Delete Schedule'];
header('Content-Type: application/json');
echo json_encode($returnValue);
} else {
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Delete bell from above reference
$Result = $fusionBellDB->prepare('DELETE FROM schedules WHERE Schedule = ? AND Time = ? AND Zone = ?');
$Result->execute(array($name,$time,$zone));
//Return Formated JSON
$returnValue = ['Status' => "Completed"];
echo json_encode($returnValue);
}
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}
}
}
/*
* Creates new bell for schedule.
*
* Christopher Fikes
* 03/03/2017
*/
function newBell(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Get information from POST
$name = $_POST['schedulename'];
$time = $_POST['time'];
$tone = $_POST['tone'];
$zone = $_POST['zone'];
//Check for empty values and send back error
if(is_null($name) || $name == "" || is_null($time) || $time == "" || is_null($tone) || $tone == "" || is_null($zone) || $zone == ""){
$returnValue = ['error' => 'no values input for New Bell'];
header('Content-Type: application/json');
echo json_encode($returnValue);
} else {
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Insert into Schedules New Bell
$Result = $fusionBellDB->prepare('INSERT INTO schedules (Schedule,Time,Tone,Zone) VALUES (?,?,?,?)');
$Result->execute(array($name,$time,$tone,$zone));
//Return Formated JSON
$returnValue = ['Status' => 'Completed'];
echo json_encode($returnValue);
}
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}
}
}
/*
* Save Setting
*
* Christopher Fikes
* 03/03/2017
*/
function saveSetting(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Grab POST Values
$settingName = $_POST['settingName'];
$settingValue = $_POST['settingValue'];
//Check if values are null, return error if so.
if( is_null($settingName) || is_null($settingValue) || $settingName == "" || $settingValue == "" ){
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Empty Values Sent"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End Check Values
else {
try {
//Connect to DB and insert posted Values
$fusionBellDB = new PDO("sqlite:$UUID.db");
$Return = $fusionBellDB->prepare('UPDATE settings SET SettingValue = ? WHERE SettingName = ?');
//Bind Parameters for Query and Execute
$Return->execute(array($settingValue,$settingName));
//Create JSON Message with Record Values
$returnValue = ['msg' => 'complete'];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
}//End of Else of Bad POST Data
}//End of saveSetting Function
/*
* Save Zone
*
* Christopher Fikes
* 05/10/2017
*/
function saveZone(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Grab POST Values
$zoneName = $_POST['zoneName'];
$zoneValue = $_POST['zoneValue'];
//Check if values are null, return error if so.
if( is_null($zoneName) || is_null($zoneValue) || $zoneName == "" || $zoneValue == "" ){
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Empty Values Sent"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End Check Values
else {
try {
//Connect to DB and insert posted Values
$fusionBellDB = new PDO("sqlite:$UUID.db");
$Return = $fusionBellDB->prepare('UPDATE zones SET ZoneValue = ? WHERE ZoneName = ?');
//Bind Parameters for Query and Execute
$Return->execute(array($zoneValue,$zoneName));
//Create JSON Message with Record Values
$returnValue = ['msg' => 'complete'];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
}//End of Else of Bad POST Data
}//End of saveZone Function
/*
* New Zone
*
* Christopher Fikes
* 05/10/2017
*/
function createNewZone(){
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
//Grab POST Values
$zoneName = $_POST['zoneName'];
$zoneValue = $_POST['zoneValue'];
//Check if values are null, return error if so.
if( is_null($zoneName) || is_null($zoneValue) || $zoneName == "" || $zoneValue == "" ){
//Create JSON Message of Error
$returnValue = ['msg' => 'error', "error" => "Empty Values Sent"];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End Check Values
else {
try {
//Connect to DB and insert posted Values
$fusionBellDB = new PDO("sqlite:$UUID.db");
$Return = $fusionBellDB->prepare('INSERT INTO zones (ZoneName,ZoneValue) VALUES (?,?)');
//Bind Parameters for Query and Execute
$Return->execute(array($zoneName,$zoneValue));
//Create JSON Message with Record Values
$returnValue = ['msg' => 'complete'];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
}//End of Else of Bad POST Data
}//End of saveZone Function
/*
* Get All Tones and return as formated JSON
*
* Christopher Fikes
* 03/03/2017
*/
function getTones(){
session_start();
$UUID = $_SESSION['domain_uuid'];
error_log("DEBUG ---------- $UUID");
$dir = __DIR__ . "/tones";
$dir = $dir . "/$UUID";
error_log("DEBUG ---------- $dir");
$tones = scandir($dir);
$returnValue = array();
$i = 0;
foreach($tones as $key => $value) {
if (!is_dir("$dir/$value")) {
$returnValue[$i] = array("file" => $value);
$i++;
}
}
header('Content-Type: application/json');
echo json_encode($returnValue);
}
/*
* Create new tone from TTS Engine
*
* Christopher Fikes
* 03/03/2017
*/
function createTTSTone(){
session_start();
$UUID = $_SESSION['domain_uuid'];
//Setup Variables
$dir = __DIR__ . "/tones/$UUID";
$saveName = $_POST['filename'];
error_log(" THIS IS THE SAVE DIR $dir/$saveName");
$text = $_POST['text'];
if(!is_null($saveName) || $saveName == "" | !is_null($text) || $text == "") {
//Call System command with specified parameters
$exec = "/usr/bin/pico2wave -w $dir/$saveName \"$text\" &";
exec($exec);
//Return Status
$returnValue = ['msg' => 'complete'];
header('Content-Type: application/json');
echo json_encode($returnValue);
} else {
error_log("VALUES: $dir $saveName $text");
}
} //End createTTSTone
/*
* Deletes tone from library
*
* Christopher Fikes
* 03/05/2017
*/
function delTone() {
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
$dir = __DIR__ . "/tones/$UUID";
$file = $_POST['filename'];
$defaultTone = "";
try {
//Connect to DB and insert posted Values
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Get Default Tone
$Default = $fusionBellDB->prepare('SELECT SettingName, SettingValue FROM settings WHERE SettingName = ?');
//Bind Parameters for Query and Execute
$Default->execute(array("Bell Tone"));
if(count($Default,0) != 0){
foreach($Default as $row){
$defaultTone = $row['SettingValue'];
}
}
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
if(!is_null($file) || $file == "" || $file == $defaultTone) {
if (!unlink("$dir/$file")) {
$returnValue = ['msg' => 'error'];
header('Content-Type: application/json');
echo json_encode($returnValue);
} else {
try {
$Update = $fusionBellDB->prepare('UPDATE schedules SET Tone = ? WHERE Tone = ?');
//Bind Parameters for Query and Execute
$Update->execute(array($defaultTone,$file));
//Create JSON Message with Record Values
$returnValue = ['msg' => 'complete'];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Try
//Issues connecting
catch (PDOException $e) {
//Set JSON Message with Error Text
$returnValue = [ "msg" => "error", "error" => $e->getMessage() ];
header('Content-Type: application/json');
echo json_encode($returnValue);
}//End of Catch
}
}
}
/*
* Gets the next bell ring for today.
*
* Christopher Fikes
* 03/06/2017
*/
function getNextRing() {
//Begin Session
session_start();
//GET UUID
$UUID = $_SESSION["domain_uuid"];
$dateNow = date("Y-m-d");
$timeNow = date("H:i");
try {
//Connect to DB
$fusionBellDB = new PDO("sqlite:$UUID.db");
//Select cout of schedules on this day
$Special = $fusionBellDB->prepare("SELECT count(*) FROM calendar WHERE Date = ?");
$Special->execute(array($dateNow));
$Count = $Special->fetchColumn();
//If result found, today has a specified schedule
if($Count != 0) {
//Get Schedule name for today
$Special01 = $fusionBellDB->prepare("SELECT Schedule FROM calendar WHERE Date = ? LIMIT 1");
$Special01->execute(array($dateNow));
while ($row = $Special01->fetch(PDO::FETCH_ASSOC)) {
$Schedule = $row['Schedule'];
//Check to see if there are any more bells in thes schedule for today
$Special02 = $fusionBellDB->prepare("SELECT count(*) FROM schedules WHERE Schedule = ? AND Time > ?");
$Special02->execute(array($Schedule,$timeNow));
$Count = $Special02->fetchColumn();
//If results found, schedule has bells left
if($Count != 0) {
$Special03 = $fusionBellDB->prepare("SELECT * FROM schedules WHERE Schedule = ? AND Time > ? ORDER BY Time LIMIT 1");
$Special03->execute(array($Schedule,$timeNow));
while ($row = $Special03->fetch(PDO::FETCH_ASSOC)) {
$Schedule = $row['Schedule'];
$Tone = $row['Tone'];
$Time = $row['Time'];
$returnJSON[0]=array("Schedule"=>$Schedule,"Time"=>$Time);
}
}
//Else Schedule has played out.
else {
$returnJSON[0]=array("Schedule"=>$Schedule,"Time"=>"Schedule Finished");
}
}
}
//Today has no schedules set using default
else {
$Default01 = $fusionBellDB->prepare("SELECT SettingValue FROM settings WHERE SettingName = ? ");
$Default01->execute(array("Default Schedule"));
while ($row = $Default01->fetch(PDO::FETCH_ASSOC)) {
$Schedule = $row['SettingValue'];
//Check to see if there are any more bells in thes schedule for today
$Default02 = $fusionBellDB->prepare("SELECT count(*) FROM schedules WHERE Schedule = ? AND Time > ?");
$Default02->execute(array($Schedule,$timeNow));
$Count = $Default02->fetchColumn();
//If results found, schedule has bells left
if($Count != 0) {
$Special03 = $fusionBellDB->prepare("SELECT * FROM schedules WHERE Schedule = ? AND Time > ? ORDER BY Time LIMIT 1");
$Special03->execute(array($Schedule,$timeNow));
while ($row = $Special03->fetch(PDO::FETCH_ASSOC)) {
$Schedule = $row['Schedule'];
$Tone = $row['Tone'];
$Time = $row['Time'];
$returnJSON[0]=array("Schedule"=>$Schedule,"Time"=>$Time);
}
}
//Else Schedule has played out.
else {
$returnJSON[0]=array("Schedule"=>$Schedule,"Time"=>"Schedule Finished");
}
}
}
header('Content-Type: application/json');
echo json_encode($returnJSON);