1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail; |
4
|
|
|
|
5
|
|
|
use Google_Client; |
6
|
|
|
use Google_Service_Gmail; |
7
|
|
|
use Illuminate\Container\Container; |
8
|
|
|
use Illuminate\Config\Repository as Config; |
9
|
|
|
use Illuminate\Support\Facades\File; |
10
|
|
|
use Illuminate\Support\Facades\Request; |
11
|
|
|
|
12
|
|
|
class GmailConnection extends Google_Client |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
protected $emailAddress; |
16
|
|
|
protected $refreshToken; |
17
|
|
|
protected $app; |
18
|
|
|
protected $accessToken; |
19
|
|
|
protected $token; |
20
|
|
|
private $config; |
|
|
|
|
21
|
|
|
|
22
|
|
|
public function __construct( Config $config = null ) |
23
|
|
|
{ |
24
|
|
|
$this->app = Container::getInstance(); |
25
|
|
|
$this->config = $config; |
26
|
|
|
parent::__construct( $this->getConfigs() ); |
27
|
|
|
$this->setScopes( $this->getUserScopes() ); |
28
|
|
|
$this->setAccessType( 'offline' ); |
29
|
|
|
if ( $this->check() ) { |
30
|
|
|
$this->refreshTokenIfNeeded(); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getAccessToken() |
35
|
|
|
{ |
36
|
|
|
$token = parent::getAccessToken() ?: $this->config(); |
37
|
|
|
|
38
|
|
|
return $token; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array|string |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
public function makeToken() |
46
|
|
|
{ |
47
|
|
|
if ( ! $this->check() ) { |
48
|
|
|
$request = Request::capture(); |
49
|
|
|
$code = $request->input( 'code', null ); |
50
|
|
|
if ( $code ) { |
51
|
|
|
$accessToken = $this->fetchAccessTokenWithAuthCode( $code ); |
52
|
|
|
$me = $this->getProfile(); |
53
|
|
|
if ( $me ) { |
54
|
|
|
$this->emailAddress = $me->emailAddress; |
55
|
|
|
} |
56
|
|
|
$this->setAccessToken( $accessToken ); |
57
|
|
|
|
58
|
|
|
return $accessToken; |
59
|
|
|
} else { |
60
|
|
|
throw new \Exception( 'No access token' ); |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
return $this->getAccessToken(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setToken( $token ) |
68
|
|
|
{ |
69
|
|
|
$this->setAccessToken( $token ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function check() |
73
|
|
|
{ |
74
|
|
|
return ! $this->isAccessTokenExpired(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function isAccessTokenExpired() |
78
|
|
|
{ |
79
|
|
|
$token = parent::getAccessToken() ?: $this->config(); |
|
|
|
|
80
|
|
|
if ( $token ) { |
81
|
|
|
$this->setAccessToken( $token ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return parent::isAccessTokenExpired(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function logout() |
88
|
|
|
{ |
89
|
|
|
$this->revokeToken(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
private function refreshTokenIfNeeded() |
94
|
|
|
{ |
95
|
|
|
if ( $this->isAccessTokenExpired() ) { |
96
|
|
|
$this->fetchAccessTokenWithRefreshToken( $this->getRefreshToken() ); |
97
|
|
|
$token = $this->getAccessToken(); |
98
|
|
|
$this->setAccessToken( $token ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \Google_Service_Gmail_Profile |
104
|
|
|
*/ |
105
|
|
|
public function getProfile() |
106
|
|
|
{ |
107
|
|
|
$service = new Google_Service_Gmail( $this ); |
108
|
|
|
|
109
|
|
|
return $service->users->getProfile( 'me' ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function setAccessToken( $token ) |
113
|
|
|
{ |
114
|
|
|
parent::setAccessToken( $token ); |
115
|
|
|
$this->saveAccessToken( $token ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Save the credentials in a file |
120
|
|
|
* |
121
|
|
|
* @param array $config |
122
|
|
|
*/ |
123
|
|
|
public function saveAccessToken( array $config ) |
124
|
|
|
{ |
125
|
|
|
$fileName = $this->getFileName(); |
126
|
|
|
$file = $file = storage_path( $fileName ); |
127
|
|
|
|
128
|
|
|
File::delete( $file ); |
129
|
|
|
|
130
|
|
|
$config[ 'email' ] = $this->emailAddress; |
131
|
|
|
File::put( $file, json_encode( $config ) ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Delete the credentials in a file |
136
|
|
|
*/ |
137
|
|
|
public function deleteAccessToken() |
138
|
|
|
{ |
139
|
|
|
$fileName = $this->getFileName(); |
140
|
|
|
$file = $file = storage_path( $fileName ); |
141
|
|
|
|
142
|
|
|
File::delete( $file ); |
143
|
|
|
|
144
|
|
|
File::put( $file, json_encode( [] ) ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function getConfigs() |
151
|
|
|
{ |
152
|
|
|
return [ |
153
|
|
|
'client_secret' => $this->config->get( 'gmail.client_secret' ), |
154
|
|
|
'client_id' => $this->config->get( 'gmail.client_id' ), |
155
|
|
|
'redirect_uri' => url( $this->config->get( 'gmail.redirect_url', '/' ) ), |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function config( $string = null, $email = null ) |
160
|
|
|
{ |
161
|
|
|
$email = $email ?: $this->emailAddress; |
162
|
|
|
$file = storage_path( "gmail-{$email}json" ); |
163
|
|
|
|
164
|
|
|
if ( File::exists( $file ) ) { |
165
|
|
|
$config = json_decode( |
166
|
|
|
File::get( $file ), |
167
|
|
|
true |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
if ( $string ) { |
171
|
|
|
if ( isset( $config[ $string ] ) ) { |
172
|
|
|
return $config[ $string ]; |
173
|
|
|
} |
174
|
|
|
} else { |
175
|
|
|
return $config; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return null; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function getFileName() |
184
|
|
|
{ |
185
|
|
|
//TODO Make the replacer function |
186
|
|
|
return $this->config->get( 'gmail.credentials_file_name' ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function getUserScopes() |
190
|
|
|
{ |
191
|
|
|
return [ |
192
|
|
|
Google_Service_Gmail::GMAIL_READONLY, |
193
|
|
|
Google_Service_Gmail::GMAIL_MODIFY, |
194
|
|
|
]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
} |