Completed
Branch master (48ec27)
by Michael
03:44
created

onupdate.php ➔ xoops_module_update_extgallery()   F

Complexity

Conditions 30
Paths 4080

Size

Total Lines 161
Code Lines 89

Duplication

Lines 53
Ratio 32.92 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 30
eloc 89
c 1
b 0
f 0
nc 4080
nop 2
dl 53
loc 161
rs 2

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 30 and the first side effect is on line 22.

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 http://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 class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
20
    || !$GLOBALS['xoopsUser']->IsAdmin()
21
) {
22
    exit('Restricted access' . PHP_EOL);
23
}
24
25
/**
26
 * @param string $tablename
27
 *
28
 * @return bool
29
 */
30
function tableExists($tablename)
0 ignored issues
show
Coding Style introduced by
tableExists uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
31
{
32
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
33
34
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0) ? true : false;
35
}
36
37
/**
38
 *
39
 * Prepares system prior to attempting to install module
40
 * @param XoopsModule $module {@link XoopsModule}
41
 *
42
 * @return bool true if ready to install, false if not
43
 */
44
function xoops_module_pre_update_extgallery(XoopsModule $module)
45
{
46
    $moduleDirName = basename(dirname(__DIR__));
47
    $classUtilities = ucfirst($moduleDirName) . 'Utilities';
48
    if (!class_exists($classUtilities)) {
49
        xoops_load('utilities', $moduleDirName);
50
    }
51
    //check for minimum XOOPS version
52
    if (!$classUtilities::checkXoopsVer($module)) {
53
        return false;
54
    }
55
56
    // check for minimum PHP version
57
    if (!$classUtilities::checkPHPVer($module)) {
58
        return false;
59
    }
60
    return true;
61
}
62
63
/**
64
 *
65
 * Performs tasks required during update of the module
66
 * @param XoopsModule $module {@link XoopsModule}
67
 * @param null        $previousVersion
68
 *
69
 * @return bool true if update successful, false if not
70
 */
71
72
function xoops_module_update_extgallery(XoopsModule $module, $previousVersion = null)
0 ignored issues
show
Coding Style introduced by
xoops_module_update_extgallery uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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
78
    $catHandler = xoops_getModuleHandler('publiccat', $moduleDirName);
79
    $catHandler->rebuild();
80
81 View Code Duplication
    if ($previousVersion < 101) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
        $db = XoopsDatabaseFactory::getDatabaseConnection();
83
        // Remove the UNIQUE key on the rating table. This constraint is software cheked now
84
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicrating') . '` DROP INDEX `photo_rate` ;';
85
        $db->query($sql);
86
    }
87
88
    if ($previousVersion < 102) {
89
        $db = XoopsDatabaseFactory::getDatabaseConnection();
90
91
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publiccat') . '` ADD `cat_imgurl` VARCHAR(150) NOT NULL AFTER `cat_nb_photo` ;';
92
        $db->query($sql);
93
94
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` ADD `photo_title` VARCHAR(150) NOT NULL AFTER `photo_id` ;';
95
        $db->query($sql);
96
97
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` ADD `photo_weight` int(11) NOT NULL AFTER `photo_extra` ;';
98
        $db->query($sql);
99
    }
100
101
    if ($previousVersion < 104) {
102
        $db = XoopsDatabaseFactory::getDatabaseConnection();
103
104
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . "` ADD `dohtml` BOOL NOT NULL DEFAULT '0';";
105
        $db->query($sql);
106
107
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` CHANGE `photo_desc` `photo_desc` TEXT;';
108
        $db->query($sql);
109
110
        // Set display parmission for all XOOPS base Groups
111
        $sql       = 'SELECT cat_id FROM `' . $db->prefix($moduleDirName . '_publiccat') . '`;';
112
        $result    = $db->query($sql);
113
        $module_id = $xoopsModule->getVar('mid');
0 ignored issues
show
Bug introduced by
The variable $xoopsModule does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
114
        /** @var XoopsGroupPermHandler $gpermHandler */
115
        $gpermHandler = xoops_getHandler('groupperm');
116
        while ($cat = $db->fetchArray($result)) {
117
            $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_ADMIN, $module_id);
118
            $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_USERS, $module_id);
119
            $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_ANONYMOUS, $module_id);
120
        }
121
    }
122
123
    if ($previousVersion < 106) {
124 View Code Duplication
        if (!file_exists(XOOPS_ROOT_PATH . '/uploads/extgallery/index.html')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            $indexFile = XOOPS_ROOT_PATH . '/modules/extgallery/include/index.html';
126
            copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/index.html');
127
        }
128
129 View Code Duplication
        if (!file_exists(XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/index.html')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
            $indexFile = XOOPS_ROOT_PATH . '/modules/extgallery/include/index.html';
131
            copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/index.html');
132
        }
133
    }
134
135
    if ($previousVersion < 107) {
136
137
        // Fix extension Bug if it's installed
138
        if (file_exists(XOOPS_ROOT_PATH . '/class/textsanitizer/gallery/gallery.php')) {
139
            $conf                          = include XOOPS_ROOT_PATH . '/class/textsanitizer/config.php';
140
            $conf['extensions']['gallery'] = 1;
141
            file_put_contents(XOOPS_ROOT_PATH . '/class/textsanitizer/config.custom.php', "<?php\rreturn \$config = " . var_export($conf, true) . "\r?>");
142
        }
143
    }
144
145 View Code Duplication
    if ($previousVersion < 109) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
        $db = XoopsDatabaseFactory::getDatabaseConnection();
147
148
        $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publiccat') . "` CHANGE `cat_weight` `cat_weight` INT( 11 ) NOT NULL DEFAULT '0' ;";
149
        $db->query($sql);
150
    }
151
    
152
    
153
    
154
    
155
    if ($previousVersion < 111) {
156
        // delete old HTML template files ============================
157
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/');
158 View Code Duplication
        if (is_dir($templateDirectory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
            $templateList = array_diff(scandir($templateDirectory), array('..', '.'));
160
            foreach ($templateList as $k => $v) {
161
                $fileInfo = new SplFileInfo($templateDirectory . $v);
162
                if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
163
                    if (file_exists($templateDirectory . $v)) {
164
                        unlink($templateDirectory . $v);
165
                    }
166
                }
167
            }
168
        }
169
        // delete old block html template files ============================
170
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n')
171
                                                     . '/templates/blocks/');
172 View Code Duplication
        if (is_dir($templateDirectory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
            $templateList = array_diff(scandir($templateDirectory), array('..', '.'));
174
            foreach ($templateList as $k => $v) {
175
                $fileInfo = new SplFileInfo($templateDirectory . $v);
176
                if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
177
                    if (file_exists($templateDirectory . $v)) {
178
                        unlink($templateDirectory . $v);
179
                    }
180
                }
181
            }
182
        }
183
184
185
        // delete old admin html template files ============================
186
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n')
187
                                                     . '/templates/admin/');
188 View Code Duplication
        if (is_dir($templateDirectory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
            $templateList = array_diff(scandir($templateDirectory), array('..', '.'));
190
            foreach ($templateList as $k => $v) {
191
                $fileInfo = new SplFileInfo($templateDirectory . $v);
192
                if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
193
                    if (file_exists($templateDirectory . $v)) {
194
                        unlink($templateDirectory . $v);
195
                    }
196
                }
197
            }
198
        }
199
200
        //delete old files: ===================
201
//        $oldFiles = array(
202
//            '/include/update_functions.php',
203
//            '/include/install_functions.php'
204
//        );
205
        include_once __DIR__ . '/config.php';
206
        if (count($oldFiles) > 0) {
207
            foreach (array_keys($oldFiles) as $file) {
208
                if (is_file($file)) {
209
                    unlink($GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . $oldFiles[$file]));
0 ignored issues
show
Bug introduced by
The variable $oldFiles does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
210
                }
211
            }
212
        }
213
214
        //delete .html entries from the tpl table
215
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname',
216
                                                                                                          'n')
217
               . "' AND `tpl_file` LIKE '%.html%'";
218
        $xoopsDB->queryF($sql);
219
220
        // Load class XoopsFile ====================
221
        xoops_load('XoopsFile');
222
223
        //delete /images directory ============
224
        $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
225
        $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
226
        $folderHandler->delete($imagesDirectory);
227
    }
228
229
    $gpermHandler = xoops_getHandler('groupperm');
230
231
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
232
}
233