Browse Source

Merge pull request #719 from gdarko/bugfix-server-restart-454

Fix: Infinite restart loop when server restart is invoked from the UI
Raphael Schneeberger 6 years ago
parent
commit
9eaa6caa12
2 changed files with 22 additions and 7 deletions
  1. 21 6
      web/restart/system/index.php
  2. 1 1
      web/templates/admin/list_services.html

+ 21 - 6
web/restart/system/index.php

@@ -11,13 +11,28 @@ if ((!isset($_GET['token'])) || ($_SESSION['token'] != $_GET['token'])) {
     exit();
 }
 
-if ($_SESSION['user'] == 'admin') {
-    if (!empty($_GET['hostname'])) {
-        exec (HESTIA_CMD."v-restart-system yes", $output, $return_var);
-        $_SESSION['error_msg'] = 'The system is going down for reboot NOW!';
+// If the stored reset token matches the current request one it means that we need 
+// to prevent the action because the browser automatically reloaded the page when 
+// the server turned on. This will prevent duplicate restarts.
+$reset_token_dir = '/var/tmp/';
+if (isset($_GET['system_reset_token']) && is_numeric($_GET['system_reset_token'])) {
+    clearstatcache();
+    $reset_token_file = $reset_token_dir . 'hst_reset_' . $_GET['system_reset_token'];
+    if (file_exists($reset_token_file)) {
+        unlink($reset_token_file);
+        sleep(5);
+        header('location: /list/server/');
+        exit();
+    }
+    if ($_SESSION['user'] == 'admin') {
+        if (!empty($_GET['hostname'])) {
+            touch($reset_token_file);
+            $_SESSION['error_msg'] = 'The system is going down for reboot NOW!';
+            exec(HESTIA_CMD . "v-restart-system yes", $output, $return_var);
+        }
+        unset($output);
     }
-    unset($output);
 }
 
 header("Location: /list/server/");
-exit;
+exit();

+ 1 - 1
web/templates/admin/list_services.html

@@ -10,7 +10,7 @@
               <a class="data-controls do_servicerestart ui-button danger cancel" title="<?=__('Restart')?>">
                 <i class="do_servicerestart fas fa-undo status-icon red"></i>
                 <?=__('Restart')?>
-                <input type="hidden" name="servicerestart_url" value="/restart/system/?hostname=<?php echo $sys['sysinfo']['HOSTNAME'] ?>&token=<?=$_SESSION['token']?>" />
+                <input type="hidden" name="servicerestart_url" value="/restart/system/?hostname=<?php echo $sys['sysinfo']['HOSTNAME'] ?>&token=<?=$_SESSION['token']?>&system_reset_token=<?php echo time(); ?>" />
                   <div class="confirmation-text-servicerestart hidden" title="<?=__('Confirmation')?>">
                     <p class="confirmation"><?=__('RESTART_CONFIRMATION', 'Server')?></p>
                   </div>