Passed
Pull Request — master (#88)
by Michael
13:07
created

update_tdmdownloads_v201()   C

Complexity

Conditions 17
Paths 32

Size

Total Lines 91
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 45
c 0
b 0
f 0
nc 32
nop 1
dl 0
loc 91
rs 5.2166

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

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

211
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...
212
{
213
    // Update size
214
215
    $moduleDirName = basename(dirname(__DIR__));
216
    $db = \XoopsDatabaseFactory::getDatabaseConnection();
217
218
    $sql = 'SELECT lid, size FROM ' . $db->prefix('tdmdownloads_downloads');
219
220
    $result = $db->query($sql);
221
222
    $helper = Helper::getInstance();
223
224
    $helper->loadLanguage('admin');
225
226
    if ($result instanceof \mysqli_result) {
227
        while (false !== ($myrow = $db->fetchArray($result))) {
228
            $size_value_arr = explode(' ', $myrow['size']);
229
230
            switch ($size_value_arr[1]) {
231
                case _AM_TDMDOWNLOADS_BYTES:
232
                case 'Bytes':
233
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' B\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
234
                    $db->query($sql);
235
                    break;
236
                case _AM_TDMDOWNLOADS_KBYTES:
237
                case 'kB':
238
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' K\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
239
                    $db->query($sql);
240
                    break;
241
                case _AM_TDMDOWNLOADS_MBYTES:
242
                case 'MB':
243
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' M\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
244
                    $db->query($sql);
245
                    break;
246
                case _AM_TDMDOWNLOADS_GBYTES:
247
                case 'GB':
248
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' G\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
249
                    $db->query($sql);
250
                    break;
251
                case _AM_TDMDOWNLOADS_TBYTES:
252
                case 'TB':
253
                    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `size` = \'' . $size_value_arr[0] . ' T\'' . ' WHERE `lid` = ' . $myrow['lid'] . ';';
254
                    $db->query($sql);
255
                    break;
256
            }
257
        }
258
    }
259
    // Update folder
260
261
    rename(XOOPS_ROOT_PATH . '/uploads/TDMDownloads', XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName );
262
263
    // Change TDMDownloads with tdmdownloads
264
265
    $sql = 'UPDATE `' . $db->prefix('tdmdownloads_downloads') . '` SET `url` = REPLACE(`url`, \'TDMDownloads\', \''. $moduleDirName.'\') WHERE `url` LIKE \'%TDMDownloads%\'';
266
267
    $result = $db->query($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
268
269
    return true;
270
}
271
272
/**
273
 * @param $module
274
 * @return bool
275
 */
276
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

276
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...
277
{
278
    $moduleDirName = basename(dirname(__DIR__));
279
    // rename module dir from upper case to lower case
280
281
    rename(XOOPS_ROOT_PATH . '/modules/TDMDownloads', XOOPS_ROOT_PATH . '/modules/' . $moduleDirName);
282
283
    // rename upload dir from upper case to lower case
284
285
    rename(XOOPS_ROOT_PATH . '/uploads/TDMDownloads', XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName);
286
287
    // files have been moved to assets-folder
288
289
    $src = XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/css/';
290
291
    rrmdir($src);
292
293
    $src = XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/images/';
294
295
    rrmdir($src);
296
297
    // delete unneeded/replacfiles
298
299
    // unlink( XOOPS_ROOT_PATH.'/modules/' . $moduleDirName . '/admin/admin_header.php' );
300
301
    // clean template directory
302
303
    @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

303
    /** @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...
304
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_download.html');
305
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_index.html');
306
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_modfile.html');
307
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_ratefile.html');
308
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_singlefile.html');
309
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_submit.html');
310
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_viewcat.html');
311
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_liste.html');
312
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/tdmdownloads_rss.html');
313
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_new.html');
314
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_random.html');
315
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_rating.html');
316
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_search.html');
317
    @unlink(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/templates/blocks/tdmdownloads_block_top.html');
318
319
    return true;
320
}
321
322
/**
323
 * @param $src
324
 */
325
function rrmdir($src)
326
{
327
    if (is_dir($src)) {
328
        $dir = opendir($src);
329
330
        while (false !== ($file = readdir($dir))) {
331
            if (('.' !== $file) && ('..' !== $file)) {
332
                $full = $src . '/' . $file;
333
334
                if (is_dir($full)) {
335
                    rrmdir($full);
336
                } else {
337
                    unlink($full);
338
                }
339
            }
340
        }
341
342
        closedir($dir);
343
344
        rmdir($src);
345
    }
346
}
347
348
/**
349
 * @return bool
350
 */
351
function update_tdmdownloads_v163()
352
{
353
    /** @var \XoopsMySQLDatabase $db */
354
355
    $db = \XoopsDatabaseFactory::getDatabaseConnection();
356
357
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . '` CHANGE `cid` `cat_cid` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT ;';
358
359
    $db->query($sql);
360
361
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . "` CHANGE `pid` `cat_pid` INT( 5 ) UNSIGNED NOT NULL DEFAULT '0' ;";
362
363
    $db->query($sql);
364
365
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . '` CHANGE `title` `cat_title` VARCHAR( 255 ) NOT NULL ;';
366
367
    $db->query($sql);
368
369
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . '` CHANGE `imgurl` `cat_imgurl` VARCHAR( 255 ) NOT NULL ;';
370
371
    $db->query($sql);
372
373
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . '` CHANGE `description_main` `cat_description_main` TEXT NOT NULL ;';
374
375
    $db->query($sql);
376
377
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_cat') . "` CHANGE `weight` `cat_weight` INT( 11 ) NOT NULL DEFAULT '0' ;";
378
379
    $db->query($sql);
380
381
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_downloads') . '` ADD `paypal` VARCHAR( 255 ) NOT NULL;';
382
383
    $db->query($sql);
384
385
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_downloads') . "` CHANGE `size` `size` VARCHAR( 15 ) NOT NULL DEFAULT '';";
386
387
    $db->query($sql);
388
389
    $sql = 'ALTER TABLE `' . $db->prefix('tdmdownloads_mod') . "` CHANGE `size` `size` VARCHAR( 15 ) NOT NULL DEFAULT '';";
390
391
    $db->query($sql);
392
393
    $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',
394
           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)
395
           ) ENGINE=MyISAM";
396
397
    $db->query($sql);
398
399
    return true;
400
}
401