xoops_module_pre_update_news()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
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.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author       XOOPS Development Team
16
 */
17
18
use XoopsModules\News;
19
use XoopsModules\News\Common\Configurator;
20
use XoopsModules\News\Utility;
21
22
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
23
    || !$GLOBALS['xoopsUser']->isAdmin()) {
24
    exit('Restricted access' . PHP_EOL);
25
}
26
27
/**
28
 * Prepares system prior to attempting to install module
29
 * @param \XoopsModule $module {@link XoopsModule}
30
 *
31
 * @return bool true if ready to install, false if not
32
 */
33
function xoops_module_pre_update_news(\XoopsModule $module)
34
{
35
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
36
    /** @var News\Helper $helper */
37
    /** @var News\Utility $utility */
38
    $helper  = News\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
39
    $utility = new Utility();
40
41
    $xoopsSuccess = $utility::checkVerXoops($module);
42
    $phpSuccess   = $utility::checkVerPhp($module);
43
44
    return $xoopsSuccess && $phpSuccess;
45
}
46
47
/**
48
 * Performs tasks required during update of the module
49
 * @param \XoopsModule $module {@link XoopsModule}
50
 * @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...
51
 *
52
 * @return bool true if update successful, false if not
53
 */
54
function xoops_module_update_news(\XoopsModule $module, $previousVersion = null)
55
{
56
    $moduleDirName      = \basename(\dirname(__DIR__));
57
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
58
59
    /** @var News\Helper $helper */ /** @var News\Utility $utility */
60
    /** @var News\Common\Configurator $configurator */
61
    $helper       = News\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
62
    $utility      = new Utility();
63
    $configurator = new Configurator();
64
65
    if ($previousVersion < 240) {
66
        //delete old HTML templates
67
        if (count($configurator->templateFolders) > 0) {
68
            foreach ($configurator->templateFolders as $folder) {
69
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
70
                if (is_dir($templateFolder)) {
71
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
72
                    foreach ($templateList as $k => $v) {
73
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
74
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
75
                            if (is_file($templateFolder . $v)) {
76
                                unlink($templateFolder . $v);
77
                            }
78
                        }
79
                    }
80
                }
81
            }
82
        }
83
84
        //  ---  DELETE OLD FILES ---------------
85
        if (count($configurator->oldFiles) > 0) {
86
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
87
            foreach (array_keys($configurator->oldFiles) as $i) {
88
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
89
                if (is_file($tempFile)) {
90
                    unlink($tempFile);
91
                }
92
            }
93
        }
94
95
        //  ---  DELETE OLD FOLDERS ---------------
96
        xoops_load('XoopsFile');
97
        if (count($configurator->oldFolders) > 0) {
98
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
99
            foreach (array_keys($configurator->oldFolders) as $i) {
100
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
101
                /** @var XoopsObjectHandler $folderHandler */
102
                $folderHandler = \XoopsFile::getHandler('folder', $tempFolder);
103
                $folderHandler->delete($tempFolder);
104
            }
105
        }
106
107
        //  ---  CREATE FOLDERS ---------------
108
        if (count($configurator->uploadFolders) > 0) {
109
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
110
            foreach (array_keys($configurator->uploadFolders) as $i) {
111
                $utility::createFolder($configurator->uploadFolders[$i]);
112
            }
113
        }
114
115
        //  ---  COPY blank.png FILES ---------------
116
        if (count($configurator->copyBlankFiles) > 0) {
117
            $file = \dirname(__DIR__) . '/assets/images/blank.png';
118
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
119
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
120
                $utility::copyFile($file, $dest);
121
            }
122
        }
123
124
        //delete .html entries from the tpl table
125
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
126
        $GLOBALS['xoopsDB']->queryF($sql);
127
128
        /** @var \XoopsGroupPermHandler $grouppermHandler */
129
        $grouppermHandler = xoops_getHandler('groupperm');
130
131
        return $grouppermHandler->deleteByModule($module->getVar('mid'), 'item_read');
132
    }
133
134
    return true;
135
}
136