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