From bd2d5060b733285456a565266f6bb4736a208981 Mon Sep 17 00:00:00 2001 From: Banana Date: Wed, 11 Nov 2026 22:01:23 +0100 Subject: [PATCH] better logging and some DB changes Signed-off-by: Banana --- CHANGELOG | 2 + TODO | 1 - documentation/scientia.sql.txt | 8 ++-- documentation/upgrade-from-1.0.md | 4 +- documentation/upgrade-from-1.1.md | 27 ++++++++++++++ webroot/add.php | 21 +++++------ webroot/config/config.php.default | 3 +- webroot/get.php | 5 +-- webroot/index.php | 5 +-- webroot/lib/entry.class.php | 25 ++++++------- webroot/lib/summoner.class.php | 53 ++++++++++++--------------- webroot/{systemout => log}/.gitignore | 0 12 files changed, 86 insertions(+), 68 deletions(-) create mode 100644 documentation/upgrade-from-1.1.md rename webroot/{systemout => log}/.gitignore (100%) diff --git a/CHANGELOG b/CHANGELOG index a73eacb..d2c4611 100644 --- 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 88b4c1e..4e95e38 100644 --- 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 diff --git a/documentation/scientia.sql.txt b/documentation/scientia.sql.txt index 7792dd0..9021730 100644 --- a/documentation/scientia.sql.txt +++ b/documentation/scientia.sql.txt @@ -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 diff --git a/documentation/upgrade-from-1.0.md b/documentation/upgrade-from-1.0.md index 3a9fa07..1b2fa8a 100644 --- a/documentation/upgrade-from-1.0.md +++ b/documentation/upgrade-from-1.0.md @@ -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 index 0000000..be95e9a --- /dev/null +++ b/documentation/upgrade-from-1.1.md @@ -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; +``` diff --git a/webroot/add.php b/webroot/add.php index 255dd44..46e7f6d 100644 --- a/webroot/add.php +++ b/webroot/add.php @@ -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; } diff --git a/webroot/config/config.php.default b/webroot/config/config.php.default index 7c7bec7..9ffa908 100644 --- a/webroot/config/config.php.default +++ b/webroot/config/config.php.default @@ -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'; diff --git a/webroot/get.php b/webroot/get.php index 7f9f90f..fc55563 100644 --- a/webroot/get.php +++ b/webroot/get.php @@ -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; diff --git a/webroot/index.php b/webroot/index.php index 25afdcc..217afb5 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -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; diff --git a/webroot/lib/entry.class.php b/webroot/lib/entry.class.php index c5ea207..3cb6297 100644 --- a/webroot/lib/entry.class.php +++ b/webroot/lib/entry.class.php @@ -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 { diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index dcbe8dd..c0bd02f 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -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/systemout/.gitignore b/webroot/log/.gitignore similarity index 100% rename from webroot/systemout/.gitignore rename to webroot/log/.gitignore -- 2.47.3