/var/www/html_us/wp-content/plugins/wp-smtp/src/Mail/Connectors/ConnectorSMTP.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
/**
 * File: Provider.php
 * Description: Abstract class representing an SMTP provider.
 *
 * @since      1.3.0
 * @package    Solid_SMTP
 */

namespace SolidWP\Mail\Connectors;

use 
SolidWP\Mail\StellarWP\Validation\Validator;

/**
 * Represents an SMTP provider.
 *
 * @since 1.3.0
 *
 * @package Solid_SMTP\Providers
 */
class ConnectorSMTP {

    
/**
     * A unique id for this provider.
     *
     * @var string
     */
    
protected string $id;

    
/**
     * The provider name.
     *
     * @var string
     */
    
protected string $name;

    
/**
     * Provider small description.
     *
     * @var string
     */
    
protected string $description;

    
/**
     * The email address used as the sender's address.
     *
     * @var string
     */
    
protected string $from_email;

    
/**
     * The name used as the sender's name.
     *
     * @var string
     */
    
protected string $from_name;

    
/**
     * The SMTP server hostname or IP address.
     *
     * @var string
     */
    
protected string $host;

    
/**
     * The port number for the SMTP server.
     *
     * @var string
     */
    
protected string $port;

    
/**
     * Whether the SMTP server requires authentication. Can be yes|no
     *
     * @var string
     */
    
protected string $authentication;

    
/**
     * The username for authenticating with the SMTP server.
     *
     * @var string
     */
    
protected string $smtp_username;

    
/**
     * The password for authenticating with the SMTP server.
     *
     * @var string
     */
    
protected string $smtp_password;

    
/**
     * Whether to disable logging for this provider.
     *
     * @var bool
     */
    
protected bool $disable_logs false;

    
/**
     * The PHPMailer Secure, can be '', ssl or tls.
     *
     * @var string
     */
    
protected string $secure;

    
/**
     * If this is set as default sender.
     *
     * @var bool
     */
    
protected bool $is_active false;

    
/**
     * The method used for message delivery (e.g., 'smtp', 'api')
     *
     * @var string
     */
    
protected string $delivery_method 'smtp';

    
/**
     * Gets the current delivery method for this connector
     *
     * @return string The delivery method being used ('smtp', 'api', etc.)
     */
    
public function isAPI(): string {
        return 
$this->delivery_method === 'api';
    }

    
/**
     * Holds validation errors.
     *
     * @var array
     */
    
protected array $validation_errors = [];

    
/**
     * Constructor.
     *
     * @param array $data
     */
    
public function __construct( array $data = [] ) {
        
$this->process_data$data );
        
// if the load data is empty, generate a new id.

        
$this->id        = empty( $data['id'] ) ? uniqid() : $data['id'];
        
$this->is_active filter_var$data['is_active'] ?? falseFILTER_VALIDATE_BOOLEAN );
    }

    
/**
     * Return the ID.
     *
     * @return string
     */
    
public function get_id(): string {
        return 
$this->id;
    }

    
/**
     * @return string
     */
    
public function get_name(): string {
        return 
$this->name;
    }

    
/**
     * @return bool
     */
    
public function is_active(): bool {
        return 
$this->is_active;
    }

    
/**
     * Sets the email address used as the sender's address.
     *
     * @param string $from_email The email address used as the sender's address.
     *
     * @return void
     */
    
public function set_from_emailstring $from_email ): void {
        
$this->from_email $from_email;
    }

    
/**
     * Gets the email address used as the sender's address.
     *
     * @return string The email address used as the sender's address.
     */
    
public function get_from_email(): string {
        return 
$this->from_email;
    }

    
/**
     * @return string
     */
    
public function get_secure(): string {
        return 
$this->secure;
    }

    
/**
     * Sets the name used as the sender's name.
     *
     * @param string $from_name The name used as the sender's name.
     *
     * @return void
     */
    
public function set_from_namestring $from_name ): void {
        
$this->from_name $from_name;
    }

    
/**
     * Gets the name used as the sender's name.
     *
     * @return string The name used as the sender's name.
     */
    
public function get_from_name(): string {
        return 
$this->from_name;
    }

    
/**
     * Sets the SMTP server hostname or IP address.
     *
     * @param string $host The SMTP server hostname or IP address.
     *
     * @return void
     */
    
public function set_hoststring $host ): void {
        
$this->host $host;
    }

    
/**
     * Gets the SMTP server hostname or IP address.
     *
     * @return string The SMTP server hostname or IP address.
     */
    
public function get_host(): string {
        return 
$this->host;
    }

    
/**
     * Sets the port number for the SMTP server.
     *
     * @param string $port The port number for the SMTP server.
     *
     * @return void
     */
    
public function set_portstring $port ): void {
        
$this->port $port;
    }

    
/**
     * Gets the port number for the SMTP server.
     *
     * @return string The port number for the SMTP server.
     */
    
public function get_port(): string {
        return 
$this->port;
    }

    
/**
     * Sets whether the SMTP server requires authentication.
     *
     * @return void
     */
    
public function set_authenticate(): void {
        
$this->authentication 'yes';
    }

    
/**
     * Gets whether the SMTP server requires authentication.
     *
     * @return bool Whether the SMTP server requires authentication.
     */
    
public function is_authentication(): bool {
        return 
$this->authentication === 'yes';
    }

    
/**
     * Sets the username for authenticating with the SMTP server.
     *
     * @param string $username The username for authenticating with the SMTP server.
     *
     * @return void
     */
    
public function set_usernamestring $username ): void {
        
$this->smtp_username $username;
    }

    
/**
     * Gets the username for authenticating with the SMTP server.
     *
     * @return string The username for authenticating with the SMTP server.
     */
    
public function get_username(): string {
        return 
$this->smtp_username;
    }

    
/**
     * Sets the password for authenticating with the SMTP server.
     *
     * @param string $password The password for authenticating with the SMTP server.
     *
     * @return void
     */
    
public function set_passwordstring $password ): void {
        
$this->smtp_password $password;
    }

    
/**
     * Gets the password for authenticating with the SMTP server.
     *
     * @return string The password for authenticating with the SMTP server.
     */
    
public function get_password(): string {
        return 
$this->smtp_password;
    }

    
/**
     * @param bool $is_active
     */
    
public function set_is_activebool $is_active ): void {
        
$this->is_active $is_active;
    }

    
/**
     * Sets whether to disable logging for this provider.
     *
     * @param bool $disable_logs Whether to disable logging for this provider.
     *
     * @return void
     */
    
public function set_disable_logsbool $disable_logs ): void {
        
$this->disable_logs $disable_logs;
    }

    
/**
     * Gets whether to disable logging for this provider.
     *
     * @return bool Whether to disable logging for this provider.
     */
    
public function get_disable_logs(): bool {
        return 
$this->disable_logs;
    }

    
/**
     * Validates the input data.
     *
     * This method validates the input data for the SMTP provider using the Validator class.
     * If validation passes, it returns true. If validation fails, it returns an array of validation errors.
     *
     * @return bool|array True if validation succeeds, an array of validation errors otherwise.
     */
    
public function validation(): bool {
        
$data   $this->to_array();
        
$labels = [
            
'name'           => __'Name''wp-smtp' ),
            
'from_email'     => __'From Email''wp-smtp' ),
            
'from_name'      => __'From Name''wp-smtp' ),
            
'smtp_host'      => __'SMTP Host''wp-smtp' ),
            
'smtp_port'      => __'SMTP Port''wp-smtp' ),
            
'authentication' => __'SMTP Authentication''wp-smtp' ),
            
'smtp_username'  => __'SMTP Username''wp-smtp' ),
            
'smtp_password'  => __'SMTP Password''wp-smtp' ),
        ];

        
$rules = [
            
'from_email' => [ 'required''email' ],
            
'from_name'  => [ 'required' ],
            
'smtp_host'  => [ 'required' ],
            
'smtp_port'  => [ 'required''numeric' ],
        ];

        if ( 
$this->is_authentication() ) {
            
$rules array_merge(
                
$rules,
                [
                    
'smtp_username' => [ 'required' ],
                    
'smtp_password' => [ 'required' ],
                ] 
            );
        }

        
$validator = new Validator$rules$data$labels );

        if ( 
true === $validator->passes() ) {
            return 
true;
        }

        
$this->validation_errors $validator->errors();

        return 
false;
    }

    
/**
     * Retrieves the validation errors.
     *
     * @return array An array of validation errors.
     */
    
public function get_errors(): array {
        return 
$this->validation_errors;
    }

    
/**
     * Processes the provided data and sets properties if possible.
     *
     * @param array $data The data to process.
     *
     * @return void
     */
    
public function process_data( array $data ) {
        
$this->name           $data['name'] ?? '';
        
$this->description    '';
        
$this->from_email     $data['from_email'] ?? '';
        
$this->from_name      $data['from_name'] ?? '';
        
$this->host           $data['smtp_host'] ?? '';
        
$this->port           $data['smtp_port'] ?? '';
        
$this->authentication $data['smtp_auth'] ?? 'no';
        
$this->smtp_username  $data['smtp_username'] ?? '';
        if ( ! empty( 
$data['smtp_password'] ) ) {
            
$this->smtp_password $data['smtp_password'];
        } else {
            
$this->smtp_password '';
        }
        
$this->disable_logs $data['disable_logs'] ?? false;
        
$this->secure       $data['smtp_secure'] ?? '';
    }

    
/**
     * Converts the ProviderSMTP object to an associative array.
     *
     * @return array The ProviderSMTP object properties as an associative array.
     */
    
public function to_array(): array {
        return [
            
'id'            => $this->id,
            
'name'          => $this->name,
            
'description'   => $this->description,
            
'from_email'    => $this->from_email,
            
'from_name'     => $this->from_name,
            
'smtp_host'     => $this->host,
            
'smtp_port'     => absint$this->port ),
            
'smtp_auth'     => $this->authentication,
            
'smtp_username' => $this->smtp_username,
            
'smtp_password' => $this->smtp_password,
            
'disable_logs'  => $this->disable_logs,
            
'smtp_secure'   => $this->secure,
            
'is_active'     => $this->is_active,
        ];
    }
}