This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
/
controller.php
197 lines (155 loc) · 5.04 KB
/
controller.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
<?php
require_once "includes/core.php";
/*
Requirements:
- WiringPi Library
- WakeOnLan
- Autostart to this page
- Apache, MySQL, PHP -> Start all as Service
- php.ini set execution-timelimit = 40 seconds
*/
// Only open for localhost
if ($SYSTEM["public_server_interface"] == 0 && $_SERVER["REMOTE_ADDR"] != "127.0.0.1")
die("Unauthorisierter Zugriff");
// If a key has been read:
if (isset($_POST["key"])) {
$key = $_POST["key"];
// No valid key: Length or numeric
if (strlen($key) != $SYSTEM["key_length"] || !is_numeric($key))
exit;
// If there is a pending Webinterface-request:
if ($SYSTEM["active_timestamp"] + 30 > time()) {
// If a certain card is requested:
if ($SYSTEM["active_certain_key"] == $key) {
// Seems like the user wants to log in:
systemRewrite("active_certain_key", "0");
exit;
}
// If any card is requested:
if ($SYSTEM["active_any_key"] == "0") {
systemRewrite("active_any_key", $_POST["key"]);
exit;
}
}
// check for the user:
$id = mysqli_fetch_array(mysqli_query($CONNECTION, 'SELECT id FROM pb_users WHERE card_id = "' . $key . '" LIMIT 1'), MYSQLI_ASSOC);
$benutzer = new user($id["id"]);
// If in Lockdown and the user is not an admin: Cancel
if ($SYSTEM["lockdown"] && $benutzer->data["admin"] != 1)
exit;
// If the card is dedicaded to switch the alarm:
if ($benutzer->data["deactivate_alarm"] == 1) {
writeLog(4, false, $benutzer->data["id"]);
if ($SYSTEM["alarm_once_timeout"] == 1)
systemRewrite("alarm_once_timeout", 0);
else
systemRewrite("alarm_once_timeout", 1);
exit;
}
// Here comes the hardware part: Call the commands:
$helper = $benutzer->getCurrentActions(true);
if ($helper === false) {
// Per_count was used up or card not active:
writeLog(2, false, $benutzer->data["id"]);
exit;
}
foreach ($helper as $command_id) {
callCommand($command_id);
}
// And log the success
writeLog(2, true, $benutzer->data["id"]);
exit;
}
// If the alarm has to go off
if (isset($_POST["alarm"]) && $_POST["alarm"] == 1) {
// if the alarm is deactivated permanently:
if ($SYSTEM["alarm_active"] == 0)
exit;
// if the alarm is not today:
$alarm_days = explode(",", $SYSTEM["alarm_days"]);
if (!in_array(date('N'), $alarm_days))
exit;
// If the alarm is deactivated once:
if ($SYSTEM["alarm_once_timeout"] == 1) {
systemRewrite("alarm_once_timeout", 0);
exit;
}
// Everthing is fine:
/*$alarm_commands = explode(",", $SYSTEM["alarm_commands"]);
foreach ($alarm_commands as $command) {
callCommand($command);
}*/
callCommand($SYSTEM["alarm_commands"]);
writeLog(4, true);
die("OK");
}
// Autostart the needed Commands
$autostarts = mysqli_query($CONNECTION, "SELECT id FROM pb_commands WHERE autostart = 1");
while ($command = mysqli_fetch_array($autostarts, MYSQLI_ASSOC)) {
callCommand($command["id"]);
}
// Get the Time:
$to_alarm = (($SYSTEM["alarm_time"] * 60) - ((date('G') * 3600) + (date('i') * 60) + date('s')));
if ($to_alarm < 0)
$to_alarm = $to_alarm + 86400; // add one day in sec
// If the alarm is deactivated (Sorry for the calculations beforehand)
if ($SYSTEM["alarm_active"] == 0)
$to_alarm = 0;
$include_js_vars = 'var key_length = ' . $SYSTEM["key_length"] . ';
var browser_reload = ' . ($SYSTEM["browser_reload"] * 60000) . ';
var to_alarm = ' . ($to_alarm * 1000) . ';';
// Write into the log:
if (!isset($_GET["reload"]))
writeLog(0, true);
else
writeLog(1, true);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Internal System</title>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript"><?php echo $include_js_vars; ?>
var timer;
$(document).ready(function(){
// Set the timeout for the reload
if (browser_reload) {
window.setTimeout(function() {
window.location.assign("http://127.0.0.1/controller.php?reload=1");
}, browser_reload);
}
// is the audio-element available?
if ($('audio').length && to_alarm) {
// Set the timeout for the alarm clock:
window.setTimeout(function() {
$.post("controller.php", {"alarm": 1}, function(data) {
// Play the audio
if (data == "OK")
$('audio').get(0).play();
});
}, to_alarm);
}
});
// Function, that is called every time a change has been made
function submitCard() {
var inhalt = $('#key').val();
if (inhalt.length == key_length) {
// Key complete and can be sent:
$.post("controller.php", {"key": inhalt});
}
// And clear it, after there was 1 second, where no input was made:
if (timer)
clearTimeout(timer);
timer = setTimeout(function() {$('#key').val("");}, 1000);
}
</script>
</head>
<body>
<input type="text" id="key" oninput="submitCard();" autofocus />
<?php
if (file_exists("wecker.mp3"))
echo '<audio src="wecker.mp3" preload="auto">Not supported</audio>';
?>
</body>
</html>