1717use phpbb \log \log_interface ;
1818use phpbb \notification \method \messenger_base ;
1919use phpbb \notification \type \type_interface ;
20+ use phpbb \path_helper ;
2021use phpbb \user ;
2122use phpbb \user_loader ;
2223use phpbb \webpushnotifications \form \form_helper ;
@@ -41,6 +42,9 @@ class webpush extends messenger_base implements extended_method_interface
4142 /** @var user */
4243 protected $ user ;
4344
45+ /** @var path_helper */
46+ protected $ path_helper ;
47+
4448 /** @var string Notification Web Push table */
4549 protected $ notification_webpush_table ;
4650
@@ -55,20 +59,22 @@ class webpush extends messenger_base implements extended_method_interface
5559 * @param log_interface $log
5660 * @param user_loader $user_loader
5761 * @param user $user
62+ * @param path_helper $path_helper
5863 * @param string $phpbb_root_path
5964 * @param string $php_ext
6065 * @param string $notification_webpush_table
6166 * @param string $push_subscriptions_table
6267 */
63- public function __construct (config $ config , driver_interface $ db , log_interface $ log , user_loader $ user_loader , user $ user , string $ phpbb_root_path ,
64- string $ php_ext , string $ notification_webpush_table , string $ push_subscriptions_table )
68+ public function __construct (config $ config , driver_interface $ db , log_interface $ log , user_loader $ user_loader , user $ user , path_helper $ path_helper ,
69+ string $ phpbb_root_path , string $ php_ext , string $ notification_webpush_table , string $ push_subscriptions_table )
6570 {
6671 parent ::__construct ($ user_loader , $ phpbb_root_path , $ php_ext );
6772
6873 $ this ->config = $ config ;
6974 $ this ->db = $ db ;
7075 $ this ->log = $ log ;
7176 $ this ->user = $ user ;
77+ $ this ->path_helper = $ path_helper ;
7278 $ this ->notification_webpush_table = $ notification_webpush_table ;
7379 $ this ->push_subscriptions_table = $ push_subscriptions_table ;
7480 }
@@ -430,10 +436,16 @@ protected function clean_expired_subscriptions(array $user_subscription_map, arr
430436 }
431437
432438 /**
433- * Takes an avatar string (usually in full html format already) and extracts the src path.
439+ * Takes an avatar string (usually in full html format already) and extracts the url.
440+ * If the avatar url is a relative path, it's converted to an absolute path.
441+ *
442+ * Converts:
443+ * <img class="avatar" src="./path/to/avatar=123456789.gif" width="123" height="123" alt="User avatar" />
444+ * or <img class="avatar" src="./styles/prosilver/theme/images/no_avatar.gif" data-src="./path/to/avatar=123456789.gif" width="123" height="123" alt="User avatar" />
445+ * into https://myboard.url/path/to/avatar=123456789.gif
434446 *
435447 * @param string $avatar
436- * @return array
448+ * @return string Absolute path to avatar image
437449 */
438450 protected function prepare_avatar ($ avatar )
439451 {
@@ -443,6 +455,23 @@ protected function prepare_avatar($avatar)
443455
444456 $ path = !empty ($ matches [1 ]) ? end ($ matches [1 ]) : $ avatar ;
445457
446- return ['src ' => preg_replace ('#^\./# ' , '/ ' , $ path )];
458+ return preg_replace ('#^ ' . preg_quote ($ this ->path_helper ->get_web_root_path (), '# ' ) . '# ' , $ this ->get_board_url (), $ path , 1 );
459+ }
460+
461+ /**
462+ * Returns the board url (and caches it in the function)
463+ *
464+ * @return string the generated board url
465+ */
466+ protected function get_board_url ()
467+ {
468+ static $ board_url ;
469+
470+ if (empty ($ board_url ))
471+ {
472+ $ board_url = generate_board_url () . '/ ' ;
473+ }
474+
475+ return $ board_url ;
447476 }
448477}
0 commit comments