Completed
Pull Request — master (#29)
by Goffy
01:40
created

include/task.inc.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * ****************************************************************************
4
 *  - A Project by Developers TEAM For Xoops - ( https://xoops.org )
5
 * ****************************************************************************
6
 *  XNEWSLETTER - MODULE FOR XOOPS
7
 *  Copyright (c) 2007 - 2012
8
 *  Goffy ( wedega.com )
9
 *
10
 *  You may not change or alter any portion of this comment or credits
11
 *  of supporting developers from this source code or any supporting
12
 *  source code which is considered copyrighted (c) material of the
13
 *  original comment or credit authors.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *  ---------------------------------------------------------------------------
20
 * @copyright  Goffy ( wedega.com )
21
 * @license    GPL 2.0
22
 * @package    xnewsletter
23
 * @author     Goffy ( [email protected] )
24
 *
25
 * ****************************************************************************
26
 */
27
28
use XoopsModules\Xnewsletter;
29
30
// defined("XOOPS_ROOT_PATH") || die("XOOPS root path not defined");
31
require_once __DIR__ . '/common.php';
32
33
/**
34
 * @param $op
35
 * @param $letter_id
36
 * @param $xn_send_in_packages
37
 * @param $xn_send_in_packages_time
38
 *
39
 * @return null|bool
40
 */
41
function xnewsletter_createTasks($op, $letter_id, $xn_send_in_packages, $xn_send_in_packages_time)
42
{
43
    global $xoopsUser, $xoopsDB;
44
    $helper = Xnewsletter\Helper::getInstance();
45
46
    $uid = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0;
47
48
    // check data before creating task list
49
    if (0 == $letter_id) {
50
        redirect_header('letter.php', 3, _AM_XNEWSLETTER_SEND_ERROR_NO_LETTERID);
51
    }
52
    $letterObj = $helper->getHandler('Letter')->get($letter_id);
53
    if (0 == count($letterObj)) {
54
        redirect_header('letter.php', 3, _AM_XNEWSLETTER_SEND_ERROR_NO_LETTERID);
55
    }
56
57
    // read categories
58
    $letter_cats = $letterObj->getVar('letter_cats');
59
    if ('' == $letter_cats) {
60
        // no cats
61
        redirect_header('letter.php', 3, _MA_XNEWSLETTER_LETTER_NONEAVAIL);
62
    }
63
64
    if ('send_test' === $op) {
65
        //check for valid email for testing
66
        $letter_email_test = $letterObj->getVar('letter_email_test');
67
        if ('' == $letter_email_test) {
68
            redirect_header('letter.php', 3, _AM_XNEWSLETTER_SEND_ERROR_NO_EMAIL);
69
        }
70
    }
71
72
    // get emails of subscribers
73
    $recipients = [];
74
    if ('send_test' === $op) {
75
        $recipients[] = 0;
76
    } else {
77
        // read all subscribers
78
        $sql = 'SELECT subscr_id, subscr_actkey ';
79
        $sql .= " FROM {$xoopsDB->prefix('xnewsletter_subscr')} INNER JOIN {$xoopsDB->prefix('xnewsletter_catsubscr')} ON subscr_id = catsubscr_subscrid ";
80
        $sql .= ' WHERE subscr_activated=1 AND (((catsubscr_catid) IN (';
81
        $sql .= str_replace('|', ',', $letter_cats);
82
        $sql .= '))) GROUP BY subscr_id;';
83
84
        if (!$subscrs = $xoopsDB->query($sql)) {
85
            die();
86
        }
87
88
        while (false !== ($subscr = $xoopsDB->fetchArray($subscrs))) {
89
            $subscr_id = $subscr['subscr_id'];
90
            if ('resend_letter' === $op) {
91
                // read subscribers, where send failed
92
                $protocolCriteria = new \CriteriaCompo();
93
                $protocolCriteria->add(new \Criteria('protocol_letter_id', $letter_id));
94
                $protocolCriteria->add(new \Criteria('protocol_subscriber_id', $subscr_id));
95
                $protocolCriteria->add(new \Criteria('protocol_success', true));
96
                $protocolsCriteria = $helper->getHandler('Protocol')->getCount($protocolCriteria);
97
                if ($protocolsCriteria > 0) {
98
                    $subscr_id = 0;
99
                } // letter already successfully sent
100
            }
101
            if ($subscr_id > 0) {
102
                if ('' == $subscr['subscr_actkey']) {
103
                    $subscrObj               = $helper->getHandler('Subscr')->get($subscr_id);
104
                    $subscr['subscr_actkey'] = xoops_makepass();
105
                    $subscrObj->setVar('subscr_actkey', $subscr['subscr_actkey']);
106
                    $helper->getHandler('Subscr')->insert($subscrObj);
107
                    unset($subscrObj);
108
                }
109
                $recipients[] = $subscr['subscr_id'];
110
            }
111
        }
112
    }
113
114
    if (0 == count($recipients)) {
115
        redirect_header('letter.php', 3, _AM_XNEWSLETTER_SEND_ERROR_NO_SUBSCR);
116
    } else {
117
        // creating task list
118
        $counter        = 0;
119
        $task_starttime = time() - 1;
120
        foreach ($recipients as $subscr_id) {
121
            // calculate start time, if letter should be sent in packages
122
            if ($xn_send_in_packages > 0) {
123
                if ($counter == $xn_send_in_packages) {
124
                    $task_starttime = $task_starttime + 60 * $xn_send_in_packages_time;
125
                    $counter        = 0;
126
                }
127
            }
128
            ++$counter;
129
            // create task list item
130
            $sql = "INSERT INTO `{$xoopsDB->prefix('xnewsletter_task')}`";
131
            $sql .= ' (`task_letter_id`, `task_subscr_id`,  `task_starttime`, `task_submitter`, `task_created` )';
132
            $sql .= " VALUES ({$letter_id}, {$subscr_id}, {$task_starttime}, {$uid}, " . time() . ')';
133
            if (!$xoopsDB->queryF($sql)) {
134
                $protocolObj = $helper->getHandler('Protocol')->create();
135
                $protocolObj->setVar('protocol_letter_id', $letter_id);
136
                $protocolObj->setVar('protocol_subscriber_id', $subscr_id);
137
                $protocolObj->setVar('protocol_status', _AM_XNEWSLETTER_TASK_ERROR_CREATE);
138
                $protocolObj->setVar('protocol_status_str_id', _XNEWSLETTER_PROTOCOL_STATUS_ERROR_CREATE_TASK);
139
                $protocolObj->setVar('protocol_status_vars', []);
140
                $protocolObj->setVar('protocol_success', false);
141
                $protocolObj->setVar('protocol_submitter', $uid);
142
                $protocolObj->setVar('protocol_created', time());
143
                if (!$helper->getHandler('Protocol')->insert($protocolObj)) {
144
                    echo $protocolObj->getHtmlErrors();
145
                }
146
                unset($protocolObj);
147
148
                return false;
149
            } elseif ('send_test' !== $op) {
150
                // update letter
151
                $letterObj = $helper->getHandler('Letter')->get($letter_id);
152
                $letterObj->setVar('letter_sender', $uid);
153
                $letterObj->setVar('letter_sent', time());
154
                $helper->getHandler('Letter')->insert($letterObj);
155
            }
156
        }
157
    }
158
159
    return true;
160
}
161
162
/**
163
 * @param     $xn_send_in_packages
164
 * @param int $letter_id
165
 *
166
 * @return mixed|string
167
 */
168
function xnewsletter_executeTasks($xn_send_in_packages, $letter_id = 0)
169
{
170
    require_once XOOPS_ROOT_PATH . '/modules/xnewsletter/include/functions.php';
171
//    require_once XNEWSLETTER_ROOT_PATH . '/class/class.xnewslettermailer.php';
172
173
    global $XoopsTpl, $xoopsDB, $xoopsUser;
174
    $helper = Xnewsletter\Helper::getInstance();
175
176 View Code Duplication
    if (!isset($xoopsTpl) || !is_object($xoopsTpl)) {
177
        require_once XOOPS_ROOT_PATH . '/class/template.php';
178
        $xoopsTpl = new \XoopsTpl();
179
    }
180
181
    // get once template path
182
    $template_path = XNEWSLETTER_ROOT_PATH . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
183
    if (!is_dir($template_path)) {
184
        $template_path = XNEWSLETTER_ROOT_PATH . '/language/english/templates/';
185
    }
186
    if (!is_dir($template_path)) {
187
        return str_replace('%p', $template_path, _AM_XNEWSLETTER_SEND_ERROR_INALID_TEMPLATE_PATH);
188
    }
189
190
    $uid         = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0;
191
    $count_total = 0;
192
    $count_err   = 0;
193
        
194
    //get letters ready to send groups by letter_id
195
    $sql = "SELECT `task_letter_id` FROM {$xoopsDB->prefix('xnewsletter_task')}";
196
    if ($letter_id > 0) {
197
        $sql .= " WHERE (`task_letter_id`={$letter_id})";
198
    }
199
    $sql .= ' GROUP BY `task_letter_id`';
200
    if (!$task_letters = $xoopsDB->query($sql)) {
201
        return _AM_XNEWSLETTER_SEND_ERROR_NO_LETTERID;
202
    }
203
204
    while (false !== ($task_letter = $xoopsDB->fetchArray($task_letters))) {
205
        $letter_id = $task_letter['task_letter_id'];
206
        $letterObj = $helper->getHandler('Letter')->get($letter_id);
207
        if (0 == count($letterObj)) {
208
            return _AM_XNEWSLETTER_SEND_ERROR_NO_LETTERID;
209
        }
210
211
        // read categories
212
        $letter_cats = $letterObj->getVar('letter_cats');
213
        if ('' == $letter_cats) {
214
            //no cats
215
            return _MA_XNEWSLETTER_LETTER_NONEAVAIL;
216
        }
217
218
        // read data of account
219
        $letter_account = $letterObj->getVar('letter_account');
220
        if ('' == $letter_account || 0 == $letter_account) {
221
            return _MA_XNEWSLETTER_ACCOUNTS_NONEAVAIL;
222
        }
223
        $accountObj             = $helper->getHandler('Accounts')->get($letter_account);
224
        $account_type           = $accountObj->getVar('accounts_type');
225
        $account_yourname       = $accountObj->getVar('accounts_yourname');
226
        $account_yourmail       = $accountObj->getVar('accounts_yourmail');
227
        $account_username       = $accountObj->getVar('accounts_username');
228
        $account_password       = $accountObj->getVar('accounts_password');
229
        $account_server_out     = $accountObj->getVar('accounts_server_out');
230
        $account_port_out       = $accountObj->getVar('accounts_port_out');
231
        $account_securetype_out = $accountObj->getVar('accounts_securetype_out');
232
233
        // create basic mail body
234
        $letter_title   = $letterObj->getVar('letter_title');
235
        $letter_content = $letterObj->getVar('letter_content', 'n');
236
237
        $letterTpl = new \XoopsTpl();
238
        // letter data
239
        $letterTpl->assign('content', $letter_content);
240
        $letterTpl->assign('title', $letter_title); // new from v1.3
241
        // letter attachments as link
242
        $attachmentAslinkCriteria = new \CriteriaCompo();
243
        $attachmentAslinkCriteria->add(new \Criteria('attachment_letter_id', $letter_id));
244
        $attachmentAslinkCriteria->add(new \Criteria('attachment_mode', _XNEWSLETTER_ATTACHMENTS_MODE_ASLINK));
245
        $attachmentAslinkCriteria->setSort('attachment_id');
246
        $attachmentAslinkCriteria->setOrder('ASC');
247
        $attachmentObjs = $helper->getHandler('Attachment')->getObjects($attachmentAslinkCriteria, true);
248 View Code Duplication
        foreach ($attachmentObjs as $attachment_id => $attachmentObj) {
249
            $attachment_array                    = $attachmentObj->toArray();
250
            $attachment_array['attachment_url']  = XNEWSLETTER_URL . "/attachment.php?attachment_id={$attachment_id}";
251
            $attachment_array['attachment_link'] = XNEWSLETTER_URL . "/attachment.php?attachment_id={$attachment_id}";
252
            $letterTpl->append('attachments', $attachment_array);
253
        }
254
        // extra data
255
        $letterTpl->assign('date', time()); // new from v1.3
256
        $letterTpl->assign('xoops_url', XOOPS_URL); // new from v1.3
257
        $letterTpl->assign('xoops_langcode', _LANGCODE); // new from v1.3
258
        $letterTpl->assign('xoops_charset', _CHARSET); // new from v1.3
259
260
        // get emails of subscribers
261
        $recipients   = [];
262
        $sql_tasklist = "SELECT `task_id`, `task_subscr_id` FROM {$xoopsDB->prefix('xnewsletter_task')}";
263
        $sql_tasklist .= " WHERE ((`task_letter_id`= {$letter_id}) AND (`task_starttime` < " . time() . '))';
264
        if (!$task_letters = $xoopsDB->query($sql_tasklist)) {
265
            return $task_letters->getErrors();
266
        }
267
        $recipients = [];
268
        while (false !== ($task_letter = $xoopsDB->fetchArray($task_letters))) {
269
            $subscr_id = $task_letter['task_subscr_id'];
270
            $task_id   = $task_letter['task_id'];
271
            if (0 == $subscr_id) {
272
                $recipients[] = [
273
                    'task_id'           => $task_id,
274
                    'address'           => $letterObj->getVar('letter_email_test'),
275
                    'firstname'         => _AM_XNEWSLETTER_SUBSCR_FIRSTNAME_PREVIEW,
276
                    'lastname'          => _AM_XNEWSLETTER_SUBSCR_LASTNAME_PREVIEW,
277
                    'subscr_sex'        => _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW,
278
                    'subscriber_id'     => '0',
279
                    'catsubscr_id'      => '0',
280
                    'subscriber_actkey' => 'Test',
281
                ];
282
            } else {
283
                $sql_subscr = "SELECT * FROM {$xoopsDB->prefix('xnewsletter_subscr')}";
284
                $sql_subscr .= " WHERE `subscr_id`= {$subscr_id}";
285
                if (!$task_subscrs = $xoopsDB->query($sql_subscr)) {
286
                    return $task_subscrs->getErrors();
287
                }
288
289
                $subscr       = $xoopsDB->fetchArray($task_subscrs);
290
                $recipients[] = [
291
                    'task_id'           => $task_id,
292
                    'address'           => $subscr['subscr_email'],
293
                    'firstname'         => $subscr['subscr_firstname'],
294
                    'lastname'          => $subscr['subscr_lastname'],
295
                    'subscr_sex'        => $subscr['subscr_sex'],
296
                    'subscriber_id'     => $subscr['subscr_id'],
297
                    'subscriber_actkey' => $subscr['subscr_actkey'],
298
                ];
299
            }
300
            if ($xn_send_in_packages > 0 && count($recipients) == $xn_send_in_packages) {
301
                break;
302
            }
303
        }
304
305
        if (0 == count($recipients)) {
306
            return null;
307
        }
308
309
        // get letter attachments as attachment
310
        $attachmentAsattachmentCriteria = new \CriteriaCompo();
311
        $attachmentAsattachmentCriteria->add(new \Criteria('attachment_letter_id', $letter_id));
312
        $attachmentAsattachmentCriteria->add(new \Criteria('attachment_mode', _XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT));
313
        $attachmentAsattachmentCriteria->setSort('attachment_id');
314
        $attachmentAsattachmentCriteria->setOrder('ASC');
315
        $attachmentObjs  = $helper->getHandler('Attachment')->getObjects($attachmentAsattachmentCriteria, true);
316
        $attachmentsPath = [];
317 View Code Duplication
        foreach ($attachmentObjs as $attachment_id => $attachmentObj) {
318
            $attachmentsPath[] = XOOPS_UPLOAD_PATH . $helper->getConfig('xn_attachment_path') . $letter_id . '/' . $attachmentObj->getVar('attachment_name');
319
        }
320
321
        try {
322
            if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_SENDMAIL == $account_type) {
323
                $pop = new POP3();
324
                $pop->Authorise($account_server_out, $account_port_out, 30, $account_username, $account_password, 1);
325
            }
326
327
            //$mail = new PHPMailer();
328
            $mail = new Xnewsletter\XnewsletterMailer();
329
330
            $mail->CharSet = _CHARSET; //use xoops default character set
331
332
            if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_SENDMAIL == $account_type) {
333
                //$mail->IsSendmail();  Fix Error
334
            }
335
336
            $mail->Username = $account_username; // SMTP account username
337
            $mail->Password = $account_password; // SMTP account password
338
339
            if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_POP3 == $account_type) {
340
                $mail->isSMTP();
341
                //$mail->SMTPDebug = 2;
342
                $mail->Host = $account_server_out;
343
            }
344
345 View Code Duplication
            if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_SMTP == $account_type
346
                || _XNEWSLETTER_ACCOUNTS_TYPE_VAL_GMAIL == $account_type) {
347
                $mail->Port = $account_port_out; // set the SMTP port
348
                $mail->Host = $account_server_out; //sometimes necessary to repeat
349
            }
350
351
            if ('' != $account_securetype_out) {
352
                $mail->SMTPAuth   = true;
353
                $mail->SMTPSecure = $account_securetype_out; // sets the prefix to the server
354
            }
355
356
            $mail->setFrom($account_yourmail, $account_yourname);
357
            $mail->addReplyTo($account_yourmail, $account_yourname);
358
            $mail->Subject = html_entity_decode($letter_title, ENT_QUOTES);
359
360
            foreach ($recipients as $recipient) {
361
                $subscr_id = $recipient['subscriber_id'];
362
                // subscr data
363
                $letterTpl->assign('sex', $recipient['subscr_sex']);
364
                $letterTpl->assign('salutation', $recipient['subscr_sex']); // new from v1.3
365
                $letterTpl->assign('firstname', $recipient['firstname']);
366
                $letterTpl->assign('lastname', $recipient['lastname']);
367
                $letterTpl->assign('subscr_email', $recipient['address']);
368
                $letterTpl->assign('email', $recipient['address']); // new from v1.3
369
                // extra data
370
                $act           = [
371
                    XOOPS_URL,
372
                    $subscr_id,
373
                    $recipient['subscriber_actkey'],
374
                    $recipient['address'],
375
                ];
376
                $activationKey = base64_encode(implode('||', $act));
377
                $letterTpl->assign('unsubscribe_link', XOOPS_URL . "/modules/xnewsletter/subscription.php?op=unsub&email={$recipient['address']}&actkey={$activationKey}");
378
                $letterTpl->assign('unsubscribe_url', XOOPS_URL . "/modules/xnewsletter/subscription.php?op=unsub&email={$recipient['address']}&actkey={$activationKey}"); // new from v1.3
379
380
                $templateObj = $helper->getHandler('Template')->get($letterObj->getVar('letter_templateid'));
381
                if (is_object($templateObj)) {
382
                    if ( (int)$templateObj->getVar('template_type') === _XNEWSLETTER_MAILINGLIST_TPL_CUSTOM_VAL) {
383
                        // get template from database
384
                        $htmlBody = $letterTpl->fetchFromData($templateObj->getVar('template_content', 'n'));
385
                    } else {
386
                        $template = $template_path . $templateObj->getVar('template_title') . '.tpl';
387
                        $htmlBody = $letterTpl->fetch($template);
388
                    }
389
                    try {
390
                        $textBody = xnewsletter_html2text($htmlBody);
391
                    }
392
                    catch (Html2TextException $e) {
393
                        $helper->addLog($e);
394
                    }
395
                } else {
396
                    $htmlBody = _AM_XNEWSLETTER_TEMPLATE_ERR;
397
                }
398
399
                $mail->addAddress($recipient['address'], $recipient['firstname'] . ' ' . $recipient['lastname']);
400
                $mail->msgHTML($htmlBody); // $mail->Body = $htmlBody;
401
                $mail->AltBody = $textBody;
0 ignored issues
show
The variable $textBody does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
402
403
                foreach ($attachmentsPath as $attachmentPath) {
404
                    if (file_exists($attachmentPath)) {
405
                        $mail->addAttachment($attachmentPath);
406
                    }
407
                }
408
                ++$count_total;
409
410
                if ($mail->send()) {
411
                    if (0 == $subscr_id) {
412
                        $protocol_status        = _AM_XNEWSLETTER_SEND_SUCCESS_TEST . ' (' . $recipient['address'] . ')'; // old style
413
                        $protocol_status_str_id = _XNEWSLETTER_PROTOCOL_STATUS_OK_SEND_TEST; // new from v1.3
414
                        $protocol_status_vars   = ['recipient' => $recipient['address']]; // new from v1.3
415
                    } else {
416
                        $protocol_status        = _AM_XNEWSLETTER_SEND_SUCCESS; // old style
417
                        $protocol_status_str_id = _XNEWSLETTER_PROTOCOL_STATUS_OK_SEND; // new from v1.3
418
                        $protocol_status_vars   = []; // new from v1.3
419
                    }
420
                    $protocol_success = true;
421
                } else {
422
                    $protocol_status        = _AM_XNEWSLETTER_FAILED . '-> ' . $mail->ErrorInfo; // old style
423
                    $protocol_status_str_id = _XNEWSLETTER_PROTOCOL_STATUS_ERROR_SEND; // new from v1.3
424
                    $protocol_status_vars   = ['error' => $mail->ErrorInfo]; // new from v1.3
425
426
                    $protocol_success = 0; //must be 0, because 'false' cause error when inserting protokol item
427
                    ++$count_err;
428
                }
429
                //create item in protocol for this email
430
                $text_clean      = ['<strong>', '</strong>', '<br>', '<br>'];
431
                $protocol_status = str_replace($text_clean, '', $protocol_status);
432
433
                $mail->clearAddresses();
434
435
                //delete item in table task, if not from cron
436
                if (0 < $uid) {
437
                    $sql_delete = "DELETE FROM {$xoopsDB->prefix('xnewsletter_task')}";
438
                    $sql_delete .= " WHERE `task_id`= {$recipient['task_id']}";
439
                    $result = $xoopsDB->queryF($sql_delete);
440
                }
441
442
                $protocolObj = $helper->getHandler('Protocol')->create();
443
                $protocolObj->setVar('protocol_letter_id', $letter_id);
444
                $protocolObj->setVar('protocol_subscriber_id', $subscr_id);
445
                $protocolObj->setVar('protocol_status', $protocol_status); // old style
446
                $protocolObj->setVar('protocol_status_str_id', $protocol_status_str_id); // new from v1.3
447
                $protocolObj->setVar('protocol_status_vars', $protocol_status_vars); // new from v1.3
448
                $protocolObj->setVar('protocol_success', $protocol_success);
449
                $protocolObj->setVar('protocol_submitter', $uid);
450
                $protocolObj->setVar('protocol_created', time());
451
                if ($helper->getHandler('Protocol')->insert($protocolObj)) {
452
                    // create protocol is ok
453
                } else {
454
                    echo $protocolObj->getHtmlErrors();die;
455
                }
456
                unset($protocolObj);
457
            }
458
459
            unset($mail);
460
        }
461
        catch (phpmailerException $e) {
462
            // IN PROGRESS
463
            $protocol_status = _AM_XNEWSLETTER_SEND_ERROR_PHPMAILER . $e->errorMessage(); //error messages from PHPMailer
464
            ++$count_err;
465
            $protocol_success = false;
466
        }
467
        catch (\Exception $e) {
468
            // IN PROGRESS
469
            $protocol_status = _AM_XNEWSLETTER_SEND_ERROR_PHPMAILER . $e->getMessage(); //error messages from anything else!
470
            ++$count_err;
471
            $protocol_success = false;
472
        }
473
    }
474
475
    //create final protocol item
476
    if ($count_err > 0) {
477
        // IN PROGRESS
478
        $protocol_status  = xnewsletter_sprintf(_AM_XNEWSLETTER_SEND_ERROR_NUMBER, ['%e' => $count_err, '%t' => $count_total]);
479
        $protocol_success = 0; //must be 0, because 'false' cause error when inserting protokol item
480
    } else {
481
        $protocol_success = true;
482
        if ($count_total > 0) {
483
            // IN PROGRESS
484
            $protocol_status = xnewsletter_sprintf(_AM_XNEWSLETTER_SEND_SUCCESS_NUMBER, ['%t' => $count_total]);
485
        } else {
486
            // IN PROGRESS
487
            $protocol_status = '';
488
        }
489
    }
490
    $protocolObj = $helper->getHandler('Protocol')->create();
491
    $protocolObj->setVar('protocol_letter_id', $letter_id);
492
    $protocolObj->setVar('protocol_subscriber_id', 0);
493
    $protocolObj->setVar('protocol_status', $protocol_status);
494
    $protocolObj->setVar('protocol_status_str_id', 0); // new from v1.3
495
    $protocolObj->setVar('protocol_status_vars', []); // new from v1.3
496
    $protocolObj->setVar('protocol_success', $protocol_success);
497
    $protocolObj->setVar('protocol_submitter', $uid);
498
    $protocolObj->setVar('protocol_created', time());
499
    if ($helper->getHandler('Protocol')->insert($protocolObj)) {
500
        // create protocol is ok
501
    } else {
502
        echo $protocolObj->getHtmlErrors();die;
503
    }
504
    unset($protocolObj);
505
506
    return $protocol_status;
507
}
508