<?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   # ||
|| ###################################################################### ||
\*========================================================================*/

class vB5_Frontend_Controller_Oauth2 extends vB5_Frontend_Controller
{
	private function getProvider() : string|bool
	{
		$provider = preg_replace('#[^\w\-\.]#', '', $_REQUEST['provider'] ?? '');
		if (empty($provider))
		{
			$this->sendAsJson(['errors' => ['invalid_provider']]);
			return false;
		}

		return $provider;
	}

	public function actionIndex()
	{
		// just debuggint that we don't need a separate route.
		echo "Hello World";
	}

	public function actionCallback()
	{
		$provider = $this->getProvider();
		if (!$provider)
		{
			return;
		}

		$api = Api_InterfaceAbstract::instance();
		$code = $_GET['code'] ?? '';
		$state = $_GET['state'] ?? '';

		$result = $api->callApi($provider . ':ExternalLogin', 'confirmToken', [$code, $state]);

		if(isset($result['errors']))
		{
			return $this->showErrorPageBare($result['errors']);
		}

		$result = $api->callApi($provider . ':ExternalLogin', 'getOauthCallbackTemplate', [$code, $state]);
		//Ping the opener.
		//Due to issues with firefox not allowing us to close the window the html got too
		//complex to hardcode here.  Make into a template but don't use the full page framework.
		$ping = new vB5_Template($result['template']);
		foreach ($result['data'] AS $__k => $__v)
		{
			$ping->register($__k, $__v);
		}
		echo $ping->render();;
	}

	public function actionStartTokenRefresh()
	{
		$provider = $this->getProvider();
		if (!$provider)
		{
			return;
		}

		$api = Api_InterfaceAbstract::instance();
		$result = $api->callApi($provider . ':ExternalLogin', 'getTokenRedirect', []);
		if(!empty($result['errors']))
		{
			$this->sendAsJson($result);
			return true;
		}

		header('Location: ' . $result['url']);
	}

	public function actionLogin()
	{
		$provider = $this->getProvider();
		if (!$provider)
		{
			return;
		}

		$url = $_REQUEST['url'] ?? '';
		$userid = $_REQUEST['userid'] ?? null;

		$api = Api_InterfaceAbstract::instance();
		$result = $api->callApi($provider . ':ExternalLogin', 'verifyAuthAndLogin', [$url, $userid]);
		if ($result['success'])
		{
			vB5_Cookie::setSessionCookies($result['login'], 'external', true);
			$newUserInfo = $api->callApi('user', 'fetchUserinfo', ['nocache' => true], true);
			$result['newtoken'] = $newUserInfo['securitytoken'];
		}
		$this->sendAsJson($result);
	}
}
/*=========================================================================*\
|| #######################################################################
|| # Downloaded: 13:27, Sun Aug 30th 2026
|| # CVS: $RCSfile$ - $Revision: 114817 $
|| #######################################################################
\*=========================================================================*/
