1313/**
1414 * @ignore
1515 */
16- use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
16+
17+ use FastImageSize \FastImageSize ;
18+ use phpbb \config \config ;
1719use phpbb \controller \helper as controller_helper ;
18- use phpbb \webpushnotifications \form \form_helper ;
1920use phpbb \language \language ;
2021use phpbb \notification \manager ;
2122use phpbb \template \template ;
23+ use phpbb \webpushnotifications \form \form_helper ;
24+ use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
2225
2326/**
2427 * Event listener
2528 */
2629class 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}
0 commit comments