Passed
Pull Request — master (#9)
by Michael
03:24
created

myblocksadmin_update_block()   F

Complexity

Conditions 17
Paths 560

Size

Total Lines 89
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 52
dl 0
loc 89
rs 1.6611
c 2
b 0
f 0
cc 17
nc 560
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 (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
if (!is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid())) {
20
    exit('Access Denied');
21
}
22
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
23
require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blocksadmin.php';
24
25
$op = \Xmf\Request::getCmd('op', 'list', 'POST');
26
27
if (!empty($_POST['bid'])) {
28
    $bid = \Xmf\Request::getInt('bid', 0, 'POST');
29
}
30
31
if (\Xmf\Request::hasVar('op', 'GET')) {
32
    if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op']
33
        || 'clone' === $_GET['op'] /* || $_GET['op'] == 'previewpopup'*/) {
34
        $op  = $_GET['op'];
35
        $bid = \Xmf\Request::getInt('bid', 0, 'GET');
36
    }
37
}
38
39
if (\Xmf\Request::hasVar('previewblock', 'POST')) {
40
    /*
41
    if (!admin_refcheck("/modules/$admin_mydirname/admin/")) {
42
        exit('Invalid Referer');
43
    }
44
*/
45
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
46
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
47
    }
48
49
    if (empty($bid)) {
50
        die('Invalid bid.');
51
    }
52
53
    $bside      = \Xmf\Request::getInt('bside', 0, 'POST');
54
    $bweight    = \Xmf\Request::getInt('bweight', 0, 'POST');
55
    $bvisible   = \Xmf\Request::getInt('bvisible', 0, 'POST');
56
    $bmodule    = !empty($_POST['bmodule']) ? $_POST['bmodule'] : [];
57
    $btitle     = !empty($_POST['btitle']) ? $_POST['btitle'] : '';
58
    $bcontent   = !empty($_POST['bcontent']) ? $_POST['bcontent'] : '';
59
    $bctype     = !empty($_POST['bctype']) ? $_POST['bctype'] : '';
60
    $bcachetime = \Xmf\Request::getInt('bcachetime', 0, 'POST');
61
62
    xoops_cp_header();
63
    require_once XOOPS_ROOT_PATH . '/class/template.php';
64
    $xoopsTpl = new \XoopsTpl();
65
    //$xoopsTpl->caching=(0);
66
    $xoopsTpl->caching = 0;
67
    $block['bid']      = $bid;
68
69
    if ('clone_ok' === $op) {
70
        $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...
71
        $block['submit_button'] = _CLONE;
72
        $myblock                = new \XoopsBlock();
73
        $myblock->setVar('block_type', 'C');
74
    } else {
75
        $op                     = 'update';
76
        $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...
77
        $block['submit_button'] = _SUBMIT;
78
        $myblock                = new \XoopsBlock($bid);
79
        $block['name']          = $myblock->getVar('name');
80
    }
81
82
    $myts = \MyTextSanitizer::getInstance();
83
    $myblock->setVar('title', $myts->stripSlashesGPC($btitle));
84
    $myblock->setVar('content', $myts->stripSlashesGPC($bcontent));
85
    /*
86
        $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($xoopsConfig['theme_set']).'"></head><body><table><tr><th>'.$myblock->getVar('title').'</th></tr><tr><td>'.$myblock->getContent('S', $bctype).'</td></tr></table></body></html>';
87
        $dummyfile = '_dummyfile_'.time().'.html';
88
        $fp = fopen(XOOPS_CACHE_PATH.'/'.$dummyfile, 'w');
89
        fwrite($fp, $dummyhtml);
90
        fclose($fp);
91
    */
92
    $block['edit_form'] = false;
93
    $block['template']  = '';
94
    $block['op']        = $op;
95
    $block['side']      = $bside;
96
    $block['weight']    = $bweight;
97
    $block['visible']   = $bvisible;
98
    $block['title']     = $myblock->getVar('title', 'E');
99
    $block['content']   = $myblock->getVar('content', 'n');
100
    $block['modules']   = &$bmodule;
101
    $block['ctype']     = isset($bctype) ? $bctype : $myblock->getVar('c_type');
102
    $block['is_custom'] = true;
103
    $block['cachetime'] = $bcachetime;
104
    echo '<a href="myblocksadmin.php">' . _AM_MYLINKS_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . $block['form_title'] . '<br><br>';
105
    require_once dirname(__DIR__) . '/admin/myblockform.php'; //GIJ
106
    //echo '<a href="admin.php?fct=blocksadmin">'. _AM_MYLINKS_BADMIN .'</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;'.$block['form_title'].'<br><br>';
107
    //require_once XOOPS_ROOT_PATH.'/modules/system/admin/blocksadmin/blockform.php';
108
    //    $xoopsGTicket->addTicketXoopsFormElement($form, __LINE__, 1800, 'myblocksadmin'); //GIJ
109
    $form->display();
110
111
    $original_level = error_reporting(E_ALL);
112
    echo "<table width='100%' class='outer' cellspacing='1'>\n" . "  <tr>\n" . '    <th>' . $myblock->getVar('title') . "</th>\n" . "  </tr>\n" . "  <tr>\n" . "    <td class='odd'>" . $myblock->getContent('S', $bctype) . "</td>\n" . "  </tr>\n" . "</table>\n";
113
    error_reporting($original_level);
114
115
    xoops_cp_footer();
116
    /*
117
        echo '<script type="text/javascript">
118
        preview_window = openWithSelfMain("'.XOOPS_URL.'/modules/system/admin.php?fct=blocksadmin&op=previewpopup&file='.$dummyfile.'", "popup", 250, 200);
119
        </script>';
120
    */
121
    exit();
122
}
123
124
/*
125
  if ($op == 'previewpopup') {
126
        if ( !admin_refcheck("/modules/$admin_mydirname/admin/") ) {
127
            exit('Invalid Referer');
128
        }
129
        $file = str_replace('..', '', XOOPS_CACHE_PATH.'/'.trim($_GET['file']));
130
        if (file_exists($file)) {
131
            require_once $file;
132
            @unlink($file);
133
        }
134
        exit();
135
  }
136
*/
137
138
/*
139
if ($op == "list") {
140
    xoops_cp_header();
141
    list_blocks();
142
    xoops_cp_footer();
143
    exit();
144
}
145
*/
146
147
if ('order' === $op) {
148
    //if ( !admin_refcheck("/modules/$admin_mydirname/admin/") ) {
149
    //  exit('Invalid Referer');
150
    //}
151
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
152
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
153
    }
154
    if (\Xmf\Request::hasVar('side', 'POST')) {
155
        $side = $_POST['side'];
156
    }
157
    //  if ( !empty($_POST['weight']) ) { $weight = $_POST['weight']; }
158
    if (\Xmf\Request::hasVar('visible', 'POST')) {
159
        $visible = $_POST['visible'];
160
    }
161
    /*
162
       if (\Xmf\Request::hasVar('oldside', 'POST')) {
163
          $oldside = $_POST['oldside'];
164
      }
165
       if (\Xmf\Request::hasVar('oldweight', 'POST')) {
166
          $oldweight = $_POST['oldweight'];
167
      }
168
       if (\Xmf\Request::hasVar('oldvisible', 'POST')) {
169
          $oldvisible = $_POST['oldvisible'];
170
      }
171
    */
172
    $bid = !empty($_POST['bid']) ? $_POST['bid'] : [];
173
    // GIJ start
174
    foreach (array_keys($bid) as $i) {
175
        if ($side[$i] < 0) {
176
            $visible[$i] = 0;
177
            $side[$i]    = -1;
178
        } else {
179
            $visible[$i] = 1;
180
        }
181
182
        $bmodule = (isset($_POST['bmodule'][$i])
183
                    && is_array($_POST['bmodule'][$i])) ? $_POST['bmodule'][$i] : [-1];
184
        myblocksadmin_update_block($i, $side[$i], $_POST['weight'][$i], $visible[$i], $_POST['title'][$i], null, null, $_POST['bcachetime'][$i], $bmodule, []);
185
186
        //        if ( $oldweight[$i] != $weight[$i] || $oldvisible[$i] != $visible[$i] || $oldside[$i] != $side[$i] )
187
        //            order_block($bid[$i], $weight[$i], $visible[$i], $side[$i]);
188
    }
189
    $query4redirect = '?dirname=' . urlencode(strip_tags(mb_substr($_POST['query4redirect'], 9)));
190
    redirect_header("myblocksadmin.php$query4redirect", 1, _AM_DBUPDATED);
191
    // GIJ end
192
    exit();
193
}
194
195
if ('order2' === $op) {
196
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
197
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
198
    }
199
200
    if (\Xmf\Request::hasVar('addblock', 'POST') && is_array($_POST['addblock'])) {
201
        // addblock
202
        foreach ($_POST['addblock'] as $bid => $val) {
203
            myblocksadmin_update_blockinstance(0, 0, 0, 0, '', null, null, 0, [], [], (int)$bid);
204
        }
205
    } else {
206
        // else change order
207
        if (\Xmf\Request::hasVar('side', 'POST')) {
208
            $side = $_POST['side'];
209
        }
210
        if (\Xmf\Request::hasVar('visible', 'POST')) {
211
            $visible = $_POST['visible'];
212
        }
213
        $id = !empty($_POST['id']) ? $_POST['id'] : [];
214
215
        foreach (array_keys($id) as $i) {
216
            // separate side and visible
217
            if ($side[$i] < 0) {
218
                $visible[$i] = 0;
219
                $side[$i]    = -1;  // for not to destroy the original position
220
            } else {
221
                $visible[$i] = 1;
222
            }
223
224
            $bmodule = (isset($_POST['bmodule'][$i])
225
                        && is_array($_POST['bmodule'][$i])) ? $_POST['bmodule'][$i] : [-1];
226
            myblocksadmin_update_blockinstance($i, $side[$i], $_POST['weight'][$i], $visible[$i], $_POST['title'][$i], null, null, $_POST['bcachetime'][$i], $bmodule, []);
227
        }
228
    }
229
230
    $query4redirect = '?dirname=' . urlencode(strip_tags(mb_substr($_POST['query4redirect'], 9)));
231
    redirect_header("myblocksadmin.php$query4redirect", 1, _MD_MYLINKS_AM_DBUPDATED);
0 ignored issues
show
Bug introduced by
The constant _MD_MYLINKS_AM_DBUPDATED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
232
}
233
234
/* if ($op == 'save') {
235
  if ( !admin_refcheck("/modules/$admin_mydirname/admin/") ) {
236
    exit('Invalid Referer');
237
  }
238
  if ( ! $GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin']) ) {
239
    redirect_header(XOOPS_URL.'/',3,$GLOBALS['xoopsSecurity']->getErrors());
240
  }
241
  if ( !empty($_POST['bside']) ) { $bside = (int)($_POST['bside']); } else { $bside = 0; }
242
  if ( !empty($_POST['bweight']) ) { $bweight = (int)($_POST['bweight']); } else { $bweight = 0; }
243
  if ( !empty($_POST['bvisible']) ) { $bvisible = (int)($_POST['bvisible']); } else { $bvisible = 0; }
244
  if ( !empty($_POST['bmodule']) ) { $bmodule = $_POST['bmodule']; } else { $bmodule = []; }
245
  if ( !empty($_POST['btitle']) ) { $btitle = $_POST['btitle']; } else { $btitle = ""; }
246
  if ( !empty($_POST['bcontent']) ) { $bcontent = $_POST['bcontent']; } else { $bcontent = ""; }
247
  if ( !empty($_POST['bctype']) ) { $bctype = $_POST['bctype']; } else { $bctype = ""; }
248
  if ( !empty($_POST['bcachetime']) ) { $bcachetime = (int)($_POST['bcachetime']); } else { $bcachetime = 0; }
249
  save_block($bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bmodule, $bcachetime);
250
  exit();
251
} */
252
253
if ('update' === $op) {
254
    /*
255
    if (!admin_refcheck("/modules/$admin_mydirname/admin/")) {
256
      exit('Invalid Referer');
257
    }
258
*/
259
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
260
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
261
    }
262
    /*
263
        if ( !empty($_POST['bside']) ) { $bside = (int)($_POST['bside']); } else { $bside = 0; }
264
        if ( !empty($_POST['bweight']) ) { $bweight = (int)($_POST['bweight']); } else { $bweight = 0; }
265
        if ( !empty($_POST['bvisible']) ) { $bvisible = (int)($_POST['bvisible']); } else { $bvisible = 0; }
266
        if ( !empty($_POST['btitle']) ) { $btitle = $_POST['btitle']; } else { $btitle = ""; }
267
        if ( !empty($_POST['bcontent']) ) { $bcontent = $_POST['bcontent']; } else { $bcontent = ""; }
268
        if ( !empty($_POST['bctype']) ) { $bctype = $_POST['bctype']; } else { $bctype = ""; }
269
        if ( !empty($_POST['bcachetime']) ) { $bcachetime = (int)($_POST['bcachetime']); } else { $bcachetime = 0; }
270
        if ( !empty($_POST['bmodule']) ) { $bmodule = $_POST['bmodule']; } else { $bmodule = []; }
271
        if ( !empty($_POST['options']) ) { $options = $_POST['options']; } else { $options = []; }
272
        update_block($bid, $bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bcachetime, $bmodule, $options);
273
    */
274
275
    $bcachetime = \Xmf\Request::getInt('bcachetime', 0, 'POST');
276
    $options    = isset($_POST['options']) ? $_POST['options'] : [];
277
    $bcontent   = \Xmf\Request::getString('bcontent', '', 'POST');
278
    $bctype     = \Xmf\Request::getString('bctype', '', 'POST');
279
    $bmodule    = (isset($_POST['bmodule']) && is_array($_POST['bmodule'])) ? $_POST['bmodule'] : [-1]; // GIJ +
280
    $msg        = myblocksadmin_update_block($_POST['bid'], $_POST['bside'], $_POST['bweight'], $_POST['bvisible'], $_POST['btitle'], $bcontent, $bctype, $bcachetime, $bmodule, $options); // GIJ !
281
    redirect_header('myblocksadmin.php', 1, $msg);
282
}
283
284
if ('delete_ok' === $op) {
285
    /*
286
    if ( !admin_refcheck("/modules/$admin_mydirname/admin/") ) {
287
      exit('Invalid Referer');
288
    }
289
*/
290
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
291
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
292
    }
293
    // delete_block_ok($bid); GIJ imported from blocksadmin.php
294
    $myblock = new \XoopsBlock($bid);
295
    if ('D' !== $myblock->getVar('block_type') && 'C' !== $myblock->getVar('block_type')) {
296
        redirect_header('myblocksadmin.php', 4, 'Invalid block');
297
    }
298
    $myblock->delete();
0 ignored issues
show
Bug introduced by
The method delete() 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

298
    $myblock->/** @scrutinizer ignore-call */ 
299
              delete();

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...
299
    if ('' != $myblock->getVar('template') && !defined('XOOPS_ORETEKI')) {
300
        $tplfileHandler = xoops_getHandler('tplfile');
301
        $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
0 ignored issues
show
Bug introduced by
The method find() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsTplfileHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

301
        /** @scrutinizer ignore-call */ 
302
        $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
Loading history...
302
        if (count($btemplate) > 0) {
303
            $tplman->delete($btemplate[0]);
304
        }
305
    }
306
    redirect_header('myblocksadmin.php', 1, _AM_DBUPDATED);
307
    // end of delete_block_ok() GIJ
308
    exit();
309
}
310
311
if ('delete' === $op) {
312
    xoops_cp_header();
313
    // delete_block($bid); GIJ imported from blocksadmin.php
314
    $myblock = new \XoopsBlock($bid);
315
    if ('S' === $myblock->getVar('block_type')) {
316
        $message = _AM_SYSTEMCANT;
0 ignored issues
show
Bug introduced by
The constant _AM_SYSTEMCANT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
317
        redirect_header('admin.php?fct=blocksadmin', 4, $message);
318
    } elseif ('M' === $myblock->getVar('block_type')) {
319
        $message = _AM_MODULECANT;
0 ignored issues
show
Bug introduced by
The constant _AM_MODULECANT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
320
        redirect_header('admin.php?fct=blocksadmin', 4, $message);
321
    } else {
322
        xoops_confirm(['fct' => 'blocksadmin', 'op' => 'delete_ok', 'bid' => $myblock->getVar('bid')], 'admin.php', sprintf(_AM_RUSUREDEL, $myblock->getVar('title')));
0 ignored issues
show
Bug introduced by
The constant _AM_RUSUREDEL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
It seems like $myblock->getVar('title') can also be of type array and array; however, parameter $args of sprintf() does only seem to accept string, 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

322
        xoops_confirm(['fct' => 'blocksadmin', 'op' => 'delete_ok', 'bid' => $myblock->getVar('bid')], 'admin.php', sprintf(_AM_RUSUREDEL, /** @scrutinizer ignore-type */ $myblock->getVar('title')));
Loading history...
323
    }
324
    // end of delete_block() GIJ
325
    xoops_cp_footer();
326
    exit();
327
}
328
329
if ('edit' === $op) {
330
    xoops_cp_header();
331
    // edit_block($bid); GIJ imported from blocksadmin.php
332
    $myblock = new \XoopsBlock($bid);
333
334
    $db      = \XoopsDatabaseFactory::getDatabaseConnection();
335
    $sql     = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
336
    $result  = $db->query($sql);
337
    $modules = [];
338
    while (false !== ($row = $db->fetchArray($result))) {
339
        $modules[] = (int)$row['module_id'];
340
    }
341
    $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type')) ? true : false;
342
    $block     = [
343
        'form_title'    => _AM_EDITBLOCK,
344
        'name'          => $myblock->getVar('name'),
345
        'side'          => $myblock->getVar('side'),
346
        'weight'        => $myblock->getVar('weight'),
347
        'visible'       => $myblock->getVar('visible'),
348
        'title'         => $myblock->getVar('title', 'E'),
349
        'content'       => $myblock->getVar('content', 'n'),
350
        'modules'       => $modules,
351
        'is_custom'     => $is_custom,
352
        'ctype'         => $myblock->getVar('c_type'),
353
        'cachetime'     => $myblock->getVar('bcachetime'),
354
        'op'            => 'update',
355
        'bid'           => $myblock->getVar('bid'),
356
        'edit_form'     => $myblock->getOptions(),
357
        'template'      => $myblock->getVar('template'),
358
        'options'       => $myblock->getVar('options'),
359
        'submit_button' => _SUBMIT,
360
    ];
361
362
    echo '<a href="myblocksadmin.php">' . _AM_MYLINKS_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_EDITBLOCK . '<br><br>';
363
    require_once dirname(__DIR__) . '/admin/myblockform.php'; //GIJ
364
    //    $xoopsGTicket->addTicketXoopsFormElement($form, __LINE__, 1800, 'myblocksadmin'); //GIJ
365
    $form->display();
366
    // end of edit_block() GIJ
367
    xoops_cp_footer();
368
    exit();
369
}
370
371
if ('clone' === $op) {
372
    xoops_cp_header();
373
    $myblock = new \XoopsBlock($bid);
374
375
    $db      = \XoopsDatabaseFactory::getDatabaseConnection();
376
    $sql     = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
377
    $result  = $db->query($sql);
378
    $modules = [];
379
    while (false !== ($row = $db->fetchArray($result))) {
380
        $modules[] = (int)$row['module_id'];
381
    }
382
    $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type')) ? true : false;
383
    $block     = [
384
        'form_title'    => _AM_CLONEBLOCK,
385
        'name'          => $myblock->getVar('name'),
386
        'side'          => $myblock->getVar('side'),
387
        'weight'        => $myblock->getVar('weight'),
388
        'visible'       => $myblock->getVar('visible'),
389
        'content'       => $myblock->getVar('content', 'N'),
390
        'title'         => $myblock->getVar('title', 'E'),
391
        'modules'       => $modules,
392
        'is_custom'     => $is_custom,
393
        'ctype'         => $myblock->getVar('c_type'),
394
        'cachetime'     => $myblock->getVar('bcachetime'),
395
        'op'            => 'clone_ok',
396
        'bid'           => $myblock->getVar('bid'),
397
        'edit_form'     => $myblock->getOptions(),
398
        'template'      => $myblock->getVar('template'),
399
        'options'       => $myblock->getVar('options'),
400
        'submit_button' => _CLONE,
401
    ];
402
    echo '<a href="myblocksadmin.php">' . _AM_MYLINKS_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_CLONEBLOCK . '<br><br>';
403
    require_once dirname(__DIR__) . '/admin/myblockform.php';
404
    //    $xoopsGTicket->addTicketXoopsFormElement($form, __LINE__, 1800, 'myblocksadmin'); //GIJ
405
    $form->display();
406
    xoops_cp_footer();
407
    exit();
408
}
409
410
if ('clone_ok' === $op) {
411
    // Ticket Check
412
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
413
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
414
    }
415
416
    $block = new \XoopsBlock($bid);
417
418
    // block type check
419
    $block_type = $block->getVar('block_type');
420
    if ('C' !== $block_type && 'M' !== $block_type && 'D' !== $block_type) {
421
        redirect_header('myblocksadmin.php', 4, 'Invalid block');
422
    }
423
424
    if (empty($_POST['options'])) {
425
        $options = [];
426
    } elseif (is_array($_POST['options'])) {
427
        $options = $_POST['options'];
428
    } else {
429
        $options = explode('|', $_POST['options']);
430
    }
431
432
    // for backward compatibility
433
    // $cblock =& $block->clone(); or $cblock =& $block->xoopsClone();
434
    $cblock = new \XoopsBlock();
435
    foreach ($block->vars as $k => $v) {
436
        $cblock->assignVar($k, $v['value']);
437
    }
438
    $cblock->setNew();
439
440
    $myts = \MyTextSanitizer::getInstance();
441
    $cblock->setVar('side', $_POST['bside']);
442
    $cblock->setVar('weight', $_POST['bweight']);
443
    $cblock->setVar('visible', $_POST['bvisible']);
444
    $cblock->setVar('title', $_POST['btitle']);
445
    $cblock->setVar('content', @$_POST['bcontent']);
446
    $cblock->setVar('c_type', @$_POST['bctype']);
447
    $cblock->setVar('bcachetime', $_POST['bcachetime']);
448
    if (isset($options) && (count($options) > 0)) {
449
        $options = implode('|', $options);
450
        $cblock->setVar('options', $options);
451
    }
452
    $cblock->setVar('bid', 0);
453
    $cblock->setVar('block_type', 'C' === $block_type ? 'C' : 'D');
454
    $cblock->setVar('func_num', 255);
455
    $newid = $cblock->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

455
    /** @scrutinizer ignore-call */ 
456
    $newid = $cblock->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...
456
    if (!$newid) {
457
        xoops_cp_header();
458
        $cblock->getHtmlErrors();
459
        xoops_cp_footer();
460
        exit();
461
    }
462
    /*
463
      if ($cblock->getVar('template') != '') {
464
            $tplfileHandler = xoops_getHandler('tplfile');
465
            $btemplate = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
466
            if (count($btemplate) > 0) {
467
                $tplclone =& $btemplate[0]->clone();
468
                $tplclone->setVar('tpl_id', 0);
469
                $tplclone->setVar('tpl_refid', $newid);
470
                $tplman->insert($tplclone);
471
            }
472
      }
473
    */
474
    $db      = \XoopsDatabaseFactory::getDatabaseConnection();
475
    $bmodule = (isset($_POST['bmodule']) && is_array($_POST['bmodule'])) ? $_POST['bmodule'] : [-1]; // GIJ +
476
    foreach ($bmodule as $bmid) {
477
        $sql = 'INSERT INTO ' . $db->prefix('block_module_link') . " (block_id, module_id) VALUES ('{$newid}', '{$bmid}')";
478
        $db->query($sql);
479
    }
480
481
    /*  global $xoopsUser;
482
      $groups =& $xoopsUser->getGroups();
483
      $count = count($groups);
484
      for ($i = 0; $i < $count; ++$i) {
485
        $sql = "INSERT INTO ".$db->prefix('group_permission')." (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (".$groups[$i].", ".$newid.", 1, 'block_read')";
486
        $db->query($sql);
487
      }
488
    */
489
490
    $sql    = 'SELECT gperm_groupid FROM ' . $db->prefix('group_permission') . " WHERE gperm_name='block_read' AND gperm_modid='1' AND gperm_itemid='{$bid}'";
491
    $result = $db->query($sql);
492
    while (list($gid) = $db->fetchRow($result)) {
493
        $sql = 'INSERT INTO ' . $db->prefix('group_permission') . " (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES ({$gid}, {$newid}, 1, 'block_read')";
494
        $db->query($sql);
495
    }
496
497
    redirect_header('myblocksadmin.php', 1, _AM_DBUPDATED);
498
}
499
500
// import from modules/system/admin/blocksadmin/blocksadmin.php
501
/**
502
 * @param         $bid
503
 * @param         $bside
504
 * @param         $bweight
505
 * @param         $bvisible
506
 * @param         $btitle
507
 * @param         $bcontent
508
 * @param         $bctype
509
 * @param         $bcachetime
510
 * @param         $bmodule
511
 * @param array   $options
512
 * @return string
513
 */
514
function myblocksadmin_update_block(
515
    $bid,
516
    $bside,
517
    $bweight,
518
    $bvisible,
519
    $btitle,
520
    $bcontent,
521
    $bctype,
522
    $bcachetime,
523
    $bmodule,
524
    $options = []
525
) {
526
    global $xoopsConfig;
527
    /*
528
        if (empty($bmodule)) {
529
            xoops_cp_header();
530
            xoops_error(sprintf(_AM_NOTSELNG, _AM_MYLINKS_VISIBLEIN));
531
            xoops_cp_footer();
532
            exit();
533
      }
534
    */
535
    $myblock = new \XoopsBlock($bid);
536
    //$myblock->setVar('side', $bside); GIJ -
537
    if ($bside >= 0) {
538
        $myblock->setVar('side', $bside); // GIJ +
539
    }
540
    $myblock->setVar('weight', $bweight);
541
    $myblock->setVar('visible', $bvisible);
542
    $myblock->setVar('title', $btitle);
543
    if (isset($bcontent)) {
544
        $myblock->setVar('content', $bcontent);
545
    }
546
    if (isset($bctype)) {
547
        $myblock->setVar('c_type', $bctype);
548
    }
549
    $myblock->setVar('bcachetime', $bcachetime);
550
    if (isset($options) && (count($options) > 0)) {
551
        $options = implode('|', $options);
552
        $myblock->setVar('options', $options);
553
    }
554
    if ('C' === $myblock->getVar('block_type')) {
555
        switch ($myblock->getVar('c_type')) {
556
            case 'H':
557
                $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...
558
                break;
559
            case 'P':
560
                $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...
561
                break;
562
            case 'S':
563
                $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...
564
                break;
565
            default:
566
                $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...
567
                break;
568
        }
569
        $myblock->setVar('name', $name);
570
    }
571
    $msg = _AM_DBUPDATED;
572
    if (false !== $myblock->store()) {
573
        $db  = \XoopsDatabaseFactory::getDatabaseConnection();
574
        $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $db->prefix('block_module_link'), $bid);
575
        $db->query($sql);
576
        foreach ($bmodule as $bmid) {
577
            $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $db->prefix('block_module_link'), $bid, (int)$bmid);
578
            $db->query($sql);
579
        }
580
        require_once XOOPS_ROOT_PATH . '/class/template.php';
581
        $xoopsTpl = new \XoopsTpl();
582
        //$xoopsTpl->caching=(2);
583
        $xoopsTpl->caching = 2;
584
        if ('' != $myblock->getVar('template')) {
585
            if ($xoopsTpl->is_cached('db:' . $myblock->getVar('template'))) {
586
                if (!$xoopsTpl->clear_cache('db:' . $myblock->getVar('template'))) {
587
                    $msg = "Unable to clear cache for block ID: {$bid}";
588
                }
589
            }
590
        } else {
591
            if ($xoopsTpl->is_cached('db:system_dummy.tpl', 'block' . $bid)) {
592
                if (!$xoopsTpl->clear_cache('db:system_dummy.tpl', 'block' . $bid)) {
593
                    $msg = 'Unable to clear cache for block ID' . $bid;
594
                }
595
            }
596
        }
597
    } else {
598
        $msg = "Failed update of block. ID: {$bid}";
599
    }
600
    // redirect_header('admin.php?fct=blocksadmin&amp;t='.time(),1,$msg);
601
    // exit(); GIJ -
602
    return $msg; // GIJ +
603
}
604
605
// update block instance for 2.2
606
/**
607
 * @param         $id
608
 * @param         $bside
609
 * @param         $bweight
610
 * @param         $bvisible
611
 * @param         $btitle
612
 * @param         $bcontent
613
 * @param         $bctype
614
 * @param         $bcachetime
615
 * @param         $bmodule
616
 * @param array   $options
617
 * @param null    $bid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $bid is correct as it would always require null to be passed?
Loading history...
618
 * @return string
619
 */
620
function myblocksadmin_update_blockinstance(
621
    $id,
622
    $bside,
623
    $bweight,
624
    $bvisible,
625
    $btitle,
626
    $bcontent,
0 ignored issues
show
Unused Code introduced by
The parameter $bcontent is not used and could be removed. ( Ignorable by Annotation )

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

626
    /** @scrutinizer ignore-unused */ $bcontent,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
627
    $bctype,
0 ignored issues
show
Unused Code introduced by
The parameter $bctype is not used and could be removed. ( Ignorable by Annotation )

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

627
    /** @scrutinizer ignore-unused */ $bctype,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
628
    $bcachetime,
629
    $bmodule,
630
    $options = [],
631
    $bid = null
632
) {
633
    global $xoopsDB;
634
635
    $instanceHandler = xoops_getHandler('blockinstance');
636
    $blockHandler    = xoops_getHandler('block');
637
    if ($id > 0) {
638
        // update
639
        $instance = $instanceHandler->get($id);
640
        if ($bside >= 0) {
641
            $instance->setVar('side', $bside);
642
        }
643
        if (!empty($options)) {
644
            $instance->setVar('options', $options);
645
        }
646
    } else {
647
        // insert
648
        $instance = $instanceHandler->create();
649
        $instance->setVar('bid', $bid);
650
        $instance->setVar('side', $bside);
651
        $block = $blockHandler->get($bid);
652
        $instance->setVar('options', $block->getVar('options'));
653
        if (empty($btitle)) {
654
            $btitle = $block->getVar('name');
655
        }
656
    }
657
    $instance->setVar('weight', $bweight);
658
    $instance->setVar('visible', $bvisible);
659
    $instance->setVar('title', $btitle);
660
    //if (isset($bcontent)) $instance->setVar('content', $bcontent);
661
    //if (isset($bctype)) $instance->setVar('c_type', $bctype);
662
    $instance->setVar('bcachetime', $bcachetime);
663
664
    if ($instanceHandler->insert($instance)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $instanceHandler->insert($instance) targeting XoopsObjectHandler::insert() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
665
        $GLOBALS['xoopsDB']->query('DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('block_module_link') . ' WHERE block_id=' . $instance->getVar('instanceid'));
666
        foreach ($bmodule as $mid) {
667
            $page   = explode('-', $mid);
668
            $mid    = $page[0];
669
            $pageid = $page[1];
670
            $GLOBALS['xoopsDB']->query('INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('block_module_link') . ' VALUES (' . $instance->getVar('instanceid') . ', ' . (int)$mid . ', ' . (int)$pageid . ')');
671
        }
672
673
        return _MD_MYLINKS_AM_DBUPDATED;
0 ignored issues
show
Bug introduced by
The constant _MD_MYLINKS_AM_DBUPDATED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
674
    }
675
676
    return "Failed update of block instance. ID: {$id}";
677
    /*      // NAME for CUSTOM BLOCK
678
        if ( $instance->getVar('block_type') == 'C') {
679
          switch ( $instance->getVar('c_type') ) {
680
          case 'H':
681
            $name = _AM_CUSTOMHTML;
682
            break;
683
          case 'P':
684
            $name = _AM_CUSTOMPHP;
685
            break;
686
          case 'S':
687
            $name = _AM_CUSTOMSMILE;
688
            break;
689
          default:
690
            $name = _AM_CUSTOMNOSMILE;
691
            break;
692
          }
693
          $instance->setVar('name', $name);
694
        }
695
    */
696
    /*          // CLEAR TEMPLATE CACHE
697
          require_once XOOPS_ROOT_PATH.'/class/template.php';
698
          $xoopsTpl = new \XoopsTpl();
699
          //$xoopsTpl->caching=(2);
700
          $xoopsTpl->caching=2;
701
          if ($instance->getVar('template') != '') {
702
            if ($xoopsTpl->is_cached('db:'.$instance->getVar('template'))) {
703
              if (!$xoopsTpl->clear_cache('db:'.$instance->getVar('template'))) {
704
                $msg = 'Unable to clear cache for block ID'.$bid;
705
              }
706
            }
707
          } else {
708
            if ($xoopsTpl->is_cached('db:system_dummy.tpl', 'block'.$bid)) {
709
              if (!$xoopsTpl->clear_cache('db:system_dummy.tpl', 'block'.$bid)) {
710
                $msg = 'Unable to clear cache for block ID'.$bid;
711
              }
712
            }
713
          }
714
    */
715
}
716
717
// TODO  edit2, delete2, customblocks
718