forked from r10r/rcswitch-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcswitch.php
More file actions
30 lines (26 loc) · 856 Bytes
/
Copy pathrcswitch.php
File metadata and controls
30 lines (26 loc) · 856 Bytes
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
<?php
// For Apache to run this, run visudo and add this line:
// www-data ALL=(ALL) NOPASSWD: /usr/bin/rcswitch
define('SWITCH_CMD', 'sudo rcswitch');
date_default_timezone_set('Europe/Berlin');
$systemCode = $_GET['system'];
$unitCode = $_GET['unit'];
$state = $_GET['state'];
$sleep = $_GET['sleep'];
$message = date('Y-m-d H:i:s').' switching '.$systemCode.':'.$unitCode.' to '.$state.'... ';
$output = array();
$retval = 0;
$cmd = implode(' ', array(SWITCH_CMD, $systemCode, $unitCode, $state, '2>&1'));
if ($sleep) {
$cmd = '(sleep '.$sleep.' ; '.$cmd.') > /dev/null &';
}
exec($cmd, $output, $retval);
if ($retval === 0) {
$message .= "OK\n";
} else {
$message .= 'FAIL: '.implode("\n", $output)."\n";
}
$logfile = fopen('/tmp/rcswitch.log', 'a');
fwrite($logfile, $message);
fclose($logfile);
echo $message;