Passed
Branch master (d2b70f)
by Michael
12:26
created

class/Common/Blocksadmin.php (12 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Publisher\Common;
6
7
/**
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 *
12
 *
13
 * @category        Module
14
 * @author          XOOPS Development Team
15
 * @copyright       XOOPS Project
16
 * @link            https://xoops.org
17
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 */
19
20
use Xmf\Request;
21
22
//require __DIR__ . '/admin_header.php';
23
24
class Blocksadmin
25
{
26
    /**
27
     * @var \XoopsMySQLDatabase|null
28
     */
29
    public $db;
30
    public $modHelper;
31
    public $moduleDirName;
32
    public $moduleDirNameUpper;
33
34
    /**
35
     * @param $db
36
     * @param $modHelper
37
     */
38
    public function __construct(\XoopsDatabase $db, $modHelper)
39
    {
40
        $this->db                 = $db;
0 ignored issues
show
Documentation Bug introduced by
$db is of type XoopsDatabase, but the property $db was declared to be of type XoopsMySQLDatabase|null. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
41
        $this->modHelper          = $modHelper;
42
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
43
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
44
        \xoops_loadLanguage('admin', 'system');
45
        \xoops_loadLanguage('admin/blocksadmin', 'system');
46
        \xoops_loadLanguage('admin/groups', 'system');
47
        \xoops_loadLanguage('common', $this->moduleDirName);
48
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
49
    }
50
51
    public function listBlocks(): void
52
    {
53
        global $xoopsModule, $pathIcon16;
54
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
55
        //        xoops_loadLanguage('admin', 'system');
56
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
57
        //        xoops_loadLanguage('admin/groups', 'system');
58
        //        xoops_loadLanguage('common', $moduleDirName);
59
        //        xoops_loadLanguage('blocks', $moduleDirName);
60
61
        /** @var \XoopsModuleHandler $moduleHandler */
62
        $moduleHandler = \xoops_getHandler('module');
63
        /** @var \XoopsMemberHandler $memberHandler */
64
        $memberHandler = \xoops_getHandler('member');
65
        /** @var \XoopsGroupPermHandler $grouppermHandler */
66
        $grouppermHandler = \xoops_getHandler('groupperm');
67
        $groups           = $memberHandler->getGroups();
68
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', 1));
69
        $criteria->add(new \Criteria('isactive', 1));
70
        $moduleList     = $moduleHandler->getList($criteria);
71
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
72
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
73
        \ksort($moduleList);
74
        echo "
75
        <h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
76
        /** @var \XoopsModuleHandler $moduleHandler */
77
        $moduleHandler = \xoops_getHandler('module');
0 ignored issues
show
The assignment to $moduleHandler is dead and can be removed.
Loading history...
78
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
79
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
80
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
81
        <tr valign='middle'><th align='center'>"
82
             . \_AM_SYSTEM_BLOCKS_TITLE
83
             . "</th><th align='center' nowrap='nowrap'>"
84
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE')
85
             . '<br>'
86
             . _LEFT
87
             . '-'
88
             . _CENTER
89
             . '-'
90
             . _RIGHT
91
             . "</th><th align='center'>"
92
             . \constant(
93
                 'CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'
94
             )
95
             . "</th><th align='center'>"
96
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE')
97
             . "</th><th align='center'>"
98
             . \_AM_SYSTEM_BLOCKS_VISIBLEIN
99
             . "</th><th align='center'>"
100
             . \_AM_SYSTEM_ADGS
101
             . "</th><th align='center'>"
102
             . \_AM_SYSTEM_BLOCKS_BCACHETIME
103
             . "</th><th align='center'>"
104
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION')
105
             . '</th></tr>
106
        ';
107
        $blockArray = \XoopsBlock::getByModule($xoopsModule->mid());
108
        $blockCount = \count($blockArray);
109
        $class      = 'even';
110
        $cachetimes = [
111
            0       => _NOCACHE,
112
            30      => \sprintf(_SECONDS, 30),
113
            60      => _MINUTE,
114
            300     => \sprintf(_MINUTES, 5),
115
            1800    => \sprintf(_MINUTES, 30),
116
            3600    => _HOUR,
117
            18000   => \sprintf(_HOURS, 5),
118
            86400   => _DAY,
119
            259200  => \sprintf(_DAYS, 3),
120
            604800  => _WEEK,
121
            2592000 => _MONTH,
122
        ];
123
        foreach ($blockArray as $i) {
124
            $groupsPermissions = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
125
            $sql               = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $i->getVar('bid');
126
            $result            = $this->db->query($sql);
127
            $modules           = [];
128
            while (false !== ($row = $this->db->fetchArray($result))) {
129
                $modules[] = (int)$row['module_id'];
130
            }
131
132
            $cachetimeOptions = '';
133
            foreach ($cachetimes as $cachetime => $cachetimeName) {
134
                if ($i->getVar('bcachetime') == $cachetime) {
135
                    $cachetimeOptions .= "<option value='$cachetime' selected='selected'>$cachetimeName</option>\n";
136
                } else {
137
                    $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n";
138
                }
139
            }
140
141
            $sel0 = $sel1 = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = $ssel5 = $ssel6 = $ssel7 = '';
142
            if (1 === $i->getVar('visible')) {
143
                $sel1 = ' checked';
144
            } else {
145
                $sel0 = ' checked';
146
            }
147
            if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
148
                $ssel0 = ' checked';
149
            } elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
150
                $ssel1 = ' checked';
151
            } elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
152
                $ssel2 = ' checked';
153
            } elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
154
                $ssel4 = ' checked';
155
            } elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
156
                $ssel3 = ' checked';
157
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
158
                $ssel5 = ' checked';
159
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
160
                $ssel6 = ' checked';
161
            } elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
162
                $ssel7 = ' checked';
163
            }
164
            if ('' === $i->getVar('title')) {
165
                $title = '&nbsp;';
166
            } else {
167
                $title = $i->getVar('title');
168
            }
169
            $name = $i->getVar('name');
170
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
171
                 . $i->getVar('bid')
172
                 . "]' value='"
173
                 . $title
174
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
175
                    <div align='center' >
176
                    <input type='radio' name='side["
177
                 . $i->getVar('bid')
178
                 . "]' value='"
179
                 . \XOOPS_CENTERBLOCK_LEFT
180
                 . "'$ssel2>
181
                        <input type='radio' name='side["
182
                 . $i->getVar('bid')
183
                 . "]' value='"
184
                 . \XOOPS_CENTERBLOCK_CENTER
185
                 . "'$ssel3>
186
                    <input type='radio' name='side["
187
                 . $i->getVar('bid')
188
                 . "]' value='"
189
                 . \XOOPS_CENTERBLOCK_RIGHT
190
                 . "'$ssel4>
191
                    </div>
192
                    <div>
193
                        <span style='float:right;'><input type='radio' name='side["
194
                 . $i->getVar('bid')
195
                 . "]' value='"
196
                 . \XOOPS_SIDEBLOCK_RIGHT
197
                 . "'$ssel1></span>
198
                    <div align='left'><input type='radio' name='side["
199
                 . $i->getVar('bid')
200
                 . "]' value='"
201
                 . \XOOPS_SIDEBLOCK_LEFT
202
                 . "'$ssel0></div>
203
                    </div>
204
                    <div align='center'>
205
                    <input type='radio' name='side["
206
                 . $i->getVar('bid')
207
                 . "]' value='"
208
                 . \XOOPS_CENTERBLOCK_BOTTOMLEFT
209
                 . "'$ssel5>
210
                        <input type='radio' name='side["
211
                 . $i->getVar('bid')
212
                 . "]' value='"
213
                 . \XOOPS_CENTERBLOCK_BOTTOM
214
                 . "'$ssel7>
215
                    <input type='radio' name='side["
216
                 . $i->getVar('bid')
217
                 . "]' value='"
218
                 . \XOOPS_CENTERBLOCK_BOTTOMRIGHT
219
                 . "'$ssel6>
220
                    </div>
221
                </td><td class='$class' align='center'><input type='text' name='weight["
222
                 . $i->getVar('bid')
223
                 . "]' value='"
224
                 . $i->getVar('weight')
225
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
226
                 . $i->getVar('bid')
227
                 . "]' value='1'$sel1>"
228
                 . _YES
229
                 . "&nbsp;<input type='radio' name='visible["
230
                 . $i->getVar('bid')
231
                 . "]' value='0'$sel0>"
232
                 . _NO
233
                 . '</td>';
234
235
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
236
            foreach ($moduleList as $k => $v) {
237
                echo "<option value='$k'" . (\in_array($k, $modules, true) ? " selected='selected'" : '') . ">$v</option>";
238
            }
239
            echo '</select></td>';
240
241
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
242
            foreach ($groups as $grp) {
243
                echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groupsPermissions, true) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>';
244
            }
245
            echo '</select></td>';
246
247
            // Cache lifetime
248
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select>
249
                                    </td>';
250
251
            // Actions
252
253
            echo "<td class='$class' align='center'><a href='blocksadmin.php?op=edit&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/edit.png' . " alt='" . _EDIT . "' title='" . _EDIT . "'>
254
                 </a> <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . _CLONE . "' title='" . _CLONE . "'>
255
                 </a>";
256
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
257
            //                echo "&nbsp;<a href='" . XOOPS_URL . '/modules/system/admin.php?fct=blocksadmin&amp;op=delete&amp;bid=' . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'>
258
            //                     </a>";
259
            //            }
260
261
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
262
            if (!\in_array($i->getVar('block_type'), ['M', 'S'], true)) {
263
                echo "&nbsp;
264
                <a href='blocksadmin.php?op=delete&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'>
265
                     </a>";
266
            }
267
            echo "
268
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
269
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
270
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
271
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
272
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
273
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
274
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
275
            </td></tr>
276
            ";
277
            $class = ('even' === $class) ? 'odd' : 'even';
278
        }
279
        echo "<tr><td class='foot' align='center' colspan='8'>
280
        <input type='hidden' name='op' value='order'>
281
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
282
        <input type='submit' name='submit' value='" . _SUBMIT . "'>
283
        </td></tr></table>
284
        </form>
285
        <br><br>";
286
    }
287
288
    /**
289
     * @param int $bid
290
     */
291
    public function deleteBlock($bid)
292
    {
293
        //        \xoops_cp_header();
294
295
        \xoops_loadLanguage('admin', 'system');
296
        \xoops_loadLanguage('admin/blocksadmin', 'system');
297
        \xoops_loadLanguage('admin/groups', 'system');
298
299
        $myblock = new \XoopsBlock($bid);
300
301
        $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid);
302
        if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
The assignment to $result is dead and can be removed.
Loading history...
303
            return false;
304
        }
305
        $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
306
        $this->db->queryF($sql);
307
308
        $this->modHelper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
309
    }
310
311
    /**
312
     * @param int $bid
313
     */
314
    public function cloneBlock($bid): void
315
    {
316
        //require __DIR__ . '/admin_header.php';
317
        //        \xoops_cp_header();
318
319
        \xoops_loadLanguage('admin', 'system');
320
        \xoops_loadLanguage('admin/blocksadmin', 'system');
321
        \xoops_loadLanguage('admin/groups', 'system');
322
323
        $myblock = new \XoopsBlock($bid);
324
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
325
        $result  = $this->db->query($sql);
326
        $modules = [];
327
        while (false !== ($row = $this->db->fetchArray($result))) {
0 ignored issues
show
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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

327
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
328
            $modules[] = (int)$row['module_id'];
329
        }
330
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E'], true);
331
        $block    = [
332
            'title'      => $myblock->getVar('title') . ' Clone',
333
            'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
334
            'name'       => $myblock->getVar('name'),
335
            'side'       => $myblock->getVar('side'),
336
            'weight'     => $myblock->getVar('weight'),
337
            'visible'    => $myblock->getVar('visible'),
338
            'content'    => $myblock->getVar('content', 'N'),
339
            'modules'    => $modules,
340
            'is_custom'  => $isCustom,
341
            'ctype'      => $myblock->getVar('c_type'),
342
            'bcachetime' => $myblock->getVar('bcachetime'),
343
            'op'         => 'clone_ok',
344
            'bid'        => $myblock->getVar('bid'),
345
            'edit_form'  => $myblock->getOptions(),
346
            'template'   => $myblock->getVar('template'),
347
            'options'    => $myblock->getVar('options'),
348
        ];
349
        echo '<a href="blocksadmin.php">' . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . \_AM_SYSTEM_BLOCKS_CLONEBLOCK . '<br><br>';
350
        //        $form = new Blockform();
351
        //        $form->render();
352
353
        echo $this->render($block);
0 ignored issues
show
Are you sure $this->render($block) of type void can be used in echo? ( Ignorable by Annotation )

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

353
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
354
        //        xoops_cp_footer();
355
        //        require_once __DIR__ . '/admin_footer.php';
356
        //        exit();
357
    }
358
359
    /**
360
     * @param int               $bid
361
     * @param string            $bside
362
     * @param string            $bweight
363
     * @param string            $bvisible
364
     * @param string            $bcachetime
365
     * @param array             $bmodule
366
     * @param null|array|string $options
367
     * @param mixed             $groups
368
     */
369
    public function isBlockCloned($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups): void
370
    {
371
        \xoops_loadLanguage('admin', 'system');
372
        \xoops_loadLanguage('admin/blocksadmin', 'system');
373
        \xoops_loadLanguage('admin/groups', 'system');
374
375
        $block = new \XoopsBlock($bid);
376
        $clone = $block->xoopsClone();
377
        if (empty($bmodule)) {
378
            //            \xoops_cp_header();
379
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
380
            \xoops_cp_footer();
381
            exit();
382
        }
383
        $clone->setVar('side', $bside);
384
        $clone->setVar('weight', $bweight);
385
        $clone->setVar('visible', $bvisible);
386
        //$clone->setVar('content', $_POST['bcontent']);
387
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
388
        $clone->setVar('bcachetime', $bcachetime);
389
        if (isset($options) && (\count($options) > 0)) {
0 ignored issues
show
It seems like $options can also be of type string; however, parameter $value of count() does only seem to accept Countable|array, 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

389
        if (isset($options) && (\count(/** @scrutinizer ignore-type */ $options) > 0)) {
Loading history...
390
            $options = \implode('|', $options);
0 ignored issues
show
It seems like $options can also be of type string; however, parameter $pieces of implode() does only seem to accept array, 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

390
            $options = \implode('|', /** @scrutinizer ignore-type */ $options);
Loading history...
391
            $clone->setVar('options', $options);
392
        }
393
        $clone->setVar('bid', 0);
394
        if (\in_array($block->getVar('block_type'), ['C', 'E'], true)) {
395
            $clone->setVar('block_type', 'E');
396
        } else {
397
            $clone->setVar('block_type', 'D');
398
        }
399
        $newid = $clone->store();
400
        if (!$newid) {
401
            //            \xoops_cp_header();
402
            $clone->getHtmlErrors();
403
            \xoops_cp_footer();
404
            exit();
405
        }
406
        if ('' !== $clone->getVar('template')) {
407
            /** @var \XoopsTplfileHandler $tplfileHandler */
408
            $tplfileHandler = \xoops_getHandler('tplfile');
409
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
410
            if (\count($btemplate) > 0) {
411
                $tplclone = $btemplate[0]->xoopsClone();
412
                $tplclone->setVar('tpl_id', 0);
413
                $tplclone->setVar('tpl_refid', $newid);
414
                $tplfileHandler->insert($tplclone);
415
            }
416
        }
417
418
        foreach ($bmodule as $bmid) {
419
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
420
            $this->db->query($sql);
421
        }
422
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
423
        foreach ($groups as $iValue) {
424
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
425
            $this->db->query($sql);
426
        }
427
        $this->modHelper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
428
    }
429
430
    /**
431
     * @param int      $bid
432
     * @param string   $title
433
     * @param int      $weight
434
     * @param bool     $visible
435
     * @param string   $side
436
     * @param int      $bcachetime
437
     * @param null|int $bmodule
438
     */
439
    public function setOrder($bid, $title, $weight, $visible, $side, $bcachetime, $bmodule = null)
440
    {
441
        $myblock = new \XoopsBlock($bid);
442
        $myblock->setVar('title', $title);
443
        $myblock->setVar('weight', $weight);
444
        $myblock->setVar('visible', $visible);
445
        $myblock->setVar('side', $side);
446
        $myblock->setVar('bcachetime', $bcachetime);
447
        //        $myblock->store();
448
        /** @var \XoopsBlockHandler $blockHandler */
449
        $blockHandler = xoops_getHandler('block');
450
451
        return $blockHandler->insert($myblock);
452
        //        /** @var \XoopsBlockHandler $blockHandler */
453
        //        $blockHandler = \xoops_getHandler('block');
454
        //        return $blockHandler->insert($myblock);
455
    }
456
457
    /**
458
     * @param int $bid
459
     */
460
    public function editBlock($bid): void
461
    {
462
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
463
        //        \xoops_cp_header();
464
        \xoops_loadLanguage('admin', 'system');
465
        \xoops_loadLanguage('admin/blocksadmin', 'system');
466
        \xoops_loadLanguage('admin/groups', 'system');
467
        //        mpu_adm_menu();
468
        $myblock = new \XoopsBlock($bid);
469
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
470
        $result  = $this->db->query($sql);
471
        $modules = [];
472
        while (false !== ($row = $this->db->fetchArray($result))) {
0 ignored issues
show
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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

472
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
473
            $modules[] = (int)$row['module_id'];
474
        }
475
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E'], true);
476
        $block    = [
477
            'title'      => $myblock->getVar('title'),
478
            'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK,
479
            //        'name'       => $myblock->getVar('name'),
480
            'side'       => $myblock->getVar('side'),
481
            'weight'     => $myblock->getVar('weight'),
482
            'visible'    => $myblock->getVar('visible'),
483
            'content'    => $myblock->getVar('content', 'N'),
484
            'modules'    => $modules,
485
            'is_custom'  => $isCustom,
486
            'ctype'      => $myblock->getVar('c_type'),
487
            'bcachetime' => $myblock->getVar('bcachetime'),
488
            'op'         => 'edit_ok',
489
            'bid'        => $myblock->getVar('bid'),
490
            'edit_form'  => $myblock->getOptions(),
491
            'template'   => $myblock->getVar('template'),
492
            'options'    => $myblock->getVar('options'),
493
        ];
494
        echo '<a href="blocksadmin.php">' . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . \_AM_SYSTEM_BLOCKS_EDITBLOCK . '<br><br>';
495
496
        /** @var \XoopsThemeForm $form */ //                $form = new Blockform();
497
        //        $form->render($block);
498
        //        $form = new \XoopsThemeForm();
499
500
        echo $this->render($block);
0 ignored issues
show
Are you sure $this->render($block) of type void can be used in echo? ( Ignorable by Annotation )

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

500
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
501
    }
502
503
    /**
504
     * @param int               $bid
505
     * @param string            $btitle
506
     * @param string            $bside
507
     * @param int               $bweight
508
     * @param bool              $bvisible
509
     * @param int               $bcachetime
510
     * @param array             $bmodule
511
     * @param null|array|string $options
512
     * @param null|array        $groups
513
     */
514
    public function updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups): void
515
    {
516
        $myblock = new \XoopsBlock($bid);
517
        $myblock->setVar('title', $btitle);
518
        $myblock->setVar('weight', $bweight);
519
        $myblock->setVar('visible', $bvisible);
520
        $myblock->setVar('side', $bside);
521
        $myblock->setVar('bcachetime', $bcachetime);
522
        //update block options
523
        if (isset($options)) {
524
            $optionsCount = \count($options);
0 ignored issues
show
It seems like $options can also be of type string; however, parameter $value of count() does only seem to accept Countable|array, 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

524
            $optionsCount = \count(/** @scrutinizer ignore-type */ $options);
Loading history...
525
            if ($optionsCount > 0) {
526
                //Convert array values to comma-separated
527
                foreach ($options as $i => $iValue) {
528
                    if (\is_array($iValue)) {
529
                        $options[$i] = \implode(',', $iValue);
530
                    }
531
                }
532
                $options = \implode('|', $options);
0 ignored issues
show
It seems like $options can also be of type string; however, parameter $pieces of implode() does only seem to accept array, 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

532
                $options = \implode('|', /** @scrutinizer ignore-type */ $options);
Loading history...
533
                $myblock->setVar('options', $options);
534
            }
535
        }
536
        $myblock->store();
537
        //        /** @var \XoopsBlockHandler $blockHandler */
538
        //        $blockHandler = \xoops_getHandler('block');
539
        //        $blockHandler->insert($myblock);
540
541
        if (!empty($bmodule) && \count($bmodule) > 0) {
542
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
543
            $this->db->query($sql);
544
            if (\in_array(0, $bmodule, true)) {
545
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
546
                $this->db->query($sql);
547
            } else {
548
                foreach ($bmodule as $bmid) {
549
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid);
550
                    $this->db->query($sql);
551
                }
552
            }
553
        }
554
        $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid);
555
        $this->db->query($sql);
556
        if (!empty($groups)) {
557
            foreach ($groups as $grp) {
558
                $sql = \sprintf("INSERT INTO `%s` (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, 1, 'block_read')", $this->db->prefix('group_permission'), $grp, $bid);
559
                $this->db->query($sql);
560
            }
561
        }
562
        $this->modHelper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
563
    }
564
565
    /**
566
     * @param array      $bid
567
     * @param mixed      $oldtitle
568
     * @param mixed      $oldside
569
     * @param mixed      $oldweight
570
     * @param mixed      $oldvisible
571
     * @param mixed      $oldgroups
572
     * @param mixed      $oldbcachetime
573
     * @param mixed      $title
574
     * @param mixed      $weight
575
     * @param mixed      $visible
576
     * @param mixed      $side
577
     * @param mixed      $bcachetime
578
     * @param mixed      $groups
579
     * @param null|mixed $bmodule
580
     */
581
    public function orderBlock(
582
        $bid,
583
        $oldtitle,
584
        $oldside,
585
        $oldweight,
586
        $oldvisible,
587
        $oldgroups,
588
        $oldbcachetime,
589
        $title,
590
        $weight,
591
        $visible,
592
        $side,
593
        $bcachetime,
594
        $groups,
595
        $bmodule = null
596
    ): void {
597
        if (!$GLOBALS['xoopsSecurity']->check()) {
598
            \redirect_header($_SERVER['SCRIPT_NAME'], 3, \implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
599
        }
600
        foreach (\array_keys($bid) as $i) {
601
            if ($oldtitle[$i] !== $title[$i] || $oldweight[$i] !== $weight[$i] || $oldvisible[$i] !== $visible[$i]
602
                || $oldside[$i] !== $side[$i]
603
                || $oldbcachetime[$i] !== $bcachetime[$i]) {
604
                $this->setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]);
605
            }
606
            if (!empty($bmodule[$i]) && \count($bmodule[$i]) > 0) {
607
                $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid[$i]);
608
                $this->db->query($sql);
609
                if (\in_array(0, $bmodule[$i], true)) {
610
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], 0);
611
                    $this->db->query($sql);
612
                } else {
613
                    foreach ($bmodule[$i] as $bmid) {
614
                        $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], (int)$bmid);
615
                        $this->db->query($sql);
616
                    }
617
                }
618
            }
619
            $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid[$i]);
620
            $this->db->query($sql);
621
            if (!empty($groups[$i])) {
622
                foreach ($groups[$i] as $grp) {
623
                    $sql = \sprintf("INSERT INTO `%s` (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, 1, 'block_read')", $this->db->prefix('group_permission'), $grp, $bid[$i]);
624
                    $this->db->query($sql);
625
                }
626
            }
627
        }
628
629
        $this->modHelper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
630
    }
631
632
    /**
633
     * @param null $block
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $block is correct as it would always require null to be passed?
Loading history...
634
     */
635
    public function render($block = null): void
636
    {
637
        \xoops_load('XoopsFormLoader');
638
        \xoops_loadLanguage('common', $this->moduleDirNameUpper);
639
640
        $form = new \XoopsThemeForm($block['form_title'], 'blockform', 'blocksadmin.php', 'post', true);
641
        if (isset($block['name'])) {
642
            $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_NAME, $block['name']));
643
        }
644
        $sideSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_TYPE, 'bside', $block['side']);
645
        $sideSelect->addOptionArray([
646
                                        0 => \_AM_SYSTEM_BLOCKS_SBLEFT,
647
                                        1 => \_AM_SYSTEM_BLOCKS_SBRIGHT,
648
                                        3 => \_AM_SYSTEM_BLOCKS_CBLEFT,
649
                                        4 => \_AM_SYSTEM_BLOCKS_CBRIGHT,
650
                                        5 => \_AM_SYSTEM_BLOCKS_CBCENTER,
651
                                        7 => \_AM_SYSTEM_BLOCKS_CBBOTTOMLEFT,
652
                                        8 => \_AM_SYSTEM_BLOCKS_CBBOTTOMRIGHT,
653
                                        9 => \_AM_SYSTEM_BLOCKS_CBBOTTOM,
654
                                    ]);
655
        $form->addElement($sideSelect);
656
        $form->addElement(new \XoopsFormText(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'), 'bweight', 2, 5, $block['weight']));
657
        $form->addElement(new \XoopsFormRadioYN(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE'), 'bvisible', $block['visible']));
658
        $modSelect = new \XoopsFormSelect(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLEIN'), 'bmodule', $block['modules'], 5, true);
659
        /** @var \XoopsModuleHandler $moduleHandler */
660
        $moduleHandler = \xoops_getHandler('module');
661
        $criteria      = new \CriteriaCompo(new \Criteria('hasmain', 1));
662
        $criteria->add(new \Criteria('isactive', 1));
663
        $moduleList     = $moduleHandler->getList($criteria);
664
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
665
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
666
        \ksort($moduleList);
667
        $modSelect->addOptionArray($moduleList);
668
        $form->addElement($modSelect);
669
        $form->addElement(new \XoopsFormText(_AM_SYSTEM_BLOCKS_TITLE, 'btitle', 50, 255, $block['title']), false);
670
        if ($block['is_custom']) {
671
            $textarea = new \XoopsFormDhtmlTextArea(\_AM_SYSTEM_BLOCKS_CONTENT, 'bcontent', $block['content'], 15, 70);
672
            $textarea->setDescription('<span style="font-size:x-small;font-weight:bold;">' . \_AM_SYSTEM_BLOCKS_USEFULTAGS . '</span><br><span style="font-size:x-small;font-weight:normal;">' . \sprintf(_AM_BLOCKTAG1, '{X_SITEURL}', XOOPS_URL . '/') . '</span>');
673
            $form->addElement($textarea, true);
674
            $ctypeSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_CTYPE, 'bctype', $block['ctype']);
675
            $ctypeSelect->addOptionArray([
676
                                             'H' => \_AM_SYSTEM_BLOCKS_HTML,
677
                                             'P' => \_AM_SYSTEM_BLOCKS_PHP,
678
                                             'S' => \_AM_SYSTEM_BLOCKS_AFWSMILE,
679
                                             'T' => \_AM_SYSTEM_BLOCKS_AFNOSMILE,
680
                                         ]);
681
            $form->addElement($ctypeSelect);
682
        } else {
683
            if ('' !== $block['template']) {
684
                /** @var \XoopsTplfileHandler $tplfileHandler */
685
                $tplfileHandler = \xoops_getHandler('tplfile');
686
                $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $block['bid']);
687
                if (\count($btemplate) > 0) {
688
                    $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_CONTENT, '<a href="' . XOOPS_URL . '/modules/system/admin.php?fct=tplsets&amp;op=edittpl&amp;id=' . $btemplate[0]->getVar('tpl_id') . '">' . \_AM_SYSTEM_BLOCKS_EDITTPL . '</a>'));
689
                } else {
690
                    $btemplate2 = $tplfileHandler->find('default', 'block', $block['bid']);
691
                    if (\count($btemplate2) > 0) {
692
                        $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_CONTENT, '<a href="' . XOOPS_URL . '/modules/system/admin.php?fct=tplsets&amp;op=edittpl&amp;id=' . $btemplate2[0]->getVar('tpl_id') . '" target="_blank">' . \_AM_SYSTEM_BLOCKS_EDITTPL . '</a>'));
693
                    }
694
                }
695
            }
696
            if (false !== $block['edit_form']) {
697
                $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_OPTIONS, $block['edit_form']));
698
            }
699
        }
700
        $cache_select = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_BCACHETIME, 'bcachetime', $block['bcachetime']);
701
        $cache_select->addOptionArray([
702
                                          0       => _NOCACHE,
703
                                          30      => \sprintf(_SECONDS, 30),
704
                                          60      => _MINUTE,
705
                                          300     => \sprintf(_MINUTES, 5),
706
                                          1800    => \sprintf(_MINUTES, 30),
707
                                          3600    => _HOUR,
708
                                          18000   => \sprintf(_HOURS, 5),
709
                                          86400   => _DAY,
710
                                          259200  => \sprintf(_DAYS, 3),
711
                                          604800  => _WEEK,
712
                                          2592000 => _MONTH,
713
                                      ]);
714
        $form->addElement($cache_select);
715
716
        /** @var \XoopsGroupPermHandler $grouppermHandler */
717
        $grouppermHandler = \xoops_getHandler('groupperm');
718
        $groups           = $grouppermHandler->getGroupIds('block_read', $block['bid']);
719
720
        $form->addElement(new \XoopsFormSelectGroup(\_AM_SYSTEM_BLOCKS_GROUP, 'groups', true, $groups, 5, true));
721
722
        if (isset($block['bid'])) {
723
            $form->addElement(new \XoopsFormHidden('bid', $block['bid']));
724
        }
725
        $form->addElement(new \XoopsFormHidden('op', $block['op']));
726
        $form->addElement(new \XoopsFormHidden('fct', 'blocksadmin'));
727
        $buttonTray = new \XoopsFormElementTray('', '&nbsp;');
728
        if ($block['is_custom']) {
729
            $buttonTray->addElement(new \XoopsFormButton('', 'previewblock', _PREVIEW, 'submit'));
730
        }
731
732
        //Submit buttons
733
        $buttonTray   = new \XoopsFormElementTray('', '');
734
        $submitButton = new \XoopsFormButton('', 'submitblock', _SUBMIT, 'submit');
735
        $buttonTray->addElement($submitButton);
736
737
        $cancelButton = new \XoopsFormButton('', '', _CANCEL, 'button');
738
        $cancelButton->setExtra('onclick="history.go(-1)"');
739
        $buttonTray->addElement($cancelButton);
740
741
        $form->addElement($buttonTray);
742
        $form->display();
743
    }
744
}
745