Passed
Push — master ( 81ba93...c6c854 )
by Michael
03:30
created

include/blocksadmin.inc.php (2 issues)

Labels
Severity
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 (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
/**
21
 * Module: SmartFAQ
22
 * Author: The SmartFactory <www.smartfactory.ca>
23
 * Licence: GNU
24
 */
25
26
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
27
28
if (!is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid())) {
29
    exit('Access Denied');
30
}
31
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
32
require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blocksadmin.php';
33
34
$op = 'list';
35
if (isset($_POST)) {
36
    foreach ($_POST as $k => $v) {
37
        ${$k} = $v;
38
    }
39
}
40
41
if (\Xmf\Request::hasVar('op', 'GET')) {
42
    if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op']
43
        || 'clone' === $_GET['op']
44
        || 'previewpopup' === $_GET['op']) {
45
        $op  = $_GET['op'];
46
        $bid = \Xmf\Request::getInt('bid', 0, 'GET');
47
    }
48
}
49
50
if (isset($previewblock)) {
51
    xoops_cp_header();
52
    require_once XOOPS_ROOT_PATH . '/class/template.php';
53
    $xoopsTpl          = new \XoopsTpl();
54
    $xoopsTpl->caching = 0;
55
    if (isset($bid)) {
56
        $block['bid']        = $bid;
57
        $block['form_title'] = _AM_EDITBLOCK;
58
        $myblock             = new \XoopsBlock($bid);
59
        $block['name']       = $myblock->getVar('name');
60
    } else {
61
        if ('save' === $op) {
62
            $block['form_title'] = _AM_ADDBLOCK;
63
        } else {
64
            $block['form_title'] = _AM_CLONEBLOCK;
65
        }
66
        $myblock = new \XoopsBlock();
67
        $myblock->setVar('block_type', 'C');
68
    }
69
    $myts = \MyTextSanitizer::getInstance();
70
    $myblock->setVar('title', $myts->stripSlashesGPC($btitle));
71
    $myblock->setVar('content', $myts->stripSlashesGPC($bcontent));
72
    $dummyhtml = '<html><head><meta http-equiv="content-type" content="text/html; charset='
73
                 . _CHARSET
74
                 . '"><meta http-equiv="content-language" content="'
75
                 . _LANGCODE
76
                 . '"><title>'
77
                 . $xoopsConfig['sitename']
78
                 . '</title><link rel="stylesheet" type="text/css" media="all" href="'
79
                 . getcss($xoopsConfig['theme_set'])
80
                 . '"></head><body><table><tr><th>'
81
                 . $myblock->getVar('title')
82
                 . '</th></tr><tr><td>'
83
                 . $myblock->getContent('S', $bctype)
84
                 . '</td></tr></table></body></html>';
85
86
    $dummyfile = '_dummyfile_' . time() . '.html';
87
    $fp        = fopen(XOOPS_CACHE_PATH . '/' . $dummyfile, 'w');
88
    fwrite($fp, $dummyhtml);
0 ignored issues
show
It seems like $fp can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, 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

88
    fwrite(/** @scrutinizer ignore-type */ $fp, $dummyhtml);
Loading history...
89
    fclose($fp);
0 ignored issues
show
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

89
    fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
90
    $block['edit_form'] = false;
91
    $block['template']  = '';
92
    $block['op']        = $op;
93
    $block['side']      = $bside;
94
    $block['weight']    = $bweight;
95
    $block['visible']   = $bvisible;
96
    $block['title']     = $myblock->getVar('title', 'E');
97
    $block['content']   = $myblock->getVar('content', 'E');
98
    $block['modules']   = &$bmodule;
99
    $block['ctype']     = isset($bctype) ? $bctype : $myblock->getVar('c_type');
100
    $block['is_custom'] = true;
101
    $block['cachetime'] = (int)$bcachetime;
102
    echo '<a href="admin.php?fct=blocksadmin">' . _AM_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . $block['form_title'] . '<br><br>';
103
    require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blockform.php';
104
    $form->display();
105
    xoops_cp_footer();
106
    echo '<script type="text/javascript">
107
    <!--//
108
    preview_window = openWithSelfMain("' . XOOPS_URL . '/modules/system/admin.php?fct=blocksadmin&op=previewpopup&file=' . $dummyfile . '", "popup", 250, 200);
109
    //-->
110
    </script>';
111
    exit();
112
}
113
114
if ('previewpopup' === $op) {
115
    $file = str_replace('..', '', XOOPS_CACHE_PATH . '/' . trim($_GET['file']));
116
    if (file_exists($file)) {
117
        require_once $file;
118
        @unlink($file);
119
    }
120
    exit();
121
}
122
123
if ('list' === $op) {
124
    xoops_cp_header();
125
    list_blocks();
126
    xoops_cp_footer();
127
    exit();
128
}
129
130
if ('order' === $op) {
131
    foreach (array_keys($bid) as $i) {
132
        if ($side[$i] < 0) {
133
            $visible[$i] = 0;
134
            $side[$i]    = -1;
135
        } else {
136
            $visible[$i] = 1;
137
        }
138
139
        $bmodule[$i] = (isset($bmodule[$i]) && is_array($bmodule[$i])) ? $bmodule[$i] : [-1];
140
141
        myblocksadmin_update_block($i, $side[$i], $weight[$i], $visible[$i], $title[$i], '', '', $bcachetime[$i], $bmodule[$i], []);
142
    }
143
144
    redirect_header('myblocksadmin.php', 1, _AM_DBUPDATED);
145
}
146
147
if ('save' === $op) {
148
    save_block($bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bmodule, $bcachetime);
149
    exit();
150
}
151
152
if ('update' === $op) {
153
    $bcachetime = isset($bcachetime) ? (int)$bcachetime : 0;
154
    $options    = isset($options) ? $options : [];
155
    $bcontent   = isset($bcontent) ? $bcontent : '';
156
    $bctype     = isset($bctype) ? $bctype : '';
157
    $bmodule    = (isset($bmodule) && is_array($bmodule)) ? $bmodule : [-1]; // GIJ +
158
    $msg        = myblocksadmin_update_block($bid, $bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bcachetime, $bmodule, $options); // GIJ c
159
    redirect_header('myblocksadmin.php', 1, $msg); // GIJ +
160
}
161
162
if ('delete_ok' === $op) {
163
    delete_block_ok($bid);
164
    exit();
165
}
166
167
if ('delete' === $op) {
168
    xoops_cp_header();
169
    delete_block($bid);
170
    xoops_cp_footer();
171
    exit();
172
}
173
174
if ('edit' === $op) {
175
    xoops_cp_header();
176
    edit_block($bid);
177
    xoops_cp_footer();
178
    exit();
179
}
180
181
// import from modules/system/admin/blocksadmin/blocksadmin.php
182
/**
183
 * @param         $bid
184
 * @param         $bside
185
 * @param         $bweight
186
 * @param         $bvisible
187
 * @param         $btitle
188
 * @param         $bcontent
189
 * @param         $bctype
190
 * @param         $bcachetime
191
 * @param         $bmodule
192
 * @param  array  $options
193
 * @return string
194
 */
195
function myblocksadmin_update_block(
196
    $bid,
197
    $bside,
198
    $bweight,
199
    $bvisible,
200
    $btitle,
201
    $bcontent,
202
    $bctype,
203
    $bcachetime,
204
    $bmodule,
205
    $options = [])
206
{
207
    global $xoopsConfig;
208
    if (empty($bmodule)) {
209
        xoops_cp_header();
210
        xoops_error(sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
211
        xoops_cp_footer();
212
        exit();
213
    }
214
    $myblock = new \XoopsBlock($bid);
215
    // $myblock->setVar('side', $bside); GIJ -
216
    if ($bside >= 0) {
217
        $myblock->setVar('side', $bside);
218
    } // GIJ +
219
    $myblock->setVar('weight', $bweight);
220
    $myblock->setVar('visible', $bvisible);
221
    $myblock->setVar('title', $btitle);
222
    $myblock->setVar('content', $bcontent);
223
    $myblock->setVar('bcachetime', $bcachetime);
224
    if (isset($options) && (count($options) > 0)) {
225
        $options = implode('|', $options);
226
        $myblock->setVar('options', $options);
227
    }
228
    if ('C' === $myblock->getVar('block_type')) {
229
        switch ($bctype) {
230
            case 'H':
231
                $name = _AM_CUSTOMHTML;
232
                break;
233
            case 'P':
234
                $name = _AM_CUSTOMPHP;
235
                break;
236
            case 'S':
237
                $name = _AM_CUSTOMSMILE;
238
                break;
239
            default:
240
                $name = _AM_CUSTOMNOSMILE;
241
                break;
242
        }
243
        $myblock->setVar('name', $name);
244
        $myblock->setVar('c_type', $bctype);
245
    } else {
246
        $myblock->setVar('c_type', 'H');
247
    }
248
    $msg = _AM_DBUPDATED;
249
    if (false !== $myblock->store()) {
250
        $db  = \XoopsDatabaseFactory::getDatabaseConnection();
251
        $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $db->prefix('block_module_link'), $bid);
252
        $db->query($sql);
253
        foreach ($bmodule as $bmid) {
254
            $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (`%u`, `%d`)', $db->prefix('block_module_link'), $bid, (int)$bmid);
255
            $db->query($sql);
256
        }
257
        require_once XOOPS_ROOT_PATH . '/class/template.php';
258
        $xoopsTpl          = new \XoopsTpl();
259
        $xoopsTpl->caching = 2;
260
        if ('' != $myblock->getVar('template')) {
261
            if ($xoopsTpl->is_cached('db:' . $myblock->getVar('template'))) {
262
                if (!$xoopsTpl->clear_cache('db:' . $myblock->getVar('template'))) {
263
                    $msg = 'Unable to clear cache for block ID' . $bid;
264
                }
265
            }
266
        } else {
267
            if ($xoopsTpl->is_cached('db:system_dummy.tpl', 'block' . $bid)) {
268
                if (!$xoopsTpl->clear_cache('db:system_dummy.tpl', 'block' . $bid)) {
269
                    $msg = 'Unable to clear cache for block ID' . $bid;
270
                }
271
            }
272
        }
273
    } else {
274
        $msg = 'Failed update of block. ID:' . $bid;
275
    }
276
277
    return $msg; // GIJ +
278
}
279