Issues (3210)

htdocs/class/theme_blocks.php (2 issues)

Check for undefined variables.

Best Practice Comprehensibility Minor
1
<?php
2
/**
3
 * xos_logos_PageBuilder component class file
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-2025 XOOPS Project (https://xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @author              Skalpa Keo <[email protected]>
15
 * @since               2.3.0
16
 * @package             kernel
17
 */
18
19
/**
20
 * This file cannot be requested directly
21
 */
22
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
23
24
include_once $GLOBALS['xoops']->path('class/xoopsblock.php');
25
include_once $GLOBALS['xoops']->path('class/template.php');
26
27
/**
28
 * xos_logos_PageBuilder main class
29
 *
30
 * @package    xos_logos
31
 * @subpackage xos_logos_PageBuilder
32
 * @author     Skalpa Keo
33
 * @since      2.3.0
34
 */
35
class xos_logos_PageBuilder
36
{
37
    public $theme  = false;
38
    public $blocks = [];
39
40
    /**
41
     * xos_logos_PageBuilder::xoInit()
42
     *
43
     * @param array $options
44
     *
45
     * @return bool
46
     */
47
    public function xoInit($options = [])
48
    {
49
        $this->retrieveBlocks();
50
        if ($this->theme) {
51
            $this->theme->template->assignByRef('xoBlocks', $this->blocks);
52
        }
53
54
        return true;
55
    }
56
57
    /**
58
     * Called before a specific zone is rendered
59
     * @param string $zone
60
     */
61
    public function preRender($zone = '') {}
62
63
    /**
64
     * Called after a specific zone is rendered
65
     * @param string $zone
66
     */
67
    public function postRender($zone = '') {}
68
69
    /**
70
     * xos_logos_PageBuilder::retrieveBlocks()
71
     *
72
     * @return void
73
     */
74
    public function retrieveBlocks()
75
    {
76
        global $xoopsConfig;
77
        $xoopsPreload = XoopsPreload::getInstance();
78
79
        $startMod = ($xoopsConfig['startpage'] == '--') ? 'system' : $xoopsConfig['startpage'];
80
        if (isset($GLOBALS['xoopsModule']) && is_object($GLOBALS['xoopsModule'])) {
81
            [$mid, $dirname] = [
82
                $GLOBALS['xoopsModule']->getVar('mid'),
83
                $GLOBALS['xoopsModule']->getVar('dirname')];
84
            $isStart = (substr($_SERVER['PHP_SELF'], -9) === 'index.php' && $xoopsConfig['startpage'] == $dirname && empty($_SERVER['QUERY_STRING']));
85
        } else {
86
            [$mid, $dirname] = [
87
                0,
88
                'system'];
89
            $isStart = !empty($GLOBALS['xoopsOption']['show_cblock']);
90
        }
91
92
        $groups = (isset($GLOBALS['xoopsUser']) && is_object($GLOBALS['xoopsUser'])) ? $GLOBALS['xoopsUser']->getGroups() : [
93
            XOOPS_GROUP_ANONYMOUS,
94
        ];
95
96
        $oldzones = [
97
            XOOPS_SIDEBLOCK_LEFT          => 'canvas_left',
98
            XOOPS_SIDEBLOCK_RIGHT         => 'canvas_right',
99
            XOOPS_CENTERBLOCK_LEFT        => 'page_topleft',
100
            XOOPS_CENTERBLOCK_CENTER      => 'page_topcenter',
101
            XOOPS_CENTERBLOCK_RIGHT       => 'page_topright',
102
            XOOPS_CENTERBLOCK_BOTTOMLEFT  => 'page_bottomleft',
103
            XOOPS_CENTERBLOCK_BOTTOM      => 'page_bottomcenter',
104
            XOOPS_CENTERBLOCK_BOTTOMRIGHT => 'page_bottomright',
105
            XOOPS_FOOTERBLOCK_LEFT        => 'footer_left',
106
            XOOPS_FOOTERBLOCK_RIGHT       => 'footer_right',
107
            XOOPS_FOOTERBLOCK_CENTER      => 'footer_center',
108
            XOOPS_FOOTERBLOCK_ALL         => 'footer_all',
109
        ];
110
111
        foreach ($oldzones as $zone) {
112
            $this->blocks[$zone] = [];
113
        }
114
        if ($this->theme) {
115
            $template = & $this->theme->template;
116
            $backup   = [
117
                $template->caching,
118
                $template->cache_lifetime,
119
            ];
120
        } else {
121
            $template = null;
122
            $template = new XoopsTpl();
123
        }
124
        $xoopsblock = new XoopsBlock();
125
        $block_arr  = [];
126
        $block_arr  = $xoopsblock->getAllByGroupModule($groups, $mid, $isStart, XOOPS_BLOCK_VISIBLE);
127
        $xoopsPreload->triggerEvent('core.class.theme_blocks.retrieveBlocks', [&$this, &$template, &$block_arr]);
128
        foreach ($block_arr as $block) {
129
            $side = $oldzones[$block->getVar('side')];
130
            if ($var = $this->buildBlock($block, $template)) {
131
                $this->blocks[$side][$var['id']] = $var;
132
            }
133
        }
134
        if ($this->theme) {
135
            [$template->caching, $template->cache_lifetime] = $backup;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $backup does not seem to be defined for all execution paths leading up to this point.
Loading history...
136
        }
137
    }
138
139
    /**
140
     * xos_logos_PageBuilder::generateCacheId()
141
     *
142
     * @param  mixed $cache_id
143
     * @return mixed
144
     */
145
    public function generateCacheId($cache_id)
146
    {
147
        if ($this->theme) {
148
            $cache_id = $this->theme->generateCacheId($cache_id);
149
        }
150
151
        return $cache_id;
152
    }
153
154
    /**
155
     * xos_logos_PageBuilder::buildBlock()
156
     *
157
     * @param  mixed $xobject
158
     * @param  mixed $template
159
     * @return array|bool
160
     */
161
    public function buildBlock($xobject, $template)
162
    {
163
        // The lame type workaround will change
164
        // bid is added temporarily as workaround for specific block manipulation
165
        $block = [
166
            'id'      => $xobject->getVar('bid'),
167
            'module'  => $xobject->getVar('dirname'),
168
            'title'   => $xobject->getVar('title'),
169
            // 'name'        => strtolower( preg_replace( '/[^0-9a-zA-Z_]/', '', str_replace( ' ', '_', $xobject->getVar( 'name' ) ) ) ),
170
            'weight'  => $xobject->getVar('weight'),
171
            'lastmod' => $xobject->getVar('last_modified'),
172
        ];
173
174
        // title is a comment, don't show it
175
        if (0 === strpos($block['title'], '// ')) {
176
            $block['title'] = '';
177
        }
178
179
        $bcachetime = (int) $xobject->getVar('bcachetime');
180
        if (empty($bcachetime)) {
181
            $template->caching = 0;
182
        } else {
183
            $template->caching        = 2;
184
            $template->cache_lifetime = $bcachetime;
185
        }
186
        $template->setCompileId($xobject->getVar('dirname', 'n'));
187
        $tplName = ($tplName = $xobject->getVar('template')) ? "db:$tplName" : 'db:system_block_dummy.tpl';
188
        $cacheid = $this->generateCacheId('blk_' . $xobject->getVar('bid'));
189
190
        $xoopsLogger = XoopsLogger::getInstance();
191
        if (!$bcachetime || !$template->isCached($tplName, $cacheid)) {
192
193
            //Get theme metas
194
            if ($this->theme && $bcachetime) {
195
                foreach ($this->theme->metas as $type => $value) {
196
                    $old[$type] = $this->theme->metas[$type];
197
                }
198
            }
199
200
            //build block
201
            $xoopsLogger->addBlock($xobject->getVar('name'));
202
            if ($bresult = $xobject->buildBlock()) {
203
                $template->assign('block', $bresult);
204
                $block['content'] = $template->fetch($tplName, $cacheid);
205
            } else {
206
                $block = false;
207
            }
208
209
            //check if theme added new metas
210
            if ($this->theme && $bcachetime) {
211
                $metas = [];
212
                foreach ($this->theme->metas as $type => $value) {
213
                    $dif = array_diff_key($this->theme->metas[$type], $old[$type]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $old does not seem to be defined for all execution paths leading up to this point.
Loading history...
214
                    if (count($dif)) {
215
                        $metas[$type] = $dif;
216
                    }
217
                }
218
                if (count($metas)) {
219
                    xoops_load('xoopscache');
220
                    $cache = XoopsCache::getInstance();
221
                    $cache->write($cacheid, $metas);
222
                }
223
            }
224
        } else {
225
            $xoopsLogger->addBlock($xobject->getVar('name'), true, $bcachetime);
226
            $block['content'] = $template->fetch($tplName, $cacheid);
227
        }
228
229
        //add block cached metas
230
        if ($this->theme && $bcachetime) {
231
            xoops_load('xoopscache');
232
            $cache = XoopsCache::getInstance();
233
            if ($metas = $cache->read($cacheid)) {
234
                foreach ($metas as $type => $value) {
235
                    $this->theme->metas[$type] = array_merge($this->theme->metas[$type], $metas[$type]);
236
                }
237
            }
238
        }
239
240
        $template->setCompileId();
241
242
        return $block;
243
    }
244
}
245