Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
office365.php
1<?php
2
4
8
10{
11
12 protected function __construct()
13 {
14 $this->oauthEntity = new Office365Interface(
15 \CSocServOffice365OAuth::getOption('office365_appid'),
16 \CSocServOffice365OAuth::getOption('office365_appsecret')
17 );
18
19 // get graph universal scopes, for user profile read access
20 $this->oauthEntity->setScope($this->oauthEntity->getGraphScopes());
21 }
22
23 protected function check()
24 {
25 $provider = new \CSocServOffice365OAuth;
26
27 return $provider->checkSettings();
28 }
29
30 protected function mapUserData(array $userData)
31 {
32 return array(
33 'email' => $userData['email'],
34 'first_name' => $userData['first_name'],
35 'last_name' => $userData['last_name'],
36 'full_name' => $userData['name'],
37 'image' => $userData['image'],
38 'error' => $userData['error_description'],
39 'userPrincipalName' => $userData['userPrincipalName'] ?? '',
40 );
41 }
42
43 public static function getServiceName()
44 {
45 return 'office365';
46 }
47
48 public function getControllerUrl()
49 {
50 return \CSocServOffice365OAuth::CONTROLLER_URL;
51 }
52
53}
54
55if (Main\Loader::includeModule('socialservices'))
56{
57 class_exists('CSocServOffice365OAuth');
58
59 class Office365Interface extends \COffice365OAuthInterface
60 {
64 const OUTLOOK_API_VERSION = "/v2.0";
65
69 const OUTLOOK_RESOURCE = "https://outlook.office.com/api";
70
71 public function getStorageTokens()
72 {
73 return false;
74 }
75
81 private function refreshToOutlookAccessToken(): void
82 {
83 $this->refreshWithScopes($this->getOutlookScopes());
84 }
85
91 public function getOutlookScopes(): array
92 {
93 return [
94 'offline_access',
95 'https://outlook.office.com/IMAP.AccessAsUser.All',
96 'https://outlook.office.com/SMTP.Send',
97 ];
98 }
99
107 private function getGraphPrincipalName(bool $isSwitchedAlready = false): string
108 {
109 $httpClient = new \Bitrix\Main\Web\HttpClient();
110 $httpClient->setHeader("Authorization", "Bearer " . $this->access_token);
111 $jsonResponse = $httpClient->get($this->resource . static::VERSION . static::CONTACTS_URL);
112 try
113 {
114 $decoded = \Bitrix\Main\Web\Json::decode($jsonResponse);
115 if (!empty($decoded['userPrincipalName']) && is_string($decoded['userPrincipalName']))
116 {
117 return $decoded['userPrincipalName'];
118 }
119 else if (!empty($decoded['error']) && !$isSwitchedAlready)
120 {
122 return $this->getGraphPrincipalName(true);
123 }
124 }
125 catch (ArgumentException $e)
126 {
127 AddMessage2Log($e->getMessage(), 'mail', 2, true);
128 }
129 return '';
130 }
131
137 private function refreshToGraphAccessToken(): void
138 {
139 $this->refreshWithScopes($this->getGraphScopes());
140 }
141
149 private function refreshWithScopes(array $scopes): void
150 {
151 if (empty($this->refresh_token))
152 {
153 return;
154 }
155
156 $httpClient = new \Bitrix\Main\Web\HttpClient();
157
158 $jsonResponse = $httpClient->post(static::TOKEN_URL, [
159 'refresh_token' => $this->refresh_token,
160 'client_id' => $this->appID,
161 'client_secret' => $this->appSecret,
162 'grant_type' => 'refresh_token',
163 'scope' => implode(' ', $scopes),
164 ]);
165
166 try
167 {
168 $decoded = \Bitrix\Main\Web\Json::decode($jsonResponse);
169 if (!empty($decoded['access_token']))
170 {
171 $this->access_token = (string)$decoded['access_token'];
172 $this->refresh_token = (string)$decoded['refresh_token'];
173 $this->accessTokenExpires = (int)$decoded["expires_in"];
174 }
175 }
176 catch (ArgumentException $e)
177 {
178 AddMessage2Log($e->getMessage(), 'mail', 2, true);
179 }
180 }
181
187 public function getGraphScopes(): array
188 {
189 return [
190 'User.read',
191 'offline_access',
192 'IMAP.AccessAsUser.All',
193 'SMTP.Send',
194 ];
195 }
196
197 public function getTokenData(): array
198 {
199 return [
200 'access_token' => $this->access_token,
201 'refresh_token' => $this->refresh_token,
202 'expires_in' => $this->accessTokenExpires + time(),
203 ];
204 }
205
206 public function getCurrentUser()
207 {
208 if (empty($this->access_token))
209 {
210 return false;
211 }
212 $userPrincipalName = $this->getGraphPrincipalName();
214
215 $httpClient = new \Bitrix\Main\Web\HttpClient();
216 $httpClient->setHeader("Authorization", "Bearer ". $this->access_token);
217
218 $result = $httpClient->get(static::OUTLOOK_RESOURCE . static::OUTLOOK_API_VERSION . static::CONTACTS_URL);
219 try
220 {
221 $result = \Bitrix\Main\Web\Json::decode($result);
222 }
223 catch (ArgumentException $e)
224 {
225 AddMessage2Log($e->getMessage(), 'mail', 2, true);
226 $result = [];
227 }
228
229 if(isset($result['EmailAddress']))
230 {
231 $email = $result['EmailAddress'];
232 $emailIsIntended = false;
233 }
234 else
235 {
237 $email = $USER->GetEmail();
238 if(is_null($email))
239 {
240 $email = '';
241 }
242 $emailIsIntended = true;
243 }
244
245 return array_merge(
246 [
247 'email' => $email,
248 'emailIsIntended' => $emailIsIntended,
249 'userPrincipalName' => $userPrincipalName,
250 ],
251 $this->getTokenData()
252 );
253 }
254
255 }
256}