Completed
Push — master ( 76b4a1...141e43 )
by Michael
06:17 queued 02:44
created

ip_manager.php ➔ badIpSave()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 20

Duplication

Lines 26
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 20
c 3
b 0
f 0
nc 3
nop 2
dl 26
loc 26
rs 8.8571
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 52 and the first side effect is on line 26.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// $Id: admin/ip_manager.php,v 1.0 2006/01/01 C. Felix alias the Cat
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
include dirname(dirname(dirname(__DIR__))) . '/include/cp_header.php';
27
include_once dirname(__DIR__) . '/include/cp_functions.php';
28
include_once __DIR__ . '/admin_header.php';
29
include_once dirname(__DIR__) . '/include/functions.php';
30
31 View Code Duplication
if (isset($_GET['op'])) {
0 ignored issues
show
Duplication introduced by
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...
32
    $op = $_GET['op'];
33
} elseif (isset($_POST['op'])) {
34
    $op = $_POST['op'];
35
} else {
36
    $op = 'badIpShow';
37
}
38
39 View Code Duplication
if (isset($_GET['ip_id'])) {
0 ignored issues
show
Duplication introduced by
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...
40
    $ip_id = (int)$_GET['ip_id'];
41
} elseif (isset($_POST['ip_id'])) {
42
    $ip_id = (int)$_POST['ip_id'];
43
} else {
44
    $ip_id = 0;
45
}
46
47
$ip_value = isset($_POST['ip_value']) ? $_POST['ip_value'] : '';
48
49
/**
50
 * @param $ip_id
51
 */
52
function badIpDel($ip_id)
0 ignored issues
show
Unused Code introduced by
The parameter $ip_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
badIpDel uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
badIpDel uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
53
{
54
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
55
    $ip_count = (!empty($_POST['ip_id']) && is_array($_POST['ip_id'])) ? count($_POST['ip_id']) : 0;
56
    if ($ip_count > 0) {
57
        $messagesent = _AM_XFGB_BADIP_DELETED;
58
        for ($i = 0; $i < $ip_count; $i++) {
59
            $sql = sprintf('DELETE FROM %s WHERE ip_id = %u', $xoopsDB->prefix('xfguestbook_badips'), $_POST['ip_id'][$i]);
60
            if (!$result = $xoopsDB->query($sql)) {
61
                $messagesent = _AM_XFGB_ERRORDEL;
62
            }
63
        }
64
    } else {
65
        $messagesent = _AM_XFGB_NOBADIP;
66
    }
67
    redirect_header($_SERVER['PHP_SELF'], 2, $messagesent);
68
}
69
70
/**
71
 * @param null $ip_id
72
 */
73
function badIpForm($ip_id = null)
74
{
75
    include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
76
    if ($ip_id) {
77
        $sform    = new XoopsThemeForm(_AM_XFGB_MOD_BADIP, 'op', xoops_getenv('PHP_SELF'));
78
        $badips   = xfgb_get_badips(true);
79
        $ip_value = $badips[$ip_id]['ip_value'];
80
    } else {
81
        $sform    = new XoopsThemeForm(_AM_XFGB_ADD_BADIP, 'op', xoops_getenv('PHP_SELF'));
82
        $ip_value = '';
83
    }
84
85
    $sform->addElement(new XoopsFormText(_AM_XFGB_VALUE, 'ip_value', 50, 50, $ip_value), true);
86
87
    $button_tray = new XoopsFormElementTray('', '');
88
    $button_tray->addElement(new XoopsFormButton('', 'save', _SUBMIT, 'submit'));
89
    if ($ip_id) {
90
        $button_tray->addElement(new XoopsFormHidden('ip_id', $ip_id));
91
    }
92
    $button_tray->addElement(new XoopsFormHidden('op', 'badIpSave'));
93
    $sform->addElement($button_tray);
94
    $sform->display();
95
}
96
97
/**
98
 * @param $ip_id
99
 * @param $ip_value
100
 */
101 View Code Duplication
function badIpSave($ip_id, $ip_value)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
102
{
103
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
104
105
    $myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
$myts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
    //$ip_value=$myts->makeTboxData4Save($ip_value);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
    if (!empty($ip_id)) {
108
        $sql = 'UPDATE ' . $xoopsDB->prefix('xfguestbook_badips') . " SET ip_id='$ip_id', ip_value='$ip_value'";
109
        $sql .= " WHERE ip_id = $ip_id";
110
        $xoopsDB->query($sql);
111
        $messagesent = _AM_XFGB_BADIP_UPDATED;
112
    } else {
113
        $sql = sprintf("SELECT COUNT(*) FROM  %s WHERE ip_value = '%s'", $xoopsDB->prefix('xfguestbook_badips'), $ip_value);
114
        list($count) = $xoopsDB->fetchRow($xoopsDB->query($sql));
115
        if ($count > 0) {
116
            $messagesent = '<font color="#FF0000">' . _AM_XFGB_BADIP_EXIST . '</font>';
117
        } else {
118
            $country_id = $xoopsDB->genId('ip_id_seq');
0 ignored issues
show
Unused Code introduced by
$country_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
            $sql        = sprintf("INSERT INTO %s (ip_id, ip_value) VALUES (%s, '%s')", $xoopsDB->prefix('xfguestbook_badips'), $ip_id, $ip_value);
120
            $xoopsDB->query($sql);
121
            $messagesent = _AM_XFGB_BADIP_ADDED;
122
        }
123
    }
124
    redirect_header('ip_manager.php', 2, $messagesent);
125
    exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function badIpSave() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
126
}
127
128
function badIpShow()
0 ignored issues
show
Coding Style introduced by
badIpShow uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
badIpShow uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
129
{
130
    global $action, $start, $xoopsModule, $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
131
    $myts      = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
$myts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
132
    $limit     = 15;
0 ignored issues
show
Unused Code introduced by
$limit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
    $badips    = xfgb_get_badips(true);
134
    $nb_badips = count($badips);
135
136
    echo "
137
    <table width='100%' cellspacing='1' cellpadding='2' border='0' style='border-left: 1px solid silver; border-top: 1px solid silver; border-right: 1px solid silver;'>
138
        <tr>
139
            <td><span style='font-weight: bold; font-size: 12px; font-variant: small-caps;'>" . _AM_XFGB_DISP_BADIPS . ' : ' . $nb_badips . "</span></td>
140
            <td align='right'>
141
            </td>
142
        </tr>
143
    </table>";
144
145
    echo "<table border='1' width='100%' cellpadding ='2' cellspacing='1'>";
146
    echo "<tr class='bg3'>";
147
    echo '<td></td>';
148
    echo "<td align='center'><b>" . _AM_XFGB_IPS . '</td>';
149
    echo "<td align='center'><b>" . _AM_XFGB_ACTION . '</td>';
150
    echo '</tr>';
151
152
    if ('0' != count($badips)) {
153
        echo "<form name='badiplist' id='list' action='" . $_SERVER['PHP_SELF'] . "' method='POST' style='margin: 0;'>";
154
155
        for ($i = 0; $i < $nb_badips; $i++) {
156
            echo '<tr>';
157
            echo "<td align='center' class='even'><input type='checkbox' name='ip_id[]' id='ip_id[]' value='" . $badips[$i]['ip_id'] . "'/></td>";
158
            echo "<td class = 'odd'>" . $badips[$i]['ip_value'] . '</td>';
159
            echo "<td align='center' class='even'><a href='ip_manager.php?op=badIpEdit&amp;ip_id=" . $badips[$i]['ip_id'] . "'>" . _EDIT . '</a></td>';
160
            echo '</tr>';
161
            //          unset($badips);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
162
        }
163
        echo "<tr class='foot'><td><select name='op'>";
164
        echo "<option value='badIpDel'>" . _DELETE . '</option>';
165
        echo '</select>&nbsp;</td>';
166
        echo "<td colspan='3'>" . $GLOBALS['xoopsSecurity']->getTokenHTML() . "<input type='submit' value='" . _GO . "' />";
167
        echo '</td></tr>';
168
        echo '</form>';
169
    } else {
170
        echo "<tr ><td align='center' colspan ='3' class = 'head'><b>" . _AM_XFGB_NOBADIP . '</b></td></tr>';
171
    }
172
    echo '</table><br>';
173
    echo '<br>';
174
}
175
176
switch ($op) {
177 View Code Duplication
    case 'badIpForm':
0 ignored issues
show
Duplication introduced by
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...
178
        xoops_cp_header();
179
        $index_admin = new ModuleAdmin();
180
        echo $index_admin->addNavigation(basename(__FILE__));
181
        //xfguestbook_admin_menu(3);
182
        badIpForm($ip_id);
183
        include __DIR__ . '/admin_footer.php';
184
        //xoops_cp_footer();
185
        break;
186
    case 'badIpDel':
187
        badIpDel($ip_id);
188
        break;
189 View Code Duplication
    case 'badIpEdit':
0 ignored issues
show
Duplication introduced by
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...
190
        xoops_cp_header();
191
        $index_admin = new ModuleAdmin();
192
        echo $index_admin->addNavigation(basename(__FILE__));
193
        //xfguestbook_admin_menu(3);
194
        badIpForm($ip_id);
195
        include __DIR__ . '/admin_footer.php';
196
        //xoops_cp_footer();
197
        break;
198
    case 'badIpSave':
199
        badIpSave($ip_id, $ip_value);
200
        break;
201 View Code Duplication
    case 'badIpAdd':
0 ignored issues
show
Duplication introduced by
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...
202
        xoops_cp_header();
203
        $index_admin = new ModuleAdmin();
204
        echo $index_admin->addNavigation(basename(__FILE__));
205
        //xfguestbook_admin_menu(3);
206
        badIpForm();
207
        include __DIR__ . '/admin_footer.php';
208
        //xoops_cp_footer();
209
        break;
210
    case 'badIpShow':
211 View Code Duplication
    default:
0 ignored issues
show
Duplication introduced by
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...
212
        xoops_cp_header();
213
        $index_admin = new ModuleAdmin();
214
        echo $index_admin->addNavigation(basename(__FILE__));
215
        //xfguestbook_admin_menu(3);
216
        badIpShow();
217
        badIpForm();
218
        include __DIR__ . '/admin_footer.php';
219
        //xoops_cp_footer();
220
        break;
221
}
222