<?php
/*========================================================================*\
|| ###################################################################### ||
|| # vBulletin 6.2.3 - Licence Number L3138846
|| # ------------------------------------------------------------------ # ||
|| # Copyright 2000-2026 MH Sub I, LLC dba vBulletin. All Rights Reserved.  # ||
|| # This file may not be redistributed in whole or significant part.   # ||
|| # ----------------- VBULLETIN IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html   # ||
|| ###################################################################### ||
\*========================================================================*/

// ######################## SET PHP ENVIRONMENT ###########################

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('CVS_REVISION', '$RCSfile$ - $Revision: 111008 $');

// #################### PRE-CACHE TEMPLATES AND DATA ######################
global $phrasegroups, $specialtemplates, $vbphrase, $vbulletin;
$phrasegroups = ['global', 'cpglobal', 'cphome', 'diagnostic', 'maintenance',];
$specialtemplates = [];

// ########################## REQUIRE BACK-END ############################
require_once(dirname(__FILE__) . '/global.php');

// ######################## CHECK ADMIN PERMISSIONS #######################
if (!can_administer('canadminsettings'))
{
	print_cp_no_permission();
}

// ############################# LOG ACTION ###############################
log_admin_action();


// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

print_cp_header($vbphrase['pushnotifications_header']);

// print_form_header2() always appends '.php?do'...
$thispagelink = 'admincp/push';


// we should add more tools here like test & mock pm|topic-notifications, diagnose errors, etc
// ###################### Reset VAPID keys #######################
if ($_REQUEST['do'] == 'vapidkeys')
{
	print_form_header2($thispagelink, 'kill_vapidkeys');

	print_table_start();
	print_table_header($vbphrase['reset_vapidkeys']);
	print_description_row($vbphrase['are_you_sure_want_to_reset_vapid_keys']);
	print_table_button_footer([['submit', $vbphrase['confirm']], ['goback', $vbphrase['cancel']]]);
}

if ($_REQUEST['do'] == 'kill_vapidkeys')
{

	// Note, it's possible for us to send a poison pill to the serviceworker, but in order to do that we have to use the
	// existing OLD app keys before we replace it.
	// For now, we added a self-check logic in the serviceworker to regularly validate (cached 1 hour) the app keys with
	// the server, and unsubscribe the user with a notification if the keys don't match.
	// I think the poison pill method would actually work a lot smoother (not have to worry about caching, etc).

	// Also note that there's a bad problem where the old app key gets stuck in the browser storage due to our js option handling,
	// so revoking can cause the browsers to get stuck in a weird state until we force clear the local storage.
	// We probably need some kind of validation in the JS to force refresh the vboption in storage every so often to avoid that.


	/** @var vB_Library_Pushnotification */
	$pushLib = vB_Library::instance('pushnotification');
	$datastore = vB::getDatastore();

	[
		'publicKey' => $publicKey,
		'privateKey' => $privateKey,
	] = $pushLib->generateVapidKeys();
	$datastore->setOption('push_publickey', $publicKey);
	$datastore->setOption('push_privatekey', $privateKey);

	print_stop_message('vapid_keys_reset_successfully');
}




print_cp_footer();

/*=========================================================================*\
|| #######################################################################
|| # Downloaded: 13:27, Sun Aug 30th 2026
|| # CVS: $RCSfile$ - $Revision: 111008 $
|| #######################################################################
\*=========================================================================*/
