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
|
<?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, time() . ' ' . $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();
}
$host = $row['host'];
$bis = strtotime($row['bis']);
$von = strtotime($row['von']);
log_to_file('from_db ' . $key . ' ' . $_POST['msg'] . ' ' . $host . ' ' . $von . ' ' . $bis);
if ($bis < $von) {
$host = NULL;
$bis = $von;
}
$bis = $bis + 30*60*(floor($now/60/60/24) - floor($von/60/60/24));
if (!is_null($host)) {
$von = $now;
}
$noch = $bis - $von;
if ($noch < 0) {
$noch = 0;
}
if ($noch > 4*60*60) {
$noch = 4*60*60;
}
$msg_parts = explode(' ', $_POST['msg'], 2);
date_default_timezone_set('Europe/Berlin');
if ((1<=date('w')) && (date('w')<=5) && (date('G')<16)) {
print("0\n");
} else {
print($noch . "\n");
}
switch ($msg_parts[1]) {
case 'start':
$host = $msg_parts[0];
break;
;;
case 'stop':
if ($host == $msg_parts[0]) {
$host = NULL;
}
break;
;;
default:
print($_POST['msg'] . ' unknown');
die();
;;
}
if ($noch == 0) {
$host = NULL;
}
$von = date('Y-m-d H:i:s', $now);
$bis = date('Y-m-d H:i:s', $now + $noch);
if (is_null($host)) {
$host = 'NULL';
} else {
$host = '"' . $host . '"';
}
log_to_file('to_db ' . $key . ' ' . $host . ' ' . $von . ' ' . $bis);
$db -> exec(
'UPDATE `computer_time`' .
' SET `host`=' . $host . ',' .
'`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 (!is_null($row['host'])) {
$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 print($row['host']); ?></td></tr>
<?php
}
?></table></body></html>
|