1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail; |
4
|
|
|
|
5
|
|
|
use Dacastro4\LaravelGmail\Traits\Configurable; |
6
|
|
|
use Google_Client; |
7
|
|
|
use Google_Service_Gmail; |
8
|
|
|
use Illuminate\Container\Container; |
9
|
|
|
use Illuminate\Support\Facades\Request; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
|
12
|
|
|
class GmailConnection extends Google_Client |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
use Configurable { |
|
|
|
|
16
|
|
|
__construct as configConstruct; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected $emailAddress; |
20
|
|
|
protected $refreshToken; |
21
|
|
|
protected $app; |
22
|
|
|
protected $accessToken; |
23
|
|
|
protected $token; |
24
|
|
|
private $configuration; |
25
|
|
|
public $userId; |
26
|
|
|
|
27
|
|
|
public function __construct($config = null, $userId = null) |
28
|
|
|
{ |
29
|
|
|
$this->app = Container::getInstance(); |
30
|
|
|
|
31
|
|
|
$this->userId = $userId; |
32
|
|
|
|
33
|
|
|
$this->configConstruct($config); |
34
|
|
|
|
35
|
|
|
$this->configuration = $config; |
36
|
|
|
|
37
|
|
|
parent::__construct($this->getConfigs()); |
38
|
|
|
|
39
|
|
|
$this->configApi(); |
40
|
|
|
|
41
|
|
|
if ($this->checkPreviouslyLoggedIn()) { |
42
|
|
|
$this->refreshTokenIfNeeded(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check and return true if the user has previously logged in without checking if the token needs to refresh |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function checkPreviouslyLoggedIn() |
53
|
|
|
{ |
54
|
|
|
$fileName = $this->getFileName(); |
55
|
|
|
$file = "gmail/tokens/$fileName.json"; |
56
|
|
|
$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt']; |
57
|
|
|
|
58
|
|
|
if (Storage::disk('local')->exists($file)) { |
59
|
|
|
if ($allowJsonEncrypt) { |
60
|
|
|
$savedConfigToken = json_decode(decrypt(Storage::disk('local')->get($file)), true); |
61
|
|
|
} else { |
62
|
|
|
$savedConfigToken = json_decode(Storage::disk('local')->get($file), true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return !empty($savedConfigToken['access_token']); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Refresh the auth token if needed |
74
|
|
|
* |
75
|
|
|
* @return mixed|null |
76
|
|
|
*/ |
77
|
|
|
private function refreshTokenIfNeeded() |
78
|
|
|
{ |
79
|
|
|
if ($this->isAccessTokenExpired()) { |
80
|
|
|
$this->fetchAccessTokenWithRefreshToken($this->getRefreshToken()); |
81
|
|
|
$token = $this->getAccessToken(); |
82
|
|
|
$this->setBothAccessToken($token); |
83
|
|
|
|
84
|
|
|
return $token; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->token; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Check if token exists and is expired |
92
|
|
|
* Throws an AuthException when the auth file its empty or with the wrong token |
93
|
|
|
* |
94
|
|
|
* |
95
|
|
|
* @return bool Returns True if the access_token is expired. |
96
|
|
|
*/ |
97
|
|
|
public function isAccessTokenExpired() |
98
|
|
|
{ |
99
|
|
|
$token = $this->getToken(); |
100
|
|
|
|
101
|
|
|
if ($token) { |
102
|
|
|
$this->setAccessToken($token); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return parent::isAccessTokenExpired(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getToken() |
109
|
|
|
{ |
110
|
|
|
return parent::getAccessToken() ?: $this->config(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function setToken($token) |
114
|
|
|
{ |
115
|
|
|
$this->setAccessToken($token); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getAccessToken() |
119
|
|
|
{ |
120
|
|
|
$token = parent::getAccessToken() ?: $this->config(); |
121
|
|
|
|
122
|
|
|
return $token; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array|string $token |
127
|
|
|
*/ |
128
|
|
|
public function setAccessToken($token) |
129
|
|
|
{ |
130
|
|
|
parent::setAccessToken($token); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $token |
135
|
|
|
*/ |
136
|
|
|
public function setBothAccessToken($token) |
137
|
|
|
{ |
138
|
|
|
$this->setAccessToken($token); |
139
|
|
|
$this->saveAccessToken($token); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Save the credentials in a file |
144
|
|
|
* |
145
|
|
|
* @param array $config |
146
|
|
|
*/ |
147
|
|
|
public function saveAccessToken(array $config) |
148
|
|
|
{ |
149
|
|
|
$disk = Storage::disk('local'); |
150
|
|
|
$fileName = $this->getFileName(); |
151
|
|
|
$file = "gmail/tokens/$fileName.json"; |
152
|
|
|
$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt']; |
153
|
|
|
$config['email'] = $this->emailAddress; |
154
|
|
|
|
155
|
|
|
if ($disk->exists($file)) { |
156
|
|
|
|
157
|
|
|
if (empty($config['email'])) { |
158
|
|
|
if ($allowJsonEncrypt) { |
159
|
|
|
$savedConfigToken = json_decode(decrypt($disk->get($file)), true); |
160
|
|
|
} else { |
161
|
|
|
$savedConfigToken = json_decode($disk->get($file), true); |
162
|
|
|
} |
163
|
|
|
if(isset( $savedConfigToken['email'])) { |
164
|
|
|
$config['email'] = $savedConfigToken['email']; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$disk->delete($file); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($allowJsonEncrypt) { |
172
|
|
|
$disk->put($file, encrypt(json_encode($config))); |
173
|
|
|
} else { |
174
|
|
|
$disk->put($file, json_encode($config)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return array|string |
181
|
|
|
* @throws \Exception |
182
|
|
|
*/ |
183
|
|
|
public function makeToken() |
184
|
|
|
{ |
185
|
|
|
if (!$this->check()) { |
186
|
|
|
$request = Request::capture(); |
187
|
|
|
$code = (string) $request->input('code', null); |
188
|
|
|
if (!is_null($code) && !empty($code)) { |
|
|
|
|
189
|
|
|
$accessToken = $this->fetchAccessTokenWithAuthCode($code); |
190
|
|
|
if($this->haveReadScope()) { |
191
|
|
|
$me = $this->getProfile(); |
192
|
|
|
if (property_exists($me, 'emailAddress')) { |
193
|
|
|
$this->emailAddress = $me->emailAddress; |
194
|
|
|
$accessToken['email'] = $me->emailAddress; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
$this->setBothAccessToken($accessToken); |
198
|
|
|
|
199
|
|
|
return $accessToken; |
200
|
|
|
} else { |
201
|
|
|
throw new \Exception('No access token'); |
202
|
|
|
} |
203
|
|
|
} else { |
204
|
|
|
return $this->getAccessToken(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Check |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
public function check() |
214
|
|
|
{ |
215
|
|
|
return !$this->isAccessTokenExpired(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Gets user profile from Gmail |
220
|
|
|
* |
221
|
|
|
* @return \Google_Service_Gmail_Profile |
222
|
|
|
*/ |
223
|
|
|
public function getProfile() |
224
|
|
|
{ |
225
|
|
|
$service = new Google_Service_Gmail($this); |
226
|
|
|
|
227
|
|
|
return $service->users->getProfile('me'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Revokes user's permission and logs them out |
232
|
|
|
*/ |
233
|
|
|
public function logout() |
234
|
|
|
{ |
235
|
|
|
$this->revokeToken(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Delete the credentials in a file |
240
|
|
|
*/ |
241
|
|
|
public function deleteAccessToken() |
242
|
|
|
{ |
243
|
|
|
$disk = Storage::disk('local'); |
244
|
|
|
$fileName = $this->getFileName(); |
245
|
|
|
$file = "gmail/tokens/$fileName.json"; |
246
|
|
|
|
247
|
|
|
$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt']; |
248
|
|
|
|
249
|
|
|
if ($disk->exists($file)) { |
250
|
|
|
$disk->delete($file); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if ($allowJsonEncrypt) { |
254
|
|
|
$disk->put($file, encrypt(json_encode([]))); |
255
|
|
|
} else { |
256
|
|
|
$disk->put($file, json_encode([])); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
private function haveReadScope() |
262
|
|
|
{ |
263
|
|
|
$scopes = $this->getUserScopes(); |
264
|
|
|
|
265
|
|
|
return in_array(Google_Service_Gmail::GMAIL_READONLY, $scopes); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* users.stop receiving push notifications for the given user mailbox. |
270
|
|
|
* |
271
|
|
|
* @param string $userEmail Email address |
272
|
|
|
* @param array $optParams |
273
|
|
|
* @return \Google_Service_Gmail_Stop |
|
|
|
|
274
|
|
|
*/ |
275
|
|
|
public function stopWatch($userEmail, $optParams = []) |
276
|
|
|
{ |
277
|
|
|
$service = new Google_Service_Gmail($this); |
278
|
|
|
|
279
|
|
|
return $service->users->stop($userEmail, $optParams); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Set up or update a push notification watch on the given user mailbox. |
284
|
|
|
* |
285
|
|
|
* @param string $userEmail Email address |
286
|
|
|
* @param Google_Service_Gmail_WatchRequest $postData |
|
|
|
|
287
|
|
|
* |
288
|
|
|
* @return \Google_Service_Gmail_WatchResponse |
289
|
|
|
*/ |
290
|
|
|
public function setWatch($userEmail, \Google_Service_Gmail_WatchRequest $postData): \Google_Service_Gmail_WatchResponse |
291
|
|
|
{ |
292
|
|
|
$service = new Google_Service_Gmail($this); |
293
|
|
|
|
294
|
|
|
return $service->users->watch($userEmail, $postData); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Lists the history of all changes to the given mailbox. History results are returned in chronological order (increasing historyId). |
299
|
|
|
* @param $userEmail |
300
|
|
|
* @param $params |
301
|
|
|
* @return \Google\Service\Gmail\ListHistoryResponse |
302
|
|
|
*/ |
303
|
|
|
public function historyList($userEmail, $params) |
304
|
|
|
{ |
305
|
|
|
$service = new Google_Service_Gmail($this); |
306
|
|
|
|
307
|
|
|
return $service->users_history->listUsersHistory($userEmail, $params); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* List the labels in the user's mailbox. |
313
|
|
|
* |
314
|
|
|
* @param $userEmail |
315
|
|
|
* |
316
|
|
|
* @return \Google\Service\Gmail\Google_Service_Gmail_ListLabelsResponse |
|
|
|
|
317
|
|
|
*/ |
318
|
|
|
public function labelsList($userEmail) |
319
|
|
|
{ |
320
|
|
|
$service = new Google_Service_Gmail($this); |
321
|
|
|
|
322
|
|
|
return $service->users_labels->listUsersLabels($userEmail); |
|
|
|
|
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Create new label by name. |
327
|
|
|
* |
328
|
|
|
* @param $userEmail |
329
|
|
|
* @param $label |
330
|
|
|
* |
331
|
|
|
* @return \Google\Service\Gmail\Google_Service_Gmail_Label |
|
|
|
|
332
|
|
|
*/ |
333
|
|
|
public function createLabel($userEmail, $label) |
334
|
|
|
{ |
335
|
|
|
$service = new Google_Service_Gmail($this); |
336
|
|
|
return $service->users_labels->create($userEmail, $label); |
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* first or create label in the user's mailbox. |
341
|
|
|
* |
342
|
|
|
* @param $userEmail |
343
|
|
|
* @param $label |
344
|
|
|
* |
345
|
|
|
* @return \Google\Service\Gmail\Google_Service_Gmail_Label |
346
|
|
|
*/ |
347
|
|
|
public function firstOrCreateLabel($userEmail, $nLabel) |
348
|
|
|
{ |
349
|
|
|
$labels = $this->labelsList($userEmail); |
350
|
|
|
$label = null; |
351
|
|
|
foreach ($labels->getLabels() as $existLabel) { |
352
|
|
|
if ($existLabel->getName() == $nLabel->getName()) { |
353
|
|
|
$label = $existLabel; |
354
|
|
|
break; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
if (is_null($label)) { |
358
|
|
|
$service = new Google_Service_Gmail($this); |
359
|
|
|
$label = $service->users_labels->create($userEmail, $nLabel); |
360
|
|
|
} |
361
|
|
|
return $label; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|