Compare commits

...

4 Commits

Author SHA1 Message Date
Clemens Schwaighofer
06c2ea5e0d Admin\Backend: make sure we do not access unset ->action vars 2024-10-16 12:34:48 +09:00
Clemens Schwaighofer
2e9239ec23 Ingore node_modules/ folder 2024-10-16 12:18:51 +09:00
Clemens Schwaighofer
0c89840dba Admin\Backend move _POST action read to sub function and trigger not auto loading it
On default it still auto loads the _POST vars for backwards compatible, but add a load class
flag to ignore it "init_action_vars"

also add a get vor tha "acl" array adbGetAcl()
2024-10-16 12:15:19 +09:00
Clemens Schwaighofer
db144493f3 Message system: allow warning level to be logged
Like error messages, they are written to the log if debug is on or the
flag is explicit set
2024-09-24 15:10:53 +09:00
11 changed files with 165 additions and 31 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
.libs .libs
node_modules/

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive"> <phive xmlns="https://phar.io/phive">
<phar name="phpunit" version="^9.6" installed="9.6.19" location="./tools/phpunit" copy="false"/> <phar name="phpunit" version="^9.6" installed="9.6.21" location="./tools/phpunit" copy="false"/>
<phar name="phpcbf" version="^3.7.2" installed="3.10.0" location="./tools/phpcbf" copy="false"/> <phar name="phpcbf" version="^3.7.2" installed="3.10.3" location="./tools/phpcbf" copy="false"/>
<phar name="phpcs" version="^3.7.2" installed="3.10.0" location="./tools/phpcs" copy="false"/> <phar name="phpcs" version="^3.7.2" installed="3.10.3" location="./tools/phpcs" copy="false"/>
<phar name="phpstan" version="^1.10.37" installed="1.11.1" location="./tools/phpstan" copy="false"/> <phar name="phpstan" version="^1.10.37" installed="1.12.4" location="./tools/phpstan" copy="false"/>
<phar name="phan" version="^5.4.2" installed="5.4.3" location="./tools/phan" copy="false"/> <phar name="phan" version="^5.4.2" installed="5.4.3" location="./tools/phan" copy="false"/>
<phar name="psalm" version="^5.15.0" installed="5.24.0" location="./tools/psalm" copy="false"/> <phar name="psalm" version="^5.15.0" installed="5.24.0" location="./tools/psalm" copy="false"/>
<phar name="phpdox" version="^0.12.0" installed="0.12.0" location="./tools/phpdox" copy="false"/> <phar name="phpdox" version="^0.12.0" installed="0.12.0" location="./tools/phpdox" copy="false"/>

View File

@@ -230,6 +230,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'ERROR MESSAGE', 'str' => 'ERROR MESSAGE',
'message' => null, 'message' => null,
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<ERROR> ERROR MESSAGE', 'expected' => '<ERROR> ERROR MESSAGE',
], ],
'error, logged' => [ 'error, logged' => [
@@ -238,6 +239,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'ERROR MESSAGE', 'str' => 'ERROR MESSAGE',
'message' => null, 'message' => null,
'log_error' => true, 'log_error' => true,
'log_warning' => null,
'expected' => '<ERROR> ERROR MESSAGE', 'expected' => '<ERROR> ERROR MESSAGE',
], ],
'error, logged, message' => [ 'error, logged, message' => [
@@ -246,14 +248,43 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'ERROR MESSAGE', 'str' => 'ERROR MESSAGE',
'message' => 'OTHER ERROR MESSAGE', 'message' => 'OTHER ERROR MESSAGE',
'log_error' => true, 'log_error' => true,
'log_warning' => null,
'expected' => '<ERROR> OTHER ERROR MESSAGE', 'expected' => '<ERROR> OTHER ERROR MESSAGE',
], ],
'warn, not logged' => [
'id' => '300',
'level' => 'warn',
'str' => 'WARNING MESSAGE',
'message' => null,
'log_error' => null,
'log_warning' => null,
'expected' => '<WARNING> WARNING MESSAGE',
],
'warn, logged' => [
'id' => '300',
'level' => 'warn',
'str' => 'WARNING MESSAGE',
'message' => null,
'log_error' => null,
'log_warning' => true,
'expected' => '<WARNING> WARNING MESSAGE',
],
'warn, logged, message' => [
'id' => '300',
'level' => 'warn',
'str' => 'WARNING MESSAGE',
'message' => 'OTHER WARNING MESSAGE',
'log_error' => null,
'log_warning' => true,
'expected' => '<WARNING> OTHER WARNING MESSAGE',
],
'notice' => [ 'notice' => [
'id' => '100', 'id' => '100',
'level' => 'notice', 'level' => 'notice',
'str' => 'NOTICE MESSAGE', 'str' => 'NOTICE MESSAGE',
'message' => null, 'message' => null,
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<NOTICE> NOTICE MESSAGE', 'expected' => '<NOTICE> NOTICE MESSAGE',
], ],
'notice, message' => [ 'notice, message' => [
@@ -262,6 +293,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'NOTICE MESSAGE', 'str' => 'NOTICE MESSAGE',
'message' => 'OTHER NOTICE MESSAGE', 'message' => 'OTHER NOTICE MESSAGE',
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<NOTICE> OTHER NOTICE MESSAGE', 'expected' => '<NOTICE> OTHER NOTICE MESSAGE',
], ],
'crash' => [ 'crash' => [
@@ -270,6 +302,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'CRASH MESSAGE', 'str' => 'CRASH MESSAGE',
'message' => null, 'message' => null,
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<ALERT> CRASH MESSAGE', 'expected' => '<ALERT> CRASH MESSAGE',
], ],
'crash, message' => [ 'crash, message' => [
@@ -278,6 +311,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'CRASH MESSAGE', 'str' => 'CRASH MESSAGE',
'message' => 'OTHER CRASH MESSAGE', 'message' => 'OTHER CRASH MESSAGE',
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<ALERT> OTHER CRASH MESSAGE', 'expected' => '<ALERT> OTHER CRASH MESSAGE',
], ],
'abort' => [ 'abort' => [
@@ -286,6 +320,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'ABORT MESSAGE', 'str' => 'ABORT MESSAGE',
'message' => null, 'message' => null,
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<CRITICAL> ABORT MESSAGE', 'expected' => '<CRITICAL> ABORT MESSAGE',
], ],
'abort, message' => [ 'abort, message' => [
@@ -294,6 +329,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'ABORT MESSAGE', 'str' => 'ABORT MESSAGE',
'message' => 'OTHER ABORT MESSAGE', 'message' => 'OTHER ABORT MESSAGE',
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<CRITICAL> OTHER ABORT MESSAGE', 'expected' => '<CRITICAL> OTHER ABORT MESSAGE',
], ],
'unknown' => [ 'unknown' => [
@@ -302,6 +338,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'WRONG LEVEL MESSAGE', 'str' => 'WRONG LEVEL MESSAGE',
'message' => null, 'message' => null,
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE', 'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE',
], ],
'unknown, message' => [ 'unknown, message' => [
@@ -310,6 +347,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => 'WRONG LEVEL MESSAGE', 'str' => 'WRONG LEVEL MESSAGE',
'message' => 'OTHER WRONG LEVEL MESSAGE', 'message' => 'OTHER WRONG LEVEL MESSAGE',
'log_error' => null, 'log_error' => null,
'log_warning' => null,
'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE', 'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE',
], ],
]; ];
@@ -326,6 +364,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
* @param string $str * @param string $str
* @param string|null $message * @param string|null $message
* @param bool|null $log_error * @param bool|null $log_error
* @param bool|null $log_warning
* @param string $expected * @param string $expected
* @return void * @return void
*/ */
@@ -335,6 +374,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
string $str, string $str,
?string $message, ?string $message,
?bool $log_error, ?bool $log_error,
?bool $log_warning,
string $expected string $expected
): void { ): void {
$log = new \CoreLibs\Logging\Logging([ $log = new \CoreLibs\Logging\Logging([
@@ -349,7 +389,8 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
$level, $level,
$str, $str,
message: $message, message: $message,
log_error: $log_error log_error: $log_error,
log_warning: $log_warning
); );
$file_content = ''; $file_content = '';
if (is_file($log->getLogFolder() . $log->getLogFile())) { if (is_file($log->getLogFolder() . $log->getLogFile())) {
@@ -363,6 +404,11 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
$expected, $expected,
$file_content $file_content
); );
} elseif ($level == 'warn' && ($log_warning === null || $log_warning === false)) {
$this->assertStringNotContainsString(
$expected,
$file_content
);
} else { } else {
$this->assertStringContainsString( $this->assertStringContainsString(
$expected, $expected,
@@ -382,6 +428,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
* @param string $str * @param string $str
* @param string|null $message * @param string|null $message
* @param bool|null $log_error * @param bool|null $log_error
* @param bool|null $log_warning
* @param string $expected * @param string $expected
* @return void * @return void
*/ */
@@ -391,6 +438,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
string $str, string $str,
?string $message, ?string $message,
?bool $log_error, ?bool $log_error,
?bool $log_warning,
string $expected string $expected
): void { ): void {
$log = new \CoreLibs\Logging\Logging([ $log = new \CoreLibs\Logging\Logging([
@@ -405,7 +453,8 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
$level, $level,
$str, $str,
message: $message, message: $message,
log_error: $log_error log_error: $log_error,
log_warning: $log_warning
); );
$file_content = ''; $file_content = '';
if (is_file($log->getLogFolder() . $log->getLogFile())) { if (is_file($log->getLogFolder() . $log->getLogFile())) {
@@ -419,6 +468,11 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
$expected, $expected,
$file_content $file_content
); );
} elseif ($level == 'warn' && $log_warning === false) {
$this->assertStringNotContainsString(
$expected,
$file_content
);
} else { } else {
$this->assertStringContainsString( $this->assertStringContainsString(
$expected, $expected,

View File

@@ -1 +1 @@
/home/clemens/.phive/phars/phpcbf-3.10.0.phar /home/clemens/.phive/phars/phpcbf-3.10.3.phar

View File

@@ -1 +1 @@
/home/clemens/.phive/phars/phpcs-3.10.0.phar /home/clemens/.phive/phars/phpcs-3.10.3.phar

View File

@@ -1 +1 @@
/home/clemens/.phive/phars/phpstan-1.11.1.phar /home/clemens/.phive/phars/phpstan-1.12.4.phar

View File

@@ -1 +1 @@
/home/clemens/.phive/phars/phpunit-9.6.19.phar /home/clemens/.phive/phars/phpunit-9.6.21.phar

View File

@@ -45,6 +45,8 @@ $em->setErrorMsg('123', 'error', 'msg this is bad, auto logged if debug');
$em->setErrorMsg('123', 'error', 'msg this is bad, auto logged if debug', 'target-id', 'other-style'); $em->setErrorMsg('123', 'error', 'msg this is bad, auto logged if debug', 'target-id', 'other-style');
$em->setErrorMsg('123', 'error', 'msg this is bad, logged always', log_error:true); $em->setErrorMsg('123', 'error', 'msg this is bad, logged always', log_error:true);
$em->setErrorMsg('123', 'error', 'msg this is bad, never logged', log_error:false); $em->setErrorMsg('123', 'error', 'msg this is bad, never logged', log_error:false);
$em->setErrorMsg('500', 'warning', 'This is perhaps not super good, logged_always', log_warning:true);
$em->setErrorMsg('500', 'warning', 'This is perhaps not super good, logged_never', log_warning:false);
$em->setErrorMsg('1000', 'info', 'This is good'); $em->setErrorMsg('1000', 'info', 'This is good');
$em->setErrorMsg('9999', 'abort', 'BAD: This is critical (abort)'); $em->setErrorMsg('9999', 'abort', 'BAD: This is critical (abort)');
$em->setErrorMsg('10-1000', 'wrong', 'Wrong level: This is emergency'); $em->setErrorMsg('10-1000', 'wrong', 'Wrong level: This is emergency');
@@ -59,6 +61,8 @@ print "ErrorsIds: <pre>" . $log->prAr($em->getErrorIds()) . "</pre>";
print "Errors: <pre>" . $log->prAr($em->getErrorMsg()) . "</pre>"; print "Errors: <pre>" . $log->prAr($em->getErrorMsg()) . "</pre>";
print "JumpTargets: <pre>" . $log->prAr($em->getJumpTarget()) . "</pre>"; print "JumpTargets: <pre>" . $log->prAr($em->getJumpTarget()) . "</pre>";
print "IS info > ok: " . ml::fromName('info')->isHigherThan(ml::ok) . "<br>";
print "</body></html>"; print "</body></html>";
$log->debug('[END]', '==========================================>'); $log->debug('[END]', '==========================================>');

View File

@@ -117,18 +117,20 @@ class Backend
/** /**
* main class constructor * main class constructor
* *
* @param \CoreLibs\DB\IO $db Database connection class * @param \CoreLibs\DB\IO $db Database connection class
* @param \CoreLibs\Logging\Logging $log Logging class * @param \CoreLibs\Logging\Logging $log Logging class
* @param \CoreLibs\Create\Session $session Session interface class * @param \CoreLibs\Create\Session $session Session interface class
* @param \CoreLibs\Language\L10n $l10n l10n language class * @param \CoreLibs\Language\L10n $l10n l10n language class
* @param int|null $set_default_acl_level Default ACL level * @param int|null $set_default_acl_level [default=null] Default ACL level
* @param bool $init_action_vars [default=true] If the action vars should be set
*/ */
public function __construct( public function __construct(
\CoreLibs\DB\IO $db, \CoreLibs\DB\IO $db,
\CoreLibs\Logging\Logging $log, \CoreLibs\Logging\Logging $log,
\CoreLibs\Create\Session $session, \CoreLibs\Create\Session $session,
\CoreLibs\Language\L10n $l10n, \CoreLibs\Language\L10n $l10n,
?int $set_default_acl_level = null ?int $set_default_acl_level = null,
bool $init_action_vars = true
) { ) {
// attach db class // attach db class
$this->db = $db; $this->db = $db;
@@ -151,9 +153,9 @@ class Backend
// set the page name // set the page name
$this->page_name = \CoreLibs\Get\System::getPageName(); $this->page_name = \CoreLibs\Get\System::getPageName();
// set the action ids // NOTE: if any of the "action" vars are used somewhere, it is recommended to NOT set them here
foreach ($this->action_list as $_action) { if ($init_action_vars) {
$this->$_action = $_POST[$_action] ?? ''; $this->adbSetActionVars();
} }
if ($set_default_acl_level === null) { if ($set_default_acl_level === null) {
@@ -170,7 +172,7 @@ class Backend
} }
// queue key // queue key
if (preg_match("/^(add|save|delete|remove|move|up|down|push_live)$/", $this->action)) { if (preg_match("/^(add|save|delete|remove|move|up|down|push_live)$/", $this->action ?? '')) {
$this->queue_key = \CoreLibs\Create\RandomKey::randomKeyGen(3); $this->queue_key = \CoreLibs\Create\RandomKey::randomKeyGen(3);
} }
} }
@@ -195,6 +197,29 @@ class Backend
$this->acl = $acl; $this->acl = $acl;
} }
/**
* Return current set ACL
*
* @return array<mixed>
*/
public function adbGetAcl(): array
{
return $this->acl;
}
/**
* Set _POST action vars if needed
*
* @return void
*/
public function adbSetActionVars()
{
// set the action ids
foreach ($this->action_list as $_action) {
$this->$_action = $_POST[$_action] ?? '';
}
}
/** /**
* writes all action vars plus other info into edit_log table * writes all action vars plus other info into edit_log table
* *
@@ -257,14 +282,14 @@ class Backend
"NULL" : "NULL" :
"'" . $this->session->getSessionId() . "'") "'" . $this->session->getSessionId() . "'")
. ", " . ", "
. "'" . $this->db->dbEscapeString($this->action) . "', " . "'" . $this->db->dbEscapeString($this->action ?? '') . "', "
. "'" . $this->db->dbEscapeString($this->action_id) . "', " . "'" . $this->db->dbEscapeString($this->action_id ?? '') . "', "
. "'" . $this->db->dbEscapeString($this->action_yes) . "', " . "'" . $this->db->dbEscapeString($this->action_yes ?? '') . "', "
. "'" . $this->db->dbEscapeString($this->action_flag) . "', " . "'" . $this->db->dbEscapeString($this->action_flag ?? '') . "', "
. "'" . $this->db->dbEscapeString($this->action_menu) . "', " . "'" . $this->db->dbEscapeString($this->action_menu ?? '') . "', "
. "'" . $this->db->dbEscapeString($this->action_loaded) . "', " . "'" . $this->db->dbEscapeString($this->action_loaded ?? '') . "', "
. "'" . $this->db->dbEscapeString($this->action_value) . "', " . "'" . $this->db->dbEscapeString($this->action_value ?? '') . "', "
. "'" . $this->db->dbEscapeString($this->action_error) . "')"; . "'" . $this->db->dbEscapeString($this->action_error ?? '') . "')";
$this->db->dbExec($q, 'NULL'); $this->db->dbExec($q, 'NULL');
} }

View File

@@ -24,16 +24,20 @@ class ErrorMessage
/** @var bool $log_error global flag to log error level message */ /** @var bool $log_error global flag to log error level message */
private bool $log_error = false; private bool $log_error = false;
/** @var bool $log_warning global flat to log warning level messages */
private bool $log_warning = false;
/** /**
* init ErrorMessage * init ErrorMessage
* *
* @param \CoreLibs\Logging\Logging $log * @param \CoreLibs\Logging\Logging $log
* @param null|bool $log_error [=null], defaults to false if log is not level debug * @param null|bool $log_error [=null], defaults to false if log is not level debug
* @param null|bool $log_warning [=null], defaults to false if log is not level debug
*/ */
public function __construct( public function __construct(
\CoreLibs\Logging\Logging $log, \CoreLibs\Logging\Logging $log,
?bool $log_error = null ?bool $log_error = null,
?bool $log_warning = null
) { ) {
$this->log = $log; $this->log = $log;
// if log default logging is debug then log_error is default set to true // if log default logging is debug then log_error is default set to true
@@ -43,6 +47,13 @@ class ErrorMessage
$log_error = $log_error ?? false; $log_error = $log_error ?? false;
} }
$this->log_error = $log_error; $this->log_error = $log_error;
// if log default logging is debug then log_warning is default set to true
if ($this->log->loggingLevelIsDebug() && $log_warning === null) {
$log_warning = true;
} else {
$log_warning = $log_warning ?? false;
}
$this->log_warning = $log_warning;
} }
/** /**
@@ -81,6 +92,8 @@ class ErrorMessage
* @param array<mixed> $context Additionl info for abort/crash messages * @param array<mixed> $context Additionl info for abort/crash messages
* @param bool|null $log_error [=null] log level 'error' to error, if null use global, * @param bool|null $log_error [=null] log level 'error' to error, if null use global,
* else set for this call only * else set for this call only
* @param bool|null $log_warning [=null] log level 'warning' to warning, if null use global,
* else set for this call only
*/ */
public function setErrorMsg( public function setErrorMsg(
string $error_id, string $error_id,
@@ -93,10 +106,14 @@ class ErrorMessage
?string $message = null, ?string $message = null,
array $context = [], array $context = [],
?bool $log_error = null, ?bool $log_error = null,
?bool $log_warning = null,
): void { ): void {
if ($log_error === null) { if ($log_error === null) {
$log_error = $this->log_error; $log_error = $this->log_error;
} }
if ($log_warning === null) {
$log_warning = $this->log_warning;
}
$original_level = $level; $original_level = $level;
$level = MessageLevel::fromName($level)->name; $level = MessageLevel::fromName($level)->name;
// if not string set, write message string if set, else level/error id // if not string set, write message string if set, else level/error id
@@ -121,6 +138,14 @@ class ErrorMessage
'level' => $original_level, 'level' => $original_level,
], $context)); ], $context));
break; break;
case 'warn':
if ($log_warning) {
$this->log->warning($message ?? $str, array_merge([
'id' => $error_id,
'level' => $original_level,
], $context));
}
break;
case 'error': case 'error':
if ($log_error) { if ($log_error) {
$this->log->error($message ?? $str, array_merge([ $this->log->error($message ?? $str, array_merge([
@@ -169,6 +194,8 @@ class ErrorMessage
* @param array<mixed> $context Additionl info for abort/crash messages * @param array<mixed> $context Additionl info for abort/crash messages
* @param bool|null $log_error [=null] log level 'error' to error, if null use global, * @param bool|null $log_error [=null] log level 'error' to error, if null use global,
* else set for this call only * else set for this call only
* @param bool|null $log_warning [=null] log level 'warning' to warning, if null use global,
* else set for this call only
*/ */
public function setMessage( public function setMessage(
string $level, string $level,
@@ -181,6 +208,7 @@ class ErrorMessage
?string $message = null, ?string $message = null,
array $context = [], array $context = [],
?bool $log_error = null, ?bool $log_error = null,
?bool $log_warning = null,
): void { ): void {
$this->setErrorMsg( $this->setErrorMsg(
$error_id ?? '', $error_id ?? '',
@@ -192,7 +220,8 @@ class ErrorMessage
$jump_target, $jump_target,
$message, $message,
$context, $context,
$log_error $log_error,
$log_warning
); );
} }
@@ -314,6 +343,27 @@ class ErrorMessage
{ {
return $this->log_error; return $this->log_error;
} }
/**
* Set the log warning flag
*
* @param bool $flag True to log level warning too, False for do not (Default)
* @return void
*/
public function setFlagLogWarning(bool $flag): void
{
$this->log_warning = $flag;
}
/**
* Get the current log error flag
*
* @return bool
*/
public function getFlagLogWarning(): bool
{
return $this->log_warning;
}
} }
// __END__ // __END__

View File

@@ -14,7 +14,7 @@ namespace CoreLibs\Logging\Logger;
enum MessageLevel: int enum MessageLevel: int
{ {
case ok = 100; case ok = 100;
case success = 110; // special for file uploads case success = 150; // special for file uploads
case info = 200; case info = 200;
case notice = 250; case notice = 250;
case warn = 300; case warn = 300;