summaryrefslogtreecommitdiff
path: root/index.php.in
blob: efa6b85b6e7cd88add88a40048824263327d3d02 (plain)
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
<?php

$f = popen('#BINDIR#/lights-out get power', 'r');
if ($f === false) {
  print 'Cannot getpower status' . "\n";
  die();
}
$power = trim(fgets($f));
pclose($f);

if ($power == '') {
  print 'Cannot read gpio - is it installed?' . "\n";
  die();
}

$f = popen('#BINDIR#/lights-out name', 'r');
if ($f === false) {
  print 'Error reading server name' . "\n";
  die();
}
$server = trim(fgets($f));
pclose($f);

if ($server == '') {
  print 'Empty server name in config' . "\n";
  die();
}

if (array_key_exists('HTTPS', $_SERVER)
&& ($_SERVER['HTTPS'] == 'on'))
  $https = true;
else
  $https = false;

function push_it() {
  if (!array_key_exists('password', $_POST))
    return;
  $f = fopen('../password', 'r');
  if ($f === false) {
    print '<font color="ff0000">Cannot read password file</font><br>' . "\n";
    return;
  }
  $saved_hash = trim(fgets($f));
  fclose($f);
  $entered_hash = $_POST['password'];
  for ($i=0; $i<1000; $i++)
    $entered_hash = hash('sha512', $entered_hash . $i . $_POST['password']);
  if ($saved_hash != $entered_hash) {
    print '<font color="ff0000">Wrong password: ' . $entered_hash . '</font><br>' . "\n";
    return;
  }
  foreach ($_POST as $key => $dummy)
    if (preg_match('/^push_([0-9]+)$/', $key, $duration))
      break;
  if (count($duration) != 2)
    return;
  $duration = $duration[1];
  if (($duration > 0)
  && ($duration < 300)) {
    print 'pushing power button for ' . ($duration * 0.1) . ' seconds ... <br>' . "\n";
    flush();
    shell_exec('#BINDIR#/lights-out push power ' . ($duration * 0.1));
    print '... done<br>' . "\n";
  }
}

print '<html>' . "\n";
print '<head>' . "\n";
print '<title>fenster lights-out</title>' . "\n";
if (array_key_exists('password', $_POST))
  print '<meta http-equiv="refresh" content="1;/">' . "\n";
print '</head>' . "\n";
print '<body>' . "\n";
print 'Fenster is currently ';
if ($power == '0')
  print '<font color="ff0000">off</font>';
else
  print '<font color="00ff00">on</font>';
print '<br>' . "\n";
if ($https) {
  push_it();
  print '<form action="" method="post">' . "\n";
  print 'Passwort: <input name="password" type="password"><br>' . "\n";
  print '<input type="submit" name="push_1" value="short power button push"><br>' . "\n";
  print '<input type="submit" name="push_150" value="long power button push"><br>' . "\n";
  print '</form>' . "\n";
}
print '</body>' . "\n";
print '</html>' . "\n";