SendMail.php 21.3 KB
Newer Older
1
2
<?php

Marc Egger's avatar
Marc Egger committed
3
namespace IMATHUZH\Qfq\Core\Report;
4

Marc Egger's avatar
Marc Egger committed
5
6
7
8
9
10
11
use IMATHUZH\Qfq\Core\Database\Database;
 
use IMATHUZH\Qfq\Core\Helper\OnArray;
use IMATHUZH\Qfq\Core\Store\Store;
use IMATHUZH\Qfq\Core\Helper\Sanitize;
use IMATHUZH\Qfq\Core\Helper\HelperFile;
use IMATHUZH\Qfq\Core\Helper\Support;
12

13
const SENDMAIL_HTML_TOKEN = '<html>';
14

15
16
17
18
/**
 * Class SendMail
 * @package qfq
 */
19
class SendMail {
20

21
22
23
24
25
    /**
     * @var Store
     */
    private $store = null;

26
    /**
27
28
     * Sends a mail as specified in $mailarr.
     * If there is no receiver specified as 'TO': no mail is sent. This is ok and no error.
29
     * Logs every send mail as a record in table `MailLog`. Additionally a `grId`, `xId` , `xId2` and `xId3`  can be specified
30
     * to assign the log entry to a specific action.
31
     * The log record also contains some information who/where generates the mail (form/formelement or QFQ query).
Carsten  Rose's avatar
Carsten Rose committed
32
     *
Marc Egger's avatar
Marc Egger committed
33
34
35
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
36
     */
37
38
39
40
41
42
43
    public function __construct() {

        $this->store = Store::getInstance('');
    }


    /**
44
     * @param array $mailConfig
Marc Egger's avatar
Marc Egger committed
45
46
47
48
49
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
     * @throws \UserFormException
     * @throws \UserReportException
50
51
52
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
53
54
     */
    public function process(array $mailConfig) {
55

56
        // If there is no 'Receiver': do not send a mail.
57
        if (!isset($mailConfig[SENDMAIL_TOKEN_RECEIVER]) || $mailConfig[SENDMAIL_TOKEN_RECEIVER] === '') {
58
59
60
            return;
        }

61
        if ($mailConfig[SENDMAIL_TOKEN_SENDER] === '' || $mailConfig[SENDMAIL_TOKEN_SUBJECT] === '' || $mailConfig[SENDMAIL_TOKEN_BODY] === '') {
Marc Egger's avatar
Marc Egger committed
62
            throw new \UserFormException("Error sendmail missing one of: sender, subject or body", ERROR_SENDMAIL_MISSING_VALUE);
63
64
        }

65
66
        $redirectAllMail = $this->store->getVar(SYSTEM_REDIRECT_ALL_MAIL_TO, STORE_SYSTEM);

67
        if (!empty($redirectAllMail)) {
68

69
70
71
72
            foreach ([SENDMAIL_TOKEN_RECEIVER, SENDMAIL_TOKEN_RECEIVER_CC, SENDMAIL_TOKEN_RECEIVER_BCC] as $key) {
                $mailConfig[$key] = (strlen($mailConfig[$key]) > 1023) ? str_replace(',', ', ', $mailConfig[$key]) : $mailConfig[$key];
            }

Carsten  Rose's avatar
Carsten Rose committed
73
            $addBody = "All QFQ outgoing mails are caught and redirected to you." . PHP_EOL . "Original receiver(s) are ..." . PHP_EOL;
74
75
76
77
            $addBody .= 'TO: ' . ($mailConfig[SENDMAIL_TOKEN_RECEIVER] ?? '') . PHP_EOL;
            $addBody .= 'CC: ' . ($mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] ?? '') . PHP_EOL;
            $addBody .= 'BCC: ' . ($mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] ?? '') . PHP_EOL;
            $addBody .= 'SENDER: ' . ($mailConfig[SENDMAIL_TOKEN_SENDER] ?? '') . PHP_EOL;
78
            $addBody .= 'WEBSITE: ' . $this->store->getVar(SYSTEM_BASE_URL, STORE_SYSTEM) . PHP_EOL;
79
80
            $addBody .= PHP_EOL . "==========================================" . PHP_EOL . PHP_EOL;

81
82
83
84
85
            // Check if the given body is a HTML body.
            if (isset($mailConfig[SENDMAIL_TOKEN_BODY_MODE]) && $mailConfig[SENDMAIL_TOKEN_BODY_MODE] === SENDMAIL_TOKEN_BODY_MODE_HTML) {
                $addBody = str_replace(PHP_EOL, '<br>', $addBody);
            }

86
            $mailConfig[SENDMAIL_TOKEN_BODY] = $addBody . $mailConfig[SENDMAIL_TOKEN_BODY];
87

88
89
90
            $mailConfig[SENDMAIL_TOKEN_RECEIVER] = $redirectAllMail;
            $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] = '';
            $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] = '';
91
            $mailConfig[SENDMAIL_TOKEN_SENDER] = $redirectAllMail;
92
93
        }

94
95
96
97
98
99
100
101
102
103
104
105
        $mailConfig = $this->setDefault($mailConfig);

        $logAttachments = $this->sendEmail($mailConfig);
        $this->mailLog($mailConfig, $logAttachments);
    }

    /**
     * @param array $mailConfig
     * @return array
     */
    private function setDefault(array $mailConfig) {

106
107
        if (empty($mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT]) || $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] === '') {
            $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] = 'on';
108
109
        }

110
        // Subject
111
112
113
        if (empty($mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY]) ||
            ($mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] !== MODE_ENCODE &&
                $mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] !== MODE_NONE)) {
114
115
116
            $mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] = MODE_DECODE;
        }

117
        // Body
118
119
120
        if (empty($mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY]) ||
            ($mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY] !== MODE_ENCODE &&
                $mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY] !== MODE_NONE)) {
121
122
123
124
            $mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY] = MODE_DECODE;
        }

        return $mailConfig;
125
126
127
128
    }

    /**
     * Use the programm 'sendEmail' - http://caspian.dotconf.net/menu/Software/SendEmail
129
     * Body and Subject is UTF8 encoded. Append attachments with '-a <file>'.
130
131
     *
     * @param array $mailConfig
132
     * @return string
Marc Egger's avatar
Marc Egger committed
133
134
135
136
137
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
     * @throws \UserFormException
     * @throws \UserReportException
138
139
140
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
141
142
143
     */
    private function sendEmail(array $mailConfig) {
        $args = array();
144
        $attachments = array();
145
        $cmdAttachments = '';
146

147
148
149
        $mailConfig[SENDMAIL_TOKEN_SUBJECT] = Support::htmlEntityEncodeDecode($mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY], $mailConfig[SENDMAIL_TOKEN_SUBJECT]);
        $mailConfig[SENDMAIL_TOKEN_BODY] = Support::htmlEntityEncodeDecode($mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY], $mailConfig[SENDMAIL_TOKEN_BODY]);

150
151
152
153
        if (isset($mailConfig[SENDMAIL_TOKEN_BODY_MODE]) && $mailConfig[SENDMAIL_TOKEN_BODY_MODE] === SENDMAIL_TOKEN_BODY_MODE_HTML) {
            $mailConfig[SENDMAIL_TOKEN_BODY] = Support::wrapTag(SENDMAIL_HTML_TOKEN, $mailConfig[SENDMAIL_TOKEN_BODY]);
        }

154
        foreach ($mailConfig as $key => $value) {
155
156
157
            if (is_array($value)) {
                continue;
            }
158
            if ($key != SENDMAIL_TOKEN_SUBJECT) { // do not escape double ticks in subject - this breaks the UTF8 encoding later
159
160
                $mailConfig[$key] = Support::escapeDoubleTick($value);
            }
161
162
        }

163
164
        $args[] = '-f "' . $mailConfig[SENDMAIL_TOKEN_SENDER] . '"';
        $args[] = '-t "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER] . '"';
165
166
167
168
        $args[] = '-o message-charset="utf-8"';

        $logFile = $this->store->getVar(SYSTEM_MAIL_LOG, STORE_SYSTEM);
        if ($logFile != '' && $logFile !== false) {
169
            $args[] = '-l "' . $logFile . '"';
170
171
        }

172
        if (!empty($mailConfig[SENDMAIL_TOKEN_RECEIVER_CC])) {
173
            $args[] = '-cc "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] . '"';
174
175
        }

176
        if (!empty($mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC])) {
Carsten  Rose's avatar
Carsten Rose committed
177
            $args[] = '-bcc "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] . '"';
178
179
        }

180
        if (!empty($mailConfig[SENDMAIL_TOKEN_SUBJECT])) {
181

182
183
            // The subject needs to be encoded to UTF-8 separately - https://stackoverflow.com/questions/4389676/email-from-php-has-broken-subject-header-encoding/27648245#27648245
            $preferences = ["scheme" => "Q", "input-charset" => "UTF-8", "output-charset" => "UTF-8"];
184
            $encodedSubject = iconv_mime_encode("Subject", $mailConfig[SENDMAIL_TOKEN_SUBJECT], $preferences);
185
186
            $encodedSubject = substr($encodedSubject, 9); // remove 'Subject: '

187
188
            $encodedSubject = Support::escapeDoubleTick($encodedSubject); // now, escape double ticks.

189
            $args[] = '-u "' . $encodedSubject . '"';
190
191
        }

192
        if (!empty($mailConfig[SENDMAIL_TOKEN_BODY])) {
193

194
            $args[] = '-m "' . $mailConfig[SENDMAIL_TOKEN_BODY] . '"';;
195
196
        }

197
198
        if (!empty($mailConfig[SENDMAIL_TOKEN_REPLY_TO])) {
            $args[] = '-o reply-to="' . $mailConfig[SENDMAIL_TOKEN_REPLY_TO] . '"';;
199
200
        }

201
        if ($mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] === 'on') {
202
203
204
            $args[] = '-o message-header="Auto-Submitted: auto-send"';
        }

205
206
        if (!empty($mailConfig[SENDMAIL_TOKEN_ATTACHMENT])) {
            $attachments = $this->attachmentsBuild($mailConfig[SENDMAIL_TOKEN_ATTACHMENT]);
207
            if (!empty($attachments)) {
208
209
                $cmdAttachments = '-a ' . implode(' -a ', $attachments);
                $args[] = $cmdAttachments;
210
            }
211
212
        }

213
214
        if (!empty($mailConfig[SENDMAIL_TOKEN_HEADER])) {
            $args[] = '-o message-header="' . $mailConfig[SENDMAIL_TOKEN_HEADER] . '"';
215
216
217
218
        }

        $sendEmail = $this->store->getVar(SYSTEM_SEND_E_MAIL, STORE_SYSTEM);
        if (empty($sendEmail)) {
Marc Egger's avatar
Marc Egger committed
219
            throw new \UserFormException("Missing 'sendEmail'", ERROR_SENDMAIL);
220
221
222
223
224
225
226
        }

        $sendEmailOptions = $this->store->getVar(SYSTEM_SEND_E_MAIL_OPTIONS, STORE_SYSTEM);
        if (!empty($sendEmailOptions)) {
            $args[] = $sendEmailOptions;
        }

227
        OnArray::arrayEscapeshellarg($args);
228
        $cmd = $sendEmail . ' ' . implode(' ', $args);
229
        $cmd = str_replace('`', '\`', $cmd); // escape backticks (escapeshellcmd would be too thorough)
230

231
        $output = Support::qfqExec($cmd, $rc);
232
        if ($rc != 0) {
233

234
            // After first installation of QFQ extension, the PERL script is not executable: is this the problem here?
235
            clearstatcache();
236
237
            $perms = fileperms($sendEmail);
            if (!($perms & 0x0040)) {
238

239
                chmod($sendEmail, 0755);
240
241
                clearstatcache();

242
                $output = Support::qfqExec($cmd, $rc);
243

244
245
                if ($rc != 0) {
                    if (!is_file($sendEmail) || !is_readable($sendEmail)) {
Marc Egger's avatar
Marc Egger committed
246
                        throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Command 'sendEmail' not found."
Marc Egger's avatar
Marc Egger committed
247
                            , ERROR_MESSAGE_TO_DEVELOPER => $sendEmail]), ERROR_SENDMAIL);
248
                    }
249

250
                    if (!is_executable($sendEmail)) {
Marc Egger's avatar
Marc Egger committed
251
                        throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Command 'sendEmail' not executable."
Marc Egger's avatar
Marc Egger committed
252
                            , ERROR_MESSAGE_TO_DEVELOPER => $sendEmail]), ERROR_SENDMAIL);
253
254
                    }
                }
255
256
257
            }

            if ($rc != 0) {
Marc Egger's avatar
Marc Egger committed
258
                throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => '"Error sendmail failed', ERROR_MESSAGE_TO_DEVELOPER => "[cmd=$cmd]$output"]), ERROR_SENDMAIL);
259
260
            }
        }
261

262
        HelperFile::cleanTempFiles($attachments);
263

264
        return $cmdAttachments;
265
    }
266

267

268
    /**
269
     * Creates a new MailLog Record based on $mailArr / $header.
270
     *
271
     * @param array $mailConfig
272
     * @param string $attachmentsLine
273
     *
Marc Egger's avatar
Marc Egger committed
274
275
276
277
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
278
     */
279
    private function mailLog(array $mailConfig, $attachmentsLine = '') {
280

281
        $log = array();
282

283
284
285
286
287
        $attachments = '';
        if (!empty($mailConfig[SENDMAIL_TOKEN_ATTACHMENT]) && is_array($mailConfig[SENDMAIL_TOKEN_ATTACHMENT])) {
            $attachments = OnArray::toString($mailConfig[SENDMAIL_TOKEN_ATTACHMENT]);
        }

288
289
290
        $header = 'OoO:' . $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT];
        if (!empty($mailConfig[SENDMAIL_TOKEN_HEADER])) {
            $header .= PHP_EOL . 'Custom: ' . $mailConfig[SENDMAIL_TOKEN_HEADER];
291
        }
292
        if (!empty($mailConfig[SENDMAIL_TOKEN_ATTACHMENT])) {
293
            $header .= PHP_EOL . 'Attachment: ' . $attachments;
294
295
        }

296
        // Log
297
        $log[] = $mailConfig[SENDMAIL_TOKEN_RECEIVER];
298
        $log[] = $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] ?? '';
299
        $log[] = $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] ?? '';
300
301
302
        $log[] = $mailConfig[SENDMAIL_TOKEN_SENDER];
        $log[] = $mailConfig[SENDMAIL_TOKEN_SUBJECT];
        $log[] = $mailConfig[SENDMAIL_TOKEN_BODY];
303
        $log[] = $header;
304
        $log[] = $attachmentsLine;
305
306
307
308
309
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_GR_ID]) ? 0 : $mailConfig[SENDMAIL_TOKEN_GR_ID];
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_X_ID]) ? 0 : $mailConfig[SENDMAIL_TOKEN_X_ID];
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_X_ID2]) ? 0 : $mailConfig[SENDMAIL_TOKEN_X_ID2];
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_X_ID3]) ? 0 : $mailConfig[SENDMAIL_TOKEN_X_ID3];
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_SRC]) ? 0 : $mailConfig[SENDMAIL_TOKEN_SRC];
310

311
        $db = new Database();
312
        $db->sql('INSERT INTO MailLog (`receiver`, `cc`, `bcc`, `sender`, `subject`, `body`, `header`, `attach`, `grId`, `xId`, `xId2`, `xId3`, `src`, `modified`, `created`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW() )', ROW_REGULAR, $log);
313

314
    }
315
316

    /**
317
318
319
320
     *
     * @param array $attachments Array of attachments. Per attachment, different & multiple sources are possible.
     *              [ [ 0 -> 'F:/etc/hostname' ],
     *                [ 0 -> 'u:http://nzz.ch', 1 -> 'd:nzz.pdf' ],
321
     *                [ 0 -> 'p:id=detailPerson&form=Person&r=1&_sip=1', 1 -> 'F:/etc/hostname'  ]
322
323
     *                [ 0 -> 'd:all.pdf', 1 -> 'U:?id=detailPerson&form=Person&r=1&_sip=1', 2 -> 'F:/etc/hostname'  ] ]
     * @return array Array of filenames. Those files has to be deleted later, if they are temporary files.
Marc Egger's avatar
Marc Egger committed
324
325
326
327
328
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
     * @throws \UserFormException
     * @throws \UserReportException
329
330
331
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
332
333
334
     */
    private function attachmentsBuild(array $attachments) {
        $files = array();
335
336
        $download = new Download();
        $tmpDir = sys_get_temp_dir();
337

338
        // Several attachments are possible. Process one by one.
339
        foreach ($attachments as $attach) {
340
341
            $vars = array();

342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
            // Extract Filename
            $exportFilename = '';
            $downloadMode = '';

            // Per attachment: extract 'mode','filename'. Leave the 'sources' untouched.
            foreach ($attach as $key => $element) {
                $token = $element[0];
                switch ($token) {

                    case SENDMAIL_TOKEN_DOWNLOAD_FILENAME:
                        $exportFilename = substr($element, 2);
                        unset($attach[$key]);
                        break;

                    case SENDMAIL_TOKEN_DOWNLOAD_MODE:
                        $downloadMode = substr($element, 2);
                        unset($attach[$key]);
                        break;

                    case SENDMAIL_TOKEN_ATTACHMENT_FILE:
362
                        if ($downloadMode == '') { // Set only if not explicitly given.
363
364
365
366
367
                            $downloadMode = DOWNLOAD_MODE_FILE;
                        }
                        break;

                    case SENDMAIL_TOKEN_ATTACHMENT_URL:
368
369
                    case SENDMAIL_TOKEN_ATTACHMENT_URL_PARAM:
                    case SENDMAIL_TOKEN_ATTACHMENT_PAGE:
370
                        if ($downloadMode == '') { // Set only if not explicitly given.
371
372
373
374
375
                            $downloadMode = DOWNLOAD_MODE_PDF;
                        }
                        break;

                    default:
Marc Egger's avatar
Marc Egger committed
376
                        throw new \UserReportException('Unknown token in _sendmail: ' . $token, ERROR_UNKNOWN_TOKEN);
377
378
379
380
381
382
                        break;
                }
            }

            $vars[SIP_DOWNLOAD_PARAMETER] = implode(PARAM_DELIMITER, $attach);
            $vars[DOWNLOAD_MODE] = (count($attach) > 1) ? DOWNLOAD_MODE_PDF : $downloadMode;
383

384
            $file = $download->process($vars, OUTPUT_MODE_FILE);
385

386
            if (empty($exportFilename) && HelperFile::isQfqTemp($file)) {
387

388
389
390
                $exportFilename = DOWNLOAD_OUTPUT_FILENAME;
                if ($downloadMode == DOWNLOAD_MODE_PDF) {
                    $exportFilename .= '.pdf';
391
                }
392
393
394
395
396
397
398
399
400
401
402
            }

            // In case an exportFilename is given: move/rename it, if it is necessary.
            if (!empty($exportFilename)) {

                $exportFilename = Sanitize::safeFilename($exportFilename, true);

                $dir = HelperFile::mktempdir(); // Attachments might have the same filename - create one directory per attachment, to ensure same filenames do not conflict.
                $exportFilename = $dir . '/' . $exportFilename;

                if (HelperFile::isQfqTemp($file)) {
403
                    HelperFile::rename($file, $exportFilename);
404
                } else {
405
                    HelperFile::copy($file, $exportFilename);
406
                }
407
                $file = $exportFilename;
408
            }
409
            $files[] = $file;
410
        }
411
412

        return $files;
413
    }
414
415
416
417


    /**
     * Convert a token based sendMail string into an array.
418
     * - Each attachment (single file or multiple concatenated files) is an array in the array.
419
420
421
     *
     * @param string $data E.g.: 't:john@doe.com|f:jane@miller.com|s:Latest|b:Dear John ...'
     * @return array
Marc Egger's avatar
Marc Egger committed
422
     * @throws \UserFormException
423
424
425
     */
    public function parseStringToArray($data) {

426
        $args = array();
427
428
429
430
431
432
        $attachment = array();

        $flagConcat = false;
        $flagSource = false;
        $flagFilename = false;

433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
        $convertToShorthandToken = [
            SENDMAIL_TOKEN_RECEIVER_LONG => SENDMAIL_TOKEN_RECEIVER,
            SENDMAIL_TOKEN_SENDER_LONG => SENDMAIL_TOKEN_SENDER,
            SENDMAIL_TOKEN_SUBJECT_LONG => SENDMAIL_TOKEN_SUBJECT,
            SENDMAIL_TOKEN_BODY_LONG => SENDMAIL_TOKEN_BODY,
            SENDMAIL_TOKEN_RECEIVER_CC_LONG => SENDMAIL_TOKEN_RECEIVER_CC,
            SENDMAIL_TOKEN_RECEIVER_BCC_LONG => SENDMAIL_TOKEN_RECEIVER_BCC,
            SENDMAIL_TOKEN_REPLY_TO_LONG => SENDMAIL_TOKEN_REPLY_TO,
            SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT_LONG => SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT,
            SENDMAIL_TOKEN_GR_ID_LONG => SENDMAIL_TOKEN_GR_ID,
            SENDMAIL_TOKEN_X_ID_LONG => SENDMAIL_TOKEN_X_ID,
            SENDMAIL_TOKEN_X_ID2_LONG => SENDMAIL_TOKEN_X_ID2,
            SENDMAIL_TOKEN_X_ID3_LONG => SENDMAIL_TOKEN_X_ID3,
            SENDMAIL_TOKEN_HEADER_LONG => SENDMAIL_TOKEN_HEADER,
            SENDMAIL_TOKEN_SRC_LONG => SENDMAIL_TOKEN_SRC,
448
            SENDMAIL_TOKEN_BODY_MODE_LONG => SENDMAIL_TOKEN_BODY_MODE,
449
450
        ];

451
        $param = explode(PARAM_DELIMITER, $data);
452

453
        // Iterate over all parameter: use token as key. Collect corresponding attachments arguments in separate array elements.
454
455
456
457
458
459
        foreach ($param AS $line) {

            if (empty($line)) {
                continue;
            }

460
            $tokenAndValue = explode(PARAM_TOKEN_DELIMITER, $line, 2);
461
            if (count($tokenAndValue) < 2 && $tokenAndValue[0] !== SENDMAIL_TOKEN_CONCAT) {
Marc Egger's avatar
Marc Egger committed
462
                throw new \UserFormException('Missing token delimiter "' . PARAM_TOKEN_DELIMITER . '" in: ' . $line, ERROR_UNKNOWN_TOKEN);
463
464
            }

465
466
467
468
469
470
471
            $token = $tokenAndValue[0];
            // convert speaking word tokens to shorthand
            if (strlen($token) > 1) {
                $token = strtolower($token); // speaking word tokens are all lowercase
                if (isset($convertToShorthandToken[$token])) {
                    $token = $convertToShorthandToken[$token];
                } else {
Marc Egger's avatar
Marc Egger committed
472
                    throw new \UserFormException('Unknown token "' . $token . '" in: ' . $line, ERROR_UNKNOWN_TOKEN);
473
474
                }
            }
475

476
477
            // Check for deprecated token.
            if ($token == SENDMAIL_TOKEN_ATTACHMENT_FILE_DEPRECATED) {
Marc Egger's avatar
Marc Egger committed
478
                throw new \UserFormException('Sendmail: Option "a:" is deprecated, please use "' . SENDMAIL_TOKEN_ATTACHMENT_FILE . '" instead', ERROR_UNKNOWN_TOKEN);
479
480
            }

481
            switch ($token) {
482
483
484
                case SENDMAIL_TOKEN_CONCAT:
                    $flagConcat = true;
                    if (!empty($attachment)) {
485
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
486
487
488
489
490
491
                        $attachment = array();
                    }
                    break;

                case SENDMAIL_TOKEN_ATTACHMENT_FILE:
                case SENDMAIL_TOKEN_ATTACHMENT_URL:
492
493
                case SENDMAIL_TOKEN_ATTACHMENT_URL_PARAM:
                case SENDMAIL_TOKEN_ATTACHMENT_PAGE:
494
                    if ($flagSource && !$flagConcat) {
495
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
496
497
498
499
500
501
502
503
504
                        $attachment = array();
                        $flagFilename = false;
                    }
                    $flagSource = true;
                    $attachment[] = $line;
                    break;

                case SENDMAIL_TOKEN_DOWNLOAD_FILENAME:
                    if ($flagFilename && !$flagConcat) {
505
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
506
507
508
509
510
511
512
513
                        $attachment = array();
                        $flagSource = false;
                    }
                    $flagFilename = true;
                    $attachment[] = $line;
                    break;

                default:
514
                    $args[$token] = $tokenAndValue[1];
515
516
517
518
519
                    break;
            }
        }

        if (!empty($attachment)) {
520
            $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
521
522
        }

523
        return ($args);
524
525
    }

526
}