|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Google_Service_Gmail; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
|
|
8
|
|
|
trait Configurable |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
public function config( $string = null, $email = null ) |
|
12
|
|
|
{ |
|
13
|
|
|
$email = $email ?: $this->emailAddress; |
|
|
|
|
|
|
14
|
|
|
$fileName = $this->getFileName( $email ); |
|
|
|
|
|
|
15
|
|
|
$file = "gmail/tokens/{$fileName}.json"; |
|
16
|
|
|
|
|
17
|
|
|
if ( Storage::exists( $file ) ) { |
|
18
|
|
|
$config = json_decode( |
|
19
|
|
|
Storage::get( $file ), |
|
|
|
|
|
|
20
|
|
|
true |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
if ( $string ) { |
|
24
|
|
|
if ( isset( $config[ $string ] ) ) { |
|
25
|
|
|
return $config[ $string ]; |
|
26
|
|
|
} |
|
27
|
|
|
} else { |
|
28
|
|
|
return $config; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return null; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getConfigs() |
|
40
|
|
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
'client_secret' => $this->configuration[ 'gmail.client_secret' ], |
|
|
|
|
|
|
43
|
|
|
'client_id' => $this->configuration[ 'gmail.client_id' ], |
|
44
|
|
|
'redirect_uri' => url( $this->configuration[ 'gmail.redirect_url' ] ), |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function getUserScopes() |
|
49
|
|
|
{ |
|
50
|
|
|
return array_merge( [ |
|
51
|
|
|
Google_Service_Gmail::GMAIL_READONLY, |
|
52
|
|
|
], $this->mapScopes() ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function configApi() |
|
56
|
|
|
{ |
|
57
|
|
|
$type = $this->configuration[ 'gmail.access_type' ]; |
|
|
|
|
|
|
58
|
|
|
$approval_prompt = $this->configuration[ 'gmail.approval_prompt' ]; |
|
59
|
|
|
|
|
60
|
|
|
$this->setScopes( $this->getUserScopes() ); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$this->setAccessType( $type ); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$this->setApprovalPrompt( $approval_prompt ); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function getFileName() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->configuration[ 'gmail.credentials_file_name' ]; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function mapScopes() |
|
73
|
|
|
{ |
|
74
|
|
|
$scopes = $this->configuration[ 'gmail.scopes' ]; |
|
|
|
|
|
|
75
|
|
|
$mappedScopes = []; |
|
76
|
|
|
|
|
77
|
|
|
if ( ! empty( $scopes ) ) { |
|
78
|
|
|
foreach ( $scopes as $scope ) { |
|
79
|
|
|
$mappedScopes[] = $this->scopeMap( $scope ); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $mappedScopes; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function scopeMap( $scope ) |
|
87
|
|
|
{ |
|
88
|
|
|
switch ( $scope ) { |
|
89
|
|
|
case 'all': |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
return Google_Service_Gmail::MAIL_GOOGLE_COM; |
|
92
|
|
|
} |
|
93
|
|
|
case 'compose': |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
return Google_Service_Gmail::GMAIL_COMPOSE; |
|
96
|
|
|
} |
|
97
|
|
|
case 'insert': |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
return Google_Service_Gmail::GMAIL_INSERT; |
|
100
|
|
|
} |
|
101
|
|
|
case 'labels': |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
return Google_Service_Gmail::GMAIL_LABELS; |
|
104
|
|
|
} |
|
105
|
|
|
case 'metadata': |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
return Google_Service_Gmail::GMAIL_METADATA; |
|
108
|
|
|
} |
|
109
|
|
|
case 'modify': |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
return Google_Service_Gmail::GMAIL_MODIFY; |
|
112
|
|
|
} |
|
113
|
|
|
case 'readonly': |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
return Google_Service_Gmail::GMAIL_READONLY; |
|
116
|
|
|
} |
|
117
|
|
|
case 'send': |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
return Google_Service_Gmail::GMAIL_SEND; |
|
120
|
|
|
} |
|
121
|
|
|
case 'settings_basic': |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
return Google_Service_Gmail::GMAIL_SETTINGS_BASIC; |
|
124
|
|
|
} |
|
125
|
|
|
case 'settings_sharing': |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
return Google_Service_Gmail::GMAIL_SETTINGS_SHARING; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |