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

cp_functions.php ➔ executeSQL()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 65
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
c 3
b 0
f 0
nc 7
nop 1
dl 0
loc 65
rs 6.7651

How to fix   Long Method   

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
// $Id: include/cp_functions.php,v 1.3 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
/**
27
 * @param int    $currentoption
28
 * @param string $breadcrumb
29
 */
30
function xfguestbook_admin_menu($currentoption = 0, $breadcrumb = '')
0 ignored issues
show
Unused Code introduced by
The parameter $breadcrumb 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...
31
{
32
33
    /* Nice buttons styles */
34
    echo "
35
        <style type='text/css'>
36
        #buttontop { float:left; width:100%; background: #e7e7e7; font-size:93%; line-height:normal; border-top: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; margin: 0; }
37
        #buttonbar { float:left; width:100%; background: #e7e7e7 url('" . XOOPS_URL . "/modules/xfguestbook/assets/images/bg.gif') repeat-x left bottom; font-size:93%; line-height:normal; border-left: 1px solid black; border-right: 1px solid black; margin-bottom: 12px; }
38
        #buttonbar ul { margin:0; margin-top: 15px; padding:10px 10px 0; list-style:none; }
39
        #buttonbar li { display:inline; margin:0; padding:0; }
40
        #buttonbar a { float:left; background:url('" . XOOPS_URL . "/modules/xfguestbook/assets/images/left_both.gif') no-repeat left top; margin:0; padding:0 0 0 9px; border-bottom:1px solid #000; text-decoration:none; }
41
        #buttonbar a span { float:left; display:block; background:url('" . XOOPS_URL . "/modules/xfguestbook/assets/images/right_both.gif') no-repeat right top; padding:5px 15px 4px 6px; font-weight:bold; color:#765; }
42
        /* Commented Backslash Hack hides rule from IE5-Mac \*/
43
        #buttonbar a span {float:none;}
44
        /* End IE5-Mac hack */
45
        #buttonbar a:hover span { color:#333; }
46
        #buttonbar #current a { background-position:0 -150px; border-width:0; }
47
        #buttonbar #current a span { background-position:100% -150px; padding-bottom:5px; color:#333; }
48
        #buttonbar a:hover { background-position:0 -150px; }
49
        #buttonbar a:hover span { background-position:100% -150px; }
50
        </style>
51
    ";
52
53
    global $xoopsModule, $xoopsConfig;
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...
54
    $myts = MyTextSanitizer::getInstance();
55
56
    $tblColors                 = array();
57
    $tblColors[0]              = $tblColors[1] = $tblColors[2] = $tblColors[3] = $tblColors[4] = $tblColors[5] = $tblColors[6] = $tblColors[7] = $tblColors[8] = '';
58
    $tblColors[$currentoption] = 'current';
59
//    if (file_exists(XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/modinfo.php')) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
60
//        include_once XOOPS_ROOT_PATH . '/modules/xfguestbook/language/' . $xoopsConfig['language'] . '/modinfo.php';
61
//    } else {
62
//        include_once XOOPS_ROOT_PATH . '/modules/xfguestbook/english/modinfo.php';
63
//    }
64
    xoops_loadLanguage('modinfo', $xoopsModule->getVar('dirname'));
65
66
    echo "<div id='buttontop'>";
67
    echo "<table style=\"width: 100%; padding: 0; \" cellspacing=\"0\"><tr>";
68
    //echo "<td style=\"width: 45%; font-size: 10px; text-align: left; color: #2F5376; padding: 0 6px; line-height: 18px;\"><a class=\"nobutton\" href=\"../../system/admin.php?fct=preferences&amp;op=showmod&amp;mod=" . $xoopsModule->getVar('mid') . "\">" . _AM_SF_OPTS . "</a> | <a href=\"import.php\">" . _AM_SF_IMPORT . "</a> | <a href=\"../index.php\">" . _AM_SF_GOMOD . "</a> | <a href=\"../help/index.html\" target=\"_blank\">" . _AM_SF_HELP . "</a> | <a href=\"about.php\">" . _AM_SF_ABOUT . "</a></td>";
69
    echo "<td style='font-size: 10px; text-align: left; color: #2F5376; padding: 0 6px; line-height: 18px;'>
70
      <a class='nobutton' href='" . XOOPS_URL . '/modules/system/admin.php?fct=preferences&amp;op=showmod&amp;mod=' . $xoopsModule->getVar('mid') . "'>" . _AM_XFGB_GENERALSET . "</a>
71
    | <a href='" . XOOPS_URL . "/modules/xfguestbook/index.php'>" . _AM_XFGB_GOINDEX . "</a>
72
    | <a href='" . XOOPS_URL . "/modules/xfguestbook/admin/flags_install.php'>" . _AM_XFGB_INSTALL_IMG . "</a>
73
    | <a href='" . XOOPS_URL . "/modules/xfguestbook/admin/upgrade.php'>" . _AM_XFGB_UPGRADE . "</a>
74
    | <a href='" . XOOPS_URL . "/modules/xfguestbook/admin/img_manager.php'>" . _AM_XFGB_MSGIMG . '</a>
75
    </td>';
76
    echo "<td style='font-size: 10px; text-align: right; color: #2F5376; padding: 0 6px; line-height: 18px;'><b>" . $myts->displayTarea($xoopsModule->name()) . ' </b> </td>';
77
    echo '</tr></table>';
78
    echo '</div>';
79
80
    echo "<div id='buttonbar'>";
81
    echo '<ul>';
82
    echo "<li id='" . $tblColors[0] . "'><a href=\"" . XOOPS_URL . "/modules/xfguestbook/admin/index.php\"><span>" . _AM_XFGB_MSGMANAGE . '</span></a></li>';
83
    echo "<li id='" . $tblColors[1] . "'><a href=\"" . XOOPS_URL . "/modules/xfguestbook/admin/config.php\"><span>" . _AM_XFGB_FORMOPT . '</span></a></li>';
84
    echo "<li id='" . $tblColors[2] . "'><a href=\"" . XOOPS_URL . "/modules/xfguestbook/admin/country_manager.php\"><span>" . _AM_XFGB_COUNTRYMANAGE . '</span></a></li>';
85
    echo "<li id='" . $tblColors[3] . "'><a href=\"" . XOOPS_URL . "/modules/xfguestbook/admin/ip_manager.php\"><span>" . _AM_XFGB_BADIPSMANAGE . '</span></a></li>';
86
    echo '</ul></div>&nbsp;';
87
}
88
89
/**
90
 * @param $sql_file_path
91
 * @return bool
92
 */
93
function executeSQL($sql_file_path)
0 ignored issues
show
Coding Style introduced by
executeSQL 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...
94
{
95
    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...
96
    $error = false;
97
    // $reservedTables = array('avatar', 'avatar_users_link', 'block_module_link', 'xoopscomments', 'config', 'configcategory', 'configoption', 'image', 'imagebody', 'imagecategory', 'imgset', 'imgset_tplset_link', 'imgsetimg', 'groups','groups_users_link','group_permission', 'online', 'bannerclient', 'banner', 'bannerfinish', 'ranks', 'session', 'smiles', 'users', 'newblocks', 'modules', 'tplfile', 'tplset', 'tplsource', 'xoopsnotifications', 'banner', 'bannerclient', 'bannerfinish');
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% 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...
98
    //   $sql_file_path = XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/sql/".$sqlfile;
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
99
    if (!file_exists($sql_file_path)) {
100
        echo "SQL file not found at <b>$sql_file_path</b><br>";
101
        //     $msg = "SQL file not found at <b>$sql_file_path</b><br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
102
        $error = true;
103
    } else {
104
        echo "SQL file found at <b>$sql_file_path</b>.<br  /> Creating tables...<br>";
105
        //      $msg = "SQL file found at <b>$sql_file_path</b>.<br  /> Creating tables...<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
106
        include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
107
        $sql_query = fread(fopen($sql_file_path, 'r'), filesize($sql_file_path));
108
        $sql_query = trim($sql_query);
109
        SqlUtility::splitMySqlFile($pieces, $sql_query);
0 ignored issues
show
Bug introduced by
The variable $pieces does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
110
        $created_tables = array();
111
        foreach ($pieces as $piece) {
112
            // [0] contains the prefixed query
113
            // [4] contains unprefixed table name
114
            $prefixed_query = SqlUtility::prefixQuery($piece, $GLOBALS['xoopsDB']->prefix());
115
            if (!$prefixed_query) {
116
                //               $msg = "<b>$piece</b> is not a valid SQL!<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
117
                echo "<b>$piece</b> is not a valid SQL!<br>";
118
                $error = true;
119
                break;
120
            }
121
            // check if the table name is reserved
122
            //if (!in_array($prefixed_query[4], $reservedTables)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
123
            // not reserved, so try to create one
124
            if (!$GLOBALS['xoopsDB']->query($prefixed_query[0])) {
125
                //$this->setErrors($GLOBALS['xoopsDB']->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
126
                echo 'erreur<br>';
127
                $error = true;
128
                break;
129
            } else {
130
                if (!in_array($prefixed_query[4], $created_tables)) {
131
                    //                  $msg = '&nbsp;&nbsp;Table <b>'.$GLOBALS['xoopsDB']->prefix($prefixed_query[4]).'</b> created.<br>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
132
                    echo '&nbsp;&nbsp;Table <b>' . $GLOBALS['xoopsDB']->prefix($prefixed_query[4]) . '</b> created.<br>';
133
                    $created_tables[] = $prefixed_query[4];
134
                } else {
135
                    echo '&nbsp;&nbsp;Data inserted to table <b>' . $GLOBALS['xoopsDB']->prefix($prefixed_query[4]) . '</b>.<br>';
136
                    //                  $msg = '&nbsp;&nbsp;Data inserted to table <b>'.$GLOBALS['xoopsDB']->prefix($prefixed_query[4]).'</b>.<br>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
137
                }
138
            }
139
            //      } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
140
            //          the table name is reserved, so halt the installation
141
            //          $this->setErrors('<b>'.$prefixed_query[4]."</b> is a reserved table!");
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% 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...
142
            //          $error = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
143
            //          break;
144
            //      }
145
        }
146
        //      if there was an error, delete the tables created so far, so the next installation will not fail
147
        if ($error === true) {
148
            foreach ($created_tables as $ct) {
149
                //echo $ct;
150
                $GLOBALS['xoopsDB']->query('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix($ct));
151
            }
152
        }
153
    }
154
155
    //}
156
    return $error;
157
}
158