Completed
Push — develop ( cb7ecf...5e631f )
by Dmytro
17s
created

Mail::init()   F

Complexity

Conditions 18
Paths 1440

Size

Total Lines 75
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 61
nc 1440
nop 0
dl 0
loc 75
rs 2.3285
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace EvolutionCMS;
2
3
use PHPMailer\PHPMailer\PHPMailer;
4
use PHPMailer\PHPMailer\Exception as PHPMailerException;
5
6
class Mail extends PHPMailer
0 ignored issues
show
Coding Style introduced by
The property $mb_language is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $encode_header_method is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $mb_language = 'UNI';
12
13
    /**
14
     * @var string
15
     */
16
    protected $encode_header_method = '';
17
18
    /**
19
     * @var
20
     */
21
    public $PluginDir;
22
23
    /**
24
     * @var Core $modx
25
     */
26
    protected $modx;
27
28
    public function init()
29
    {
30
        $modx = evolutionCMS();
31
        $this->modx = $modx;
32
        $this->PluginDir = MODX_MANAGER_PATH . 'includes/controls/phpmailer/';
33
34
        switch ($modx->config['email_method']) {
35
            case 'smtp':
36
                $this->isSMTP();
37
                $this->SMTPSecure = $modx->config['smtp_secure'] === 'none' ? '' : $modx->config['smtp_secure'];
38
                $this->Port = $modx->config['smtp_port'];
39
                $this->Host = $modx->config['smtp_host'];
40
                $this->SMTPAuth = $modx->config['smtp_auth'] === '1' ? true : false;
41
                $this->Username = $modx->config['smtp_username'];
42
                $this->Password = $modx->config['smtppw'];
43
                if (10 < strlen($this->Password)) {
44
                    $this->Password = substr($this->Password, 0, -7);
45
                    $this->Password = str_replace('%', '=', $this->Password);
46
                    $this->Password = base64_decode($this->Password);
47
                }
48
                break;
49
            case 'mail':
50
            default:
51
                $this->isMail();
52
        }
53
54
        $this->From = $modx->config['emailsender'];
55
        if (isset($modx->config['email_sender_method']) && !$modx->config['email_sender_method']) {
56
            $this->Sender = $modx->config['emailsender'];
57
        }
58
        $this->FromName = $modx->config['site_name'];
59
        $this->isHTML(true);
60
61
        if (isset($modx->config['mail_charset']) && !empty($modx->config['mail_charset'])) {
62
            $mail_charset = $modx->config['mail_charset'];
63
        } else {
64
            if (substr($modx->config['manager_language'], 0, 8) === 'japanese') {
65
                $mail_charset = 'jis';
66
            } else {
67
                $mail_charset = $modx->config['modx_charset'];
68
            }
69
        }
70
71
        switch ($mail_charset) {
72
            case 'iso-8859-1':
73
                $this->CharSet = 'iso-8859-1';
74
                $this->Encoding = 'quoted-printable';
75
                $this->mb_language = 'English';
76
                break;
77
            case 'jis':
78
                $this->CharSet = 'ISO-2022-JP';
79
                $this->Encoding = '7bit';
80
                $this->mb_language = 'Japanese';
81
                $this->encode_header_method = 'mb_encode_mimeheader';
82
                $this->isHTML(false);
83
                break;
84
            case 'windows-1251':
85
                $this->CharSet = 'cp1251';
86
                break;
87
            case 'utf8':
88
            case 'utf-8':
89
            default:
90
                $this->CharSet = 'UTF-8';
91
                $this->Encoding = 'base64';
92
                $this->mb_language = 'UNI';
93
        }
94
        if (extension_loaded('mbstring')) {
95
            mb_language($this->mb_language);
96
            mb_internal_encoding($modx->config['modx_charset']);
97
        }
98
        $exconf = MODX_MANAGER_PATH . 'includes/controls/phpmailer/config.inc.php';
99
        if (is_file($exconf)) {
100
            include($exconf);
101
        }
102
    }
103
104
    /**
105
     * Encode a header value (not including its label) optimally.
106
     * Picks shortest of Q, B, or none. Result includes folding if needed.
107
     * See RFC822 definitions for phrase, comment and text positions.
108
     *
109
     * @param string $str The header value to encode
110
     * @param string $position What context the string will be used in
111
     *
112
     * @return string
113
     */
114
    public function EncodeHeader($str, $position = 'text')
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
115
    {
116
        $str = $this->modx->removeSanitizeSeed($str);
117
118
        if ($this->encode_header_method == 'mb_encode_mimeheader') {
119
            return mb_encode_mimeheader($str, $this->CharSet, 'B', "\n");
120
        } else {
121
            return parent::EncodeHeader($str, $position);
122
        }
123
    }
124
125
    /**
126
     * Create a message and send it.
127
     * Uses the sending method specified by $Mailer.
128
     *
129
     * @throws PHPMailerException
130
     *
131
     * @return bool false on error - See the ErrorInfo property for details of the error
132
     */
133
    public function Send()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
134
    {
135
        $this->Body = $this->modx->removeSanitizeSeed($this->Body);
136
        $this->Subject = $this->modx->removeSanitizeSeed($this->Subject);
137
138
        return parent::send();
139
    }
140
141
    /**
142
     * @param string $header The message headers
143
     * @param string $body The message body
144
     *
145
     * @return bool
146
     */
147
    public function MailSend($header, $body)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
148
    {
149
        $org_body = $body;
150
151
        switch ($this->CharSet) {
152
            case 'ISO-2022-JP':
153
                $body = mb_convert_encoding($body, 'JIS', $this->modx->config['modx_charset']);
154
                if (ini_get('safe_mode')) {
155
                    $mode = 'normal';
156
                } else {
157
                    $this->Subject = $this->EncodeHeader($this->Subject);
158
                    $mode = 'mb';
159
                }
160
                break;
161
            default:
162
                $mode = 'normal';
163
        }
164
165
        if ($this->modx->debug) {
166
            $debug_info = 'CharSet = ' . $this->CharSet . "\n";
167
            $debug_info .= 'Encoding = ' . $this->Encoding . "\n";
168
            $debug_info .= 'mb_language = ' . $this->mb_language . "\n";
169
            $debug_info .= 'encode_header_method = ' . $this->encode_header_method . "\n";
170
            $debug_info .= "send_mode = {$mode}\n";
171
            $debug_info .= 'Subject = ' . $this->Subject . "\n";
172
            $log = "<pre>{$debug_info}\n{$header}\n{$org_body}</pre>";
173
            $this->modx->logEvent(1, 1, $log, 'MODxMailer debug information');
174
175
            return true;
176
        }
177
178
        switch ($mode) {
179
            case 'normal':
180
                $out = parent::mailSend($header, $body);
181
                break;
182
            case 'mb':
183
                $out = $this->mbMailSend($header, $body);
184
                break;
185
            default:
186
                $out = false;
187
        }
188
189
        return $out;
190
    }
191
192
    /**
193
     * @param string $header The message headers
194
     * @param string $body The message body
195
     *
196
     * @return bool
197
     */
198
    public function mbMailSend($header, $body)
199
    {
200
        $rt = false;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $rt. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
201
        $to = '';
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $to. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
202
        $countTo = count($this->to);
203
        for ($i = 0; $i < $countTo; $i++) {
204
            if ($i != 0) {
205
                $to .= ', ';
206
            }
207
            $to .= $this->AddrFormat($this->to[$i]);
208
        }
209
210
        $toArr = array_filter(array_map('trim', explode(',', $to)));
211
212
        $params = sprintf("-oi -f %s", $this->Sender);
213
        if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
214
            $old_from = ini_get('sendmail_from');
215
            ini_set('sendmail_from', $this->Sender);
216 View Code Duplication
            if ($this->SingleTo === true && count($toArr) > 1) {
217
                foreach ($toArr as $key => $val) {
218
                    $rt = @mail($val, $this->Subject, $body, $header, $params);
219
                }
220
            } else {
221
                $rt = @mail($to, $this->Subject, $body, $header, $params);
222
            }
223 View Code Duplication
        } else {
224
            if ($this->SingleTo === true && count($toArr) > 1) {
225
                foreach ($toArr as $key => $val) {
226
                    $rt = @mail($val, $this->Subject, $body, $header, $params);
227
                }
228
            } else {
229
                $rt = @mail($to, $this->Subject, $body, $header);
230
            }
231
        }
232
233
        if (isset($old_from)) {
234
            ini_set('sendmail_from', $old_from);
235
        }
236
        if (!$rt) {
237
            $msg = $this->Lang('instantiate') . "<br />\n";
238
            $msg .= "{$this->Subject}<br />\n";
239
            $msg .= "{$this->FromName}&lt;{$this->From}&gt;<br />\n";
240
            $msg .= mb_convert_encoding($body, $this->modx->config['modx_charset'], $this->CharSet);
241
            $this->SetError($msg);
242
243
            return false;
244
        }
245
246
        return true;
247
    }
248
249
    /**
250
     * Add an error message to the error container.
251
     *
252
     * @param string $msg
253
     */
254
    public function SetError($msg)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
255
    {
256
        $classDump = call_user_func('get_object_vars', $this);
257
        unset($classDump['modx']);
258
        $this->modx->config['send_errormail'] = '0';
259
        $this->modx->logEvent(0, 3, $msg . '<pre>' . print_r($classDump, true) . '</pre>', 'phpmailer');
260
261
        return parent::SetError($msg);
262
    }
263
264
    /**
265
     * @param $address
266
     *
267
     * @return array
268
     */
269
    public function address_split($address)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
270
    {
271
        $address = trim($address);
272
        if (strpos($address, '<') !== false && substr($address, -1) === '>') {
273
            $address = rtrim($address, '>');
274
            list($name, $address) = explode('<', $address);
275
        } else {
276
            $name = '';
277
        }
278
        return array($name, $address);
279
    }
280
281
    /**
282
     * @return string
283
     */
284
    public function getMIMEHeader()
285
    {
286
        return $this->MIMEHeader;
287
    }
288
289
    /**
290
     * @return string
291
     */
292
    public function getMIMEBody()
293
    {
294
        return $this->MIMEBody;
295
    }
296
297
    /**
298
     * @param string $header
299
     *
300
     * @return $this
301
     */
302
    public function setMIMEHeader($header = '')
303
    {
304
        $this->MIMEHeader = $header;
305
306
        return $this;
307
    }
308
309
    /**
310
     * @param string $body
311
     *
312
     * @return $this
313
     */
314
    public function setMIMEBody($body = '')
315
    {
316
        $this->MIMEBody = $body;
317
318
        return $this;
319
    }
320
321
    /**
322
     * @param string $header
323
     *
324
     * @return $this
325
     */
326
    public function setMailHeader($header = '')
327
    {
328
        $this->mailHeader = $header;
329
330
        return $this;
331
    }
332
333
    /**
334
     * @return string
335
     */
336
    public function getMessageID()
337
    {
338
        return trim($this->lastMessageID, '<>');
339
    }
340
}
341