From d6c7b502a6fc7d9ed97c8e4be017dec347fb916a Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Tue, 9 Aug 2016 21:06:02 +0200
Subject: [PATCH] Check the server has a sensible time

DokuWiki assumes that the server's time is correct. Especially page
revisions and cache handling depend on correct time. If that's not the
case it can lead to problems later (as mentioned in #1644).

This patch adds a very simple time check using the Date response header
from the DokuWiki server to our do=check mechanism.
---
 inc/infoutils.php | 39 +++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/inc/infoutils.php b/inc/infoutils.php
index fe312d13f..a124e2c95 100644
--- a/inc/infoutils.php
+++ b/inc/infoutils.php
@@ -60,7 +60,7 @@ function getVersionData(){
     //import version string
     if(file_exists(DOKU_INC.'VERSION')){
         //official release
-        $version['date'] = trim(io_readfile(DOKU_INC.'VERSION'));
+        $version['date'] = trim(io_readFile(DOKU_INC.'VERSION'));
         $version['type'] = 'Release';
     }elseif(is_dir(DOKU_INC.'.git')){
         $version['type'] = 'Git';
@@ -249,18 +249,41 @@ function check(){
         }
     }
 
-    if ($index_corrupted)
-        msg('The search index is corrupted. It might produce wrong results and most
+    if($index_corrupted) {
+        msg(
+            'The search index is corrupted. It might produce wrong results and most
                 probably needs to be rebuilt. See
                 <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
-                for ways to rebuild the search index.', -1);
-    elseif (!empty($lengths))
+                for ways to rebuild the search index.', -1
+        );
+    } elseif(!empty($lengths)) {
         msg('The search index seems to be working', 1);
-    else
-        msg('The search index is empty. See
+    } else {
+        msg(
+            'The search index is empty. See
                 <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
                 for help on how to fix the search index. If the default indexer
-                isn\'t used or the wiki is actually empty this is normal.');
+                isn\'t used or the wiki is actually empty this is normal.'
+        );
+    }
+
+    // rough time check
+    $http = new DokuHTTPClient();
+    $http->max_redirect = 0;
+    $http->timeout = 3;
+    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
+    $now = time();
+    if(isset($http->resp_headers['date'])) {
+        $time = strtotime($http->resp_headers['date']);
+        $diff = $time - $now;
+
+        if(abs($diff) < 4) {
+            msg("Server time seems to be okay. Diff: {$diff}s", 1);
+        } else {
+            msg("Your server's clock seems to be out of sync! Consider configuring a sync with a NTP server.  Diff: {$diff}s");
+        }
+    }
+
 }
 
 /**
-- 
GitLab