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

main.php ➔ delete()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 23
Code Lines 17

Duplication

Lines 4
Ratio 17.39 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 10
nop 0
dl 4
loc 23
rs 8.5906
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 55 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/index.php,v 1.40 2006/01/01 C. Felix AKA 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
30
if (!isset($xoopsModuleConfig['flagdir'])) {
31
    redirect_header(XOOPS_URL . '/modules/system/admin.php?fct=modulesadmin&op=update&module=' . $xoopsModule->dirname(), 4, _AM_XFGB_MUST_UPDATE);
32
}
33
34
include_once dirname(__DIR__) . '/include/functions.php';
35
//include_once("../class/msg.php");
36
37 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...
38
    $op = $_GET['op'];
39
} elseif (isset($_POST['op'])) {
40
    $op = $_POST['op'];
41
} else {
42
    $op = 'show';
43
}
44
45 View Code Duplication
if (isset($_GET['msg_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...
46
    $msg_id = (int)$_GET['msg_id'];
47
} elseif (isset($_POST['msg_id'])) {
48
    $msg_id = (int)$_POST['msg_id'];
49
} else {
50
    $msg_id = 0;
51
}
52
53
$msg_handler = xoops_getModuleHandler('msg');
54
55
function delete()
0 ignored issues
show
Coding Style introduced by
delete 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
delete 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...
56
{
57
    global $msg_handler, $xoopsModule;
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...
58
    $msg_count = (!empty($_POST['msg_id']) && is_array($_POST['msg_id'])) ? count($_POST['msg_id']) : 0;
59
    if ($msg_count > 0) {
60
        $messagesent = _AM_XFGB_MSGDELETED;
61
        for ($i = 0; $i < $msg_count; $i++) {
62
            $msg      = $msg_handler->get($_POST['msg_id'][$i]);
63
            $filename = $msg->getVar('title');
0 ignored issues
show
Unused Code introduced by
$filename 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...
64
            $filename = $msg->getVar('photo');
65
            if (!$msg_handler->delete($msg)) {
66
                $messagesent = _AM_XFGB_ERRORDEL;
67
            }
68 View Code Duplication
            if ('' !== $filename) {
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...
69
                $filename = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/' . $filename;
70
                unlink($filename);
71
            }
72
        }
73
    } else {
74
        $messagesent = _AM_XFGB_NOMSG;
75
    }
76
    redirect_header($_SERVER['PHP_SELF'], 2, $messagesent);
77
}
78
79
function approve()
0 ignored issues
show
Coding Style introduced by
approve 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
approve 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...
80
{
81
    global $msg_handler;
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...
82
    $msg_count = (!empty($_POST['msg_id']) && is_array($_POST['msg_id'])) ? count($_POST['msg_id']) : 0;
83
    if ($msg_count > 0) {
84
        $messagesent = _AM_XFGB_VALIDATE;
85
        for ($i = 0; $i < $msg_count; $i++) {
86
            $msg = $msg_handler->get($_POST['msg_id'][$i]);
87
            $msg->setVar('moderate', 0);
88
            if (!$msg_handler->insert($msg)) {
89
                $messagesent = _AM_XFGB_ERRORVALID;
90
            }
91
        }
92
    } else {
93
        $messagesent = _AM_XFGB_NOMSG;
94
    }
95
    redirect_header($_SERVER['PHP_SELF'], 2, $messagesent);
96
    exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function approve() 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...
97
}
98
99
function banish()
0 ignored issues
show
Coding Style introduced by
banish 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
banish 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...
100
{
101
    global $msg_handler, $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...
102
    $msg_count = (!empty($_POST['msg_id']) && is_array($_POST['msg_id'])) ? count($_POST['msg_id']) : 0;
103
    if ($msg_count > 0) {
104
        $messagesent = _AM_XFGB_BANISHED;
105
        for ($i = 0; $i < $msg_count; $i++) {
106
            $msg    = $msg_handler->get($_POST['msg_id'][$i]);
107
            $ip[$i] = $msg->getVar('poster_ip');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ip was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ip = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
108
            $msg->setVar('moderate', 1);
109
            if (!$msg_handler->insert($msg)) {
110
                $messagesent = _AM_XFGB_ERRORBANISHED;
111
            }
112
        }
113
        $ip     = array_unique($ip);
0 ignored issues
show
Bug introduced by
The variable $ip does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
114
        $badips = xfgb_get_badips();
115
        foreach ($ip as $oneip) {
116
            if (!in_array($oneip, $badips)) {
117
                $sql    = 'INSERT INTO ' . $xoopsDB->prefix('xfguestbook_badips') . " (ip_value) VALUES ('$oneip')";
118
                $result = $xoopsDB->query($sql);
0 ignored issues
show
Unused Code introduced by
$result 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
            }
120
        }
121
    } else {
122
        $messagesent = _AM_XFGB_NOMSG;
123
    }
124
125
    redirect_header($_SERVER['PHP_SELF'], 2, $messagesent);
126
    exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function banish() 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...
127
}
128
129
function show()
0 ignored issues
show
Coding Style introduced by
show uses the super-global variable $_GET 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
show 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
show 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...
130
{
131
    global $msg_handler, $xoopsModule, $pathIcon16;
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...
132
    $pick              = isset($_GET['pick']) ? (int)$_GET['pick'] : 0;
0 ignored issues
show
Unused Code introduced by
$pick 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
    $start             = isset($_GET['start']) ? (int)$_GET['start'] : 0;
134
    $sel_status        = isset($_GET['sel_status']) ? $_GET['sel_status'] : 0;
135
    $sel_order         = isset($_GET['sel_order']) ? $_GET['sel_order'] : 0;
136
    $limit             = 10;
137
    $status_option0    = '';
138
    $status_option1    = '';
139
    $status_option2    = '';
140
    $order_option_asc  = '';
141
    $order_option_desc = '';
142
143
    switch ($sel_status) {
144
        case 0 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
145
            $status_option0 = "selected='selected'";
146
            $title          = _AM_XFGB_ALLMSG;
147
            $criteria       = new Criteria('msg_id', 0, '>');
148
            $criteria->setSort('post_time');
149
            break;
150
151
        case 1 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
152
            $status_option1 = "selected='selected'";
153
            $title          = _AM_XFGB_PUBMSG;
154
            $criteria       = new Criteria('moderate', '0');
155
            $criteria->setSort('post_time');
156
            break;
157
158
        case 2 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
159
            $status_option2 = "selected='selected'";
160
            $title          = _AM_XFGB_WAITMSG;
161
            $criteria       = new Criteria('moderate', '1');
162
            $criteria->setSort('post_time');
163
            break;
164
165
    }
166
167
    switch ($sel_order) {
168
        case 1:
169
            $order_option_asc = "selected='selected'";
170
            $criteria->setOrder('ASC');
0 ignored issues
show
Bug introduced by
The variable $criteria does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
171
            break;
172
173
        case 0:
174
            $order_option_desc = "selected='selected'";
175
            $criteria->setOrder('DESC');
176
            break;
177
    }
178
179
    $totalcount = $msg_handler->countMsg($criteria);
180
    $criteria->setOrder('DESC');
181
    $criteria->setLimit($limit);
182
    $criteria->setStart($start);
183
    $msg =& $msg_handler->getObjects($criteria);
184
185
    $badips = xfgb_get_badips();
186
187
    /* -- Code to show selected terms -- */
188
    echo "<form name='pick' id='pick' action='" . $_SERVER['PHP_SELF'] . "' method='GET' style='margin: 0;'>";
189
190
    echo "
191
        <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;'>
192
            <tr>
193
                <td><span style='font-weight: bold; font-size: 12px; font-variant: small-caps;'>" . $title . ' : ' . $totalcount . "</span></td>
0 ignored issues
show
Bug introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
194
                <td align='right'>
195
                " . _AM_XFGB_DISPLAY . " :
196
                    <select name='sel_status' onchange='submit()'>
197
                        <option value = '0' $status_option0>" . _AM_XFGB_ALLMSG . " </option>
198
                        <option value = '1' $status_option1>" . _AM_XFGB_PUBMSG . " </option>
199
                        <option value = '2' $status_option2>" . _AM_XFGB_WAITMSG . ' </option>
200
                    </select>
201
                ' . _AM_XFGB_SELECT_SORT . "
202
                    <select name='sel_order' onchange='submit()'>
203
                        <option value = '1' $order_option_asc>" . _AM_XFGB_SORT_ASC . "</option>
204
                        <option value = '0' $order_option_desc>" . _AM_XFGB_SORT_DESC . '</option>
205
                    </select>
206
                </td>
207
            </tr>
208
        </table>
209
        </form>';
210
    /* -- end code to show selected terms -- */
211
212
    echo "<table border='1' width='100%' cellpadding ='2' cellspacing='1'>";
213
    echo "<tr class='bg3'>";
214
    echo "<td align='center'></td>";
215
    echo "<td align='center'><b><input type='hidden' name='op' value='delete' /></td>";
216
    echo "<td align='center'><b>" . _AM_XFGB_NAME . '</td>';
217
    echo "<td align='center'><b>" . _AM_XFGB_TITLE . '</td>';
218
    echo "<td align='center'><b>" . _AM_XFGB_MESSAGE . '</td>';
219
    echo "<td align='center'><b>" . _AM_XFGB_DATE . '</td>';
220
    echo "<td align='center'><b>" . _AM_XFGB_ACTION . '</td>';
221
    echo '</tr>';
222
223
    if ('0' != $totalcount) {
224
        echo "<form name='msglist' id='list' action='" . $_SERVER['PHP_SELF'] . "' method='POST' style='margin: 0;'>";
225
226
        foreach ($msg as $onemsg) {
227
            $all_msg              = array();
228
            $all_msg['post_time'] = formatTimestamp($onemsg->getVar('post_time'));
229
            $all_msg['msg_id']    = $onemsg->getVar('msg_id');
230
            $all_msg['user']      = ($onemsg->getVar('user_id') > 0) ? XoopsUser::getUnameFromId($onemsg->getVar('user_id')) : $onemsg->getVar('uname');
231
            $all_msg['action']    = "<a href='main.php?op=edit&amp;msg_id=" . $onemsg->getVar('msg_id') . "'><img src='" . $pathIcon16 . "/edit.png'></a>";
232
            $img_status           = "<img src='" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/assets/images/';
233
            if ($onemsg->getVar('moderate')) {
234
                $img_status .= "ic15_question.gif'>";
235
            } else {
236
                $img_status .= "ic15_ok.gif'>";
237
            }
238
            $all_msg['title']   = "<a href='../main.php?op=show_one&msg_id=" . $onemsg->getVar('msg_id') . "'>" . $onemsg->getVar('title') . '</a>';
239
            $all_msg['message'] = $onemsg->getVar('message');
240
241
            if ($onemsg->getVar('photo')) {
242
                $all_msg['message'] =
243
                    "<img src=\"" . XOOPS_UPLOAD_URL . '/' . $xoopsModule->getVar('dirname') . '/' . $onemsg->getVar('photo') . "\" align = \"left\" hspace =\"10\">" . $onemsg->getVar('message');
244
            } else {
245
                $all_msg['message'] = $onemsg->getVar('message');
246
            }
247
248
            echo '<tr>';
249
            echo "<td align='center' class='even'><input type='checkbox' name='msg_id[]' id='msg_id[]' value='" . $all_msg['msg_id'] . "'/></td>";
250
            echo "<td align='center' class = 'head'><b>" . $img_status . '</b></td>';
251
            echo "<td align='center' class = 'even'>" . $all_msg['user'] . '</td>';
252
            echo "<td align='left' class = 'odd'>" . $all_msg['title'] . '</td>';
253
            echo "<td align='left' class = 'even'>" . $all_msg['message'] . '</td>';
254
            echo "<td class='odd'>" . $all_msg['post_time'] . '<br>';
255
            if (in_array($onemsg->getVar('poster_ip'), $badips)) {
256
                echo "<font color=\"#FF0000\"><b>" . $onemsg->getVar('poster_ip') . '</b></font></td>';
257
            } else {
258
                echo $onemsg->getVar('poster_ip') . '</td>';
259
            }
260
            echo "<td align='center' class='even'>" . $all_msg['action'] . '</td>';
261
            echo '</tr>';
262
            unset($all_msg);
263
        }
264
        echo "<tr class='foot'><td><select name='op'>";
265
        if (1 != $sel_status) {
266
            echo "<option value='approve'>" . _AM_XFGB_PUB . '</option>';
267
        }
268
        echo "<option value='delete'>" . _DELETE . '</option>';
269
        echo "<option value='banish'>" . _AM_XFGB_BAN . '</option>';
270
        echo '</select>&nbsp;</td>';
271
        echo "<td colspan='6'>" . $GLOBALS['xoopsSecurity']->getTokenHTML() . "<input type='submit' value='" . _GO . "' />";
272
        echo '</td></tr>';
273
        echo '</form>';
274
    } else {
275
        echo "<tr ><td align='center' colspan ='10' class = 'head'><b>" . _AM_XFGB_NOMSG . '</b></td></tr>';
276
    }
277
    echo '</table><br>';
278 View Code Duplication
    if ($totalcount > $limit) {
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...
279
        include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
280
        $pagenav = new XoopsPageNav($totalcount, $limit, $start, 'start', 'sel_status=' . $sel_status . '&sel_order=' . $sel_order);
281
        echo "<div style='text-align: center;' class = 'head'>" . $pagenav->renderNav() . '</div><br>';
282
    } else {
283
        echo '';
284
    }
285
    echo '<br>';
286
}
287
288
switch ($op) {
289
    case 'save':
290
        global $xoopsModule;
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...
291
        if (!$GLOBALS['xoopsSecurity']->check()) {
292
            redirect_header('index.php', 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
293
        }
294
        $msgstop = '';
295
        $msg     = $msg_handler->get($msg_id);
296
        $del_img = isset($_POST['del_img']) ? (int)$_POST['del_img'] : 0;
297
        if ($del_img) {
298
            $filename = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/' . $msg->getVar('photo');
299
            unlink($filename);
300
            $msg->setVar('photo', '');
301
        } elseif (!empty($_FILES['photo']['name'])) {
302
            xfgb_upload();
303
            $photo      = str_replace('tmp_', 'msg_', $preview_name);
304
            $photos_dir = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/';
305
            rename($photos_dir . $preview_name, $photos_dir . $photo);
306 View Code Duplication
            if ('' !== $msg->getVar('photo')) {
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...
307
                $filename = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/' . $msg->getVar('photo');
308
                unlink($filename);
309
            }
310
            $msg->setVar('photo', $photo);
311
        }
312
        if (!empty($msgstop)) {
313
            redirect_header('main.php?op=edit&msg_id=' . $msg_id, 2, $msgstop);
314
        }
315
        $uname    = isset($_POST['uname']) ? $_POST['uname'] : '';
316
        $email    = isset($_POST['email']) ? $_POST['email'] : '';
317
        $url      = isset($_POST['url']) ? $_POST['url'] : '';
318
        $title    = isset($_POST['title']) ? $_POST['title'] : '';
319
        $message  = isset($_POST['message']) ? $_POST['message'] : '';
320
        $note     = isset($_POST['note']) ? $_POST['note'] : '';
321
        $gender   = isset($_POST['gender']) ? $_POST['gender'] : '';
322
        $country  = isset($_POST['country']) ? $_POST['country'] : '';
323
        $other    = isset($_POST['other']) ? $_POST['other'] : '';
324
        $moderate = isset($_POST['moderate']) ? (int)$_POST['moderate'] : 0;
325
326
        $msg->setVar('uname', $uname);
327
        $msg->setVar('email', $email);
328
        $msg->setVar('url', $url);
329
        $msg->setVar('title', $title);
330
        $msg->setVar('message', $message);
331
        $msg->setVar('note', $note);
332
        $msg->setVar('gender', $gender);
333
        if ('' !== $country) {
334
            $msg->setVar('country', $country);
335
            $msg->setVar('flagdir', $xoopsModuleConfig['flagdir']);
336
        }
337
        $msg->setVar('other', $other);
338
        $msg->setVar('moderate', $moderate);
339
        if ($msg_handler->insert($msg)) {
340
            redirect_header('main.php?op=show', 1, _AM_XFGB_MSGMOD);
341
        } else {
342
            redirect_header('main.php?op=show', 2, _AM_XFGB_MSGERROR);
343
        }
344
        break;
345
346
    case 'edit':
347
        xoops_cp_header();
348
        $index_admin = new ModuleAdmin();
349
        echo $index_admin->addNavigation(basename(__FILE__));
350
        //xfguestbook_admin_menu(0);
351
        $msg = $msg_handler->get($msg_id);
352
        include_once dirname(__DIR__) . '/include/form_edit.inc.php';
353
        $msg_form->display();
354
        include __DIR__ . '/admin_footer.php';
355
        //xoops_cp_footer();
356
        break;
357
358
    case 'approve':
359
        approve();
360
        break;
361
362
    case 'delete':
363
        delete();
364
        break;
365
366
    case 'banish':
367
        banish();
368
        break;
369
370
    case 'show':
371 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...
372
        xoops_cp_header();
373
        $index_admin = new ModuleAdmin();
374
        echo $index_admin->addNavigation(basename(__FILE__));
375
        //xfguestbook_admin_menu(0);
376
        show();
377
        include __DIR__ . '/admin_footer.php';
378
        //xoops_cp_footer();
379
        break;
380
}
381