Passed
Push — master ( 81ba93...c6c854 )
by Michael
03:30
created

myblocksadmin_update_block()   C

Complexity

Conditions 16
Paths 141

Size

Total Lines 83
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 83
rs 5.225
cc 16
nc 141
nop 10

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
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 https://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') || die('Restricted access');
27
28
if (!is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid())) {
29
    exit('Access Denied');
30
}
31
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
32
require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blocksadmin.php';
33
34
$op = 'list';
35
if (isset($_POST)) {
36
    foreach ($_POST as $k => $v) {
37
        ${$k} = $v;
38
    }
39
}
40
41
if (\Xmf\Request::hasVar('op', 'GET')) {
42
    if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op']
43
        || 'clone' === $_GET['op']
44
        || 'previewpopup' === $_GET['op']) {
45
        $op  = $_GET['op'];
46
        $bid = \Xmf\Request::getInt('bid', 0, 'GET');
47
    }
48
}
49
50
if (isset($previewblock)) {
51
    xoops_cp_header();
52
    require_once XOOPS_ROOT_PATH . '/class/template.php';
53
    $xoopsTpl          = new \XoopsTpl();
54
    $xoopsTpl->caching = 0;
55
    if (isset($bid)) {
56
        $block['bid']        = $bid;
57
        $block['form_title'] = _AM_EDITBLOCK;
0 ignored issues
show
Bug introduced by
The constant _AM_EDITBLOCK was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
58
        $myblock             = new \XoopsBlock($bid);
59
        $block['name']       = $myblock->getVar('name');
60
    } else {
61
        if ('save' === $op) {
62
            $block['form_title'] = _AM_ADDBLOCK;
63
        } else {
64
            $block['form_title'] = _AM_CLONEBLOCK;
0 ignored issues
show
Bug introduced by
The constant _AM_CLONEBLOCK was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
        }
66
        $myblock = new \XoopsBlock();
67
        $myblock->setVar('block_type', 'C');
68
    }
69
    $myts = \MyTextSanitizer::getInstance();
70
    $myblock->setVar('title', $myts->stripSlashesGPC($btitle));
71
    $myblock->setVar('content', $myts->stripSlashesGPC($bcontent));
72
    $dummyhtml = '<html><head><meta http-equiv="content-type" content="text/html; charset='
73
                 . _CHARSET
74
                 . '"><meta http-equiv="content-language" content="'
75
                 . _LANGCODE
76
                 . '"><title>'
77
                 . $xoopsConfig['sitename']
78
                 . '</title><link rel="stylesheet" type="text/css" media="all" href="'
79
                 . getcss($xoopsConfig['theme_set'])
80
                 . '"></head><body><table><tr><th>'
81
                 . $myblock->getVar('title')
82
                 . '</th></tr><tr><td>'
83
                 . $myblock->getContent('S', $bctype)
84
                 . '</td></tr></table></body></html>';
85
86
    $dummyfile = '_dummyfile_' . time() . '.html';
87
    $fp        = fopen(XOOPS_CACHE_PATH . '/' . $dummyfile, 'w');
88
    fwrite($fp, $dummyhtml);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
    fwrite(/** @scrutinizer ignore-type */ $fp, $dummyhtml);
Loading history...
89
    fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
    fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
90
    $block['edit_form'] = false;
91
    $block['template']  = '';
92
    $block['op']        = $op;
93
    $block['side']      = $bside;
94
    $block['weight']    = $bweight;
95
    $block['visible']   = $bvisible;
96
    $block['title']     = $myblock->getVar('title', 'E');
97
    $block['content']   = $myblock->getVar('content', 'E');
98
    $block['modules']   = &$bmodule;
99
    $block['ctype']     = isset($bctype) ? $bctype : $myblock->getVar('c_type');
100
    $block['is_custom'] = true;
101
    $block['cachetime'] = (int)$bcachetime;
102
    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>';
103
    require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blockform.php';
104
    $form->display();
105
    xoops_cp_footer();
106
    echo '<script type="text/javascript">
107
    <!--//
108
    preview_window = openWithSelfMain("' . XOOPS_URL . '/modules/system/admin.php?fct=blocksadmin&op=previewpopup&file=' . $dummyfile . '", "popup", 250, 200);
109
    //-->
110
    </script>';
111
    exit();
112
}
113
114
if ('previewpopup' === $op) {
115
    $file = str_replace('..', '', XOOPS_CACHE_PATH . '/' . trim($_GET['file']));
116
    if (file_exists($file)) {
117
        require_once $file;
118
        @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

118
        /** @scrutinizer ignore-unhandled */ @unlink($file);

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...
119
    }
120
    exit();
121
}
122
123
if ('list' === $op) {
124
    xoops_cp_header();
125
    list_blocks();
126
    xoops_cp_footer();
127
    exit();
128
}
129
130
if ('order' === $op) {
131
    foreach (array_keys($bid) as $i) {
132
        if ($side[$i] < 0) {
133
            $visible[$i] = 0;
134
            $side[$i]    = -1;
135
        } else {
136
            $visible[$i] = 1;
137
        }
138
139
        $bmodule[$i] = (isset($bmodule[$i]) && is_array($bmodule[$i])) ? $bmodule[$i] : [-1];
140
141
        myblocksadmin_update_block($i, $side[$i], $weight[$i], $visible[$i], $title[$i], '', '', $bcachetime[$i], $bmodule[$i], []);
142
    }
143
144
    redirect_header('myblocksadmin.php', 1, _AM_DBUPDATED);
145
}
146
147
if ('save' === $op) {
148
    save_block($bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bmodule, $bcachetime);
0 ignored issues
show
Bug introduced by
The function save_block was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
    /** @scrutinizer ignore-call */ 
149
    save_block($bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bmodule, $bcachetime);
Loading history...
149
    exit();
150
}
151
152
if ('update' === $op) {
153
    $bcachetime = isset($bcachetime) ? (int)$bcachetime : 0;
154
    $options    = isset($options) ? $options : [];
155
    $bcontent   = isset($bcontent) ? $bcontent : '';
156
    $bctype     = isset($bctype) ? $bctype : '';
157
    $bmodule    = (isset($bmodule) && is_array($bmodule)) ? $bmodule : [-1]; // GIJ +
158
    $msg        = myblocksadmin_update_block($bid, $bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bcachetime, $bmodule, $options); // GIJ c
159
    redirect_header('myblocksadmin.php', 1, $msg); // GIJ +
160
}
161
162
if ('delete_ok' === $op) {
163
    delete_block_ok($bid);
0 ignored issues
show
Bug introduced by
The function delete_block_ok was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
    /** @scrutinizer ignore-call */ 
164
    delete_block_ok($bid);
Loading history...
164
    exit();
165
}
166
167
if ('delete' === $op) {
168
    xoops_cp_header();
169
    delete_block($bid);
0 ignored issues
show
Bug introduced by
The function delete_block was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
    /** @scrutinizer ignore-call */ 
170
    delete_block($bid);
Loading history...
170
    xoops_cp_footer();
171
    exit();
172
}
173
174
if ('edit' === $op) {
175
    xoops_cp_header();
176
    edit_block($bid);
0 ignored issues
show
Bug introduced by
The function edit_block was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
    /** @scrutinizer ignore-call */ 
177
    edit_block($bid);
Loading history...
177
    xoops_cp_footer();
178
    exit();
179
}
180
181
// import from modules/system/admin/blocksadmin/blocksadmin.php
182
/**
183
 * @param         $bid
184
 * @param         $bside
185
 * @param         $bweight
186
 * @param         $bvisible
187
 * @param         $btitle
188
 * @param         $bcontent
189
 * @param         $bctype
190
 * @param         $bcachetime
191
 * @param         $bmodule
192
 * @param  array  $options
193
 * @return string
194
 */
195
function myblocksadmin_update_block(
196
    $bid,
197
    $bside,
198
    $bweight,
199
    $bvisible,
200
    $btitle,
201
    $bcontent,
202
    $bctype,
203
    $bcachetime,
204
    $bmodule,
205
    $options = [])
206
{
207
    global $xoopsConfig;
208
    if (empty($bmodule)) {
209
        xoops_cp_header();
210
        xoops_error(sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
211
        xoops_cp_footer();
212
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
213
    }
214
    $myblock = new \XoopsBlock($bid);
215
    // $myblock->setVar('side', $bside); GIJ -
216
    if ($bside >= 0) {
217
        $myblock->setVar('side', $bside);
218
    } // GIJ +
219
    $myblock->setVar('weight', $bweight);
220
    $myblock->setVar('visible', $bvisible);
221
    $myblock->setVar('title', $btitle);
222
    $myblock->setVar('content', $bcontent);
223
    $myblock->setVar('bcachetime', $bcachetime);
224
    if (isset($options) && (count($options) > 0)) {
225
        $options = implode('|', $options);
226
        $myblock->setVar('options', $options);
227
    }
228
    if ('C' === $myblock->getVar('block_type')) {
229
        switch ($bctype) {
230
            case 'H':
231
                $name = _AM_CUSTOMHTML;
0 ignored issues
show
Bug introduced by
The constant _AM_CUSTOMHTML was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
232
                break;
233
            case 'P':
234
                $name = _AM_CUSTOMPHP;
0 ignored issues
show
Bug introduced by
The constant _AM_CUSTOMPHP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
235
                break;
236
            case 'S':
237
                $name = _AM_CUSTOMSMILE;
0 ignored issues
show
Bug introduced by
The constant _AM_CUSTOMSMILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
238
                break;
239
            default:
240
                $name = _AM_CUSTOMNOSMILE;
0 ignored issues
show
Bug introduced by
The constant _AM_CUSTOMNOSMILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
241
                break;
242
        }
243
        $myblock->setVar('name', $name);
244
        $myblock->setVar('c_type', $bctype);
245
    } else {
246
        $myblock->setVar('c_type', 'H');
247
    }
248
    $msg = _AM_DBUPDATED;
249
    if (false !== $myblock->store()) {
0 ignored issues
show
Bug introduced by
The method store() does not exist on XoopsBlock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

249
    if (false !== $myblock->/** @scrutinizer ignore-call */ store()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
250
        $db  = \XoopsDatabaseFactory::getDatabaseConnection();
251
        $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $db->prefix('block_module_link'), $bid);
252
        $db->query($sql);
253
        foreach ($bmodule as $bmid) {
254
            $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (`%u`, `%d`)', $db->prefix('block_module_link'), $bid, (int)$bmid);
255
            $db->query($sql);
256
        }
257
        require_once XOOPS_ROOT_PATH . '/class/template.php';
258
        $xoopsTpl          = new \XoopsTpl();
259
        $xoopsTpl->caching = 2;
260
        if ('' != $myblock->getVar('template')) {
261
            if ($xoopsTpl->is_cached('db:' . $myblock->getVar('template'))) {
262
                if (!$xoopsTpl->clear_cache('db:' . $myblock->getVar('template'))) {
263
                    $msg = 'Unable to clear cache for block ID' . $bid;
264
                }
265
            }
266
        } else {
267
            if ($xoopsTpl->is_cached('db:system_dummy.tpl', 'block' . $bid)) {
268
                if (!$xoopsTpl->clear_cache('db:system_dummy.tpl', 'block' . $bid)) {
269
                    $msg = 'Unable to clear cache for block ID' . $bid;
270
                }
271
            }
272
        }
273
    } else {
274
        $msg = 'Failed update of block. ID:' . $bid;
275
    }
276
277
    return $msg; // GIJ +
278
}
279