diff --git a/phpBB/includes/ucp/ucp_notifications.php b/phpBB/includes/ucp/ucp_notifications.php index 9e03e68509..62d7fbd112 100644 --- a/phpBB/includes/ucp/ucp_notifications.php +++ b/phpBB/includes/ucp/ucp_notifications.php @@ -14,6 +14,11 @@ /** * @ignore */ + +use phpbb\controller\helper; +use phpbb\form\form_helper; +use phpbb\notification\method\extended_method_interface; + if (!defined('IN_PHPBB')) { exit; @@ -23,17 +28,29 @@ class ucp_notifications { public $u_action; + private const FORM_TOKEN_NAME = 'ucp_notification'; + + /** @var helper */ + private helper $controller_helper; + + /** @var form_helper */ + private form_helper $form_helper; + public function main($id, $mode) { global $config, $template, $user, $request, $phpbb_container, $phpbb_dispatcher; global $phpbb_root_path, $phpEx; - add_form_key('ucp_notification'); + add_form_key(self::FORM_TOKEN_NAME); $start = $request->variable('start', 0); $form_time = $request->variable('form_time', 0); $form_time = ($form_time <= 0 || $form_time > time()) ? time() : $form_time; + + $this->controller_helper = $phpbb_container->get('controller.helper'); + $this->form_helper = $phpbb_container->get('form_helper'); + /* @var $phpbb_notifications \phpbb\notification\manager */ $phpbb_notifications = $phpbb_container->get('notification_manager'); @@ -48,7 +65,7 @@ class ucp_notifications // Add/remove subscriptions if ($request->is_set_post('submit')) { - if (!check_form_key('ucp_notification')) + if (!check_form_key(self::FORM_TOKEN_NAME)) { trigger_error('FORM_INVALID'); } @@ -103,15 +120,12 @@ class ucp_notifications trigger_error($message); } - $this->output_notification_methods($phpbb_notifications, $template, $user, 'notification_methods'); + $this->output_notification_methods($phpbb_notifications, $template, $user); $this->output_notification_types($subscriptions, $phpbb_notifications, $template, $user, $phpbb_dispatcher, 'notification_types'); - /** @var \phpbb\controller\helper $controller_helper */ - $controller_helper = $phpbb_container->get('controller.helper'); - $template->assign_vars([ - 'T_WEBPUSH_JS_PATH' => $controller_helper->route('phpbb_ucp_push_js_controller'), + 'FORM_TOKENS' => $this->form_helper->get_form_tokens(self::FORM_TOKEN_NAME), ]); $this->tpl_name = 'ucp_notifications_options'; @@ -145,7 +159,7 @@ class ucp_notifications // Mark specific notifications read if ($request->is_set_post('submit')) { - if (!check_form_key('ucp_notification')) + if (!check_form_key(self::FORM_TOKEN_NAME)) { trigger_error('FORM_INVALID'); } @@ -273,35 +287,18 @@ class ucp_notifications { $notification_methods = $phpbb_notifications->get_subscription_methods(); - if (isset($notification_methods['notification.method.webpush'])) + foreach ($notification_methods as $method_data) { - $this->output_webpush_data($template); - } + if ($method_data['method'] instanceof extended_method_interface) + { + $ucp_template_data = $method_data['method']->get_ucp_template_data($this->controller_helper, $this->form_helper); + $template->assign_vars($ucp_template_data); + } - foreach ($notification_methods as $method => $method_data) - { $template->assign_block_vars($block, array( 'METHOD' => $method_data['id'], - 'NAME' => $user->lang($method_data['lang']), )); } } - - /** - * Output data for webpush - * - * @param \phpbb\template\template $template - * - * @return void - */ - protected function output_webpush_data(\phpbb\template\template $template): void - { - global $config; - - $template->assign_vars([ - 'NOTIFICATIONS_WEBPUSH_ENABLE' => true, // already checked, otherwise we wouldn't be here - 'NOTIFICATIONS_WEBPUSH_VAPID_PUBLIC' => $config['webpush_vapid_public'], - ]); - } } diff --git a/phpBB/phpbb/notification/method/extended_method_interface.php b/phpBB/phpbb/notification/method/extended_method_interface.php new file mode 100644 index 0000000000..866288ec3e --- /dev/null +++ b/phpBB/phpbb/notification/method/extended_method_interface.php @@ -0,0 +1,29 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\notification\method; + +use phpbb\controller\helper; +use phpbb\form\form_helper; + +interface extended_method_interface extends method_interface +{ + /** + * Get UCP template data for type + * + * @param helper $controller_helper + * @param form_helper $form_helper + * @return array Template data + */ + public function get_ucp_template_data(helper $controller_helper, form_helper $form_helper): array; +} diff --git a/phpBB/phpbb/notification/method/webpush.php b/phpBB/phpbb/notification/method/webpush.php index c4d44946ed..273ddcddd5 100644 --- a/phpBB/phpbb/notification/method/webpush.php +++ b/phpBB/phpbb/notification/method/webpush.php @@ -15,7 +15,9 @@ namespace phpbb\notification\method; use Minishlink\WebPush\Subscription; use phpbb\config\config; +use phpbb\controller\helper; use phpbb\db\driver\driver_interface; +use phpbb\form\form_helper; use phpbb\log\log_interface; use phpbb\notification\type\type_interface; use phpbb\user; @@ -26,7 +28,7 @@ use phpbb\user_loader; * This class handles sending push messages for notifications */ -class webpush extends messenger_base +class webpush extends messenger_base implements extended_method_interface { /** @var config */ protected $config; @@ -320,6 +322,33 @@ class webpush extends messenger_base return array_intersect_key($data, $row); } + public function get_ucp_template_data(helper $controller_helper, form_helper $form_helper): array + { + $subscription_map = $this->get_user_subscription_map([$this->user->id()]); + $subscriptions = []; + + if (isset($subscription_map[$this->user->id()])) + { + foreach ($subscription_map[$this->user->id()] as $subscription) + { + $subscriptions[] = [ + 'endpoint' => $subscription['endpoint'], + 'expirationTime' => $subscription['expiration_time'], + ]; + } + } + + return [ + 'NOTIFICATIONS_WEBPUSH_ENABLE' => true, + 'U_WEBPUSH_SUBSCRIBE' => $controller_helper->route('phpbb_ucp_push_subscribe_controller'), + 'U_WEBPUSH_UNSUBSCRIBE' => $controller_helper->route('phpbb_ucp_push_unsubscribe_controller'), + 'VAPID_PUBLIC_KEY' => $this->config['webpush_vapid_public'], + 'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_ucp_push_worker_controller'), + 'SUBSCRIPTIONS' => $subscriptions, + 'WEBPUSH_FORM_TOKENS' => $form_helper->get_form_tokens(\phpbb\ucp\controller\webpush::FORM_TOKEN_UCP), + ]; + } + /** * Get subscriptions for notify users * @@ -332,7 +361,7 @@ class webpush extends messenger_base // Get subscriptions for users $user_subscription_map = []; - $sql = 'SELECT subscription_id, user_id, endpoint, p256dh, auth + $sql = 'SELECT subscription_id, user_id, endpoint, p256dh, auth, expiration_time FROM ' . $this->push_subscriptions_table . ' WHERE ' . $this->db->sql_in_set('user_id', $notify_users); $result = $this->db->sql_query($sql); diff --git a/phpBB/phpbb/ucp/controller/webpush.php b/phpBB/phpbb/ucp/controller/webpush.php index e292f35fac..a9b5e133cf 100644 --- a/phpBB/phpbb/ucp/controller/webpush.php +++ b/phpBB/phpbb/ucp/controller/webpush.php @@ -33,7 +33,7 @@ use Twig\Error\SyntaxError; class webpush { /** @var string UCP form token name */ - private const FORM_TOKEN_UCP = 'ucp_webpush'; + public const FORM_TOKEN_UCP = 'ucp_webpush'; /** @var config */ protected $config;