1414use phpbb \controller \helper as controller_helper ;
1515use phpbb \db \driver \driver_interface ;
1616use phpbb \exception \http_exception ;
17+ use phpbb \language \language ;
18+ use phpbb \notification \manager ;
1719use phpbb \webpushnotifications \form \form_helper ;
1820use phpbb \webpushnotifications \json \sanitizer as json_sanitizer ;
1921use phpbb \path_helper ;
@@ -44,6 +46,12 @@ class webpush
4446 /** @var form_helper */
4547 protected $ form_helper ;
4648
49+ /** @var language */
50+ protected $ language ;
51+
52+ /** @var manager */
53+ protected $ notification_manager ;
54+
4755 /** @var path_helper */
4856 protected $ path_helper ;
4957
@@ -69,20 +77,24 @@ class webpush
6977 * @param controller_helper $controller_helper
7078 * @param driver_interface $db
7179 * @param form_helper $form_helper
80+ * @param language $language
81+ * @param manager $notification_manager
7282 * @param path_helper $path_helper
7383 * @param request_interface $request
7484 * @param user $user
7585 * @param Environment $template
7686 * @param string $notification_webpush_table
7787 * @param string $push_subscriptions_table
7888 */
79- public function __construct (config $ config , controller_helper $ controller_helper , driver_interface $ db , form_helper $ form_helper , path_helper $ path_helper ,
80- request_interface $ request , user $ user , Environment $ template , string $ notification_webpush_table , string $ push_subscriptions_table )
89+ public function __construct (config $ config , controller_helper $ controller_helper , driver_interface $ db , form_helper $ form_helper , language $ language , manager $ notification_manager ,
90+ path_helper $ path_helper , request_interface $ request , user $ user , Environment $ template , string $ notification_webpush_table , string $ push_subscriptions_table )
8191 {
8292 $ this ->config = $ config ;
8393 $ this ->controller_helper = $ controller_helper ;
8494 $ this ->db = $ db ;
8595 $ this ->form_helper = $ form_helper ;
96+ $ this ->language = $ language ;
97+ $ this ->notification_manager = $ notification_manager ;
8698 $ this ->path_helper = $ path_helper ;
8799 $ this ->request = $ request ;
88100 $ this ->user = $ user ;
@@ -143,7 +155,7 @@ private function get_user_notifications(): string
143155 $ notification_data = $ this ->db ->sql_fetchfield ('push_data ' );
144156 $ this ->db ->sql_freeresult ($ result );
145157
146- return $ notification_data ;
158+ return $ this -> get_notification_data ( $ notification_data) ;
147159 }
148160
149161 /**
@@ -174,23 +186,52 @@ private function get_anonymous_notifications(): string
174186 $ push_token = $ notification_row ['push_token ' ];
175187
176188 // Check if passed push token is valid
177- $ sql = 'SELECT user_form_salt
189+ $ sql = 'SELECT user_form_salt, user_lang
178190 FROM ' . USERS_TABLE . '
179191 WHERE user_id = ' . (int ) $ user_id ;
180192 $ result = $ this ->db ->sql_query ($ sql );
181- $ user_form_token = $ this ->db ->sql_fetchfield ( ' user_form_salt ' );
193+ $ row = $ this ->db ->sql_fetchrow ( $ result );
182194 $ this ->db ->sql_freeresult ($ result );
183195
196+ $ user_form_token = $ row ['user_form_salt ' ];
197+ $ user_lang = $ row ['user_lang ' ];
198+
184199 $ expected_push_token = hash ('sha256 ' , $ user_form_token . $ push_token );
185200 if ($ expected_push_token === $ token )
186201 {
187- return $ notification_data ;
202+ if ($ user_lang !== $ this ->language ->get_used_language ())
203+ {
204+ $ this ->language ->set_user_language ($ user_lang , true );
205+ }
206+ return $ this ->get_notification_data ($ notification_data );
188207 }
189208 }
190209
191210 throw new http_exception (Response::HTTP_FORBIDDEN , 'NO_AUTH_OPERATION ' );
192211 }
193212
213+ private function get_notification_data (string $ notification_data ): string
214+ {
215+ $ row_data = json_decode ($ notification_data , true );
216+
217+ // Old notification data is pre-parsed and just needs to be returned
218+ if (isset ($ row_data ['heading ' ]))
219+ {
220+ return $ notification_data ;
221+ }
222+
223+ // Get notification from row_data
224+ $ notification = $ this ->notification_manager ->get_item_type_class ($ row_data ['notification_type_name ' ], $ row_data );
225+
226+ return json_encode ([
227+ 'heading ' => $ this ->config ['sitename ' ],
228+ 'title ' => strip_tags ($ notification ->get_title ()),
229+ 'text ' => strip_tags ($ notification ->get_reference ()),
230+ 'url ' => htmlspecialchars_decode ($ notification ->get_url ()),
231+ 'avatar ' => $ notification ->get_avatar (),
232+ ]);
233+ }
234+
194235 /**
195236 * Handle request to push worker javascript
196237 *
0 commit comments