Skip to content

Commit

Permalink
Remove emoji symbols and pictographs from shortname
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Friedman <[email protected]>
  • Loading branch information
iMattPro committed Dec 4, 2024
1 parent 89eda11 commit 4e5e02a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion controller/manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use phpbb\language\language;
use phpbb\path_helper;
use phpbb\user;
use phpbb\webpushnotifications\json\sanitizer as json_sanitizer;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -65,7 +66,7 @@ public function handle(): JsonResponse

$manifest = [
'name' => $this->config['sitename'],
'short_name' => $this->config['pwa_short_name'] ?: utf8_substr(preg_replace('/[^\x21-\x7E]/', '', html_entity_decode($this->config['sitename'], ENT_QUOTES, 'UTF-8')), 0, 12),
'short_name' => $this->config['pwa_short_name'] ?: utf8_substr(preg_replace('/\s+/', '', json_sanitizer::strip_emoji($this->config['sitename'])), 0, 12),
'display' => 'standalone',
'orientation' => 'portrait',
'dir' => $this->language->lang('DIRECTION'),
Expand Down
3 changes: 2 additions & 1 deletion event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpbb\template\template;
use phpbb\user;
use phpbb\webpushnotifications\form\form_helper;
use phpbb\webpushnotifications\json\sanitizer as json_sanitizer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand Down Expand Up @@ -353,6 +354,6 @@ protected function can_use_notifications()
*/
protected function get_shortname($name)
{
return utf8_substr(preg_replace('/[^\x20-\x7E]/', '', $name), 0, 12);
return utf8_substr(preg_replace('/\s+/', '', json_sanitizer::strip_emoji($name)), 0, 12);
}
}
15 changes: 15 additions & 0 deletions json/sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,19 @@ public static function decode(string $json) : array
$data = json_decode($json, true);
return !empty($data) ? self::sanitize($data) : [];
}

/**
* Remove emoji from a string
*
* @param string $string
* @return string
*/
public static function strip_emoji(string $string) : string
{
return preg_replace(
'/[\x{1F000}-\x{1F9FF}]|[\x{2600}-\x{27FF}]/u',
'',
html_entity_decode($string, ENT_QUOTES, 'UTF-8')
);
}
}

0 comments on commit 4e5e02a

Please sign in to comment.