Skip to content

Commit 65408fe

Browse files
committed
Reworked shutdown handling
Use new command to retrieve system information about power events in future but keep old command for compatibility reasons Fixed bug concerning authentication 🐛
1 parent 74c2dd7 commit 65408fe

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

backend/serv.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,27 @@
3131
}
3232
exit();
3333
}
34+
function getShutdownEventsInfo(){
35+
// system("date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)");
36+
// old command which is not very comfortable in order to retrieve which event is scheduled (poweroff vs reboot)
37+
// For now use old one for compatibility reasons which I dont know so far
38+
$return=array();
39+
$output2=shell_exec("busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown");
40+
if($output2==""){
41+
$output=shell_exec("date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)");
42+
$return["date"]=$output;
43+
$return["act"]="unknown";
44+
}else{
45+
$strings=explode(" ", $output2);
46+
// The output specifies the shutdown time as microseconds since the Unix epoch -> divide by 1000
47+
//echo gettype($strings[2]);
48+
$return["date"]=round(floatval($strings[2]) / 1000); // Unix milliseconds
49+
$return["act"]=explode("\"", $strings[1])[1];
50+
}
51+
return $return;
52+
}
3453
if(isset($_GET["checkShutdown"])){
35-
system("date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)");
54+
echo json_encode(getShutdownEventsInfo());
3655
exit();
3756
}else if(isset($_GET["cancelShutdown"])){
3857
system('sudo /sbin/shutdown -c');
@@ -58,6 +77,6 @@
5877
}else{
5978
echo "false";
6079
}
61-
system("date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)");
80+
echo json_encode(getShutdownEventsInfo());
6281
}
6382
?>

js/main.js

+29-23
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,22 @@ function authorize() {
6161
return;
6262
} else {
6363
var vReq = new ntwReq("backend/serv.php?p=" + pass+"&a="+act+"&time="+time, function (data) {
64-
console.log(this.responseText);
65-
if(this.responseText.indexOf("true") > -1){
64+
console.log(data.responseText);
65+
if(data.responseText.indexOf("true") > -1){
6666
document.getElementById("currentState").innerHTML = "<div class='alert alert-success' role='alert'><i class='bi bi-check2-circle'></i>&nbsp;Authorization completed!</font>";
6767
$("#confbtn").html("<i class='bi bi-check2-circle'></i>&nbsp;Saved");
68-
var res=this.responseText.split("_");
69-
outputShutdown(res[1],act);
68+
var res=JSON.parse(data.responseText.split("true_")[1]);
69+
outputShutdown(res.date,res.act);
7070
setTimeout(function(){
7171
$("#exampleModalCenter").modal("hide");
7272
document.getElementById("pwrform").reset();
7373
$("#pwrform input, select").prop("disabled","");
7474
$("#confbtn").prop("disabled","");
7575
document.getElementById("currentState").innerHTML = "";
76-
$("#confbtn").html("Confirm identity");
76+
$("#confbtn").html("Confirm identity");
7777
},3000);
78-
}else if(this.responseText=="wrongCredentials"){
79-
document.getElementById("currentState").innerHTML = "<div class='alert alert-success' role='alert'><i class='bi bi-x-circle'></i>&nbsp;Authorization failed!</div>";
78+
}else if(data.responseText=="wrongCredentials"){
79+
document.getElementById("currentState").innerHTML = "<div class='alert alert-danger' role='alert'><i class='bi bi-x-circle'></i>&nbsp;Authorization failed!</div>";
8080
}else{
8181
document.getElementById("currentState").innerHTML = "<div class='alert alert-success' role='alert'><i class='bi bi-x-circle'></i>&nbsp;Error!</div>";
8282
}
@@ -88,12 +88,14 @@ function authorize() {
8888
function checkShutdown(callback) {
8989
document.getElementById("currentState").innerHTML='<div class="alert alert-info" role="alert"><i class="bi bi-chevron-double-right"></i>&nbsp;Checking for power events...</div>';
9090
var vReq = new ntwReq("backend/serv.php?checkShutdown", function (data) {
91-
if(this.responseText==""){
91+
console.log(data.responseText);
92+
var res=JSON.parse(data.responseText);
93+
if( (res.act=="") || (res.date==null) ){
9294
document.getElementById("sys2").innerHTML="";
9395
shutdownCurrent=false;
9496
}else{
9597
shutdownCurrent=true;
96-
outputShutdown(this.responseText,"unknown");
98+
outputShutdown(res.date,res.act);
9799
}
98100
if(callback !== undefined){
99101
callback();
@@ -115,8 +117,8 @@ function cancelShutdown(force) {
115117
return;
116118
}
117119
var vReq = new ntwReq("backend/serv.php?cancelShutdown", function (data) {
118-
console.log(this.responseText);
119-
if(this.responseText==""){
120+
console.log(data.responseText);
121+
if(data.responseText==""){
120122
console.log("Cancel response is empty");
121123
mdtoast('<i class="bi bi-check2-circle"></i>&nbsp;Power event was cancelled!', { type: 'success'});
122124
checkShutdown();
@@ -129,12 +131,21 @@ function cancelShutdown(force) {
129131

130132
var dobj={Mon: "Monday", Tue: "Tuesday", Wed: "Wednesday", Thu: "Thursday", Fri: "Friday", Sat: "Saturday", Sun: "Sunday"};
131133
function outputShutdown(data,act) {
132-
var toParse=data.split(" CEST ")[0];
133-
var day=data.substring(0,3);
134-
var s = data.replace(day,dobj[day]);
135-
s=s.split(" ")[0]+" "+s.split(" ")[1]+" "+s.split(" ")[2]+", "+s.split(" ")[5]+" "+s.split(" ")[3];
136-
scheduled=Date.parse(s);
137-
d = new Date(scheduled);
134+
if(typeof data !== "number"){ // for compatibility reasons
135+
data=data.replace("\n","");
136+
console.log("Trying to process old info...");
137+
var day=data.substring(0,3);
138+
var s = data.replace(day,dobj[day]);
139+
console.log(s);
140+
s=s.split(" ")[0]+", "+s.split(" ")[3]+" "+s.split(" ")[1]+", "+s.split(" ")[6]+" "+s.split(" ")[4];
141+
console.log(s);
142+
scheduled=Date.parse(s);
143+
d = new Date(scheduled);
144+
}else{
145+
d = new Date(data);
146+
}
147+
148+
138149
var restd = Math.floor((d.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
139150
var resth = Math.floor((d.getTime() - Date.now()) / (1000 * 60 * 60)) % 24;
140151
var restm = Math.floor((d.getTime() - Date.now()) / (1000 * 60)) % 60;
@@ -150,12 +161,7 @@ function outputShutdown(data,act) {
150161
}
151162
if(str==""){ str="<font class='text-danger'>&lt; 1 min</font>"; }
152163
console.log(str);
153-
var action = (act=="1") ? "shutdown" : "reboot";
154-
if(act=="unknown"){
155-
action="shutdown/reboot";
156-
}
157-
var c =toParse.split(" ");
158-
document.getElementById("sys2").innerHTML='<div class="alert alert-warning" role="alert"><button class="btn btn-sm btn-outline-danger" onclick="cancelShutdown()" style="float:right">Cancel</button>Planned to '+action+' at <kbd>'+c[3]+'</kbd> on <kbd>'+c[0]+', '+c[1]+' '+c[2]+'</kbd><br>Remaining time: <kbd>'+str+'</kbd><br></div>';
164+
document.getElementById("sys2").innerHTML='<div class="alert alert-warning" role="alert"><button class="btn btn-sm btn-outline-danger" onclick="cancelShutdown()" style="float:right">Cancel</button>Scheduled power event: <kbd>'+act+'</kbd><br>Remaining time: <kbd>'+str+'</kbd><br></div>';
159165
}
160166

161167
function shutdown(){

0 commit comments

Comments
 (0)