/var/www/html_fr/wp-content/plugins/fluent-smtp/app/Services/Mailer/Providers/Gmail/Handler.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php

namespace FluentMail\App\Services\Mailer\Providers\Gmail;

use 
FluentMail\App\Models\Settings;
use 
FluentMail\Includes\Support\Arr;
use 
FluentMail\App\Services\Mailer\BaseHandler;

class 
Handler extends BaseHandler
{

    public function 
send()
    {
        if (
$this->preSend() && $this->phpMailer->preSend()) {
            return 
$this->postSend();
        }

        return 
$this->handleResponse(new \WP_Error(422__('Something went wrong!''fluent-smtp'), []));
    }

    protected function 
postSend()
    {
        try {
            
$returnResponse $this->sendViaApi();
        } catch (
\Exception $e) {
            
$returnResponse = new \WP_Error(422$e->getMessage(), []);
        }

        
$this->response $returnResponse;

        return 
$this->handleResponse($this->response);
    }

    public function 
setSettings($settings)
    {
        if (
Arr::get($settings'key_store') == 'wp_config') {
            
$settings['client_id'] = defined('FLUENTMAIL_GMAIL_CLIENT_ID') ? FLUENTMAIL_GMAIL_CLIENT_ID '';
            
$settings['client_secret'] = defined('FLUENTMAIL_GMAIL_CLIENT_SECRET') ? FLUENTMAIL_GMAIL_CLIENT_SECRET '';
        }

        
$this->settings $settings;

        return 
$this;
    }

    private function 
sendViaApi()
    {
        if (!
class_exists('\FluentSmtpLib\Google\Service\Gmail\Message')) {
            require_once 
FLUENTMAIL_PLUGIN_PATH 'includes/libs/google-api-client/build/vendor/autoload.php';
        }

        
$message $this->phpMailer->getSentMIMEMessage();

        
$data $this->getSetting();

        
$googleApiMessage = new \FluentSmtpLib\Google\Service\Gmail\Message();

        
$file_size strlen($message);
        
$googleClient $this->getApiClient($data);

        if (
is_wp_error($googleClient)) {
            return 
$googleClient;
        }

        
$googleService = new \FluentSmtpLib\Google\Service\Gmail($googleClient);

        
$result = array();
        try {
            
$googleClient->setDefer(true);
            
$result $googleService->users_messages->send('me'$googleApiMessage, array('uploadType' => 'resumable'));

            
$chunkSizeBytes 1024 1024;

            
// create mediafile upload
            
$media = new \FluentSmtpLib\Google\Http\MediaFileUpload(
                
$googleClient,
                
$result,
                
'message/rfc822',
                
$message,
                
true,
                
$chunkSizeBytes
            
);
            
$media->setFileSize($file_size);

            
$status false;
            while (!
$status) {
                
$status $media->nextChunk();
            }
            
$result false;

            
// Reset to the client to execute requests immediately in the future.
            
$googleClient->setDefer(false);

            
$googleMessageId $status->getId();

        } catch (
\Exception $e) {
            
$errorMessage $e->getMessage();
            return new 
\WP_Error(422$errorMessage, []);
        }

        return array(
            
'MessageId' => $googleMessageId,
        );
    }

    public function 
validateProviderInformation($connection)
    {
        
$errors = [];

        
$keyStoreType $connection['key_store'];

        
$clientId Arr::get($connection'client_id');
        
$clientSecret Arr::get($connection'client_secret');

        if (
$keyStoreType == 'db') {
            if (!
$clientId) {
                
$errors['client_id']['required'] = __('Application Client ID is required.''fluent-smtp');
            }

            if (!
$clientSecret) {
                
$errors['client_secret']['required'] = __('Application Client Secret key is required.''fluent-smtp');
            }
        } else if (
$keyStoreType == 'wp_config') {
            if (!
defined('FLUENTMAIL_GMAIL_CLIENT_ID') || !FLUENTMAIL_GMAIL_CLIENT_ID) {
                
$errors['client_id']['required'] = __('Please define FLUENTMAIL_GMAIL_CLIENT_ID in wp-config.php file.''fluent-smtp');
            } else {
                
$clientId FLUENTMAIL_GMAIL_CLIENT_ID;
            }

            if (!
defined('FLUENTMAIL_GMAIL_CLIENT_SECRET') || !FLUENTMAIL_GMAIL_CLIENT_SECRET) {
                
$errors['client_secret']['required'] = __('Please define FLUENTMAIL_GMAIL_CLIENT_SECRET in wp-config.php file.''fluent-smtp');
            } else {
                
$clientSecret FLUENTMAIL_GMAIL_CLIENT_SECRET;
            }
        }

        if (
$errors) {
            
$this->throwValidationException($errors);
        }

        
$accessToken Arr::get($connection'access_token');
        
$authToken Arr::get($connection'auth_token');

        if (!
$accessToken && $authToken) {
            
// this is new, We have to generate the tokens
            
$body = [
                
'code'          => $authToken,
                
'grant_type'    => 'authorization_code',
                
'redirect_uri'  => apply_filters('fluentsmtp_gapi_callback''https://fluentsmtp.com/gapi/'), // 'urn:ietf:wg:oauth:2.0:oob'
                
'client_id'     => $clientId,
                
'client_secret' => $clientSecret
            
];
            
$tokens $this->makeRequest('https://accounts.google.com/o/oauth2/token'$body'POST');
            if (
is_wp_error($tokens)) {
                
$errors['auth_token']['required'] = $tokens->get_error_message();
            } else {
                
add_filter('fluentmail_saving_connection_data', function ($con$provider) use ($connection$tokens) {

                    if (
$provider != 'gmail') {
                        return 
$con;
                    }

                    if (
Arr::get($con'connection.sender_email') != $connection['sender_email']) {
                        return 
$con;
                    }

                    
$con['connection']['refresh_token'] = $tokens['refresh_token'];
                    
$con['connection']['access_token'] = $tokens['access_token'];
                    
$con['connection']['auth_token'] = '';
                    
$con['connection']['expire_stamp'] = time() + $tokens['expires_in'];
                    
$con['connection']['expires_in'] = $tokens['expires_in'];
                    
$con['connection']['version'] = 2;
                    return 
$con;
                }, 
102);
            }
        } else if (!
$authToken && !$accessToken) {
            
$errors['auth_token']['required'] = __('Please Provide Auth Token.''fluent-smtp');
        }

        if (
$errors) {
            
$this->throwValidationException($errors);
        }
    }

    private function 
makeRequest($url$bodyArgs$type 'GET'$headers false)
    {
        if (!
$headers) {
            
$headers = array(
                
'Content-Type'              => 'application/http',
                
'Content-Transfer-Encoding' => 'binary',
                
'MIME-Version'              => '1.0',
            );
        }

        
$args = [
            
'headers' => $headers
        
];
        if (
$bodyArgs) {
            
$args['body'] = json_encode($bodyArgs);
        }


        
$args['method'] = $type;
        
$request wp_remote_request($url$args);

        if (
is_wp_error($request)) {
            
$message $request->get_error_message();
            return new 
\WP_Error(422$message);
        }

        
$body json_decode(wp_remote_retrieve_body($request), true);

        if (!empty(
$body['error'])) {
            
$error 'Unknown Error';
            if (isset(
$body['error_description'])) {
                
$error $body['error_description'];
            } else if (!empty(
$body['error']['message'])) {
                
$error $body['error']['message'];
            }
            return new 
\WP_Error(422$error);
        }

        return 
$body;
    }

    private function 
saveNewTokens($existingData$tokens)
    {
        if (empty(
$tokens['access_token']) || empty($tokens['refresh_token'])) {
            return 
false;
        }

        
$senderEmail $existingData['sender_email'];

        
$existingData['access_token'] = $tokens['access_token'];
        
$existingData['refresh_token'] = $tokens['refresh_token'];
        
$existingData['expire_stamp'] = $tokens['expires_in'] + time();
        
$existingData['expires_in'] = $tokens['expires_in'];

        (new 
Settings())->updateConnection($senderEmail$existingData);
        
fluentMailGetProvider($senderEmailtrue); // we are clearing the static cache here
        
wp_schedule_single_event($existingData['expire_stamp'] - 360'fluentsmtp_renew_gmail_token');
        return 
true;
    }

    private function 
getApiClient($data)
    {
        
$senderEmail $data['sender_email'];

        static 
$cachedServices = [];
        if (isset(
$cachedServices[$senderEmail])) {
            return 
$cachedServices[$senderEmail];
        }

        if (!
class_exists('\FluentSmtpLib\Google\Client')) {
            require_once 
FLUENTMAIL_PLUGIN_PATH 'includes/libs/google-api-client/build/vendor/autoload.php';
        }

        
$client = new \FluentSmtpLib\Google\Client();
        
$client->setClientId($data['client_id']);
        
$client->setClientSecret($data['client_secret']);
        
$client->addScope("https://www.googleapis.com/auth/gmail.compose");
        
$client->setAccessType('offline');
        
$client->setApprovalPrompt('force');

        
$tokens = [
            
'access_token'  => $data['access_token'],
            
'refresh_token' => $data['refresh_token'],
            
'expires_in'    => $data['expire_stamp'] - time()
        ];

        
$client->setAccessToken($tokens);

        
// check if expired or will be expired in 5 minutes
        
if (($data['expire_stamp'] - 300) < time()) {
            
$newTokens $client->refreshToken($data['refresh_token']);

            
$result $this->saveNewTokens($data$newTokens);

            if (!
$result) {
                
$errorDescription Arr::get($newTokens'error_description');
                if (!
$errorDescription) {
                    
$errorDescription __('Failed to renew token with Gmail Api''fluent-smtp');
                }

                return new 
\WP_Error('api_error'$errorDescription);
            }

            
$client->setAccessToken($newTokens);
        }

        
$cachedServices[$senderEmail] = $client;

        return 
$cachedServices[$senderEmail];
    }

    public function 
getConnectionInfo($connection)
    {
        if (
Arr::get($connection'key_store') == 'wp_config') {
            
$connection['client_id'] = defined('FLUENTMAIL_GMAIL_CLIENT_ID') ? FLUENTMAIL_GMAIL_CLIENT_ID '';
            
$connection['client_secret'] = defined('FLUENTMAIL_GMAIL_CLIENT_SECRET') ? FLUENTMAIL_GMAIL_CLIENT_SECRET '';
        }

        if (!
class_exists('\FluentSmtpLib\Google\Client')) {
            require_once 
FLUENTMAIL_PLUGIN_PATH 'includes/libs/google-api-client/build/vendor/autoload.php';
        }

        
$client $this->getApiClient($connection);

        if (
is_wp_error($client)) {
            return 
'<p style="color: red; text-align: center; font-size: 18px;">ERROR: ' $connection->get_error_message() . '</p>';
        }

        
$info fluentMailgetConnection($connection['sender_email']);

        
$connection $info->getSetting();

        
$extraRow = [
            
'title'   => __('Token Validity''fluent-smtp'),
            
'content' => 'Valid (' . (int)(($connection['expire_stamp'] - time()) / 60) . 'minutes)'
        
];

        if ((
$connection['expire_stamp']) < time()) {
            
$extraRow['content'] = __('Invalid. Please re-authenticate''fluent-smtp');
        }

        
$connection['extra_rows'] = [$extraRow];

        return [
            
'info' => (string)fluentMail('view')->make('admin.general_connection_info', [
                
'connection' => $connection
            
])
        ];
    }
}