Mail::init()   F
last analyzed

Complexity

Conditions 19
Paths 2880

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

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

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
117
     */
118
    public function EncodeHeader($str, $position = 'text')
119
    {
120
        $str = removeSanitizeSeed($str);
121
122
        if ($this->encode_header_method == 'mb_encode_mimeheader') {
123
            return mb_encode_mimeheader($str, $this->CharSet, 'B', "\n");
124
        } else {
125
            return parent::EncodeHeader($str, $position);
126
        }
127
    }
128
129
    /**
130
     * Create a message and send it.
131
     * Uses the sending method specified by $Mailer.
132
     *
133
     * @throws PHPMailerException
134
     *
135
     * @return bool false on error - See the ErrorInfo property for details of the error
136
     */
137
    public function Send()
138
    {
139
        $this->Body = removeSanitizeSeed($this->Body);
140
        $this->Subject = removeSanitizeSeed($this->Subject);
141
142
        return parent::send();
143
    }
144
145
    /**
146
     * @param string $header The message headers
147
     * @param string $body The message body
148
     *
149
     * @return bool
150
     */
151
    public function MailSend($header, $body)
152
    {
153
        $org_body = $body;
154
155
        switch ($this->CharSet) {
156
            case 'ISO-2022-JP':
157
                $body = mb_convert_encoding($body, 'JIS', $this->modx->getConfig('modx_charset'));
158
                if (ini_get('safe_mode')) {
159
                    $mode = 'normal';
160
                } else {
161
                    $this->Subject = $this->EncodeHeader($this->Subject);
162
                    $mode = 'mb';
163
                }
164
                break;
165
            default:
166
                $mode = 'normal';
167
        }
168
169
        if ($this->modx->debug) {
170
            $debug_info = 'CharSet = ' . $this->CharSet . "\n";
171
            $debug_info .= 'Encoding = ' . $this->Encoding . "\n";
172
            $debug_info .= 'mb_language = ' . $this->mb_language . "\n";
173
            $debug_info .= 'encode_header_method = ' . $this->encode_header_method . "\n";
174
            $debug_info .= "send_mode = {$mode}\n";
175
            $debug_info .= 'Subject = ' . $this->Subject . "\n";
176
            $log = "<pre>{$debug_info}\n{$header}\n{$org_body}</pre>";
177
            $this->modx->logEvent(1, 1, $log, 'MODxMailer debug information');
178
179
            return true;
180
        }
181
182
        switch ($mode) {
183
            case 'normal':
184
                $out = parent::mailSend($header, $body);
185
                break;
186
            case 'mb':
187
                $out = $this->mbMailSend($header, $body);
188
                break;
189
            default:
190
                $out = false;
191
        }
192
193
        return $out;
194
    }
195
196
    /**
197
     * @param string $header The message headers
198
     * @param string $body The message body
199
     *
200
     * @return bool
201
     */
202
    public function mbMailSend($header, $body)
203
    {
204
        $rt = false;
205
        $to = '';
206
        $countTo = count($this->to);
207
        for ($i = 0; $i < $countTo; $i++) {
208
            if ($i != 0) {
209
                $to .= ', ';
210
            }
211
            $to .= $this->AddrFormat($this->to[$i]);
212
        }
213
214
        $toArr = array_filter(array_map('trim', explode(',', $to)));
215
216
        $params = sprintf("-oi -f %s", $this->Sender);
217
        if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
218
            $old_from = ini_get('sendmail_from');
219
            ini_set('sendmail_from', $this->Sender);
220 View Code Duplication
            if ($this->SingleTo === true && count($toArr) > 1) {
221
                foreach ($toArr as $key => $val) {
222
                    $rt = @mail($val, $this->Subject, $body, $header, $params);
223
                }
224
            } else {
225
                $rt = @mail($to, $this->Subject, $body, $header, $params);
226
            }
227 View Code Duplication
        } else {
228
            if ($this->SingleTo === true && count($toArr) > 1) {
229
                foreach ($toArr as $key => $val) {
230
                    $rt = @mail($val, $this->Subject, $body, $header, $params);
231
                }
232
            } else {
233
                $rt = @mail($to, $this->Subject, $body, $header);
234
            }
235
        }
236
237
        if (isset($old_from)) {
238
            ini_set('sendmail_from', $old_from);
239
        }
240
        if (!$rt) {
241
            $msg = $this->Lang('instantiate') . "<br />\n";
242
            $msg .= "{$this->Subject}<br />\n";
243
            $msg .= "{$this->FromName}&lt;{$this->From}&gt;<br />\n";
244
            $msg .= mb_convert_encoding($body, $this->modx->getConfig('modx_charset'), $this->CharSet);
245
            $this->SetError($msg);
246
247
            return false;
248
        }
249
250
        return true;
251
    }
252
253
    /**
254
     * Add an error message to the error container.
255
     *
256
     * @param string $msg
257
     */
258
    public function SetError($msg)
259
    {
260
        $classDump = call_user_func('get_object_vars', $this);
261
        unset($classDump['modx']);
262
        $this->modx->setConfig('send_errormail', '0');
263
        $this->modx->logEvent(0, 3, $msg . '<pre>' . print_r($classDump, true) . '</pre>', 'phpmailer');
264
265
        return parent::SetError($msg);
266
    }
267
268
    /**
269
     * @param $address
270
     *
271
     * @return array
272
     */
273
    public function address_split($address)
274
    {
275
        $address = trim($address);
276
        if (strpos($address, '<') !== false && substr($address, -1) === '>') {
277
            $address = rtrim($address, '>');
278
            list($name, $address) = explode('<', $address);
279
        } else {
280
            $name = '';
281
        }
282
        return array($name, $address);
283
    }
284
285
    /**
286
     * @return string
287
     */
288
    public function getMIMEHeader()
289
    {
290
        return $this->MIMEHeader;
291
    }
292
293
    /**
294
     * @return string
295
     */
296
    public function getMIMEBody()
297
    {
298
        return $this->MIMEBody;
299
    }
300
301
    /**
302
     * @param string $header
303
     *
304
     * @return $this
305
     */
306
    public function setMIMEHeader($header = '')
307
    {
308
        $this->MIMEHeader = $header;
309
310
        return $this;
311
    }
312
313
    /**
314
     * @param string $body
315
     *
316
     * @return $this
317
     */
318
    public function setMIMEBody($body = '')
319
    {
320
        $this->MIMEBody = $body;
321
322
        return $this;
323
    }
324
325
    /**
326
     * @param string $header
327
     *
328
     * @return $this
329
     */
330
    public function setMailHeader($header = '')
331
    {
332
        $this->mailHeader = $header;
333
334
        return $this;
335
    }
336
337
    /**
338
     * @return string
339
     */
340
    public function getMessageID()
341
    {
342
        return trim($this->lastMessageID, '<>');
343
    }
344
}
345