forked from albsierra/codetest
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpanic-restart.php
More file actions
69 lines (56 loc) · 2 KB
/
panic-restart.php
File metadata and controls
69 lines (56 loc) · 2 KB
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
<?php
//https://localhost/tsugi/mod/codetest/panic-restart.php
$token = $_GET['token'] ?? null;
$now = date('ymdhis');
$base_token_path = '/tmp';
//clean old token files
$handle = opendir($base_token_path);
while (false !== ($entry = readdir($handle))) {
$query = 'token-';
if(substr($entry, 0, strlen($query)) === $query){
$date = file_get_contents("$base_token_path/$entry");
if($now >= $date){
unlink("$base_token_path/$entry");
}
}
}
closedir($handle);
if($token == null){
//---------------------------- BASIC AUTH ----------------------------------
$AUTH_USER = 'admin';
$AUTH_PASS = (getenv('TSUGI_PANIC_RESTART_PASSWORD') ?: 'panic');
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
$is_not_authenticated = (
!$has_supplied_credentials ||
$_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
$_SERVER['PHP_AUTH_PW'] != $AUTH_PASS
);
if ($is_not_authenticated) {
header('HTTP/1.1 401 Authorization Required');
header('WWW-Authenticate: Basic realm="Access denied"');
exit;
}
//--------------------------------------------------------------------------
$token = substr(base64_encode(mt_rand()), 0, 15);
$min5 = date('ymdhis', strtotime("+5 minutes"));
$token_path = "$base_token_path/token-$token";
file_put_contents($token_path, $min5);
$proto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? $_SERVER['REQUEST_SCHEME'];
$msg = "{$proto}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}?token=$token";
//echo "MSG: <a href='$msg'>$msg</a><br/>";
//echo "TO: ".getenv('TSUGI_PANIC_RESTART_EMAIL')."<br/>";
// send email
mail(getenv('TSUGI_PANIC_RESTART_EMAIL'), "Reset password: {$_SERVER['HTTP_HOST']}", $msg);
echo "Enviado";
} else {
$token_path = "$base_token_path/token-$token";
if(file_exists($token_path)){
$date = file_get_contents($token_path);
unlink($token_path);
if($now < $date){
echo "OK";
file_put_contents("/var/pipe/hostpipe", 'restart');
}
}
}