-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.inc.php
879 lines (762 loc) · 31.3 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
<?php
include_once('ApiClient/DeviceDataApi.php');
//require_once('/opt/fpp/www/common.php');
//Helper functions
function persistMainPlaylist()
{
}
function persistSparePlaylist()
{
}
/**
* Writes the dynamic playlist data to the ini file
*
* @param $dynamic_playlist_data
*/
function persistDynamicPlaylist($dynamic_playlist_data)
{
//Write the returned dynamic playlist into our setting storage
setPluginSettings('DYNAMIC_PLAYLIST_DATA', $dynamic_playlist_data);
setPluginSettings('DYNAMIC_PLAYLIST_LAST_UPDATE', time());
}
/**
* Submits the now-playing sequence to the API
* called through FPPD callbacks
*
* @param $sequence
* @param $title
* @param $length
*/
function submitNowPlaying($sequence, $title, $length)
{
$nowPlayingData = array();
//Get settings
$settings = getPluginSettings();
//api client
$_apiClient = new DeviceDataApi($settings['API_SERVER'], $settings['API_KEY']);
//Makre sure we have a title
if (isset($title) && !empty($title)) {
$nowPlayingData['name'] = $title;
} else {
//use the sequence name, clean it up first
$clean_sequence_name = ucwords(strtolower(trim(str_replace("_", " ", str_replace(".fseq", "", $sequence)))));
$nowPlayingData['name'] = $clean_sequence_name;
}
//Build up the length if minutes and seconds area supplied
if ((isset($length))) {
$nowPlayingData['duration'] = ($length);
}
//Call the api and submit the now playing data
logEntry("Submit Now Playing data: " . json_encode($nowPlayingData));
$_apiClient->nowPlaying($nowPlayingData);
}
/**
* Retrieves the voted (whole) playlist from the API
*/
function getVotedPlaylist()
{
$voted_playlist_data = array();
//Get settings
$settings = getPluginSettings();
//api client
$_apiClient = new DeviceDataApi($settings['API_SERVER'], $settings['API_KEY']);
$voted_playlist_data = $_apiClient->getDynamicPlaylist();
//Call the api and get the most voted item
logEntry("Retrieve Voted Playlist -- DATA: " . json_encode($voted_playlist_data));
//Write dynamic playlist data into settings
persistDynamicPlaylist($voted_playlist_data['data']['playlist']);
return $voted_playlist_data;
}
/**
* Clears the list of played sequences
*/
function clearPlayedSequences()
{
global $pluginName;
writeToConfig('PLAYED_SEQUENCES', urlencode(json_encode(array())), $pluginName);
}
/**
* Creates a playlist for the 'voted playlist'
* the voted playlist will always have the VOTE_CHECK event included so the playlist is updated every round
* and keep playing the dynamic playlist
*
* @param $voted_playlist array
* @return bool|int
*/
function createVoted_Playlist($voted_playlist)
{
global $playlistDirectory, $DEBUG;
//Playlist name
$playlist_name = "VOTED_PLAYLIST";
$first = $last = 0;//Play first entry once, play last entry once, 0 is false or off
$written = false;
$sequence_exists = false;
$event_added = false;
//make sure the supplied item is not empty
//holds the rows of data for the playlist
$playlist_item_data = array();
//Get the plugin settings
$settings = getPluginSettings();
//if we have voted playlist data
if (!empty($voted_playlist)) {
//Voted playlist is an array so loop over it and process each item
foreach ($voted_playlist as $vp_idx => $vp_data) {
$sequence_exists = checkSequenceExists($vp_data);
//If sequence exists then we can make the playlist
if ($sequence_exists == true) {
//we can use this playlist data, save it
$playlist_item_data[] = $vp_data;
} else {
logEntry("createVoted_Playlist: Sequence DOES NOT Exists - SKIPPING - " . json_encode($vp_data));
}
}
//we have playlist item rows
if (isset($playlist_item_data) && !empty($playlist_item_data)) {
logEntry("createVoted_Playlist: Sequence Exists - Creating playlist");
//calculate the playlist path
$voted_playlist_path = $playlistDirectory . '/' . $playlist_name;
//remove the existing playlist file, we'll write a new one
unlink($voted_playlist_path);
//create the most voted playlist - taken from fppxml.php
$f = fopen($voted_playlist_path, "w");
//If w were able to open / get the file resource, then proceed with writing
if ($f !== FALSE) {
//write the playlist header that specifies whether first and/or last item plays once
//this hardly matters because we're changing playlists often
$entries = sprintf("%s,%s,\n", $first, $last);
//if there is a start event set, insert it
if (isset($settings['START_EVENT_SET']) && isset($settings['START_EVENT']) && $settings['START_EVENT_SET'] == true) {
//insert the event
logEntry("createVoted_Playlist: Inserting START EVENT -- " . $settings['START_EVENT']);
//remove the file extension from the event
$START_EVENT_FILE_NAME = trim(str_ireplace(".fevt", "", $settings['START_EVENT']));
$entries .= sprintf("%s,%s,\n", 'e', $START_EVENT_FILE_NAME);
}
//first item should be the spacer sequence
if (isset($settings['SPACER_SEQUENCE']) && !empty($settings['SPACER_SEQUENCE'])) {
//insert the sequence
logEntry("createVoted_Playlist: Inserting Spacer Sequence -- " . $settings['SPACER_SEQUENCE']);
$entries .= sprintf("%s,%s,\n", 's', $settings['SPACER_SEQUENCE']);
}
//loop over the $playlist_item_data, which contains the items to add to the playlist
foreach ($playlist_item_data as $pi_idx => $pi_data) {
//now write the playlist line, we're supplied with the whole line so put that in
$entries .= $pi_data . "\n";
}
//if there is a end event set, insert it
if (isset($settings['END_EVENT_SET']) && isset($settings['END_EVENT']) && $settings['END_EVENT_SET'] == true) {
//insert the event
logEntry("createVoted_Playlist: Inserting END EVENT -- " . $settings['END_EVENT']);
//remove the file extension from the event
$END_EVENT_FILE_NAME = trim(str_ireplace(".fevt", "", $settings['END_EVENT']));
$entries .= sprintf("%s,%s,\n", 'e', $END_EVENT_FILE_NAME);
}
// //Add the event at the end
// if ($event_added == false) {
// //Find if the event exists already, returned value is true/false
// $EVENT_FILE_NAME = getEventFileNameForKey("VOTE_CHECK_API");
// //remove the file event file extension
// $EVENT_FILE_NAME = trim(str_ireplace(".fevt", "", $EVENT_FILE_NAME));
// if (isset($EVENT_FILE_NAME) && !empty($EVENT_FILE_NAME)) {
// logEntry("createVoted_Playlist: Adding VOTE_CHECK_API event - " . $EVENT_FILE_NAME);
// //add the event file. eg e,01_05,
// $entries .= sprintf("%s,%s,\n", 'e', $EVENT_FILE_NAME);
// $event_added = true;
// } else {
// logEntry("createVoted_Playlist: Could note find VOTE_CHECK_API event");
// }
// }
//Write the file out
$written = fwrite($f, $entries);
//close file
fclose($f);
logEntry("createVoted_Playlist: Creating Playlist for All Voted Items -- " . $entries);
} else {
logEntry("createVoted_Playlist: " . "Unable to open file! : " . $voted_playlist_path);
exit("Unable to open file! : " . $voted_playlist_path);
}
//final double check that the playlist exists
$written = file_exists($voted_playlist_path);
//If it was written, then sync playlists, which copy the playlists to all the slave devices if we're in the correct mode
if ($written !== false) {
//Sync to slaves if required
syncPlaylists();
}
}
//Will also be false if sequence doesn't exists
//return file write result
return $written;
}
//we'll get here if the playlist_item was empty
return $written;
}
/**
* Retrieve the most voted item from the API
*
* @param bool $auto_reset
* @return null|string
*/
function getMostVotedItem($auto_reset = false)
{
$most_voted_data = array();
//Get settings
$settings = getPluginSettings();
//api client
$_apiClient = new DeviceDataApi($settings['API_SERVER'], $settings['API_KEY']);
$most_voted_data = $_apiClient->getMostVoted($auto_reset);
//Call the api and get the most voted item
logEntry("Retrieve Most Voted Item: Reset [" . $auto_reset . "] -- DATA: " . json_encode($most_voted_data));
return $most_voted_data;
}
/**
* Resets the vote count on the Most Voted item
*
* @param $playlist_item_id
* @return bool
*/
function resetMostVotedItem($playlist_item_id)
{
$playlist_item_post_reset = array();
//Get settings
$settings = getPluginSettings();
//api client
$_apiClient = new DeviceDataApi($settings['API_SERVER'], $settings['API_KEY']);
$playlist_item_post_reset = $_apiClient->resetPlaylistItemVotes($playlist_item_id);
//Call the api and get the most voted item
logEntry("Reset Most Voted Item votes - ID [" . $playlist_item_id . "] -- DATA: " . json_encode($playlist_item_post_reset));
return $playlist_item_post_reset;
}
/**
* Creates a playlist for the most voted item
* This playlist is then played by FPPD
*
* @param $playlist_item
* @return bool|int
*/
function createMostVoted_Playlist($playlist_item)
{
global $playlistDirectory, $settings, $DEBUG;
//Playlist name
$playlist_name = "MOST_VOTED";
$first = $last = 0;//Play first entry once, play last entry once, 0 is false or off
$written = false;
$sequence_exists = false;
//make sure the supplied item is not empty
if (!empty($playlist_item)) {
$sequence_exists = checkSequenceExists($playlist_item);
//If sequence exists then we can make the playlist
if ($sequence_exists == true) {
logEntry("createMostVoted_Playlist: Sequence Exists - Creating playlist ");
$most_voted_playlist_path = $playlistDirectory . '/' . $playlist_name;
//remove the existing playlist file, we'll write a new one
unlink($most_voted_playlist_path);
//create the most voted playlist - taken from fppxml.php
$f = fopen($most_voted_playlist_path, "w");
//If w were able to open / get the file resource, then proceed with writing
if ($f !== FALSE) {
//write the playlist header that specifies whether first and/or last item plays once
$entries = sprintf("%s,%s,\n", $first, $last);
//no space sequence is required for the most voted playlist as once it ends
//we return to the main playlist
//but the main playlist should have the spacer sequence as the first item (after the check votes event)
//now write the playlist line, we're supplied with the whole line so put that in
$entries .= $playlist_item . "\n";
//Write the file out
$written = fwrite($f, $entries);
//close file
fclose($f);
logEntry("createMostVoted_Playlist: Creating Playlist for Most Voted Item -- " . $entries);
} else {
logEntry("ERROR :: createMostVoted_Playlist: " . "Unable to open file! : " . $most_voted_playlist_path);
exit("Unable to open file! : " . $most_voted_playlist_path);
}
//final double check that the playlist exists
$written = file_exists($most_voted_playlist_path);
//If it was written, then sync playlists, which copy the playlists to all the slave devices if we're in the correct mode
if ($written !== false) {
//Sync to slaves if required
syncPlaylists();
}
} else {
logEntry("ERROR :: createMostVoted_Playlist: Sequence DOES NOT Exists - Aborting");
return $written;
}
//Will also be false if sequence doesn't exists
//return file write result
return $written;
}
//we'll get here if the playlist_item was empty
return $written;
}
/**
* If FPP is in a Master / Slave setup, then playlists will copied to all the slaved to keep everything in sync
*
*/
function syncPlaylists()
{
global $settings, $fppHome, $fppMode;
$dir = "playlists";
//if the fpp mode is master
if (strtolower($fppMode) == "master") {
if (isset($settings['MultiSyncRemotes']) && !empty($settings['MultiSyncRemotes'])) {
$remotes = explode(',', $settings['MultiSyncRemotes']);
//if we have remotes
if (!empty($remotes)) {
foreach ($remotes as $remote_id => $remote_ip) {
$command = "rsync -av --stats $fppHome/media/$dir/ $remote_ip::media/$dir/ 2>&1";
$command_result = exec($command);
logEntry("syncPlaylists: Attempt Sync Playlists to (" . $remote_ip . ") -- Result: " . $command_result);
}
}
}
}
}
/**
* Checks if a sequence exists
*
* @param $playlist_item_row string Should be a string that represents the playlist item row,
* @return boolean
*/
function checkSequenceExists($playlist_item_row)
{
global $settings;
$exists = false;
if (isset($playlist_item_row) && !empty($playlist_item_row)) {
//extract data out
$row_data_array = explode(",", $playlist_item_row);
//[0] - type
// 'b' => Media and Sequence
// 'm' => Media Only
// 's' => Sequence Only
$type = $row_data_array[0];
//[1] - Sequence name
$sequence_name = $row_data_array[1];
//[2] - Media - if set
if (isset($row_data_array[2])) {
$sequence_audio = $row_data_array[2];
}
//Make sure the sequence name is set and not empty if so then something is wrong
if (isset($sequence_name) && !empty($sequence_name)) {
//check return all the valid types for a sequence
$sequence_types = playlistItemTypeMappings('sequence');
//Doubly check that the type of this playlist item is a sequence type
if (in_array($type, $sequence_types)) {
//
// logEntry("checkSequenceExists: Playlist Item is a sequence");
//Build up the sequence path
$sequence_path = $settings['sequenceDirectory'] . '/' . $sequence_name;
//Check that it exists
$exists = file_exists($sequence_path);
return $exists;
} else {
//
logEntry("WARNING :: checkSequenceExists: Playlist Item is NOT a sequence");
return false;
}
} else {
logEntry("ERROR :: checkSequenceExists: Could not extract sequence name from: " . $playlist_item_row);
return $exists;
}
}
//Exists will be false if we make it down here
return $exists;
}
/**
* Uploads the playilist(s) to the voting server
* stores the response as the dynamic playlist (DYNAMIC_PLAYLIST_DATA).
*
* DYNAMIC_PLAYLIST_DATA is the playlist as it is on the server
*/
function uploadPlaylist()
{
//Get settings
$settings = getPluginSettings();
//api client
$_apiClient = new DeviceDataApi($settings['API_SERVER'], $settings['API_KEY']);
$playlist_options = array();
$playlist_upload_api = $dynamic_playlist_data = $playlist_data = array();
//Build the playlist options up based on what our selected settings here
if (!empty($settings['FULLY_DYNAMIC_ONLY']) && strtolower($settings['FULLY_DYNAMIC_ONLY']) == "on") {
$playlist_options['auto_rearrange'] = true;
$playlist_options['auto_weighting'] = true;
} else if (!empty($settings['HIGHEST_VOTED_ONLY']) && strtolower($settings['HIGHEST_VOTED_ONLY']) == "on") {
$playlist_options['auto_rearrange'] = false;
$playlist_options['auto_weighting'] = false;
}
//Get the playlist data
$playlist_data['main'] = $settings['MAIN_PLAYLIST_DATA'];
//get the spare playlist data if spare voting
if (isset($settings['SPARE_VOTING']) && strtolower($settings['SPARE_VOTING']) == 'on') {
$playlist_data['spare'] = $settings['SPARE_PLAYLIST_DATA'];
}
//If we have playlist data... well at least the main playlist, we can upload it
if (isset($playlist_data['main']) && !isset($playlist_data['spare']) && isset($playlist_options)) {
//Main playlist upload
//make the call
logEntry("Sending main playlist data to api: " . json_encode(array('options' => $playlist_options, 'main' => $playlist_data['main'])));
//just send the main playlist
$playlist_upload_api = $_apiClient->upload($playlist_options, $playlist_data['main'], 'main');
} //if we have both the main and spare playlists set
elseif (isset($playlist_data['main']) && isset($playlist_data['spare']) && isset($playlist_options)) {
//Main + Spare playlist upload
//make the call
logEntry("Sending both main+spare playlist data to api: " . json_encode(array('options' => $playlist_options, 'main' => $playlist_data['main'], 'spare' => $playlist_data['spare'])));
//send both playlists
$playlist_upload_api = $_apiClient->upload($playlist_options, $playlist_data, 'both');
}
//From the response we want the playlist_main data, we'll store it as the dynamic playlist
if (isset($playlist_upload_api['data']['device_data'])) {
//get the main playlist by default
$dynamic_playlist_data = $playlist_upload_api['data']['device_data']['playlist_main'];
logEntry("Playlist upload successful: " . json_encode($dynamic_playlist_data));
//if the spare playlist is also not null & spare voting was set
if (isset($playlist_upload_api['data']['device_data']['playlist_spare']) && strtolower($settings['SPARE_VOTING']) == 'on') {
$dynamic_playlist_data = array_merge($playlist_upload_api['data']['device_data']['playlist_main'], $playlist_upload_api['data']['device_data']['playlist_spare']);
}
persistDynamicPlaylist($dynamic_playlist_data);
}
}
/**
* Returns the Device / API key from the settings
*
* @return mixed
*/
function getDeviceKey()
{
$settings = getPluginSettings();
return $settings['API_KEY'];
}
/**
* Write plugin settings
*
* @param $setting_key
* @param $setting_data
*/
function setPluginSettings($setting_key, $setting_data)
{
$pluginName = "FPP-VotingAPI-Integration";
//If setting data is any kind of array or object, json_encode it so we can put it in our settings file
if (is_object($setting_data) or is_array($setting_data)) {
$setting_data = urlencode(json_encode($setting_data));
}
WriteSettingToFile("$setting_key", $setting_data, $pluginName);
}
/**
* Returns an array of our plugin settings, with decoded data etc.
*
* @return array
*/
function getPluginSettings()
{
global $settings;
//Build up the path tot he plugin settings
$pluginName = "FPP-VotingAPI-Integration";
$pluginConfigFile = $settings['configDirectory'] . "/plugin." . $pluginName;
//settings array
$settings_array = [];
//load settings if plugin settings file exists
if (file_exists($pluginConfigFile)) {
$pluginSettings = parse_ini_file($pluginConfigFile);
//Read checkbox settings in
$settings_array['ENABLED'] = $pluginSettings['ENABLED'];
//API / Device Key
$settings_array['API_KEY'] = $pluginSettings['API_KEY'];
//API server
if (isset($pluginSettings['API_SERVER']) && !empty($pluginSettings['API_SERVER'])) {
$settings_array['API_SERVER'] = urldecode($pluginSettings['API_SERVER']);
} else {
//default to production if not set
$api_servers = getApiServers('production');
//try set the default server to production from the environment file
if (array_key_exists('server', $api_servers)) {
$settings_array['API_SERVER'] = $api_servers['server'];
} else {
$settings_array['API_SERVER'] = "http://prod.christmaslightsnear.me/api/";
}
}
//Playlists -- all JSON encoded
$settings_array['MAIN_PLAYLIST'] = ($pluginSettings['MAIN_PLAYLIST']);
$settings_array['MAIN_PLAYLIST_DATA'] = json_decode(urldecode($pluginSettings['MAIN_PLAYLIST_DATA']), true);
//Backup made of the main playlist before it's modified by the vote_check system
$settings_array['MAIN_PLAYLIST_BACKUP'] = json_decode(urldecode($pluginSettings['MAIN_PLAYLIST_BACKUP']), true);
//Spare playlist
$settings_array['SPARE_PLAYLIST'] = ($pluginSettings['SPARE_PLAYLIST']);
$settings_array['SPARE_PLAYLIST_DATA'] = json_decode(urldecode($pluginSettings['SPARE_PLAYLIST_DATA']), true);
//Spacer sequence
$settings_array['SPACER_SEQUENCE'] = ($pluginSettings['SPACER_SEQUENCE']);
//Start & End events
$settings_array['START_EVENT'] = ($pluginSettings['START_EVENT']);
$settings_array['START_EVENT_SET'] = ($pluginSettings['START_EVENT_SET']);
$settings_array['END_EVENT_SET'] = ($pluginSettings['END_EVENT_SET']);
$settings_array['END_EVENT'] = ($pluginSettings['END_EVENT']);
//Backup playlist is a copy of the main playlist
$settings_array['BACKUP_PLAYLIST'] = json_decode(urldecode($pluginSettings['BACKUP_PLAYLIST']), true);
//Dynamic Playlist is the playlist returned from the API,
$settings_array['DYNAMIC_PLAYLIST_DATA'] = json_decode($pluginSettings['DYNAMIC_PLAYLIST_DATA'], true);
$settings_array['DYNAMIC_PLAYLIST_LAST_UPDATE'] = $pluginSettings['DYNAMIC_PLAYLIST_LAST_UPDATE'];
//Sequence repeating options
$settings_array['SPARE_VOTING'] = $pluginSettings['SPARE_VOTING'];
$settings_array['MAX_ITEM_REPEATS'] = $pluginSettings['MAX_ITEM_REPEATS'];
$settings_array['MAX_ITEM_ROTATIONS'] = $pluginSettings['MAX_ITEM_ROTATIONS'];
$settings_array['MOST_VOTED_RESET'] = $pluginSettings['MOST_VOTED_RESET'];
//Playlist options
$settings_array['FULLY_DYNAMIC_ONLY'] = $pluginSettings['FULLY_DYNAMIC_ONLY'];
$settings_array['HIGHEST_VOTED_ONLY'] = $pluginSettings['HIGHEST_VOTED_ONLY'];
//
$settings_array['SET_MAIN_PL_NAMES_ADDED'] = $pluginSettings['SET_MAIN_PL_NAMES_ADDED'];
$settings_array['SET_SPARE_PL_NAMES_ADDED'] = $pluginSettings['SET_SPARE_PL_NAMES_ADDED'];
//Sequences played / tracking items (so we know what and how many times they've played)
$settings_array['PLAYED_SEQUENCES'] = json_decode(urldecode($pluginSettings['PLAYED_SEQUENCES']), true);
$settings_array['PL_TRANSITIONED'] = $pluginSettings['PL_TRANSITIONED'];
//Dynamic Playlist Upate Sequence Number -- used to track if there were any updates to the playlist checked against the API value, if changed, pull down a new playlist
// $DYNAMIC_PLAYLIST_USN = $pluginSettings['DYNAMIC_PLAYLIST_USN'];
}
return $settings_array;
}
/**
* Returns a full date format to be used by the php date_format function
* Current: Y-m-d H:i:s
*
* @return string
*/
function getDateTimeFormatFull()
{
$date_time_format_full = "Y-m-d H:i:s";
return $date_time_format_full;
}
/**
* Returns a date format to be used by the php date_format function
* Current: Y-m-d
*
* @return string
*/
function getDateFormat()
{
$date_time_format_full = "Y-m-d";
return $date_time_format_full;
}
/**
* Returns a date format to be used by the php date_format function
* Current: H:i:s
*
* @return string
*/
function getTimeFormat()
{
$date_time_format_full = "H:i:s";
return $date_time_format_full;
}
/**
* Returns the item types used in the playlist files for sequences, event, etc etc.
*
* @param $type
* @return array|mixed
*/
function playlistItemTypeMappings($type)
{
// 'b' => Media and Sequence
// 'm' => Media Only
// 's' => Sequence Only
// 'p' => Pause
// 'e' => Event
// 'P' => Plugin
//convert type to all lower characters
$type_lower = strtolower($type);
//mappings array
$playlist_type_mappings = array(
'sequence' => array(
'b',
'm',
'a'
),
'event' => array('e'),
'pause' => array('p'),
'plugin' => array('P')
);
//if supplied type exists in the array, return it's values
if (array_key_exists($type_lower, $playlist_type_mappings)) {
return $playlist_type_mappings[$type_lower];
}
return null;
}
/**
* Processes the supplied playlist item entry and returns a easy to deal with array
*
* @param $row_data
* @return array
*/
function processPlaylistItemRow($row_data)
{
//b,2015-wizards in winter-tso.fseq,04-Wizards in Winter.mp3,
//Make sure we've been supplied with data
if (isset($row_data)) {
//Explode array
$row_data_array = explode(",", $row_data);
//If array data is now empty
if (!empty($row_data_array)) {
$sequence_name_clean = '';
//extract data out
$type = $row_data_array[0];
//Sequence name
if (isset($row_data_array[1])) {
$sequence_name = $row_data_array[1];
$sequence_name_clean = str_replace(".fseq", "", $row_data_array[1]);
}
//Media name
if (isset($row_data_array[2])) {
$song_name = $row_data_array[2];
}
//Get the sequence types for playlists
$_sequence_types = playlistItemTypeMappings('sequence');
//If the item type is one of sequences then we want to keep this row
if (in_array($type, $_sequence_types)) {
//return a nicely formatted array
return array('name' => $sequence_name_clean, 'data' => $row_data);
}
}
}
return null;
}
/**
* Returns a list of playlists from the the /playlists directory
*
* @return array
*/
function retrievePlaylists()
{
global $settings;
$playlists = read_playlist_directory_files($settings['mediaDirectory'] . "/playlists", false);
$playlist_names = array();
//find the plugin configs
foreach ($playlists as $fname => $fdata) {
if ((stripos(strtolower($fname), ".json") == false)) {
$playlist_name = $fname;
$playlist_names[$playlist_name] = array('type' => 'file', 'location' => $settings['mediaDirectory'] . "/playlists" . "/" . $fname);
}
}
return $playlist_names;
}
/**
* Returns a list of sequences from the sequences directory
*
* @return array
*/
function retrieveSequences()
{
global $settings;
$sequences = read_directory_files($settings['sequenceDirectory'] . "/", false);
$sequence_names = array();
//find the plugin configs
foreach ($sequences as $fname => $fdata) {
if ((stripos(strtolower($fname), ".json") == false)) {
$sequence_name = $fname;
$sequence_names[$sequence_name] = array('type' => 'file', 'location' => $settings['sequenceDirectory'] . "/" . $fname);
}
}
return $sequence_names;
}
/**
* Returns a list of events
*
* @return array
*/
function retrieveEvents()
{
global $settings, $eventDirectory;
//Read events from the event directory
$events = read_directory_files($eventDirectory, false);
$event_names = array();
//Get the event file for this plugin so we can exclude itr
$event_for_plugin = getEventFileNameForKey("VOTE_CHECK_API");
//find the plugin configs
foreach ($events as $fname => $fdata) {
if ((stripos(strtolower($fname), ".json") == false)) {
if (strtolower($event_for_plugin) !== strtolower($fname)) {
$event_content_name = "";
//try get the name of the event
$event_data = parse_ini_file($eventDirectory . "/" . $fname);
if (!empty($event_data)) {
$event_content_name = $event_data['name'];
}
$event_name = $fname;
$event_names[$event_name] = array('type' => 'file', 'name' => $event_content_name, 'location' => $eventDirectory . "/" . $fname);
}
}
}
return $event_names;
}
/**
* Returns a contents of the specified playlist
*
* @param string $playlist_name
* @return array
*/
function retrievePlaylistContents($playlist_name)
{
global $settings;
$playlist_data = null;
//check the supplied playlist name isn't empty
if (!empty($playlist_name)) {
$playlist_contents = read_playlist_directory_files($settings['mediaDirectory'] . "/playlists", true);
//find the plugin configs
foreach ($playlist_contents as $fname => $fdata) {
if ((stripos(strtolower($fname), ".json") == false)) {
//Find the playlist supplied, $fname is the filename found in the playlist directory
if (strtolower($fname) == strtolower($playlist_name)) {
//split the string to get jsut the plugin name
// $playlist_name = explode(".", $fname);
// $playlist_name = $playlist_name[1];
// $playlist_name = str_replace("_"," ",$fname);
$playlist_name = $fname;
$playlist_data[$playlist_name] = array('type' => 'file', 'data' => $fdata);
}
}
}
}
return $playlist_data;
}
/**
* Looks in a directory and if specified reads file contents of files within it
*
* @param $directory String directory to search in
* @param $return_data Boolean switch to include file data
* @return array Array of file names and respective data
*/
function read_playlist_directory_files($directory, $return_data = false)
{
$file_list = array();
$file_data = false;
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
// do something with the file
// note that '.' and '..' is returned even
// if file isn't this directory or its parent, add it to the results
if ($file != "." && $file != "..") {
// collect the filenames & data
if ($return_data == true) {
$file_data = explode("\n", file_get_contents($directory . '/' . $file));
}
$file_list[$file] = $file_data;
}
}
closedir($handle);
}
return $file_list;
}
/**
* Writes settings to the plugin config
*
* @param $setting
* @param $payload
* @param $pluginName
* @param bool $encode
*/
function writeToConfig($setting, $payload, $pluginName, $encode = false)
{
if ((isset($setting) && is_string($setting)) && isset($payload)) {
if ($encode == true) {
$payload = json_encode($payload);
}
WriteSettingToFile($setting, $payload, $pluginName);
}
}