Skip to content

Commit e969e6b

Browse files
committed
Validate PWA options in ACP
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent 69c5ba0 commit e969e6b

4 files changed

Lines changed: 94 additions & 15 deletions

File tree

config/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ services:
1212
phpbb.wpn.listener:
1313
class: phpbb\webpushnotifications\event\listener
1414
arguments:
15+
- '@config'
1516
- '@controller.helper'
17+
- '@upload_imagesize'
1618
- '@phpbb.wpn.form_helper'
1719
- '@language'
1820
- '@template'
1921
- '@notification_manager'
22+
- '%core.root_path%'
2023
tags:
2124
- { name: event.listener }
2225

event/listener.php

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,33 @@
1313
/**
1414
* @ignore
1515
*/
16-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16+
17+
use FastImageSize\FastImageSize;
18+
use phpbb\config\config;
1719
use phpbb\controller\helper as controller_helper;
18-
use phpbb\webpushnotifications\form\form_helper;
1920
use phpbb\language\language;
2021
use phpbb\notification\manager;
2122
use phpbb\template\template;
23+
use phpbb\webpushnotifications\form\form_helper;
24+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2225

2326
/**
2427
* Event listener
2528
*/
2629
class listener implements EventSubscriberInterface
2730
{
28-
public static function getSubscribedEvents()
29-
{
30-
return [
31-
'core.ucp_display_module_before' => 'load_language',
32-
'core.acp_main_notice' => 'compatibility_notice',
33-
'core.page_header_after' => 'load_template_data',
34-
'core.acp_board_config_edit_add' => 'acp_pwa_options',
35-
];
36-
}
31+
/** @var config */
32+
protected $config;
3733

3834
/* @var controller_helper */
3935
protected $controller_helper;
4036

4137
/* @var form_helper */
4238
protected $form_helper;
4339

40+
/** @var FastImageSize */
41+
protected $imagesize;
42+
4443
/* @var language */
4544
protected $language;
4645

@@ -50,22 +49,42 @@ public static function getSubscribedEvents()
5049
/* @var manager */
5150
protected $phpbb_notifications;
5251

52+
/** @var string */
53+
protected $root_path;
54+
5355
/**
5456
* Constructor
5557
*
58+
* @param config $config
5659
* @param controller_helper $controller_helper Controller helper object
60+
* @param FastImageSize $imagesize
5761
* @param form_helper $form_helper Form helper object
5862
* @param language $language Language object
5963
* @param template $template Template object
6064
* @param manager $phpbb_notifications Notifications manager object
65+
* @param $root_path
6166
*/
62-
public function __construct(controller_helper $controller_helper, form_helper $form_helper, language $language, template $template, manager $phpbb_notifications)
67+
public function __construct(config $config, controller_helper $controller_helper, FastImageSize $imagesize, form_helper $form_helper, language $language, template $template, manager $phpbb_notifications, $root_path)
6368
{
69+
$this->config = $config;
6470
$this->controller_helper = $controller_helper;
71+
$this->imagesize = $imagesize;
6572
$this->form_helper = $form_helper;
6673
$this->language = $language;
6774
$this->template = $template;
6875
$this->phpbb_notifications = $phpbb_notifications;
76+
$this->root_path = $root_path;
77+
}
78+
79+
public static function getSubscribedEvents()
80+
{
81+
return [
82+
'core.ucp_display_module_before' => 'load_language',
83+
'core.acp_main_notice' => 'compatibility_notice',
84+
'core.page_header_after' => 'load_template_data',
85+
'core.acp_board_config_edit_add' => 'acp_pwa_options',
86+
'core.validate_config_variable' => 'validate_pwa_options',
87+
];
6988
}
7089

7190
/**
@@ -119,11 +138,55 @@ public function acp_pwa_options($event)
119138
$my_config_vars = [
120139
'legend_pwa_settings'=> 'PWA_SETTINGS',
121140
'pwa_short_name' => ['lang' => 'PWA_SHORT_NAME', 'validate' => 'string', 'type' => 'text:40:12', 'explain' => true],
122-
'pwa_icon_small' => ['lang' => 'PWA_ICON_SMALL', 'validate' => 'string', 'type' => 'text:40:255', 'explain' => true],
123-
'pwa_icon_large' => ['lang' => 'PWA_ICON_LARGE', 'validate' => 'string', 'type' => 'text:40:255', 'explain' => true],
141+
'pwa_icon_small' => ['lang' => 'PWA_ICON_SMALL', 'validate' => 'pwa_options', 'type' => 'text:40:255', 'explain' => true],
142+
'pwa_icon_large' => ['lang' => 'PWA_ICON_LARGE', 'validate' => 'pwa_options', 'type' => 'text:40:255', 'explain' => true],
124143
];
125144

126145
$event->update_subarray('display_vars', 'vars', phpbb_insert_config_array($event['display_vars']['vars'], $my_config_vars, ['before' => 'legend4']));
127146
}
128147
}
148+
149+
/**
150+
* Validate PWA options
151+
*
152+
* @param \phpbb\event\data $event
153+
* @return void
154+
*/
155+
public function validate_pwa_options($event)
156+
{
157+
if ($event['config_definition']['validate'] !== 'pwa_options' || empty($event['cfg_array']['pwa_icon_small']) || empty($event['cfg_array']['pwa_icon_large']))
158+
{
159+
return;
160+
}
161+
162+
$value = $event['cfg_array'][$event['config_name']];
163+
$error = $event['error'];
164+
165+
$image = $this->root_path . $this->config['icons_path'] . '/' . $value;
166+
if (!file_exists($image))
167+
{
168+
$error[] = $this->language->lang('PWA_IMAGE_NOT_FOUND', $value);
169+
}
170+
171+
$image_info = $this->imagesize->getImageSize($image);
172+
if ($image_info !== false)
173+
{
174+
if (($event['config_name'] === 'pwa_icon_small' && $image_info['width'] !== 192 && $image_info['height'] !== 192) ||
175+
($event['config_name'] === 'pwa_icon_large' && $image_info['width'] !== 512 && $image_info['height'] !== 512))
176+
{
177+
$error[] = $this->language->lang('PWA_ICON_SIZE_INVALID', $value);
178+
}
179+
180+
if ($image_info['type'] !== IMAGETYPE_PNG)
181+
{
182+
$error[] = $this->language->lang('PWA_ICON_MIME_INVALID', $value);
183+
}
184+
}
185+
else
186+
{
187+
$error[] = $this->language->lang('PWA_IMAGE_INVALID', $value);
188+
}
189+
190+
$event['error'] = $error;
191+
}
129192
}

language/en/webpushnotifications_common_acp.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@
4545
'PWA_ICON_SMALL_EXPLAIN' => 'File name of a 192px x 192px PNG image. This file must be uploaded to your <samp>icons</samp> directory, e.g. <samp>./images/icons</samp>',
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 <samp>icons</samp> directory, e.g. <samp>./images/icons</samp>',
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.',
4852
]);

tests/event/listener_test.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class listener_test extends \phpbb_database_test_case
3333
/* @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\notification\manager */
3434
protected $phpbb_notifications;
3535

36+
/** @var string */
37+
protected $root_path;
38+
3639
protected static function setup_extensions()
3740
{
3841
return ['phpbb/webpushnotifications'];
@@ -101,16 +104,21 @@ protected function setUp(): void
101104
'phpbb_wpn_notification_push',
102105
'phpbb_wpn_push_subscriptions'
103106
);
107+
108+
$this->root_path = $phpbb_root_path;
104109
}
105110

106111
protected function set_listener()
107112
{
108113
$this->listener = new \phpbb\webpushnotifications\event\listener(
114+
$this->config,
109115
$this->controller_helper,
116+
new \FastImageSize\FastImageSize(),
110117
$this->form_helper,
111118
$this->language,
112119
$this->template,
113-
$this->notifications
120+
$this->notifications,
121+
$this->root_path
114122
);
115123
}
116124

@@ -127,6 +135,7 @@ public function test_getSubscribedEvents()
127135
'core.acp_main_notice',
128136
'core.page_header_after',
129137
'core.acp_board_config_edit_add',
138+
'core.validate_config_variable',
130139
], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents()));
131140
}
132141

0 commit comments

Comments
 (0)