Skip to content

Commit

Permalink
Merge pull request #70 from iMattPro/bugs
Browse files Browse the repository at this point in the history
Fix ACP validation
  • Loading branch information
iMattPro authored Jul 2, 2024
2 parents 7ffb021 + a39818f commit f68cd30
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
32 changes: 24 additions & 8 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,39 +166,55 @@ public function pwa_icon_name($value, $key)
*/
public function validate_pwa_options($event)
{
if ($event['config_definition']['validate'] !== 'pwa_options' || empty($event['cfg_array']['pwa_icon_small']) || empty($event['cfg_array']['pwa_icon_large']))
// Ignore validation if icon fields are empty
if ($event['config_definition']['validate'] !== 'pwa_options' || (empty($event['cfg_array']['pwa_icon_small']) && empty($event['cfg_array']['pwa_icon_large'])))
{
return;
}

$value = $event['cfg_array'][$event['config_name']];
$error = $event['error'];

$image = $this->root_path . $this->config['icons_path'] . '/' . $value;
if (!file_exists($image))
// Don't allow empty values, if one icon is set, both must be set.
if (empty($value))
{
$error[] = $this->language->lang('PWA_IMAGE_NOT_FOUND', $value);
$this->add_error($event, 'PWA_IMAGE_NOT_PROVIDED', $this->language->lang(strtoupper($event['config_name'])));
return;
}

// Check if image is valid
$image = $this->root_path . $this->config['icons_path'] . '/' . $value;
$image_info = $this->imagesize->getImageSize($image);
if ($image_info !== false)
{
if (($event['config_name'] === 'pwa_icon_small' && $image_info['width'] !== 192 && $image_info['height'] !== 192) ||
($event['config_name'] === 'pwa_icon_large' && $image_info['width'] !== 512 && $image_info['height'] !== 512))
{
$error[] = $this->language->lang('PWA_ICON_SIZE_INVALID', $value);
$this->add_error($event, 'PWA_ICON_SIZE_INVALID', $value);
}

if ($image_info['type'] !== IMAGETYPE_PNG)
{
$error[] = $this->language->lang('PWA_ICON_MIME_INVALID', $value);
$this->add_error($event, 'PWA_ICON_MIME_INVALID', $value);
}
}
else
{
$error[] = $this->language->lang('PWA_IMAGE_INVALID', $value);
$this->add_error($event, 'PWA_IMAGE_INVALID', $value);
}
}

/**
* Add errors to the error array
*
* @param \phpbb\event\data $event
* @param string $error_key
* @param string $param
* @return void
*/
protected function add_error($event, $error_key, $param = null)
{
$error = $event['error'];
$error[] = $this->language->lang($error_key, $param);
$event['error'] = $error;
}
}
8 changes: 4 additions & 4 deletions language/en/webpushnotifications_common_acp.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
'PWA_ICON_SMALL_EXPLAIN' => 'File name of a 192px x 192px PNG image. This file must be uploaded to your board’s <samp>icons</samp> directory.',
'PWA_ICON_LARGE' => 'Large mobile device icon',
'PWA_ICON_LARGE_EXPLAIN' => 'File name of a 512px x 512px PNG image. This file must be uploaded to your board’s <samp>icons</samp> directory.',
'PWA_ICON_SIZE_INVALID' => '%s does not have the correct image dimensions.',
'PWA_ICON_MIME_INVALID' => '%s must be a PNG image file.',
'PWA_IMAGE_INVALID' => '%s does not appear to be a valid image file.',
'PWA_IMAGE_NOT_FOUND' => '%s could not be found.',
'PWA_ICON_SIZE_INVALID' => '“%s” does not have the correct image dimensions.',
'PWA_ICON_MIME_INVALID' => '“%s” must be a PNG image file.',
'PWA_IMAGE_INVALID' => '“%s” does not appear to be a valid image file.',
'PWA_IMAGE_NOT_PROVIDED' => '%s field must not be empty. All icon fields must contain and image.',
]);
8 changes: 4 additions & 4 deletions language/ru/webpushnotifications_common_acp.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
'PWA_ICON_SMALL_EXPLAIN' => 'Имя файла изображения формата PNG размером 192 x 192 пикселя. Файл изображения должен быть загружен на сервер в папку <samp>images/icons</samp>.',
'PWA_ICON_LARGE' => 'Большой значок для мобильного устройства',
'PWA_ICON_LARGE_EXPLAIN' => 'Имя файла изображения формата PNG размером 512 x 512 пикселей. Файл изображения должен быть загружен на сервер в папку <samp>images/icons</samp>.',
'PWA_ICON_SIZE_INVALID' => 'Изображение %s имеет некорректные размеры.',
'PWA_ICON_MIME_INVALID' => 'Файл изображения %s должен иметь формат PNG.',
'PWA_IMAGE_INVALID' => 'Файл %s не яввляется файлом изображения.',
'PWA_IMAGE_NOT_FOUND' => 'Файл %s не найден.',
'PWA_ICON_SIZE_INVALID' => 'Изображение «%s» имеет некорректные размеры.',
'PWA_ICON_MIME_INVALID' => 'Файл изображения «%s» должен иметь формат PNG.',
'PWA_IMAGE_INVALID' => 'Файл «%s» не яввляется файлом изображения.',
'PWA_IMAGE_NOT_PROVIDED' => 'Настройка «%s» не может быть пустой. Необходимо задать все пути к значкам для мобильного устройства.',
]);
8 changes: 4 additions & 4 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,19 +310,19 @@ public function validate_pwa_options_data()
return [
[
['pwa_icon_small' => '192.png', 'pwa_icon_large' => '512.png'],
['PWA_IMAGE_NOT_FOUND'],
[],
],
[
['pwa_icon_small' => '1.png', 'pwa_icon_large' => '512.png'],
['PWA_IMAGE_NOT_FOUND', 'PWA_ICON_SIZE_INVALID'],
['PWA_ICON_SIZE_INVALID'],
],
[
['pwa_icon_small' => '1.png', 'pwa_icon_large' => '12.png'],
['PWA_IMAGE_NOT_FOUND', 'PWA_ICON_SIZE_INVALID'],
['PWA_ICON_SIZE_INVALID'],
],
[
['pwa_icon_small' => '192.jpg', 'pwa_icon_large' => '512.gif'],
['PWA_IMAGE_NOT_FOUND', 'PWA_ICON_MIME_INVALID'],
['PWA_ICON_MIME_INVALID'],
],
[
['pwa_icon_small' => '', 'pwa_icon_large' => ''],
Expand Down

0 comments on commit f68cd30

Please sign in to comment.