update_tdmdownloads_v200()   C
last analyzed

Complexity

Conditions 13
Paths 2

Size

Total Lines 47
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 13
eloc 39
nc 2
nop 1
dl 0
loc 47
rs 6.6166
c 2
b 1
f 0

How to fix   Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
use Xmf\Database\Tables;
6
use XoopsModules\Tdmdownloads\{
7
    Common,
8
    Common\Configurator,
9
    Common\Migrate,
10
    Helper,
11
    Utility
12
};
13
14
/** @var Helper $helper */
15
/** @var Utility $utility */
16
/** @var Configurator $configurator */
17
/** @var Migrate $migrator */
18
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
19
    || !$GLOBALS['xoopsUser']->isAdmin()) {
20
    exit('Restricted access' . PHP_EOL);
21
}
22
/**
23
 * TDMDownload
24
 *
25
 * You may not change or alter any portion of this comment or credits
26
 * of supporting developers from this source code or any supporting source code
27
 * which is considered copyrighted (c) material of the original comment or credit authors.
28
 * This program is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
 *
32
 * @param      $module
33
 * @param null $prev_version
34
 * @return bool|null
35
 * @copyright   Gregory Mage (Aka Mage)
36
 * @license     GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
37
 * @author      Gregory Mage (Aka Mage)
38
 */
39
/**
40
 * Prepares system prior to attempting to install module
41
 * @param \XoopsModule $module {@link XoopsModule}
42
 * @return bool true if ready to install, false if not
43
 */
44
function xoops_module_pre_update_tdmdownloads(\XoopsModule $module)
45
{
46
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
47
    $helper        = Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
48
    $utility       = new Utility();
49
    $xoopsSuccess  = $utility::checkVerXoops($module);
50
    $phpSuccess    = $utility::checkVerPhp($module);
51
    $configurator = new Configurator();
52
    //create upload folders
53
    $uploadFolders = $configurator->uploadFolders;
54
    foreach ($uploadFolders as $value) {
55
        $utility::prepareFolder($value);
56
    }
57
    $migrator = new Migrate();
58
    $migrator->synchronizeSchema();
59
    return $xoopsSuccess && $phpSuccess;
60
}
61
62
function xoops_module_update_tdmdownloads(&$module, $prev_version = null)
63
{
64
    $ret                = null;
65
    $moduleDirName      = \basename(\dirname(__DIR__));
66
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
67
    $helper             = Helper::getInstance();
68
    $utility            = new Utility();
0 ignored issues
show
Unused Code introduced by
The assignment to $utility is dead and can be removed.
Loading history...
69
    $configurator       = new Configurator();
0 ignored issues
show
Unused Code introduced by
The assignment to $configurator is dead and can be removed.
Loading history...
70
    $helper->loadLanguage('common');
71
    if ($prev_version < 163) {
72
        $ret = update_tdmdownloads_v163($module);
0 ignored issues
show
Unused Code introduced by
The call to update_tdmdownloads_v163() has too many arguments starting with $module. ( Ignorable by Annotation )

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

72
        $ret = /** @scrutinizer ignore-call */ update_tdmdownloads_v163($module);

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

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

Loading history...
73
    }
74
    if ($prev_version < 167) {
75
        $ret = update_tdmdownloads_v167($module);
76
    }
77
    if ($prev_version < 200) {
78
        $ret = update_tdmdownloads_v200($module);
79
    }
80
    if ($prev_version < 201) {
81
        $ret = update_tdmdownloads_v201($module);
82
    }
83
    $errors = $module->getErrors();
84
    if (!empty($errors)) {
85
        //        print_r($errors);
86
    }
87
    return $ret;
88
}
89
90
/**
91
 * @param $module
92
 * @return bool
93
 */
94
function update_tdmdownloads_v201($module)
95
{
96
    $moduleDirName      = \basename(\dirname(__DIR__));
97
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
98
    $helper             = Helper::getInstance();
99
    $utility            = new Utility();
100
    $configurator       = new Configurator();
101
    $helper->loadLanguage('common');
102
    //delete old HTML templates
103
    if (count($configurator->templateFolders) > 0) {
104
        foreach ($configurator->templateFolders as $folder) {
105
            $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
106
            if (is_dir($templateFolder)) {
107
                $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
108
                foreach ($templateList as $k => $v) {
109
                    $fileInfo = new SplFileInfo($templateFolder . $v);
110
                    if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
111
                        if (is_file($templateFolder . $v)) {
112
                            unlink($templateFolder . $v);
113
                        }
114
                    }
115
                }
116
            }
117
        }
118
    }
119
    //  ---  DELETE OLD FILES ---------------
120
    if (count($configurator->oldFiles) > 0) {
121
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
122
        foreach (array_keys($configurator->oldFiles) as $i) {
123
            $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
124
            if (is_file($tempFile)) {
125
                unlink($tempFile);
126
            }
127
        }
128
    }
129
    //  ---  DELETE OLD FOLDERS ---------------
130
    xoops_load('XoopsFile');
131
    if (count($configurator->oldFolders) > 0) {
132
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
133
        foreach (array_keys($configurator->oldFolders) as $i) {
134
            $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
135
            /** @var XoopsObjectHandler $folderHandler */
136
            $folderHandler = \XoopsFile::getHandler('folder', $tempFolder);
137
            $folderHandler->delete($tempFolder);
138
        }
139
    }
140
    //  ---  CREATE UPLOAD FOLDERS ---------------
141
    if (count($configurator->uploadFolders) > 0) {
142
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
143
        foreach (array_keys($configurator->uploadFolders) as $i) {
144
            $utility::createFolder($configurator->uploadFolders[$i]);
145
        }
146
    }
147
    //  ---  COPY blank.png FILES ---------------
148
    if (count($configurator->copyBlankFiles) > 0) {
149
        $file = dirname(__DIR__) . '/assets/images/blank.png';
150
        foreach (array_keys($configurator->copyBlankFiles) as $i) {
151
            $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
152
            $utility::copyFile($file, $dest);
153
        }
154
    }
155
    //delete .html entries from the tpl table
156
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
157
    $GLOBALS['xoopsDB']->queryF($sql);
158
    //delete .tpl entries from the tpl table
159
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.tpl%'";
160
    $GLOBALS['xoopsDB']->queryF($sql);
161
    //delete tdmdownloads entries from the tpl_source table
162
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplsource') . " WHERE `tpl_source` LIKE '%tdmdownloads%'";
163
    $GLOBALS['xoopsDB']->queryF($sql);
164
    $sql = 'CREATE TABLE `' . $GLOBALS['xoopsDB']->prefix('tdmdownloads_downlimit') . "` (downlimit_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, downlimit_lid INT(11) UNSIGNED NOT NULL DEFAULT '0',
165
           downlimit_uid INT(11) NOT NULL DEFAULT '0', downlimit_hostname VARCHAR(60) NOT NULL DEFAULT '', downlimit_date INT(10) NOT NULL DEFAULT '0', PRIMARY KEY  (downlimit_id)
166
           ) ENGINE=MyISAM";
167
    $GLOBALS['xoopsDB']->query($sql);
168
    /** @var XoopsGroupPermHandler $gpermHandler */
169
    $gpermHandler = xoops_getHandler('groupperm');
170
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
171
}
172
173
/**
174
 * @param $module
175
 * @return bool
176
 */
177
function update_tdmdownloads_v200(&$module)
0 ignored issues
show
Unused Code introduced by
The parameter $module 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

177
function update_tdmdownloads_v200(/** @scrutinizer ignore-unused */ &$module)

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...
178
{
179
    // Update size
180
    $moduleDirName = basename(dirname(__DIR__));
181
    $db            = \XoopsDatabaseFactory::getDatabaseConnection();
182
    $sql           = 'SELECT lid, size FROM ' . $db->prefix('tdmdownloads_downloads');
183
    $result        = $db->query($sql);
184
    $helper        = Helper::getInstance();
185
    $helper->loadLanguage('admin');
186
    if ($result instanceof \mysqli_result) {
187
        while (false !== ($myrow = $db->fetchArray($result))) {
188
            $size_value_arr = explode(' ', $myrow['size']);
189
            switch ($size_value_arr[1]) {
190
                case _AM_TDMDOWNLOADS_BYTES:
191
                case 'Bytes':
192
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' B\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
193
                    $db->query($sql);
194
                    break;
195
                case _AM_TDMDOWNLOADS_KBYTES:
196
                case 'kB':
197
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' K\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
198
                    $db->query($sql);
199
                    break;
200
                case _AM_TDMDOWNLOADS_MBYTES:
201
                case 'MB':
202
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' M\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
203
                    $db->query($sql);
204
                    break;
205
                case _AM_TDMDOWNLOADS_GBYTES:
206
                case 'GB':
207
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' G\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
208
                    $db->query($sql);
209
                    break;
210
                case _AM_TDMDOWNLOADS_TBYTES:
211
                case 'TB':
212
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' T\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
213
                    $db->query($sql);
214
                    break;
215
            }
216
        }
217
    }
218
    // Update folder
219
    rename(XOOPS_ROOT_PATH . '/uploads/TDMDownloads', XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName);
220
    // Change TDMDownloads with tdmdownloads
221
    $sql    = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `url` = REPLACE(`url`, \'TDMDownloads\', \'' . $moduleDirName . '\') WHERE `url` LIKE \'%TDMDownloads%\'';
222
    $result = $db->query($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
223
    return true;
224
}
225
226
/**
227
 * @param $module
228
 * @return bool
229
 */
230
function update_tdmdownloads_v167(&$module)
0 ignored issues
show
Unused Code introduced by
The parameter $module 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

230
function update_tdmdownloads_v167(/** @scrutinizer ignore-unused */ &$module)

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...
231
{
232
    $moduleDirName = basename(dirname(__DIR__));
233
    // rename module dir from upper case to lower case
234
    rename(XOOPS_ROOT_PATH . '/modules/TDMDownloads', XOOPS_ROOT_PATH . '/modules/' . $moduleDirName);
235
    // rename upload dir from upper case to lower case
236
    rename(XOOPS_ROOT_PATH . '/uploads/TDMDownloads', XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName);
237
    // files have been moved to assets-folder
238
    $src = XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/css/';
239
    rrmdir($src);
240
    $src = XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/images/';
241
    rrmdir($src);
242
    // delete unneeded/replacfiles
243
    // unlink( XOOPS_ROOT_PATH.'/modules/' . $moduleDirName . '/admin/admin_header.php' );
244
    // clean template directory
245
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_brokenfile.html');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

245
    /** @scrutinizer ignore-unhandled */ @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_brokenfile.html');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
246
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_download.html');
247
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_index.html');
248
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_modfile.html');
249
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_ratefile.html');
250
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_singlefile.html');
251
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_submit.html');
252
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_viewcat.html');
253
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_liste.html');
254
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_rss.html');
255
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_new.html');
256
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_random.html');
257
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_rating.html');
258
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_search.html');
259
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_top.html');
260
    return true;
261
}
262
263
/**
264
 * @param $src
265
 */
266
function rrmdir($src)
267
{
268
    if (is_dir($src)) {
269
        $dir = opendir($src);
270
        while (false !== ($file = readdir($dir))) {
271
            if (('.' !== $file) && ('..' !== $file)) {
272
                $full = $src . '/' . $file;
273
                if (is_dir($full)) {
274
                    rrmdir($full);
275
                } else {
276
                    unlink($full);
277
                }
278
            }
279
        }
280
        closedir($dir);
281
        rmdir($src);
282
    }
283
}
284
285
/**
286
 * @return bool
287
 */
288
function update_tdmdownloads_v163()
289
{
290
    /** @var \XoopsMySQLDatabase $db */
291
    $db  = \XoopsDatabaseFactory::getDatabaseConnection();
292
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . '` CHANGE `cid` `cat_cid` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT ;';
293
    $db->query($sql);
294
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . "` CHANGE `pid` `cat_pid` INT( 5 ) UNSIGNED NOT NULL DEFAULT '0' ;";
295
    $db->query($sql);
296
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . '` CHANGE `title` `cat_title` VARCHAR( 255 ) NOT NULL ;';
297
    $db->query($sql);
298
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . '` CHANGE `imgurl` `cat_imgurl` VARCHAR( 255 ) NOT NULL ;';
299
    $db->query($sql);
300
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . '` CHANGE `description_main` `cat_description_main` TEXT NOT NULL ;';
301
    $db->query($sql);
302
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . "` CHANGE `weight` `cat_weight` INT( 11 ) NOT NULL DEFAULT '0' ;";
303
    $db->query($sql);
304
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_downloads') . '` ADD `paypal` VARCHAR( 255 ) NOT NULL;';
305
    $db->query($sql);
306
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_downloads') . "` CHANGE `size` `size` VARCHAR( 15 ) NOT NULL DEFAULT '';";
307
    $db->query($sql);
308
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_mod') . "` CHANGE `size` `size` VARCHAR( 15 ) NOT NULL DEFAULT '';";
309
    $db->query($sql);
310
    $sql = 'CREATE TABLE `' . $db->prefix('tdmdownloads_downlimit') . "` (downlimit_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, downlimit_lid INT(11) UNSIGNED NOT NULL DEFAULT '0',
311
           downlimit_uid INT(11) NOT NULL DEFAULT '0', downlimit_hostname VARCHAR(60) NOT NULL DEFAULT '', downlimit_date INT(10) NOT NULL DEFAULT '0', PRIMARY KEY  (downlimit_id)
312
           ) ENGINE=MyISAM";
313
    $db->query($sql);
314
    return true;
315
}
316