From de05dcf96c8d3aaa310af1d00894e48e520a811a Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Thu, 16 May 2024 19:12:34 -0700 Subject: [PATCH] [ticket/17314] Parse avatars for push notifications PHPBB3-17314 Signed-off-by: Matt Friedman --- .../container/services_notification.yml | 1 + phpBB/phpbb/notification/method/webpush.php | 52 +++++++++++++++++-- phpBB/styles/all/js/push_worker.js.twig | 2 +- .../notification_method_webpush_test.php | 14 +++++ 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/phpBB/config/default/container/services_notification.yml b/phpBB/config/default/container/services_notification.yml index 92592850f9..82d606a3f1 100644 --- a/phpBB/config/default/container/services_notification.yml +++ b/phpBB/config/default/container/services_notification.yml @@ -253,6 +253,7 @@ services: - '@log' - '@user_loader' - '@user' + - '@path_helper' - '%core.root_path%' - '%core.php_ext%' - '%tables.notification_push%' diff --git a/phpBB/phpbb/notification/method/webpush.php b/phpBB/phpbb/notification/method/webpush.php index e5b8910a07..aac88c7057 100644 --- a/phpBB/phpbb/notification/method/webpush.php +++ b/phpBB/phpbb/notification/method/webpush.php @@ -20,6 +20,7 @@ use phpbb\db\driver\driver_interface; use phpbb\form\form_helper; use phpbb\log\log_interface; use phpbb\notification\type\type_interface; +use phpbb\path_helper; use phpbb\user; use phpbb\user_loader; @@ -42,6 +43,9 @@ class webpush extends messenger_base implements extended_method_interface /** @var user */ protected $user; + /** @var path_helper */ + protected $path_helper; + /** @var string Notification Web Push table */ protected $notification_webpush_table; @@ -56,13 +60,14 @@ class webpush extends messenger_base implements extended_method_interface * @param log_interface $log * @param user_loader $user_loader * @param user $user + * @param path_helper $path_helper * @param string $phpbb_root_path * @param string $php_ext * @param string $notification_webpush_table * @param string $push_subscriptions_table */ - public function __construct(config $config, driver_interface $db, log_interface $log, user_loader $user_loader, user $user, string $phpbb_root_path, - string $php_ext, string $notification_webpush_table, string $push_subscriptions_table) + public function __construct(config $config, driver_interface $db, log_interface $log, user_loader $user_loader, user $user, path_helper $path_helper, + string $phpbb_root_path, string $php_ext, string $notification_webpush_table, string $push_subscriptions_table) { parent::__construct($user_loader, $phpbb_root_path, $php_ext); @@ -70,6 +75,7 @@ class webpush extends messenger_base implements extended_method_interface $this->db = $db; $this->log = $log; $this->user = $user; + $this->path_helper = $path_helper; $this->notification_webpush_table = $notification_webpush_table; $this->push_subscriptions_table = $push_subscriptions_table; } @@ -131,7 +137,7 @@ class webpush extends messenger_base implements extended_method_interface 'title' => strip_tags($notification->get_title()), 'text' => strip_tags($notification->get_reference()), 'url' => htmlspecialchars_decode($notification->get_url()), - 'avatar' => $notification->get_avatar(), + 'avatar' => $this->prepare_avatar($notification->get_avatar()), ]), 'notification_time' => time(), ]; @@ -429,4 +435,44 @@ class webpush extends messenger_base implements extended_method_interface $this->remove_subscriptions($remove_subscriptions); } + + /** + * Takes an avatar string (usually in full html format already) and extracts the url. + * If the avatar url is a relative path, it's converted to an absolute path. + * + * Converts: + * User avatar + * or User avatar + * into https://myboard.url/path/to/avatar=123456789.gif + * + * @param string $avatar + * @return string Absolute path to avatar image + */ + protected function prepare_avatar(string $avatar): string + { + $pattern = '/src=["\']?([^"\'>]+)["\']?/'; + + preg_match_all($pattern, $avatar, $matches); + + $path = !empty($matches[1]) ? end($matches[1]) : $avatar; + + return preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1); + } + + /** + * Returns the board url (and caches it in the function) + * + * @return string the generated board url + */ + protected function get_board_url(): string + { + static $board_url; + + if (empty($board_url)) + { + $board_url = generate_board_url() . '/'; + } + + return $board_url; + } } diff --git a/phpBB/styles/all/js/push_worker.js.twig b/phpBB/styles/all/js/push_worker.js.twig index 8d6ec3c6af..428c21114f 100644 --- a/phpBB/styles/all/js/push_worker.js.twig +++ b/phpBB/styles/all/js/push_worker.js.twig @@ -36,7 +36,7 @@ self.addEventListener('push', event => { const options = { body: responseBody, data: response, - icon: response.avatar.src, + icon: response.avatar, }; self.registration.showNotification(response.heading, options); }); diff --git a/tests/notification/notification_method_webpush_test.php b/tests/notification/notification_method_webpush_test.php index ff95c74b09..070f9231e2 100644 --- a/tests/notification/notification_method_webpush_test.php +++ b/tests/notification/notification_method_webpush_test.php @@ -122,6 +122,18 @@ class notification_method_webpush_test extends phpbb_tests_notification_base $phpEx ); + $request = new \phpbb_mock_request; + $symfony_request = new \phpbb\symfony_request( + $request + ); + $filesystem = new \phpbb\filesystem\filesystem(); + $phpbb_path_helper = new \phpbb\path_helper( + $symfony_request, + $request, + $phpbb_root_path, + $phpEx + ); + $log_table = 'phpbb_log'; $this->log = new \phpbb\log\log($this->db, $user, $auth, $this->phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, $log_table); @@ -139,6 +151,7 @@ class notification_method_webpush_test extends phpbb_tests_notification_base $phpbb_container->set('log', $this->log); $phpbb_container->set('text_formatter.utils', new \phpbb\textformatter\s9e\utils()); $phpbb_container->set('dispatcher', $this->phpbb_dispatcher); + $phpbb_container->set('path_helper', $phpbb_path_helper); $phpbb_container->setParameter('core.root_path', $phpbb_root_path); $phpbb_container->setParameter('core.php_ext', $phpEx); $phpbb_container->setParameter('tables.notifications', 'phpbb_notifications'); @@ -177,6 +190,7 @@ class notification_method_webpush_test extends phpbb_tests_notification_base $phpbb_container->get('log'), $phpbb_container->get('user_loader'), $phpbb_container->get('user'), + $phpbb_container->get('path_helper'), $phpbb_root_path, $phpEx, $phpbb_container->getParameter('tables.notification_push'),