Skip to content

Commit 965c753

Browse files
committed
Alternate multi lang issue fix
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent d8e1884 commit 965c753

3 files changed

Lines changed: 50 additions & 13 deletions

File tree

config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ services:
4848
- '@controller.helper'
4949
- '@dbal.conn'
5050
- '@phpbb.wpn.form_helper'
51+
- '@language'
52+
- '@notification_manager'
5153
- '@path_helper'
5254
- '@request'
5355
- '@user'

notification/method/webpush.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,7 @@ public function notify()
144144
{
145145
$data = $notification->get_insert_array();
146146
$data += [
147-
'push_data' => json_encode([
148-
'heading' => $this->config['sitename'],
149-
'title' => strip_tags($notification->get_title()),
150-
'text' => strip_tags($notification->get_reference()),
151-
'url' => htmlspecialchars_decode($notification->get_url()),
152-
'avatar' => $this->prepare_avatar($notification->get_avatar()),
153-
]),
147+
'push_data' => json_encode(array_merge($notification->get_insert_array(), ['notification_type_name' => $notification->get_type()])),
154148
'notification_time' => time(),
155149
'push_token' => hash('sha256', random_bytes(32))
156150
];

ucp/controller/webpush.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use phpbb\controller\helper as controller_helper;
1515
use phpbb\db\driver\driver_interface;
1616
use phpbb\exception\http_exception;
17+
use phpbb\language\language;
18+
use phpbb\notification\manager;
1719
use phpbb\webpushnotifications\form\form_helper;
1820
use phpbb\webpushnotifications\json\sanitizer as json_sanitizer;
1921
use 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

Comments
 (0)