myblocksadmin_update_block()   C
last analyzed

Complexity

Conditions 16
Paths 141

Size

Total Lines 81
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 56
c 2
b 0
f 0
dl 0
loc 81
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 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
/**
19
 * Module: SmartFAQ
20
 * Author: The SmartFactory <www.smartfactory.ca>
21
 * Licence: GNU
22
 */
23
24
use Xmf\Request;
25
26
if (!is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid())) {
27
    exit('Access Denied');
28
}
29
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
30
require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blocksadmin.php';
31
32
$moduleDirName      = \basename(\dirname(__DIR__));
33
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
34
35
$op = 'list';
36
if (isset($_POST)) {
37
    foreach ($_POST as $k => $v) {
38
        ${$k} = $v;
39
    }
40
}
41
42
if (Request::hasVar('op', 'GET')) {
43
    if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op']
44
        || 'clone' === $_GET['op']
45
        || 'previewpopup' === $_GET['op']) {
46
        $op  = $_GET['op'];
47
        $bid = Request::getInt('bid', 0, 'GET');
48
    }
49
}
50
51
if (isset($previewblock)) {
52
    xoops_cp_header();
53
    require_once XOOPS_ROOT_PATH . '/class/template.php';
54
    $xoopsTpl          = new \XoopsTpl();
55
    $xoopsTpl->caching = 0;
56
    if (isset($bid)) {
57
        $block['bid']        = $bid;
58
        $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...
59
        $myblock             = new \XoopsBlock($bid);
60
        $block['name']       = $myblock->getVar('name');
61
    } else {
62
        if ('save' === $op) {
63
            $block['form_title'] = _AM_ADDBLOCK;
64
        } else {
65
            $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...
66
        }
67
        $myblock = new \XoopsBlock();
68
        $myblock->setVar('block_type', 'C');
69
    }
70
    $myts = \MyTextSanitizer::getInstance();
71
    $myblock->setVar('title', ($btitle));
72
    $myblock->setVar('content', ($bcontent));
73
    $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(
74
            $xoopsConfig['theme_set']
75
        ) . '"></head><body><table><tr><th>' . $myblock->getVar('title') . '</th></tr><tr><td>' . $myblock->getContent('S', $bctype) . '</td></tr></table></body></html>';
76
77
    $dummyfile = '_dummyfile_' . time() . '.html';
78
    $fp        = fopen(XOOPS_CACHE_PATH . '/' . $dummyfile, 'wb');
79
    fwrite($fp, $dummyhtml);
80
    fclose($fp);
81
    $block['edit_form'] = false;
82
    $block['template']  = '';
83
    $block['op']        = $op;
84
    $block['side']      = $bside;
85
    $block['weight']    = $bweight;
86
    $block['visible']   = $bvisible;
87
    $block['title']     = $myblock->getVar('title', 'E');
88
    $block['content']   = $myblock->getVar('content', 'E');
89
    $block['modules']   = &$bmodule;
90
    $block['ctype']     = $bctype ?? $myblock->getVar('c_type');
91
    $block['is_custom'] = true;
92
    $block['cachetime'] = (int)$bcachetime;
93
    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>';
94
    require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blockform.php';
95
    $form->display();
96
    xoops_cp_footer();
97
    echo '<script type="text/javascript">
98
    <!--//
99
    preview_window = openWithSelfMain("' . XOOPS_URL . '/modules/system/admin.php?fct=blocksadmin&op=previewpopup&file=' . $dummyfile . '", "popup", 250, 200);
100
    //-->
101
    </script>';
102
    exit();
103
}
104
105
if ('previewpopup' === $op) {
106
    $file = str_replace('..', '', XOOPS_CACHE_PATH . '/' . trim($_GET['file']));
107
    if (file_exists($file)) {
108
        require_once $file;
109
        @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

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

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

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

160
    /** @scrutinizer ignore-call */ 
161
    delete_block($bid);
Loading history...
161
    xoops_cp_footer();
162
    exit();
163
}
164
165
if ('edit' === $op) {
166
    xoops_cp_header();
167
    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

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

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