Completed
Pull Request — master (#563)
by Richard
08:33
created

SystemModule::deleteBlocks()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 50
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 41
nc 10
nop 1
dl 0
loc 50
rs 6.3636
c 0
b 0
f 0
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
use Xoops\Core\Database\Schema\ImportSchema;
13
use Xoops\Core\FixedGroups;
14
use Xoops\Core\Kernel\Criteria;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Criteria. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Xoops\Core\Kernel\CriteriaCompo;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CriteriaCompo. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use Xoops\Core\Kernel\Handlers\XoopsBlock;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsBlock. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Xoops\Core\Kernel\Handlers\XoopsModule;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModule. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use Xoops\Module\Plugin\ConfigCollector;
19
use Xmf\Yaml;
20
use Doctrine\DBAL\DBALException;
21
use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer;
22
23
/**
24
 * System Module
25
 *
26
 * @category  SystemModule
27
 * @package   SystemModule
28
 * @author    Andricq Nicolas (AKA MusS)
29
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
30
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
31
 * @link      http://xoops.org
32
 */
33
class SystemModule
34
{
35
    public $error = array();
36
    public $trace = array();
37
    protected $modulesList = array();
38
    protected $modulesDirnames = array();
39
    protected $config_delng = array();
40
    protected $template_delng = array();
41
    protected $config_old = array();
42
    protected $reservedTables = array(
43
        'system_blockmodule',
44
        'system_config',
45
        'system_configoption',
46
        'system_group',
47
        'system_usergroup',
48
        'system_permission',
49
        'system_module',
50
        'system_block',
51
        'system_online',
52
        'system_privatemessage',
53
        'system_session',
54
        'system_tplfile',
55
        'system_tplset',
56
        'system_tplsource',
57
        'system_user',
58
    );
59
60
    /**
61
     * Constructor
62
     */
63
    public function __construct()
64
    {
65
        // Get main instance
66
        $xoops = Xoops::getInstance();
67
        $module_handler = $xoops->getHandlerModule();
68
69
        $this->modulesList = XoopsLists::getModulesList();
70
71
        $modules = $module_handler->getObjectsArray();
72
        /* @var $module XoopsModule */
73
        foreach ($modules as $module) {
74
            $this->modulesDirnames[] = $module->getInfo('dirname');
75
        }
76
    }
77
78
    /**
79
     * getModuleList
80
     *
81
     * @return array of modules
82
     */
83
    public function getModuleList()
84
    {
85
        // Get main instance
86
        $xoops = Xoops::getInstance();
87
        $module_handler = $xoops->getHandlerModule();
88
        $moduleperm_handler = $xoops->getHandlerGroupPermission();
89
90
        $criteria = new CriteriaCompo();
91
        $criteria->setSort('weight');
92
        // Get all installed modules
93
        $modules = $module_handler->getObjects($criteria, true);
94
        $list = array();
95
        /* @var $module XoopsModule */
96
        foreach ($modules as $module) {
97
            if (!$module->getInfo('extension')) {
98
                if ($module->getInfo('dirname') === 'system') {
99
                    $module->setInfo('can_delete', false);
100
                    $module->setInfo('can_disable', false);
101
                } else {
102
                    $module->setInfo('can_delete', true);
103
                    $module->setInfo('can_disable', true);
104
                }
105
                if (round($module->getInfo('version'), 2) != $module->getVar('version')) {
106
                    $module->setInfo('warning_update', true);
107
                }
108
                if (XoopsLoad::fileExists(
109
                    \XoopsBaseConfig::get('root-path') . '/modules/' . $module->getVar('dirname') . '/icons/logo_small.png'
110
                )) {
111
                    $module->setInfo(
112
                        'logo_small',
113
                        \XoopsBaseConfig::get('url') . '/modules/' . $module->getVar('dirname') . '/icons/logo_small.png'
114
                    );
115
                } else {
116
                    $module->setInfo('logo_small', \XoopsBaseConfig::get('url') . '/media/xoops/images/icons/16/default.png');
117
                }
118
                $module->setInfo('version', round($module->getVar('version') / 100, 2));
119
                $module->setInfo('update', XoopsLocale::formatTimestamp($module->getVar('last_update'), 's'));
120
                $module->setInfo(
121
                    'link_admin',
122
                    \XoopsBaseConfig::get('url') . '/modules/' . $module->getVar('dirname') . '/' . $module->getInfo('adminindex')
123
                );
124
125
                if ($module->getVar('isactive')) {
126
                    $module->setInfo('options', $module->getAdminMenu());
127
                }
128
129
                $groups = array();
130
                if (is_object($xoops->user)) {
131
                    $groups = $xoops->user->getGroups();
132
                }
133
134
                $sadmin = $moduleperm_handler->checkRight(
135
                    'module_admin',
136
                    $module->getVar('mid'),
137
                    $groups
138
                );
139
                if ($sadmin && ($module->getVar('hasnotification')
140
                    || is_array($module->getInfo('config')) || is_array($module->getInfo('comments')))
141
                ) {
142
                    $module->setInfo(
143
                        'link_pref',
144
                        \XoopsBaseConfig::get('url') . '/modules/system/admin.php?fct=preferences&amp;op=showmod&amp;mod='
145
                        . $module->getVar('mid')
146
                    );
147
                }
148
149
                $list[] = $module;
150
            }
151
        }
152
        return $list;
153
    }
154
155
    /**
156
     * getInstalledModules
157
     *
158
     * @return array of installed modules
159
     */
160
    public function getInstalledModules()
161
    {
162
        // Get main instance
163
        $xoops = Xoops::getInstance();
164
        $module_handler = $xoops->getHandlerModule();
165
166
        $ret = array();
167
        $i = 0;
168
        foreach ($this->modulesList as $file) {
169
            if (XoopsLoad::fileExists(\XoopsBaseConfig::get('root-path') . '/modules/' . $file . '/xoops_version.php')) {
170
                clearstatcache();
171
                $file = trim($file);
172
                if (!in_array($file, $this->modulesDirnames)) {
173
                    /* @var $module XoopsModule */
174
                    $module = $module_handler->create();
175
                    $module->loadInfo($file);
176
                    if (!$module->getInfo('extension')) {
177
                        $module->setInfo('mid', $i);
178
                        $module->setInfo('version', round($module->getInfo('version'), 2));
0 ignored issues
show
Bug introduced by
$module->getInfo('version') of type string|string[] is incompatible with the type double expected by parameter $val of round(). ( Ignorable by Annotation )

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

178
                        $module->setInfo('version', round(/** @scrutinizer ignore-type */ $module->getInfo('version'), 2));
Loading history...
179
                        $ret[] = $module;
180
                        unset($module);
181
                        ++$i;
182
                    }
183
                }
184
            }
185
        }
186
        return $ret;
187
    }
188
189
    /**
190
     * install a module
191
     *
192
     * @param string  $mod   module dirname
193
     * @param boolean $force force query
194
     *
195
     * @return bool|XoopsModule|XoopsObject
196
     */
197
    public function install($mod = '', $force = false)
0 ignored issues
show
Unused Code introduced by
The parameter $force is not used and could be removed. ( Ignorable by Annotation )

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

197
    public function install($mod = '', /** @scrutinizer ignore-unused */ $force = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
198
    {
199
        $xoops = Xoops::getInstance();
200
        $module_handler = $xoops->getHandlerModule();
201
        $mod = trim($mod);
202
        try {
203
            $cnt = $module_handler->getCount(new Criteria('dirname', $mod));
204
        } catch (DBALException $e) {
205
            $cnt = 0;
206
        }
207
        if ($cnt == 0) {
208
            /* @var $module XoopsModule */
209
            $module = $module_handler->create();
210
            $module->loadInfoAsVar($mod);
211
            $module->setVar('weight', 1);
212
            $module->setVar('isactive', 1);
213
            $module->setVar('last_update', time());
214
            $install_script = $module->getInfo('onInstall');
215
            if ($install_script && trim($install_script) != '') {
0 ignored issues
show
Bug introduced by
It seems like $install_script can also be of type string[]; however, parameter $str 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

215
            if ($install_script && trim(/** @scrutinizer ignore-type */ $install_script) != '') {
Loading history...
216
                XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($install_script)));
217
            }
218
            $func = "xoops_module_pre_install_{$mod}";
219
            // If pre install function is defined, execute
220
            if (function_exists($func)) {
221
                $result = $func($module);
222
                if (!$result) {
223
                    $this->error[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func);
224
                    $this->error = array_merge($this->error, $module->getErrors());
225
                    return false;
226
                } else {
227
                    $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>");
228
                    $this->trace = array_merge($this->trace, $module->getMessages());
229
                }
230
            }
231
            // Create tables
232
            $created_tables = array();
233
            if (count($this->error) == 0) {
234
                $schema_file = $module->getInfo('schema');
235
                $sql_file = $module->getInfo('sqlfile');
236
                if (!empty($schema_file)) {
237
                    $schema_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $schema_file;
0 ignored issues
show
Bug introduced by
Are you sure $schema_file of type string|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

237
                    $schema_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . /** @scrutinizer ignore-type */ $schema_file;
Loading history...
238
                    if (!XoopsLoad::fileExists($schema_file_path)) {
239
                        $this->error[] =
240
                            sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$schema_file}</strong>");
241
                        return false;
242
                    }
243
                    $importer = new ImportSchema;
244
                    $importSchema = $importer->importSchemaArray(Yaml::read($schema_file_path));
0 ignored issues
show
Bug introduced by
It seems like Xmf\Yaml::read($schema_file_path) can also be of type boolean; however, parameter $schemaArray of Xoops\Core\Database\Sche...ma::importSchemaArray() 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

244
                    $importSchema = $importer->importSchemaArray(/** @scrutinizer ignore-type */ Yaml::read($schema_file_path));
Loading history...
245
                    $synchronizer = new SingleDatabaseSynchronizer($xoops->db());
246
                    $synchronizer->updateSchema($importSchema, true);
247
                } elseif (is_array($sql_file) && !empty($sql_file[\XoopsBaseConfig::get('db-type')])) {
248
                    $xoops->deprecated('Install SQL files are deprecated since 2.6.0. Convert to portable Schemas');
249
250
                    $sql_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $sql_file[\XoopsBaseConfig::get('db-type')];
251
                    if (!XoopsLoad::fileExists($sql_file_path)) {
252
                        $this->error[] =
253
                            sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$sql_file_path}</strong>");
254
                        return false;
255
                    } else {
256
                        $this->trace[] = sprintf(SystemLocale::SF_SQL_FILE_FOUND, "<strong>{$sql_file_path}</strong>");
257
                        $this->trace[] = SystemLocale::MANAGING_TABLES;
258
259
                        $sql_query = fread(fopen($sql_file_path, 'r'), filesize($sql_file_path));
0 ignored issues
show
Bug introduced by
It seems like fopen($sql_file_path, 'r') can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

259
                        $sql_query = fread(/** @scrutinizer ignore-type */ fopen($sql_file_path, 'r'), filesize($sql_file_path));
Loading history...
260
                        $sql_query = trim($sql_query);
261
                        SqlUtility::splitMySqlFile($pieces, $sql_query);
262
                        foreach ($pieces as $piece) {
263
                            // [0] contains the prefixed query
264
                            // [4] contains unprefixed table name
265
                            $prefixed_query = SqlUtility::prefixQuery($piece, $xoops->db()->prefix());
266
                            if (!$prefixed_query) {
267
                                $this->error[]['sub'] = '<span class="red">' . sprintf(
268
                                    XoopsLocale::EF_INVALID_SQL,
269
                                    '<strong>' . $piece . '</strong>'
270
                                ) . '</span>';
271
                                break;
272
                            }
273
                            // check if the table name is reserved
274
                            if (!in_array($prefixed_query[4], $this->reservedTables) || $mod === 'system') {
275
                                // not reserved, so try to create one
276
                                try {
277
                                    $result = $xoops->db()->query($prefixed_query[0]);
278
                                } catch (Exception $e) {
279
                                    $xoops->events()->triggerEvent('core.exception', $e);
280
                                    $result=false;
281
                                }
282
283
                                if (!$result) {
284
                                    $this->error[] = $xoops->db()->errorInfo();
285
                                    break;
286
                                } else {
287
                                    if (!in_array($prefixed_query[4], $created_tables)) {
288
                                        $this->trace[]['sub'] = sprintf(
289
                                            XoopsLocale::SF_TABLE_CREATED,
290
                                            '<strong>' . $xoops->db()->prefix($prefixed_query[4]) . '</strong>'
291
                                        );
292
                                        $created_tables[] = $prefixed_query[4];
293
                                    } else {
294
                                        $this->trace[]['sub'] = sprintf(
295
                                            XoopsLocale::SF_DATA_INSERTED_TO_TABLE,
296
                                            '<strong>' . $xoops->db()->prefix($prefixed_query[4]) . '</strong>'
297
                                        );
298
                                    }
299
                                }
300
                            } else {
301
                                // the table name is reserved, so halt the installation
302
                                $this->error[]['sub'] = sprintf(
303
                                    SystemLocale::EF_TABLE_IS_RESERVED,
304
                                    '<strong>' . $prefixed_query[4] . '</strong>'
305
                                );
306
                                break;
307
                            }
308
                        }
309
                        // if there was an error, delete the tables created so far,
310
                        // so the next installation will not fail
311
                        if (count($this->error) > 0) {
312
                            foreach ($created_tables as $table) {
313
                                try {
314
                                    $xoops->db()->query('DROP TABLE ' . $xoops->db()->prefix($table));
315
                                } catch (Exception $e) {
316
                                    $xoops->events()->triggerEvent('core.exception', $e);
317
                                }
318
                            }
319
                            return false;
320
                        }
321
                    }
322
                }
323
            }
324
            // Save module info, blocks, templates and perms
325
            if (count($this->error) == 0) {
326
                if (!$module_handler->insertModule($module)) {
327
                    $this->error[] = sprintf(
328
                        XoopsLocale::EF_NOT_INSERTED_TO_DATABASE,
329
                        '<strong>' . $module->getVar('name') . '</strong>'
330
                    );
331
                    foreach ($created_tables as $ct) {
332
                        try {
333
                            $xoops->db()->query('DROP TABLE ' . $xoops->db()->prefix($ct));
334
                        } catch (Exception $e) {
335
                            $xoops->events()->triggerEvent('core.exception', $e);
336
                        }
337
                    }
338
                    $this->error[] = sprintf(XoopsLocale::EF_NOT_INSTALLED, "<strong>" . $module->name() . "</strong>");
339
                    $this->error[] = XoopsLocale::C_ERRORS;
340
                    unset($module);
341
                    unset($created_tables);
342
                    return false;
343
                }
344
                unset($created_tables);
345
                $this->trace[] = XoopsLocale::S_DATA_INSERTED . sprintf(
346
                    SystemLocale::F_MODULE_ID,
347
                    '<strong>' . $module->getVar('mid') . '</strong>'
348
                );
349
                $xoops->db()->beginTransaction();
350
                // install Templates
351
                $this->installTemplates($module);
352
353
                $xoops->templateClearModuleCache($module->getVar('mid'));
354
355
                // install blocks
356
                $this->installBlocks($module);
357
358
                // Install Configs
359
                $this->installConfigs($module);
360
361
                if ($module->getInfo('hasMain')) {
362
                    $groups = array(FixedGroups::ADMIN, FixedGroups::USERS, FixedGroups::ANONYMOUS);
363
                } else {
364
                    $groups = array(FixedGroups::ADMIN);
365
                }
366
                // retrieve all block ids for this module
367
                $block_handler = $xoops->getHandlerBlock();
368
                $blocks = $block_handler->getByModule($module->getVar('mid'), false);
369
                $this->trace[] = SystemLocale::MANAGING_PERMISSIONS;
370
                $gperm_handler = $xoops->getHandlerGroupPermission();
371
                foreach ($groups as $mygroup) {
372
                    if ($gperm_handler->checkRight('module_admin', 0, $mygroup)) {
373
                        $mperm = $gperm_handler->create();
374
                        $mperm->setVar('gperm_groupid', $mygroup);
375
                        $mperm->setVar('gperm_itemid', $module->getVar('mid'));
376
                        $mperm->setVar('gperm_name', 'module_admin');
377
                        $mperm->setVar('gperm_modid', 1);
378
                        if (!$gperm_handler->insert($mperm)) {
379
                            $this->trace[]['sub'] = '<span class="red">' . sprintf(
380
                                SystemLocale::EF_GROUP_ID_ADMIN_ACCESS_RIGHT_NOT_ADDED,
381
                                '<strong>' . $mygroup . '</strong>'
382
                            ) . '</span>';
383
                        } else {
384
                            $this->trace[]['sub'] = sprintf(
385
                                SystemLocale::SF_GROUP_ID_ADMIN_ACCESS_RIGHT_ADDED,
386
                                '<strong>' . $mygroup . '</strong>'
387
                            );
388
                        }
389
                        unset($mperm);
390
                    }
391
                    $mperm = $gperm_handler->create();
392
                    $mperm->setVar('gperm_groupid', $mygroup);
393
                    $mperm->setVar('gperm_itemid', $module->getVar('mid'));
394
                    $mperm->setVar('gperm_name', 'module_read');
395
                    $mperm->setVar('gperm_modid', 1);
396
                    if (!$gperm_handler->insert($mperm)) {
397
                        $this->trace[]['sub'] = '<span class="red">' . sprintf(
398
                            SystemLocale::EF_GROUP_ID_USER_ACCESS_RIGHT_NOT_ADDED,
399
                            '<strong>' . $mygroup . '</strong>'
400
                        ) . '</span>';
401
                    } else {
402
                        $this->trace[]['sub'] = sprintf(
403
                            SystemLocale::SF_GROUP_ID_USER_ACCESS_RIGHT_ADDED,
404
                            '<strong>' . $mygroup . '</strong>'
405
                        );
406
                    }
407
                    unset($mperm);
408
                    foreach ($blocks as $blc) {
409
                        $bperm = $gperm_handler->create();
410
                        $bperm->setVar('gperm_groupid', $mygroup);
411
                        $bperm->setVar('gperm_itemid', $blc);
412
                        $bperm->setVar('gperm_name', 'block_read');
413
                        $bperm->setVar('gperm_modid', 1);
414
                        if (!$gperm_handler->insert($bperm)) {
415
                            $this->trace[]['sub'] = '<span class="red">'
416
                            . SystemLocale::E_BLOCK_ACCESS_NOT_ADDED . ' Block ID: <strong>'
417
                            . $blc . '</strong> Group ID: <strong>' . $mygroup . '</strong></span>';
418
                        } else {
419
                            $this->trace[]['sub'] = SystemLocale::S_BLOCK_ACCESS_ADDED
420
                                . sprintf(SystemLocale::F_BLOCK_ID, "<strong>" . $blc . "</strong>")
421
                                . sprintf(SystemLocale::F_GROUP_ID, "<strong>" . $mygroup . "</strong>");
422
                        }
423
                        unset($bperm);
424
                    }
425
                }
426
                unset($blocks);
427
                unset($groups);
428
429
                // execute module specific install script if any
430
                // If pre install function is defined, execute
431
                $func = "xoops_module_install_{$mod}";
432
                if (function_exists($func)) {
433
                    $result = $func($module);
434
                    if (!$result) {
435
                        $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func);
436
                        $this->trace = array_merge($this->trace, $module->getErrors());
437
                    } else {
438
                        $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>");
439
                        $this->trace = array_merge($this->trace, $module->getMessages());
440
                    }
441
                }
442
443
                $this->trace[] = sprintf(
444
                    XoopsLocale::SF_INSTALLED,
445
                    '<strong>' . $module->getVar('name', 's') . '</strong>'
446
                );
447
                unset($blocks);
448
449
                $xoops->db()->commit();
450
451
                $xoops->events()->triggerEvent('system.module.install', $module);
452
                return $module;
453
            }
454
        } else {
455
            $this->error[] = sprintf(
456
                XoopsLocale::EF_NOT_INSTALLED,
457
                '<strong>' . $mod . '</strong>'
458
            ) . "&nbsp;" . XoopsLocale::C_ERRORS;
459
            return false;
460
        }
461
        return false;
462
    }
463
464
    /**
465
     * uninstall
466
     *
467
     * @param string $mod module dirname
468
     *
469
     * @return bool|XoopsModule false on failure, module context on success
470
     */
471
    public function uninstall($mod = '')
472
    {
473
        $xoops = Xoops::getInstance();
474
        $module_handler = $xoops->getHandlerModule();
475
        $module = $module_handler->getByDirname($mod);
476
        $xoops->templateClearModuleCache($module->getVar('mid'));
477
478
        if ($module->getVar('dirname') === 'system') {
479
            $this->error[] = sprintf(
480
                XoopsLocale::EF_NOT_UNINSTALLED,
481
                '<strong>' . $module->getVar('name') . '</strong>'
482
            ) . "&nbsp;" . XoopsLocale::C_ERRORS;
483
            $this->error[] = " - " . SystemLocale::E_SYSTEM_MODULE_CANNOT_BE_DEACTIVATED;
484
            return false;
485
        } elseif ($module->getVar('dirname') == $xoops->getConfig('startpage')) {
486
            $this->error[] = sprintf(
487
                XoopsLocale::EF_NOT_UNINSTALLED,
488
                '<strong>' . $module->getVar('name') . '</strong>'
489
            ) . "&nbsp;" . XoopsLocale::C_ERRORS;
490
            $this->error[] = " - " . SystemLocale::E_THIS_MODULE_IS_SET_AS_DEFAULT_START_PAGE;
491
            return false;
492
        } else {
493
            // Load module specific install script if any
494
            $uninstall_script = $module->getInfo('onUninstall');
495
            if ($uninstall_script && trim($uninstall_script) != '') {
0 ignored issues
show
Bug introduced by
It seems like $uninstall_script can also be of type string[]; however, parameter $str 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

495
            if ($uninstall_script && trim(/** @scrutinizer ignore-type */ $uninstall_script) != '') {
Loading history...
496
                XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($uninstall_script)));
497
            }
498
            $func = "xoops_module_pre_uninstall_{$mod}";
499
            // If pre uninstall function is defined, execute
500
            if (function_exists($func)) {
501
                $result = $func($module);
502
                if (!$result) {
503
                    $this->error[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func);
504
                    $this->error[] = sprintf(
505
                        XoopsLocale::EF_NOT_UNINSTALLED,
506
                        '<strong>' . $module->getVar('name') . '</strong>'
507
                    ) . "&nbsp;" . XoopsLocale::C_ERRORS;
508
                    $this->error = array_merge($this->error, $module->getErrors());
509
                    return false;
510
                } else {
511
                    $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>");
512
                    $this->trace = array_merge($this->trace, $module->getMessages());
513
                }
514
            }
515
516
            if (false === $module_handler->deleteModule($module)) {
517
                $this->error[] = sprintf(XoopsLocale::EF_NOT_DELETED, $module->getVar('name'));
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('name') can also be of type array; however, parameter $args of sprintf() 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

517
                $this->error[] = sprintf(XoopsLocale::EF_NOT_DELETED, /** @scrutinizer ignore-type */ $module->getVar('name'));
Loading history...
518
                return false;
519
            } else {
520
                // delete templates
521
                $this->deleteTemplates($module);
522
523
                // Delete blocks and block template files
524
                $this->deleteBlocks($module);
525
526
                // Delete tables used by this module
527
                $modtables = $module->getInfo('tables');
528
                if ($modtables != false && is_array($modtables)) {
529
                    // get a schema manager
530
                    $schemaManager = $xoops->db()->getSchemaManager();
531
                    // create schema from the current database
532
                    $toSchema = $schemaManager->createSchema();
533
534
                    $this->trace[] = SystemLocale::MANAGING_TABLES;
535
                    foreach ($modtables as $table) {
536
                        // prevent deletion of reserved core tables!
537
                        if (!in_array($table, $this->reservedTables)) {
538
                            $toSchema->dropTable($xoops->db()->prefix($table));
539
                            $this->trace[]['sub'] = sprintf(
540
                                XoopsLocale::SF_TABLE_DROPPED,
541
                                '<strong>' . $xoops->db()->prefix($table) . '</strong>'
542
                            );
543
                        } else {
544
                            $this->trace[]['sub'] = '<span class="red">' . sprintf(
545
                                XoopsLocale::EF_TABLE_DROP_NOT_ALLOWED,
546
                                '<strong>' . $xoops->db()->prefix($table) . '</strong>'
547
                            ) . '</span>';
548
                        }
549
                    }
550
                    $synchronizer = new SingleDatabaseSynchronizer($xoops->db());
551
                    $synchronizer->updateSchema($toSchema, false);
552
                }
553
554
                // delete permissions if any
555
                $gperm_handler = $xoops->getHandlerGroupPermission();
556
                if (false === $gperm_handler->deleteByModule($module->getVar('mid'))) {
557
                    $this->trace[] = '<span class="red">' . SystemLocale::E_GROUP_PERMISSIONS_NOT_DELETED . '</span>';
558
                } else {
559
                    $this->trace[] = SystemLocale::S_GROUP_PERMISSIONS_DELETED;
560
                }
561
562
                // delete module config options if any
563
                $this->deleteConfigs($module);
564
565
                // execute module specific install script if any
566
                $func = 'xoops_module_uninstall_' . $mod;
567
                if (function_exists($func)) {
568
                    $result = $func($module);
569
                    if (!$result) {
570
                        $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func);
571
                        $this->trace = array_merge($this->error, $module->getErrors());
572
                    } else {
573
                        $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>");
574
                        $this->trace = array_merge($this->trace, $module->getMessages());
575
                    }
576
                }
577
                $this->trace[] = sprintf(
578
                    XoopsLocale::SF_UNINSTALLED,
579
                    '<strong>' . $module->getVar('name') . '</strong>'
580
                );
581
                $xoops->events()->triggerEvent('system.module.uninstall', $module);
582
                return $module;
583
            }
584
        }
585
    }
586
587
    /**
588
     * update
589
     *
590
     * @param string $mod module dirname
591
     *
592
     * @return mixed boolean false if failed, XoopsModule if success
593
     */
594
    public function update($mod = '')
595
    {
596
        $xoops = Xoops::getInstance();
597
        $module_handler = $xoops->getHandlerModule();
598
        $module = $module_handler->getByDirname($mod);
599
        $xoops->templateClearModuleCache($module->getVar('mid'));
600
        // Save current version for use in the update function
601
        $prev_version = $module->getVar('version');
602
        // we don't want to change the module name set by admin
603
        $temp_name = $module->getVar('name');
604
        $module->loadInfoAsVar($module->getVar('dirname'));
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('dirname') can also be of type array; however, parameter $dirname of Xoops\Core\Kernel\Handle...Module::loadInfoAsVar() 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

604
        $module->loadInfoAsVar(/** @scrutinizer ignore-type */ $module->getVar('dirname'));
Loading history...
605
        $module->setVar('name', $temp_name);
606
        $module->setVar('last_update', time());
607
608
        if (!$module_handler->insertModule($module)) {
609
            $this->error[] = sprintf(XoopsLocale::EF_NOT_UPDATED, "<strong>" . $module->getVar('name') . "</strong>");
610
            return false;
611
        } else {
612
            // execute module specific preupdate script if any
613
            $update_script = $module->getInfo('onUpdate');
614
            if (false != $update_script && trim($update_script) != '') {
0 ignored issues
show
Bug introduced by
It seems like $update_script can also be of type string[]; however, parameter $str 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

614
            if (false != $update_script && trim(/** @scrutinizer ignore-type */ $update_script) != '') {
Loading history...
615
                XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($update_script)));
616
                $func = 'xoops_module_pre_update_' . $mod;
617
                if (function_exists($func)) {
618
                    $result = $func($module, $prev_version);
619
                    if (!$result) {
620
                        $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func);
621
                        $this->trace = array_merge($this->error, $module->getErrors());
622
                    } else {
623
                        $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>");
624
                        $this->trace = array_merge($this->trace, $module->getMessages());
625
                    }
626
                }
627
            }
628
629
            // update schema
630
            $schema_file = $module->getInfo('schema');
631
            if (!empty($schema_file)) {
632
                $schema_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $schema_file;
0 ignored issues
show
Bug introduced by
Are you sure $schema_file of type string|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

632
                $schema_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . /** @scrutinizer ignore-type */ $schema_file;
Loading history...
633
                if (!XoopsLoad::fileExists($schema_file_path)) {
634
                    $this->error[] =
635
                        sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$schema_file}</strong>");
636
                    return false;
637
                }
638
                $importer = new ImportSchema;
639
                $importSchema = $importer->importSchemaArray(Yaml::read($schema_file_path));
0 ignored issues
show
Bug introduced by
It seems like Xmf\Yaml::read($schema_file_path) can also be of type boolean; however, parameter $schemaArray of Xoops\Core\Database\Sche...ma::importSchemaArray() 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

639
                $importSchema = $importer->importSchemaArray(/** @scrutinizer ignore-type */ Yaml::read($schema_file_path));
Loading history...
640
                $synchronizer = new SingleDatabaseSynchronizer($xoops->db());
641
                $synchronizer->updateSchema($importSchema, true);
642
            }
643
644
            // delete templates
645
            $this->deleteTemplates($module);
646
647
            // install templates
648
            $this->installTemplates($module);
649
650
            // install blocks
651
            $this->installBlocks($module);
652
653
            // reset compile_id
654
            $xoops->tpl()->setCompileId();
655
656
            // first delete all config entries
657
            $this->deleteConfigs($module);
658
659
            // Install Configs
660
            $this->installConfigs($module);
661
662
            // execute module specific update script if any
663
            $update_script = $module->getInfo('onUpdate');
664
            if (false != $update_script && trim($update_script) != '') {
665
                XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($update_script)));
666
                $func = 'xoops_module_update_' . $mod;
667
                if (function_exists($func)) {
668
                    $result = $func($module, $prev_version);
669
                    if (!$result) {
670
                        $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func);
671
                        $this->trace = array_merge($this->error, $module->getErrors());
672
                    } else {
673
                        $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>");
674
                        $this->trace = array_merge($this->trace, $module->getMessages());
675
                    }
676
                }
677
            }
678
            $this->trace[] = sprintf(XoopsLocale::SF_UPDATED, '<strong>' . $module->getVar('name', 's') . '</strong>');
679
            return $module;
680
        }
681
    }
682
683
    /**
684
     * getTemplate
685
     *
686
     * @param string $dirname  module directory
687
     * @param string $template template name
688
     * @param string $type     template type - blocks, admin
689
     *
690
     * @return string
691
     */
692
    public function getTemplate($dirname, $template, $type = '')
693
    {
694
        $xoops = Xoops::getInstance();
695
        $ret = '';
696
        switch ($type) {
697
            case 'blocks':
698
            case 'admin':
699
                $path = $xoops->path('modules/' . $dirname . '/templates/' . $type . '/' . $template);
700
                break;
701
            default:
702
                $path = $xoops->path('modules/' . $dirname . '/templates/' . $template);
703
                break;
704
        }
705
        if (!XoopsLoad::fileExists($path)) {
706
            return $ret;
707
        } else {
708
            $lines = file($path);
709
        }
710
        if (!$lines) {
0 ignored issues
show
introduced by
The condition ! $lines can never be false.
Loading history...
711
            return $ret;
712
        }
713
        $count = count($lines);
714
        for ($i = 0; $i < $count; ++$i) {
715
            $ret .= str_replace("\n", "\r\n", str_replace("\r\n", "\n", $lines[$i]));
716
        }
717
        return $ret;
718
    }
719
720
    /**
721
     * installTemplates
722
     *
723
     * @param XoopsModule $module module context
724
     *
725
     * @return void
726
     */
727
    public function installTemplates(XoopsModule $module)
728
    {
729
        $xoops = Xoops::getInstance();
730
        $templates = $module->getInfo('templates');
731
        if (is_array($templates) && count($templates) > 0) {
732
            $this->trace[] = SystemLocale::MANAGING_TEMPLATES;
733
            $tplfile_handler = $xoops->getHandlerTplFile();
734
            foreach ($templates as $tpl) {
735
                $tpl['file'] = trim($tpl['file']);
736
                if (!in_array($tpl['file'], $this->template_delng)) {
737
                    $type = (isset($tpl['type']) ? $tpl['type'] : 'module');
738
                    $tpldata = $this->getTemplate($module->getVar('dirname'), $tpl['file'], $type);
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('dirname') can also be of type array; however, parameter $dirname of SystemModule::getTemplate() 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

738
                    $tpldata = $this->getTemplate(/** @scrutinizer ignore-type */ $module->getVar('dirname'), $tpl['file'], $type);
Loading history...
739
                    $tplfile = $tplfile_handler->create();
740
                    $tplfile->setVar('tpl_refid', $module->getVar('mid'));
741
                    $tplfile->setVar('tpl_lastimported', 0);
742
                    $tplfile->setVar('tpl_lastmodified', time());
743
744
                    if (preg_match("/\.css$/i", $tpl['file'])) {
745
                        $tplfile->setVar('tpl_type', 'css');
746
                    } else {
747
                        $tplfile->setVar('tpl_type', $type);
748
                    }
749
                    $tplfile->setVar('tpl_source', $tpldata);
750
                    $tplfile->setVar('tpl_module', $module->getVar('dirname'));
751
                    $tplfile->setVar('tpl_tplset', 'default');
752
                    $tplfile->setVar('tpl_file', $tpl['file']);
753
                    $tplfile->setVar('tpl_desc', $tpl['description']);
754
                    if (!$tplfile_handler->insertTpl($tplfile)) {
755
                        $this->trace[]['sub'] = '<span class="red">' . sprintf(
756
                            SystemLocale::EF_TEMPLATE_NOT_ADDED_TO_DATABASE,
757
                            '<strong>' . $tpl['file'] . '</strong>'
758
                        ) . '</span>';
759
                    } else {
760
                        $newid = $tplfile->getVar('tpl_id');
761
                        $this->trace[]['sub'] = sprintf(
762
                            SystemLocale::SF_TEMPLATE_ADDED,
763
                            '<strong>' . $tpl['file'] . '</strong>'
764
                        );
765
                        if ($module->getVar('dirname') === 'system') {
766
                            if (!$xoops->templateTouch($newid)) {
767
                                $this->trace[]['sub'] = '<span class="red">' . sprintf(
768
                                    SystemLocale::EF_TEMPLATE_NOT_RECOMPILED,
769
                                    '<strong>' . $tpl['file'] . '</strong>'
770
                                ) . '</span>';
771
                            } else {
772
                                $this->trace[]['sub'] = sprintf(
773
                                    SystemLocale::SF_TEMPLATE_RECOMPILED,
774
                                    '<strong>' . $tpl['file'] . '</strong>'
775
                                );
776
                            }
777
                        } else {
778
                            if ($xoops->config['template_set'] === 'default') {
779
                                if (!$xoops->templateTouch($newid)) {
780
                                    $this->trace[]['sub'] = '<span class="red">' . sprintf(
781
                                        SystemLocale::EF_TEMPLATE_NOT_RECOMPILED,
782
                                        '<strong>' . $tpl['file'] . '</strong>'
783
                                    ) . '</span>';
784
                                } else {
785
                                    $this->trace[]['sub'] = sprintf(
786
                                        SystemLocale::SF_TEMPLATE_RECOMPILED,
787
                                        '<strong>' . $tpl['file'] . '</strong>'
788
                                    );
789
                                }
790
                            }
791
                        }
792
                    }
793
                    unset($tpldata);
794
                } else {
795
                    $this->trace[]['sub'] = '<span class="red">' . sprintf(
796
                        SystemLocale::EF_TEMPLATE_NOT_DELETED,
797
                        '<strong>' . $tpl['file'] . '</strong>'
798
                    ) . '</span>';
799
                }
800
            }
801
        }
802
    }
803
804
    /**
805
     * deleteTemplates
806
     *
807
     * @param XoopsModule $module module context
808
     *
809
     * @return void
810
     */
811
    public function deleteTemplates(XoopsModule $module)
812
    {
813
        $xoops = Xoops::getInstance();
814
        $tplfile_handler = $xoops->getHandlerTplFile();
815
        $templates = $tplfile_handler->find('default', 'module', $module->getVar('mid'));
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('mid') can also be of type array; however, parameter $refid of Xoops\Core\Kernel\Handle...sTplFileHandler::find() does only seem to accept null|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

815
        $templates = $tplfile_handler->find('default', 'module', /** @scrutinizer ignore-type */ $module->getVar('mid'));
Loading history...
816
        if (is_array($templates) && count($templates) > 0) {
817
            $this->trace[] = SystemLocale::MANAGING_TEMPLATES;
818
            // delete template file entry in db
819
            /* @var $template XoopsTplFile */
820
            foreach ($templates as $template) {
821
                if (!$tplfile_handler->deleteTpl($template)) {
822
                    $this->template_delng[] = $template->getVar('tpl_file');
823
                }
824
            }
825
        }
826
    }
827
828
    /**
829
     * installBlocks
830
     *
831
     * @param XoopsModule $module module context
832
     *
833
     * @return void
834
     */
835
    public function installBlocks(XoopsModule $module)
836
    {
837
        $xoops = Xoops::getInstance();
838
        $blocks = $module->getInfo('blocks');
839
        $this->trace[] = SystemLocale::MANAGING_BLOCKS;
840
        $block_handler = $xoops->getHandlerBlock();
841
        $blockmodulelink_handler = $xoops->getHandlerBlockModuleLink();
842
        $tplfile_handler = $xoops->getHandlerTplFile();
843
        $showfuncs = array();
844
        $funcfiles = array();
845
        if (is_array($blocks) && count($blocks) > 0) {
846
            foreach ($blocks as $i => $block) {
847
                if (isset($block['show_func']) && $block['show_func'] != ''
848
                    && isset($block['file']) && $block['file'] != ''
849
                ) {
850
                    $showfuncs[] = $block['show_func'];
851
                    $funcfiles[] = $block['file'];
852
853
                    $criteria = new CriteriaCompo();
854
                    $criteria->add(new Criteria('mid', $module->getVar('mid')));
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('mid') can also be of type array; however, parameter $value of Xoops\Core\Kernel\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

854
                    $criteria->add(new Criteria('mid', /** @scrutinizer ignore-type */ $module->getVar('mid')));
Loading history...
855
                    $criteria->add(new Criteria('func_num', $i));
856
857
                    $block_obj = $block_handler->getObjects($criteria);
858
                    if (count($block_obj) == 0) {
859
                        $block_obj[0] = $block_handler->create();
860
                        $block_obj[0]->setVar('func_num', $i);
861
                        $block_obj[0]->setVar('mid', $module->getVar('mid'));
862
                        $block_obj[0]->setVar('name', addslashes($block['name']));
863
                        $block_obj[0]->setVar('title', addslashes($block['name']));
864
                        $block_obj[0]->setVar('side', 0);
865
                        $block_obj[0]->setVar('weight', 0);
866
                        $block_obj[0]->setVar('visible', 0);
867
                        $block_obj[0]->setVar(
868
                            'block_type',
869
                            ($module->getVar('dirname') === 'system')
870
                                ? XoopsBlock::BLOCK_TYPE_SYSTEM
871
                                : XoopsBlock::BLOCK_TYPE_MODULE
872
                        );
873
                        $block_obj[0]->setVar('isactive', 1);
874
                        $block_obj[0]->setVar('content', '');
875
                        $block_obj[0]->setVar('c_type', XoopsBlock::CUSTOM_HTML);
876
                        $block_obj[0]->setVar('dirname', $module->getVar('dirname'));
877
                        $block_obj[0]->setVar('options', isset($block['options']) ? $block['options'] : '');
878
                    }
879
                    $block_obj[0]->setVar('func_file', $block['file']);
880
                    $block_obj[0]->setVar('show_func', isset($block['show_func']) ? $block['show_func'] : '');
881
                    $block_obj[0]->setVar('edit_func', isset($block['edit_func']) ? $block['edit_func'] : '');
882
                    $template = $this->getTemplate($module->getVar('dirname'), $block['template'], 'blocks');
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('dirname') can also be of type array; however, parameter $dirname of SystemModule::getTemplate() 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

882
                    $template = $this->getTemplate(/** @scrutinizer ignore-type */ $module->getVar('dirname'), $block['template'], 'blocks');
Loading history...
883
                    $block_obj[0]->setVar('template', !empty($template) ? $block['template'] : '');
884
                    $block_obj[0]->setVar('last_modified', time());
885
886
                    if (!$block_handler->insert($block_obj[0])) {
887
                        $this->trace[]['sub'] = '<span class="red">' . sprintf(
888
                            XoopsLocale::EF_NOT_UPDATED,
889
                            $block_obj[0]->getVar('name')
890
                        ) . '</span>';
891
                    } else {
892
                        $this->trace[]['sub'] = sprintf(
893
                            SystemLocale::SF_BLOCK_UPDATED,
894
                            '<strong>' . $block_obj[0]->getVar('name')
895
                        ) . '</strong>' . sprintf(
896
                            SystemLocale::F_BLOCK_ID,
897
                            '<strong>' . $block_obj[0]->getVar('bid') . '</strong>'
898
                        );
899
900
                        if (0 == $blockmodulelink_handler->getCount(new Criteria('block_id', $block_obj[0]->getVar('bid')))) {
901
                            $blockmodulelink = $blockmodulelink_handler->create();
902
                            $blockmodulelink->setVar('block_id', $block_obj[0]->getVar('bid'));
903
                            $blockmodulelink->setVar('module_id', 0); //show on all pages
904
                            $blockmodulelink_handler->insert($blockmodulelink);
905
                        }
906
907
                        if ($template != '') {
908
                            $tplfile = $tplfile_handler->find('default', 'block', $block_obj[0]->getVar('bid'));
909
                            if (count($tplfile) == 0) {
910
                                $tplfile_new = $tplfile_handler->create();
911
                                $tplfile_new->setVar('tpl_module', $module->getVar('dirname'));
912
                                $tplfile_new->setVar('tpl_refid', $block_obj[0]->getVar('bid'));
913
                                $tplfile_new->setVar('tpl_tplset', 'default');
914
                                $tplfile_new->setVar('tpl_file', $block_obj[0]->getVar('template'));
915
                                $tplfile_new->setVar('tpl_type', 'block');
916
                            } else {
917
                                /* @var $tplfile_new XoopsTplFile */
918
                                $tplfile_new = $tplfile[0];
919
                                $tplfile_new->setVars($tplfile_new->getValues());
920
                            }
921
                            $tplfile_new->setVar('tpl_source', $template);
922
                            $tplfile_new->setVar('tpl_desc', $block['description']);
923
                            $tplfile_new->setVar('tpl_lastmodified', time());
924
                            $tplfile_new->setVar('tpl_lastimported', 0);
925
                            if (!$tplfile_handler->insertTpl($tplfile_new)) {
926
                                $this->trace[]['sub'] = '<span class="red">' . sprintf(
927
                                    SystemLocale::EF_TEMPLATE_NOT_UPDATED,
928
                                    '<strong>' . $block['template'] . '</strong>'
929
                                ) . '</span>';
930
                            } else {
931
                                $this->trace[]['sub'] = sprintf(
932
                                    SystemLocale::SF_TEMPLATE_UPDATED,
933
                                    '<strong>' . $block['template'] . '</strong>'
934
                                );
935
                                if ($module->getVar('dirname') === 'system') {
936
                                    if (!$xoops->templateTouch($tplfile_new->getVar('tpl_id'))) {
0 ignored issues
show
Bug introduced by
It seems like $tplfile_new->getVar('tpl_id') can also be of type array; however, parameter $tpl_id of Xoops::templateTouch() 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

936
                                    if (!$xoops->templateTouch(/** @scrutinizer ignore-type */ $tplfile_new->getVar('tpl_id'))) {
Loading history...
937
                                        $this->trace[]['sub'] = '<span class="red">' . sprintf(
938
                                            SystemLocale::EF_TEMPLATE_NOT_RECOMPILED,
939
                                            '<strong>' . $block['template'] . '</strong>'
940
                                        ) . '</span>';
941
                                    } else {
942
                                        $this->trace[]['sub'] = sprintf(
943
                                            SystemLocale::SF_TEMPLATE_RECOMPILED,
944
                                            '<strong>' . $block['template'] . '</strong>'
945
                                        );
946
                                    }
947
                                } else {
948
                                    if ($xoops->config['template_set'] === 'default') {
949
                                        if (!$xoops->templateTouch($tplfile_new->getVar('tpl_id'))) {
950
                                            $this->trace[]['sub'] = '<span class="red">' . sprintf(
951
                                                SystemLocale::EF_TEMPLATE_NOT_RECOMPILED,
952
                                                '<strong>' . $block['template'] . '</strong>'
953
                                            ) . '</span>';
954
                                        } else {
955
                                            $this->trace[]['sub'] = sprintf(
956
                                                SystemLocale::SF_TEMPLATE_RECOMPILED,
957
                                                '<strong>' . $block['template'] . '</strong>'
958
                                            );
959
                                        }
960
                                    }
961
                                }
962
                            }
963
                        }
964
                    }
965
                }
966
            }
967
        }
968
        $blocks = $block_handler->getByModule($module->getVar('mid'));
969
        foreach ($blocks as $block) {
970
            /* @var $block XoopsBlock */
971
            if (!in_array($block->getVar('show_func'), $showfuncs)
972
                || !in_array($block->getVar('func_file'), $funcfiles)
973
            ) {
974
                if (!$block_handler->delete($block)) {
975
                    $this->trace[]['sub'] = '<span class="red">' . sprintf(
976
                        SystemLocale::EF_BLOCK_NOT_DELETED,
977
                        "<strong>" . $block->getVar('name') . "</strong>"
978
                    ) . sprintf(
979
                        SystemLocale::F_BLOCK_ID,
980
                        "<strong>" . $block->getVar('bid') . "</strong>"
981
                    ) . '</span>';
982
                } else {
983
                    $this->trace[]['sub'] = sprintf(
984
                        SystemLocale::SF_BLOCK_DELETED,
985
                        '<strong>' . $block->getVar('name') . '</strong>'
986
                    ) . '&nbsp;' . sprintf(
987
                        SystemLocale::F_BLOCK_ID,
988
                        '<strong>' . $block->getVar('bid') . '</strong>'
989
                    );
990
                    if ($block->getVar('template') != '') {
991
                        $tplfiles = $tplfile_handler->find(null, 'block', $block->getVar('bid'));
0 ignored issues
show
Bug introduced by
It seems like $block->getVar('bid') can also be of type array; however, parameter $refid of Xoops\Core\Kernel\Handle...sTplFileHandler::find() does only seem to accept null|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

991
                        $tplfiles = $tplfile_handler->find(null, 'block', /** @scrutinizer ignore-type */ $block->getVar('bid'));
Loading history...
992
                        if (is_array($tplfiles)) {
993
                            /* @var $tplfile XoopsTplFile */
994
                            foreach ($tplfiles as $tplfile) {
995
                                if (!$tplfile_handler->deleteTpl($tplfile)) {
996
                                    $this->trace[]['sub'] = '<span class="red">'
997
                                        . SystemLocale::E_BLOCK_TEMPLATE_DEPRECATED_NOT_REMOVED
998
                                        . '(ID: <strong>' . $tplfile->getVar('tpl_id') . '</strong>)</span>';
999
                                } else {
1000
                                    $this->trace[]['sub'] = sprintf(
1001
                                        SystemLocale::SF_BLOCK_TEMPLATE_DEPRECATED,
1002
                                        "<strong>" . $tplfile->getVar('tpl_file') . "</strong>"
1003
                                    );
1004
                                }
1005
                            }
1006
                        }
1007
                    }
1008
                }
1009
            }
1010
        }
1011
    }
1012
1013
    /**
1014
     * deleteBlocks
1015
     *
1016
     * @param XoopsModule $module module
1017
     *
1018
     * @return void
1019
     */
1020
    public function deleteBlocks(XoopsModule $module)
1021
    {
1022
        $xoops = Xoops::getInstance();
1023
        $block_handler = $xoops->getHandlerBlock();
1024
        $blocks = $block_handler->getByModule($module->getVar('mid'));
1025
        if (is_array($blocks) && count($blocks) > 0) {
1026
            $tplfile_handler = $xoops->getHandlerTplFile();
1027
            $this->trace[] = SystemLocale::MANAGING_BLOCKS;
1028
            /* @var $block XoopsBlock */
1029
            foreach ($blocks as $block) {
1030
                if (false === $block_handler->deleteBlock($block)) {
1031
                    $this->trace[]['sub'] = '<span class="red">' . sprintf(
1032
                        SystemLocale::EF_BLOCK_NOT_DELETED,
1033
                        "<strong>" . $block->getVar('name') . "</strong>"
1034
                    ) . sprintf(
1035
                        SystemLocale::F_BLOCK_ID,
1036
                        "<strong>" . $block->getVar('bid') . "</strong>"
1037
                    ) . '</span>';
1038
                } else {
1039
                    $this->trace[]['sub'] = sprintf(
1040
                        SystemLocale::SF_BLOCK_DELETED,
1041
                        "<strong>" . $block->getVar('name') . "</strong>"
1042
                    ) . sprintf(
1043
                        SystemLocale::F_BLOCK_ID,
1044
                        "<strong>" . $block->getVar('bid') . "</strong>"
1045
                    );
1046
                }
1047
                if ($block->getVar('template') != '') {
1048
                    $templates = $tplfile_handler->find(null, 'block', $block->getVar('bid'));
1049
                    /* @var $template XoopsTplFile */
1050
                    foreach ($templates as $template) {
1051
                        if (!$tplfile_handler->delete($template)) {
1052
                            $this->trace[]['sub'] = '<span class="red">' . sprintf(
1053
                                SystemLocale::EF_BLOCK_TEMPLATE_NOT_DELETED,
1054
                                $template->getVar('tpl_file')
1055
                            ) . sprintf(
1056
                                SystemLocale::F_TEMPLATE_ID,
1057
                                "<strong>" . $template->getVar('tpl_id') . "</strong>"
1058
                            ) . '</span>';
1059
                        } else {
1060
                            $this->trace[]['sub'] = sprintf(
1061
                                SystemLocale::SF_BLOCK_TEMPLATE_DELETED,
1062
                                "<strong>" . $template->getVar('tpl_file') . "</strong>"
1063
                            ) . sprintf(
1064
                                SystemLocale::F_TEMPLATE_ID,
1065
                                "<strong>" . $template->getVar('tpl_id') . "</strong>"
1066
                            );
1067
                        }
1068
                    }
1069
                    unset($templates);
1070
                }
1071
            }
1072
        }
1073
    }
1074
1075
    /**
1076
     * deleteConfigs
1077
     *
1078
     * @param XoopsModule $module module
1079
     *
1080
     * @return void
1081
     */
1082
    public function deleteConfigs(XoopsModule $module)
1083
    {
1084
        $xoops = Xoops::getInstance();
1085
1086
        $config_handler = $xoops->getHandlerConfig();
1087
        $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; however, parameter $value of Xoops\Core\Kernel\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

1087
        $configs = $config_handler->getConfigs(new Criteria('conf_modid', /** @scrutinizer ignore-type */ $module->getVar('mid')));
Loading history...
1088
        if (is_array($configs) && count($configs) > 0) {
1089
            $this->trace[] = SystemLocale::MANAGING_PREFERENCES;
1090
            /* @var $config XoopsConfigItem */
1091
            foreach ($configs as $config) {
1092
                if (!$config_handler->deleteConfig($config)) {
1093
                    $this->trace[]['sub'] = '<span class="red">'
1094
                        . SystemLocale::E_CONFIG_DATA_NOT_DELETED
1095
                        . sprintf(SystemLocale::F_CONFIG_ID, "<strong>" . $config->getVar('conf_id') . "</strong>")
1096
                        . '</span>';
1097
                    // save the name of config failed to delete for later use
1098
                    $this->config_delng[] = $config->getVar('conf_name');
1099
                } else {
1100
                    $this->config_old[$config->getVar('conf_name')]['value'] = $config->getVar('conf_value', 'N');
1101
                    $this->config_old[$config->getVar('conf_name')]['formtype'] = $config->getVar('conf_formtype');
1102
                    $this->config_old[$config->getVar('conf_name')]['valuetype'] = $config->getVar('conf_valuetype');
1103
                    $this->trace[]['sub'] = SystemLocale::S_CONFIG_DATA_DELETED
1104
                        . sprintf(SystemLocale::F_CONFIG_ID, "<strong>" . $config->getVar('conf_id') . "</strong>");
1105
                }
1106
            }
1107
        }
1108
    }
1109
1110
    /**
1111
     * installconfigs
1112
     *
1113
     * @param XoopsModule $module module being installed
1114
     *
1115
     * @return void
1116
     */
1117
    public function installConfigs(XoopsModule $module)
1118
    {
1119
        $xoops = Xoops::getInstance();
1120
        // now reinsert them with the new settings
1121
        $configs = $module->getInfo('config');
1122
        if (!is_array($configs)) {
1123
            $configs = array();
1124
        }
1125
1126
        $collector = new ConfigCollector($module, $configs);
1127
        $xoops->events()->triggerEvent('system.module.update.configs', $collector);
1128
1129
        if (is_array($configs) && count($configs) > 0) {
1130
            $this->trace[] = SystemLocale::MANAGING_PREFERENCES;
1131
            $config_handler = $xoops->getHandlerConfig();
1132
            $order = 0;
1133
            foreach ($configs as $config) {
1134
                // only insert ones that have been deleted previously with success
1135
                if (!in_array($config['name'], $this->config_delng)) {
1136
                    $confobj = $config_handler->createConfig();
1137
                    $confobj->setVar('conf_modid', $module->getVar('mid'));
1138
                    $confobj->setVar('conf_catid', 0);
1139
                    $confobj->setVar('conf_name', $config['name']);
1140
                    $confobj->setVar('conf_title', $config['title']);
1141
                    $confobj->setVar('conf_desc', $config['description']);
1142
                    $confobj->setVar('conf_formtype', $config['formtype']);
1143
                    $confobj->setVar('conf_valuetype', $config['valuetype']);
1144
                    if (isset($this->config_old[$config['name']]['value'])
1145
                        && $this->config_old[$config['name']]['formtype'] == $config['formtype']
1146
                        && $this->config_old[$config['name']]['valuetype'] == $config['valuetype']
1147
                    ) {
1148
                        // preserver the old value if any
1149
                        // form type and value type must be the same
1150
                        $confobj->setVar('conf_value', $this->config_old[$config['name']]['value']);
1151
                    } else {
1152
                        $confobj->setConfValueForInput($config['default']);
1153
                        //$confobj->setVar('conf_value', $config['default']);
1154
                    }
1155
                    $confobj->setVar('conf_order', $order);
1156
                    $confop_msgs = '';
1157
                    if (isset($config['options']) && is_array($config['options'])) {
1158
                        foreach ($config['options'] as $key => $value) {
1159
                            $confop = $config_handler->createConfigOption();
1160
                            $confop->setVar('confop_name', $key);
1161
                            $confop->setVar('confop_value', $value);
1162
                            $confobj->setConfOptions($confop);
1163
                            $confop_msgs .= '<br />&nbsp;&nbsp;&nbsp;&nbsp;';
1164
                            $confop_msgs .= SystemLocale::S_CONFIG_OPTION_ADDED;
1165
                            $confop_msgs .= '&nbsp;';
1166
                            $confop_msgs .= XoopsLocale::C_NAME;
1167
                            $confop_msgs .= ' <strong>'
1168
                                . \Xoops\Locale::translate($key, $module->getVar('dirname'))
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('dirname') can also be of type array; however, parameter $dirname of Xoops\Locale::translate() 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

1168
                                . \Xoops\Locale::translate($key, /** @scrutinizer ignore-type */ $module->getVar('dirname'))
Loading history...
1169
                                . '</strong> ';
1170
                            $confop_msgs .= XoopsLocale::C_VALUE . ' <strong>' . $value . '</strong> ';
1171
                            unset($confop);
1172
                        }
1173
                    }
1174
                    ++$order;
1175
                    if (false != $config_handler->insertConfig($confobj)) {
1176
                        $this->trace[]['sub'] = sprintf(
1177
                            SystemLocale::SF_CONFIG_ADDED,
1178
                            "<strong>" . $config['name'] . "</strong>"
1179
                        ) . $confop_msgs;
1180
                    } else {
1181
                        $this->trace[]['sub'] = '<span class="red">'
1182
                            . sprintf(SystemLocale::EF_CONFIG_NOT_ADDED, "<strong>" . $config['name'] . "</strong>")
1183
                            . '</span>';
1184
                    }
1185
                    unset($confobj);
1186
                }
1187
            }
1188
            unset($configs);
1189
        }
1190
    }
1191
}
1192