Passed
Push — master ( 8d8e58...047d50 )
by Michael
02:21
created

onupdate.php ➔ xoops_module_update_extgallery()   F

Complexity

Conditions 33
Paths 15600

Size

Total Lines 168
Code Lines 94

Duplication

Lines 60
Ratio 35.71 %

Importance

Changes 0
Metric Value
cc 33
eloc 94
nc 15600
nop 2
dl 60
loc 168
rs 2
c 0
b 0
f 0

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @author       XOOPS Development Team
17
 */
18
19
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
0 ignored issues
show
Bug introduced by
The type XoopsUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
    || !$GLOBALS['xoopsUser']->IsAdmin()) {
21
    exit('Restricted access' . PHP_EOL);
22
}
23
24
/**
25
 * @param string $tablename
26
 *
27
 * @return bool
28
 */
29
function tableExists($tablename)
30
{
31
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
32
33
    return $GLOBALS['xoopsDB']->getRowsNum($result) > 0;
34
}
35
36
/**
37
 *
38
 * Prepares system prior to attempting to install module
39
 * @param XoopsModule $module {@link XoopsModule}
40
 *
41
 * @return bool true if ready to install, false if not
42
 */
43
function xoops_module_pre_update_extgallery(\XoopsModule $module)
44
{
45
    /** @var Extgallery\Helper $helper */
46
    /** @var Extgallery\Utility $utility */
47
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
48
    $helper       = Extgallery\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
Bug introduced by
The type Extgallery\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
    $utility      = new Extgallery\Utility();
0 ignored issues
show
Bug introduced by
The type Extgallery\Utility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
51
    $xoopsSuccess = $utility::checkVerXoops($module);
52
    $phpSuccess   = $utility::checkVerPhp($module);
53
    return $xoopsSuccess && $phpSuccess;
54
}
55
56
/**
57
 *
58
 * Performs tasks required during update of the module
59
 * @param XoopsModule $module {@link XoopsModule}
60
 * @param null        $previousVersion
61
 *
62
 * @return bool true if update successful, false if not
63
 */
64
65
use XoopsModules\Extgallery;
66
67
/**
68
 * @param \XoopsModule $module
69
 * @param null         $previousVersion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $previousVersion is correct as it would always require null to be passed?
Loading history...
70
 * @return bool
71
 */
72
function xoops_module_update_extgallery(\XoopsModule $module, $previousVersion = null)
73
{
74
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
75
76
    $moduleDirName = basename(dirname(__DIR__));
77
    $capsDirName   = strtoupper($moduleDirName);
78
79
    /** @var Extgallery\Helper $helper */
80
    /** @var Extgallery\Utility $utility */
81
    /** @var Extgallery\Common\Configurator $configurator */
82
    $helper  = Extgallery\Helper::getInstance();
83
    $utility = new Extgallery\Utility();
84
    $configurator = new Extgallery\Common\Configurator();
85
86
    $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
87
    $catHandler->rebuild();
88
89
    if ($previousVersion < 101) {
90
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug introduced by
The type XoopsDatabaseFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
        // Remove the UNIQUE key on the rating table. This constraint is software cheked now
92
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicrating') . '` DROP INDEX `photo_rate` ;';
93
        $db->query($sql);
94
    }
95
96
    if ($previousVersion < 102) {
97
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
98
99
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publiccat') . '` ADD `cat_imgurl` VARCHAR(150) NOT NULL AFTER `cat_nb_photo` ;';
100
        $db->query($sql);
101
102
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` ADD `photo_title` VARCHAR(150) NOT NULL AFTER `photo_id` ;';
103
        $db->query($sql);
104
105
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` ADD `photo_weight` INT(11) NOT NULL AFTER `photo_extra` ;';
106
        $db->query($sql);
107
    }
108
109
    if ($previousVersion < 104) {
110
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
111
112
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . "` ADD `dohtml` BOOL NOT NULL DEFAULT '0';";
113
        $db->query($sql);
114
115
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` CHANGE `photo_desc` `photo_desc` TEXT;';
116
        $db->query($sql);
117
118
        // Set display parmission for all XOOPS base Groups
119
        $sql       = 'SELECT cat_id FROM `' . $db->prefix($moduleDirName . '_publiccat') . '`;';
120
        $result    = $db->query($sql);
121
        $module_id = $xoopsModule->getVar('mid');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $xoopsModule seems to be never defined.
Loading history...
122
        /** @var XoopsGroupPermHandler $gpermHandler */
123
        $gpermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

123
        $gpermHandler = /** @scrutinizer ignore-call */ xoops_getHandler('groupperm');
Loading history...
124
        while (false !== ($cat = $db->fetchArray($result))) {
125
            $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_ADMIN, $module_id);
0 ignored issues
show
Bug introduced by
The constant XOOPS_GROUP_ADMIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
126
            $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_USERS, $module_id);
0 ignored issues
show
Bug introduced by
The constant XOOPS_GROUP_USERS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
127
            $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_ANONYMOUS, $module_id);
0 ignored issues
show
Bug introduced by
The constant XOOPS_GROUP_ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
        }
129
    }
130
131
    if ($previousVersion < 106) {
132
        if (!file_exists(XOOPS_ROOT_PATH . '/uploads/extgallery/index.html')) {
0 ignored issues
show
Bug introduced by
The constant XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
133
            $indexFile = XOOPS_ROOT_PATH . '/modules/extgallery/include/index.html';
134
            copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/index.html');
135
        }
136
137
        if (!file_exists(XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/index.html')) {
138
            $indexFile = XOOPS_ROOT_PATH . '/modules/extgallery/include/index.html';
139
            copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/index.html');
140
        }
141
    }
142
143
    if ($previousVersion < 107) {
144
145
        // Fix extension Bug if it's installed
146
        if (file_exists(XOOPS_ROOT_PATH . '/class/textsanitizer/gallery/gallery.php')) {
147
            $conf                          = include XOOPS_ROOT_PATH . '/class/textsanitizer/config.php';
148
            $conf['extensions']['gallery'] = 1;
149
            file_put_contents(XOOPS_ROOT_PATH . '/class/textsanitizer/config.custom.php', "<?php\rreturn \$config = " . var_export($conf, true) . "\r?>");
150
        }
151
    }
152
153
    if ($previousVersion < 109) {
154
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
155
156
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publiccat') . "` CHANGE `cat_weight` `cat_weight` INT( 11 ) NOT NULL DEFAULT '0' ;";
157
        $db->query($sql);
158
    }
159
160
    if ($previousVersion < 114) {
161
        // delete old HTML template files ============================
162
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $moduleDirName . '/templates/');
163
        if (is_dir($templateDirectory)) {
164
            $templateList = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
165
            foreach ($templateList as $k => $v) {
166
                $fileInfo = new \SplFileInfo($templateDirectory . $v);
167
                if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
168
                    if (file_exists($templateDirectory . $v)) {
169
                        unlink($templateDirectory . $v);
170
                    }
171
                }
172
            }
173
        }
174
        // delete old block html template files ============================
175
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $moduleDirName . '/templates/blocks/');
176
        if (is_dir($templateDirectory)) {
177
            $templateList = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
178
            foreach ($templateList as $k => $v) {
179
                $fileInfo = new \SplFileInfo($templateDirectory . $v);
180
                if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
181
                    if (file_exists($templateDirectory . $v)) {
182
                        unlink($templateDirectory . $v);
183
                    }
184
                }
185
            }
186
        }
187
188
        // delete old admin html template files ============================
189
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/admin/');
190
        if (is_dir($templateDirectory)) {
191
            $templateList = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
192
            foreach ($templateList as $k => $v) {
193
                $fileInfo = new \SplFileInfo($templateDirectory . $v);
194
                if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
195
                    if (file_exists($templateDirectory . $v)) {
196
                        unlink($templateDirectory . $v);
197
                    }
198
                }
199
            }
200
        }
201
202
        $configurator = include __DIR__ . '/config.php';
203
        /** @var Extgallery\Utility $utility */
204
        $utility = new Extgallery\Utility();
205
206
        //  ---  COPY blank.png FILES ---------------
207
        if (count($configurator->copyBlankFiles) > 0) {
208
            $file = __DIR__ . '/../assets/images/blank.png';
209
            foreach (array_keys($configurator->copyFiles) as $i) {
210
                $dest = $configurator->copyFiles[$i] . '/blank.png';
211
                $utility::copyFile($file, $dest);
212
            }
213
        }
214
215
        //  ---  DELETE OLD FILES ---------------
216
        if (count($configurator->oldFiles) > 0) {
217
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
218
            foreach (array_keys($configurator->oldFiles) as $i) {
219
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
220
                if (is_file($tempFile)) {
221
                    unlink($tempFile);
222
                }
223
            }
224
        }
225
226
        //---------------------
227
228
        //delete .html entries from the tpl table
229
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
230
        $xoopsDB->queryF($sql);
231
232
        // Load class XoopsFile ====================
233
        xoops_load('XoopsFile');
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

233
        /** @scrutinizer ignore-call */ 
234
        xoops_load('XoopsFile');
Loading history...
234
235
        //delete /images directory ============
236
        $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
237
        $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
0 ignored issues
show
Bug introduced by
The type XoopsFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
238
        $folderHandler->delete($imagesDirectory);
239
    }
240
241
    $gpermHandler = xoops_getHandler('groupperm');
242
243
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
244
}
245