Skip to content

Commit 70140d5

Browse files
committed
Better handling of unexpected empty icon image settings
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent 7ffb021 commit 70140d5

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

event/listener.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,39 +166,62 @@ public function pwa_icon_name($value, $key)
166166
*/
167167
public function validate_pwa_options($event)
168168
{
169-
if ($event['config_definition']['validate'] !== 'pwa_options' || empty($event['cfg_array']['pwa_icon_small']) || empty($event['cfg_array']['pwa_icon_large']))
169+
// Ignore validation if icon fields are empty
170+
if ($event['config_definition']['validate'] !== 'pwa_options' || (empty($event['cfg_array']['pwa_icon_small']) && empty($event['cfg_array']['pwa_icon_large'])))
170171
{
171172
return;
172173
}
173174

174175
$value = $event['cfg_array'][$event['config_name']];
175-
$error = $event['error'];
176176

177+
// Don't allow empty values, if one icon is set, both must be set.
178+
if (empty($value))
179+
{
180+
$this->add_error($event, 'PWA_IMAGE_NOT_PROVIDED', $this->language->lang(strtoupper($event['config_name'])));
181+
return;
182+
}
183+
184+
// Check if image file exists
177185
$image = $this->root_path . $this->config['icons_path'] . '/' . $value;
178186
if (!file_exists($image))
179187
{
180-
$error[] = $this->language->lang('PWA_IMAGE_NOT_FOUND', $value);
188+
$this->add_error($event, 'PWA_IMAGE_NOT_FOUND', $value);
189+
return;
181190
}
182191

192+
// Check if image is the correct dimensions and type
183193
$image_info = $this->imagesize->getImageSize($image);
184194
if ($image_info !== false)
185195
{
186196
if (($event['config_name'] === 'pwa_icon_small' && $image_info['width'] !== 192 && $image_info['height'] !== 192) ||
187197
($event['config_name'] === 'pwa_icon_large' && $image_info['width'] !== 512 && $image_info['height'] !== 512))
188198
{
189-
$error[] = $this->language->lang('PWA_ICON_SIZE_INVALID', $value);
199+
$this->add_error($event, 'PWA_ICON_SIZE_INVALID', $value);
190200
}
191201

192202
if ($image_info['type'] !== IMAGETYPE_PNG)
193203
{
194-
$error[] = $this->language->lang('PWA_ICON_MIME_INVALID', $value);
204+
$this->add_error($event, 'PWA_ICON_MIME_INVALID', $value);
195205
}
196206
}
197207
else
198208
{
199-
$error[] = $this->language->lang('PWA_IMAGE_INVALID', $value);
209+
$this->add_error($event, 'PWA_IMAGE_INVALID', $value);
200210
}
211+
}
201212

213+
/**
214+
* Add errors to the error array
215+
*
216+
* @param \phpbb\event\data $event
217+
* @param string $error_key
218+
* @param string $param
219+
* @return void
220+
*/
221+
protected function add_error($event, $error_key, $param = null)
222+
{
223+
$error = $event['error'];
224+
$error[] = $this->language->lang($error_key, $param);
202225
$event['error'] = $error;
203226
}
204227
}

language/en/webpushnotifications_common_acp.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
'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.',
4646
'PWA_ICON_LARGE' => 'Large mobile device icon',
4747
'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.',
48-
'PWA_ICON_SIZE_INVALID' => '%s does not have the correct image dimensions.',
49-
'PWA_ICON_MIME_INVALID' => '%s must be a PNG image file.',
50-
'PWA_IMAGE_INVALID' => '%s does not appear to be a valid image file.',
51-
'PWA_IMAGE_NOT_FOUND' => '%s could not be found.',
48+
'PWA_ICON_SIZE_INVALID' => '“%s” does not have the correct image dimensions.',
49+
'PWA_ICON_MIME_INVALID' => '“%s” must be a PNG image file.',
50+
'PWA_IMAGE_INVALID' => '“%s” does not appear to be a valid image file.',
51+
'PWA_IMAGE_NOT_FOUND' => '“%s” could not be found.',
52+
'PWA_IMAGE_NOT_PROVIDED' => '%s must not be empty. All icon fields must contain and image.',
5253
]);

language/ru/webpushnotifications_common_acp.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
'PWA_ICON_SMALL_EXPLAIN' => 'Имя файла изображения формата PNG размером 192 x 192 пикселя. Файл изображения должен быть загружен на сервер в папку <samp>images/icons</samp>.',
4646
'PWA_ICON_LARGE' => 'Большой значок для мобильного устройства',
4747
'PWA_ICON_LARGE_EXPLAIN' => 'Имя файла изображения формата PNG размером 512 x 512 пикселей. Файл изображения должен быть загружен на сервер в папку <samp>images/icons</samp>.',
48-
'PWA_ICON_SIZE_INVALID' => 'Изображение %s имеет некорректные размеры.',
49-
'PWA_ICON_MIME_INVALID' => 'Файл изображения %s должен иметь формат PNG.',
50-
'PWA_IMAGE_INVALID' => 'Файл %s не яввляется файлом изображения.',
51-
'PWA_IMAGE_NOT_FOUND' => 'Файл %s не найден.',
48+
'PWA_ICON_SIZE_INVALID' => 'Изображение “%s” имеет некорректные размеры.',
49+
'PWA_ICON_MIME_INVALID' => 'Файл изображения “%s” должен иметь формат PNG.',
50+
'PWA_IMAGE_INVALID' => 'Файл “%s” не яввляется файлом изображения.',
51+
'PWA_IMAGE_NOT_FOUND' => 'Файл “%s” не найден.',
52+
'PWA_IMAGE_NOT_PROVIDED' => '“%s” must not be empty. All icon fields must contain and image.',
5253
]);

0 commit comments

Comments
 (0)