Skip to content

Commit

Permalink
added token cleaner and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Jun 1, 2022
1 parent c8e5098 commit 975de23
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [1.22.0] - 2022-06-01
- Added: Tokens::cleanInvalidTokens()
- Changed: invalid tokens now always cleaned

## [1.21.4] - 2022-05-27
- Fixed: call member functon on null error

Expand Down
18 changes: 10 additions & 8 deletions classes/EventListener/FormGeneratorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Contao\System;
use Contao\Validator;
use HeimrichHannot\Submissions\SubmissionModel;
use HeimrichHannot\Submissions\Util\Tokens;
use NotificationCenter\Model\Gateway;
use NotificationCenter\Model\Message;

Expand Down Expand Up @@ -115,14 +116,6 @@ public function onSendNotificationMessage(Message $message, array &$tokens, stri
$tokens['raw_data_filled'] = str_replace($tokens['form_uuid'], $uuid, $tokens['raw_data_filled']);
$tokens['form_uuid'] = $uuid;
}

if (false === json_encode($tokens)) {
System::log(
sprintf("The message '%s' (ID %s) contains invalid tokens!", $message->title, $message->id),
__METHOD__,
TL_ERROR
);
}
}

if (version_compare(VERSION, '4.7', '>=')) {
Expand All @@ -132,6 +125,15 @@ public function onSendNotificationMessage(Message $message, array &$tokens, stri
}
}

if (false === json_encode($tokens)) {
System::log(
sprintf("The message '%s' (ID %s) contains invalid tokens!", $message->title, $message->id),
__METHOD__,
TL_ERROR
);
$tokens = Tokens::cleanInvalidTokens($tokens);
}

return true;
}

Expand Down
8 changes: 7 additions & 1 deletion classes/EventListener/GeneratePageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Contao\System;
use HeimrichHannot\Submissions\Event\SubmissionsBeforeSendConfirmationNotificationEvent;
use HeimrichHannot\Submissions\SubmissionModel;
use HeimrichHannot\Submissions\Util\Tokens;
use NotificationCenter\tl_form;

/**
Expand Down Expand Up @@ -92,7 +93,12 @@ public function __invoke(PageModel $pageModel, LayoutModel $layout, PageRegular
$instance = System::importStatic(tl_form::class);
if ($instance) {
$submissionCache = StringUtil::deserialize($submission->huhSubOptInCache);
$instance->sendFormNotification($submission->row(), $form->row(), $submissionCache['files'] ?? [], $submissionCache['labels'] ?? []);
$instance->sendFormNotification(
Tokens::cleanInvalidTokens($submission->row()),
Tokens::cleanInvalidTokens($form->row()),
$submissionCache['files'] ?? [],
$submissionCache['labels'] ?? []
);
}

// clean database
Expand Down
32 changes: 32 additions & 0 deletions classes/util/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
namespace HeimrichHannot\Submissions\Util;


use Contao\StringUtil;
use Contao\Validator;

class Tokens
{
public static function replace($strBuffer, \HeimrichHannot\Submissions\SubmissionModel $objSubmission)
Expand Down Expand Up @@ -59,4 +62,33 @@ public static function transform($varValue, $strField, $arrParams, \HeimrichHann

return $varValue;
}

/**
* Convert or remove invalid tokens for notification center
*
* @param array $tokens
* @return array
*/
public static function cleanInvalidTokens(array $tokens): array
{
if (false !== json_encode($tokens)) {
return $tokens;
}

foreach ($tokens as $k => $v) {
if (false !== json_encode($v)) {
continue;
}

if (Validator::isBinaryUuid($v)) {
$tokens[$k] = $v = StringUtil::binToUuid($v);
}

if (false === json_encode($v)) {
unset($tokens[$k]);
}
}

return $tokens;
}
}

0 comments on commit 975de23

Please sign in to comment.