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 }
@@ -130,7 +136,7 @@ public function notify()
130136 'title ' => strip_tags ($ notification ->get_title ()),
131137 'text ' => strip_tags ($ notification ->get_reference ()),
132138 'url ' => htmlspecialchars_decode ($ notification ->get_url ()),
133- 'avatar ' => $ notification ->get_avatar (),
139+ 'avatar ' => $ this -> prepare_avatar ( $ notification ->get_avatar () ),
134140 ]),
135141 'notification_time ' => time (),
136142 ];
@@ -428,4 +434,44 @@ protected function clean_expired_subscriptions(array $user_subscription_map, arr
428434
429435 $ this ->remove_subscriptions ($ remove_subscriptions );
430436 }
437+
438+ /**
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
446+ *
447+ * @param string $avatar
448+ * @return string Absolute path to avatar image
449+ */
450+ protected function prepare_avatar ($ avatar )
451+ {
452+ $ pattern = '/src=[" \']?([^" \'>]+)[" \']?/ ' ;
453+
454+ preg_match_all ($ pattern , $ avatar , $ matches );
455+
456+ $ path = !empty ($ matches [1 ]) ? end ($ matches [1 ]) : $ avatar ;
457+
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 ;
476+ }
431477}
0 commit comments