summaryrefslogtreecommitdiff
path: root/computer-time-limit.php
blob: b81081de57e4d0b01d63697dbe8a665388e830d1 (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
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
<?php

$db = new SQLite3('../backstage/computer-time-limit/computer-time-limit.sqlite');
$now = time();

function log_to_file($line) {
  $handle = fopen('../backstage/computer-time-limit/log', 'a');
  fwrite($handle, $now . ' ' . $line . "\n");
  fclose($handle);
}

if (array_key_exists('msg', $_POST) && array_key_exists('sig', $_POST)) {

  $sig_file = tempnam('/tmp', 'ctl-sig');
  $h = fopen($sig_file, 'w');
  fwrite($h, base64_decode($_POST['sig']) . "\n");
  fclose($h);

  $msg_file = tempnam('/tmp', 'ctl-msg');
  $h = fopen($msg_file, 'w');
  fwrite($h, $_POST['msg'] . "\n");
  fclose($h);

  $key = trim(shell_exec('sed -n "1 s@^.* \([a-zA-Z]\+\)\.pub\$@\1@; T; p" ' . $sig_file));

  $erg = shell_exec('signify -V -p ../backstage/computer-time-limit/keys/' . $key . '.pub -x ' . $sig_file . ' -m ' . $msg_file . ' 2>&1; echo $?');

  if ($erg != 'Signature Verified' . "\n" . '0' . "\n") {
    print($erg);
    die();
  }

  unlink($sig_file);
  unlink($msg_file);

  $result = $db->query('SELECT * FROM `computer_time` WHERE `name`="' . $key . '"');
  $row = $result->fetchArray();
  if (!$row) {
    print($key . ' is not known.');
    die();
  }
  $aktiv = $row['aktiv'];
  $bis = strtotime($row['bis']);
  $von = strtotime($row['von']);
  log_to_file('from_db ' . $key . ' ' . $_POST['msg'] . ' ' . $aktiv . ' ' . $von . ' ' . $bis);
  if ($bis < $von) {
    $aktiv = 0;
    $bis = $von;
  }
  $bis = $bis + 30*60*(floor($now/60/60/24) - floor($von/60/60/24));
  if ($aktiv) {
    $von = $now;
  }
  $noch = $bis - $von;
  if ($noch < 0) {
    $noch = 0;
  }
  if ($noch > 4*60*60) {
    $noch = 4*60*60;
  }

  print($noch . "\n");
  switch ($_POST['msg']) {
    case 'start':
      $aktiv = 1;
      break;
    ;;
    case 'stop':
      $aktiv = 0;
      break;
    ;;
    default:
      print($_POST['msg'] . ' unknown');
      die();
    ;;
  }
  if ($noch == 0) {
    $aktiv = 0;
  }
  $von = date('Y-m-d H:i:s', $now);
  $bis = date('Y-m-d H:i:s', $now + $noch);
  log_to_file('to_db ' . $key . ' ' . $aktiv . ' ' . $von . ' ' . $bis);
  $db -> exec(
    'UPDATE `computer_time`' .
    ' SET `aktiv`=' . $aktiv . ',' .
    '`von`="' . $von . '",' .
    '`bis`="' . $bis . '"' .
    ' WHERE `name`="' . $key . '"');
  die();
}

$result = $db->query('select * from `computer_time`');

?><html><body><table>
<?php

while ($row = $result->fetchArray()) {

  $bis = strtotime($row['bis']);
  $von = strtotime($row['von']);
  if ($row['aktiv']) {
    $von = $now;
  }
  $noch = $bis - $von;

  ?>  <tr><td><?php print($row['name']); ?></td><td><?php

  print(date('Y-m-d H:i:s', $bis));
  print(' (');
  print($noch);
  print(')');

  ?></td><td><?php

    if ($row['aktiv']) {
      print('eingeloggt');
    } else {
      print('&nbsp;');
    }

  ?></td></tr>
<?php
}

?></table></body></html>