| Server IP : 103.161.17.216 / Your IP : 216.73.216.1 Web Server : nginx/1.18.0 System : Linux tipsysaigoncharming 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 7.4.3-4ubuntu2.29 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/app.houseland.info/application/controllers/admin/ |
Upload File : |
<?php
/**
* Aliases for League Provider Classes
* Make sure you have added these to your composer.json and run `composer install`
* Plenty to choose from here:
* @see http://oauth2-client.thephpleague.com/providers/thirdparty/
*/
use Greew\OAuth2\Client\Provider\Azure;
//@see https://github.com/greew/oauth2-azure-provider
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
class Smtp_oauth_microsoft extends AdminController
{
public function token()
{
$providerName = 'Microsoft';
$clientId = get_option('microsoft_mail_client_id');
$clientSecret = $this->encryption->decrypt(get_option('microsoft_mail_client_secret'));
$tenantId = get_option('microsoft_mail_azure_tenant_id');
if (!$clientId && !$clientSecret) {
die('Add client ID and Client Secret in settings.');
}
if ($tenantId) {
$providerName = 'Azure';
}
$redirectUri = admin_url('smtp_oauth_microsoft/token');
$params = [
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $redirectUri,
'accessType' => 'offline',
];
$options = [];
$provider = null;
switch ($providerName) {
case 'Microsoft':
$provider = new Microsoft($params);
$options = [
'scope' => [
'wl.imap',
'wl.offline_access',
],
];
break;
case 'Azure':
$params['tenantId'] = $tenantId;
$provider = new Azure($params);
$options = [
'scope' => [
'https://outlook.office.com/SMTP.Send',
'offline_access',
],
];
break;
}
if (null === $provider) {
exit('Provider missing');
}
if (!isset($_GET['code'])) {
//If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl($options);
$this->session->set_userdata(['oauth2state' => $provider->getState()]);
header('Location: ' . $authUrl);
exit;
//Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $this->session->userdata('oauth2state'))) {
$this->session->unset_userdata('oauth2state');
exit('Invalid state');
}
try {
//Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken(
'authorization_code',
[
'code' => $_GET['code'],
]
);
//Use this to interact with an API on the users behalf
//Use this to get a new access token if the old one expires
update_option('microsoft_mail_refresh_token', $token->getRefreshToken());
} catch(Exception $e) {
set_alert('danger', $e->getMessage());
}
redirect(admin_url('settings?group=email'));
}
}