@@ -142,6 +142,7 @@ public function pwa_manifest()
142142 $ this ->template ->assign_vars ([
143143 'U_MANIFEST_URL ' => $ this ->controller_helper ->route ('phpbb_webpushnotifications_manifest_controller ' ),
144144 'U_TOUCH_ICON ' => $ this ->config ['pwa_icon_small ' ],
145+ 'SHORT_SITE_NAME ' => $ this ->config ['pwa_short_name ' ] ?: $ this ->get_shortname ($ this ->config ['sitename ' ]),
145146 ]);
146147 }
147148
@@ -159,9 +160,9 @@ public function acp_pwa_options($event)
159160
160161 $ my_config_vars = [
161162 'legend_pwa_settings ' => 'PWA_SETTINGS ' ,
162- 'pwa_short_name ' => ['lang ' => 'PWA_SHORT_NAME ' , 'validate ' => 'string ' , 'type ' => 'text:40:12 ' , 'explain ' => true ],
163- 'pwa_icon_small ' => ['lang ' => 'PWA_ICON_SMALL ' , 'validate ' => 'pwa_options ' , 'type ' => 'custom ' , 'function ' => [$ this , 'pwa_icon_name ' ], 'explain ' => true ],
164- 'pwa_icon_large ' => ['lang ' => 'PWA_ICON_LARGE ' , 'validate ' => 'pwa_options ' , 'type ' => 'custom ' , 'function ' => [$ this , 'pwa_icon_name ' ], 'explain ' => true ],
163+ 'pwa_short_name ' => ['lang ' => 'PWA_SHORT_NAME ' , 'validate ' => 'pwa_options: string ' , 'type ' => 'custom ' , ' function ' => [ $ this , ' pwa_short_sitename ' ] , 'explain ' => true ],
164+ 'pwa_icon_small ' => ['lang ' => 'PWA_ICON_SMALL ' , 'validate ' => 'pwa_options:icons ' , 'type ' => 'custom ' , 'function ' => [$ this , 'pwa_icon_name ' ], 'explain ' => true ],
165+ 'pwa_icon_large ' => ['lang ' => 'PWA_ICON_LARGE ' , 'validate ' => 'pwa_options:icons ' , 'type ' => 'custom ' , 'function ' => [$ this , 'pwa_icon_name ' ], 'explain ' => true ],
165166 ];
166167
167168 $ event ->update_subarray ('display_vars ' , 'vars ' , phpbb_insert_config_array ($ event ['display_vars ' ]['vars ' ], $ my_config_vars , ['before ' => 'legend4 ' ]));
@@ -180,6 +181,20 @@ public function pwa_icon_name($value, $key)
180181 return $ this ->config ['icons_path ' ] . '/<input id=" ' . $ key . '" type="text" size="40" maxlength="255" name="config[ ' . $ key . ']" value=" ' . $ value . '"> ' ;
181182 }
182183
184+ /**
185+ * Return HTML for PWA short site name setting
186+ *
187+ * @param string $value Value of config
188+ * @param string $key Name of config
189+ * @return string
190+ */
191+ public function pwa_short_sitename ($ value , $ key )
192+ {
193+ $ placeholder = $ this ->get_shortname ($ this ->config ['sitename ' ]);
194+
195+ return '<input id=" ' . $ key . '" type="text" size="40" maxlength="12" name="config[ ' . $ key . ']" value=" ' . $ value . '" placeholder=" ' . $ placeholder . '"> ' ;
196+ }
197+
183198 /**
184199 * Validate PWA options
185200 *
@@ -188,40 +203,79 @@ public function pwa_icon_name($value, $key)
188203 */
189204 public function validate_pwa_options ($ event )
190205 {
191- // Ignore validation if icon fields are empty
192- if ($ event ['config_definition ' ]['validate ' ] !== 'pwa_options ' || (empty ($ event ['cfg_array ' ]['pwa_icon_small ' ]) && empty ($ event ['cfg_array ' ]['pwa_icon_large ' ])))
193- {
194- return ;
195- }
206+ $ type = 0 ;
207+ $ mode = 1 ;
196208
197- $ value = $ event ['cfg_array ' ][$ event [ ' config_name ' ]] ;
209+ $ validator = explode ( ' : ' , $ event ['config_definition ' ][' validate ' ]) ;
198210
199- // Don't allow empty values, if one icon is set, both must be set.
200- if (empty ($ value ))
211+ if ($ validator [$ type ] !== 'pwa_options ' )
201212 {
202- $ this ->add_error ($ event , 'PWA_IMAGE_NOT_PROVIDED ' , $ this ->language ->lang (strtoupper ($ event ['config_name ' ])));
203213 return ;
204214 }
205215
206- // Check if image is valid
207- $ image = $ this ->root_path . $ this ->config ['icons_path ' ] . '/ ' . $ value ;
208- $ image_info = $ this ->imagesize ->getImageSize ($ image );
209- if ($ image_info !== false )
216+ switch ($ validator [$ mode ])
210217 {
211- if (($ event ['config_name ' ] === 'pwa_icon_small ' && $ image_info ['width ' ] !== 192 && $ image_info ['height ' ] !== 192 ) ||
212- ($ event ['config_name ' ] === 'pwa_icon_large ' && $ image_info ['width ' ] !== 512 && $ image_info ['height ' ] !== 512 ))
213- {
214- $ this ->add_error ($ event , 'PWA_ICON_SIZE_INVALID ' , $ value );
215- }
216-
217- if ($ image_info ['type ' ] !== IMAGETYPE_PNG )
218- {
219- $ this ->add_error ($ event , 'PWA_ICON_MIME_INVALID ' , $ value );
220- }
221- }
222- else
223- {
224- $ this ->add_error ($ event , 'PWA_IMAGE_INVALID ' , $ value );
218+ case 'string ' :
219+ // Ignore validation if icon fields are empty
220+ if (empty ($ event ['cfg_array ' ]['pwa_short_name ' ]))
221+ {
222+ return ;
223+ }
224+
225+ $ short_name = $ event ['cfg_array ' ]['pwa_short_name ' ];
226+
227+ // Do not allow multibyte characters or emoji
228+ if (strlen ($ short_name ) !== mb_strlen ($ short_name , 'UTF-8 ' ))
229+ {
230+ $ this ->add_error ($ event , 'PWA_SHORT_NAME_INVALID ' );
231+ return ;
232+ }
233+
234+ // Do not allow strings longer than 12 characters
235+ if (strlen ($ short_name ) > 12 )
236+ {
237+ $ this ->add_error ($ event , 'PWA_SHORT_NAME_INVALID ' );
238+ return ;
239+ }
240+ break ;
241+
242+ case 'icons ' :
243+ // Ignore validation if icon fields are empty
244+ if (empty ($ event ['cfg_array ' ]['pwa_icon_small ' ]) && empty ($ event ['cfg_array ' ]['pwa_icon_large ' ]))
245+ {
246+ return ;
247+ }
248+
249+ $ value = $ event ['cfg_array ' ][$ event ['config_name ' ]];
250+
251+ // Don't allow empty values, if one icon is set, both must be set.
252+ if (empty ($ value ))
253+ {
254+ $ this ->add_error ($ event , 'PWA_IMAGE_NOT_PROVIDED ' , $ this ->language ->lang (strtoupper ($ event ['config_name ' ])));
255+ return ;
256+ }
257+
258+ // Check if image is valid
259+ $ image = $ this ->root_path . $ this ->config ['icons_path ' ] . '/ ' . $ value ;
260+ $ image_info = $ this ->imagesize ->getImageSize ($ image );
261+ if ($ image_info !== false )
262+ {
263+ if (($ event ['config_name ' ] === 'pwa_icon_small ' && $ image_info ['width ' ] !== 192 && $ image_info ['height ' ] !== 192 ) ||
264+ ($ event ['config_name ' ] === 'pwa_icon_large ' && $ image_info ['width ' ] !== 512 && $ image_info ['height ' ] !== 512 ))
265+ {
266+ $ this ->add_error ($ event , 'PWA_ICON_SIZE_INVALID ' , $ value );
267+ }
268+
269+ if ($ image_info ['type ' ] !== IMAGETYPE_PNG )
270+ {
271+ $ this ->add_error ($ event , 'PWA_ICON_MIME_INVALID ' , $ value );
272+ }
273+ }
274+ else
275+ {
276+ $ this ->add_error ($ event , 'PWA_IMAGE_INVALID ' , $ value );
277+ }
278+ break ;
225279 }
226280 }
227281
@@ -251,4 +305,15 @@ protected function can_use_notifications()
251305 && ANONYMOUS !== $ this ->user ->id ()
252306 && USER_IGNORE !== (int ) $ this ->user ->data ['user_type ' ];
253307 }
308+
309+ /**
310+ * Get short name from a string (strip out multibyte characters and trim to 12 characters)
311+ *
312+ * @param string $name
313+ * @return string 12 max characters string
314+ */
315+ protected function get_shortname ($ name )
316+ {
317+ return utf8_substr (preg_replace ('/[^\x20-\x7E]/ ' , '' , $ name ), 0 , 12 );
318+ }
254319}
0 commit comments