Completed
Pull Request — master (#27)
by Michael
01:42
created

admin/accounts.php (2 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 Xmf\Request;
29
30
$currentFile = basename(__FILE__);
31
require_once __DIR__ . '/admin_header.php';
32
xoops_cp_header();
33
34
// We recovered the value of the argument op in the URL$
35
$op             = \Xmf\Request::getString('op', 'list');
36
$save_and_check = \Xmf\Request::getString('save_and_check', 'none');
37
$accounts_id    = \Xmf\Request::getInt('accounts_id', 0);
38
$post           = \Xmf\Request::getString('post', '');
39
40
if ('' == $post && 'save_accounts' === $op && 'none' === $save_and_check) {
41
    $op = 'edit_account';
42
}
43
44
switch ($op) {
45
    case 'check_account':
46
        $adminObject->displayNavigation($currentFile);
47
        $adminObject->addItemButton(_AM_XNEWSLETTER_ACCOUNTSLIST, '?op=list', 'list');
48
        $adminObject->displayButton('left');
49
50
        if (0 == $accounts_id) {
51
            redirect_header($currentFile, 3, _AM_XNEWSLETTER_ERROR_NO_VALID_ID);
52
        } else {
53
            $accountObj = $helper->getHandler('Accounts')->get($accounts_id);
54
        }
55
56
        $mailhost = $accountObj->getVar('accounts_server_in');
57
        $port     = $accountObj->getVar('accounts_port_in');
58
        switch ($accountObj->getVar('accounts_type')) {
59
            case _XNEWSLETTER_ACCOUNTS_TYPE_VAL_POP3:
60
                $service = 'pop3';
61
                break;
62
            case _XNEWSLETTER_ACCOUNTS_TYPE_VAL_SMTP:
63
            case _XNEWSLETTER_ACCOUNTS_TYPE_VAL_GMAIL:
64
                $service = 'imap';
65
                break;
66
            case 'default':
67
            default:
68
                $service = '';
69
                break;
70
        }
71
        $service_option      = $accountObj->getVar('accounts_securetype_in');
72
        $accounts_password   = $accountObj->getVar('accounts_password');
73
        $accounts_username   = $accountObj->getVar('accounts_username');
74
        $accounts_inbox      = $accountObj->getVar('accounts_inbox');
75
        $accounts_inbox_ok   = false;
76
        $accounts_hardbox    = $accountObj->getVar('accounts_hardbox');
77
        $accounts_hardbox_ok = false;
78
        $accounts_softbox    = $accountObj->getVar('accounts_softbox');
79
        $accounts_softbox_ok = false;
80
81
        $command = $mailhost . ':' . $port;
82
        if ('' != $service) {
83
            $command .= '/' . $service;
84
        }
85
        if ('' != $service_option) {
86
            $command .= '/' . $service_option;
87
        }
88
89
        echo "<table class='outer width100' cellspacing='1'>";
90
        echo '<tr>';
91
        echo '    <th></th>';
92
        echo '    <th>' . _AM_XNEWSLETTER_ACCOUNTS_CHECK . '</th>';
93
        echo '    <th>' . _AM_XNEWSLETTER_ACCOUNTS_CHECK_INFO . '</th>';
94
        echo '</tr>';
95
96
        $mbox = @imap_open('{' . $command . '}', $accounts_username, $accounts_password); // || die ("can't connect: " . imap_last_error());
97
        if (false === $mbox) {
98
            echo '<tr>';
99
            echo '<td>' . XNEWSLETTER_IMG_FAILED . '</td>';
100
            echo '<td>' . _AM_XNEWSLETTER_ACCOUNTS_CHECK_OPEN_MAILBOX . _AM_XNEWSLETTER_ACCOUNTS_CHECK_FAILED . '</td>';
101
            echo '<td>' . imap_last_error() . '</td>';
102
            echo '</tr>';
103
        } else {
104
            echo '<tr>';
105
            echo '<td>' . XNEWSLETTER_IMG_OK . '</td>';
106
            echo '<td>' . _AM_XNEWSLETTER_ACCOUNTS_CHECK_OPEN_MAILBOX . _AM_XNEWSLETTER_ACCOUNTS_CHECK_OK . '</td>';
107
            echo '<td></td>';
108
            echo '</tr>';
109
110
            $folders = imap_list($mbox, '{' . $command . '}', '*');
111
            if (false === $folders) {
112
                echo '<tr>';
113
                echo '<td>' . XNEWSLETTER_IMG_FAILED . '</td>';
114
                echo '<td>' . _AM_XNEWSLETTER_ACCOUNTS_CHECK_OPEN_FOLDERS . _AM_XNEWSLETTER_ACCOUNTS_CHECK_FAILED . '</td>';
115
                echo '<td>' . imap_last_error() . '</td>';
116
                echo '</tr>';
117
            } else {
118
                echo '<tr>';
119
                echo '<td>' . XNEWSLETTER_IMG_OK . '</td>';
120
                echo '<td>' . _AM_XNEWSLETTER_ACCOUNTS_CHECK_OPEN_FOLDERS . _AM_XNEWSLETTER_ACCOUNTS_CHECK_OK . '</td>';
121
                echo '<td>';
122
                if (is_array($folders)) {
123
                    //                    reset($folders);
124
                    sort($folders);
125
                    $foldercreated = 0;
126
                    //                    while (list($key, $val) = each($folders)) {
127
                    foreach ($folders as $key => $val) {
128
                        echo "($key) ";
129
                        echo imap_utf7_decode(str_replace('{' . $command . '}', '', $val)) . "<br>\n";
130
                        if ('{' . $command . '}' . $accounts_inbox == $val) {
131
                            $accounts_inbox_ok = true;
132
                        }
133 View Code Duplication
                        if ('{' . $command . '}' . $accounts_hardbox == $val) {
134
                            $accounts_hardbox_ok = true;
135
                        } else {
136
                            @imap_createmailbox($mbox, imap_utf7_encode('{' . $command . '}' . $accounts_hardbox));
137
                            $foldercreated = 1;
138
                        }
139 View Code Duplication
                        if ('{' . $command . '}' . $accounts_softbox == $val) {
140
                            $accounts_softbox_ok = true;
141
                        } else {
142
                            @imap_createmailbox($mbox, imap_utf7_encode('{' . $command . '}' . $accounts_softbox));
143
                            $foldercreated = 1;
144
                        }
145
                    }
146
                    if (1 == $foldercreated) {
147
                        $folders_recheck = imap_list($mbox, '{' . $command . '}', '*');
148
                        //                        while (list($key, $val) = each($folders_recheck)) {
149
                        foreach ($folders_recheck as $key => $val) {
150
                            if ('{' . $command . '}' . $accounts_hardbox == $val) {
151
                                $accounts_hardbox_ok = true;
152
                            }
153
                            if ('{' . $command . '}' . $accounts_softbox == $val) {
154
                                $accounts_softbox_ok = true;
155
                            }
156
                        }
157
                    }
158
                }
159
160
                echo '</td>';
161
                echo '</tr>';
162
                echo '<tr>';
163
                if ('1' == $accountObj->getVar('accounts_use_bmh')) {
164
                    if (true === $accounts_inbox_ok && true === $accounts_hardbox_ok && true === $accounts_softbox_ok) {
165
                        echo '<td>' . XNEWSLETTER_IMG_OK . '</td>';
166
                    } else {
167
                        echo '<td>' . XNEWSLETTER_IMG_FAILED . '</td>';
168
                    }
169
                    echo '<td>' . _AM_XNEWSLETTER_ACCOUNTS_CHECK_BMH . '</td>';
170
                    echo '<td>';
171
                    echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_BMH_INBOX . ' ';
172 View Code Duplication
                    if (true === $accounts_inbox_ok) {
173
                        echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_OK . XNEWSLETTER_IMG_OK;
174
                    } else {
175
                        echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_FAILED . XNEWSLETTER_IMG_FAILED;
176
                    }
177
                    echo '<br>';
178
                    echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_BMH_HARDBOX . ' ';
179 View Code Duplication
                    if (true === $accounts_hardbox_ok) {
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...
180
                        echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_OK . XNEWSLETTER_IMG_OK;
181
                    } else {
182
                        echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_FAILED . XNEWSLETTER_IMG_FAILED;
183
                    }
184
                    echo '<br>';
185
                    echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_BMH_SOFTBOX . ' ';
186 View Code Duplication
                    if (true === $accounts_softbox_ok) {
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...
187
                        echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_OK . XNEWSLETTER_IMG_OK;
188
                    } else {
189
                        echo _AM_XNEWSLETTER_ACCOUNTS_CHECK_FAILED . XNEWSLETTER_IMG_FAILED;
190
                    }
191
                    echo '<br>';
192
                    echo '</td>';
193
                } else {
194
                    echo '<td>' . XNEWSLETTER_IMG_OK . '</td>';
195
                    echo '<td>' . _AM_XNEWSLETTER_ACCOUNTS_CHECK_BMH . _AM_XNEWSLETTER_ACCOUNTS_CHECK_SKIPPED . '</td>';
196
                    echo '<td></td>';
197
                }
198
                echo '</tr>';
199
            }
200
            imap_close($mbox);
201
        }
202
        echo '</table>';
203
        break;
204
    case 'list':
205
    case 'list_accounts':
206
    default:
207
        $adminObject->displayNavigation($currentFile);
208
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWACCOUNTS, '?op=new_account', 'add');
209
        $adminObject->displayButton('left');
210
211
        $limit            = $helper->getConfig('adminperpage');
212
        $accountsCriteria = new \CriteriaCompo();
213
        $accountsCriteria->setSort('accounts_id ASC, accounts_type');
214
        $accountsCriteria->setOrder('ASC');
215
        $accountsCount = $helper->getHandler('Accounts')->getCount();
216
        $start         = \Xmf\Request::getInt('start', 0);
217
        $accountsCriteria->setStart($start);
218
        $accountsCriteria->setLimit($limit);
219
        $accountsObjs = $helper->getHandler('Accounts')->getAll($accountsCriteria);
220 View Code Duplication
        if ($accountsCount > $limit) {
221
            require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
222
            $pagenav = new \XoopsPageNav($accountsCount, $limit, $start, 'start', 'op=list');
223
            $pagenav = $pagenav->renderNav(4);
224
        } else {
225
            $pagenav = '';
226
        }
227
228
        // View Table
229
        echo "<table class='outer width100' cellspacing='1'>";
230
        echo '<tr>';
231
        echo '    <th>' . _AM_XNEWSLETTER_ACCOUNTS_ID . '</th>';
232
        echo '    <th>' . _AM_XNEWSLETTER_ACCOUNTS_TYPE . '</th>';
233
        echo '    <th>' . _AM_XNEWSLETTER_ACCOUNTS_NAME . '</th>';
234
        echo '    <th>' . _AM_XNEWSLETTER_ACCOUNTS_YOURNAME . '</th>';
235
        echo '    <th>' . _AM_XNEWSLETTER_ACCOUNTS_YOURMAIL . '</th>';
236
        echo '    <th>' . _AM_XNEWSLETTER_ACCOUNTS_DEFAULT . '</th>';
237
        echo '    <th>' . _AM_XNEWSLETTER_FORMACTION . '</th>';
238
        echo '</tr>';
239
240
        if ($accountsCount > 0) {
241
            $class = 'odd';
242
            foreach ($accountsObjs as $accounts_id => $accountsObj) {
243
                echo "<tr class='" . $class . "'>";
244
                $class = ('even' === $class) ? 'odd' : 'even';
245
                echo "<td class='center'>{$accounts_id}</td>";
246
                $accounts_types = [
247
                    _XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_MAIL     => _AM_XNEWSLETTER_ACCOUNTS_TYPE_PHPMAIL,
248
                    _XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_SENDMAIL => _AM_XNEWSLETTER_ACCOUNTS_TYPE_PHPSENDMAIL,
249
                    _XNEWSLETTER_ACCOUNTS_TYPE_VAL_POP3         => _AM_XNEWSLETTER_ACCOUNTS_TYPE_POP3,
250
                    _XNEWSLETTER_ACCOUNTS_TYPE_VAL_SMTP         => _AM_XNEWSLETTER_ACCOUNTS_TYPE_SMTP,
251
                    _XNEWSLETTER_ACCOUNTS_TYPE_VAL_GMAIL        => _AM_XNEWSLETTER_ACCOUNTS_TYPE_GMAIL,
252
                ];
253
                echo "<td>{$accounts_types[$accountsObj->getVar('accounts_type')]}</td>";
254
                echo "<td>{$accountsObj->getVar('accounts_name')}</td>";
255
                echo "<td>{$accountsObj->getVar('accounts_yourname')}</td>";
256
                echo "<td>{$accountsObj->getVar('accounts_yourmail')}</td>";
257
                $verif_accounts_default = (1 == $accountsObj->getVar('accounts_default')) ? _YES : _NO;
258
                echo "<td class='center'>{$verif_accounts_default}</td>";
259
260
                echo "<td class='center'>";
261
                echo "    <a href='?op=edit_account&accounts_id={$accounts_id}'><img src=" . XNEWSLETTER_ICONS_URL . "/xn_edit.png alt='" . _EDIT . "' title='" . _EDIT . "'></a>";
262
                echo "    <a href='?op=delete_account&accounts_id={$accounts_id}'><img src=" . XNEWSLETTER_ICONS_URL . "/xn_delete.png alt='" . _DELETE . "' title='" . _DELETE . "'></a>";
263
                if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_MAIL != $accountsObj->getVar('accounts_type')
264
                    && _XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_SENDMAIL != $accountsObj->getVar('accounts_type')) {
265
                    echo "    <a href='?op=check_account&accounts_id={$accounts_id}'><img src=" . XNEWSLETTER_ICONS_URL . "/xn_check.png alt='" . _AM_XNEWSLETTER_ACCOUNTS_TYPE_CHECK . "' title='" . _AM_XNEWSLETTER_ACCOUNTS_TYPE_CHECK . "'></a>";
266
                }
267
                echo '</td>';
268
                echo '</tr>';
269
            }
270
        }
271
        echo '</table>';
272
        echo '<br>';
273
        echo "<div>{$pagenav}</div>";
274
        echo '<br>';
275
        break;
276
    case 'new_account':
277
        $adminObject->displayNavigation($currentFile);
278
        $adminObject->addItemButton(_AM_XNEWSLETTER_ACCOUNTSLIST, '?op=list', 'list');
279
        $adminObject->displayButton('left');
280
281
        $accountObj = $helper->getHandler('Accounts')->create();
282
        $accountObj = xnewsletter_setPost($accountObj, $_POST);
283
        $form       = $accountObj->getForm();
284
        $form->display();
285
        break;
286
    case 'save_accounts':
287
        if (!$GLOBALS['xoopsSecurity']->check()) {
288
            redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
289
        }
290
291
        $accountObj           = $helper->getHandler('Accounts')->get($accounts_id);
292
        $_POST['accounts_id'] = $accounts_id;
293
        $accountObj           = xnewsletter_setPost($accountObj, $_POST);
294
295
        $accountsCriteria = new \CriteriaCompo();
296
        $accountsCriteria->add(new \Criteria('accounts_default', 1));
297
        $count_accounts_default = $helper->getHandler('Accounts')->getCount($accountsCriteria);
298
        if ($count_accounts_default > 0) {
299
            if (1 == $accountObj->getVar('accounts_default')) {
300
                $verif_accounts_default = 1;
301
                //reset old accounts_default
302
                if (!$helper->getHandler('Accounts')->updateAll('accounts_default', 0, null, false)) {
303
                    exit('MySQL-Error: ' . $GLOBALS['xoopsDB']->error());
304
                }
305
            } else {
306
                $verif_accounts_default = 0;
307
            }
308
        } else {
309
            $verif_accounts_default = 1;
310
        }
311
        $accountObj->setVar('accounts_default', $verif_accounts_default);
312
        if ('' != $accountObj->getVar('accounts_yourmail')
313
            && _AM_XNEWSLETTER_ACCOUNTS_TYPE_YOUREMAIL != $accountObj->getVar('accounts_yourmail')) {
314
            if ($helper->getHandler('Accounts')->insert($accountObj)) {
315
                if ('none' === $save_and_check) {
316
                    redirect_header('?op=list', 3, _AM_XNEWSLETTER_FORMOK);
317
                } else {
318
                    redirect_header("?op=check_account&accounts_id={$accountObj->getVar('accounts_id')}", 3, _AM_XNEWSLETTER_FORMOK);
319
                }
320
            }
321
        } else {
322
            $accountObj->setErrors(_MA_XNEWSLETTER_SUBSCRIPTION_ERROR_NOEMAIL);
323
        }
324
325
        echo $accountObj->getHtmlErrors();
326
        $form = $accountObj->getForm();
327
        $form->display();
328
        break;
329
    case 'edit_account':
330
        $adminObject->displayNavigation($currentFile);
331
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWACCOUNTS, '?op=new_account', 'add');
332
        $adminObject->addItemButton(_AM_XNEWSLETTER_ACCOUNTSLIST, '?op=list', 'list');
333
        $adminObject->displayButton('left');
334
335
        $accountObj = $helper->getHandler('Accounts')->get($accounts_id);
336
        if (!empty($_POST)) {
337
            xnewsletter_setPost($accountObj, $_POST);
338
        }
339
        $form = $accountObj->getForm();
340
        $form->display();
341
        break;
342
    case 'delete_account':
343
        $accountObj = $helper->getHandler('Accounts')->get($accounts_id);
344
        if (true === \Xmf\Request::getBool('ok', false, 'POST')) {
345
            if (!$GLOBALS['xoopsSecurity']->check()) {
346
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
347
            }
348
            if ($helper->getHandler('Accounts')->delete($accountObj)) {
349
                redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
350
            } else {
351
                echo $accountObj->getHtmlErrors();
352
            }
353
        } else {
354
            xoops_confirm(['ok' => true, 'accounts_id' => $accounts_id, 'op' => 'delete_account'], $currentFile, sprintf(_AM_XNEWSLETTER_FORMSUREDEL, $accountObj->getVar('accounts_name')));
355
        }
356
        break;
357
}
358
require_once __DIR__ . '/admin_footer.php';
359