From 53cfa13fdf285fa9636c9175f10bd079075a0a22 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Tue, 1 Oct 2024 11:20:24 +0200
Subject: [PATCH] experimen tell: delete server logs

---
 classes/cronlog-renderer.class.php |  6 +++++-
 classes/cronlog.class.php          | 30 ++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/classes/cronlog-renderer.class.php b/classes/cronlog-renderer.class.php
index 8a6ddd6..b7b9fb3 100644
--- a/classes/cronlog-renderer.class.php
+++ b/classes/cronlog-renderer.class.php
@@ -66,6 +66,10 @@ class cronlogrenderer extends cronlog
         return '<div class="accessandage">'
             . sprintf($this->t("request-time"), date("Y-m-d H:i:s")) . '<br>'
             . sprintf($this->t("last-entry"), $sAge)
+            .($iAge > 60*24
+                ? " <a href=\"?deleteserverlogs=$this->_sActiveServer\" class=\"btn btn-danger\">Delete server</a>"
+                : ''
+            )
             . '</div>'
         ;
     }
@@ -594,7 +598,7 @@ class cronlogrenderer extends cronlog
               var timeline = new vis.Timeline(container, items, options);
 
               // set focus to newest jobs
-              timeline.moveTo("'.date("Y-m-d H:i:s", time()-60*60*24*4).'");
+              // timeline.moveTo("'.date("Y-m-d H:i:s", time()-60*60*24*4).'");
               
               // fix: some timelines do not properly work ... but I make them visible
               $(\'#' . $sDivId . ' .vis-timeline\').css(\'visibility\', \'visible\');
diff --git a/classes/cronlog.class.php b/classes/cronlog.class.php
index 0670aa7..2340d1f 100644
--- a/classes/cronlog.class.php
+++ b/classes/cronlog.class.php
@@ -147,6 +147,10 @@ class cronlog
         $this->_sDataDir = str_replace("__APPDIR__", dirname(dirname(__FILE__)), $this->_sDataDir);
         $this->_sDataDir = str_replace('\\', '/', $this->_sDataDir);
 
+        if (isset($_GET['deleteserverlogs']) && $_GET['deleteserverlogs']){
+            $this->_deleteServerlogs($_GET['deleteserverlogs']);
+        }
+
         $this->getServers();
     }
 
@@ -155,6 +159,32 @@ class cronlog
     // ----------------------------------------------------------------------
 
 
+    protected function _deleteServerlogs(string $sServer): bool
+    {
+        $dirCronlog="$this->_sDataDir/$sServer";
+
+        if (! is_dir($dirCronlog)) {
+            // throw new InvalidArgumentException("$dirCronlog must be a directory");
+            return false;
+        }
+        if (substr($dirCronlog, strlen($dirCronlog) - 1, 1) != '/') {
+            $dirCronlog .= '/';
+        }
+        $files = array_merge(
+            glob($dirCronlog . '*', GLOB_MARK),
+            glob($this->_getCacheDir() . "/*$sServer", GLOB_MARK)
+        );
+        foreach ($files as $file) {
+            /*
+            if (is_dir($file)) {
+                deleteDir($file);
+            } else {
+            */
+            @unlink($file);
+            //}
+        }
+        return @rmdir($dirCronlog);
+    }
     /**
      * chaching: get the full path of directory for caching
      * @return string
-- 
GitLab