forked from FalconChristmas/FPP-Plugin-EventDate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonFunctions.inc.php
executable file
·484 lines (347 loc) · 10.4 KB
/
commonFunctions.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
<?php
//curl do nothing function
function do_nothing($curl, $input) {
return 0; // aborts transfer with an error
}
//pring Min selection
function printMinSelection($min, $minName) {
global $DEBUG;
echo "<select name=\"".$minName."\"> \n";
for($i=0;$i<=59;$i++) {
if($i == $min) {
echo "<option selected value=\"".$i."\">".str_pad($i,2,'0',STR_PAD_LEFT)."</option> \n";
} else {
echo "<option value=\"".$i."\">".str_pad($i,2,'0',STR_PAD_LEFT)."</option> \n";
}
}
echo "</select> \n";
}
//pring hour selection
function printHourSelection($hour, $hourName) {
global $DEBUG;
echo "<select name=\"".$hourName."\"> \n";
for($i=0;$i<=23;$i++) {
if($i == $hour) {
echo "<option selected value=\"".$i."\">".str_pad($i,2,'0',STR_PAD_LEFT)."</option> \n";
} else {
echo "<option value=\"".$i."\">".str_pad($i,2,'0',STR_PAD_LEFT)."</option> \n";
}
}
echo "</select> \n";
}
//print month selection
function printMonthSelection($month, $monthName) {
global $DEBUG;
$month_array = array("1" => "January",
"2" => "February",
"3" => "March",
"4" => "April",
"5" => "May",
"6" => "June",
"7" => "July",
"8" => "August",
"9" => "September",
"10" => "October",
"11" => "November",
"12" => "December");
echo "<select name=\"".$monthName."\"> \n";
foreach($month_array as $m => $name) {
if($m == $month) {
echo "<option selected value=\"".$m."\">".$name."</option> \n";
} else {
echo "<option value=\"".$m."\">".$name."</option> \n";
}
}
echo "</select> \n";
}
//print month selection
function printDaySelection($day, $dayName) {
global $DEBUG;
echo "<select name=\"".$dayName."\"> \n";
for($i=1;$i<=31;$i++) {
if($i == $day) {
echo "<option selected value=\"".$i."\">".$i."</option> \n";
} else {
echo "<option value=\"".$i."\">".$i."</option> \n";
}
}
echo "</select> \n";
}
//print month selection
function printYearSelection($year, $yearName) {
global $DEBUG;
$dateHolder = date("Y");
$year_array = array($dateHolder);
for ($x = 1; $x < 5; $x++) {
array_push($year_array, date("Y") + $x);
}
echo "<select name=\"".$yearName."\"> \n";
foreach($year_array as $y) {
if($y == $year) {
echo "<option selected value=\"".$y."\">".$y."</option> \n";
} else {
echo "<option value=\"".$y."\">".$y."</option> \n";
}
}
echo "</select> \n";
}
//send response function
function sendResponse($from,$REPLY_TEXT,$GMAIL_ADDRESS,$subject) {
global $DEBUG, $gv, $EMAIL, $RESPONSE_METHOD;
logEntry("Sending response using: ".$RESPONSE_METHOD);
switch ($RESPONSE_METHOD) {
case "SMS":
$gv->sendSMS($from,$REPLY_TEXT);
break;
case "EMAIL":
sendMail($GMAIL_ADDRESS, $EMAIL, $subject, $REPLY_TEXT);
break;
}
}
//sendmail using phpmailer function
function sendMail($to, $from, $subject, $body) {
global $DEBUG, $EMAIL, $PASSWORD;
date_default_timezone_set('Etc/UTC');
if($DEBUG) {
echo "To: ".$to."\n";
echo "From: ".$from."\n";
echo "subject: ".$subject."\n";
echo "body: ".$body."\n";
}
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
if($DEBUG) {
$mail->SMTPDebug = 2;
} else {
$mail->SMTPDebug = 0;
}
//Ask for HTML-friendly debug output
if($DEBUG)
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $EMAIL;
//Password to use for SMTP authentication
$mail->Password = $PASSWORD;
//Set who the message is to be sent from
$mail->setFrom($EMAIL, 'Holiday');
//Set an alternative reply-to address
$mail->addReplyTo($EMAIL, 'Holiday');
//Set who the message is to be sent to
$mail->addAddress($to, $from);
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($body);
//Replace the plain text body with one created manually
$mail->AltBody = $body;
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
logEntry( "Mailer Error: " . $mail->ErrorInfo);
} else {
logEntry( "Message sent!");
}
}
//fork a non blocking fppd process
function forkExec($cmd) {
global $DEBUG;
if($DEBUG)
logEntry("Forking command: ".$cmd);
//$safe_arg["arg_2"] = escapeshellarg($arg_2);
$pid = pcntl_fork();
if ( $pid == -1 ) {
// Fork failed
if($DEBUG)
logEntry("fork failed");
exit(1);
} else if ( $pid ) {
// We are the parent
if($DEBUG) {
logEntry("------------");
logEntry("fork parent");
logEntry("------------");
}
return "Parent";
// Can no longer use $db because it will be closed by the child
// Instead, make a new MySQL connection for ourselves to work with
} else {
if($DEBUG){
logEntry("------------");
logEntry("fork child");
logEntry("------------");
}
//logEntry("sleeping 5 seconds, processing, thensleeping agin");
exec($cmd);
return "Child";
}
}
//fork a non blocking fppd process
function fork($argv) {
global $DEBUG;
$safe_arg = escapeshellarg($argv[4]);
//$safe_arg["arg_2"] = escapeshellarg($arg_2);
$pid = pcntl_fork();
if ( $pid == -1 ) {
// Fork failed
if($DEBUG)
logEntry("fork failed");
exit(1);
} else if ( $pid ) {
// We are the parent
if($DEBUG) {
logEntry("------------");
logEntry("fork parent");
logEntry("------------");
}
return "Parent";
// Can no longer use $db because it will be closed by the child
// Instead, make a new MySQL connection for ourselves to work with
} else {
if($DEBUG){
logEntry("------------");
logEntry("fork child");
logEntry("------------");
}
//logEntry("sleeping 5 seconds, processing, thensleeping agin");
processCallback($argv);
return "Child";
}
}
//get the string between two characters
function get_string_between ($str,$from,$to) {
$string = substr($str,strpos($str,$from)+strlen($from));
if (strstr ($string,$to,TRUE) != FALSE) {
$string = strstr ($string,$to,TRUE);
}
return $string;
}
//update plugin
function updatePluginFromGitHub($gitURL, $branch="master", $pluginName) {
global $settings;
logEntry ("updating plugin: ".$pluginName);
logEntry("settings: ".$settings['pluginDirectory']);
//create update script
//$gitUpdateCMD = "sudo cd ".$settings['pluginDirectory']."/".$pluginName."/; sudo /usr/bin/git git pull ".$gitURL." ".$branch;
$pluginUpdateCMD = "/opt/fpp/scripts/update_plugin ".$pluginName;
logEntry("update command: ".$pluginUpdateCMD);
exec($pluginUpdateCMD, $updateResult);
//logEntry("update result: ".print_r($updateResult));
//loop through result
return;// ($updateResult);
}
//create script to randmomize
function createScriptFile($scriptFilename,$scriptCMD) {
global $scriptDirectory,$pluginName;
$scriptFilename = $scriptDirectory."/".$scriptFilename;
logEntry("Creating script: ".$scriptFilename);
$ext = pathinfo($scriptFilename, PATHINFO_EXTENSION);
$data = "";
$data .="#!/bin/sh\n";
$data .= "\n";
$data .= "#Script to run randomizer\n";
$data .= "#Created by ".$pluginName."\n";
$data .= "#\n";
$data .= "/usr/bin/php ".$scriptCMD."\n";
logEntry($data);
$fs = fopen($scriptFilename,"w");
fputs($fs, $data);
fclose($fs);
}
//return the next event file available for use
//get the next available event filename
function getNextEventFilename() {
$MAX_MAJOR_DIGITS=2;
$MAX_MINOR_DIGITS=2;
global $eventDirectory;
//echo "Event Directory: ".$eventDirectory."<br/> \n";
$MAJOR=array();
$MINOR=array();
$MAJOR_INDEX=0;
$MINOR_INDEX=0;
$EVENT_FILES = directoryToArray($eventDirectory, false);
//print_r($EVENT_FILES);
foreach ($EVENT_FILES as $eventFile) {
$eventFileParts = explode("_",$eventFile);
$MAJOR[] = (int)basename($eventFileParts[0]);
//$MAJOR = $eventFileParts[0];
$minorTmp = explode(".fevt",$eventFileParts[1]);
$MINOR[] = (int)$minorTmp[0];
//echo "MAJOR: ".$MAJOR." MINOR: ".$MINOR."\n";
//print_r($MAJOR);
//print_r($MINOR);
}
$MAJOR_INDEX = max(array_values($MAJOR));
$MINOR_INDEX = max(array_values($MINOR));
//echo "Major max: ".$MAJOR_INDEX." MINOR MAX: ".$MINOR_INDEX."\n";
if($MAJOR_INDEX <= 0) {
$MAJOR_INDEX=1;
}
if($MINOR_INDEX <= 0) {
$MINOR_INDEX=1;
} else {
$MINOR_INDEX++;
}
$MAJOR_INDEX = str_pad($MAJOR_INDEX, $MAX_MAJOR_DIGITS, '0', STR_PAD_LEFT);
$MINOR_INDEX = str_pad($MINOR_INDEX, $MAX_MINOR_DIGITS, '0', STR_PAD_LEFT);
//for now just return the next MINOR index up and keep the same Major
$newIndex=$MAJOR_INDEX."_".$MINOR_INDEX.".fevt";
//echo "new index: ".$newIndex."\n";
return $newIndex;
}
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
//check all the event files for a string matching this and return true/false if exist
function checkEventFilesForKey($keyCheckString) {
global $eventDirectory;
$keyExist = false;
$eventFiles = array();
$eventFiles = directoryToArray($eventDirectory, false);
foreach ($eventFiles as $eventFile) {
if( strpos(file_get_contents($eventFile),$keyCheckString) !== false) {
// do stuff
$keyExist= true;
break;
// return $keyExist;
}
}
return $keyExist;
}
?>