]> 91.132.146.200 Git - scientia.git/commitdiff
better logging and some DB changes
authorBanana <mail@bananas-playground.net>
Wed, 11 Nov 2026 21:01:23 +0000 (22:01 +0100)
committerBanana <mail@bananas-playground.net>
Wed, 11 Nov 2026 21:01:23 +0000 (22:01 +0100)
Signed-off-by: Banana <mail@bananas-playground.net>
13 files changed:
CHANGELOG
TODO
documentation/scientia.sql.txt
documentation/upgrade-from-1.0.md
documentation/upgrade-from-1.1.md [new file with mode: 0644]
webroot/add.php
webroot/config/config.php.default
webroot/get.php
webroot/index.php
webroot/lib/entry.class.php
webroot/lib/summoner.class.php
webroot/log/.gitignore [new file with mode: 0644]
webroot/systemout/.gitignore [deleted file]

index a73eacb66dff92953af6413ac7780883bb17403c..d2c4611b00bb96fe2e7ebd68cc731daf2fc8a945 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,7 @@
 1.x Eos -
+    + Config change. Important breaking changes. See upgrade-from-1.1.md for more details.
     + Updated: README.md
+    + Updated: upgrade-from-1.0.md file for better reading
 
 1.1 Atlas - 2024-06-29
     + Config change: i18n. See upgrade-from-1.0.md for more details.
diff --git a/TODO b/TODO
index 88b4c1ee18f43575b61b37694fd97e906eb0da1d..4e95e3824d8389521a20a27169da75cf5e097a32 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,7 +1,6 @@
 + search
 ++ filter by date and if available by category
 + categories with colors
-+ PHP better logging
 + PHP Better API
 + Go client improvements
 ++ local storage and sync
index 7792dd09b3e36d47d5b111150a8402f5b5d00a66..90217303d5046047135918bf39a5fe4fa3b8250b 100644 (file)
@@ -1,11 +1,11 @@
 CREATE TABLE `sc_entry` (
-  `ident` char(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
+  `ident` char(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NOT NULL,
   `date` date NOT NULL,
   `created` datetime NOT NULL,
   `modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-  `body` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
-  `words` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
+  `body` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
+  `words` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
   PRIMARY KEY (`ident`) USING BTREE,
   KEY `date` (`date`),
   FULLTEXT KEY `words` (`words`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
index 3a9fa07095cce8c2d65f3b152ad893b4a58d7cfc..1b2fa8a6282ac93671c934ba43ad10a195f7c8d4 100644 (file)
@@ -3,7 +3,7 @@
 Update your existing `config.php` file with the following changes.
 Missing those changes, errors will happen.
 
-# i18n
+## i18n
 New config for i18n. Open config.php and add the following:
 ```
 # language settings
@@ -11,7 +11,7 @@ const FRONTEND_LANGUAGE = 'en';
 ```
 currently only en (default) and de are available.
 
-#  Installation URL
+##  Installation URL
 
 To provide the correct URL please add this.
 
diff --git a/documentation/upgrade-from-1.1.md b/documentation/upgrade-from-1.1.md
new file mode 100644 (file)
index 0000000..be95e9a
--- /dev/null
@@ -0,0 +1,27 @@
+# Config changes
+
+Update your existing `config.php` file with the following changes.
+Missing those changes, errors will happen. See `config.php.default` for defaults.
+
+## Constant rename
+
+Rename `PATH_SYSTEMOUT` to `PATH_LOG`.
+
+## Constant addition
+
+Add the following line to your config file: `const LOGFILE = PATH_LOG.'/scientia.log';`
+
+## Log directory rename
+
+Rename the directory `systemout` to `log`.
+
+# Database changes
+
+Run the following SQL to alter your existing database.
+
+```
+ALTER TABLE `sc_entry` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
+ALTER TABLE `sc_entry` CHANGE `ident` `ident` CHAR(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NOT NULL;
+ALTER TABLE `sc_entry` CHANGE `words` `words` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL;
+ALTER TABLE `sc_entry` CHANGE `body` `body` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL;
+```
index 255dd4413d29b2c1cbf67ebb53569aa6b57f3d86..46e7f6d251d16c970b172d918e786353ae41b32f 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * scientia
  *
- * Copyright 2023 - 2024 Johannes Keßler
+ * Copyright 2023 - 2026 Johannes Keßler
  *
  * https://www.bananas-playground.net/projekt/scientia/
  *
@@ -42,7 +42,6 @@ require_once('config/config.php');
 
 ## set the error reporting
 ini_set('log_errors',true);
-ini_set('error_log',PATH_SYSTEMOUT.'/error.log');
 if(DEBUG === true) {
     ini_set('display_errors',true);
 }
@@ -56,22 +55,22 @@ date_default_timezone_set(TIMEZONE);
 # required libs
 require_once('lib/summoner.class.php');
 
-if(DEBUG) error_log("Dump SERVER ".var_export($_SERVER,true));
+if(DEBUG) Summoner::sysLog("[DEBUG] Dump SERVER ".Summoner::cleanForLog($_SERVER));
 ## check if request is valid
 $_create = false;
 $filteredData = '';
 if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE'] === 'application/json; charset=UTF-8') {
     $payload = json_decode(file_get_contents('php://input'), true);
-    if(DEBUG) error_log("[DEBUG] Dump payload ".var_export($payload,true));
+    if(DEBUG) Summoner::sysLog("[DEBUG] Dump payload ".Summoner::sysLog($payload));
     if(!empty($payload)) {
         if(isset($payload['asl']) && !empty($payload['asl'])
             && isset($payload['data']) && !empty($payload['data'])
             && isset(UPLOAD_SECRET[$payload['asl']])
         ) {
-            if(DEBUG) error_log("[DEBUG] Valid payload so far");
+            if(DEBUG) Summoner::sysLog("[DEBUG] Valid payload so far");
             $filteredData = filter_var($payload['data'],FILTER_SANITIZE_FULL_SPECIAL_CHARS);
             if(!empty($filteredData)) {
-                if(DEBUG) error_log("[DEBUG] Validated payload");
+                if(DEBUG) Summoner::sysLog("[DEBUG] Validated payload");
                 $_create = true;
             }
         }
@@ -99,7 +98,7 @@ if($_create === false) {
 $DB = new mysqli(DB_HOST, DB_USERNAME,DB_PASSWORD, DB_NAME);
 if ($DB->connect_errno) exit('Can not connect to MySQL Server');
 $DB->set_charset("utf8mb4");
-$DB->query("SET collation_connection = 'utf8mb4_bin'");
+$DB->query("SET collation_connection = 'utf8mb4_0900_ai_ci'");
 $driver = new mysqli_driver();
 $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
 
@@ -107,12 +106,12 @@ require_once 'lib/entry.class.php';
 $Entry = new Entry($DB);
 $do = $Entry->create($filteredData);
 if(!empty($do)) {
-    $contentBody['message'] = INSTALL_URL . PATH_WEBROOT . date('/Y/m/d/').$do;
+    $contentBody['message'] = INSTALL_URL.PATH_WEBROOT.date('/Y/m/d/').$do;
 }
 else {
-    $hash = md5($do.time());
-    error_log("[ERROR] $hash Can not create. ". var_export($do,true));
-    $contentBody['message'] = "Something went wrong. $hash";
+    if(DEBUG) Summoner::sysLog("[DEBUG] Can not create new entry ".Summoner::cleanForLog($filteredData));
+    Summoner::sysLog("[ERROR] Can not create new entry.");
+    $contentBody['message'] = "Something went wrong.";
     $contentBody['status'] = 500;
 }
 
index 7c7bec76f51e6a8f8899eb92b3261d8525b7ee33..9ffa90895a07fff5f9494e156b64a050de57db1f 100644 (file)
@@ -33,8 +33,9 @@ const FRONTEND_LANGUAGE = 'en';
 
 # path settings
 const PATH_ABSOLUTE = '/path/to/scientia/webroot';
-const PATH_SYSTEMOUT = PATH_ABSOLUTE . '/systemout';
+const PATH_LOG = PATH_ABSOLUTE . '/log';
 const PATH_WEBROOT = '/code/scientia/webroot';
+const LOGFILE = PATH_LOG.'/scientia.log';
 
 # Installation Domain. Webrootpath will be added automatically
 const INSTALL_URL = 'http://www.example.tld';
index 7f9f90fad573f21391f6636f45d9f7404349cc21..fc5556304ef40025901160ebedfcdf0cc9366598 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * scientia
  *
- * Copyright 2023 - 2024 Johannes Keßler
+ * Copyright 2023 - 2026 Johannes Keßler
  *
  * https://www.bananas-playground.net/projekt/scientia/
  *
@@ -42,7 +42,6 @@ require_once('config/config.php');
 
 ## set the error reporting
 ini_set('log_errors',true);
-ini_set('error_log',PATH_SYSTEMOUT.'/error.log');
 if(DEBUG === true) {
     ini_set('display_errors',true);
 }
@@ -88,7 +87,7 @@ $contentBody = array (
 $DB = new mysqli(DB_HOST, DB_USERNAME,DB_PASSWORD, DB_NAME);
 if ($DB->connect_errno) exit('Can not connect to MySQL Server');
 $DB->set_charset("utf8mb4");
-$DB->query("SET collation_connection = 'utf8mb4_bin'");
+$DB->query("SET collation_connection = 'utf8mb4_0900_ai_ci'");
 $driver = new mysqli_driver();
 $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
 
index 25afdcc826d87115f9536b276515b7f5981c84f7..217afb57f581d58f68b83bd43b4dddd0c68590a7 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * scientia
  *
- * Copyright 2023 - 2024 Johannes Keßler
+ * Copyright 2023 - 2026 Johannes Keßler
  *
  * https://www.bananas-playground.net/projekt/scientia/
  *
@@ -38,7 +38,6 @@ require_once('config/config.php');
 
 ## set the error reporting
 ini_set('log_errors',true);
-ini_set('error_log',PATH_SYSTEMOUT.'/error.log');
 if(DEBUG === true) {
     ini_set('display_errors',true);
 }
@@ -62,7 +61,7 @@ $i18n = new I18n();
 $DB = new mysqli(DB_HOST, DB_USERNAME,DB_PASSWORD, DB_NAME);
 if ($DB->connect_errno) exit('Can not connect to MySQL Server');
 $DB->set_charset("utf8mb4");
-$DB->query("SET collation_connection = 'utf8mb4_bin'");
+$DB->query("SET collation_connection = 'utf8mb4_0900_ai_ci'");
 $driver = new mysqli_driver();
 $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
 
index c5ea20729f106240fea81602d34bc3ddfa268217..3cb6297af3cf211dcf9c0287864ee1f6d40d6500 100644 (file)
@@ -60,14 +60,14 @@ class Entry {
                         `ident` = '".$this->_DB->real_escape_string($_ident)."',
                         `body` = '".$this->_DB->real_escape_string($data)."',
                         `words` = '".$this->_DB->real_escape_string($_words)."'";
-        if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
+        if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
 
         try {
             $this->_DB->query($queryStr);
             $ret = $_ident;
         }
         catch(Exception $e) {
-            error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
+            Summoner::sysLog("[ERROR] ".__METHOD__." catch: ".Summoner::cleanForLog($e->getMessage()));
         }
 
         return $ret;
@@ -90,7 +90,7 @@ class Entry {
                             FROM `".DB_PREFIX."_entry`
                             WHERE `ident` = '".$this->_DB->real_escape_string($id)."'
                                 AND `date` = '".$this->_DB->real_escape_string($y.'-'.$m.'-'.$d)."'";
-            if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
+            if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
             try {
                 $query = $this->_DB->query($queryStr);
                 if($query !== false && $query->num_rows > 0) {
@@ -98,7 +98,7 @@ class Entry {
                 }
             }
             catch(Exception $e) {
-                error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
+                Summoner::sysLog("[ERROR] ".__METHOD__." catch: ".Summoner::cleanForLog($e->getMessage()));
             }
         }
 
@@ -119,7 +119,7 @@ class Entry {
             $queryStr = "SELECT `ident`,`date`,`body`
                             FROM `".DB_PREFIX."_entry`
                             WHERE `ident` = '".$this->_DB->real_escape_string($id)."'";
-            if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
+            if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
             try {
                 $query = $this->_DB->query($queryStr);
                 if($query !== false && $query->num_rows > 0) {
@@ -127,7 +127,7 @@ class Entry {
                 }
             }
             catch(Exception $e) {
-                error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
+                Summoner::sysLog("[ERROR] ".__METHOD__." catch: ".Summoner::cleanForLog($e->getMessage()));
             }
         }
 
@@ -150,13 +150,13 @@ class Entry {
                             `body` = '".$this->_DB->real_escape_string($data)."',
                             `words` = '".$this->_DB->real_escape_string($_words)."'
                             WHERE `ident` = '".$this->_DB->real_escape_string($id)."'";
-            if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
+            if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
             try {
                 $this->_DB->query($queryStr);
                 $ret = $id;
             }
             catch(Exception $e) {
-                error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
+                Summoner::sysLog("[ERROR] ".__METHOD__." catch: ".Summoner::cleanForLog($e->getMessage()));
             }
         }
 
@@ -175,13 +175,13 @@ class Entry {
         if(!empty($id)) {
             $queryStr = "DELETE FROM `".DB_PREFIX."_entry`
                             WHERE `ident` = '".$this->_DB->real_escape_string($id)."'";
-            if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
+            if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
             try {
                 $this->_DB->query($queryStr);
                 $ret = true;
             }
             catch(Exception $e) {
-                error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
+                Summoner::sysLog("[ERROR] ".__METHOD__." catch: ".Summoner::cleanForLog($e->getMessage()));
             }
         }
 
@@ -212,13 +212,13 @@ class Entry {
         $queryStr .= " ORDER BY `created` DESC";
         $queryStr .= " LIMIT $limit";
 
-        if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
+        if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
         try {
             $query = $this->_DB->query($queryStr);
             $ret = $query->fetch_all(MYSQLI_ASSOC);
         }
         catch(Exception $e) {
-            error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage());
+            Summoner::sysLog("[ERROR] ".__METHOD__." catch: ".Summoner::cleanForLog($e->getMessage()));
         }
 
         return $ret;
@@ -229,7 +229,6 @@ class Entry {
      *
      * @param $data string
      * @return array
-     * @todo ignores
      *
      */
     private function _words(string $data): array {
index dcbe8ddc5957c0604a97d0d2f5139114fd050221..c0bd02fe1a68812cb7b58dbc13376a9b9dcb4141 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * scientia
  *
- * Copyright 2023 - 2024 Johannes Keßler
+ * Copyright 2023 - 2026 Johannes Keßler
  *
  * https://www.bananas-playground.net/projekt/scientia/
  *
@@ -126,35 +126,6 @@ class Summoner {
         return $ret;
     }
 
-    /**
-     * check if a string starts with a given string
-     *
-     * @param string $haystack
-     * @param string $needle
-     * @return boolean
-     */
-    static function startsWith(string $haystack, string $needle): bool {
-        $length = strlen($needle);
-        return (substr($haystack, 0, $length) === $needle);
-    }
-
-    /**
-     * check if a string ends with a given string
-     *
-     * @param string $haystack
-     * @param string $needle
-     * @return boolean
-     */
-    static function endsWith(string $haystack, string $needle): bool {
-        $length = strlen($needle);
-        if ($length == 0) {
-            return true;
-        }
-
-        return (substr($haystack, -$length) === $needle);
-    }
-
-
     /**
      * create a short string based on a integer
      *
@@ -206,4 +177,26 @@ class Summoner {
             exit;
         }
     }
+
+    /**
+     * Make the input more safe for logging
+     *
+     * @param mixed $input The string to be made more safe
+     * @return string
+     */
+    static function cleanForLog(mixed $input): string {
+        $input = var_export($input, true);
+        $input = preg_replace( "/[\t\n\r]/", " ", $input);
+        return addcslashes($input, "\000..\037\177..\377\\");
+    }
+
+    /**
+     * error_log with a dedicated destination
+     * Uses LOGFILE const
+     *
+     * @param string $msg The string to be written to the log
+     */
+    static function sysLog(string $msg): void {
+        error_log(date("c")." ".self::cleanForLog($_SERVER['REMOTE_ADDR']??'-')." ".self::cleanForLog($_SERVER['HTTP_USER_AGENT']??'-')." ".$msg."\n", 3, LOGFILE);
+    }
 }
diff --git a/webroot/log/.gitignore b/webroot/log/.gitignore
new file mode 100644 (file)
index 0000000..397b4a7
--- /dev/null
@@ -0,0 +1 @@
+*.log
diff --git a/webroot/systemout/.gitignore b/webroot/systemout/.gitignore
deleted file mode 100644 (file)
index 397b4a7..0000000
+++ /dev/null
@@ -1 +0,0 @@
-*.log