1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\GMailConnector;
9:
10: /**
11: * Adds ability to work with GMail inside Mail module.
12: *
13: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
14: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
15: * @copyright Copyright (c) 2023, Afterlogic Corp.
16: *
17: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractModule
20: {
21: protected $sService = 'gmail';
22:
23: protected $aRequireModules = array(
24: 'OAuthIntegratorWebclient',
25: 'GoogleAuthWebclient'
26: );
27:
28: protected function issetScope($sScope)
29: {
30: return \in_array($sScope, \explode(' ', $this->getConfig('Scopes')));
31: }
32:
33: public function init()
34: {
35: $this->subscribeEvent('PopulateScopes', array($this, 'onPopulateScopes'));
36:
37: $this->subscribeEvent('Mail::BeforeDeleteAccount', array($this, 'onBeforeDeleteAccount'));
38: $this->subscribeEvent('OAuthIntegratorAction', array($this, 'onOAuthIntegratorAction'));
39: $this->subscribeEvent('ResetAccessToken', array($this, 'onResetAccessToken'));
40: $this->subscribeEvent('GetAccessToken', array($this, 'onGetAccessToken'));
41: }
42:
43: /**
44: * Deletes cPanel account, its aliases, forward, autoresponder and filters.
45: * @param array $aArgs
46: * @param mixed $mResult
47: */
48: public function onBeforeDeleteAccount($aArgs, &$mResult)
49: {
50: $oAccount = $aArgs['Account'];
51: if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) {
52: \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator()->DeleteAccount(
53: $oAccount->XOAuth,
54: $oAccount->Email
55: );
56: }
57: }
58:
59:
60: public function onPopulateScopes($sScope, &$aResult)
61: {
62: $aScopes = \explode('|', $sScope);
63: foreach ($aScopes as $sScope) {
64: if ($sScope === 'mail') {
65: $aResult[] = 'https://mail.google.com/';
66: }
67: }
68: }
69:
70: /**
71: * Passes data to connect to service.
72: *
73: * @ignore
74: * @param string $aArgs Service type to verify if data should be passed.
75: * @param boolean|array $mResult variable passed by reference to take the result.
76: */
77: public function onOAuthIntegratorAction($aArgs, &$mResult)
78: {
79: if ($aArgs['Service'] === $this->sService) {
80: $sOAuthScopes = isset($_COOKIE['oauth-scopes']) ? $_COOKIE['oauth-scopes'] : '';
81: $aGoogleScopes = [
82: 'https://www.googleapis.com/auth/userinfo.email',
83: 'https://www.googleapis.com/auth/userinfo.profile'
84: ];
85: $this->broadcastEvent('PopulateScopes', $sOAuthScopes, $aGoogleScopes);
86:
87: $mResult = false;
88: $oConnector = new Classes\Connector($this);
89: if ($oConnector) {
90: $oGoogleModule = \Aurora\System\Api::GetModule('Google');
91: if ($oGoogleModule) {
92: $sId = $oGoogleModule->getConfig('Id');
93: $sSecret = $oGoogleModule->getConfig('Secret');
94:
95: $mResult = $oConnector->Init(
96: $sId,
97: $sSecret,
98: [$sOAuthScopes, \implode(' ', $aGoogleScopes)]
99: );
100: if (isset($mResult['type'])) {
101: $mResult['type'] = $this->sService;
102:
103: $oAccount = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator()->GetAccount($mResult['type'], $mResult['email']);
104: if ($oAccount instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount) {
105: $mResult = false;
106: }
107: }
108: }
109: }
110: return true;
111: }
112: }
113:
114: public function onResetAccessToken($aArgs)
115: {
116: if ($aArgs['Service'] === $this->sService) {
117: $oConnector = new Classes\Connector($this);
118: if ($oConnector) {
119: $oGoogleModule = \Aurora\System\Api::GetModule('Google');
120: if ($oGoogleModule && $oGoogleModule->getConfig('EnableModule')) {
121: $mResult = $oConnector->ResetAccessToken(
122: $oGoogleModule->getConfig('Id'),
123: $oGoogleModule->getConfig('Secret')
124: );
125: }
126: }
127: }
128: }
129:
130: public function onGetAccessToken($aArgs, &$mResult)
131: {
132: if ($aArgs['Service'] === $this->sService && isset($aArgs['Account'])) {
133: $mResult = false;
134: $oAccount = $aArgs['Account'];
135: $oTokenData = \json_decode($oAccount->AccessToken);
136: if ($oTokenData) {
137: $iCreated = (int) $oTokenData->created;
138: $iExpiresIn = (int) $oTokenData->expires_in;
139: if (time() > ($iCreated + $iExpiresIn) && isset($oAccount->RefreshToken)) {
140: $oGoogleModule = \Aurora\System\Api::GetModule('Google');
141: if ($oGoogleModule) {
142: $oConnector = new Classes\Connector($this);
143: $aResult = $oConnector->RefreshAccessToken(
144: $oGoogleModule->getConfig('Id'),
145: $oGoogleModule->getConfig('Secret'),
146: $oAccount->RefreshToken
147: );
148: if (isset($aResult['access_token'])) {
149: $oTokenData->access_token = $aResult['access_token'];
150: $oTokenData->created = time();
151: $oTokenData->expires_in = $aResult['expires_in'];
152:
153: $mResult = $oTokenData->access_token;
154:
155: $oAccount->AccessToken = \json_encode($oTokenData);
156: $oAccount->Save();
157: }
158: }
159: } else {
160: $mResult = $oTokenData->access_token;
161: }
162: }
163:
164: return true;
165: }
166: }
167: }
168: