Passed
Push — master ( 37a2f2...290aa0 )
by Michael
02:35
created

show()   C

Complexity

Conditions 14
Paths 72

Size

Total Lines 156
Code Lines 117

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 117
nc 72
nop 0
dl 0
loc 156
rs 5.0133
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
//
3
//  ------------------------------------------------------------------------ //
4
//             XF Guestbook                                                  //
5
// ------------------------------------------------------------------------- //
6
//  This program is free software; you can redistribute it and/or modify     //
7
//  it under the terms of the GNU General Public License as published by     //
8
//  the Free Software Foundation; either version 2 of the License, or        //
9
//  (at your option) any later version.                                      //
10
//                                                                           //
11
//  You may not change or alter any portion of this comment or credits       //
12
//  of supporting developers from this source code or any supporting         //
13
//  source code which is considered copyrighted (c) material of the          //
14
//  original comment or credit authors.                                      //
15
//                                                                           //
16
//  This program is distributed in the hope that it will be useful,          //
17
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
18
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
19
//  GNU General Public License for more details.                             //
20
//                                                                           //
21
//  You should have received a copy of the GNU General Public License        //
22
//  along with this program; if not, write to the Free Software              //
23
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
24
//  ------------------------------------------------------------------------ //
25
26
use XoopsModules\Xfguestbook;
27
28
require_once __DIR__ . '/admin_header.php';
29
require_once  dirname(__DIR__) . '/include/cp_functions.php';
30
31
/** @var \XoopsModules\Xfguestbook\Helper $helper */
32
$helper = \XoopsModules\Xfguestbook\Helper::getInstance();
33
34
if (null === $helper->getConfig('flagdir')) {
35
    redirect_header(XOOPS_URL . '/modules/system/admin.php?fct=modulesadmin&op=update&module=' . $xoopsModule->dirname(), 4, AM_XFGUESTBOOK_MUST_UPDATE);
36
}
37
38
if (\Xmf\Request::hasVar('op', 'GET')) {
39
    $op = $_GET['op'];
40
} elseif (\Xmf\Request::hasVar('op', 'POST')) {
41
    $op = $_POST['op'];
42
} else {
43
    $op = 'show';
44
}
45
46
if (\Xmf\Request::hasVar('msg_id', 'GET')) {
47
    $msg_id = \Xmf\Request::getInt('msg_id', 0, 'GET');
48
} else {
49
    $msg_id = \Xmf\Request::getInt('msg_id', 0, 'POST');
50
}
51
52
$msgHandler = $helper->getHandler('Message');
53
54
function delete()
55
{
56
    global $msgHandler, $xoopsModule;
57
    $msg_count = (!empty($_POST['msg_id']) && is_array($_POST['msg_id'])) ? count($_POST['msg_id']) : 0;
58
    if ($msg_count > 0) {
59
        $messagesent = AM_XFGUESTBOOK_MSGDELETED;
60
        for ($i = 0; $i < $msg_count; $i++) {
61
            $msg      = $msgHandler->get($_POST['msg_id'][$i]);
62
            $filename = $msg->getVar('title');
0 ignored issues
show
Unused Code introduced by
The assignment to $filename is dead and can be removed.
Loading history...
63
            $filename = $msg->getVar('photo');
64
            if (!$msgHandler->delete($msg)) {
65
                $messagesent = AM_XFGUESTBOOK_ERRORDEL;
66
            }
67
            if ('' !== $filename) {
68
                $filename = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/' . $filename;
69
                unlink($filename);
70
            }
71
        }
72
    } else {
73
        $messagesent = AM_XFGUESTBOOK_NOMSG;
74
    }
75
    redirect_header($_SERVER['PHP_SELF'], 2, $messagesent);
76
}
77
78
function approve()
79
{
80
    global $msgHandler;
81
    $msg_count = (!empty($_POST['msg_id']) && is_array($_POST['msg_id'])) ? count($_POST['msg_id']) : 0;
82
    if ($msg_count > 0) {
83
        $messagesent = AM_XFGUESTBOOK_VALIDATE;
84
        for ($i = 0; $i < $msg_count; $i++) {
85
            $msg = $msgHandler->get($_POST['msg_id'][$i]);
86
            $msg->setVar('moderate', 0);
87
            if (!$msgHandler->insert($msg)) {
88
                $messagesent = AM_XFGUESTBOOK_ERRORVALID;
89
            }
90
        }
91
    } else {
92
        $messagesent = AM_XFGUESTBOOK_NOMSG;
93
    }
94
    redirect_header($_SERVER['PHP_SELF'], 2, $messagesent);
95
}
96
97
function banish()
98
{
99
    global $msgHandler, $xoopsDB;
100
    $msg_count = (!empty($_POST['msg_id']) && is_array($_POST['msg_id'])) ? count($_POST['msg_id']) : 0;
101
    if ($msg_count > 0) {
102
        $messagesent = AM_XFGUESTBOOK_BANISHED;
103
        for ($i = 0; $i < $msg_count; $i++) {
104
            $msg    = $msgHandler->get($_POST['msg_id'][$i]);
105
            $ip[$i] = $msg->getVar('poster_ip');
106
            $msg->setVar('moderate', 1);
107
            if (!$msgHandler->insert($msg)) {
108
                $messagesent = AM_XFGUESTBOOK_ERRORBANISHED;
109
            }
110
        }
111
        $ip     = array_unique($ip);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ip does not seem to be defined for all execution paths leading up to this point.
Loading history...
112
        $badips = Xfguestbook\Utility::get_badips();
113
        foreach ($ip as $oneip) {
114
            if (!in_array($oneip, $badips)) {
115
                $sql    = 'INSERT INTO ' . $xoopsDB->prefix('xfguestbook_badips') . " (ip_value) VALUES ('$oneip')";
116
                $result = $xoopsDB->query($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
117
            }
118
        }
119
    } else {
120
        $messagesent = AM_XFGUESTBOOK_NOMSG;
121
    }
122
123
    redirect_header($_SERVER['PHP_SELF'], 2, $messagesent);
124
}
125
126
function show()
127
{
128
    global $msgHandler, $xoopsModule, $pathIcon16;
129
    $pick              = \Xmf\Request::getInt('pick', 0, 'GET');
0 ignored issues
show
Unused Code introduced by
The assignment to $pick is dead and can be removed.
Loading history...
130
    $start             = \Xmf\Request::getInt('start', 0, 'GET');
131
    $sel_status        = \Xmf\Request::getInt('sel_status', 0, 'GET');
132
    $sel_order         = \Xmf\Request::getInt('sel_order', 0, 'GET');
133
    $limit             = 10;
134
    $status_option0    = '';
135
    $status_option1    = '';
136
    $status_option2    = '';
137
    $order_option_asc  = '';
138
    $order_option_desc = '';
139
140
    switch ($sel_status) {
141
        case 0:
142
            $status_option0 = 'selected';
143
            $title          = AM_XFGUESTBOOK_ALLMSG;
144
            $criteria       = new \Criteria('msg_id', 0, '>');
145
            $criteria->setSort('post_time');
146
            break;
147
148
        case 1:
149
            $status_option1 = 'selected';
150
            $title          = AM_XFGUESTBOOK_PUBMSG;
151
            $criteria       = new \Criteria('moderate', '0');
152
            $criteria->setSort('post_time');
153
            break;
154
155
        case 2:
156
            $status_option2 = 'selected';
157
            $title          = AM_XFGUESTBOOK_WAITMSG;
158
            $criteria       = new \Criteria('moderate', '1');
159
            $criteria->setSort('post_time');
160
            break;
161
162
    }
163
164
    switch ($sel_order) {
165
        case 1:
166
            $order_option_asc = 'selected';
167
            $criteria->setOrder('ASC');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $criteria does not seem to be defined for all execution paths leading up to this point.
Loading history...
168
            break;
169
170
        case 0:
171
            $order_option_desc = 'selected';
172
            $criteria->setOrder('DESC');
173
            break;
174
    }
175
176
    $totalcount = $msgHandler->countMsg($criteria);
177
    $criteria->setOrder('DESC');
178
    $criteria->setLimit($limit);
179
    $criteria->setStart($start);
180
    $msg = $msgHandler->getObjects($criteria);
181
182
    $badips = Xfguestbook\Utility::get_badips();
183
184
    /* -- Code to show selected terms -- */
185
    echo "<form name='pick' id='pick' action='" . $_SERVER['PHP_SELF'] . '\' method=\'GET\' style=\'margin: 0;\'>';
186
187
    echo "
188
        <table width='100%' cellspacing='1' cellpadding='2' border='0' style='border-left: 1px solid #c0c0c0; border-top: 1px solid #c0c0c0; border-right: 1px solid #c0c0c0;'>
189
            <tr>
190
                <td><span style='font-weight: bold; font-size: 12px; font-variant: small-caps;'>" . $title . ' : ' . $totalcount . "</span></td>
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
191
                <td align='right'>
192
                " . AM_XFGUESTBOOK_DISPLAY . " :
193
                    <select name='sel_status' onchange='submit()'>
194
                        <option value = '0' $status_option0>" . AM_XFGUESTBOOK_ALLMSG . " </option>
195
                        <option value = '1' $status_option1>" . AM_XFGUESTBOOK_PUBMSG . " </option>
196
                        <option value = '2' $status_option2>" . AM_XFGUESTBOOK_WAITMSG . ' </option>
197
                    </select>
198
                ' . AM_XFGUESTBOOK_SELECT_SORT . "
199
                    <select name='sel_order' onchange='submit()'>
200
                        <option value = '1' $order_option_asc>" . AM_XFGUESTBOOK_SORT_ASC . "</option>
201
                        <option value = '0' $order_option_desc>" . AM_XFGUESTBOOK_SORT_DESC . '</option>
202
                    </select>
203
                </td>
204
            </tr>
205
        </table>
206
        </form>';
207
    /* -- end code to show selected terms -- */
208
209
    echo "<table border='1' width='100%' cellpadding ='2' cellspacing='1'>";
210
    echo "<tr class='bg3'>";
211
    echo "<td align='center'></td>";
212
    echo "<td align='center'><b><input type='hidden' name='op' value='delete'></td>";
213
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_NAME . '</td>';
214
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_TITLE . '</td>';
215
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_MESSAGE . '</td>';
216
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_DATE . '</td>';
217
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_ACTION . '</td>';
218
    echo '</tr>';
219
220
    if ('0' != $totalcount) {
221
        echo "<form name='msglist' id='list' action='" . $_SERVER['PHP_SELF'] . '\' method=\'POST\' style=\'margin: 0;\'>';
222
223
        foreach ($msg as $onemsg) {
224
            $all_msg              = [];
225
            $all_msg['post_time'] = formatTimestamp($onemsg->getVar('post_time'));
226
            $all_msg['msg_id']    = $onemsg->getVar('msg_id');
227
            $all_msg['user']      = ($onemsg->getVar('user_id') > 0) ? \XoopsUser::getUnameFromId($onemsg->getVar('user_id')) : $onemsg->getVar('uname');
228
            $all_msg['action']    = "<a href='main.php?op=edit&amp;msg_id=" . $onemsg->getVar('msg_id') . '\'><img src=\'' . $pathIcon16 . "/edit.png'></a>";
229
            $img_status           = "<img src='" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/assets/images/';
230
            if ($onemsg->getVar('moderate')) {
231
                $img_status .= "ic15_question.gif'>";
232
            } else {
233
                $img_status .= "ic15_ok.gif'>";
234
            }
235
            $all_msg['title']   = "<a href='../index.php?op=show_one&msg_id=" . $onemsg->getVar('msg_id') . '\'>' . $onemsg->getVar('title') . '</a>';
236
            $all_msg['message'] = $onemsg->getVar('message');
237
238
            if ($onemsg->getVar('photo')) {
239
                $all_msg['message'] = '<img src="' . XOOPS_UPLOAD_URL . '/' . $xoopsModule->getVar('dirname') . '/' . $onemsg->getVar('photo') . '" align = "left" hspace ="10">' . $onemsg->getVar('message');
240
            } else {
241
                $all_msg['message'] = $onemsg->getVar('message');
242
            }
243
244
            echo '<tr>';
245
            echo "<td align='center' class='even'><input type='checkbox' name='msg_id[]' id='msg_id[]' value='" . $all_msg['msg_id'] . '\'></td>';
246
            echo "<td align='center' class = 'head'><b>" . $img_status . '</b></td>';
247
            echo "<td align='center' class = 'even'>" . $all_msg['user'] . '</td>';
248
            echo "<td align='left' class = 'odd'>" . $all_msg['title'] . '</td>';
249
            echo "<td align='left' class = 'even'>" . $all_msg['message'] . '</td>';
250
            echo "<td class='odd'>" . $all_msg['post_time'] . '<br>';
251
            if (in_array($onemsg->getVar('poster_ip'), $badips)) {
252
                echo "<span style='color: #FF0000; '><b>" . $onemsg->getVar('poster_ip') . '</b></span></td>';
253
            } else {
254
                echo $onemsg->getVar('poster_ip') . '</td>';
255
            }
256
            echo "<td align='center' class='even'>" . $all_msg['action'] . '</td>';
257
            echo '</tr>';
258
            unset($all_msg);
259
        }
260
        echo "<tr class='foot'><td><select name='op'>";
261
        if (1 != $sel_status) {
262
            echo "<option value='approve'>" . AM_XFGUESTBOOK_PUB . '</option>';
263
        }
264
        echo "<option value='delete'>" . _DELETE . '</option>';
265
        echo "<option value='banish'>" . AM_XFGUESTBOOK_BAN . '</option>';
266
        echo '</select>&nbsp;</td>';
267
        echo "<td colspan='6'>" . $GLOBALS['xoopsSecurity']->getTokenHTML() . "<input type='submit' value='" . _GO . '\'>';
268
        echo '</td></tr>';
269
        echo '</form>';
270
    } else {
271
        echo "<tr ><td align='center' colspan ='10' class = 'head'><b>" . AM_XFGUESTBOOK_NOMSG . '</b></td></tr>';
272
    }
273
    echo '</table><br>';
274
    if ($totalcount > $limit) {
275
        require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
276
        $pagenav = new \XoopsPageNav($totalcount, $limit, $start, 'start', 'sel_status=' . $sel_status . '&sel_order=' . $sel_order);
277
        echo "<div class='center;' class = 'head'>" . $pagenav->renderNav() . '</div><br>';
278
    } else {
279
        echo '';
280
    }
281
    echo '<br>';
282
}
283
284
switch ($op) {
285
    case 'save':
286
        global $xoopsModule;
287
        if (!$GLOBALS['xoopsSecurity']->check()) {
288
            redirect_header('index.php', 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
289
        }
290
        $msgstop = '';
291
        $msg     = $msgHandler->get($msg_id);
292
        $del_img = \Xmf\Request::getInt('del_img', 0, 'POST');
293
        if ($del_img) {
294
            $filename = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/' . $msg->getVar('photo');
295
            unlink($filename);
296
            $msg->setVar('photo', '');
297
        } elseif (!empty($_FILES['photo']['name'])) {
298
            Xfguestbook\Utility::upload();
299
            $photo      = str_replace('tmp_', 'msg_', $preview_name);
300
            $photos_dir = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/';
301
            rename($photos_dir . $preview_name, $photos_dir . $photo);
302
            if ('' !== $msg->getVar('photo')) {
303
                $filename = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/' . $msg->getVar('photo');
304
                unlink($filename);
305
            }
306
            $msg->setVar('photo', $photo);
307
        }
308
        if (!empty($msgstop)) {
0 ignored issues
show
introduced by
The condition empty($msgstop) is always true.
Loading history...
309
            redirect_header('main.php?op=edit&msg_id=' . $msg_id, 2, $msgstop);
310
        }
311
        $uname    = \Xmf\Request::getString('uname', '', 'POST');
312
        $email    = \Xmf\Request::getString('email', '', 'POST');
313
        $url      = \Xmf\Request::getString('url', '', 'POST');
314
        $title    = \Xmf\Request::getString('title', '', 'POST');
315
        $message  = \Xmf\Request::getString('message', '', 'POST');
316
        $note     = \Xmf\Request::getString('note', '', 'POST');
317
        $gender   = \Xmf\Request::getString('gender', '', 'POST');
318
        $country  = \Xmf\Request::getString('country', '', 'POST');
319
        $other    = \Xmf\Request::getString('other', '', 'POST');
320
        $moderate = \Xmf\Request::getInt('moderate', 0, 'POST');
321
322
        $msg->setVar('uname', $uname);
323
        $msg->setVar('email', $email);
324
        $msg->setVar('url', $url);
325
        $msg->setVar('title', $title);
326
        $msg->setVar('message', $message);
327
        $msg->setVar('note', $note);
328
        $msg->setVar('gender', $gender);
329
        if ('' !== $country) {
330
            $msg->setVar('country', $country);
331
            $msg->setVar('flagdir', $helper->getConfig('flagdir'));
332
        }
333
        $msg->setVar('other', $other);
334
        $msg->setVar('moderate', $moderate);
335
        if ($msgHandler->insert($msg)) {
336
            redirect_header('main.php?op=show', 1, AM_XFGUESTBOOK_MSGMOD);
337
        } else {
338
            redirect_header('main.php?op=show', 2, AM_XFGUESTBOOK_MSGERROR);
339
        }
340
        break;
341
342
    case 'edit':
343
        xoops_cp_header();
344
        $adminObject = \Xmf\Module\Admin::getInstance();
345
        $adminObject->displayNavigation(basename(__FILE__));
346
        //xfguestbook_admin_menu(0);
347
        $msg = $msgHandler->get($msg_id);
348
        require_once  dirname(__DIR__) . '/include/form_edit.inc.php';
349
        $msg_form->display();
350
        require_once __DIR__   . '/admin_footer.php';
351
        //xoops_cp_footer();
352
        break;
353
354
    case 'approve':
355
        approve();
0 ignored issues
show
Bug introduced by
The call to approve() has too few arguments starting with msg_id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

355
        /** @scrutinizer ignore-call */ 
356
        approve();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
356
        break;
357
358
    case 'delete':
359
        delete();
0 ignored issues
show
Bug introduced by
The call to delete() has too few arguments starting with msg_id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

359
        /** @scrutinizer ignore-call */ 
360
        delete();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
360
        break;
361
362
    case 'banish':
363
        banish();
364
        break;
365
366
    case 'show':
367
    default:
368
        xoops_cp_header();
369
        $adminObject = \Xmf\Module\Admin::getInstance();
370
        $adminObject->displayNavigation(basename(__FILE__));
371
        //xfguestbook_admin_menu(0);
372
        show();
373
        require_once __DIR__   . '/admin_footer.php';
374
        //xoops_cp_footer();
375
        break;
376
}
377