Skip to content

Commit 3cc4625

Browse files
authored
Handle config decoding errors from DB charset issues (#333)
If the database's character set is not set correctly, the decoding of the config attributes may fail. For that case an error message will be rendered in the form. There will also be a docs entry to prevent that. Closes #271
2 parents 988b334 + 7f68bc3 commit 3cc4625

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

application/forms/ChannelForm.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
use DateTime;
88
use Icinga\Exception\Http\HttpNotFoundException;
9-
use Icinga\Module\Notifications\Model\Channel;
109
use Icinga\Module\Notifications\Model\AvailableChannelType;
10+
use Icinga\Module\Notifications\Model\Channel;
1111
use Icinga\Module\Notifications\Model\Contact;
1212
use Icinga\Module\Notifications\Model\RuleEscalationRecipient;
1313
use Icinga\Web\Session;
14+
use ipl\Html\Attributes;
1415
use ipl\Html\Contract\FormSubmitElement;
1516
use ipl\Html\FormElement\BaseFormElement;
1617
use ipl\Html\FormElement\FieldsetElement;
18+
use ipl\Html\HtmlElement;
1719
use ipl\I18n\GettextTranslator;
1820
use ipl\I18n\StaticTranslator;
1921
use ipl\Sql\Connection;
@@ -267,6 +269,24 @@ protected function createConfigElements(string $type, string $config): void
267269
$elementsConfig = json_decode($config, true);
268270

269271
if (empty($elementsConfig)) {
272+
$this->prependHtml(
273+
HtmlElement::create(
274+
'ul',
275+
Attributes::create(['class' => 'errors']),
276+
HtmlElement::create(
277+
'li',
278+
null,
279+
sprintf(
280+
$this->translate(
281+
'Could not decode options for type \'%s\'.'
282+
. ' Check if your database\'s character set is correctly configured.'
283+
),
284+
$type
285+
)
286+
)
287+
)
288+
);
289+
270290
return;
271291
}
272292

@@ -449,4 +469,20 @@ private function fetchDbValues(): array
449469
'config' => json_decode($channel->config, true) ?? []
450470
];
451471
}
472+
473+
/**
474+
* Validate all elements
475+
*
476+
* @return $this
477+
*/
478+
public function validate(): self
479+
{
480+
parent::validate();
481+
482+
if (! $this->hasElement('config')) {
483+
$this->isValid = false;
484+
}
485+
486+
return $this;
487+
}
452488
}

library/Notifications/Common/Database.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ private static function getConnection(): Connection
8282
. ",NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'";
8383
}
8484

85+
if (empty($config->charset)) {
86+
$config->charset = match ($config->db) {
87+
'mysql' => 'utf8mb4',
88+
default => 'utf8',
89+
};
90+
}
91+
8592
$db = new Connection($config);
8693

8794
$adapter = $db->getAdapter();

0 commit comments

Comments
 (0)