SystemBlock::isCustom()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Block Class Manager
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             system
15
 */
16
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
17
18
require_once XOOPS_ROOT_PATH . '/kernel/block.php';
19
20
/**
21
 * System Block
22
 *
23
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
24
 * @package             system
25
 */
26
class SystemBlock extends XoopsBlock
27
{
28
    /**
29
     *
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * @param string $mode
38
     *
39
     * @return XoopsThemeForm
40
     */
41
    public function getForm($mode = 'edit')
42
    {
43
        if ($this->isNew()) {
44
            $title   = _AM_SYSTEM_BLOCKS_ADDBLOCK;
45
            $modules = array(-1);
46
            $groups  = array(XOOPS_GROUP_USERS, XOOPS_GROUP_ANONYMOUS, XOOPS_GROUP_ADMIN);
47
            $this->setVar('block_type', 'C');
48
            $this->setVar('visible', 1);
49
            $op = 'save';
50
        } else {
51
            // Search modules
52
            /** @var  SystemBlockLinkModuleHandler $blocklinkmodule_handler */
53
            $blocklinkmodule_handler = xoops_getModuleHandler('blocklinkmodule');
54
            $criteria                = new CriteriaCompo(new Criteria('block_id', $this->getVar('bid')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('bid') can also be of type array and array; however, parameter $value of Criteria::__construct() 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

54
            $criteria                = new CriteriaCompo(new Criteria('block_id', /** @scrutinizer ignore-type */ $this->getVar('bid')));
Loading history...
55
            $blocklinkmodule         = $blocklinkmodule_handler->getObjects($criteria);
56
            foreach ($blocklinkmodule as $link) {
57
                /** @var  SystemBlockLinkModule $link */
58
                $modules[] = $link->getVar('module_id');
59
            }
60
            // Search perms
61
            /** @var XoopsGroupPermHandler $groupperm_handler */
62
            $groupperm_handler = xoops_getHandler('groupperm');
63
            $groups            = $groupperm_handler->getGroupIds('block_read', $this->getVar('bid'));
64
            switch ($mode) {
65
                case 'edit':
66
                    $title = _AM_SYSTEM_BLOCKS_EDITBLOCK;
67
                    break;
68
                case 'clone':
69
                    $title = _AM_SYSTEM_BLOCKS_CLONEBLOCK;
70
                    $this->setVar('bid', 0);
71
                    if ($this->isCustom()) {
72
                        $this->setVar('block_type', 'C');
73
                    } else {
74
                        $this->setVar('block_type', 'D');
75
                    }
76
                    break;
77
            }
78
            $op = 'save';
79
        }
80
        $form = new XoopsThemeForm($title, 'blockform', 'admin.php', 'post', true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
81
        if (!$this->isNew()) {
82
			$form->addElement(new XoopsFormLabel(_AM_SYSTEM_BLOCKS_NAME, $this->getVar('name') . ' [' . $this->getVar('dirname') . ']'));
83
        }
84
        // Side position
85
        $side_select = new XoopsFormSelect(_AM_SYSTEM_BLOCKS_TYPE, 'side', $this->getVar('side'));
86
        $side_select->addOptionArray(array(
87
                                         0  => _AM_SYSTEM_BLOCKS_SBLEFT,
88
                                         1  => _AM_SYSTEM_BLOCKS_SBRIGHT,
89
                                         3  => _AM_SYSTEM_BLOCKS_CBLEFT,
90
                                         4  => _AM_SYSTEM_BLOCKS_CBRIGHT,
91
                                         5  => _AM_SYSTEM_BLOCKS_CBCENTER,
92
                                         7  => _AM_SYSTEM_BLOCKS_CBBOTTOMLEFT,
93
                                         8  => _AM_SYSTEM_BLOCKS_CBBOTTOMRIGHT,
94
                                         9  => _AM_SYSTEM_BLOCKS_CBBOTTOM,
95
                                         10 => _AM_SYSTEM_BLOCKS_CBFOOTERLEFT,
96
                                         11 => _AM_SYSTEM_BLOCKS_CBFOOTERRIGHT,
97
                                         12 => _AM_SYSTEM_BLOCKS_CBFOOTERCENTER));
98
99
        $form->addElement($side_select);
100
        // Order
101
        $form->addElement(new XoopsFormText(_AM_SYSTEM_BLOCKS_WEIGHT, 'weight', 2, 5, $this->getVar('weight')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('weight') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() 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

101
        $form->addElement(new XoopsFormText(_AM_SYSTEM_BLOCKS_WEIGHT, 'weight', 2, 5, /** @scrutinizer ignore-type */ $this->getVar('weight')));
Loading history...
102
        // Display
103
        $form->addElement(new XoopsFormRadioYN(_AM_SYSTEM_BLOCKS_VISIBLE, 'visible', $this->getVar('visible')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('visible') can also be of type array and array; however, parameter $value of XoopsFormRadioYN::__construct() 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

103
        $form->addElement(new XoopsFormRadioYN(_AM_SYSTEM_BLOCKS_VISIBLE, 'visible', /** @scrutinizer ignore-type */ $this->getVar('visible')));
Loading history...
104
        // Visible In
105
        $mod_select     = new XoopsFormSelect(_AM_SYSTEM_BLOCKS_VISIBLEIN, 'modules', $modules, 5, true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modules does not seem to be defined for all execution paths leading up to this point.
Loading history...
106
        /** @var XoopsModuleHandler $module_handler */
107
        $module_handler = xoops_getHandler('module');
108
        $criteria       = new CriteriaCompo(new Criteria('hasmain', 1));
109
        $criteria->add(new Criteria('isactive', 1));
110
        $module_list     = $module_handler->getList($criteria);
111
        $module_list[-1] = _AM_SYSTEM_BLOCKS_TOPPAGE;
112
        $module_list[0]  = _AM_SYSTEM_BLOCKS_ALLPAGES;
113
        ksort($module_list);
114
        $mod_select->addOptionArray($module_list);
115
        $form->addElement($mod_select);
116
        // Title
117
        $form->addElement(new XoopsFormText(_AM_SYSTEM_BLOCKS_TITLE, 'title', 50, 255, $this->getVar('title')), false);
118
        if ($this->isNew() || $this->isCustom()) {
119
            $editor_configs           = array();
120
            $editor_configs['name']   = 'content_block';
121
            $editor_configs['value']  = $this->getVar('content', 'e');
122
            $editor_configs['rows']   = 20;
123
            $editor_configs['cols']   = 100;
124
            $editor_configs['width']  = '100%';
125
            $editor_configs['height'] = '400px';
126
            $editor_configs['editor'] = xoops_getModuleOption('blocks_editor', 'system');
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() 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

126
            $editor_configs['editor'] = /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('blocks_editor', 'system');
Loading history...
127
            $form->addElement(new XoopsFormEditor(_AM_SYSTEM_BLOCKS_CONTENT, 'content_block', $editor_configs), true);
128
            if (in_array($editor_configs['editor'], array('dhtmltextarea', 'textarea'))) {
129
                $ctype_select = new XoopsFormSelect(_AM_SYSTEM_BLOCKS_CTYPE, 'c_type', $this->getVar('c_type'));
130
                $ctype_select->addOptionArray(array(
131
                                                  'H' => _AM_SYSTEM_BLOCKS_HTML,
132
                                                  'P' => _AM_SYSTEM_BLOCKS_PHP,
133
                                                  'S' => _AM_SYSTEM_BLOCKS_AFWSMILE,
134
                                                  'T' => _AM_SYSTEM_BLOCKS_AFNOSMILE));
135
                $form->addElement($ctype_select);
136
            } else {
137
                $form->addElement(new XoopsFormHidden('c_type', 'H'));
138
            }
139
        } else {
140
            if ($this->getVar('template') !== '') {
141
                $tplfile_handler = xoops_getHandler('tplfile');
142
                $btemplate       = $tplfile_handler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $this->getVar('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

142
                /** @scrutinizer ignore-call */ 
143
                $btemplate       = $tplfile_handler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $this->getVar('bid'));
Loading history...
143
                if (count($btemplate) > 0) {
144
                    $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>'));
145
                } else {
146
                    $btemplate2 = $tplfile_handler->find('default', 'block', $this->getVar('bid'));
147
                    if (count($btemplate2) > 0) {
148
                        $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') . '" rel="external">' . _AM_SYSTEM_BLOCKS_EDITTPL . '</a>'));
149
                    }
150
                }
151
            }
152
            if ($this->getOptions() !== false) {
153
                $form->addElement(new XoopsFormLabel(_AM_SYSTEM_BLOCKS_OPTIONS, $this->getOptions()));
0 ignored issues
show
Bug introduced by
It seems like $this->getOptions() can also be of type true; however, parameter $value of XoopsFormLabel::__construct() 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

153
                $form->addElement(new XoopsFormLabel(_AM_SYSTEM_BLOCKS_OPTIONS, /** @scrutinizer ignore-type */ $this->getOptions()));
Loading history...
154
            } else {
155
                $form->addElement(new XoopsFormHidden('options', $this->getVar('options')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('options') can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() 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

155
                $form->addElement(new XoopsFormHidden('options', /** @scrutinizer ignore-type */ $this->getVar('options')));
Loading history...
156
            }
157
            $form->addElement(new XoopsFormHidden('c_type', 'H'));
158
        }
159
        $cache_select = new XoopsFormSelect(_AM_SYSTEM_BLOCKS_BCACHETIME, 'bcachetime', $this->getVar('bcachetime'));
160
        $cache_select->addOptionArray(array(
161
                                          '0'       => _NOCACHE,
162
                                          '30'      => sprintf(_SECONDS, 30),
163
                                          '60'      => _MINUTE,
164
                                          '300'     => sprintf(_MINUTES, 5),
165
                                          '1800'    => sprintf(_MINUTES, 30),
166
                                          '3600'    => _HOUR,
167
                                          '18000'   => sprintf(_HOURS, 5),
168
                                          '86400'   => _DAY,
169
                                          '259200'  => sprintf(_DAYS, 3),
170
                                          '604800'  => _WEEK,
171
                                          '2592000' => _MONTH));
172
        $form->addElement($cache_select);
173
        // Groups
174
        $form->addElement(new XoopsFormSelectGroup(_AM_SYSTEM_BLOCKS_GROUP, 'groups', true, $groups, 5, true));
175
176
        $form->addElement(new XoopsFormHidden('block_type', $this->getVar('block_type')));
177
        $form->addElement(new XoopsFormHidden('mid', $this->getVar('mid')));
178
        $form->addElement(new XoopsFormHidden('func_num', $this->getVar('func_num')));
179
        $form->addElement(new XoopsFormHidden('func_file', $this->getVar('func_file')));
180
        $form->addElement(new XoopsFormHidden('show_func', $this->getVar('show_func')));
181
        $form->addElement(new XoopsFormHidden('edit_func', $this->getVar('edit_func')));
182
        $form->addElement(new XoopsFormHidden('template', $this->getVar('template')));
183
        $form->addElement(new XoopsFormHidden('dirname', $this->getVar('dirname')));
184
        $form->addElement(new XoopsFormHidden('name', $this->getVar('name')));
185
        $form->addElement(new XoopsFormHidden('bid', $this->getVar('bid')));
186
        $form->addElement(new XoopsFormHidden('op', $op));
187
        $form->addElement(new XoopsFormHidden('fct', 'blocksadmin'));
188
        $button_tray = new XoopsFormElementTray('', '&nbsp;');
189
        if ($this->isNew() || $this->isCustom()) {
190
            $preview = new XoopsFormButton('', 'previewblock', _PREVIEW, 'preview');
191
            $preview->setExtra("onclick=\"blocks_preview();\"");
192
            $button_tray->addElement($preview);
193
        }
194
        $button_tray->addElement(new XoopsFormButton('', 'submitblock', _SUBMIT, 'submit'));
195
        $form->addElement($button_tray);
196
197
        return $form;
198
    }
199
200
    /**
201
     * XoopsBlock::getOptions()
202
     *
203
     * @return bool|string
204
     */
205
    public function getOptions()
206
    {
207
        global $xoopsConfig;
208
        if (!$this->isCustom()) {
209
            $edit_func = $this->getVar('edit_func');
210
            if (!$edit_func) {
211
                return false;
212
            }
213
            if (file_exists($GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')))) {
214
                if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/blocks.php'))) {
215
                    include_once $file;
216
                } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/language/english/blocks.php'))) {
217
                    include_once $file;
218
                }
219
                include_once $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file'));
220
                $options   = explode('|', $this->getVar('options'));
221
                $edit_form = $edit_func($options);
222
                if (!$edit_form) {
223
                    return false;
224
                }
225
226
                return $edit_form;
227
            } else {
228
                return false;
229
            }
230
        } else {
231
            return false;
232
        }
233
    }
234
235
    /**
236
     * @return bool
237
     */
238
    public function isCustom()
239
    {
240
        return $this->getVar('block_type') === 'C';
241
    }
242
243
    /**
244
     * do stripslashes/htmlspecialchars according to the needed output
245
     *
246
     * @param string $format output use: S for Show and E for Edit
247
     * @param string $c_type type of block content
248
     *
249
     * @returns string
250
     */
251
    public function getContent($format = 's', $c_type = 'T')
252
    {
253
        $format = strtolower($format);
254
        $c_type = strtoupper($c_type);
255
        switch ($format) {
256
            case 's':
257
                // check the type of content
258
                // H : custom HTML block
259
                // P : custom PHP block
260
                // S : use text sanitizater (smilies enabled)
261
                // T : use text sanitizater (smilies disabled)
262
                if ($c_type === 'H') {
263
                    return str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
264
                } elseif ($c_type === 'P') {
265
                    ob_start();
266
                    echo eval($this->getVar('content', 'n'));
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
267
                    $content = ob_get_contents();
268
                    ob_end_clean();
269
270
                    return str_replace('{X_SITEURL}', XOOPS_URL . '/', $content);
271
                } elseif ($c_type === 'S') {
272
                    $myts    = \MyTextSanitizer::getInstance();
273
                    $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
274
275
                    return $myts->displayTarea($content, 1, 1);
276
                } else {
277
                    $myts    = \MyTextSanitizer::getInstance();
278
                    $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
279
280
                    return $myts->displayTarea($content, 1, 0);
281
                }
282
                break;
283
            case 'e':
284
                return $this->getVar('content', 'e');
285
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
286
            default:
287
                return $this->getVar('content', 'n');
288
                break;
289
        }
290
    }
291
}
292
293
/**
294
 * System block handler class.
295
 *
296
 * This class is responsible for providing data access mechanisms to the data source
297
 * of XOOPS block class objects.
298
 *
299
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
300
 * @package             system
301
 * @subpackage          blocks
302
 */
303
class SystemBlockHandler extends XoopsPersistableObjectHandler
304
{
305
    /**
306
     * @param null|XoopsDatabase $db
307
     */
308
    public function __construct(XoopsDatabase $db)
309
    {
310
        parent::__construct($db, 'newblocks', 'SystemBlock', 'bid', 'title');
311
    }
312
313
    /**
314
     * @param XoopsObject|SystemBlock $obj
315
     *
316
     * @return int|bool object id on success, otherwise false
317
     */
318
    public function insert(XoopsObject $obj, $force = true)
319
    {
320
        if (!($obj instanceof $this->className)) {
321
            return false;
322
        }
323
        $obj->setVar('last_modified', time());
324
325
        return parent::insert($obj, $force);
326
    }
327
328
    /**
329
     * retrieve array of {@link XoopsBlock}s meeting certain conditions
330
     *
331
     * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} with conditions for the blocks
332
     * @param bool $id_as_key           should the blocks' bid be the key for the returned array?
333
     * @param bool $as_object           return an array of objects
334
     *
335
     * @return array {@link XoopsBlock}s matching the conditions
336
     **/
337
    public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
338
    {
339
        $ret   = array();
340
        $limit = $start = 0;
341
        $sql   = 'SELECT DISTINCT(b.bid), b.* FROM ' . $this->db->prefix('newblocks') . ' b LEFT JOIN ' . $this->db->prefix('block_module_link') . ' l ON b.bid=l.block_id';
342
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
343
            $sql .= ' ' . $criteria->renderWhere();
344
            $limit = $criteria->getLimit();
345
            $start = $criteria->getStart();
346
        }
347
        $result = $this->db->query($sql, $limit, $start);
0 ignored issues
show
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

347
        /** @scrutinizer ignore-call */ 
348
        $result = $this->db->query($sql, $limit, $start);
Loading history...
348
        if (!$this->db->isResultSet($result)) {
349
            return $ret;
350
        }
351
352
        if ($as_object) {
353
            /** @var array $myrow */
354
            while (false !== ($myrow = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
The method fetchArray() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

354
            while (false !== ($myrow = $this->db->/** @scrutinizer ignore-call */ fetchArray($result))) {
Loading history...
355
                $object = $this->create(false);
356
                $object->assignVars($myrow);
357
                if ($id_as_key) {
358
                    $ret[$myrow[$this->keyName]] = $object;
359
                } else {
360
                    $ret[] = $object;
361
                }
362
                unset($object);
363
            }
364
        } else {
365
            $object = $this->create(false);
366
            /** @var array $myrow */
367
            while (false !== ($myrow = $this->db->fetchArray($result))) {
368
                $object->assignVars($myrow);
369
                if ($id_as_key) {
370
                    $ret[$myrow[$this->keyName]] = $object->getValues(array_keys($myrow));
371
                } else {
372
                    $ret[] = $object->getValues(array_keys($myrow));
373
                }
374
            }
375
            unset($object);
376
        }
377
378
        return $ret;
379
    }
380
381
    /**
382
     * get all the blocks that match the supplied parameters
383
     *
384
     * @param int|int[] $groupid  groupid (can be an array)
385
     * @param bool $asobject
386
     * @param int|string $side
387
     *                            0: sideblock - left
388
     *                            1: sideblock - right
389
     *                            2: sideblock - left and right
390
     *                            3: centerblock - left
391
     *                            4: centerblock - right
392
     *                            5: centerblock - center
393
     *                            6: centerblock - left, right, center
394
     * @param           $visible  0: not visible 1: visible
0 ignored issues
show
Documentation Bug introduced by
The doc comment 0: not at position 0 could not be parsed: Unknown type name '0' at position 0 in 0: not.
Loading history...
395
     * @param string $orderby     order of the blocks
396
     * @param int $isactive
397
     *
398
     * @return array of block objects
399
     */
400
    public function getAllBlocksByGroup($groupid, $asobject = true, $side = null, $visible = null, $orderby = 'b.weight,b.bid', $isactive = 1)
401
    {
402
        /** @var XoopsMySQLDatabase $db */
403
        $db  = XoopsDatabaseFactory::getDatabaseConnection();
404
        $ret = array();
405
        $sql = 'SELECT b.* ';
406
        if (!$asobject) {
407
            $sql = 'SELECT b.bid ';
408
        }
409
        $sql .= 'FROM ' . $db->prefix('newblocks') . ' b LEFT JOIN ' . $db->prefix('group_permission') . " l ON l.gperm_itemid=b.bid WHERE gperm_name = 'block_read' AND gperm_modid = 1";
410
        if (is_array($groupid)) {
411
            $sql .= ' AND (l.gperm_groupid=' . $groupid[0] . '';
412
            $size = count($groupid);
413
            if ($size > 1) {
414
                for ($i = 1; $i < $size; ++$i) {
415
                    $sql .= ' OR l.gperm_groupid=' . $groupid[$i] . '';
416
                }
417
            }
418
            $sql .= ')';
419
        } else {
420
            $sql .= ' AND l.gperm_groupid=' . $groupid . '';
421
        }
422
        $sql .= ' AND b.isactive=' . $isactive;
423
        if (isset($side)) {
424
            // get both sides in sidebox? (some themes need this)
425
            if ($side === XOOPS_SIDEBLOCK_BOTH) {
426
                $side = '(b.side=0 OR b.side=1)';
427
            } elseif ($side === XOOPS_CENTERBLOCK_ALL) {
428
                $side = '(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )';
429
            } elseif ($side === XOOPS_FOOTERBLOCK_ALL) {
430
                $side = '(b.side=10 OR b.side=11 OR b.side=12 )';
431
            } else {
432
                $side = 'b.side=' . $side;
433
            }
434
            $sql .= ' AND ' . $side;
435
        }
436
        if (isset($visible)) {
437
            $sql .= " AND b.visible=$visible";
438
        }
439
        $sql .= " ORDER BY $orderby";
440
        $result = $db->query($sql);
441
        if (!$db->isResultSet($result)) {
442
            throw new \RuntimeException(
443
                \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
444
            );
445
        }
446
        $added  = array();
447
        while (false !== ($myrow = $db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
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

447
        while (false !== ($myrow = $db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
448
            if (!in_array($myrow['bid'], $added)) {
449
                if (!$asobject) {
450
                    $ret[] = $myrow['bid'];
451
                } else {
452
                    $ret[] = new XoopsBlock($myrow);
453
                }
454
                $added[] = $myrow['bid'];
455
            }
456
        }
457
458
        return $ret;
459
    }
460
461
    /**
462
     * @param $groupid
463
     *
464
     * @return array
465
     */
466
    public function getBlockByPerm($groupid)
467
    {
468
        if (isset($groupid)) {
469
            $sql = 'SELECT DISTINCT gperm_itemid FROM ' . $this->db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1";
470
            if (is_array($groupid)) {
471
                $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')';
472
            } else {
473
                if ((int)$groupid > 0) {
474
                    $sql .= ' AND gperm_groupid=' . (int)$groupid;
475
                }
476
            }
477
            $result = $this->db->query($sql);
478
            if (!$this->db->isResultSet($result)) {
479
                throw new \RuntimeException(
480
                    \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
0 ignored issues
show
Bug introduced by
The method error() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

480
                    \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->/** @scrutinizer ignore-call */ error(), E_USER_ERROR
Loading history...
481
                );
482
            }
483
            $blockids = array();
484
            /** @var array $myrow */
485
            while (false !== ($myrow = $this->db->fetchArray($result))) {
486
                $blockids[] = $myrow['gperm_itemid'];
487
            }
488
            if (empty($blockids)) {
489
                return $blockids;
490
            }
491
492
            return $blockids;
493
        }
494
495
        return null;
496
    }
497
498
    /**
499
     * @param        $groupid
500
     * @param int $module_id
501
     * @param bool $toponlyblock
502
     * @param null $visible
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $visible is correct as it would always require null to be passed?
Loading history...
503
     * @param string $orderby
504
     * @param int $isactive
505
     *
506
     * @return array
507
     */
508
    public function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)
509
    {
510
        $isactive = (int)$isactive;
511
        $db       = $GLOBALS['xoopsDB'];
512
        $ret      = array();
513
        if (isset($groupid)) {
514
            $sql = 'SELECT DISTINCT gperm_itemid FROM ' . $db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1";
515
            if (is_array($groupid)) {
516
                $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')';
517
            } else {
518
                if ((int)$groupid > 0) {
519
                    $sql .= ' AND gperm_groupid=' . (int)$groupid;
520
                }
521
            }
522
            $result = $db->query($sql);
523
            if (!$db->isResultSet($result)) {
524
                throw new \RuntimeException(
525
                    \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
526
                );
527
            }
528
            $blockids = array();
529
            while (false !== ($myrow = $db->fetchArray($result))) {
530
                $blockids[] = $myrow['gperm_itemid'];
531
            }
532
            if (empty($blockids)) {
533
                return $blockids;
534
            }
535
        }
536
        $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid';
537
        $sql .= ' AND b.isactive=' . $isactive;
538
        if (isset($visible)) {
539
            $sql .= ' AND b.visible=' . (int)$visible;
540
        }
541
        if (!isset($module_id)) {
542
        } elseif (!empty($module_id)) {
543
            $sql .= ' AND m.module_id IN (0,' . (int)$module_id;
544
            if ($toponlyblock) {
545
                $sql .= ',-1';
546
            }
547
            $sql .= ')';
548
        } else {
549
            if ($toponlyblock) {
550
                $sql .= ' AND m.module_id IN (0,-1)';
551
            } else {
552
                $sql .= ' AND m.module_id=0';
553
            }
554
        }
555
        if (!empty($blockids)) {
556
            $sql .= ' AND b.bid IN (' . implode(',', $blockids) . ')';
557
        }
558
        $sql .= ' ORDER BY ' . $orderby;
559
        $result = $db->query($sql);
560
        if (!$db->isResultSet($result)) {
561
            throw new \RuntimeException(
562
                \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
563
            );
564
        }
565
        while (false !== ($myrow = $db->fetchArray($result))) {
566
            $block              = new XoopsBlock($myrow);
567
            $ret[$myrow['bid']] =& $block;
568
            unset($block);
569
        }
570
571
        return $ret;
572
    }
573
574
    /**
575
     * @param int $module_id
576
     * @param bool $toponlyblock
577
     * @param null $visible
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $visible is correct as it would always require null to be passed?
Loading history...
578
     * @param string $orderby
579
     * @param int $isactive
580
     *
581
     * @return array
582
     */
583
    public function getNonGroupedBlocks($module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)
584
    {
585
        $db   = $GLOBALS['xoopsDB'];
586
        $ret  = array();
587
        $bids = array();
588
        $sql  = 'SELECT DISTINCT(bid) from ' . $db->prefix('newblocks');
589
        $result = $db->query($sql);
590
        if ($db->isResultSet($result)) {
591
            while (false !== ($myrow = $db->fetchArray($result))) {
592
                $bids[] = $myrow['bid'];
593
            }
594
        }
595
596
        $sql     = 'SELECT DISTINCT(p.gperm_itemid) from ' . $db->prefix('group_permission') . ' p, ' . $db->prefix('groups') . " g WHERE g.groupid=p.gperm_groupid AND p.gperm_name='block_read'";
597
        $grouped = array();
598
        $result = $db->query($sql);
599
        if ($db->isResultSet($result)) {
600
            while (false !== ($myrow = $db->fetchArray($result))) {
601
                $grouped[] = $myrow['gperm_itemid'];
602
            }
603
        }
604
605
        $non_grouped = array_diff($bids, $grouped);
606
        if (!empty($non_grouped)) {
607
            $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid';
608
            $sql .= ' AND b.isactive=' . (int)$isactive;
609
            if (isset($visible)) {
610
                $sql .= ' AND b.visible=' . (int)$visible;
611
            }
612
            if (!isset($module_id)) {
613
            } elseif (!empty($module_id)) {
614
                $sql .= ' AND m.module_id IN (0,' . (int)$module_id;
615
                if ($toponlyblock) {
616
                    $sql .= ',-1';
617
                }
618
                $sql .= ')';
619
            } else {
620
                if ($toponlyblock) {
621
                    $sql .= ' AND m.module_id IN (0,-1)';
622
                } else {
623
                    $sql .= ' AND m.module_id=0';
624
                }
625
            }
626
            $sql .= ' AND b.bid IN (' . implode(',', $non_grouped) . ')';
627
            $sql .= ' ORDER BY ' . $orderby;
628
            $result = $db->query($sql);
629
            if (!$db->isResultSet($result)) {
630
                throw new \RuntimeException(
631
                    \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
632
                );
633
            }
634
            while (false !== ($myrow = $db->fetchArray($result))) {
635
                $block              = new XoopsBlock($myrow);
636
                $ret[$myrow['bid']] =& $block;
637
                unset($block);
638
            }
639
        }
640
641
        return $ret;
642
    }
643
644
    /**
645
     * XoopsBlock::countSimilarBlocks()
646
     *
647
     * @param  mixed $moduleId
648
     * @param  mixed $funcNum
649
     * @param  mixed $showFunc
650
     * @return int
651
     */
652
    public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
653
    {
654
        $funcNum  = (int)$funcNum;
655
        $moduleId = (int)$moduleId;
656
        if ($funcNum < 1 || $moduleId < 1) {
657
            // invalid query
658
            return 0;
659
        }
660
        /** @var XoopsMySQLDatabase $db */
661
        $db = XoopsDatabaseFactory::getDatabaseConnection();
662
        if (isset($showFunc)) {
663
            // showFunc is set for more strict comparison
664
            $sql = sprintf('SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d AND show_func = %s', $db->prefix('newblocks'), $moduleId, $funcNum, $db->quoteString(trim($showFunc)));
665
        } else {
666
            $sql = sprintf('SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d', $db->prefix('newblocks'), $moduleId, $funcNum);
667
        }
668
        $result = $db->query($sql);
669
        if (!$db->isResultSet($result)) {
670
            // throw new \RuntimeException(
671
            //       \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
672
            // );
673
            return 0;
674
        }
675
        list($count) = $db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() 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

675
        list($count) = $db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
676
677
        return (int)$count;
678
    }
679
}
680