Completed
Branch master (c92e39)
by Michael
02:32
created

blocksadmin.inc.php ➔ myblocksadmin_update_block()   D

Complexity

Conditions 16
Paths 141

Size

Total Lines 84
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 68
nc 141
nop 10
dl 0
loc 84
rs 4.5663
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 196 and the first side effect is on line 29.

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
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project http://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
/**
21
 * Module: SmartFAQ
22
 * Author: The SmartFactory <www.smartfactory.ca>
23
 * Licence: GNU
24
 */
25
26
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
27
28
if (!is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid())) {
29
    exit('Access Denied');
30
}
31
include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
32
include XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blocksadmin.php';
33
34
$op = 'list';
35
if (isset($HTTP_POST_VARS)) {
36
    foreach ($HTTP_POST_VARS as $k => $v) {
37
        $$k = $v;
38
    }
39
}
40
41
if (isset($HTTP_GET_VARS['op'])) {
42
    if ($HTTP_GET_VARS['op'] === 'edit' || $HTTP_GET_VARS['op'] === 'delete' || $HTTP_GET_VARS['op'] === 'delete_ok'
43
        || $HTTP_GET_VARS['op'] === 'clone'
44
        || $HTTP_GET_VARS['op'] === 'previewpopup'
45
    ) {
46
        $op  = $HTTP_GET_VARS['op'];
47
        $bid = isset($HTTP_GET_VARS['bid']) ? (int)$HTTP_GET_VARS['bid'] : 0;
48
    }
49
}
50
51
if (isset($previewblock)) {
52
    xoops_cp_header();
53
    include_once XOOPS_ROOT_PATH . '/class/template.php';
54
    $xoopsTpl = new XoopsTpl();
55
    $xoopsTpl->xoops_setCaching(0);
56
    if (isset($bid)) {
57
        $block['bid']        = $bid;
58
        $block['form_title'] = _AM_EDITBLOCK;
59
        $myblock             = new XoopsBlock($bid);
60
        $block['name']       = $myblock->getVar('name');
61
    } else {
62
        if ($op === 'save') {
63
            $block['form_title'] = _AM_ADDBLOCK;
64
        } else {
65
            $block['form_title'] = _AM_CLONEBLOCK;
66
        }
67
        $myblock = new XoopsBlock();
68
        $myblock->setVar('block_type', 'C');
69
    }
70
    $myts = MyTextSanitizer::getInstance();
71
    $myblock->setVar('title', $myts->stripSlashesGPC($btitle));
72
    $myblock->setVar('content', $myts->stripSlashesGPC($bcontent));
73
    $dummyhtml = '<html><head><meta http-equiv="content-type" content="text/html; charset='
74
                 . _CHARSET
75
                 . '" /><meta http-equiv="content-language" content="'
76
                 . _LANGCODE
77
                 . '" /><title>'
78
                 . $xoopsConfig['sitename']
79
                 . '</title><link rel="stylesheet" type="text/css" media="all" href="'
80
                 . getcss($xoopsConfig['theme_set'])
81
                 . '" /></head><body><table><tr><th>'
82
                 . $myblock->getVar('title')
83
                 . '</th></tr><tr><td>'
84
                 . $myblock->getContent('S', $bctype)
85
                 . '</td></tr></table></body></html>';
86
87
    $dummyfile = '_dummyfile_' . time() . '.html';
88
    $fp        = fopen(XOOPS_CACHE_PATH . '/' . $dummyfile, 'w');
89
    fwrite($fp, $dummyhtml);
90
    fclose($fp);
91
    $block['edit_form'] = false;
92
    $block['template']  = '';
93
    $block['op']        = $op;
94
    $block['side']      = $bside;
95
    $block['weight']    = $bweight;
96
    $block['visible']   = $bvisible;
97
    $block['title']     = $myblock->getVar('title', 'E');
98
    $block['content']   = $myblock->getVar('content', 'E');
99
    $block['modules']   =& $bmodule;
100
    $block['ctype']     = isset($bctype) ? $bctype : $myblock->getVar('c_type');
101
    $block['is_custom'] = true;
102
    $block['cachetime'] = (int)$bcachetime;
103
    echo '<a href="admin.php?fct=blocksadmin">' . _AM_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . $block['form_title'] . '<br><br>';
104
    include XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blockform.php';
105
    $form->display();
106
    xoops_cp_footer();
107
    echo '<script type="text/javascript">
108
    <!--//
109
    preview_window = openWithSelfMain("' . XOOPS_URL . '/modules/system/admin.php?fct=blocksadmin&op=previewpopup&file=' . $dummyfile . '", "popup", 250, 200);
110
    //-->
111
    </script>';
112
    exit();
113
}
114
115
if ($op === 'previewpopup') {
116
    $file = str_replace('..', '', XOOPS_CACHE_PATH . '/' . trim($HTTP_GET_VARS['file']));
117
    if (file_exists($file)) {
118
        include $file;
119
        @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
120
    }
121
    exit();
122
}
123
124
if ($op === 'list') {
125
    xoops_cp_header();
126
    list_blocks();
127
    xoops_cp_footer();
128
    exit();
129
}
130
131
if ($op === 'order') {
132
    foreach (array_keys($bid) as $i) {
133
        if ($side[$i] < 0) {
134
            $visible[$i] = 0;
135
            $side[$i]    = -1;
136
        } else {
137
            $visible[$i] = 1;
138
        }
139
140
        $bmodule[$i] = (isset($bmodule[$i]) && is_array($bmodule[$i])) ? $bmodule[$i] : array(-1);
141
142
        myblocksadmin_update_block($i, $side[$i], $weight[$i], $visible[$i], $title[$i], '', '', $bcachetime[$i], $bmodule[$i], array());
143
    }
144
145
    redirect_header('myblocksadmin.php', 1, _AM_DBUPDATED);
146
}
147
148
if ($op === 'save') {
149
    save_block($bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bmodule, $bcachetime);
150
    exit();
151
}
152
153
if ($op === 'update') {
154
    $bcachetime = isset($bcachetime) ? (int)$bcachetime : 0;
155
    $options    = isset($options) ? $options : array();
156
    $bcontent   = isset($bcontent) ? $bcontent : '';
157
    $bctype     = isset($bctype) ? $bctype : '';
158
    $bmodule    = (isset($bmodule) && is_array($bmodule)) ? $bmodule : array(-1); // GIJ +
159
    $msg        = myblocksadmin_update_block($bid, $bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bcachetime, $bmodule, $options); // GIJ c
160
    redirect_header('myblocksadmin.php', 1, $msg); // GIJ +
161
}
162
163
if ($op === 'delete_ok') {
164
    delete_block_ok($bid);
165
    exit();
166
}
167
168
if ($op === 'delete') {
169
    xoops_cp_header();
170
    delete_block($bid);
171
    xoops_cp_footer();
172
    exit();
173
}
174
175
if ($op === 'edit') {
176
    xoops_cp_header();
177
    edit_block($bid);
178
    xoops_cp_footer();
179
    exit();
180
}
181
182
// import from modules/system/admin/blocksadmin/blocksadmin.php
183
/**
184
 * @param         $bid
185
 * @param         $bside
186
 * @param         $bweight
187
 * @param         $bvisible
188
 * @param         $btitle
189
 * @param         $bcontent
190
 * @param         $bctype
191
 * @param         $bcachetime
192
 * @param         $bmodule
193
 * @param  array  $options
194
 * @return string
195
 */
196
function myblocksadmin_update_block(
197
    $bid,
198
    $bside,
199
    $bweight,
200
    $bvisible,
201
    $btitle,
202
    $bcontent,
203
    $bctype,
204
    $bcachetime,
205
    $bmodule,
206
    $options = array()
207
) {
208
    global $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...
209
    if (empty($bmodule)) {
210
        xoops_cp_header();
211
        xoops_error(sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
212
        xoops_cp_footer();
213
        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function myblocksadmin_update_block() 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...
214
    }
215
    $myblock = new XoopsBlock($bid);
216
    // $myblock->setVar('side', $bside); GIJ -
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
217
    if ($bside >= 0) {
218
        $myblock->setVar('side', $bside);
219
    } // GIJ +
220
    $myblock->setVar('weight', $bweight);
221
    $myblock->setVar('visible', $bvisible);
222
    $myblock->setVar('title', $btitle);
223
    $myblock->setVar('content', $bcontent);
224
    $myblock->setVar('bcachetime', $bcachetime);
225
    if (isset($options) && (count($options) > 0)) {
226
        $options = implode('|', $options);
227
        $myblock->setVar('options', $options);
228
    }
229
    if ($myblock->getVar('block_type') === 'C') {
230
        switch ($bctype) {
231
            case 'H':
232
                $name = _AM_CUSTOMHTML;
233
                break;
234
            case 'P':
235
                $name = _AM_CUSTOMPHP;
236
                break;
237
            case 'S':
238
                $name = _AM_CUSTOMSMILE;
239
                break;
240
            default:
241
                $name = _AM_CUSTOMNOSMILE;
242
                break;
243
        }
244
        $myblock->setVar('name', $name);
245
        $myblock->setVar('c_type', $bctype);
246
    } else {
247
        $myblock->setVar('c_type', 'H');
248
    }
249
    $msg = _AM_DBUPDATED;
250
    if ($myblock->store() !== false) {
251
        $db  = XoopsDatabaseFactory::getDatabaseConnection();
252
        $sql = sprintf('DELETE FROM %s WHERE block_id = %u', $db->prefix('block_module_link'), $bid);
253
        $db->query($sql);
254
        foreach ($bmodule as $bmid) {
255
            $sql = sprintf('INSERT INTO %s (block_id, module_id) VALUES (%u, %d)', $db->prefix('block_module_link'), $bid, (int)$bmid);
256
            $db->query($sql);
257
        }
258
        include_once XOOPS_ROOT_PATH . '/class/template.php';
259
        $xoopsTpl = new XoopsTpl();
260
        $xoopsTpl->xoops_setCaching(2);
261
        if ($myblock->getVar('template') != '') {
262
            if ($xoopsTpl->is_cached('db:' . $myblock->getVar('template'))) {
263
                if (!$xoopsTpl->clear_cache('db:' . $myblock->getVar('template'))) {
264
                    $msg = 'Unable to clear cache for block ID' . $bid;
265
                }
266
            }
267
        } else {
268
            if ($xoopsTpl->is_cached('db:system_dummy.tpl', 'block' . $bid)) {
269
                if (!$xoopsTpl->clear_cache('db:system_dummy.tpl', 'block' . $bid)) {
270
                    $msg = 'Unable to clear cache for block ID' . $bid;
271
                }
272
            }
273
        }
274
    } else {
275
        $msg = 'Failed update of block. ID:' . $bid;
276
    }
277
278
    return $msg; // GIJ +
279
}
280