From fe48b7a54f5384404d76db2f272670534fb4b1ee Mon Sep 17 00:00:00 2001 From: Banana Date: Sun, 19 Nov 2023 11:42:24 +0100 Subject: [PATCH] better logging and code cleanups --- CHANGELOG | 5 ++++- client/webclient/config.default.php | 3 +++ client/webclient/index.php | 11 +++++----- documentation/setup.txt | 4 ++-- documentation/update.txt | 13 ++++++++++++ webroot/config.default.php | 12 +++++++++++ webroot/index.php | 18 ++++++---------- webroot/lib/mancubus.class.php | 6 +++--- webroot/lib/summoner.class.php | 33 +++++++++++++++++++++++++++++ 9 files changed, 82 insertions(+), 23 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ecf8598..42a674e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,7 @@ -yyyymmdd version x.x Neon +yyyymmdd version 1.x Neon +* Moved and added settings to config file. See update.txt +* Better logging +* Updated client/webclient 20231009 version 1.5 Fluorine * Maintenance release. Updated requirements to current versions if possible diff --git a/client/webclient/config.default.php b/client/webclient/config.default.php index 545ba57..e04c0cd 100644 --- a/client/webclient/config.default.php +++ b/client/webclient/config.default.php @@ -20,6 +20,9 @@ * this is the config file for the webclient */ +const DEBUG = false; +const TIMEZONE = 'Europe/Berlin'; + /* please provide a unique username for this installation */ const FRONTEND_USERNAME = 'some'; /* please provide a unique password for this installation */ diff --git a/client/webclient/index.php b/client/webclient/index.php index 81ba8bc..d315654 100644 --- a/client/webclient/index.php +++ b/client/webclient/index.php @@ -21,16 +21,15 @@ * copy the config.default.php file to config.php and update its settings */ -const DEBUG = false; -require_once 'config.php'; - # Encoding and error reporting setting mb_http_output('UTF-8'); mb_internal_encoding('UTF-8'); error_reporting(-1); // E_ALL & E_STRICT +require_once 'config.php'; + # default time setting -date_default_timezone_set('Europe/Berlin'); +date_default_timezone_set(TIMEZONE); # check request $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); @@ -40,7 +39,7 @@ if(!empty($_urlToParse)) { } } ini_set('display_errors',false); -if(DEBUG === true) { +if(DEBUG) { ini_set('display_errors',true); } @@ -146,7 +145,7 @@ function curlPostUploadCall(string $url,array $data, string $port=''): mixed { $ret = $do; } else { - error_log(var_export(curl_error($ch),true),3,'./sp-webclient.log'); + error_log(var_export(curl_error($ch),true),3,'./selfpaste-webclient.log'); } curl_close($ch); diff --git a/documentation/setup.txt b/documentation/setup.txt index 8e733d4..86dd763 100644 --- a/documentation/setup.txt +++ b/documentation/setup.txt @@ -8,7 +8,7 @@ Open config.php, read the comments and make your changes. For more information about some config settings read the separate documentation file for this setting. -Folder logs and pasties need t be writeable by webserver process / owner +Folder logs and pasties need to be writeable by webserver process / owner -Change date_default_timezone_set in index.php if your timezone is +Change TIMEZONE in config.php if your timezone is different to Europe/Berlin diff --git a/documentation/update.txt b/documentation/update.txt index 3bf87e7..15c973c 100644 --- a/documentation/update.txt +++ b/documentation/update.txt @@ -5,6 +5,19 @@ Update process is as follows: - update your config.php - make sure of any special update steps listed below. +## x.x Neon +Moved and added settings to config file. See config.default.php +Make sure to add (with default values): +const DEBUG = false; +const TIMEZONE = 'Europe/Berlin'; +const PATH_ABSOLUTE = '/path/to/your/installation/'; +const PATH_SYSTEMOUT = PATH_ABSOLUTE.'/logs'; +const ERROR_LOG_FILE = PATH_SYSTEMOUT.'/selfpaste-error.log'; +const CREATE_LOG = PATH_SYSTEMOUT.'/selfpaste-create.log'; + +Logfilenames changed. Existing create.log and error.log can be removed. + + ## 1.5 Fluorine New syntax in config file. Switched from define() to const syntax. This change apply also to the included webclient diff --git a/webroot/config.default.php b/webroot/config.default.php index 46470b4..78bddb7 100644 --- a/webroot/config.default.php +++ b/webroot/config.default.php @@ -16,6 +16,18 @@ * 2019 - 2023 https://://www.bananas-playground.net/projekt/selfpaste */ +# debug setting +const DEBUG = false; + +# timezone settings +const TIMEZONE = 'Europe/Berlin'; + +# path settings +const PATH_ABSOLUTE = '/www/htdocs/'; +const PATH_SYSTEMOUT = PATH_ABSOLUTE.'/logs'; +const ERROR_LOG_FILE = PATH_SYSTEMOUT.'/selfpaste-error.log'; +const CREATE_LOG = PATH_SYSTEMOUT.'/selfpaste-create.log'; + # this is your installation secret. Could be anything. # Think of it as a key. Change it often to avoid any abuse. # The description will be used in the log files diff --git a/webroot/index.php b/webroot/index.php index 29f4b94..0078112 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -16,16 +16,16 @@ * 2019 - 2023 https://://www.bananas-playground.net/projekt/selfpaste */ -# global debug setting -const DEBUG = false; - # Encoding and error reporting setting mb_http_output('UTF-8'); mb_internal_encoding('UTF-8'); error_reporting(-1); // E_ALL & E_STRICT +# config file +require_once 'config.php'; + # default time setting -date_default_timezone_set('Europe/Berlin'); +date_default_timezone_set(TIMEZONE); # check request $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); @@ -35,9 +35,6 @@ if(!empty($_urlToParse)) { } } -const ERROR_LOG_FILE = './logs/error.log'; -const CREATE_LOG = './logs/create.log'; - # error reporting ini_set('log_errors',true); if(DEBUG) { @@ -49,8 +46,7 @@ else { # static helper class require_once 'lib/summoner.class.php'; -# config file -require_once 'config.php'; + # upload / file handling require_once 'lib/mancubus.class.php'; @@ -108,7 +104,7 @@ elseif ($_create === true) { if($_do['status'] === true) { $httpResponseCode = 200; if(defined('LOG_CREATION') && LOG_CREATION === true) { - error_log(date("c")." ".$_message." ".SELFPASTE_UPLOAD_SECRET[$_POST['dl']]."\n",3,CREATE_LOG); + Summoner::createLog($_message." ".SELFPASTE_UPLOAD_SECRET[$_POST['dl']]); } } } @@ -126,7 +122,7 @@ if(file_exists('view/'.$contentView.'.inc.php')) { require_once 'view/'.$contentView.'.inc.php'; } else { - error_log('Content body file missing. '.var_export($_SERVER,true),3,ERROR_LOG_FILE); + Summoner::syslog('Content body file missing. '.Summoner::cleanForLog($_SERVER)); http_response_code(400); die('Well, something went wrong...'); } diff --git a/webroot/lib/mancubus.class.php b/webroot/lib/mancubus.class.php index 10c424a..c3acebe 100644 --- a/webroot/lib/mancubus.class.php +++ b/webroot/lib/mancubus.class.php @@ -33,9 +33,9 @@ class Mancubus { */ private string $_short; - private $_saveFilename; - private $_storagePath; - private $_shortURL; + private string $_saveFilename; + private string $_storagePath; + private string $_shortURL; /** * Mancubus constructor. diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index 5eb9d24..7d51b4f 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -214,4 +214,37 @@ class Summoner { } return $ret; } + + /** + * Make the input more safe for logging + * + * @param mixed $input The array/string to be made more safe + * @return string + */ + static function cleanForLog(mixed $input): mixed { + $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")." ".$msg."\n", 3, ERROR_LOG_FILE); + } + + /** + * error_log with a dedicated destination + * Uses CREATE_LOG const + * + * @param string $msg + * @return void + */ + static function createLog(string $msg): void { + error_log(date("c")." ".$msg."\n", 3, CREATE_LOG); + } } -- 2.39.5