myblocksadmin_update_block()   C
last analyzed

Complexity

Conditions 16
Paths 141

Size

Total Lines 83
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

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

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 declare(strict_types=1);
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.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
16
 */
17
18
use Xmf\Request;
19
20
if (!is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid())) {
21
    exit('Access Denied');
22
}
23
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
24
require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blocksadmin.php';
25
26
$moduleDirName      = \basename(\dirname(__DIR__));
27
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
28
29
$op = 'list';
30
if (isset($_POST)) {
31
    foreach ($_POST as $k => $v) {
32
        ${$k} = $v;
33
    }
34
}
35
36
if (Request::hasVar('op', 'GET')) {
37
    if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op']
38
        || 'clone' === $_GET['op']
39
        || 'previewpopup' === $_GET['op']) {
40
        $op  = $_GET['op'];
41
        $bid = Request::getInt('bid', 0, 'GET');
42
    }
43
}
44
45
if (isset($previewblock)) {
46
    xoops_cp_header();
47
    require_once XOOPS_ROOT_PATH . '/class/template.php';
48
    $xoopsTpl          = new \XoopsTpl();
49
    $xoopsTpl->caching = 0;
50
    if (isset($bid)) {
51
        $block['bid']        = $bid;
52
        $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...
53
        $myblock             = new \XoopsBlock($bid);
54
        $block['name']       = $myblock->getVar('name');
55
    } else {
56
        if ('save' === $op) {
57
            $block['form_title'] = _AM_ADDBLOCK;
58
        } else {
59
            $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...
60
        }
61
        $myblock = new \XoopsBlock();
62
        $myblock->setVar('block_type', 'C');
63
    }
64
    $myts = \MyTextSanitizer::getInstance();
65
    $myblock->setVar('title', ($btitle));
66
    $myblock->setVar('content', ($bcontent));
67
    $dummyhtml = '<html><head><meta http-equiv="content-type" content="text/html; charset=' . _CHARSET . '"><meta http-equiv="content-language" content="' . _LANGCODE . '"><title>' . $xoopsConfig['sitename'] . '</title><link rel="stylesheet" type="text/css" media="all" href="' . getcss(
68
            $xoopsConfig['theme_set']
69
        ) . '"></head><body><table><tr><th>' . $myblock->getVar('title') . '</th></tr><tr><td>' . $myblock->getContent('S', $bctype) . '</td></tr></table></body></html>';
70
71
    $dummyfile = '_dummyfile_' . time() . '.html';
72
    $fp        = fopen(XOOPS_CACHE_PATH . '/' . $dummyfile, 'wb');
73
    fwrite($fp, $dummyhtml);
74
    fclose($fp);
75
    $block['edit_form'] = false;
76
    $block['template']  = '';
77
    $block['op']        = $op;
78
    $block['side']      = $bside;
79
    $block['weight']    = $bweight;
80
    $block['visible']   = $bvisible;
81
    $block['title']     = $myblock->getVar('title', 'E');
82
    $block['content']   = $myblock->getVar('content', 'E');
83
    $block['modules']   = &$bmodule;
84
    $block['ctype']     = $bctype ?? $myblock->getVar('c_type');
85
    $block['is_custom'] = true;
86
    $block['cachetime'] = (int)$bcachetime;
87
    echo '<a href="admin.php?fct=blocksadmin">' . constant('CO_' . $moduleDirNameUpper . '_' . 'BADMIN') . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . $block['form_title'] . '<br><br>';
88
    require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blockform.php';
89
    $form->display();
90
    xoops_cp_footer();
91
    echo '<script type="text/javascript">
92
    <!--//
93
    preview_window = openWithSelfMain("' . XOOPS_URL . '/modules/system/admin.php?fct=blocksadmin&op=previewpopup&file=' . $dummyfile . '", "popup", 250, 200);
94
    //-->
95
    </script>';
96
    exit();
97
}
98
99
if ('previewpopup' === $op) {
100
    $file = str_replace('..', '', XOOPS_CACHE_PATH . '/' . trim($_GET['file']));
101
    if (file_exists($file)) {
102
        require_once $file;
103
        @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

103
        /** @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...
104
    }
105
    exit();
106
}
107
108
if ('list' === $op) {
109
    xoops_cp_header();
110
    list_blocks();
0 ignored issues
show
Bug introduced by
The function list_blocks 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

110
    /** @scrutinizer ignore-call */ 
111
    list_blocks();
Loading history...
111
    xoops_cp_footer();
112
    exit();
113
}
114
115
if ('order' === $op) {
116
    foreach (array_keys($bid) as $i) {
117
        if ($side[$i] < 0) {
118
            $visible[$i] = 0;
119
            $side[$i]    = -1;
120
        } else {
121
            $visible[$i] = 1;
122
        }
123
124
        $bmodule[$i] = (isset($bmodule[$i]) && is_array($bmodule[$i])) ? $bmodule[$i] : [-1];
125
126
        myblocksadmin_update_block($i, $side[$i], $weight[$i], $visible[$i], $title[$i], '', '', $bcachetime[$i], $bmodule[$i], []);
127
    }
128
129
    redirect_header('myblocksadmin.php', 1, _AM_DBUPDATED);
130
}
131
132
if ('save' === $op) {
133
    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

133
    /** @scrutinizer ignore-call */ 
134
    save_block($bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bmodule, $bcachetime);
Loading history...
134
    exit();
135
}
136
137
if ('update' === $op) {
138
    $bcachetime = isset($bcachetime) ? (int)$bcachetime : 0;
139
    $options    = $options ?? [];
140
    $bcontent   = $bcontent ?? '';
141
    $bctype     = $bctype ?? '';
142
    $bmodule    = (isset($bmodule) && is_array($bmodule)) ? $bmodule : [-1]; // GIJ +
143
    $msg        = myblocksadmin_update_block($bid, $bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bcachetime, $bmodule, $options); // GIJ c
144
    redirect_header('myblocksadmin.php', 1, $msg); // GIJ +
145
}
146
147
if ('delete_ok' === $op) {
148
    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

148
    /** @scrutinizer ignore-call */ 
149
    delete_block_ok($bid);
Loading history...
149
    exit();
150
}
151
152
if ('delete' === $op) {
153
    xoops_cp_header();
154
    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

154
    /** @scrutinizer ignore-call */ 
155
    delete_block($bid);
Loading history...
155
    xoops_cp_footer();
156
    exit();
157
}
158
159
if ('edit' === $op) {
160
    xoops_cp_header();
161
    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

161
    /** @scrutinizer ignore-call */ 
162
    edit_block($bid);
Loading history...
162
    xoops_cp_footer();
163
    exit();
164
}
165
166
// import from modules/system/admin/blocksadmin/blocksadmin.php
167
/**
168
 * @param       $bid
169
 * @param       $bside
170
 * @param       $bweight
171
 * @param       $bvisible
172
 * @param       $btitle
173
 * @param       $bcontent
174
 * @param       $bctype
175
 * @param       $bcachetime
176
 * @param       $bmodule
177
 * @param array $options
178
 * @return string
179
 */
180
function myblocksadmin_update_block(
181
    $bid,
182
    $bside,
183
    $bweight,
184
    $bvisible,
185
    $btitle,
186
    $bcontent,
187
    $bctype,
188
    $bcachetime,
189
    $bmodule,
190
    $options = []
191
) {
192
    global $xoopsConfig;
193
    if (empty($bmodule)) {
194
        xoops_cp_header();
195
        xoops_error(sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
196
        xoops_cp_footer();
197
        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...
198
    }
199
    $myblock = new \XoopsBlock($bid);
200
    // $myblock->setVar('side', $bside); GIJ -
201
    if ($bside >= 0) {
202
        $myblock->setVar('side', $bside);
203
    } // GIJ +
204
    $myblock->setVar('weight', $bweight);
205
    $myblock->setVar('visible', $bvisible);
206
    $myblock->setVar('title', $btitle);
207
    $myblock->setVar('content', $bcontent);
208
    $myblock->setVar('bcachetime', $bcachetime);
209
    if (null !== $options && (count($options) > 0)) {
210
        $options = implode('|', $options);
211
        $myblock->setVar('options', $options);
212
    }
213
    if ('C' === $myblock->getVar('block_type')) {
214
        switch ($bctype) {
215
            case 'H':
216
                $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...
217
                break;
218
            case 'P':
219
                $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...
220
                break;
221
            case 'S':
222
                $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...
223
                break;
224
            default:
225
                $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...
226
                break;
227
        }
228
        $myblock->setVar('name', $name);
229
        $myblock->setVar('c_type', $bctype);
230
    } else {
231
        $myblock->setVar('c_type', 'H');
232
    }
233
    $msg = _AM_DBUPDATED;
234
    if (false !== $myblock->store()) {
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::store() has been deprecated. ( Ignorable by Annotation )

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

234
    if (false !== /** @scrutinizer ignore-deprecated */ $myblock->store()) {
Loading history...
235
        $db  = \XoopsDatabaseFactory::getDatabaseConnection();
236
        $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $db->prefix('block_module_link'), $bid);
237
        $db->query($sql);
238
        foreach ($bmodule as $bmid) {
239
            $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $db->prefix('block_module_link'), $bid, (int)$bmid);
240
            $db->query($sql);
241
        }
242
        require_once XOOPS_ROOT_PATH . '/class/template.php';
243
        $xoopsTpl          = new \XoopsTpl();
244
        $xoopsTpl->caching = 2;
245
        if ('' != $myblock->getVar('template')) {
246
            if ($xoopsTpl->is_cached('db:' . $myblock->getVar('template'))) {
247
                if (!$xoopsTpl->clear_cache('db:' . $myblock->getVar('template'))) {
248
                    $msg = 'Unable to clear cache for block ID' . $bid;
249
                }
250
            }
251
        } else {
252
            if ($xoopsTpl->is_cached('db:system_dummy.tpl', 'block' . $bid)) {
253
                if (!$xoopsTpl->clear_cache('db:system_dummy.tpl', 'block' . $bid)) {
254
                    $msg = 'Unable to clear cache for block ID' . $bid;
255
                }
256
            }
257
        }
258
    } else {
259
        $msg = 'Failed update of block. ID:' . $bid;
260
    }
261
262
    return $msg; // GIJ +
263
}
264