Issues (381)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

subscription.php (14 issues)

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
use Xmf\Request;
30
31
$currentFile = basename(__FILE__);
32
require_once __DIR__ . '/header.php';
33
$op            = Request::getString('op', 'list_subscriptions');
34
$activationKey = Request::getString('actkey', '');
35
$subscr_id     = Request::getInt('subscr_id', 0);
36
$subscr_email  = Request::getString('subscr_email', '');
37
38
if (Request::hasVar('addnew', 'REQUEST')) {
39
    $op = 'addnew_subscription';
40
}
41
42
$show_anon = false;
43
if ('' != $activationKey && 'anonlistsubscr' === $op) {
44
    $op = 'list_subscriptions';
45
    $show_anon = true;
46
} else if ('' != $activationKey && ('edit_subscription' === $op || 'delete_subscription' === $op || 'list_subscriptions' === $op)) {
47
} else if ('' != $activationKey && 'unsub' !== $op && 'search_subscriptions' !== $op) {
48
    $op = 'save_subscription';
49
}
50
if ('unsub' === $op) {
51
    $subscr_email = Request::getString('email', '');
52
    $op           = 'delete_subscription';
53
    //$GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_subscription.tpl';
54
    $_SESSION['redirect_mail'] = Request::getString('email', '');
55
    $_SESSION['unsub']         = '1';
56
} else {
57
    $_SESSION['redirect_mail'] = '';
58
    $_SESSION['unsub']         = '0';
59
}
60
61
$uid = is_object($xoopsUser) ? (int)$xoopsUser->getVar('uid') : 0;
62
63
//to avoid errors in debug when xn_groups_change_other
64
$subscr_sex       = '';
65
$subscr_firstname = '';
66
$subscr_lastname  = '';
67
68
switch ($op) {
69
    case 'search_subscription':
70
    default:
71
        // if not anonymous subscriber / subscriber is a Xoops user
72
        if ($uid > 0) {
73
            header("Location:{$currentFile}?op=list_subscriptions&subscr_email=" . $subscr_email);
74
            exit();
75
        }
76
        // if anonymous subscriber
77
        $GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_subscription_list_subscriptions.tpl';
78
        require_once XOOPS_ROOT_PATH . '/header.php';
79
80
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
81
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
82
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
83
        // breadcrumb
84
        $breadcrumb = new Xnewsletter\Breadcrumb();
85
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
86
        $breadcrumb->addLink(_MD_XNEWSLETTER_SUBSCRIBE, '');
87
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
88
89
        $actionProts_ok      = [];
90
        $actionProts_error   = [];
91
        $actionProts_warning = [];
92
93
        $subscr_email         = '';
94
        $showSubscrSearchForm = true;
95
        $showSubscrForm       = false;
96
97
        // show search subscr form
98
        $xoopsTpl->assign('showSubscrSearchForm', $showSubscrSearchForm);
99
        // show form search
100
        $subscrObj = $helper->getHandler('Subscr')->create();
101
        $xoopsTpl->assign('subscrSearchForm', $subscrObj->getSearchForm('subscription.php')->render());
102
103
        break;
104
        
105
    case 'list_subscriptions':
106
        $GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_subscription_list_subscriptions.tpl';
107
        require_once XOOPS_ROOT_PATH . '/header.php';
108
109
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
110
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
111
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
112
        // breadcrumb
113
        $breadcrumb = new Xnewsletter\Breadcrumb();
114
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
115
        $breadcrumb->addLink(_MD_XNEWSLETTER_SUBSCRIBE, '');
116
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
117
        // init vars
118
        $actionProts_ok       = [];
119
        $actionProts_warning  = [];
120
        $actionProts_error    = [];
121
        $showSubscrSearchForm = false;
122
        $showSubscrForm       = true;
123
124
        // get newsletters available for current user
125
        /** @var \XoopsGroupPermHandler $grouppermHandler */
126
        $grouppermHandler = xoops_getHandler('groupperm');
127
        $groups           = [0 => XOOPS_GROUP_ANONYMOUS];
128
        if ($uid > 0) {
129
            $groups = $xoopsUser->getGroups();
130
        }
131
132
        $catCriteria = new \CriteriaCompo();
133
        $catCriteria->setSort('cat_id');
134
        $catCriteria->setOrder('ASC');
135
        $catObjs = $helper->getHandler('Cat')->getAll($catCriteria);
136
        $cats_readable = [];
137
        $cats_showlist = [];
138
        foreach ($catObjs as $cat_id => $catObj) {
139 View Code Duplication
            if ($grouppermHandler->checkRight('newsletter_read_cat', $cat_id, $groups, $helper->getModule()->mid())) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
                $cats_readable[$cat_id]['cat_id'] = $cat_id;
141
                $cats_readable[$cat_id]['cat_name'] = $catObj->getVar('cat_name');
142
            }
143 View Code Duplication
            if ($grouppermHandler->checkRight('newsletter_list_cat', $cat_id, $groups, $helper->getModule()->mid())) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
                $cats_showlist[$cat_id]['cat_id'] = $cat_id;
145
                $cats_showlist[$cat_id]['cat_name'] = $catObj->getVar('cat_name');
146
            }
147
        }
148
        $perm_read_cat = (count($cats_readable) > 0);
149
        $perm_list_cat = (count($cats_showlist) > 0);
150
151
        if ($show_anon) {
152
            // anonymous user with activation key
153
            $search_mail = $subscr_email;
154
        } else if ($uid > 0) {
155
            // not anonymous subscriber
156
            // check whether current user has the right to see list subscribers, then take email from form
157
            if ($perm_list_cat) {
158
                $search_mail = $subscr_email;
159
            } else {
160
                // if user has no right to see list subscribers, then take email from Xoops user
161
                $search_mail = $xoopsUser->email();
162
            }
163
        } else {
164
            // if anonymous subscriber get subscr_email from search form
165
            if ('' != $subscr_email) {
166
                $search_mail = $subscr_email;
167
                // check captcha
168
                xoops_load('xoopscaptcha');
169
                $xoopsCaptcha = XoopsCaptcha::getInstance();
170
                if (!$xoopsCaptcha->verify()) {
171
                    $_SESSION['redirect_mail'] = $subscr_email;
172
                    redirect_header('?op=search_subscription', 3, $xoopsCaptcha->getMessage());
173
                }
174
                // check subscr_email
175
                if (!xnewsletter_checkEmail($subscr_email)) {
176
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOEMAIL);
177
                }                
178
            } else {
179
                //redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOEMAIL);
180
            }
181
        }
182
183
        // look for existing subscriptions
184
        $subscrCriteria = new \CriteriaCompo();
185
        $subscrCriteria->add(new \Criteria('subscr_email', $search_mail));
186
        $subscrCriteria->setSort('subscr_id');
187
        $subscrCriteria->setOrder('ASC');
188
        $subscrCount = $helper->getHandler('Subscr')->getCount($subscrCriteria);
189
190
        if ('' !== $subscr_email && $subscrCount > 0) {
191
            // there are subscriptions with this email
192
            $subscr_list = '';
193
            $actionProts_ok[] = _MA_XNEWSLETTER_REGISTRATION_EXIST;
194
            $subscrObjs            = $helper->getHandler('Subscr')->getAll($subscrCriteria);
195
            foreach ($subscrObjs as $subscrObj) {
196
                $subscr_array                             = $subscrObj->toArray();
197
                $subscr_array['subscr_created_formatted'] = formatTimestamp($subscr_array['subscr_created'], $helper->getConfig('dateformat'));
198
                // subscr exists but is unactivated
199
                if (0 == $subscr_array['subscr_activated']) {
200
                    $actionProts_warning[] = str_replace('%link', "?op=resend_subscription&subscr_id={$subscr_array['subscr_id']}", _MA_XNEWSLETTER_SUBSCRIPTION_UNFINISHED);
201
                }
202
                $catsubscrCriteria = new \CriteriaCompo();
203
                $catsubscrCriteria->add(new \Criteria('catsubscr_subscrid', $subscr_array['subscr_id']));
204
                $catsubscrCriteria->setSort('catsubscr_id');
205
                $catsubscrCriteria->setOrder('ASC');
206
                $catsubscrCount = $helper->getHandler('Catsubscr')->getCount($catsubscrCriteria);
207
                $catsubscrObjs  = $helper->getHandler('Catsubscr')->getAll($catsubscrCriteria);
208
                foreach ($catsubscrObjs as $catsubscr_id => $catsubscrObj) {
209
                    $catsubscr_array              = $catsubscrObj->toArray();
210
                    $catObj                       = $helper->getHandler('Cat')->get($catsubscrObj->getVar('catsubscr_catid'));
211
                    if (is_object($catObj)) {
212
                        $cat_array                    = $catObj->toArray();
213
                        $catsubscr_array['cat']       = $cat_array;
214
                    }
215
                    $subscr_array['catsubscrs'][] = $catsubscr_array;
216
                    $subscr_list .= ' - ' . $cat_array['cat_name'] . " \n";
217
218
                    unset($catsubscr_array);
219
                    unset($cat_array);
220
                }
221
            }
222
            
223
            // check activation key
224
            $perm_showresult = false;
225
            $activationKey_array  = explode('||', base64_decode($activationKey, true));
226
            if (XOOPS_URL === trim($activationKey_array[0]) && $subscr_email === trim($activationKey_array[4]) && $subscr_array['subscr_actkey'] === trim($activationKey_array[3])) {
227
                $perm_showresult = true;
228
            }
229
            
230
            if (($uid > 0 && $perm_list_cat) || $perm_showresult) {
231
                //if user is logged in and have right to see list of registration then show corresponding result
232
                $xoopsTpl->append('subscrs', $subscr_array);
233
                $xoopsTpl->assign('subscrCount', $subscrCount);
234
                $xoopsTpl->assign('actionProts_ok', $actionProts_ok);
235
                $xoopsTpl->assign('actionProts_warning', $actionProts_warning);
236
                $xoopsTpl->assign('actionProts_error', $actionProts_error);
237
                $activationKey = base64_encode(XOOPS_URL . "||update||{$subscrObj->getVar('subscr_id')}||{$subscrObj->getVar('subscr_actkey')}||{$subscr_email}");
238
                $xoopsTpl->assign('activationKey', $activationKey);
239
            } else {
240
                // anonymous, send email with the confirmation code to given email address
241
                $activationKey = base64_encode(XOOPS_URL . "||list||{$subscrObj->getVar('subscr_id')}||{$subscrObj->getVar('subscr_actkey')}||{$subscr_email}");
242
                $xoopsMailer = xoops_getMailer();
243
                $xoopsMailer->reset();
244
                $xoopsMailer->setTemplateDir();
245
                $xoopsMailer->useMail();
246
                $xoopsMailer->setTemplate('subscriptions.tpl');
247
                $xoopsMailer->setToEmails($subscr_email);
248
                if (isset($xoopsConfig['adminmail'])) {
249
                    $xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
250
                }
251
                if (isset($xoopsConfig['sitename'])) {
252
                    $xoopsMailer->setFromName($xoopsConfig['sitename']);
253
                }
254
                $xoopsMailer->assign('EMAIL', $subscr_email);
255
                $xoopsMailer->assign('SEX', '' != $subscrObj->getVar('subscr_sex') ? $subscrObj->getVar('subscr_sex') : $subscr_sex);
256
                $xoopsMailer->assign('FIRSTNAME', '' != $subscrObj->getVar('subscr_firstname') ? $subscrObj->getVar('subscr_firstname') : $subscr_firstname);
257
                $xoopsMailer->assign('LASTNAME', '' != $subscrObj->getVar('subscr_lastname') ? $subscrObj->getVar('subscr_lastname') : $subscr_lastname);
258
                $xoopsMailer->assign('IP', xoops_getenv('REMOTE_ADDR'));
259
                $xoopsMailer->assign('RESULT', $subscr_list);
260
                $xoopsMailer->assign('CHANGELINK', XOOPS_URL . "/modules/xnewsletter/{$currentFile}?op=anonlistsubscr&subscr_email={$subscr_email}&actkey={$activationKey}");
261
                $xoopsMailer->setSubject(_MA_XNEWSLETTER_SUBSCRIPTION_SENDINFO . $GLOBALS['xoopsConfig']['sitename']);
262
                if (!$xoopsMailer->send()) {
263
                    $actionProts_error[] = _MA_XNEWSLETTER_SUBSCRIPTION_SENDINFO_ERROR . '<br>' . $xoopsMailer->getErrors();
264
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_SENDINFO_ERROR . '<br>' . $xoopsMailer->getErrors());
265
                } else {
266
                    $actionProts_ok = [];
267
                    $actionProts_ok[] = str_replace('%subscr_email', $subscr_email, _MA_XNEWSLETTER_SUBSCRIPTION_SENDINFO_OK);
268
                    $xoopsTpl->assign('actionProts_ok', $actionProts_ok);
269
                }
270
            }
271
        } else {
272
            // email not in database, show subscr form
273
            if ('' !== $subscr_email) {
274
                $actionProts_warning[] = str_replace('%s', $subscr_email, _MA_XNEWSLETTER_REGISTRATION_NONE);
275
            }
276
            $xoopsTpl->assign('actionProts_warning', $actionProts_warning);
277
            $xoopsTpl->assign('showSubscrForm', true);
278
            $subscrObj = $helper->getHandler('Subscr')->create();
279
            $subscrObj->setVar('subscr_email', $subscr_email);
280
            $form = $subscrObj->getForm($currentFile);
281
            $xoopsTpl->assign('subscrForm', $form->render());
282
        }
283
284
        if (count($cats_showlist) > 0) {
285
            // show search subscr form
286
            $xoopsTpl->assign('showSubscrSearchForm', true);
287
            // render form search
288
            $subscrObj = $helper->getHandler('Subscr')->create();
289
            $xoopsTpl->assign('subscrSearchForm', $subscrObj->getSearchForm()->render());
290
        } else {
291
            $xoopsTpl->assign('showSubscrSearchForm', false);
292
        }
293
        break;
294
    case 'resend_subscription':
295
        $GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_subscription_result.tpl';
296
        require_once XOOPS_ROOT_PATH . '/header.php';
297
298
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
299
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
300
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
301
        // breadcrumb
302
        $breadcrumb = new Xnewsletter\Breadcrumb();
303
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
304
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
305
        // init vars
306
        $actionProts_ok      = [];
307
        $actionProts_warning = [];
308
        $actionProts_error   = [];
309
310
        // check if subscr exists
311
        $subscr_id      = Request::getInt('subscr_id', 0);
312
        $subscrCriteria = new \Criteria('subscr_id', $subscr_id);
313
        $subscrCount    = $helper->getHandler('Subscr')->getCount($subscrCriteria);
314
        if (0 == $subscrCount) {
315
            redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOID);
316
        }
317
        // get subscr data
318
        $subscrObj    = $helper->getHandler('Subscr')->get($subscr_id);
319
        $subscr_email = $subscrObj->getVar('subscr_email');
320
        // resend the email with the confirmation code
321
        $xoopsMailer = xoops_getMailer();
322
        $xoopsMailer->reset();
323
        $xoopsMailer->setTemplateDir();
324
        $xoopsMailer->useMail();
325
        $xoopsMailer->setTemplate('activate.tpl');
326
        $xoopsMailer->setToEmails($subscr_email);
327
        if (isset($xoopsConfig['adminmail'])) {
328
            $xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
329
        }
330
        if (isset($xoopsConfig['sitename'])) {
331
            $xoopsMailer->setFromName($xoopsConfig['sitename']);
332
        }
333
        $xoopsMailer->assign('EMAIL', $subscr_email);
334
        $xoopsMailer->assign('SEX', $subscrObj->getVar('subscr_sex'));
335
        $xoopsMailer->assign('FIRSTNAME', $subscrObj->getVar('subscr_firstname'));
336
        $xoopsMailer->assign('LASTNAME', $subscrObj->getVar('subscr_lastname'));
337
        $xoopsMailer->assign('IP', xoops_getenv('REMOTE_ADDR'));
338
        $activationKey = base64_encode(XOOPS_URL . "||addnew||{$subscr_id}||{$subscrObj->getVar('subscr_actkey')}||{$subscr_email}");
339
        $xoopsMailer->assign('ACTLINK', XOOPS_URL . "/modules/xnewsletter/{$currentFile}?actkey={$activationKey}");
340
        $subject = _MA_XNEWSLETTER_SUBSCRIPTIONSUBJECT . $GLOBALS['xoopsConfig']['sitename'];
341
        $xoopsMailer->setSubject($subject);
342
        if (!$xoopsMailer->send()) {
343
            $actionProts_error[] = _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SENDACTKEY . '<br>' . $xoopsMailer->getErrors();
344
        } else {
345
            $actionProts_ok[] = str_replace('%subscr_email', $subscr_email, _MA_XNEWSLETTER_RESENDMAIL_REG_OK);
346
        }
347
348
        $xoopsTpl->assign('actionProts_ok', $actionProts_ok);
349
        $xoopsTpl->assign('actionProts_warning', $actionProts_warning);
350
        $xoopsTpl->assign('actionProts_error', $actionProts_error);
351
        break;
352
    case 'add_subscription':
353
    case 'create_subscription':
354
        $GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_subscription.tpl';
355
        require_once XOOPS_ROOT_PATH . '/header.php';
356
357
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
358
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
359
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
360
        // breadcrumb
361
        $breadcrumb = new Xnewsletter\Breadcrumb();
362
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
363
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
364
365
        // get create subscr form
366
        if ('' != $subscr_email) {
367
            // existing email
368
            if (!xnewsletter_checkEmail($subscr_email)) {
369
                redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOEMAIL);
370
            }
371
        } elseif ($uid > 0) {
372
            // take actual xoops user
373
            $subscr_email = $xoopsUser->email();
374
        } else {
375
            $subscr_email = '';
376
        }
377
        $subscrObj = $helper->getHandler('Subscr')->create();
378
        $subscrObj->setVar('subscr_email', $subscr_email);
379
        $subscrForm = $subscrObj->getForm();
380
        $xoopsTpl->assign('xnewsletter_content', $subscrForm->render());
381
        break;
382
    case 'edit_subscription':
383
        $GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_subscription.tpl';
384
        require_once XOOPS_ROOT_PATH . '/header.php';
385
386
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
387
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
388
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
389
        // breadcrumb
390
        $breadcrumb = new Xnewsletter\Breadcrumb();
391
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
392
        $breadcrumb->addLink(_MD_XNEWSLETTER_SUBSCRIBE, XNEWSLETTER_URL . '/subscription.php?op=list_subscriptions');
393
        $breadcrumb->addLink(_MD_XNEWSLETTER_SUBSCRIPTION_EDIT, '');
394
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
395
396
        // get edit subscr form
397
        $subscr_id = Request::getInt('subscr_id', 0);
398
        if ($subscr_id <= 0) {
399
            redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOID);
400
        }
401
        $subscrObj  = $helper->getHandler('Subscr')->get($subscr_id);
402
403
404
        $activationKey_array  = explode('||', base64_decode($activationKey, true));
405
        $activationKeyIsValid = false;
406
        if ((XOOPS_URL === trim($activationKey_array[0]))
407
            && ($subscr_id === (int)$activationKey_array[2])
408
            && ($subscrObj->getVar('subscr_actkey') === trim($activationKey_array[3]))
409
            && ($subscrObj->getVar('subscr_email') === trim($activationKey_array[4]))) {
410
            $activationKeyIsValid = true;
411
        } else {
412
            redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_INVALIDKEY);
413
        }
414
415
        $subscrForm = $subscrObj->getForm();
416
        $xoopsTpl->assign('xnewsletter_content', $subscrForm->render());
417
        break;
418
    case 'save_subscription':
419
        $GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_subscription_result.tpl';
420
        require_once XOOPS_ROOT_PATH . '/header.php';
421
422
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
423
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
424
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
425
        // breadcrumb
426
        $breadcrumb = new Xnewsletter\Breadcrumb();
427
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
428
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
429
        // init vars
430
        $actionProts_ok       = [];
431
        $actionProts_warning  = [];
432
        $actionProts_error    = [];
433
        $count_ok             = 0;
434
        $count_err            = 0;
435
        $activationKeyIsValid = false;
436
        // check right to subscribe directly
437
        $allowedWithoutActivationKey = false;
438 View Code Duplication
        if ($uid > 0) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
439
            // if not anonymous subscriber / subscriber is a Xoops user
440
            $submitter_email = $xoopsUser->email();
441
            foreach ($xoopsUser->getGroups() as $group) {
442
                if (in_array($group, $helper->getConfig('xn_groups_without_actkey'))
443
                    || XOOPS_GROUP_ADMIN == $group) {
444
                    $allowedWithoutActivationKey = true;
445
                    break;
446
                }
447
            }
448
        }
449
        // if anonymous subscriber
450
        // NOP
451
452
        if ($allowedWithoutActivationKey) {
453
            // 1st case: subscribe WITHOUT confirmation
454
            // check form
455
            if (!$GLOBALS['xoopsSecurity']->check()) {
456
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
457
            }
458
            // check email
459
            if ('' == $subscr_email || !xnewsletter_checkEmail($subscr_email)) {
460
                redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOEMAIL);
461
            }
462
            // get or create subscr
463 View Code Duplication
            if ($subscr_id > 0) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
464
                $subscrObj = $helper->getHandler('Subscr')->get($subscr_id);
465
                $saveType  = 'update';
466
            } else {
467
                $subscrObj = $helper->getHandler('Subscr')->create();
468
                $saveType  = 'addnew';
469
            }
470
            $subscrObj->setVar('subscr_sex', Request::getString('subscr_sex', ''));
471
            $subscrObj->setVar('subscr_firstname', Request::getString('subscr_firstname', ''));
472
            $subscrObj->setVar('subscr_lastname', Request::getString('subscr_lastname', ''));
473
            $subscrObj->setVar('subscr_email', Request::getString('subscr_email', ''));
474
            $subscr_actkey = ('' === Request::getString('subscr_actkey', '')) ? xoops_makepass() : Request::getString('subscr_actkey', '');
475
            $subscrObj->setVar('subscr_actkey', $subscr_actkey);
476
            // insert subscr
477 View Code Duplication
            if (!$helper->getHandler('Subscr')->insert($subscrObj)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
478
                redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SAVESUBSCR . '<br>' . $subscrObj->getHtmlErrors());
479
            }
480
            if ($subscr_id > 0) {
481
                $actionProts_ok[] = _MA_XNEWSLETTER_SUBSCRIPTION_UPDATE_OK;
482
            } else {
483
                $actionProts_ok[] = _MA_XNEWSLETTER_SUBSCRIPTION_REG_OK;
484
            }
485
            $subscr_id = $subscrObj->getVar('subscr_id');
486
            // create $code_selections string
487
            $catCriteria = new \CriteriaCompo();
488
            $catCriteria->setSort('cat_id');
489
            $catCriteria->setOrder('ASC');
490
            $catObjs    = $helper->getHandler('Cat')->getAll($catCriteria);
491
            $selections = [];
492 View Code Duplication
            foreach ($catObjs as $cat_id => $catObj) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
493
                // create selections: $cat_id-$cat_selected-$old_catsubcr_id-$old_catsubscr_quited
494
                $selection      = [];
495
                $selection[0]   = $cat_id;
496
                $selection[1]   = in_array($cat_id, $_REQUEST['cats']) ? '1' : '0'; //isset($_REQUEST["cats_{$cat_id}"]);
497
                $selection[2]   = Request::getInt("existing_catsubcr_id_{$cat_id}", 0);
498
                $selection[3]   = Request::getInt("existing_catsubscr_quited_{$cat_id}", 0);
499
                $code_selection = implode('-', $selection);
500
                $selections[]   = $code_selection;
501
                unset($selection);
502
            }
503
            $code_selections = implode('|', $selections);
504
        }
505
506
        if (!$allowedWithoutActivationKey) {
507
            // 2nd case: subscribe WITH confirmation
508
            if ('' == $activationKey) {
509
                // activation key DOESN'T EXIST
510
                // create and send confirmation email
511
                // check form
512
                if (!$GLOBALS['xoopsSecurity']->check()) {
513
                    redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
514
                }
515
                // check email
516
                if ('' == $subscr_email || !xnewsletter_checkEmail($subscr_email)) {
517
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOEMAIL);
518
                }
519
                // get subscr fields from form
520
                $subscr_firstname = Request::getString('subscr_firstname', '');
521
                $subscr_lastname  = Request::getString('subscr_lastname', '');
522
                $subscr_sex       = Request::getString('subscr_sex', '');
523
                // create $code_selections string
524
                $catCriteria = new \CriteriaCompo();
525
                $catCriteria->setSort('cat_id');
526
                $catCriteria->setOrder('ASC');
527
                $catObjs    = $helper->getHandler('Cat')->getAll($catCriteria);
528
                $selections = [];
529 View Code Duplication
                foreach ($catObjs as $cat_id => $catObj) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
530
                    // create selections: $cat_id-$cat_selected-$old_catsubcr_id-$old_catsubscr_quited
531
                    $selection      = [];
532
                    $selection[0]   = $cat_id;
533
                    $selection[1]   = in_array($cat_id, Request::getArray('cats')) ? '1' : '0'; //isset($_REQUEST["cats_{$cat_id}"]);
534
                    $selection[2]   = Request::getInt("existing_catsubcr_id_{$cat_id}", 0);
535
                    $selection[3]   = Request::getInt("existing_catsubscr_quited_{$cat_id}", 0);
536
                    $code_selection = implode('-', $selection);
537
                    $selections[]   = $code_selection;
538
                    unset($selection);
539
                }
540
                $code_selections = implode('|', $selections); // string
541
                //
542
                // get or create subscr
543 View Code Duplication
                if ($subscr_id > 0) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
544
                    $subscrObj = $helper->getHandler('Subscr')->get($subscr_id);
545
                    $saveType  = 'update';
546
                } else {
547
                    $subscrObj = $helper->getHandler('Subscr')->create();
548
                    $saveType  = 'addnew';
549
                }
550
                // fill subscr
551
                if ($subscr_id <= 0) {
552
                    // form subscr_email
553
                    $subscrObj->setVar('subscr_email', $subscr_email);
554
                    // form subscr_uid
555
                    $subscr_uid = 0;
556
                    $sql        = "SELECT `uid` FROM {$xoopsDB->prefix('users')}";
557
                    $sql        .= " WHERE (`email`='{$subscr_email}')";
558
                    $sql        .= ' LIMIT 1';
559
                    $user       = $xoopsDB->query($sql);
560
                    if ($user) {
561
                        $row_user   = $xoopsDB->fetchRow($user);
562
                        $subscr_uid = $row_user[0];
563
                    }
564
                    $subscrObj->setVar('subscr_uid', $subscr_uid);
565
                    // form subscr_submitter
566
                    $subscrObj->setVar('subscr_submitter', $uid);
567
                }
568
569
                $subscrObj->setVar('subscr_created', time());
570
                $subscrObj->setVar('subscr_ip', xoops_getenv('REMOTE_ADDR'));
571
                $subscr_actkey = ('' === Request::getString('subscr_actkey', '')) ? xoops_makepass() : Request::getString('subscr_actkey', '');
572
                $subscrObj->setVar('subscr_actkey', $subscr_actkey);
573
                // format subscr_actoptions: selected_newsletters||firstname||lastname||sex
574
                $activationOptions = [
575
                    'code_selections'  => $code_selections,
576
                    'subscr_firstname' => $subscr_firstname,
577
                    'subscr_lastname'  => $subscr_lastname,
578
                    'subscr_sex'       => $subscr_sex,
579
                    'subscr_created'   => $subscrObj->getVar('subscr_created'),
580
                    'subscr_ip'        => $subscrObj->getVar('subscr_ip'),
581
                ];
582
                $subscrObj->setVar('subscr_actoptions', $activationOptions); // XOBJ_DTYPE_ARRAY
583
                // insert subscr
584 View Code Duplication
                if (!$helper->getHandler('Subscr')->insert($subscrObj)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
585
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SAVESUBSCR . '<br>' . $subscrObj->getHtmlErrors());
586
                }
587
                if ($subscr_id > 0) {
588
                    $actionProts_ok[] = _MA_XNEWSLETTER_SUBSCRIPTION_UPDATE_OK;
589
                } else {
590
                    $actionProts_ok[] = _MA_XNEWSLETTER_SUBSCRIPTION_REG_OK;
591
                }
592
                $subscr_id = $subscrObj->getVar('subscr_id');
593
                // send the email with the confirmation code
594
                $xoopsMailer = xoops_getMailer();
595
                $xoopsMailer->reset();
596
                $xoopsMailer->setTemplateDir();
597
                $xoopsMailer->useMail();
598
                $xoopsMailer->setTemplate(('update' === $saveType) ? 'update.tpl' : 'activate.tpl');
599
                $xoopsMailer->setToEmails($subscr_email);
600
                if (isset($xoopsConfig['adminmail'])) {
601
                    $xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
602
                }
603
                if (isset($xoopsConfig['sitename'])) {
604
                    $xoopsMailer->setFromName($xoopsConfig['sitename']);
605
                }
606
                $xoopsMailer->assign('EMAIL', $subscr_email);
607
                $xoopsMailer->assign('SEX', '' != $subscrObj->getVar('subscr_sex') ? $subscrObj->getVar('subscr_sex') : $subscr_sex);
608
                $xoopsMailer->assign('FIRSTNAME', '' != $subscrObj->getVar('subscr_firstname') ? $subscrObj->getVar('subscr_firstname') : $subscr_firstname);
609
                $xoopsMailer->assign('LASTNAME', '' != $subscrObj->getVar('subscr_lastname') ? $subscrObj->getVar('subscr_lastname') : $subscr_lastname);
610
                $xoopsMailer->assign('IP', xoops_getenv('REMOTE_ADDR'));
611
                $act           = [
612
                    XOOPS_URL,
613
                    $saveType,
614
                    $subscr_id,
615
                    $subscr_actkey,
616
                    $subscr_email,
617
                ];
618
                $activationKey = base64_encode(implode('||', $act));
619
                $xoopsMailer->assign('ACTLINK', XOOPS_URL . "/modules/xnewsletter/{$currentFile}?actkey={$activationKey}");
620
                $xoopsMailer->setSubject(_MA_XNEWSLETTER_SUBSCRIPTIONSUBJECT . $GLOBALS['xoopsConfig']['sitename']);
621
                if (!$xoopsMailer->send()) {
622
                    $actionProts_error[] = _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SENDACTKEY . '<br>' . $xoopsMailer->getErrors();
623
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SENDACTKEY . '<br>' . $xoopsMailer->getErrors());
624
                } else {
625
                    $actionProts_ok[] = str_replace('%subscr_email', $subscr_email, _MA_XNEWSLETTER_SENDMAIL_REG_OK);
626
                }
627
            } else {
628
                // activation key EXISTS
629
                // check confirmation email
630
                // check activation key
631
                $activationKey_array  = explode('||', base64_decode($activationKey, true));
632
                $activationKeyIsValid = false;
633
                if ((XOOPS_URL == $activationKey_array[0]) && ('' != trim($activationKey_array[1]))
634
                    && ((int)$activationKey_array[2] > 0)
635
                    && ('' != trim($activationKey_array[3]))) {
636
                    $activationKeyIsValid = true;
637
                } else {
638
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_INVALIDKEY);
639
                }
640
                $saveType      = trim($activationKey_array[1]);
641
                $subscr_id     = (int)$activationKey_array[2];
642
                $subscr_actkey = trim($activationKey_array[3]);
643
                // check given data with table subscr
644
                $subscrCriteria = new \CriteriaCompo();
645
                $subscrCriteria->add(new \Criteria('subscr_id', $subscr_id));
646
                $subscrCriteria->add(new \Criteria('subscr_actkey', $subscr_actkey));
647
                $subscrCriteria->setLimit(1);
648
                $subscrCount = $helper->getHandler('Subscr')->getCount($subscrCriteria);
649
                if (0 == $subscrCount) {
650
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NODATAKEY);
651
                }
652
                // get subscr
653
                $subscrObj         = $helper->getHandler('Subscr')->get($subscr_id);
654
                $activationOptions = $subscrObj->getVar('subscr_actoptions'); // XOBJ_DTYPE_ARRAY
655
                // check time: confirmation not later than ... hours
656
                if (('update' !== $saveType) && (0 != $helper->getConfig('confirmation_time'))
657
                    && ((int)$activationOptions['subscr_created'] < time() - (3600 + (int)$helper->getConfig('confirmation_time')))) {
658
                    // time expired
659
                    $subscrObj->setVar('subscr_actoptions', []);
660
                    $helper->getHandler('Subscr')->insert($subscrObj);
661
                    // IN PROGRESS
662
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_KEYEXPIRED);
663
                }
664
                if ('update' === $saveType) {
665
                    // get subscr fields from form
666
                    $subscr_firstname = Request::getString('subscr_firstname', '');
667
                    $subscr_lastname  = Request::getString('subscr_lastname', '');
668
                    $subscr_sex       = Request::getString('subscr_sex', '');
669
                    // create $code_selections string
670
                    $catCriteria = new \CriteriaCompo();
671
                    $catCriteria->setSort('cat_id');
672
                    $catCriteria->setOrder('ASC');
673
                    $catObjs    = $helper->getHandler('Cat')->getAll($catCriteria);
674
                    $selections = [];
675 View Code Duplication
                    foreach ($catObjs as $cat_id => $catObj) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
676
                        // create selections: $cat_id-$cat_selected-$old_catsubcr_id-$old_catsubscr_quited
677
                        $selection      = [];
678
                        $selection[0]   = $cat_id;
679
                        $selection[1]   = in_array($cat_id, Request::getArray('cats')) ? '1' : '0'; //isset($_REQUEST["cats_{$cat_id}"]);
680
                        $selection[2]   = Request::getInt("existing_catsubcr_id_{$cat_id}", 0);
681
                        $selection[3]   = Request::getInt("existing_catsubscr_quited_{$cat_id}", 0);
682
                        $code_selection = implode('-', $selection);
683
                        $selections[]   = $code_selection;
684
                        unset($selection);
685
                    }
686
                    $code_selections = implode('|', $selections); // string
687
                } else {
688
                    // get subscr fields from subscr_actoptions
689
                    $subscr_sex       = $activationOptions['subscr_sex'];
690
                    $subscr_firstname = $activationOptions['subscr_firstname'];
691
                    $subscr_lastname  = $activationOptions['subscr_lastname'];
692
                    $code_selections = $activationOptions['code_selections']; // string
693
                }
694
                // insert subscr
695
                $subscrObj->setVar('subscr_sex', $subscr_sex);
696
                $subscrObj->setVar('subscr_firstname', $subscr_firstname);
697
                $subscrObj->setVar('subscr_lastname', $subscr_lastname);
698
                if (!$helper->getHandler('Subscr')->insert($subscrObj)) {
699
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SAVESUBSCR);
700
                }
701
702
            }
703
        }
704
        //
705
        //
706
        // subscribe subscr to cat (create/update catsubscr)
707
        if ($activationKeyIsValid || $allowedWithoutActivationKey) {
708
            // update xnewsletter_subscr
709
            $subscrObj = $helper->getHandler('Subscr')->get($subscr_id);
710
            if (0 == $subscrObj->getVar('subscr_activated')) {
711
                $subscrObj->setVar('subscr_created', time());
712
                $subscrObj->setVar('subscr_ip', xoops_getenv('REMOTE_ADDR'));
713
                $subscrObj->setVar('subscr_activated', 1);
714
            }
715
            // reset act fields
716
            $subscrObj->setVar('subscr_actoptions', []);
717
            // insert subscr
718
            if (!$helper->getHandler('Subscr')->insert($subscrObj)) {
719
                redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SAVESUBSCR);
720
            }
721
            // create cat subscr
722
            $selections = explode('|', $code_selections); // array
723
            foreach ($selections as $code_selection) {
724
                if ('' == $code_selection) {
725
                    $code_selection = '0-0-0-0';
726
                }
727
                $selection           = explode('-', $code_selection); // array
728
                $cat_id              = $selection[0];
729
                $catsubcr            = $selection[1];
730
                $catsubcr_id_old     = (int)$selection[2];
731
                $catsubcr_quited_old = (int)$selection[3];
732
                $catObj              = $helper->getHandler('Cat')->get($cat_id);
733
                $cat_mailinglist     = $catObj->getVar('cat_mailinglist');
734
                $cat_name            = $catObj->getVar('cat_name');
735
                if ('1' == $catsubcr && 0 == $catsubcr_id_old) {
736
                    $catsubscrObj = $helper->getHandler('Catsubscr')->create();
737
                    $catsubscrObj->setVar('catsubscr_catid', $cat_id);
738
                    $catsubscrObj->setVar('catsubscr_subscrid', $subscr_id);
739
                    $catsubscrObj->setVar('catsubscr_submitter', $uid);
740
                    $catsubscrObj->setVar('catsubscr_created', time());
741
                    if ($helper->getHandler('Catsubscr')->insert($catsubscrObj)) {
742
                        $count_ok++;
743
                        if ($catsubcr_id_old > 0) {
744
                            $actionProts_ok[] = str_replace('%nl', $cat_name, _MA_XNEWSLETTER_SUBSCRIPTION_PROT_NO_CHANGE);
745
                        } else {
746
                            $actionProts_ok[] = str_replace('%nl', $cat_name, _MA_XNEWSLETTER_SUBSCRIPTION_PROT_SUBSCRIBE);
747
                        }
748
                        // handle mailinglists
749
                        if ($cat_mailinglist > 0) {
750
                            require_once XOOPS_ROOT_PATH . '/modules/xnewsletter/include/mailinglist.php';
751
                            subscribingMLHandler(_XNEWSLETTER_MAILINGLIST_SUBSCRIBE, $subscr_id, $cat_mailinglist);
752
                        }
753
                    } else {
754
                        $count_err++;
755
                        $actionProts_error[] = _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SAVECATSUBSCR; //$catsubscrObj->getHtmlErrors();
756
                    }
757
                } elseif ('0' == $catsubcr && $catsubcr_id_old > 0) {
758
                    // unsubscribe / delete old subscription
759
                    $catsubscrObj = $helper->getHandler('Catsubscr')->get($catsubcr_id_old);
760 View Code Duplication
                    if ($helper->getHandler('Catsubscr')->delete($catsubscrObj, true)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
761
                        // handle mailinglists
762
                        if ($cat_mailinglist > 0) {
763
                            require_once XOOPS_ROOT_PATH . '/modules/xnewsletter/include/mailinglist.php';
764
                            subscribingMLHandler(_XNEWSLETTER_MAILINGLIST_UNSUBSCRIBE, $subscr_id, $cat_mailinglist);
765
                        }
766
                    } else {
767
                        $count_err++;
768
                        $actionProts_error[] = _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SAVECATSUBSCR; //$catsubscrObj->getHtmlErrors();
769
                    }
770
                    /*
771
                                        if ($count_err > 0) {
772
                                            redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELNOTOK);
773
                                        }
774
                    */
775
                    $actionProts_ok[] = str_replace('%nl', $cat_name, _MA_XNEWSLETTER_SUBSCRIPTION_PROT_UNSUBSCRIBE);
776
                } elseif ($catsubcr_id_old > 0 && $catsubcr_quited_old > 0) {
777
                    // newsletter stay selected, but catsubscr_quited will be removed
778
                    $catsubscrObj = $helper->getHandler('Catsubscr')->get($catsubcr_id_old);
779
                    // Form catsubscr_quited
780
                    $catsubscrObj->setVar('catsubscr_quited', '0');
781
                    if ($helper->getHandler('Catsubscr')->insert($catsubscrObj)) {
782
                        $count_ok++;
783
                        $actionProts_ok[] = str_replace('%nl', $cat_name, _MA_XNEWSLETTER_SUBSCRIPTION_PROT_DAT_QUITED_REMOVED);
784
                    } else {
785
                        $count_err++;
786
                        $actionProts_error[] = _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SAVECATSUBSCR; //$catsubscrObj->getHtmlErrors();
787
                    }
788
                } elseif ($catsubcr_id_old > 0) {
789
                    // newsletter still subscribed
790
                    $actionProts_ok[] = str_replace('%nl', $cat_name, _MA_XNEWSLETTER_SUBSCRIPTION_PROT_NO_CHANGE);
791
                }
792
                // nothing to do
793
            }
794
            //
795
            // send infomail to subscriber if current user (submitter) is not the subscriber (subscr)
796
            if (isset($submitter_email) && ('' != $submitter_email) && ($submitter_email != $subscr_email)) {
797
                if ('' == $subscr_sex && '' == $subscr_firstname && '' == $subscr_lastname) {
798
                    $subscrObj        = $helper->getHandler('Subscr')->get($subscr_id);
799
                    $subscr_sex       = $subscrObj->getVar('subscr_sex');
800
                    $subscr_firstname = $subscrObj->getVar('subscr_firstname');
801
                    $subscr_lastname  = $subscrObj->getVar('subscr_lastname');
802
                    $subscr_actkey    = $subscrObj->getVar('subscr_actkey');
803
                }
804
                // send the email with the confirmation code
805
                $xoopsMailer = xoops_getMailer();
806
                $xoopsMailer->reset();
807
                $xoopsMailer->setTemplateDir();
808
                $xoopsMailer->useMail();
809
                $xoopsMailer->setHTML();
810
                $xoopsMailer->setTemplate('info_change.tpl');
811
                $xoopsMailer->setToEmails($subscr_email);
812
                if (isset($xoopsConfig['adminmail'])) {
813
                    $xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
814
                }
815
                if (isset($xoopsConfig['sitename'])) {
816
                    $xoopsMailer->setFromName($xoopsConfig['sitename']);
817
                }
818
                $xoopsMailer->assign('EMAIL', $subscr_email);
819
                $xoopsMailer->assign('SEX', $subscr_sex);
820
                $xoopsMailer->assign('FIRSTNAME', $subscr_firstname);
821
                $xoopsMailer->assign('LASTNAME', $subscr_lastname);
822
                $xoopsMailer->assign('IP', xoops_getenv('REMOTE_ADDR'));
823
                $act           = [
824
                    XOOPS_URL,
825
                    'list',
826
                    $subscr_id,
827
                    $subscr_actkey,
828
                    $subscr_email,
829
                ];
830
                $activationKey = base64_encode(implode('||', $act));
831
                $xoopsMailer->assign('ACTLINK', XOOPS_URL . "/modules/xnewsletter/{$currentFile}?op=anonlistsubscr&subscr_email={$subscr_email}&actkey={$activationKey}");
832
                $xoopsMailer->assign('USERLINK', XOOPS_URL . '/userinfo.php?uid=' . $xoopsUser->uid());
833
                $username = $xoopsUser->name() == '' ? $xoopsUser->uname() : $xoopsUser->name();
834
                $xoopsMailer->assign('USERNAME', $username);
835
                $subject = _MA_XNEWSLETTER_SUBSCRIPTION_SUBJECT_CHANGE . $GLOBALS['xoopsConfig']['sitename'];
836
                $xoopsMailer->setSubject($subject);
837 View Code Duplication
                if (!$xoopsMailer->send()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
838
                    $count_err++;
839
                    $actionProts_error[] = _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SENDACTKEY . '<br>Error:' . $xoopsMailer->getErrors();
840
                    //redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SENDACTKEY . '<br>' . $xoopsMailer->getErrors());
841
                } else {
842
                    $actionProts_ok[] = str_replace('%e', $subscr_email, _MA_XNEWSLETTER_SUBSCRIPTION_PROT_SENT_INFO);
843
                }
844
            }
845
            if (0 == $count_err) {
846
                if ('addnew' === $saveType) {
847
                    $actionProts_ok[] = _MA_XNEWSLETTER_SUBSCRIPTION_REG_CLOSED;
848
                } else {
849
                    $actionProts_ok[] = _MA_XNEWSLETTER_SUBSCRIPTION_REG_UPDATE_CLOSED;
850
                }
851
                //$actionProts_ok[] = _MA_XNEWSLETTER_SUBSCRIPTION_OK;
852
            }
853
        }
854
855
        $xoopsTpl->assign('actionProts_ok', $actionProts_ok);
856
        $xoopsTpl->assign('actionProts_warning', $actionProts_warning);
857
        $xoopsTpl->assign('actionProts_error', $actionProts_error);
858
        break;
859
860
    case 'delete_subscription':
861
		if ((!$activationKey && $subscr_id <= 0) && ('1' != $_SESSION['unsub'])) {
862
			redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOID);
863
        }
864
865
        if ('1' == $_SESSION['unsub']) {
866
            $subscrCriteria = new \CriteriaCompo();
867
            $subscrCriteria->add(new \Criteria('subscr_email', $subscr_email));
868
            $subscrCriteria->setLimit(1);
869
            $subscrCount = $helper->getHandler('Subscr')->getCount($subscrCriteria);
870
            if (0 == $subscrCount) {
871
				redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR);
872
            }
873
            if ($subscr_id <= 0) {
874
                $subscrObjs = $helper->getHandler('Subscr')->getAll($subscrCriteria);
875
                foreach ($subscrObjs as $subscrObj) {
876
                    $subscr_id = $subscrObj->getVar('subscr_id');
877
                }
878
            }
879
        }
880
881
        if (Request::getBool('ok', false, 'POST') || '' != $activationKey) {
882
            $GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_subscription_result.tpl';
883
            require_once XOOPS_ROOT_PATH . '/header.php';
884
885
            $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
886
            $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
887
            $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
888
            // breadcrumb
889
            $breadcrumb = new Xnewsletter\Breadcrumb();
890
            $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
891
            $breadcrumb->addLink(_MD_XNEWSLETTER_SUBSCRIBE, XNEWSLETTER_URL . '/subscription.php?op=list_subscriptions');
892
            $breadcrumb->addLink(_MD_XNEWSLETTER_SUBSCRIPTION_DELETE, '');
893
            $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
894
            // init vars
895
            $actionProts_ok       = [];
896
            $actionProts_warning  = [];
897
            $actionProts_error    = [];
898
            $count_ok             = 0;
899
            $count_err            = 0;
900
            $activationKeyIsValid = false;
901
            // check right to unsubscribe directly
902
            $allowedWithoutActivationKey = false;
903 View Code Duplication
            if ($uid > 0) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
904
                // if not anonymous subscriber / subscriber is a Xoops user
905
                $submitter_email = $xoopsUser->email();
906
                foreach ($xoopsUser->getGroups() as $group) {
907
                    if (in_array($group, $helper->getConfig('xn_groups_without_actkey'))
908
                        || XOOPS_GROUP_ADMIN == $group) {
909
                        $allowedWithoutActivationKey = true;
910
                        break;
911
                    }
912
                }
913
            }
914
            // if anonymous subscriber
915
            // NOP
916
917
            if ('' != $activationKey || $allowedWithoutActivationKey) {
918
                // 1st case: unsubscribe WITHOUT confirmation
919
                // 2nd case: unsubscribe WITH confirmation & activation key EXISTS
920
                // check given data with table subscr
921
                $subscrCriteria = new \CriteriaCompo();
922
                $subscrCriteria->add(new \Criteria('subscr_email', $subscr_email));
923
                $subscrCriteria->add(new \Criteria('subscr_id', $subscr_id));
924
                // got actkey or user is allowed to delete without actkey
925
                if ('' != $activationKey) {
926
                    // check activation key
927
                    $activationKey_array  = explode('||', base64_decode($activationKey, true));
928
                    $activationKeyIsValid = false;
929
                    $subscr_id            = (int)$activationKey_array[2];
930
                    $subscr_actkey        = trim($activationKey_array[3]);
931
                    $subscr_email         = trim($activationKey_array[4]);
932
                    if ((XOOPS_URL == $activationKey_array[0]) && ((int)$activationKey_array[2] > 0)
933
                        && ('' != trim($activationKey_array[3]))) {
934
                        $activationKeyIsValid = true;
935
                    } else {
936
                        redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_INVALIDKEY);
937
                    }
938
                    $subscrCriteria->add(new \Criteria('subscr_actkey', $subscr_actkey));
939
                }
940
                $subscrCriteria->setLimit(1);
941
                $subscrCount = $helper->getHandler('Subscr')->getCount($subscrCriteria);
942
                if (0 == $subscrCount) {
943
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR);
944
                }
945
                // delete subscriptions (catsubscrs)
946
                $catsubscrCriteria = new \CriteriaCompo();
947
                $catsubscrCriteria->add(new \Criteria('catsubscr_subscrid', $subscr_id));
948
                $catsubscrCriteria->setSort('catsubscr_id');
949
                $catsubscrCriteria->setOrder('ASC');
950
                $catsubscrObjs  = $helper->getHandler('Catsubscr')->getAll($catsubscrCriteria);
951
                foreach ($catsubscrObjs as $catsubscr_id => $catsubscrObj) {
952
                    if ($helper->getHandler('Catsubscr')->delete($catsubscrObj, true)) {
953
                        // handle mailinglists
954
                        $catObj              = $helper->getHandler('Cat')->get($catsubscrObj->getVar('catsubscr_catid'));
955
                        $cat_mailinglist     = $catObj->getVar('cat_mailinglist');
956
                        if ($cat_mailinglist > 0) {
957
                            require_once XOOPS_ROOT_PATH . '/modules/xnewsletter/include/mailinglist.php';
958
                            subscribingMLHandler(_XNEWSLETTER_MAILINGLIST_UNSUBSCRIBE, $subscr_id, $cat_mailinglist);
959
                        }
960
                    } else {
961
                        $actionProts_error[] = $catsubscrObj->getHtmlErrors();
962
                        redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR . $subscrObj->getHtmlErrors());
963
                    }
964
                }
965
                // delete subscriber (subscr)
966
                $subscrObj = $helper->getHandler('Subscr')->get($subscr_id);
967
                if (!$helper->getHandler('Subscr')->delete($subscrObj, true)) {
968
                    $actionProts_error[] = $subscrObj->getHtmlErrors();
969
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR . $subscrObj->getHtmlErrors());
970
                }
971
972
                if (0 == $count_err) {
973
                    $actionProts_ok[] = _AM_XNEWSLETTER_FORMDELOK;
974
                } else {
975
                    $xoopsTpl->assign('actionProts_error', $actionProts_error);
976
                }
977
            } else {
978
                // 2nd case: unsubscribe WITH confirmation & activation key DOESN'T EXIST
979
                // check form
980
                if (!$GLOBALS['xoopsSecurity']->check()) {
981
                    redirect_header('subscr.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
982
                }
983
                $subscrObj     = $helper->getHandler('Subscr')->get($subscr_id);
984
                $subscr_actkey = xoops_makepass();
985
                $subscrObj->setVar('subscr_actkey', $subscr_actkey);
986
                // insert subscr
987
                if (!$helper->getHandler('Subscr')->insert($subscrObj)) {
988
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR);
989
                }
990
                //                if (!$allowedWithoutActivationKey) {
991
                $xoopsMailer = xoops_getMailer();
992
                $xoopsMailer->reset();
993
                $xoopsMailer->setTemplateDir();
994
                $xoopsMailer->useMail();
995
                $xoopsMailer->setTemplate('delete.tpl');
996
                $xoopsMailer->setToEmails($subscrObj->getVar('subscr_email'));
997
                if (isset($xoopsConfig['adminmail'])) {
998
                    $xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
999
                }
1000
                if (isset($xoopsConfig['sitename'])) {
1001
                    $xoopsMailer->setFromName($xoopsConfig['sitename']);
1002
                }
1003
                $xoopsMailer->assign('EMAIL', $subscrObj->getVar('subscr_email'));
1004
                $xoopsMailer->assign('SEX', $subscrObj->getVar('subscr_sex'));
1005
                $xoopsMailer->assign('FIRSTNAME', $subscrObj->getVar('subscr_firstname'));
1006
                $xoopsMailer->assign('LASTNAME', $subscrObj->getVar('subscr_lastname'));
1007
                $xoopsMailer->assign('IP', xoops_getenv('REMOTE_ADDR'));
1008
                $act           = [
1009
                    XOOPS_URL,
1010
                   'delete',
1011
                    $subscrObj->getVar('subscr_id'),
1012
                    $subscrObj->getVar('subscr_actkey'),
1013
                    $subscrObj->getVar('subscr_email'),
1014
                ];
1015
                $activationKey = base64_encode(implode('||', $act));
1016
                $xoopsMailer->assign('ACTLINK', XOOPS_URL . "/modules/xnewsletter/{$currentFile}?op=unsub&email={$subscrObj->getVar('subscr_email')}&actkey={$activationKey}");
1017
                $xoopsMailer->setSubject(_MA_XNEWSLETTER_DELETESUBJECT . $GLOBALS['xoopsConfig']['sitename']);
1018 View Code Duplication
                if (!$xoopsMailer->send()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1019
                    $count_err++;
1020
                    $actionProts_error[] = _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SENDACTKEY . '<br>' . $xoopsMailer->getErrors();
1021
                    redirect_header($currentFile, 3, _MA_XNEWSLETTER_SUBSCRIPTION_ERROR_SENDACTKEY . '<br>' . $xoopsMailer->getErrors());
1022
                }
1023
                //                }
1024
                if (0 == $count_err) {
1025
                    $actionProts_ok[] = str_replace('%subscr_email', $subscrObj->getVar('subscr_email'), _MA_XNEWSLETTER_SENDMAIL_UNREG_OK);
1026
                }
1027
            }
1028
1029
            $xoopsTpl->assign('actionProts_ok', $actionProts_ok);
1030
            $xoopsTpl->assign('actionProts_warning', $actionProts_warning);
1031
            $xoopsTpl->assign('actionProts_error', $actionProts_error);
1032
        } else {
1033
			$GLOBALS['xoopsOption']['template_main'] = 'xnewsletter_empty.tpl';
1034
            require_once XOOPS_ROOT_PATH . '/header.php';
1035
1036
            $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
1037
            $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
1038
            $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
1039
            // breadcrumb
1040
            $breadcrumb = new Xnewsletter\Breadcrumb();
1041
            $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
1042
            $breadcrumb->addLink(_MD_XNEWSLETTER_SUBSCRIBE, XNEWSLETTER_URL . '/subscription.php?op=list_subscriptions');
1043
            $breadcrumb->addLink(_MD_XNEWSLETTER_SUBSCRIPTION_DELETE, '');
1044
            $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
1045
1046
            $subscrObj = $helper->getHandler('Subscr')->get($subscr_id);
1047
            xoops_confirm([
1048
                              'ok'           => true,
1049
                              'subscr_id'    => $subscr_id,
1050
                              'subscr_email' => $subscr_email,
1051
                              'op'           => 'delete_subscription',
1052
                          ], $currentFile, sprintf(_MA_XNEWSLETTER_SUBSCRIPTION_DELETE_SURE));
1053
        }
1054
        break;
1055
}
1056
1057
require_once __DIR__ . '/footer.php';
1058