functions.php ➔ bookshop_get_module_option()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
//  ------------------------------------------------------------------------ //
3
//                      BOOKSHOP - MODULE FOR XOOPS 2                        //
4
//                  Copyright (c) 2007, 2008 Instant Zero                    //
5
//                     <http://www.instant-zero.com/>                        //
6
// ------------------------------------------------------------------------- //
7
//  This program is free software; you can redistribute it and/or modify     //
8
//  it under the terms of the GNU General Public License as published by     //
9
//  the Free Software Foundation; either version 2 of the License, or        //
10
//  (at your option) any later version.                                      //
11
//                                                                           //
12
//  You may not change or alter any portion of this comment or credits       //
13
//  of supporting developers from this source code or any supporting         //
14
//  source code which is considered copyrighted (c) material of the          //
15
//  original comment or credit authors.                                      //
16
//                                                                           //
17
//  This program is distributed in the hope that it will be useful,          //
18
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
19
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
20
//  GNU General Public License for more details.                             //
21
//                                                                           //
22
//  You should have received a copy of the GNU General Public License        //
23
//  along with this program; if not, write to the Free Software              //
24
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
25
//  ------------------------------------------------------------------------ //
26
27
/**
28
 * @param int    $currentoption
29
 * @param string $breadcrumb
30
 */
31
function bookshop_adminMenu($currentoption = 0, $breadcrumb = '')
32
{
33
    global $xoopsConfig, $xoopsModule;
34 View Code Duplication
    if (file_exists(XOOPS_ROOT_PATH . '/modules/bookshop/language/' . $xoopsConfig['language'] . '/modinfo.php')) {
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...
35
        include_once XOOPS_ROOT_PATH . '/modules/bookshop/language/' . $xoopsConfig['language'] . '/modinfo.php';
36
    } else {
37
        include_once XOOPS_ROOT_PATH . '/modules/bookshop/language/english/modinfo.php';
38
    }
39
    global $adminmenu;
40
    include __DIR__ . '/menu.php';
41
42
    echo "<style type=\"text/css\">\n";
43
    echo "#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; }\n";
44
    echo "#buttonbar { float:left; width:100%; background: #e7e7e7 url('../assets/images/modadminbg.gif') repeat-x left bottom; font-size:93%; line-height:normal; border-left: 1px solid black; border-right: 1px solid black; margin-bottom: 12px; }\n";
45
    echo "#buttonbar ul { margin:0; margin-top: 15px; padding:10px 10px 0; list-style:none; }\n";
46
    echo '#buttonbar li { display:inline; margin:0; padding:0; }';
47
    echo "#buttonbar a { float:left; background:url('../assets/images/left_both.gif') no-repeat left top; margin:0; padding:0 0 0 9px; border-bottom:1px solid #000; text-decoration:none; }\n";
48
    echo "#buttonbar a span { float:left; display:block; background:url('../assets/images/right_both.gif') no-repeat right top; padding:5px 15px 4px 6px; font-weight:bold; color:#765; }\n";
49
    echo "/* Commented Backslash Hack hides rule from IE5-Mac \*/\n";
50
    echo "#buttonbar a span {float:none;}\n";
51
    echo "/* End IE5-Mac hack */\n";
52
    echo "#buttonbar a:hover span { color:#333; }\n";
53
    echo "#buttonbar .current a { background-position:0 -150px; border-width:0; }\n";
54
    echo "#buttonbar .current a span { background-position:100% -150px; padding-bottom:5px; color:#333; }\n";
55
    echo "#buttonbar a:hover { background-position:0% -150px; }\n";
56
    echo "#buttonbar a:hover span { background-position:100% -150px; }\n";
57
    echo "</style>\n";
58
59
    echo "<div id=\"buttontop\">\n";
60
    echo "<table style=\"width: 100%; padding: 0; \" cellspacing=\"0\">\n";
61
    echo "<tr>\n";
62
    echo "<td style=\"width: 70%; font-size: 10px; text-align: left; color: #2F5376; padding: 0 6px; line-height: 18px;\">\n";
63
    echo "<a href=\"../index.php\">" . _AM_BOOKSHOP_GO_TO_MODULE . "</a> | <a href=\"" . XOOPS_URL . '/modules/system/admin.php?fct=preferences&amp;op=showmod&amp;mod=' . $xoopsModule->getVar('mid') . "\">" . _AM_BOOKSHOP_PREFERENCES . "</a>\n";
64
    echo "</td>\n";
65
    echo "<td style=\"width: 30%; font-size: 10px; text-align: right; color: #2F5376; padding: 0 6px; line-height: 18px;\">\n";
66
    echo '<b>' . $xoopsModule->getVar('name') . '&nbsp;' . _AM_BOOKSHOP_ADMINISTRATION . '</b>&nbsp;' . $breadcrumb . "\n";
67
    echo "</td>\n";
68
    echo "</tr>\n";
69
    echo "</table>\n";
70
    echo "</div>\n";
71
    echo "<div id=\"buttonbar\">\n";
72
    echo "<ul>\n";
73
    foreach ($GLOBALS['adminmenu'] as $key => $link) {
74
        if ($key == $currentoption) {
75
            echo "<li class=\"current\">\n";
76
        } else {
77
            echo "<li>\n";
78
        }
79
        echo "<a href=\"" . XOOPS_URL . '/modules/bookshop/' . $link['link'] . "\"><span>" . $link['title'] . "</span></a>\n";
80
        echo "</li>\n";
81
    }
82
    echo "</ul>\n";
83
    echo "</div>\n";
84
    echo "<br style=\"clear:both;\" />\n";
85
}
86
87
/**
88
 * Internal function
89
 */
90
function bookshop_get_mid()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
91
{
92
    global $xoopsModule;
93
94
    return $xoopsModule->getVar('mid');
95
}
96
97
/**
98
 * Internal function
99
 */
100
function bookshop_get_config_handler()
101
{
102
    $config_handler = null;
0 ignored issues
show
Unused Code introduced by
$config_handler 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...
103
    $config_handler = xoops_getHandler('config');
104
    if (!is_object($config_handler)) {
105
        trigger_error('Error, unable to get and handler on the Config object');
106
        exit;
107
    } else {
108
        return $config_handler;
109
    }
110
}
111
112
/**
113
 * Returns a module option
114
 *
115
 * @param string $option_name
116
 * @param bool   $as_object
117
 *
118
 * @return mixed
119
 */
120
function bookshop_get_module_option($option_name = '', $as_object = false)
121
{
122
    $tbl_options    = array();
0 ignored issues
show
Unused Code introduced by
$tbl_options 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...
123
    $mid            = bookshop_get_mid();
124
    $config_handler = bookshop_get_config_handler();
125
    $critere        = new CriteriaCompo();
126
    $critere->add(new Criteria('conf_modid', $mid, '='));
127
    $critere->add(new Criteria('conf_name', $option_name, '='));
128
    $tbl_options = $config_handler->getConfigs($critere, false, false);
129
    if (count($tbl_options) > 0) {
130
        $option = $tbl_options[0];
131
        if (!$as_object) {
132
            return $option->getVar('conf_value');
133
        } else {
134
            return $option;
135
        }
136
    }
137
}
138
139
/**
140
 * Set a module's option
141
 * @param string $option_name
142
 * @param string $option_value
143
 * @return mixed
144
 */
145
function bookshop_set_module_option($option_name = '', $option_value = '')
146
{
147
    $config_handler = bookshop_get_config_handler();
148
    $option         = bookshop_get_module_option($option_name, true);
149
    $option->setVar('conf_value', $option_value);
150
    $retval = $config_handler->insertConfig($option, true);
151
152
    return $retval;
153
}
154
155
/**
156
 * Create a unique upload filename
157
 *
158
 * @package          Bookshop
159
 * @author           Instant Zero http://www.instant-zero.com
160
 * @copyright    (c) Instant Zero http://www.instant-zero.com
161
 * @param      $folder
162
 * @param      $filename
163
 * @param bool $trimname
164
 * @return string
165
 */
166
167
function bookshop_createUploadName($folder, $filename, $trimname = false)
168
{
169
    $workingfolder = $folder;
170
    if (xoops_substr($workingfolder, strlen($workingfolder) - 1, 1) !== '/') {
171
        $workingfolder .= '/';
172
    }
173
    $ext  = basename($filename);
174
    $ext  = explode('.', $ext);
175
    $ext  = '.' . $ext[count($ext) - 1];
176
    $true = true;
177
    while ($true) {
178
        $ipbits = explode('.', $_SERVER['REMOTE_ADDR']);
179
        list($usec, $sec) = explode(' ', microtime());
180
        $usec = (integer)($usec * 65536);
181
        $sec  = ((integer)$sec) & 0xFFFF;
182
183
        if ($trimname) {
184
            $uid = sprintf('%06x%04x%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec);
185
        } else {
186
            $uid = sprintf('%08x-%04x-%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec);
187
        }
188
        if (!file_exists($workingfolder . $uid . $ext)) {
189
            $true = false;
190
        }
191
    }
192
193
    return $uid . $ext;
0 ignored issues
show
Bug introduced by
The variable $uid 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
}
195
196
/**
197
 * @param string $title
198
 * @param int    $level
199
 */
200
function bookshop_htitle($title = '', $level = 1)
201
{
202
    printf('<h%01d>%s</h%01d>', $level, $title, $level);
203
}
204
205
/**
206
 * Verify that a field exists inside a mysql table
207
 * @param $fieldname
208
 * @param $table
209
 * @return bool
210
 */
211
function booksop_FieldExists($fieldname, $table)
212
{
213
    global $xoopsDB;
214
    $result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'");
215
216
    return ($xoopsDB->getRowsNum($result) > 0);
217
}
218
219
/**
220
 * Add a field to a mysql table
221
 * @param $field
222
 * @param $table
223
 * @return resource
224
 */
225
function booksop_AddField($field, $table)
226
{
227
    global $xoopsDB;
228
    $result = $xoopsDB->queryF('ALTER TABLE ' . $table . " ADD $field;");
229
230
    return $result;
231
}
232