forked from LightsOnHudson/FPP-Plugin-RadioStation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.inc.php
executable file
·283 lines (234 loc) · 7.89 KB
/
funcs.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
<?php
$pluginName = "RadioStation";
$pluginVersion ="1.0";
//TODO: get .git/config [remote "origin"] url =
$gitURL = "https://github.com/LightsOnHudson/FPP-Plugin-RadioStation.git";
$logFile = $settings['logDirectory']."/".$pluginName.".log";
$pluginConfigFile = $settings['configDirectory'] . "/plugin." . $pluginName;
function get_ini_config(){
global $pluginName;
global $pluginConfigFile;
global $logFile;
echo "pluginName: $pluginName<br>";
echo "pluginConfigFile: $pluginConfigFile<br>";
echo "log_file: $logFile<br>";
$my_settings = parse_ini_file($pluginConfigFile, True);
// LoadPluginSettings($pluginName); # doesn't get headings/sections :-(
// Debug info
?>
<pre>
<?php
echo print_r($my_settings, TRUE);
?>
</pre>
POSTED
<pre>
<?php
echo print_r($_POST, TRUE);
?>
</pre>
<?php
return $my_settings;
}
function run_scripts(){
$path = dirname(__FILE__);
recurseCopy($path."/scripts", "/tmp/scripts");
echo "<br>$path/scripts";
$output = shell_exec('ls -l /tmp/scripts');
$output = $output."<br>".shell_exec("chmod +x /tmp/scripts/generate.sh && /tmp/scripts/generate.sh '$path'");
echo "<br><pre>$output</pre>";
}
function recurseCopy(
string $sourceDirectory,
string $destinationDirectory,
string $childFolder = ''
): void {
$directory = opendir($sourceDirectory);
if (is_dir($destinationDirectory) === false) {
echo "<br>mkdir $destinationDirectory";
mkdir($destinationDirectory);
}
if ($childFolder !== '') {
if (is_dir("$destinationDirectory/$childFolder") === false) {
mkdir("$destinationDirectory/$childFolder");
}
while (($file = readdir($directory)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
if (is_dir("$sourceDirectory/$file") === true) {
recurseCopy("$sourceDirectory/$file", "$destinationDirectory/$childFolder/$file");
} else {
copy("$sourceDirectory/$file", "$destinationDirectory/$childFolder/$file");
}
}
closedir($directory);
return;
}
while (($file = readdir($directory)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
if (is_dir("$sourceDirectory/$file") === true) {
recurseCopy("$sourceDirectory/$file", "$destinationDirectory/$file");
}
else {
copy("$sourceDirectory/$file", "$destinationDirectory/$file");
}
}
closedir($directory);
}
// function PrintMediaOptions($selectName,$selectedOption)
// {
// global $musicDirectory;
// global $videoDirectory;
// global $OPEN;
// // echo "OPEN: ".$OPEN."<br/> \n";
// echo "<select name=\"".$selectName."\">";
// $mediaEntries = array_merge(scandir($musicDirectory),scandir($videoDirectory));
// sort($mediaEntries);
// foreach($mediaEntries as $mediaFile)
// {
// if($mediaFile != '.' && $mediaFile != '..')
// {
// if($selectedOption != "" && $selectedOption == $mediaFile) {
// echo "<option selected value=\"" . $mediaFile . "\">" . $mediaFile . "</option>";
// } else {
// echo "<option value=\"" . $mediaFile . "\">" . $mediaFile . "</option>";
// }
// }
// }
// echo "</select>";
// }
function checkbox($group, $label, $name, $checked){
$check = "";
// echo "<br> checked? $checked";
if ($checked == 1){
$check = "checked";
}
return "<br><input type=\"checkbox\" id=\"$group~$name\" name=\"$group~$name\" value=\"1\" $check> <label for=\"$name\">$name</label>";
}
function input_text($group, $label, $name, $value){
return "<br><label for=\"$label\">$label</label>: <input type=\"text\" name=\"$group~$name\" size=\"32\" value=\"$value\">\n";
}
function input_hidden($name, $value){
return "<input type=\"hidden\" id=\"$name\" name=\"$name\" value=\"$value\">\n";
}
function edit_config_group($pl, $group_name, $prefix){
/**
* repeatable config area
*
* @param string $group_name
* @param string $prefix
* @return string
*/
return "<b>$group_name [X]</b>".input_text($pl, "name", "PLAYLIST_NAME", $group_name).input_text($pl, "prefix", "PREFIX", $prefix);
}
function post_to_array($post){
/** */
$thing = [];
foreach ($post as $key => $values) {
// echo "<br>$key=>$values";
if ($key === "submit"){
continue;
}
$s = explode("~", $key);
$group = $s[0];
$setting = $s[1];
$thing[$group][$setting] = $values;
echo "<br>[$group] $setting = $values";
}
return $thing;
}
function how_many($config, $key_start){
$count = 0;
// count the existing PL entries
foreach ($config as $key => $value) {
// echo "<br>$key starts? $key_start";
if(substr( $key, 0, 2 ) === $key_start) {
$count++;
}
}
return $count;
}
function add_new($new_post, $existing){
$posted = post_to_array($new_post);
echo "<br>new posted: $posted";
$counted = how_many($existing, "PL");
$new_num = $counted + 1;
echo "<br>Existing: $counted, adding as $new_num";
foreach( $posted as $key=>$vals){
echo "<br>$key => $vals";
$existing["PL".$new_num] = $vals;
}
return $existing;
}
if (!function_exists('write_ini_file')) {
/**
* Write an ini configuration file
*
* @param string $file
* @param array $array
* @return bool
*/
function write_ini_file($file, $array = []) {
echo "<br>writing ".print_r($array, TRUE)." to $file";
// check first argument is string
if (!is_string($file)) {
throw new \InvalidArgumentException('Function argument 1 must be a string.');
}
// check second argument is array
if (!is_array($array)) {
throw new \InvalidArgumentException('Function argument 2 must be an array.');
}
// process array
$data = array();
foreach ($array as $key => $val) {
if (is_array($val)) {
$data[] = "[$key]";
foreach ($val as $skey => $sval) {
if (is_array($sval)) {
foreach ($sval as $_skey => $_sval) {
if (is_numeric($_skey)) {
$data[] = $skey.'[] = '.(is_numeric($_sval) ? $_sval : (ctype_upper($_sval) ? $_sval : '"'.$_sval.'"'));
} else {
$data[] = $skey.'['.$_skey.'] = '.(is_numeric($_sval) ? $_sval : (ctype_upper($_sval) ? $_sval : '"'.$_sval.'"'));
}
}
} else {
$data[] = $skey.' = '.(is_numeric($sval) ? $sval : (ctype_upper($sval) ? $sval : '"'.$sval.'"'));
}
}
} else {
$data[] = $key.' = '.(is_numeric($val) ? $val : (ctype_upper($val) ? $val : '"'.$val.'"'));
}
// empty line
$data[] = null;
}
// open file pointer, init flock options
$fp = fopen($file, 'w');
$retries = 0;
$max_retries = 100;
if (!$fp) {
return false;
}
// loop until get lock, or reach max retries
do {
if ($retries > 0) {
usleep(rand(1, 5000));
}
$retries += 1;
} while (!flock($fp, LOCK_EX) && $retries <= $max_retries);
// couldn't get the lock
if ($retries == $max_retries) {
return false;
}
// got lock, write data
fwrite($fp, implode(PHP_EOL, $data).PHP_EOL);
// release lock
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
}
?>