From 33c2168af2523a336f245385a598efdac5ee0e61 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Wed, 17 Aug 2022 14:09:59 +0200 Subject: [PATCH 1/2] :loud_sound: Adds a debug log for invalid placeholders (#282) This adds a debug log when a placeholder cannot be replaced. Before the placeholder was just not replaced. Now we can enable a debug log on the LogChannels::NOTIFICATION channel. --- core/metamodel.class.php | 101 ++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/core/metamodel.class.php b/core/metamodel.class.php index a1b7ed3cb..744bfb26c 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -7344,14 +7344,11 @@ abstract class MetaModel $aSearches = array(); $aReplacements = array(); - foreach ($aParams as $sSearch => $replace) - { + foreach ($aParams as $sSearch => $replace) { // Some environment parameters are objects, we just need scalars - if (is_object($replace)) - { + if (is_object($replace)) { $iPos = strpos($sSearch, '->object()'); - if ($iPos !== false) - { + if ($iPos !== false) { // Expand the parameters for the object $sName = substr($sSearch, 0, $iPos); // Note: Capturing @@ -7359,63 +7356,67 @@ abstract class MetaModel // 2 - The arrow // 3 - The attribute code $aRegExps = array( - '/(\\$)'.$sName.'-(>|>)([^\\$]+)\\$/', // Support both syntaxes: $this->xxx$ or $this->xxx$ for HTML compatibility - '/(%24)'.$sName.'-(>|>)([^%24]+)%24/', // Support for urlencoded in HTML attributes (%20this->xxx%20) - ); - foreach($aRegExps as $sRegExp) - { - if(preg_match_all($sRegExp, $sInput, $aMatches)) - { - foreach($aMatches[3] as $idx => $sPlaceholderAttCode) - { - try - { - $sReplacement = $replace->GetForTemplate($sPlaceholderAttCode); - if($sReplacement !== null) - { - $aReplacements[] = $sReplacement; - $aSearches[] = $aMatches[1][$idx] . $sName . '-' . $aMatches[2][$idx] . $sPlaceholderAttCode . $aMatches[1][$idx]; - } - } - catch(Exception $e) - { - // No replacement will occur - } - } - } - } - } - else - { + '/(\\$)'.$sName.'-(>|>)([^\\$]+)\\$/', // Support both syntaxes: $this->xxx$ or $this->xxx$ for HTML compatibility + '/(%24)'.$sName.'-(>|>)([^%24]+)%24/', // Support for urlencoded in HTML attributes (%20this->xxx%20) + ); + foreach ($aRegExps as $sRegExp) { + if (preg_match_all($sRegExp, $sInput, $aMatches)) { + foreach ($aMatches[3] as $idx => $sPlaceholderAttCode) { + try { + $sReplacement = $replace->GetForTemplate($sPlaceholderAttCode); + if ($sReplacement !== null) { + $aReplacements[] = $sReplacement; + $aSearches[] = $aMatches[1][$idx].$sName.'-'.$aMatches[2][$idx].$sPlaceholderAttCode.$aMatches[1][$idx]; + } + } + catch (Exception $e) { + $aContext = [ + 'placeholder' => $sPlaceholderAttCode, + 'replace class' => get_class($replace), + ]; + if ($replace instanceof DBObject) { + $aContext['replace id'] = $replace->GetKey(); + } + IssueLog::Debug( + 'Invalid placeholder in notification, no replacement will occur!', + LogChannels::NOTIFICATION, + $aContext + ); + } + } + } + } + } else { continue; // Ignore this non-scalar value } - } - else - { + } else { $aRegExps = array( '/(\$)'.$sSearch.'\$/', // Support for regular placeholders (eg. $APP_URL$) '/(%24)'.$sSearch.'%24/', // Support for urlencoded in HTML attributes (eg. %24APP_URL%24) ); - foreach($aRegExps as $sRegExp) - { - if(preg_match_all($sRegExp, $sInput, $aMatches)) - { - foreach($aMatches[1] as $idx => $sDelimiter) - { - try - { - $aReplacements[] = (string) $replace; - $aSearches[] = $aMatches[1][$idx] . $sSearch . $aMatches[1][$idx]; + foreach ($aRegExps as $sRegExp) { + if (preg_match_all($sRegExp, $sInput, $aMatches)) { + foreach ($aMatches[1] as $idx => $sDelimiter) { + try { + $aReplacements[] = (string)$replace; + $aSearches[] = $aMatches[1][$idx].$sSearch.$aMatches[1][$idx]; } - catch(Exception $e) - { - // No replacement will occur + catch (Exception $e) { + IssueLog::Debug( + 'Invalid placeholder in notification, no replacement will occur !', + LogChannels::NOTIFICATION, + [ + 'placeholder' => $sPlaceholderAttCode, + 'replace' => $replace, + ] + ); } } } } } } + return str_replace($aSearches, $aReplacements, $sInput); } From 6b32be089972c3f63950c775b4b9c8661a19eaa1 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Wed, 17 Aug 2022 14:11:32 +0200 Subject: [PATCH 2/2] =?UTF-8?q?N=C2=B05216=20Fix=20invalid=20message-id=20?= =?UTF-8?q?when=20sending=20notification=20using=20cron=20on=20system=20wi?= =?UTF-8?q?th=20a=20specific=20locale=20set=20(#297)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The timestamp used was indeed locale dependent. This commit fixes this behavior by removing the locale dependency using a better printf format (see https://www.php.net/manual/fr/function.sprintf.php) --- core/action.class.inc.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/action.class.inc.php b/core/action.class.inc.php index 16754df99..343cd3660 100644 --- a/core/action.class.inc.php +++ b/core/action.class.inc.php @@ -328,8 +328,12 @@ class ActionEmail extends ActionNotification $sBody = MetaModel::ApplyParams($this->Get('body'), $aContextArgs); $oObj = $aContextArgs['this->object()']; - $sMessageId = sprintf('iTop_%s_%d_%f@%s.openitop.org', get_class($oObj), $oObj->GetKey(), microtime(true /* get as float*/), - MetaModel::GetEnvironmentId()); + $sMessageId = sprintf('iTop_%s_%d_%F@%s.openitop.org', + get_class($oObj), + $oObj->GetKey(), + microtime(true /* get as float*/), + MetaModel::GetEnvironmentId() + ); $sReference = '<'.$sMessageId.'>'; } catch (Exception $e) {