xoops_module_uninstall()   F
last analyzed

Complexity

Conditions 39
Paths > 20000

Size

Total Lines 213
Code Lines 154

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 154
dl 0
loc 213
rs 0
c 0
b 0
f 0
cc 39
nc 20762
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
/*
21
if ( !is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
22
    exit("Access Denied");
23
}
24
*/
25
26
/**
27
 * @param $dirname
28
 *
29
 * @return string
30
 */
31
function xoops_module_install($dirname)
32
{
33
    global $xoopsUser, $xoopsConfig;
34
    $dirname        = trim((string) $dirname);
35
    $db             =& $GLOBALS['xoopsDB'];
36
    $reservedTables = [
37
        'avatar',
38
        'avatar_users_link',
39
        'block_module_link',
40
        'xoopscomments',
41
        'config',
42
        'configcategory',
43
        'configoption',
44
        'image',
45
        'imagebody',
46
        'imagecategory',
47
        'imgset',
48
        'imgset_tplset_link',
49
        'imgsetimg',
50
        'groups',
51
        'groups_users_link',
52
        'group_permission',
53
        'online',
54
        'bannerclient',
55
        'banner',
56
        'bannerfinish',
57
        'priv_msgs',
58
        'ranks',
59
        'session',
60
        'smiles',
61
        'users',
62
        'newblocks',
63
        'modules',
64
        'tplfile',
65
        'tplset',
66
        'tplsource',
67
        'xoopsnotifications',
68
        'banner',
69
        'bannerclient',
70
        'bannerfinish',
71
    ];
72
    /** @var XoopsModuleHandler $module_handler */
73
    $module_handler = xoops_getHandler('module');
74
    if ($module_handler->getCount(new Criteria('dirname', $dirname)) == 0) {
75
        $module = $module_handler->create();
76
        $module->loadInfoAsVar($dirname);
77
        $module->setVar('weight', 1);
78
        $module->setVar('isactive', 1);
79
        $module->setVar('last_update', time());
80
        $error = false;
81
        $errs  = [];
82
        $msgs  = [];
83
        $msgs[] = '<div id="xo-module-log"><div class="header">';
84
        $msgs[] = $errs[] = '<h4>' . _AM_SYSTEM_MODULES_INSTALLING . $module->getInfo('name') . '</h4>';
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('name') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

84
        $msgs[] = $errs[] = '<h4>' . _AM_SYSTEM_MODULES_INSTALLING . /** @scrutinizer ignore-type */ $module->getInfo('name') . '</h4>';
Loading history...
85
        if ($module->getInfo('image') !== false && trim($module->getInfo('image')) != '') {
0 ignored issues
show
Bug introduced by
It seems like $module->getInfo('image') can also be of type array; however, parameter $string of trim() 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

85
        if ($module->getInfo('image') !== false && trim(/** @scrutinizer ignore-type */ $module->getInfo('image')) != '') {
Loading history...
86
            $msgs[] = '<a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname') . '/' . $module->getInfo('adminindex') . '"><img src="' . XOOPS_URL . '/modules/' . $dirname . '/' . trim($module->getInfo('image')) . '" alt="" /></a>';
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('dirname') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

86
            $msgs[] = '<a href="' . XOOPS_URL . '/modules/' . /** @scrutinizer ignore-type */ $module->getInfo('dirname') . '/' . $module->getInfo('adminindex') . '"><img src="' . XOOPS_URL . '/modules/' . $dirname . '/' . trim($module->getInfo('image')) . '" alt="" /></a>';
Loading history...
Bug introduced by
Are you sure $module->getInfo('adminindex') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

86
            $msgs[] = '<a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname') . '/' . /** @scrutinizer ignore-type */ $module->getInfo('adminindex') . '"><img src="' . XOOPS_URL . '/modules/' . $dirname . '/' . trim($module->getInfo('image')) . '" alt="" /></a>';
Loading history...
87
        }
88
        $msgs[] = '<strong>' . _VERSION . ':</strong> ' . $module->getInfo('version');
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('version') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

88
        $msgs[] = '<strong>' . _VERSION . ':</strong> ' . /** @scrutinizer ignore-type */ $module->getInfo('version');
Loading history...
89
        if ($module->getInfo('author') !== false && trim($module->getInfo('author')) != '') {
90
            $msgs[] = '<strong>' . _AUTHOR . ':</strong> ' . htmlspecialchars(trim($module->getInfo('author')), ENT_QUOTES | ENT_HTML5);
91
        }
92
        $msgs[] = '</div><div class="logger">';
93
        // Load module specific install script if any
94
        $install_script = $module->getInfo('onInstall');
95
        if ($install_script && trim($install_script) != '') {
96
            include_once XOOPS_ROOT_PATH . '/modules/' . $dirname . '/' . trim($install_script);
97
        }
98
        $func = "xoops_module_pre_install_{$dirname}";
99
        // If pre-install function is defined, execute
100
        if (function_exists($func)) {
101
            $result = $func($module);
102
            if (!$result) {
103
                $error  = true;
104
                $errs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_EXECUTE, $func) . '</p>';
105
                $errs   = array_merge($errs, $module->getErrors());
106
            } else {
107
                $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_SUCESS, "<strong>{$func}</strong>") . '</p>';
108
                $msgs += $module->getErrors();
109
            }
110
        }
111
112
        if ($error === false) {
113
            $sqlfile = $module->getInfo('sqlfile');
114
            if (is_array($sqlfile) && !empty($sqlfile[XOOPS_DB_TYPE])) {
115
                $sql_file_path = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/' . $sqlfile[XOOPS_DB_TYPE];
116
                if (!file_exists($sql_file_path)) {
117
                    $errs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_SQL_NOT_FOUND, "<strong>{$sql_file_path}</strong>");
118
                    $error  = true;
119
                } else {
120
                    $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_SQL_FOUND, "<strong>{$sql_file_path}</strong>") . '<br  />' . _AM_SYSTEM_MODULES_CREATE_TABLES;
121
                    include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
122
                    $sql_query = fread(fopen($sql_file_path, 'r'), filesize($sql_file_path));
123
                    $sql_query = trim($sql_query);
124
                    SqlUtility::splitMySqlFile($pieces, $sql_query);
125
                    $created_tables = [];
126
                    foreach ($pieces as $piece) {
127
                        // [0] contains the prefixed query
128
                        // [4] contains unprefixed table name
129
                        $prefixed_query = SqlUtility::prefixQuery($piece, $db->prefix());
130
                        if (!$prefixed_query) {
131
                            $errs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_SQL_NOT_VALID, '<strong>' . $piece . '</strong>');
132
                            $error  = true;
133
                            break;
134
                        }
135
                        // check if the table name is reserved
136
                        if (!in_array($prefixed_query[4], $reservedTables)) {
137
                            // not reserved, so try to create one
138
                            if (!$db->query($prefixed_query[0])) {
139
                                $errs[] = $db->error();
140
                                $error  = true;
141
                                break;
142
                            } else {
143
                                if (!in_array($prefixed_query[4], $created_tables)) {
144
                                    $msgs[]           = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TABLE_CREATED, '<strong>' . $db->prefix($prefixed_query[4]) . '</strong>');
145
                                    $created_tables[] = $prefixed_query[4];
146
                                } else {
147
                                    $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_INSERT_DATA, '<strong>' . $db->prefix($prefixed_query[4]) . '</strong>');
148
                                }
149
                            }
150
                        } else {
151
                            // the table name is reserved, so halt the installation
152
                            $errs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TABLE_RESERVED, '<strong>' . $prefixed_query[4] . '</strong>');
153
                            $error  = true;
154
                            break;
155
                        }
156
                    }
157
                    // if there was an error, delete the tables created so far, so the next installation will not fail
158
                    if ($error === true) {
159
                        foreach ($created_tables as $ct) {
160
                            $db->query('DROP TABLE ' . $db->prefix($ct));
161
                        }
162
                    }
163
                }
164
            }
165
        }
166
        // if no error, save the module info and blocks info associated with it
167
        if ($error === false) {
168
            if (!$module_handler->insert($module)) {
169
                $errs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_INSERT_DATA_FAILD, '<strong>' . $module->getVar('name') . '</strong>');
170
                foreach ($created_tables as $ct) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $created_tables does not seem to be defined for all execution paths leading up to this point.
Loading history...
171
                    $db->query('DROP TABLE ' . $db->prefix($ct));
172
                }
173
                $ret = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILINS, '<strong>' . $module->name() . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br>';
174
                foreach ($errs as $err) {
175
                    $ret .= ' - ' . $err . '<br>';
176
                }
177
                $ret .= '</p>';
178
                unset($module, $created_tables, $errs, $msgs);
179
180
                return $ret;
181
            } else {
182
                $newmid = $module->getVar('mid');
183
                unset($created_tables);
184
                $msgs[]          = '<p>' . _AM_SYSTEM_MODULES_INSERT_DATA_DONE . sprintf(_AM_SYSTEM_MODULES_MODULEID, '<strong>' . $newmid . '</strong>');
185
                $tplfile_handler = xoops_getHandler('tplfile');
186
                $templates       = $module->getInfo('templates');
187
                if ($templates !== false) {
0 ignored issues
show
introduced by
The condition $templates !== false is always true.
Loading history...
188
                    $msgs[] = _AM_SYSTEM_MODULES_TEMPLATES_ADD;
189
                    foreach ($templates as $tpl) {
190
                        $tplfile = $tplfile_handler->create();
191
                        $type    = ($tpl['type'] ?? 'module');
192
                        $tpldata =&  xoops_module_gettemplate($dirname, $tpl['file'], $type);
193
                        $tplfile->setVar('tpl_source', $tpldata, true);
194
                        $tplfile->setVar('tpl_refid', $newmid);
195
196
                        $tplfile->setVar('tpl_tplset', 'default');
197
                        $tplfile->setVar('tpl_file', $tpl['file']);
198
                        $tplfile->setVar('tpl_desc', $tpl['description'], true);
199
                        $tplfile->setVar('tpl_module', $dirname);
200
                        $tplfile->setVar('tpl_lastmodified', time());
201
                        $tplfile->setVar('tpl_lastimported', time());
202
                        $tplfile->setVar('tpl_type', $type);
203
                        if (!$tplfile_handler->insert($tplfile)) {
204
                            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ADD_ERROR, '<strong>' . $tpl['file'] . '</strong>') . '</span>';
205
                        } else {
206
                            $newtplid = $tplfile->getVar('tpl_id');
207
                            $msgs[]   = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ADD_DATA, '<strong>' . $tpl['file'] . '</strong>') . '(ID: <strong>' . $newtplid . '</strong>)';
208
                            // generate compiled file
209
                            include_once XOOPS_ROOT_PATH . '/class/template.php';
210
                            if (!xoops_template_touch($newtplid)) {
211
                                $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_COMPILED_FAILED, '<strong>' . $tpl['file'] . '</strong>') . '</span>';
212
                            } else {
213
                                $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_COMPILED, '<strong>' . $tpl['file'] . '</strong>');
214
                            }
215
                        }
216
                        unset($tplfile, $tpldata);
217
                    }
218
                }
219
                include_once XOOPS_ROOT_PATH . '/class/template.php';
220
                xoops_template_clear_module_cache($newmid);
221
                $blocks = $module->getInfo('blocks');
222
                if ($blocks !== false) {
0 ignored issues
show
introduced by
The condition $blocks !== false is always true.
Loading history...
223
                    $msgs[] = _AM_SYSTEM_MODULES_BLOCKS_ADD;
224
                    foreach ($blocks as $blockkey => $block) {
225
                        // break the loop if missing block config
226
                        if (!isset($block['file']) || !isset($block['show_func'])) {
227
                            break;
228
                        }
229
                        $options = '';
230
                        if (!empty($block['options'])) {
231
                            if (is_array($block['options'])) {
232
                                $options = implode('|', $block['options']);
233
                            } else {
234
                                $options = $block['options'];
235
                            }
236
                            $options = trim((string) $options);
237
                        }
238
                        $newbid    = $db->genId($db->prefix('newblocks') . '_bid_seq');
239
                        $edit_func = isset($block['edit_func']) ? trim((string) $block['edit_func']) : '';
240
                        $template  = '';
241
                        if (isset($block['template']) && trim((string) $block['template']) != '') {
242
                            $content =& xoops_module_gettemplate($dirname, $block['template'], 'blocks');
243
                        }
244
                        if (empty($content)) {
245
                            $content = '';
246
                        } else {
247
                            $template = trim((string) $block['template']);
248
                        }
249
                        $block_name = addslashes(trim((string) $block['name']));
250
                        $sql        = 'INSERT INTO ' . $db->prefix('newblocks') . " (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, c_type, isactive, dirname, func_file, show_func, edit_func, template, bcachetime, last_modified) VALUES ($newbid, $newmid, " . (int)$blockkey . ", '$options', '" . $block_name . "','" . $block_name . "', '', 0, 0, 0, 'M', 'H', 1, '" . addslashes($dirname) . "', '" . addslashes(trim((string) $block['file'])) . "', '" . addslashes(trim((string) $block['show_func'])) . "', '" . addslashes($edit_func) . "', '" . $template . "', 0, " . time() . ')';
251
                        if (!$db->query($sql)) {
252
                            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_BLOCK_ADD_ERROR, '<strong>' . $block['name'] . '</strong>') . sprintf(_AM_SYSTEM_MODULES_BLOCK_ADD_ERROR_DATABASE, '<strong>' . $db->error() . '</strong>') . '</span>';
253
                        } else {
254
                            if (empty($newbid)) {
255
                                $newbid = $db->getInsertId();
256
                            }
257
                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_BLOCK_ADD, '<strong>' . $block['name'] . '</strong>') . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $newbid . '</strong>');
258
                            $sql    = 'INSERT INTO ' . $db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newbid . ', -1)';
259
                            $db->query($sql);
260
                            if ($template != '') {
261
                                $tplfile = $tplfile_handler->create();
262
                                $tplfile->setVar('tpl_refid', $newbid);
263
                                $tplfile->setVar('tpl_source', $content, true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $content does not seem to be defined for all execution paths leading up to this point.
Loading history...
264
                                $tplfile->setVar('tpl_tplset', 'default');
265
                                $tplfile->setVar('tpl_file', $block['template']);
266
                                $tplfile->setVar('tpl_module', $dirname);
267
                                $tplfile->setVar('tpl_type', 'block');
268
                                $tplfile->setVar('tpl_desc', $block['description']??'', true);
269
                                $tplfile->setVar('tpl_lastimported', 0);
270
                                $tplfile->setVar('tpl_lastmodified', time());
271
                                if (!$tplfile_handler->insert($tplfile)) {
272
                                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ADD_ERROR, '<strong>' . $block['template'] . '</strong>') . '</span>';
273
                                } else {
274
                                    $newtplid = $tplfile->getVar('tpl_id');
275
                                    $msgs[]   = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ADD_DATA, '<strong>' . $block['template'] . '</strong>') . ' (ID: <strong>' . $newtplid . '</strong>)';
276
                                    // generate compiled file
277
                                    include_once XOOPS_ROOT_PATH . '/class/template.php';
278
                                    if (!xoops_template_touch($newtplid)) {
279
                                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_COMPILED_FAILED, '<strong>' . $block['template'] . '</strong>') . '</span>';
280
                                    } else {
281
                                        $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_COMPILED, '<strong>' . $block['template'] . '</strong>');
282
                                    }
283
                                }
284
                                unset($tplfile);
285
                            }
286
                        }
287
                        unset($content);
288
                    }
289
                    unset($blocks);
290
                }
291
                $configs = $module->getInfo('config');
292
                if ($configs !== false) {
0 ignored issues
show
introduced by
The condition $configs !== false is always true.
Loading history...
293
                    if ($module->getVar('hascomments') != 0) {
294
                        include_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
295
                        $configs[] = [
296
                            'name'        => 'com_rule',
297
                            'title'       => '_CM_COMRULES',
298
                            'description' => '',
299
                            'formtype'    => 'select',
300
                            'valuetype'   => 'int',
301
                            'default'     => 1,
302
                            'options'     => [
303
                                '_CM_COMNOCOM'        => XOOPS_COMMENT_APPROVENONE,
304
                                '_CM_COMAPPROVEALL'   => XOOPS_COMMENT_APPROVEALL,
305
                                '_CM_COMAPPROVEUSER'  => XOOPS_COMMENT_APPROVEUSER,
306
                                '_CM_COMAPPROVEADMIN' => XOOPS_COMMENT_APPROVEADMIN,
307
                            ],
308
                        ];
309
                        $configs[] = [
310
                            'name'        => 'com_anonpost',
311
                            'title'       => '_CM_COMANONPOST',
312
                            'description' => '',
313
                            'formtype'    => 'yesno',
314
                            'valuetype'   => 'int',
315
                            'default'     => 0,
316
                        ];
317
                    }
318
                } else {
319
                    if ($module->getVar('hascomments') != 0) {
320
                        $configs = [];
321
                        include_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
322
                        $configs[] = [
323
                            'name'        => 'com_rule',
324
                            'title'       => '_CM_COMRULES',
325
                            'description' => '',
326
                            'formtype'    => 'select',
327
                            'valuetype'   => 'int',
328
                            'default'     => 1,
329
                            'options'     => [
330
                                '_CM_COMNOCOM'        => XOOPS_COMMENT_APPROVENONE,
331
                                '_CM_COMAPPROVEALL'   => XOOPS_COMMENT_APPROVEALL,
332
                                '_CM_COMAPPROVEUSER'  => XOOPS_COMMENT_APPROVEUSER,
333
                                '_CM_COMAPPROVEADMIN' => XOOPS_COMMENT_APPROVEADMIN,
334
                            ],
335
                        ];
336
                        $configs[] = [
337
                            'name'        => 'com_anonpost',
338
                            'title'       => '_CM_COMANONPOST',
339
                            'description' => '',
340
                            'formtype'    => 'yesno',
341
                            'valuetype'   => 'int',
342
                            'default'     => 0,
343
                        ];
344
                    }
345
                }
346
                // RMV-NOTIFY
347
                if ($module->getVar('hasnotification') != 0) {
348
                    if (empty($configs)) {
349
                        $configs = [];
350
                    }
351
                    // Main notification options
352
                    include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
353
                    include_once XOOPS_ROOT_PATH . '/include/notification_functions.php';
354
                    $options                             = [];
355
                    $options['_NOT_CONFIG_DISABLE']      = XOOPS_NOTIFICATION_DISABLE;
356
                    $options['_NOT_CONFIG_ENABLEBLOCK']  = XOOPS_NOTIFICATION_ENABLEBLOCK;
357
                    $options['_NOT_CONFIG_ENABLEINLINE'] = XOOPS_NOTIFICATION_ENABLEINLINE;
358
                    $options['_NOT_CONFIG_ENABLEBOTH']   = XOOPS_NOTIFICATION_ENABLEBOTH;
359
360
                    $configs[] = [
361
                        'name'        => 'notification_enabled',
362
                        'title'       => '_NOT_CONFIG_ENABLE',
363
                        'description' => '_NOT_CONFIG_ENABLEDSC',
364
                        'formtype'    => 'select',
365
                        'valuetype'   => 'int',
366
                        'default'     => XOOPS_NOTIFICATION_ENABLEBOTH,
367
                        'options'     => $options,
368
                    ];
369
                    // Event-specific notification options
370
                    // FIXME: doesn't work when update module... can't read back the array of options properly...  " changing to &quot;
371
                    $options    = [];
372
                    $categories =& notificationCategoryInfo('', $module->getVar('mid'));
373
                    foreach ($categories as $category) {
374
                        $events =& notificationEvents($category['name'], false, $module->getVar('mid'));
375
                        foreach ($events as $event) {
376
                            if (!empty($event['invisible'])) {
377
                                continue;
378
                            }
379
                            $option_name           = $category['title'] . ' : ' . $event['title'];
380
                            $option_value          = $category['name'] . '-' . $event['name'];
381
                            $options[$option_name] = $option_value;
382
                        }
383
                        unset($events);
384
                    }
385
                    unset($categories);
386
                    $configs[] = [
387
                        'name'        => 'notification_events',
388
                        'title'       => '_NOT_CONFIG_EVENTS',
389
                        'description' => '_NOT_CONFIG_EVENTSDSC',
390
                        'formtype'    => 'select_multi',
391
                        'valuetype'   => 'array',
392
                        'default'     => array_values($options),
393
                        'options'     => $options,
394
                    ];
395
                }
396
397
                if ($configs !== false) {
0 ignored issues
show
introduced by
The condition $configs !== false is always true.
Loading history...
398
                    $msgs[]         = _AM_SYSTEM_MODULES_MODULE_DATA_ADD;
399
                    /** @var XoopsConfigHandler $config_handler */
400
                    $config_handler = xoops_getHandler('config');
401
                    $order          = 0;
402
                    foreach ($configs as $config) {
403
                        $confobj = $config_handler->createConfig();
404
                        $confobj->setVar('conf_modid', $newmid);
405
                        $confobj->setVar('conf_catid', 0);
406
                        $confobj->setVar('conf_name', $config['name']);
407
                        $confobj->setVar('conf_title', $config['title'], true);
408
                        $confobj->setVar('conf_desc', $config['description'] ?? '', true);
409
                        $confobj->setVar('conf_formtype', $config['formtype']);
410
                        $confobj->setVar('conf_valuetype', $config['valuetype']);
411
                        $confobj->setConfValueForInput($config['default'], true);
412
                        $confobj->setVar('conf_order', $order);
413
                        $confop_msgs = '';
414
                        if (isset($config['options']) && \is_array($config['options'])) {
415
                            foreach ($config['options'] as $key => $value) {
416
                                $confop = $config_handler->createConfigOption();
417
                                $confop->setVar('confop_name', $key, true);
418
                                $confop->setVar('confop_value', $value, true);
419
                                $confobj->setConfOptions($confop);
420
                                $confop_msgs .= '<br>&nbsp;&nbsp;&nbsp;&nbsp; ' . _AM_SYSTEM_MODULES_CONFIG_ADD . _AM_SYSTEM_MODULES_NAME . ' <strong>' . (defined($key) ? constant($key) : $key) . '</strong> ' . _AM_SYSTEM_MODULES_VALUE . ' <strong>' . $value . '</strong> ';
421
                                unset($confop);
422
                            }
423
                        }
424
                        ++$order;
425
                        if ($config_handler->insertConfig($confobj) != false) {
426
                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_CONFIG_DATA_ADD, '<strong>' . $config['name'] . '</strong>') . $confop_msgs;
427
                        } else {
428
                            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_CONFIG_DATA_ADD_ERROR, '<strong>' . $config['name'] . '</strong>') . '</span>';
429
                        }
430
                        unset($confobj);
431
                    }
432
                    unset($configs);
433
                }
434
            }
435
            $groups = [XOOPS_GROUP_ADMIN];
436
            if ($module->getInfo('hasMain')) {
437
                $groups = [XOOPS_GROUP_ADMIN, XOOPS_GROUP_USERS, XOOPS_GROUP_ANONYMOUS];
438
            }
439
            // retrieve all block ids for this module
440
            $blocks        = XoopsBlock::getByModule($newmid, false);
441
            $msgs[]        = _AM_SYSTEM_MODULES_GROUP_SETTINGS_ADD;
442
            /** @var  XoopsGroupPermHandler $gperm_handler */
443
            $gperm_handler = xoops_getHandler('groupperm');
444
            foreach ($groups as $mygroup) {
445
                if ($gperm_handler->checkRight('module_admin', 0, $mygroup)) {
446
                    /** @var XoopsGroupPerm $mperm */
447
                    $mperm = $gperm_handler->create();
448
                    $mperm->setVar('gperm_groupid', $mygroup);
449
                    $mperm->setVar('gperm_itemid', $newmid);
450
                    $mperm->setVar('gperm_name', 'module_admin');
451
                    $mperm->setVar('gperm_modid', 1);
452
                    if (!$gperm_handler->insert($mperm)) {
453
                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_ACCESS_ADMIN_ADD_ERROR, '<strong>' . $mygroup . '</strong>') . '</span>';
454
                    } else {
455
                        $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_ACCESS_ADMIN_ADD, '<strong>' . $mygroup . '</strong>');
456
                    }
457
                    unset($mperm);
458
                }
459
                $mperm = $gperm_handler->create();
460
                $mperm->setVar('gperm_groupid', $mygroup);
461
                $mperm->setVar('gperm_itemid', $newmid);
462
                $mperm->setVar('gperm_name', 'module_read');
463
                $mperm->setVar('gperm_modid', 1);
464
                if (!$gperm_handler->insert($mperm)) {
465
                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_ACCESS_USER_ADD_ERROR, '<strong>' . $mygroup . '</strong>') . '</span>';
466
                } else {
467
                    $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_ACCESS_USER_ADD_ERROR, '<strong>' . $mygroup . '</strong>');
468
                }
469
                unset($mperm);
470
                foreach ($blocks as $blc) {
471
                    /** @var XoopsGroupPerm $bperm */
472
                    $bperm = $gperm_handler->create();
473
                    $bperm->setVar('gperm_groupid', $mygroup);
474
                    $bperm->setVar('gperm_itemid', $blc);
475
                    $bperm->setVar('gperm_name', 'block_read');
476
                    $bperm->setVar('gperm_modid', 1);
477
                    if (!$gperm_handler->insert($bperm)) {
478
                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . _AM_SYSTEM_MODULES_BLOCK_ACCESS_ERROR . ' Block ID: <strong>' . $blc . '</strong> Group ID: <strong>' . $mygroup . '</strong></span>';
479
                    } else {
480
                        $msgs[] = '&nbsp;&nbsp;' . _AM_SYSTEM_MODULES_BLOCK_ACCESS . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $blc . '</strong>') . sprintf(_AM_SYSTEM_MODULES_GROUP_ID, '<strong>' . $mygroup . '</strong>');
481
                    }
482
                    unset($bperm);
483
                }
484
            }
485
            unset($blocks, $groups);
486
487
            // execute module specific install script if any
488
            $func = "xoops_module_install_{$dirname}";
489
            if (function_exists($func)) {
490
                if (!$lastmsg = $func($module)) {
491
                    $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_EXECUTE, $func) . '</p>';
492
                } else {
493
                    $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_SUCESS, "<strong>{$func}</strong>") . '</p>';
494
                    if (is_string($lastmsg)) {
495
                        $msgs[] = $lastmsg;
496
                    }
497
                }
498
            }
499
500
            $msgs[] = sprintf(_AM_SYSTEM_MODULES_OKINS, '<strong>' . $module->getVar('name', 's') . '</strong>');
501
            $msgs[] = '</div></div>';
502
503
            $blocks = $module->getInfo('blocks');
504
            $redDevider = '<span class="red bold">  |  </span>';
0 ignored issues
show
Unused Code introduced by
The assignment to $redDevider is dead and can be removed.
Loading history...
505
            $msgs[] = '<div class="noininstall center"><a href="admin.php?fct=modulesadmin">' . _AM_SYSTEM_MODULES_BTOMADMIN . '</a> |
506
                        <a href="admin.php?fct=modulesadmin&op=installlist">' . _AM_SYSTEM_MODULES_TOINSTALL . '</a>';
507
            $msgs[] = '<br><span class="red bold">' . _AM_SYSTEM_MODULES_MODULE . ' ' . $module->getInfo('name') . ': </span></div>';
508
            if ($blocks !== false) {
0 ignored issues
show
introduced by
The condition $blocks !== false is always true.
Loading history...
509
                $msgs[] = '<div class="noininstall center"><a href="admin.php?fct=blocksadmin&op=list&filter=1&selgen=' . $newmid . '&selmod=-2&selgrp=-1&selvis=-1&filsave=1">' . _AM_SYSTEM_BLOCKS . '</a></div>';
510
            }
511
			if ($module->getInfo('config') !== false) {
0 ignored issues
show
introduced by
The condition $module->getInfo('config') !== false is always true.
Loading history...
512
				$msgs[] = '<div class="noininstall center"><a href="admin.php?fct=preferences&op=showmod&mod=' . $newmid . '">' . _AM_SYSTEM_PREF . '</a>';
513
            } else {
514
				$msgs[] = '<div class="noininstall center">';
515
			}
516
			if ($module->getInfo('adminindex') != ''){
517
				$msgs[] = '<a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname') . '/' . $module->getInfo('adminindex') . '">' . _AM_SYSTEM_MODULES_INSTALL_THISMODULE . '</a>';
518
			}
519
            $testdataDirectory = XOOPS_ROOT_PATH . '/modules/' . $module->getInfo('dirname') . '/testdata';
520
            if (file_exists($testdataDirectory)) {
521
                $msgs[] = '<a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname') . '/testdata/index.php?op=load' . '">' . _AM_SYSTEM_MODULES_INSTALL_TESTDATA . '</a></div>';
522
            } else {
523
                $msgs[] = '</div>';
524
            }
525
526
            $ret = implode('<br>', $msgs);
527
            unset($blocks, $msgs, $errs, $module);
528
529
            return $ret;
530
        } else {
531
            $ret = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILINS, '<strong>' . $dirname . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br>' . implode('<br>', $errs) . '</p>';
532
            unset($msgs, $errs);
533
534
            return $ret;
535
        }
536
    } else {
537
        return '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILINS, '<strong>' . $dirname . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br>&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_ALEXISTS, $dirname) . '</p>';
538
    }
539
}
540
541
/**
542
 * @param        $dirname
543
 * @param        $template
544
 * @param string $type
545
 *
546
 * @return string
547
 */
548
function &xoops_module_gettemplate($dirname, $template, $type = '')
549
{
550
    global $xoopsConfig;
551
    $ret = '';
552
    switch ($type) {
553
        case 'blocks':
554
        case 'admin':
555
            $path = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/templates/' . $type . '/' . $template;
556
            break;
557
        default:
558
            $path = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/templates/' . $template;
559
            break;
560
    }
561
    if (!file_exists($path)) {
562
        return $ret;
563
    } else {
564
        $lines = file($path);
565
    }
566
    if (!$lines) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $lines of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
567
        return $ret;
568
    }
569
    $count = count($lines);
570
    for ($i = 0; $i < $count; ++$i) {
571
        $ret .= str_replace("\n", "\r\n", str_replace("\r\n", "\n", $lines[$i]));
572
    }
573
574
    return $ret;
575
}
576
577
/**
578
 * @param $dirname
579
 *
580
 * @return string
581
 */
582
function xoops_module_uninstall($dirname)
583
{
584
    global $xoopsConfig;
585
    $reservedTables = [
586
        'avatar',
587
        'avatar_users_link',
588
        'block_module_link',
589
        'xoopscomments',
590
        'config',
591
        'configcategory',
592
        'configoption',
593
        'image',
594
        'imagebody',
595
        'imagecategory',
596
        'imgset',
597
        'imgset_tplset_link',
598
        'imgsetimg',
599
        'groups',
600
        'groups_users_link',
601
        'group_permission',
602
        'online',
603
        'bannerclient',
604
        'banner',
605
        'bannerfinish',
606
        'priv_msgs',
607
        'ranks',
608
        'session',
609
        'smiles',
610
        'users',
611
        'newblocks',
612
        'modules',
613
        'tplfile',
614
        'tplset',
615
        'tplsource',
616
        'xoopsnotifications',
617
        'banner',
618
        'bannerclient',
619
        'bannerfinish',
620
    ];
621
    $db             = XoopsDatabaseFactory::getDatabaseConnection();
622
    /** @var XoopsModuleHandler $module_handler */
623
    $module_handler = xoops_getHandler('module');
624
    $module         = $module_handler->getByDirname($dirname);
625
    include_once XOOPS_ROOT_PATH . '/class/template.php';
626
    xoops_template_clear_module_cache($module->getVar('mid'));
627
    if ($module->getVar('dirname') === 'system') {
628
        return '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILUNINS, '<strong>' . $module->getVar('name') . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br> - ' . _AM_SYSTEM_MODULES_SYSNO . '</p>';
629
    } elseif ($module->getVar('dirname') == $xoopsConfig['startpage']) {
630
        return '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILUNINS, '<strong>' . $module->getVar('name') . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br> - ' . _AM_SYSTEM_MODULES_STRTNO . '</p>';
631
    } else {
632
        $msgs   = [];
633
        $msgs[] = '<div id="xo-module-log"><div class="header">';
634
        $msgs[] = $errs[] = '<h4>' . _AM_SYSTEM_MODULES_UNINSTALL . ' ' . $module->getInfo('name', 's') . '</h4>';
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('name', 's') of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

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

634
        $msgs[] = $errs[] = '<h4>' . _AM_SYSTEM_MODULES_UNINSTALL . ' ' . /** @scrutinizer ignore-type */ $module->getInfo('name', 's') . '</h4>';
Loading history...
Comprehensibility Best Practice introduced by
$errs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $errs = array(); before regardless.
Loading history...
Unused Code introduced by
The call to XoopsModule::getInfo() has too many arguments starting with 's'. ( Ignorable by Annotation )

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

634
        $msgs[] = $errs[] = '<h4>' . _AM_SYSTEM_MODULES_UNINSTALL . ' ' . $module->/** @scrutinizer ignore-call */ getInfo('name', 's') . '</h4>';

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
635
        if ($module->getInfo('image') !== false && trim($module->getInfo('image')) != '') {
0 ignored issues
show
Bug introduced by
It seems like $module->getInfo('image') can also be of type array; however, parameter $string of trim() 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

635
        if ($module->getInfo('image') !== false && trim(/** @scrutinizer ignore-type */ $module->getInfo('image')) != '') {
Loading history...
636
            $msgs[] = '<img src="' . XOOPS_URL . '/modules/' . $dirname . '/' . trim($module->getInfo('image')) . '" alt="" />';
637
        }
638
        $msgs[] = '<strong>' . _VERSION . ':</strong> ' . $module->getInfo('version');
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('version') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

638
        $msgs[] = '<strong>' . _VERSION . ':</strong> ' . /** @scrutinizer ignore-type */ $module->getInfo('version');
Loading history...
639
        if ($module->getInfo('author') !== false && trim($module->getInfo('author')) != '') {
640
            $msgs[] = '<strong>' . _AUTHOR . ':</strong> ' . htmlspecialchars(trim($module->getInfo('author')), ENT_QUOTES | ENT_HTML5);
641
        }
642
        $msgs[] = '</div><div class="logger">';
643
        // Load module specific install script if any
644
        $uninstall_script = $module->getInfo('onUninstall');
645
        if ($uninstall_script && trim($uninstall_script) != '') {
646
            include_once XOOPS_ROOT_PATH . '/modules/' . $dirname . '/' . trim($uninstall_script);
647
        }
648
        $func = "xoops_module_pre_uninstall_{$dirname}";
649
        // If pre uninstall function is defined, execute
650
        if (function_exists($func)) {
651
            $result = $func($module);
652
            if (false === $result) {
653
                $errs   = $module->getErrors();
654
                $errs[] = sprintf(_AM_SYSTEM_MODULES_FAILED_EXECUTE, $func);
655
656
                return '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILUNINS, '<strong>' . $module->getVar('name') . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br>' . implode('<br>', $errs) . '</p>';
657
            } else {
658
                $prevErrs = $module->getErrors();
659
                array_unshift($prevErrs, '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_SUCESS, "<strong>{$func}</strong>") . '</p>');
660
                $msgs = array_merge($msgs, $prevErrs);
661
            }
662
        }
663
664
        if (false === $module_handler->delete($module)) {
0 ignored issues
show
Bug introduced by
It seems like $module can also be of type false; however, parameter $module of XoopsModuleHandler::delete() does only seem to accept XoopsObject, 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

664
        if (false === $module_handler->delete(/** @scrutinizer ignore-type */ $module)) {
Loading history...
665
            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_DELETE_ERROR, $module->getVar('name')) . '</span>';
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('name') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|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

665
            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_DELETE_ERROR, /** @scrutinizer ignore-type */ $module->getVar('name')) . '</span>';
Loading history...
666
        } else {
667
668
            // delete template files
669
            $tplfile_handler = xoops_getHandler('tplfile');
670
            $templates       = $tplfile_handler->find(null, 'module', $module->getVar('mid'));
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

670
            /** @scrutinizer ignore-call */ 
671
            $templates       = $tplfile_handler->find(null, 'module', $module->getVar('mid'));
Loading history...
671
            $tcount          = count($templates);
672
            if ($tcount > 0) {
673
                $msgs[] = _AM_SYSTEM_MODULES_TEMPLATES_DELETE;
674
                for ($i = 0; $i < $tcount; ++$i) {
675
                    if (false === $tplfile_handler->delete($templates[$i])) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $tplfile_handler->delete($templates[$i]) targeting XoopsObjectHandler::delete() seems to always return null.

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

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

}

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

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

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

Loading history...
676
                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_DELETE_DATA_FAILD, $templates[$i]->getVar('tpl_file')) . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ID, '<strong>' . $templates[$i]->getVar('tpl_id') . '</strong>') . '</span>';
677
                    } else {
678
                        $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_DELETE_DATA, '<strong>' . $templates[$i]->getVar('tpl_file') . '</strong>') . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ID, '<strong>' . $templates[$i]->getVar('tpl_id') . '</strong>');
679
                    }
680
                }
681
            }
682
            unset($templates);
683
684
            // delete blocks and block template files
685
            $block_arr = XoopsBlock::getByModule($module->getVar('mid'));
686
            if (is_array($block_arr)) {
0 ignored issues
show
introduced by
The condition is_array($block_arr) is always true.
Loading history...
687
                $bcount = count($block_arr);
688
                $msgs[] = _AM_SYSTEM_MODULES_BLOCKS_DELETE;
689
                for ($i = 0; $i < $bcount; ++$i) {
690
                    if (false === $block_arr[$i]->delete()) {
691
                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_BLOCK_DELETE_ERROR, '<strong>' . $block_arr[$i]->getVar('name') . '</strong>') . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $block_arr[$i]->getVar('bid') . '</strong>') . '</span>';
692
                    } else {
693
                        $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_BLOCK_DELETE, '<strong>' . $block_arr[$i]->getVar('name') . '</strong>') . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $block_arr[$i]->getVar('bid') . '</strong>');
694
                    }
695
                    if ($block_arr[$i]->getVar('template') != '') {
696
                        $templates = $tplfile_handler->find(null, 'block', $block_arr[$i]->getVar('bid'));
697
                        $btcount   = count($templates);
698
                        if ($btcount > 0) {
699
                            for ($j = 0; $j < $btcount; ++$j) {
700
                                if (!$tplfile_handler->delete($templates[$j])) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $tplfile_handler->delete($templates[$j]) targeting XoopsObjectHandler::delete() seems to always return null.

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

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

}

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

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

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

Loading history...
701
                                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_BLOCK_DELETE_TEMPLATE_ERROR, $templates[$j]->getVar('tpl_file')) . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ID, '<strong>' . $templates[$j]->getVar('tpl_id') . '</strong>') . '</span>';
702
                                } else {
703
                                    $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_BLOCK_DELETE_DATA, '<strong>' . $templates[$j]->getVar('tpl_file') . '</strong>') . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ID, '<strong>' . $templates[$j]->getVar('tpl_id') . '</strong>');
704
                                }
705
                            }
706
                        }
707
                        unset($templates);
708
                    }
709
                }
710
            }
711
712
            // delete tables used by this module
713
            $modtables = $module->getInfo('tables');
714
            if ($modtables !== false && \is_array($modtables)) {
715
                $msgs[] = _AM_SYSTEM_MODULES_DELETE_MOD_TABLES;
716
                foreach ($modtables as $table) {
717
                    // prevent deletion of reserved core tables!
718
                    if (!in_array($table, $reservedTables)) {
719
                        $sql = 'DROP TABLE ' . $db->prefix($table);
720
                        if (!$db->query($sql)) {
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

720
                        if (!$db->/** @scrutinizer ignore-call */ query($sql)) {
Loading history...
721
                            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TABLE_DROPPED_ERROR, '<strong>' . $db->prefix($table) . '</strong>') . '</span>';
722
                        } else {
723
                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TABLE_DROPPED, '<strong>' . $db->prefix($table) . '</strong>');
724
                        }
725
                    } else {
726
                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TABLE_DROPPED_FAILDED, '<strong>' . $db->prefix($table) . '</strong>') . '</span>';
727
                    }
728
                }
729
            }
730
731
            // delete comments if any
732
            if ($module->getVar('hascomments') != 0) {
733
                $msgs[]          = _AM_SYSTEM_MODULES_COMMENTS_DELETE;
734
                $comment_handler = xoops_getHandler('comment');
735
                if (false === $comment_handler->deleteByModule($module->getVar('mid'))) {
0 ignored issues
show
Bug introduced by
The method deleteByModule() does not exist on XoopsObjectHandler. Did you maybe mean delete()? ( Ignorable by Annotation )

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

735
                if (false === $comment_handler->/** @scrutinizer ignore-call */ deleteByModule($module->getVar('mid'))) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
736
                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . _AM_SYSTEM_MODULES_COMMENTS_DELETE_ERROR . '</span>';
737
                } else {
738
                    $msgs[] = '&nbsp;&nbsp;' . _AM_SYSTEM_MODULES_COMMENTS_DELETED;
739
                }
740
            }
741
742
            // RMV-NOTIFY
743
            // delete notifications if any
744
            if ($module->getVar('hasnotification') != 0) {
745
                $msgs[] = _AM_SYSTEM_MODULES_NOTIFICATIONS_DELETE;
746
                if (false === xoops_notification_deletebymodule($module->getVar('mid'))) {
747
                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . _AM_SYSTEM_MODULES_NOTIFICATIONS_DELETE_ERROR . '</span>';
748
                } else {
749
                    $msgs[] = '&nbsp;&nbsp;' . _AM_SYSTEM_MODULES_NOTIFICATIONS_DELETED;
750
                }
751
            }
752
753
            // delete permissions if any
754
            $gperm_handler = xoops_getHandler('groupperm');
755
            if (false === $gperm_handler->deleteByModule($module->getVar('mid'))) {
756
                $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . _AM_SYSTEM_MODULES_GROUP_PERMS_DELETE_ERROR . '</span>';
757
            } else {
758
                $msgs[] = '&nbsp;&nbsp;' . _AM_SYSTEM_MODULES_GROUP_PERMS_DELETED;
759
            }
760
761
            // delete module config options if any
762
            if ($module->getVar('hasconfig') != 0 || $module->getVar('hascomments') != 0) {
763
                /** @var XoopsConfigHandler $config_handler */
764
                $config_handler = xoops_getHandler('config');
765
                $configs        = $config_handler->getConfigs(new Criteria('conf_modid', $module->getVar('mid')));
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('mid') 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

765
                $configs        = $config_handler->getConfigs(new Criteria('conf_modid', /** @scrutinizer ignore-type */ $module->getVar('mid')));
Loading history...
766
                $confcount      = count($configs);
767
                if ($confcount > 0) {
768
                    $msgs[] = _AM_SYSTEM_MODULES_MODULE_DATA_DELETE;
769
                    for ($i = 0; $i < $confcount; ++$i) {
770
                        if (false === $config_handler->deleteConfig($configs[$i])) {
771
                            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . _AM_SYSTEM_MODULES_CONFIG_DATA_DELETE_ERROR . sprintf(_AM_SYSTEM_MODULES_GONFIG_ID, '<strong>' . $configs[$i]->getvar('conf_id') . '</strong>') . '</span>';
772
                        } else {
773
                            $msgs[] = '&nbsp;&nbsp;' . _AM_SYSTEM_MODULES_GONFIG_DATA_DELETE . sprintf(_AM_SYSTEM_MODULES_GONFIG_ID, '<strong>' . $configs[$i]->getvar('conf_id') . '</strong>');
774
                        }
775
                    }
776
                }
777
            }
778
779
            // execute module specific install script if any
780
            $func = 'xoops_module_uninstall_' . $dirname;
781
            if (function_exists($func)) {
782
                if (!$func($module)) {
783
                    $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_EXECUTE, $func) . '</p>';
784
                } else {
785
                    $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_SUCESS, "<strong>{$func}</strong>") . '</p>';
786
                }
787
            }
788
            $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_OKUNINS, '<strong>' . $module->getVar('name') . '</strong>') . '</p>';
789
        }
790
        $msgs[] = '</div></div>';
791
		$msgs[] = '<div class="center"><a href="admin.php?fct=modulesadmin">' . _AM_SYSTEM_MODULES_BTOMADMIN . '</a></div>';
792
        $ret    = implode('<br>', $msgs);
793
794
        return $ret;
795
    }
796
}
797
798
/**
799
 * @param string $dirname
800
 * @return string
801
 */
802
function xoops_module_update($dirname)
803
{
804
    global $xoopsUser, $xoopsConfig, $xoopsTpl;
805
    $dirname = trim((string) $dirname);
806
    $xoopsDB =& $GLOBALS['xoopsDB'];
807
808
    $myts = \MyTextSanitizer::getInstance();
809
810
    $dirname        = $myts->htmlSpecialChars(trim($dirname));
811
    /** @var XoopsModuleHandler $module_handler */
812
    $module_handler = xoops_getHandler('module');
813
    $module         = $module_handler->getByDirname($dirname);
814
    // Save current version for use in the update function
815
    $prev_version = $module->getVar('version');
816
    $clearTpl     = new XoopsTpl();
817
    $clearTpl->xoopsClearCache($dirname);
818
819
    // we don't want to change the module name set by admin
820
    $temp_name = $module->getVar('name');
821
    $module->loadInfoAsVar($dirname);
822
    $module->setVar('name', $temp_name);
823
    $module->setVar('last_update', time());
824
    /*
825
        // Call Header
826
        // Define main template
827
        $GLOBALS['xoopsOption']['template_main'] = 'system_header.html';
828
        // Call Header
829
        xoops_cp_header();
830
        // Define Stylesheet
831
        $xoTheme->addStylesheet(XOOPS_URL . '/modules/system/css/admin.css');
832
        // Define Breadcrumb and tips
833
        $xoBreadCrumb->addLink(_AM_SYSTEM_MODULES_ADMIN, system_adminVersion('modulesadmin', 'adminpath'));
834
        $xoBreadCrumb->addLink(_AM_SYSTEM_MODULES_UPDATE);
835
        $xoBreadCrumb->addHelp(system_adminVersion('modulesadmin', 'help') . '#update');https://www.facebook.com/photo.php?v=10154358806675333
836
        $xoBreadCrumb->render();
837
838
        */
839
    if (!$module_handler->insert($module)) {
0 ignored issues
show
Bug introduced by
It seems like $module can also be of type false; however, parameter $module of XoopsModuleHandler::insert() does only seem to accept XoopsObject, 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

839
    if (!$module_handler->insert(/** @scrutinizer ignore-type */ $module)) {
Loading history...
840
        echo '<p>Could not update ' . $module->getVar('name') . '</p>';
841
        echo "<br><div class='center'><a href='admin.php?fct=modulesadmin'>" . _AM_SYSTEM_MODULES_BTOMADMIN . '</a></div>';
842
    } else {
843
        $newmid = $module->getVar('mid');
844
        $msgs   = [];
845
        $msgs[] = '<div id="xo-module-log"><div class="header">';
846
        $msgs[] = $errs[] = '<h4>' . _AM_SYSTEM_MODULES_UPDATING . $module->getInfo('name', 's') . '</h4>';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$errs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $errs = array(); before regardless.
Loading history...
Bug introduced by
Are you sure $module->getInfo('name', 's') of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

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

846
        $msgs[] = $errs[] = '<h4>' . _AM_SYSTEM_MODULES_UPDATING . /** @scrutinizer ignore-type */ $module->getInfo('name', 's') . '</h4>';
Loading history...
Unused Code introduced by
The call to XoopsModule::getInfo() has too many arguments starting with 's'. ( Ignorable by Annotation )

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

846
        $msgs[] = $errs[] = '<h4>' . _AM_SYSTEM_MODULES_UPDATING . $module->/** @scrutinizer ignore-call */ getInfo('name', 's') . '</h4>';

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
847
        if ($module->getInfo('image') !== false && trim($module->getInfo('image')) != '') {
0 ignored issues
show
Bug introduced by
It seems like $module->getInfo('image') can also be of type array; however, parameter $string of trim() 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

847
        if ($module->getInfo('image') !== false && trim(/** @scrutinizer ignore-type */ $module->getInfo('image')) != '') {
Loading history...
848
            $msgs[] = '<img src="' . XOOPS_URL . '/modules/' . $dirname . '/' . trim($module->getInfo('image')) . '" alt="" />';
849
        }
850
        $msgs[] = '<strong>' . _VERSION . ':</strong> ' . $module->getInfo('version');
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('version') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

850
        $msgs[] = '<strong>' . _VERSION . ':</strong> ' . /** @scrutinizer ignore-type */ $module->getInfo('version');
Loading history...
851
        if ($module->getInfo('author') !== false && trim($module->getInfo('author')) != '') {
852
            $msgs[] = '<strong>' . _AUTHOR . ':</strong> ' . $myts->htmlSpecialChars(trim($module->getInfo('author')));
853
        }
854
        $msgs[]          = '</div><div class="logger">';
855
856
        $update_script = $module->getInfo('onUpdate');
857
        if (!empty($update_script) && trim($update_script) != '') {
858
            include_once XOOPS_ROOT_PATH . '/modules/' . $dirname . '/' . trim($update_script);
859
        }
860
        // execute module specific update script if any
861
        if (function_exists('xoops_module_pre_update_' . $dirname)) {
862
            $func = 'xoops_module_pre_update_' . $dirname;
863
            if (!$func($module, $prev_version)) {
864
                $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_EXECUTE, $func) . '</p>';
865
                $msgs = array_merge($msgs, $module->getErrors());
866
            } else {
867
                $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_SUCESS, '<strong>' . $func . '</strong>') . '</p>';
868
                $msgs += $module->getErrors();
869
            }
870
        }
871
        $msgs[]          = _AM_SYSTEM_MODULES_MODULE_DATA_UPDATE;
872
        /** @var XoopsTplfileHandler $tplfile_handler */
873
        $tplfile_handler = xoops_getHandler('tplfile');
874
        // irmtfan bug fix: remove codes for delete templates
875
        /*
876
        $deltpl          = $tplfile_handler->find('default', 'module', $module->getVar('mid'));
877
        $delng           = array();
878
        if (is_array($deltpl)) {
879
            // delete template file entry in db
880
            $dcount = count($deltpl);
881
            for ($i = 0; $i < $dcount; $i++) {
882
                if (!$tplfile_handler->delete($deltpl[$i])) {
883
                    $delng[] = $deltpl[$i]->getVar('tpl_file');
884
                }
885
            }
886
        }
887
        */
888
        // irmtfan bug fix: remove codes for delete templates
889
        $templates = $module->getInfo('templates');
890
        if ($templates !== false) {
0 ignored issues
show
introduced by
The condition $templates !== false is always true.
Loading history...
891
            $msgs[] = _AM_SYSTEM_MODULES_TEMPLATES_UPDATE;
892
            foreach ($templates as $tpl) {
893
                $tpl['file'] = trim((string) $tpl['file']);
894
                // START irmtfan solve templates duplicate issue
895
                // if (!in_array($tpl['file'], $delng)) { // irmtfan bug fix: remove codes for delete templates
896
                $type = ($tpl['type'] ?? 'module');
897
                if (preg_match("/\.css$/i", $tpl['file'])) {
898
                    $type = 'css';
899
                }
900
                $criteria = new CriteriaCompo();
901
                $criteria->add(new Criteria('tpl_refid', $newmid), 'AND');
0 ignored issues
show
Bug introduced by
It seems like $newmid 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

901
                $criteria->add(new Criteria('tpl_refid', /** @scrutinizer ignore-type */ $newmid), 'AND');
Loading history...
902
                $criteria->add(new Criteria('tpl_module', $dirname), 'AND');
903
                $criteria->add(new Criteria('tpl_tplset', 'default'), 'AND');
904
                $criteria->add(new Criteria('tpl_file', $tpl['file']), 'AND');
905
                $criteria->add(new Criteria('tpl_type', $type), 'AND');
906
                $tplfiles = $tplfile_handler->getObjects($criteria);
907
908
                $tpldata =& xoops_module_gettemplate($dirname, $tpl['file'], $type);
909
                $tplfile = empty($tplfiles) ? $tplfile_handler->create() : $tplfiles[0];
910
                // END irmtfan solve templates duplicate issue
911
                $tplfile->setVar('tpl_refid', $newmid);
912
                $tplfile->setVar('tpl_lastimported', 0);
913
                $tplfile->setVar('tpl_lastmodified', time());
914
                $tplfile->setVar('tpl_type', $type);
915
                $tplfile->setVar('tpl_source', $tpldata, true);
916
                $tplfile->setVar('tpl_module', $dirname);
917
                $tplfile->setVar('tpl_tplset', 'default');
918
                $tplfile->setVar('tpl_file', $tpl['file'], true);
919
                $tplfile->setVar('tpl_desc', $tpl['description'], true);
920
                if (!$tplfile_handler->insert($tplfile)) {
921
                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ADD_ERROR, '<strong>' . $tpl['file'] . '</strong>') . '</span>';
922
                } else {
923
                    $newid  = $tplfile->getVar('tpl_id');
924
                    $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_INSERT_DATA, '<strong>' . $tpl['file'] . '</strong>');
925
                    if ($xoopsConfig['template_set'] === 'default') {
926
                        if (!xoops_template_touch($newid)) {
927
                            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_RECOMPILE_ERROR, '<strong>' . $tpl['file'] . '</strong>') . '</span>';
928
                        } else {
929
                            $msgs[] = '&nbsp;&nbsp;<span>' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_RECOMPILE, '<strong>' . $tpl['file'] . '</strong>') . '</span>';
930
                        }
931
                    }
932
                }
933
                unset($tpldata);
934
                // irmtfan bug fix: remove codes for delete templates
935
                /*
936
                } else {
937
                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(_AM_SYSTEM_MODULES_TEMPLATE_DELETE_OLD_ERROR, "<strong>".$tpl['file']."</strong>").'</span>';
938
                }
939
                */
940
                // irmtfan bug fix: remove codes for delete templates
941
            }
942
        }
943
        $blocks = $module->getInfo('blocks');
944
        $msgs[] = _AM_SYSTEM_MODULES_BLOCKS_REBUILD;
945
        if ($blocks !== false) {
0 ignored issues
show
introduced by
The condition $blocks !== false is always true.
Loading history...
946
            $showfuncs = [];
947
            $funcfiles = [];
948
            foreach ($blocks as $i => $block) {
949
                if (isset($block['show_func']) && $block['show_func'] != '' && isset($block['file']) && $block['file'] != '') {
950
                    $editfunc    = $block['edit_func'] ?? '';
951
                    $showfuncs[] = $block['show_func'];
952
                    $funcfiles[] = $block['file'];
953
                    $content = '';
954
                    $template = '';
955
                    if (isset($block['template']) && trim((string) $block['template']) != '') {
956
                        $content =& xoops_module_gettemplate($dirname, $block['template'], 'blocks');
957
                    }
958
                    if (!$content) {
959
                        $content = '';
960
                    } else {
961
                        $template = $block['template'];
962
                    }
963
                    $options = '';
964
                    if (!empty($block['options'])) {
965
                        $options = $block['options'];
966
                    }
967
                    $sql     = 'SELECT bid, name FROM ' . $xoopsDB->prefix('newblocks') . ' WHERE mid=' . $module->getVar('mid') . ' AND func_num=' . $i . " AND show_func='" . addslashes((string) $block['show_func']) . "' AND func_file='" . addslashes((string) $block['file']) . "'";
968
                    $fresult = $xoopsDB->query($sql);
969
                    if (!$xoopsDB->isResultSet($fresult)) {
970
                        throw new \RuntimeException(
971
                            \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(),
972
                            E_USER_ERROR,
973
                        );
974
                    }
975
                    $fcount  = 0;
976
                    while (false !== ($fblock = $xoopsDB->fetchArray($fresult))) {
977
                        ++$fcount;
978
                        $sql    = 'UPDATE ' . $xoopsDB->prefix('newblocks') . " SET name='" . addslashes((string) $block['name']) . "', edit_func='" . addslashes((string) $editfunc) . "', content='', template='" . $template . "', last_modified=" . time() . ' WHERE bid=' . $fblock['bid'];
979
                        $result = $xoopsDB->query($sql);
980
                        if (!$result) {
981
                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_UPDATE_ERROR, $fblock['name']);
982
                        } else {
983
                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_BLOCK_UPDATE, $fblock['name']) . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $fblock['bid'] . '</strong>');
984
                            if ($template != '') {
985
                                $tplfile = $tplfile_handler->find('default', 'block', $fblock['bid']);
986
                                if (count($tplfile) == 0) {
987
                                    $tplfile_new = $tplfile_handler->create();
988
                                    $tplfile_new->setVar('tpl_module', $dirname);
989
                                    $tplfile_new->setVar('tpl_refid', $fblock['bid']);
990
                                    $tplfile_new->setVar('tpl_tplset', 'default');
991
                                    $tplfile_new->setVar('tpl_file', $block['template'], true);
992
                                    $tplfile_new->setVar('tpl_type', 'block');
993
                                } else {
994
                                    $tplfile_new = $tplfile[0];
995
                                }
996
                                $tplfile_new->setVar('tpl_source', $content, true);
997
                                $tplfile_new->setVar('tpl_desc', $block['description'], true);
998
                                $tplfile_new->setVar('tpl_lastmodified', time());
999
                                $tplfile_new->setVar('tpl_lastimported', 0);
1000
                                $tplfile_new->setVar('tpl_file', $block['template'], true); // irmtfan bug fix:  block template file will not be updated after update the module
1001
                                if (!$tplfile_handler->insert($tplfile_new)) {
1002
                                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_UPDATE_ERROR, '<strong>' . $block['template'] . '</strong>') . '</span>';
1003
                                } else {
1004
                                    $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_UPDATE, '<strong>' . $block['template'] . '</strong>');
1005
                                    if ($xoopsConfig['template_set'] === 'default') {
1006
                                        if (!xoops_template_touch($tplfile_new->getVar('tpl_id'))) {
1007
                                            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_RECOMPILE_ERROR, '<strong>' . $block['template'] . '</strong>') . '</span>';
1008
                                        } else {
1009
                                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_RECOMPILE, '<strong>' . $block['template'] . '</strong>');
1010
                                        }
1011
                                    }
1012
                                }
1013
                            }
1014
                        }
1015
                    }
1016
                    if ($fcount == 0) {
1017
                        $newbid     = $xoopsDB->genId($xoopsDB->prefix('newblocks') . '_bid_seq');
1018
                        $block_name = addslashes((string) $block['name']);
1019
                        $block_type = ($module->getVar('dirname') === 'system') ? 'S' : 'M';
1020
                        $sql        = 'INSERT INTO ' . $xoopsDB->prefix('newblocks') . ' (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, isactive, dirname, func_file, show_func, edit_func, template, last_modified) VALUES (' . $newbid . ', ' . $module->getVar('mid') . ', ' . $i . ",'" . addslashes((string) $options) . "','" . $block_name . "', '" . $block_name . "', '', 0, 0, 0, '{$block_type}', 1, '" . addslashes($dirname) . "', '" . addslashes((string) $block['file']) . "', '" . addslashes((string) $block['show_func']) . "', '" . addslashes((string) $editfunc) . "', '" . $template . "', " . time() . ')';
1021
                        $result     = $xoopsDB->query($sql);
1022
                        if (!$result) {
1023
                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_SQL_NOT_CREATE, $block['name']);
1024
                            echo $sql;
1025
                        } else {
1026
                            if (empty($newbid)) {
1027
                                $newbid = $xoopsDB->getInsertId();
1028
                            }
1029
                            if ($module->getInfo('hasMain')) {
1030
                                $groups = [XOOPS_GROUP_ADMIN, XOOPS_GROUP_USERS, XOOPS_GROUP_ANONYMOUS];
1031
                            } else {
1032
                                $groups = [XOOPS_GROUP_ADMIN];
1033
                            }
1034
                            $gperm_handler = xoops_getHandler('groupperm');
1035
                            foreach ($groups as $mygroup) {
1036
                                $bperm = $gperm_handler->create();
1037
                                $bperm->setVar('gperm_groupid', $mygroup);
1038
                                $bperm->setVar('gperm_itemid', $newbid);
1039
                                $bperm->setVar('gperm_name', 'block_read');
1040
                                $bperm->setVar('gperm_modid', 1);
1041
                                if (!$gperm_handler->insert($bperm)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $gperm_handler->insert($bperm) targeting XoopsObjectHandler::insert() seems to always return null.

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

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

}

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

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

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

Loading history...
1042
                                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . _AM_SYSTEM_MODULES_BLOCK_ACCESS_ERROR . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $newbid . '</strong>') . sprintf(_AM_SYSTEM_MODULES_GROUP_ID, '<strong>' . $mygroup . '</strong>') . '</span>';
1043
                                } else {
1044
                                    $msgs[] = '&nbsp;&nbsp;' . _AM_SYSTEM_MODULES_BLOCK_ACCESS . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $newbid . '</strong>') . sprintf(_AM_SYSTEM_MODULES_GROUP_ID, '<strong>' . $mygroup . '</strong>');
1045
                                }
1046
                            }
1047
1048
                            if ($template != '') {
1049
                                $tplfile = $tplfile_handler->create();
1050
                                $tplfile->setVar('tpl_module', $dirname);
1051
                                $tplfile->setVar('tpl_refid', $newbid);
1052
                                $tplfile->setVar('tpl_source', $content, true);
1053
                                $tplfile->setVar('tpl_tplset', 'default');
1054
                                $tplfile->setVar('tpl_file', $block['template'], true);
1055
                                $tplfile->setVar('tpl_type', 'block');
1056
                                $tplfile->setVar('tpl_lastimported', time());
1057
                                $tplfile->setVar('tpl_lastmodified', time());
1058
                                $tplfile->setVar('tpl_desc', $block['description'], true);
1059
                                if (!$tplfile_handler->insert($tplfile)) {
1060
                                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ADD_ERROR, '<strong>' . $block['template'] . '</strong>') . '</span>';
1061
                                } else {
1062
                                    $newid  = $tplfile->getVar('tpl_id');
1063
                                    $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_ADD_DATA, '<strong>' . $block['template'] . '</strong>');
1064
                                    if ($xoopsConfig['template_set'] === 'default') {
1065
                                        if (!xoops_template_touch($newid)) {
1066
                                            $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_RECOMPILE_FAILD, '<strong>' . $block['template'] . '</strong>') . '</span>';
1067
                                        } else {
1068
                                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_TEMPLATE_RECOMPILE, '<strong>' . $block['template'] . '</strong>');
1069
                                        }
1070
                                    }
1071
                                }
1072
                            }
1073
                            $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_BLOCK_CREATED, '<strong>' . $block['name'] . '</strong>') . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $newbid . '</strong>');
1074
                            $sql    = 'INSERT INTO ' . $xoopsDB->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newbid . ', -1)';
1075
                            $xoopsDB->query($sql);
1076
                        }
1077
                    }
1078
                }
1079
            }
1080
            $block_arr = XoopsBlock::getByModule($module->getVar('mid'));
1081
            /** @var XoopsBlock $block */
1082
            foreach ($block_arr as $block) {
1083
                if (!in_array($block->getVar('show_func'), $showfuncs) || !in_array($block->getVar('func_file'), $funcfiles)) {
1084
                    $sql = sprintf('DELETE FROM %s WHERE bid = %u', $xoopsDB->prefix('newblocks'), $block->getVar('bid'));
1085
                    if (!$xoopsDB->query($sql)) {
1086
                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_BLOCK_DELETE_ERROR, '<strong>' . $block->getVar('name') . '</strong>') . sprintf(_AM_SYSTEM_MODULES_BLOCK_ID, '<strong>' . $block->getVar('bid') . '</strong>') . '</span>';
1087
                    } else {
1088
                        $msgs[] = '&nbsp;&nbsp;Block <strong>' . $block->getVar('name') . '</strong> deleted. Block ID: <strong>' . $block->getVar('bid') . '</strong>';
1089
                        if ($block->getVar('template') != '') {
1090
                            $tplfiles = $tplfile_handler->find(null, 'block', $block->getVar('bid'));
1091
                            if (is_array($tplfiles)) {
1092
                                $btcount = count($tplfiles);
1093
                                for ($k = 0; $k < $btcount; $k++) {
1094
                                    if (!$tplfile_handler->delete($tplfiles[$k])) {
1095
                                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . _AM_SYSTEM_MODULES_BLOCK_DEPRECATED_ERROR . '(ID: <strong>' . $tplfiles[$k]->getVar('tpl_id') . '</strong>)</span>';
1096
                                    } else {
1097
                                        $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_BLOCK_DEPRECATED, '<strong>' . $tplfiles[$k]->getVar('tpl_file') . '</strong>');
1098
                                    }
1099
                                }
1100
                            }
1101
                        }
1102
                    }
1103
                }
1104
            }
1105
        }
1106
1107
        // reset compile_id
1108
1109
        //        $xoTheme  =& $xoopsThemeFactory->createInstance(array('contentTemplate' => @$GLOBALS['xoopsOption']['template_main']));
1110
        //        $xoopsTpl =& $xoTheme->template;
1111
        //        $xoopsTpl->setCompileId();
1112
1113
        $template = $clearTpl;
1114
        $template->setCompileId();
1115
        //        $GLOBALS['xoopsTpl']->setCompileId();
1116
        //        $xoopsTpl->setCompileId();
1117
1118
        // first delete all config entries
1119
        /** @var XoopsConfigHandler $config_handler */
1120
        $config_handler = xoops_getHandler('config');
1121
        $configs        = $config_handler->getConfigs(new Criteria('conf_modid', $module->getVar('mid')));
1122
        $confcount      = count($configs);
1123
        $config_delng   = [];
1124
        if ($confcount > 0) {
1125
            $msgs[] = _AM_SYSTEM_MODULES_MODULE_DATA_DELETE;
1126
            for ($i = 0; $i < $confcount; $i++) {
1127
                if (!$config_handler->deleteConfig($configs[$i])) {
1128
                    $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . _AM_SYSTEM_MODULES_CONFIG_DATA_DELETE_ERROR . sprintf(_AM_SYSTEM_MODULES_GONFIG_ID, '<strong>' . $configs[$i]->getvar('conf_id') . '</strong>') . '</span>';
1129
                    // save the name of config failed to delete for later use
1130
                    $config_delng[] = $configs[$i]->getvar('conf_name');
1131
                } else {
1132
                    $config_old[$configs[$i]->getvar('conf_name')]['value']     = $configs[$i]->getvar('conf_value', 'N');
1133
                    $config_old[$configs[$i]->getvar('conf_name')]['formtype']  = $configs[$i]->getvar('conf_formtype');
1134
                    $config_old[$configs[$i]->getvar('conf_name')]['valuetype'] = $configs[$i]->getvar('conf_valuetype');
1135
                    $msgs[]                                                     = '&nbsp;&nbsp;' . _AM_SYSTEM_MODULES_GONFIG_DATA_DELETE . sprintf(_AM_SYSTEM_MODULES_GONFIG_ID, '<strong>' . $configs[$i]->getVar('conf_id') . '</strong>');
1136
                }
1137
            }
1138
        }
1139
1140
        // now reinsert them with the new settings
1141
        $configs = $module->getInfo('config');
1142
        if ($configs !== false) {
0 ignored issues
show
introduced by
The condition $configs !== false is always true.
Loading history...
1143
            if ($module->getVar('hascomments') != 0) {
1144
                include_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
1145
                array_push(
1146
                    $configs,
0 ignored issues
show
Bug introduced by
It seems like $configs can also be of type string; however, parameter $array of array_push() 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

1146
                    /** @scrutinizer ignore-type */ $configs,
Loading history...
1147
                    [
1148
                    'name'        => 'com_rule',
1149
                    'title'       => '_CM_COMRULES',
1150
                    'description' => '',
1151
                    'formtype'    => 'select',
1152
                    'valuetype'   => 'int',
1153
                    'default'     => 1,
1154
                    'options'     => [
1155
                        '_CM_COMNOCOM'        => XOOPS_COMMENT_APPROVENONE,
1156
                        '_CM_COMAPPROVEALL'   => XOOPS_COMMENT_APPROVEALL,
1157
                        '_CM_COMAPPROVEUSER'  => XOOPS_COMMENT_APPROVEUSER,
1158
                            '_CM_COMAPPROVEADMIN' => XOOPS_COMMENT_APPROVEADMIN,
1159
                        ],
1160
                    ],
1161
                );
1162
                array_push(
1163
                    $configs,
1164
                    [
1165
                    'name'        => 'com_anonpost',
1166
                    'title'       => '_CM_COMANONPOST',
1167
                    'description' => '',
1168
                    'formtype'    => 'yesno',
1169
                    'valuetype'   => 'int',
1170
                        'default'     => 0,
1171
                    ],
1172
                );
1173
            }
1174
        } else {
1175
            if ($module->getVar('hascomments') != 0) {
1176
                $configs = [];
1177
                include_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
1178
                $configs[] = [
1179
                    'name'        => 'com_rule',
1180
                    'title'       => '_CM_COMRULES',
1181
                    'description' => '',
1182
                    'formtype'    => 'select',
1183
                    'valuetype'   => 'int',
1184
                    'default'     => 1,
1185
                    'options'     => [
1186
                        '_CM_COMNOCOM'        => XOOPS_COMMENT_APPROVENONE,
1187
                        '_CM_COMAPPROVEALL'   => XOOPS_COMMENT_APPROVEALL,
1188
                        '_CM_COMAPPROVEUSER'  => XOOPS_COMMENT_APPROVEUSER,
1189
                        '_CM_COMAPPROVEADMIN' => XOOPS_COMMENT_APPROVEADMIN,
1190
                    ],
1191
                ];
1192
                $configs[] = [
1193
                    'name'        => 'com_anonpost',
1194
                    'title'       => '_CM_COMANONPOST',
1195
                    'description' => '',
1196
                    'formtype'    => 'yesno',
1197
                    'valuetype'   => 'int',
1198
                    'default'     => 0,
1199
                ];
1200
            }
1201
        }
1202
        // RMV-NOTIFY
1203
        if ($module->getVar('hasnotification') != 0) {
1204
            if (empty($configs)) {
1205
                $configs = [];
1206
            }
1207
            // Main notification options
1208
            include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
1209
            include_once XOOPS_ROOT_PATH . '/include/notification_functions.php';
1210
            $options                             = [];
1211
            $options['_NOT_CONFIG_DISABLE']      = XOOPS_NOTIFICATION_DISABLE;
1212
            $options['_NOT_CONFIG_ENABLEBLOCK']  = XOOPS_NOTIFICATION_ENABLEBLOCK;
1213
            $options['_NOT_CONFIG_ENABLEINLINE'] = XOOPS_NOTIFICATION_ENABLEINLINE;
1214
            $options['_NOT_CONFIG_ENABLEBOTH']   = XOOPS_NOTIFICATION_ENABLEBOTH;
1215
1216
            //$configs[] = array ('name' => 'notification_enabled', 'title' => '_NOT_CONFIG_ENABLED', 'description' => '_NOT_CONFIG_ENABLEDDSC', 'formtype' => 'yesno', 'valuetype' => 'int', 'default' => 1);
1217
            $configs[] = [
1218
                'name'        => 'notification_enabled',
1219
                'title'       => '_NOT_CONFIG_ENABLE',
1220
                'description' => '_NOT_CONFIG_ENABLEDSC',
1221
                'formtype'    => 'select',
1222
                'valuetype'   => 'int',
1223
                'default'     => XOOPS_NOTIFICATION_ENABLEBOTH,
1224
                'options'     => $options,
1225
            ];
1226
            // Event specific notification options
1227
            // FIXME: for some reason the default doesn't come up properly
1228
            //  initially is ok, but not when 'update' module..
1229
            $options    = [];
1230
            $categories =& notificationCategoryInfo('', $module->getVar('mid'));
1231
            foreach ($categories as $category) {
1232
                $events =& notificationEvents($category['name'], false, $module->getVar('mid'));
1233
                foreach ($events as $event) {
1234
                    if (!empty($event['invisible'])) {
1235
                        continue;
1236
                    }
1237
                    $option_name           = $category['title'] . ' : ' . $event['title'];
1238
                    $option_value          = $category['name'] . '-' . $event['name'];
1239
                    $options[$option_name] = $option_value;
1240
                    //$configs[] = array ('name' => notificationGenerateConfig($category,$event,'name'), 'title' => notificationGenerateConfig($category,$event,'title_constant'), 'description' => notificationGenerateConfig($category,$event,'description_constant'), 'formtype' => 'yesno', 'valuetype' => 'int', 'default' => 1);
1241
                }
1242
            }
1243
            $configs[] = [
1244
                'name'        => 'notification_events',
1245
                'title'       => '_NOT_CONFIG_EVENTS',
1246
                'description' => '_NOT_CONFIG_EVENTSDSC',
1247
                'formtype'    => 'select_multi',
1248
                'valuetype'   => 'array',
1249
                'default'     => array_values($options),
1250
                'options'     => $options,
1251
            ];
1252
        }
1253
1254
        if ($configs !== false) {
1255
            $msgs[]         = 'Adding module config data...';
1256
            /** @var XoopsConfigHandler $config_handler */
1257
            $config_handler = xoops_getHandler('config');
1258
            $order          = 0;
1259
            foreach ($configs as $config) {
1260
                // only insert ones that have been deleted previously with success
1261
                if (!in_array($config['name'], $config_delng)) {
1262
                    $confobj = $config_handler->createConfig();
1263
                    $confobj->setVar('conf_modid', $newmid);
1264
                    $confobj->setVar('conf_catid', 0);
1265
                    $confobj->setVar('conf_name', $config['name']);
1266
                    $confobj->setVar('conf_title', $config['title'], true);
1267
                    $confobj->setVar('conf_desc', $config['description'], true);
1268
                    $confobj->setVar('conf_formtype', $config['formtype']);
1269
                    if (isset($config['valuetype'])) {
1270
                        $confobj->setVar('conf_valuetype', $config['valuetype']);
1271
                    }
1272
                    if (isset($config_old[$config['name']]['value']) && $config_old[$config['name']]['formtype'] == $config['formtype'] && $config_old[$config['name']]['valuetype'] == $config['valuetype']) {
1273
                        // preserver the old value if any
1274
                        // form type and value type must be the same
1275
                        $confobj->setVar('conf_value', $config_old[$config['name']]['value'], true);
1276
                    } else {
1277
                        $confobj->setConfValueForInput($config['default'], true);
1278
1279
                        //$confobj->setVar('conf_value', $config['default'], true);
1280
                    }
1281
                    $confobj->setVar('conf_order', $order);
1282
                    $confop_msgs = '';
1283
                    if (isset($config['options']) && \is_array($config['options'])) {
1284
                        foreach ($config['options'] as $key => $value) {
1285
                            $confop = $config_handler->createConfigOption();
1286
                            $confop->setVar('confop_name', $key, true);
1287
                            $confop->setVar('confop_value', $value, true);
1288
                            $confobj->setConfOptions($confop);
1289
                            $confop_msgs .= '<br>&nbsp;&nbsp;&nbsp;&nbsp; ' . _AM_SYSTEM_MODULES_CONFIG_ADD . _AM_SYSTEM_MODULES_NAME . ' <strong>' . (defined($key) ? constant($key) : $key) . '</strong> ' . _AM_SYSTEM_MODULES_VALUE . ' <strong>' . $value . '</strong> ';
1290
                            unset($confop);
1291
                        }
1292
                    }
1293
                    $order++;
1294
                    if (false !== $config_handler->insertConfig($confobj)) {
1295
                        //$msgs[] = '&nbsp;&nbsp;Config <strong>'.$config['name'].'</strong> added to the database.'.$confop_msgs;
1296
                        $msgs[] = '&nbsp;&nbsp;' . sprintf(_AM_SYSTEM_MODULES_CONFIG_DATA_ADD, '<strong>' . $config['name'] . '</strong>') . $confop_msgs;
1297
                    } else {
1298
                        $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(_AM_SYSTEM_MODULES_CONFIG_DATA_ADD_ERROR, '<strong>' . $config['name'] . '</strong>') . '</span>';
1299
                    }
1300
                    unset($confobj);
1301
                }
1302
            }
1303
            unset($configs);
1304
        }
1305
1306
        // execute module specific update script if any
1307
        if (function_exists('xoops_module_update_' . $dirname)) {
1308
            $func = 'xoops_module_update_' . $dirname;
1309
            if (!$func($module, $prev_version)) {
1310
                $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_EXECUTE, $func) . '</p>';
1311
                $msgs = array_merge($msgs, $module->getErrors());
1312
            } else {
1313
                $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILED_SUCESS, '<strong>' . $func . '</strong>') . '</p>';
1314
                $msgs += $module->getErrors();
1315
            }
1316
        }
1317
        $msgs[] = sprintf(_AM_SYSTEM_MODULES_OKUPD, '<strong>' . $module->getVar('name', 's') . '</strong>');
1318
        $msgs[] = '</div></div>';
1319
		$msgs[] = '<div class="center"><a href="admin.php?fct=modulesadmin">' . _AM_SYSTEM_MODULES_BTOMADMIN . '</a>  | <a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname', 'e') . '/' . $module->getInfo('adminindex') . '">' . _AM_SYSTEM_MODULES_INSTALL_THISMODULE . '</a></div>';
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('dirname', 'e') of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

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

1319
		$msgs[] = '<div class="center"><a href="admin.php?fct=modulesadmin">' . _AM_SYSTEM_MODULES_BTOMADMIN . '</a>  | <a href="' . XOOPS_URL . '/modules/' . /** @scrutinizer ignore-type */ $module->getInfo('dirname', 'e') . '/' . $module->getInfo('adminindex') . '">' . _AM_SYSTEM_MODULES_INSTALL_THISMODULE . '</a></div>';
Loading history...
Bug introduced by
Are you sure $module->getInfo('adminindex') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

1319
		$msgs[] = '<div class="center"><a href="admin.php?fct=modulesadmin">' . _AM_SYSTEM_MODULES_BTOMADMIN . '</a>  | <a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname', 'e') . '/' . /** @scrutinizer ignore-type */ $module->getInfo('adminindex') . '">' . _AM_SYSTEM_MODULES_INSTALL_THISMODULE . '</a></div>';
Loading history...
1320
        //        foreach ($msgs as $msg) {
1321
        //            echo $msg . '<br>';
1322
        //        }
1323
    }
1324
    // Call Footer
1325
    //    xoops_cp_footer();
1326
    // Flush cache files for cpanel GUIs
1327
    //    xoops_load("cpanel", "system");
1328
    //    XoopsSystemCpanel::flush();
1329
    //
1330
    //    require_once XOOPS_ROOT_PATH . '/modules/system/class/maintenance.php';
1331
    //    $maintenance = new SystemMaintenance();
1332
    //    $folder      = array(1, 3);
1333
    //    $maintenance->CleanCache($folder);
1334
    //Set active modules in cache folder
1335
    //    xoops_setActiveModules();
1336
    //    break;
1337
    //-----------------------------------------------
1338
1339
    $ret = implode('<br>', $msgs);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $msgs does not seem to be defined for all execution paths leading up to this point.
Loading history...
1340
1341
    return $ret;
1342
}
1343
1344
/**
1345
 * @param $mid
1346
 *
1347
 * @return string
1348
 */
1349
function xoops_module_activate($mid)
1350
{
1351
    // Get module handler
1352
1353
    $module_handler = xoops_getHandler('module');
1354
    /** @var \XoopsModule $module */
1355
    $module         = $module_handler->get($mid);
1356
    include_once XOOPS_ROOT_PATH . '/class/template.php';
1357
    xoops_template_clear_module_cache($module->getVar('mid'));
1358
    // Display header
1359
    $msgs   = [];
1360
    $msgs[] = '<div id="xo-module-log">';
1361
1362
    $logHeader = xoops_module_log_header($module, _AM_SYSTEM_MODULES_ACTIVATE);
1363
    $msgs[] = implode('<br>', $logHeader);
1364
1365
    // Change value
1366
    $module->setVar('isactive', 1);
1367
    if (!$module_handler->insert($module)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $module_handler->insert($module) targeting XoopsObjectHandler::insert() seems to always return null.

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

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

}

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

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

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

Loading history...
1368
        $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILACT, '<strong>' . $module->getVar('name', 's') . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br>' . $module->getHtmlErrors() . '</p>';
1369
    } else {
1370
        $blocks = XoopsBlock::getByModule($module->getVar('mid'));
1371
        $bcount = count($blocks);
1372
        for ($i = 0; $i < $bcount; ++$i) {
1373
            $blocks[$i]->setVar('isactive', 1);
1374
            $blocks[$i]->store();
1375
        }
1376
        // provide a link to the activated module
1377
        $moduleName = $module->getVar('name', 's');
1378
        $moduleLink = '<a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname') . '/' . $module->getInfo('adminindex') . '">' . $moduleName . '</a>';
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('adminindex') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

1378
        $moduleLink = '<a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname') . '/' . /** @scrutinizer ignore-type */ $module->getInfo('adminindex') . '">' . $moduleName . '</a>';
Loading history...
Bug introduced by
Are you sure $module->getInfo('dirname') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

1378
        $moduleLink = '<a href="' . XOOPS_URL . '/modules/' . /** @scrutinizer ignore-type */ $module->getInfo('dirname') . '/' . $module->getInfo('adminindex') . '">' . $moduleName . '</a>';
Loading history...
1379
        $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_OKACT, '<strong>' . $moduleLink . '</strong>') . '</p></div>';
1380
1381
    }
1382
    //$msgs[] = '</div>';
1383
    $msgs[] = '<div class="center"><a href="admin.php?fct=modulesadmin">' . _AM_SYSTEM_MODULES_BTOMADMIN . '</a></div>';
1384
    $ret    = implode('<br>', $msgs);
1385
1386
    return $ret;
1387
}
1388
1389
/**
1390
 * @param $mid
1391
 *
1392
 * @return string
1393
 */
1394
function xoops_module_deactivate($mid)
1395
{
1396
    global $xoopsConfig;
1397
    // Get module handler
1398
1399
    $module_handler = xoops_getHandler('module');
1400
    $module         = $module_handler->get($mid);
1401
    include_once XOOPS_ROOT_PATH . '/class/template.php';
1402
    xoops_template_clear_module_cache($mid);
1403
    // Display header
1404
    $msgs   = [];
1405
    $msgs[] = '<div id="xo-module-log">';
1406
1407
    $logHeader = xoops_module_log_header($module, _AM_SYSTEM_MODULES_DEACTIVATE);
1408
    $msgs[] = implode('<br>', $logHeader);
1409
1410
    // Change value
1411
    $module->setVar('isactive', 0);
1412
    if ($module->getVar('dirname') === 'system') {
1413
        $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILDEACT, '<strong>' . $module->getVar('name') . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br> - ' . _AM_SYSTEM_MODULES_SYSNO . '</p>';
1414
    } elseif ($module->getVar('dirname') == $xoopsConfig['startpage']) {
1415
        $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILDEACT, '<strong>' . $module->getVar('name') . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br> - ' . _AM_SYSTEM_MODULES_STRTNO . '</p>';
1416
    } else {
1417
        if (!$module_handler->insert($module)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $module_handler->insert($module) targeting XoopsObjectHandler::insert() seems to always return null.

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

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

}

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

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

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

Loading history...
1418
            $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILDEACT, '<strong>' . $module->getVar('name') . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br>' . $module->getHtmlErrors() . '</p>';
1419
        } else {
1420
            $blocks = XoopsBlock::getByModule($module->getVar('mid'));
1421
            $bcount = count($blocks);
1422
            for ($i = 0; $i < $bcount; ++$i) {
1423
                $blocks[$i]->setVar('isactive', 0);
1424
                $blocks[$i]->store();
1425
            }
1426
            $msgs[] = '<p>' . sprintf(_AM_SYSTEM_MODULES_OKDEACT, '<strong>' . $module->getVar('name') . '</strong>') . '</p>';
1427
        }
1428
    }
1429
    $msgs[] = '<div class="center"><a href="admin.php?fct=modulesadmin">' . _AM_SYSTEM_MODULES_BTOMADMIN . '</a></div>';
1430
    $ret    = implode('<br>', $msgs);
1431
1432
    return $ret;
1433
}
1434
1435
/**
1436
 * @param $mid
1437
 * @param $name
1438
 *
1439
 * @return string
1440
 */
1441
function xoops_module_change($mid, $name)
1442
{
1443
1444
    $module_handler = xoops_getHandler('module');
1445
    $module         = $module_handler->get($mid);
1446
    $module->setVar('name', $name);
1447
    if (!$module_handler->insert($module)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $module_handler->insert($module) targeting XoopsObjectHandler::insert() seems to always return null.

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

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

}

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

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

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

Loading history...
1448
        $ret = '<p>' . sprintf(_AM_SYSTEM_MODULES_FAILORDER, '<strong>' . $name . '</strong>') . '&nbsp;' . _AM_SYSTEM_MODULES_ERRORSC . '<br>';
1449
        $ret .= $module->getHtmlErrors() . '</p>';
1450
1451
        return $ret;
1452
    }
1453
1454
    return '<p>' . sprintf(_AM_SYSTEM_MODULES_OKORDER, '<strong>' . $name . '</strong>') . '</p>';
1455
}
1456
1457
/**
1458
 * @param $module
1459
 * @param $title
1460
 *
1461
 * @return array
1462
 */
1463
function xoops_module_log_header($module, $title)
1464
{
1465
    $msgs[] = '<div class="header">';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$msgs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $msgs = array(); before regardless.
Loading history...
1466
    $msgs[] = $errs[] = '<h4>' . $title . $module->getInfo('name', 's') . '</h4>';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$errs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $errs = array(); before regardless.
Loading history...
1467
1468
    if ($module->getInfo('image') !== false && trim((string) $module->getInfo('image')) != '') {
1469
        if (_AM_SYSTEM_MODULES_ACTIVATE === $title) {
1470
            $msgs[] = '<a href="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname', 'e') . '/' . $module->getInfo('adminindex') . '"><img src="' . XOOPS_URL . '/modules/' . $module->getInfo('dirname', 'e') . '/' . trim((string) $module->getInfo('image')) . '" alt="" /></a>';
1471
        } else {
1472
            $msgs[] = '<img src="' . XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/' . trim((string) $module->getInfo('image')) . '" alt="" />';
1473
        }
1474
    }
1475
    $msgs[] = '<strong>' . _VERSION . ':</strong> ' . $module->getInfo('version');
1476
    if ($module->getInfo('author') !== false && trim((string) $module->getInfo('author')) != '') {
1477
        $msgs[] = '<strong>' . _AUTHOR . ':</strong> ' . htmlspecialchars(trim((string) $module->getInfo('author')), ENT_QUOTES | ENT_HTML5);
1478
    }
1479
    $msgs[] = '</div>';
1480
1481
    return $msgs;
1482
}
1483
1484
/**
1485
 * Clean cache 'xoops_data/caches/smarty_cache'
1486
 *
1487
 * @param array|null $cacheList int[] of cache "ids"
1488
 *                              1 = smarty cache
1489
 *                              2 = smarty compile
1490
 *                              3 = xoops cache
1491
 *                              or null to clear all
1492
 * @return bool
1493
 */
1494
function xoops_module_delayed_clean_cache($cacheList = null)
1495
{
1496
    if (empty($cacheList)) {
1497
        $cacheList = [1, 2, 3];
1498
    }
1499
    require_once XOOPS_ROOT_PATH . '/modules/system/class/maintenance.php';
1500
    $maintenance = new SystemMaintenance();
1501
    register_shutdown_function([$maintenance, 'CleanCache'], $cacheList);
1502
}
1503