-
Notifications
You must be signed in to change notification settings - Fork 6
/
functions.inc.php
executable file
·901 lines (591 loc) · 24.6 KB
/
functions.inc.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
<?php
function mkTimestamp($year,$month,$day, $hours=0,$minutes=0,$seconds=0){
// Same as mktime() but parameters are in most significant to least significant order.
return mktime($hours,$minutes,$seconds, $month,$day,$year);
}
//create database
function createTwilioTables($db) {
//global $db;
global $DEBUG;
$createQuery = "CREATE TABLE IF NOT EXISTS profanity (messageID INTEGER PRIMARY KEY AUTOINCREMENT, timestamp int(16) NOT NULL, message varchar(255), pluginName varchar(64), pluginData varchar(64));";
logEntry("TWILIO: CREATING Messages Table for TWILIO: ".$createQuery);
$db->exec($createQuery) or die('Create Table Failed');
$createQuery = "CREATE TABLE IF NOT EXISTS blacklist (messageID INTEGER PRIMARY KEY AUTOINCREMENT, timestamp int(16) NOT NULL, message varchar(255), pluginName varchar(64), pluginData varchar(64));";
logEntry("TWILIO: CREATING Messages Table for TWILIO: ".$createQuery);
$db->exec($createQuery) or die('Create Table Failed');
$createQuery = "CREATE TABLE IF NOT EXISTS messages (messageID INTEGER PRIMARY KEY AUTOINCREMENT, timestamp int(16) NOT NULL, message varchar(255), pluginName varchar(64), pluginData varchar(64));";
logEntry("TWILIO: CREATING Messages Table for TWILIO: ".$createQuery);
$db->exec($createQuery) or die('Create Table Failed');
}
function insertTwilioMessage($message, $pluginName, $pluginData) {
global $db;
$messagesTable = "messages";
//$db = new SQLite3($DBName) or die('Unable to open database');
$insertQuery = "INSERT INTO ".$messagesTable." (timestamp, message, pluginName, pluginData) VALUES ('".time()."','".urlencode($message)."','".$pluginName."','".$pluginData."');";
logEntry("TWILIO: INSERT query string: ".$insertQuery);
$db->exec($insertQuery) or die('could not insert into database');
}
function insertBlacklistMessage($message, $pluginName, $pluginData) {
global $db;
$blackListTable = "blacklist";
//$db = new SQLite3($DBName) or die('Unable to open database');
$insertQuery = "INSERT INTO ".$blackListTable." (timestamp, message, pluginName, pluginData) VALUES ('".time()."','".urlencode($message)."','".$pluginName."','".$pluginData."');";
logEntry("TWILIO: INSERT query string: ".$insertQuery);
$db->exec($insertQuery) or die('could not insert into database');
}
function insertProfanityMessage($message, $pluginName, $pluginData) {
global $db;
$profanityListTable = "profanity";
//$db = new SQLite3($DBName) or die('Unable to open database');
$insertQuery = "INSERT INTO ".$profanityListTable." (timestamp, message, pluginName, pluginData) VALUES ('".time()."','".urlencode($message)."','".$pluginName."','".$pluginData."');";
logEntry("TWILIO: INSERT query string: ".$insertQuery);
$db->exec($insertQuery) or die('could not insert into database');
}
//check if the user is in the blacklist
function checkBlacklist($fromNumber) {
global $db, $DEBUG;
$blackListTable = "blacklist";
$blackListQuery = "SELECT * FROM ".$blackListTable." WHERE pluginData = '".$fromNumber."'";
if($DEBUG)
logEntry("TWILIO: Blacklist query: ".$blackListQuery);
$result = $db->query($blackListQuery) or die('Query failed');
//$rows = $result->numRows();//($result);
while ($row = $result->fetchArray())
{
//TODO: return
$blackListDate = $row['timestamp'];
return $blackListDate;
}
return null;
//$blackListQueryCount = "SELECT COUNT(*) FROM ".$blackListTable." WHERE pluginData = ".$fromNumber;
// $blackListCount = $db->query($blackListQueryCount) or die('Query failed');
//$rows = $result->numRows();//($result);
//if ($blackListCount > 0) {
// return true;
//} else {
// return false;
// }
}
//chec how many profanitys for a number in profanity queue
function checkProfanityCount($numberToCheck) {
global $db, $profanityMessageQueueFile, $DEBUG, $TwilioVersion;
$blacklist = false;
$profanityCount =0;
if($DEBUG)
logEntry("TWILIO: Inside Checking profanity number: ".$numberToCheck);
switch($TwilioVersion) {
case "2.0":
$profanityListTable = "profanity";
$profanityQuery = "SELECT COUNT(*) FROM ".$profanityListTable." WHERE pluginData = '".$numberToCheck."'";
logEntry("TWILIO: Profanity search count query: ".$profanityQuery);
//$profanityCheckCountResult = $db->query($profanityQuery) or die('Query failed');
$profanityCheckCountResult = $db->querySingle($profanityQuery) or die('Query failed');
logEntry("TWILIO: Profanity check counter: ".$profanityCheckCountResult);
//$rows = $result->numRows();//($result);
return $profanityCheckCountResult;
while ($row = $result->fetchArray())
{
//TODO: return
$profanityListDate = $row['timestamp'];
}
$rows = $result->numRows();//($result);
if ($rows > 0) {
return $rows;
} else {
return 0;
}
break;
default:
break;
}
//open same file and use "w" to clear file
$fc=file($profanityMessageQueueFile);
//loop through array using foreach
foreach($fc as $line)
{
if($DEBUG) {
logEntry("Looking at line: ".urldecode($line)." for profanity number: ".$numberToCheck);
}
$line = urldecode($line);
$numberToCheck = urldecode($numberToCheck);
$messageQueueParts = explode("|",$line);
$phoneNumber = trim(urldecode($messageQueueParts[3]));
if($DEBUG) {
logEntry("Number found in prfanity file: ".$phoneNumber. " number to check: ".$numberToCheck);
}
if($phoneNumber == $numberToCheck) {
logEntry("Found number in profanity: ".$numberToCheck);
$profanityCount++;
}
}
return $profanityCount;
}
//check to see if a number is blacklisted
function checkBlacklistNumber($numberToCheck) {
global $blacklistFile, $DEBUG, $DB;
$blacklist = false;
if($DEBUG)
logEntry("Inside Checking blacklist number: ".$numberToCheck);
switch ($TwilioVersion) {
case "2.0":
$result = $db->query('SELECT count(*) FROM blacklist where pluginData =\'".$numberToCheck."') or die('Query failed');
if($result >0) {
return true;
} else {
return false;
}
break;
default:
}
//open same file and use "w" to clear file
$fc=file($blacklistFile);
//loop through array using foreach
foreach($fc as $line)
{
if($DEBUG) {
logEntry("Looking at line: ".urldecode($line)." for blacklist number: ".$numberToCheck);
}
$line = urldecode($line);
$numberToCheck = urldecode($numberToCheck);
$messageQueueParts = explode("|",$line);
$phoneNumber = trim(urldecode($messageQueueParts[3]));
if($DEBUG) {
logEntry("Number found in blacklist file: ".$phoneNumber. " number to check: ".$numberToCheck);
}
if($phoneNumber == $numberToCheck) {
logEntry("Found number in blacklist: ".$numberToCheck);
return true;
}
}
if($DEBUG)
logEntry("Did not find number: ".$numberToCheck." in blacklist");
return false;
}
//add to blacklist
function addBlacklist($messageText,$pluginName,$pluginData="") {
global $blacklistFile, $DEBUG;
//logEntry("MESSAGEQUEUE_PLUGIN: Message File: ".$messageQueueFile);
// logEntry("MESSAGEQUEUE_PLUGIN: Adding message to message queue: ".$messageText." :".$pluginName." :".$pluginData);
if($DEBUG) {
logEntry("Adding number: ".$pluginData. " to blacklist file: ".$blacklistFile);
}
$messageLine = "";
$messageLine = time()."| ".urlencode($messageText) . " | ".$pluginName. " | ".$pluginData."\n";
//$messageLine = date('Y-m-d h:i:s A',time())."| ".$messageText . " | ".$pluginName. " | ".$pluginData."\n";
//echo "writing message line \r\n".$messageLine;
file_put_contents($blacklistFile, $messageLine, FILE_APPEND | LOCK_EX);
}
//add profanity message to profanity file
function addProfanityMessage($messageText,$pluginName,$pluginData="") {
global $profanityMessageQueueFile;
//logEntry("MESSAGEQUEUE_PLUGIN: Message File: ".$messageQueueFile);
logEntry("MESSAGEQUEUE_PLUGIN: Adding message to profanity message queue: ".$messageText." :".$pluginName." :".$pluginData);
$messageLine = "";
$messageLine = time()."| ".urlencode($messageText) . " | ".$pluginName. " | ".$pluginData."\n";
//$messageLine = date('Y-m-d h:i:s A',time())."| ".$messageText . " | ".$pluginName. " | ".$pluginData."\n";
//echo "writing message line \r\n".$messageLine;
file_put_contents($profanityMessageQueueFile, $messageLine, FILE_APPEND | LOCK_EX);
}
//send a TSMS message https post
function sendTSMSMessage($messageText) {
global $DEBUG,$TSMS_BODY_CONTAINED_HEX,$TSMS_phoneNumber,$TSMS_from,$TSMS_body,$TSMS_account_sid, $TSMS_auth_token;
if($TSMS_BODY_CONTAINED_HEX) {
$messageText .= " However; we removed any emoticons or non text characters";
}
if($DEBUG)
logEntry("Inside sendTSMSMessage");
$TSMS_URL = "https://api.twilio.com/2010-04-01/Accounts/".$TSMS_account_sid."/Messages.json";
//$postfields = array(urlencode("To=".$TSMS_from),
// urlencode("From=".$TSMS_phoneNumber),
// urlencode("Body=".$messageText),
// "-u " => $TSMS_account_sid.":".$TSMS_auth_token
// );
$postfields = array('To' => $TSMS_from,
'From' => $TSMS_phoneNumber,
'Body' => $messageText
);
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_USERPWD, "$TSMS_account_sid:$TSMS_auth_token");
curl_setopt($ch2, CURLOPT_URL, $TSMS_URL);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch2, CURLOPT_WRITEFUNCTION, 'do_nothing');
curl_setopt($ch2, CURLOPT_VERBOSE, false);
curl_setopt($ch2, CURLOPT_POST, 1);
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch2, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result2 = curl_exec($ch2);
if($DEBUG)
logEntry("TSMS Curl result: ".$result2);
//$TSMS_CURL_CMD = "curl -s -X POST 'https://api.twilio.com/2010-04-01/Accounts/".$TSMS_account_sid."/Messages.json' \
//--data-urlencode 'To=$TSMS_from' \
//--data-urlencode 'From=$TSMS_phoneNumber' \
//--data-urlencode 'Body=$messageText' \
//-u $TSMS_account_sid:$TSMS_auth_token";
//exec($TSMS_CURL_CMD);
if($DEBUG) {
logEntry("exiting sending TSMS Message");
}
return;
}
//strip hex characters from message - possible emoticons
function stripHexChars($line) {
return preg_replace('/([0-9#][\x{20E3}])|[\x{00ae}\x{00a9}\x{203C}\x{2047}\x{2048}\x{2049}\x{3030}\x{303D}\x{2139}\x{2122}\x{3297}\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u', '', $line);
}
//print the valid commands table and their variables
function printValidFPPCommands() {
global $DEBUG, $playCommands,$stopCommands, $repeatCommands, $statusCommands;
if($DEBUG) {
logEntry("Valid Play commands: ".$playCommands);
logEntry("Valid Stop commands: ".$stopCommands);
logEntry("Valid Repeat commands: ".$repeatCommands);
logEntry("Valid Status commands: ".$statusCommands);
}
//print a table
echo "<table border=\"3\" cellspacing=\"2\" cellpadding=\"2\"> \n";
echo "<th colspan=\"2\"> \n";
echo "Valid Commands \n";
echo "</th> \n";
echo "<tr> \n";
echo "<td> \n";
echo "Play commands: \n";
echo "</td> \n";
echo "<td> \n";
echo "<input type=\"text\" size=\"32\" name=\"PLAY_COMMANDS\" value=\"".$playCommands."\"> \n";
echo "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td> \n";
echo "Stop commands: \n";
echo "</td> \n";
echo "<td> \n";
echo "<input type=\"text\" size=\"32\" name=\"STOP_COMMANDS\" value=\"".$stopCommands."\"> \n";
echo "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td> \n";
echo "Repeat commands: \n";
echo "</td> \n";
echo "<td> \n";
echo "<input type=\"text\" size=\"32\" name=\"REPEAT_COMMANDS\" value=\"".$repeatCommands."\"> \n";
echo "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td> \n";
echo "Status commands: \n";
echo "</td> \n";
echo "<td> \n";
echo "<input type=\"text\" size=\"32\" name=\"STATUS_COMMANDS\" value=\"".$statusCommands."\"> \n";
echo "</td> \n";
echo "</tr> \n";
echo "</table> \n";
}
//get the fpp log level
function getFPPLogLevel() {
$FPP_LOG_LEVEL_FILE = "/home/fpp/media/settings";
if (file_exists($FPP_LOG_LEVEL_FILE)) {
$FPP_SETTINGS_DATA = parse_ini_file($FPP_LOG_LEVEL_FILE);
} else {
//return log level 0
return 0;
}
logEntry("FPP Settings file: ".$FPP_LOG_LEVEL_FILE);
$logLevelString = trim($FPP_SETTINGS_DATA['LogLevel']);
logEntry("Log level in fpp settings file: ".$logLevelString);
switch($logLevelString) {
case "info":
$logLevel=0;
// break;
case "warn":
$logLevel=1;
// break;
case "debug":
$logLevel=2;
// break;
case "excess":
$logLevel=3;
//break;
default:
$logLevel = 0;
}
return $logLevel;
}
function processSMSMessage($from,$messageText, $messageFile="") {
global $pluginName,$MESSAGE_QUEUE_PLUGIN_ENABLED, $MATRIX_MODE, $NAMES_PRE_TEXT,$messageQueueFile;
if($messageFile == "") {
$messageFile = $messageQueueFile;
}
switch ($MATRIX_MODE) {
case "FREE":
//do nothing
break;
case "NAMES":
$messageText = $NAMES_PRE_TEXT." ".$messageText;
break;
default:
break;
}
logEntry("TWILIO: Adding message from: ".$from. ": ".$messageText. " to Twillio message queue");
insertTwilioMessage($messageText, $pluginName, $from);
// logEntry("TWILIO: Adding message from: ".$from. ": ".$messageText. " to message queue");
// logEntry("TWILIO: Message queue file: ".$messageFile);
// if($MESSAGE_QUEUE_PLUGIN_ENABLED) {
// addNewMessage($messageText,$pluginName,$from, $messageFile);
// } else {
// logEntry("TWILIO: MessageQueue plugin is not enabled/installed: Cannot add message: ".$messageText);
// }
return;
}
//old profanity checkers
function profanityChecker($messageText) {
$profanityCheck = false;
logEntry("Checking: ".$messageText." for profanity");
return $profanityCheck;
}
//process the SMS commnadn coming in from a control number
function processSMSCommand($from,$SMSCommand="",$playlistName="") {
global $DEBUG,$client,$SMS_TYPE, $TSMS_phoneNumber, $REMOTE_FPP_ENABLED, $REMOTE_FPP_IP;
$FPPDStatus=false;
$output="";
// if($playlistName != "") {
$PLAYLIST_NAME = trim($playlistName);
// } else {
// logEntry("No playlist name specified, using Plugin defined playlist: ".$PLAYLIST_NAME);
// }
logEntry("Processing command: ".$SMSCommand." for playlist: ".$PLAYLIST_NAME);
$FPPDStatus = isFPPDRunning();
logEntry("FPPD status: ".$FPPDStatus);
if($FPPDStatus != "RUNNING") {
logEntry("FPPD NOT RUNNING: Sending message to : ".$from. " that FPPD status: ".$FPPDStatus);
//send a message that the daemon is not running and cannot execute the command
$client->account->messages->create(array( 'To' => $from, 'From' => $TSMS_phoneNumber, 'Body' => "FPPD is not running, cannot execute cmd"));//: ".$SMSCommand));
// sleep(1);
return;
} else {
//TODO: Maybe include an option to send message about FPPD is running. only send if FPPD is not running
// logEntry("Sending message to : ".$from. " that FPPD status: ".$FPPDStatus);
// $client->account->messages->create(array( 'To' => $from, 'From' => $TSMS_phoneNumber, 'Body' => "FPPD is running, I will execute command"));//: ".$SMSCommand));
}
$cmd = "/opt/fpp/bin.pi/fpp ";
switch (trim(strtoupper($SMSCommand))) {
case "PLAY":
$cmd .= "-P \"".$PLAYLIST_NAME."\"";
$REMOTE_cmd = "/usr/bin/curl \"http://".$REMOTE_FPP_IP."/fppxml.php?command=startPlaylist&playList=".$PLAYLIST_NAME."\"";
break;
case "STOP":
$cmd .= "-c stop";
$REMOTE_cmd = "/usr/bin/curl \"http://".$REMOTE_FPP_IP."/fppxml.php?command=stopNow\"";
break;
case "REPEAT":
$cmd .= "-p \"".$PLAYLIST_NAME."\"";
$REMOTE_cmd = "/usr/bin/curl \"http://".$REMOTE_FPP_IP."/fppxml.php?command=startPlaylist&playList=".$PLAYLIST_NAME."&repeat=checked\"";
break;
case "STATUS":
$playlistName = getRunningPlaylist();
if($playlistName == null) {
$playlistName = " No current playlist active or FPPD starting, please try your command again in a few";
}
logEntry("Sending SMS to : ".$from. " playlist: ".$playlistName);
$client->account->messages->create(array( 'To' => $from, 'From' => $TSMS_phoneNumber, 'Body' => "Playlist STATUS: ".$playlistName));
break;
default:
$cmd = "";
break;
}
if($REMOTE_FPP_ENABLED) {
logEntry("Remote FPP Command ENABLED");
$cmd = $REMOTE_cmd;
} else {
logEntry("Remote FPP command NOT ENABLED");
}
if($cmd !="" ) {
logEntry("Executing SMS command: ".$cmd);
exec($cmd,$output);
//system($cmd,$output);
}
//logEntry("Processing command: ".$cmd);
}
//is fppd running?????
function isFPPDRunning() {
$FPPDStatus=null;
logEntry("Checking to see if fpp is running...");
exec("if ps cax | grep -i fppd; then echo \"True\"; else echo \"False\"; fi",$output);
if($output[1] == "True" || $output[1] == 1 || $output[1] == "1") {
$FPPDStatus = "RUNNING";
}
//print_r($output);
return $FPPDStatus;
//interate over the results and see if avahi is running?
}
//get current running playlist
function getRunningPlaylist() {
global $sequenceDirectory;
$playlistName = null;
$i=0;
//can we sleep here????
//sleep(10);
//FPPD is running and we shoud expect something back from it with the -s status query
// #,#,#,Playlist name
// #,1,# = running
$currentFPP = file_get_contents("/tmp/FPP.playlist");
logEntry("Reading /tmp/FPP.playlist : ".$currentFPP);
if($currentFPP == "false") {
logEntry("We got a FALSE status from fpp -s status file.. we should not really get this, the daemon is locked??");
}
$fppParts="";
$fppParts = explode(",",$currentFPP);
// logEntry("FPP Parts 1 = ".$fppParts[1]);
//check to see the second variable is 1 - meaning playing
if($fppParts[1] == 1 || $fppParts[1] == "1") {
//we are playing
$playlistParts = pathinfo($fppParts[3]);
$playlistName = $playlistParts['basename'];
logEntry("We are playing a playlist...: ".$playlistName);
} else {
logEntry("FPPD Daemon is starting up or no active playlist.. please try again");
}
//now we should have had something
return $playlistName;
}
//create sequence files
function createSMSSequenceFiles() {
global $sequenceDirectory;
$SMSStartSendSequence= $sequenceDirectory."/"."SMS-STATUS-SEND.FSEQ";
$tmpFile = fopen($SMSStartSendSequence, "w") or die("Unable to open file for writing SMS SequencesFile!");
fclose($tmpFile);
}
function processSequenceName($sequenceName,$sequenceAction="NONE RECEIVED") {
global $CONTROL_NUMBER_ARRAY,$PLAYLIST_NAME,$EMAIL,$PASSWORD,$pluginDirectory,$pluginName;
logEntry("Sequence name: ".$sequenceName);
$sequenceName = strtoupper($sequenceName);
//$PLAYLIST_NAME= getRunningPlaylist();
if($PLAYLIST_NAME == null) {
$PLAYLIST_NAME = "FPPD Did not return a playlist name in time, please try again later";
}
// switch ($sequenceName) {
// case "SMS-STATUS-SEND.FSEQ":
$messageToSend="";
// $gv = new GoogleVoice($EMAIL, $PASSWORD);
//send a message to all numbers in control array and then delete them from new messages
for($i=0;$i<=count($CONTROL_NUMBER_ARRAY)-1;$i++) {
logEntry("Sending message to : ".$CONTROL_NUMBER_ARRAY[$i]. " that playlist: ".$PLAYLIST_NAME." is ACTION:".$sequenceAction);
//get the current running playlist name! :)
//$gv->sendSMS($CONTROL_NUMBER_ARRAY[$i], "PLAYLIST EVENT: ".$PLAYLIST_NAME." Action: ".$sequenceAction);
// $gv->sendSMS($CONTROL_NUMBER_ARRAY[$i], "PLAYLIST EVENT: Action: ".$sequenceAction);
}
logEntry("Plugin Directory: ".$pluginDirectory);
//run the sms processor outside of cron
$cmd = $pluginDirectory."/".$pluginName."/getSMS.php";
exec($cmd,$output);
}
//process new messages
function processNewMessages($SMS_FROM, $SMS_BODY) {
global $DEBUG;
$messageQueue = array();
$newmsgIDs = array();
$from = $SMS_FROM;
$msgText = $SMS_BODY;
if($DEBUG) {
logEntry("From: ".$from." MsgText: ".$msgText);
}
//strip the +1 from the phone number
if(substr($from,0,2) == "+1")
{
$from=substr($from,2);
}
//$messageQueue[$newMessageCount]=array($from,$msgText);
$messageQueue[$newMessageCount]=array($from,$msgText);
if($DEBUG){
print_r($messageQueue);
}
return $messageQueue;
}
//process read/sent messages
function logEntry($data,$logLevel=1,$sourceFile, $sourceLine) {
global $logFile,$myPid, $LOG_LEVEL;
if($logLevel <= $LOG_LEVEL)
//return
if($sourceFile == "") {
$sourceFile = $_SERVER['PHP_SELF'];
}
$data = $sourceFile." : [".$myPid."] ".$data;
if($sourceLine !="") {
$data .= " (Line: ".$sourceLine.")";
}
$logWrite= fopen($logFile, "a") or die("Unable to open file!");
fwrite($logWrite, date('Y-m-d h:i:s A',time()).": ".$data."\n");
fclose($logWrite);
}
function processCallback($argv) {
global $DEBUG,$pluginName;
if($DEBUG)
print_r($argv);
//argv0 = program
//argv2 should equal our registration // need to process all the rgistrations we may have, array??
//argv3 should be --data
//argv4 should be json data
$registrationType = $argv[2];
$data = $argv[4];
logEntry("PROCESSING CALLBACK: ".$registrationType);
$clearMessage=FALSE;
switch ($registrationType)
{
case "media":
if($argv[3] == "--data")
{
$data=trim($data);
logEntry("DATA: ".$data);
$obj = json_decode($data);
$type = $obj->{'type'};
logEntry("Type: ".$type);
switch ($type) {
case "sequence":
logEntry("media sequence name received: ");
processSequenceName($obj->{'Sequence'},"STATUS");
break;
case "media":
logEntry("We do not support type media at this time");
//$songTitle = $obj->{'title'};
//$songArtist = $obj->{'artist'};
//sendMessage($songTitle, $songArtist);
//exit(0);
break;
case "both":
logEntry("We do not support type media/both at this time");
// logEntry("MEDIA ENTRY: EXTRACTING TITLE AND ARTIST");
// $songTitle = $obj->{'title'};
// $songArtist = $obj->{'artist'};
// if($songArtist != "") {
// sendMessage($songTitle, $songArtist);
//exit(0);
break;
default:
logEntry("We do not understand: type: ".$obj->{'type'}. " at this time");
exit(0);
break;
}
}
break;
exit(0);
case "playlist":
logEntry("playlist type received");
if($argv[3] == "--data")
{
$data=trim($data);
logEntry("DATA: ".$data);
$obj = json_decode($data);
$sequenceName = $obj->{'sequence0'}->{'Sequence'};
$sequenceAction = $obj->{'Action'};
processSequenceName($sequenceName,$sequenceAction);
//logEntry("We do not understand: type: ".$obj->{'type'}. " at this time");
// logEntry("We do not understand: type: ".$obj->{'type'}. " at this time");
}
break;
exit(0);
default:
exit(0);
}
}
?>