Completed
Branch master (b963e0)
by Michael
03:39
created

onupdate.php ➔ xoops_module_pre_update_publisher()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 1
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
1
<?php
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         http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @author          trabis <[email protected]>
16
 *
17
 */
18
19
/**
20
 *
21
 * Prepares system prior to attempting to install module
22
 * @param XoopsModule $module {@link XoopsModule}
23
 *
24
 * @return bool true if ready to install, false if not
25
 */
26
function xoops_module_pre_update_publisher(XoopsModule $module)
27
{
28
    $moduleDirName = basename(dirname(__DIR__));
29
30
    $className = ucfirst($moduleDirName) . 'Utility';
31
    if (!class_exists($className)) {
32
        xoops_load('utility', $moduleDirName);
33
    }
34
    //check for minimum XOOPS version
35
    if (!$className::checkVerXoops($module)) {
36
        return false;
37
    }
38
39
    // check for minimum PHP version
40
    if (!$className::checkVerPhp($module)) {
41
        return false;
42
    }
43
44
    return true;
45
}
46
47
/**
48
 *
49
 * Performs tasks required during update of the module
50
 * @param XoopsModule $module {@link XoopsModule}
51
 * @param null        $previousVersion
52
 *
53
 * @return bool true if update successful, false if not
54
 */
55
56
function xoops_module_update_publisher(XoopsModule $module, $previousVersion = null)
0 ignored issues
show
Coding Style introduced by
xoops_module_update_publisher 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...
57
{
58
    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...
59
    require_once __DIR__ . '/../../../mainfile.php';
60
    require_once __DIR__ . '/../include/config.php';
61
62
    $moduleDirName = basename(dirname(__DIR__));
63
64
    if (false !== ($moduleHelper = Xmf\Module\Helper::getHelper($moduleDirName))) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
65
    } else {
66
        $moduleHelper = Xmf\Module\Helper::getHelper('system');
67
    }
68
69
    // Load language files
70
    $moduleHelper->loadLanguage('admin');
71
    $moduleHelper->loadLanguage('modinfo');
72
73
    $configurator = new PublisherConfigurator();
74
    /** @var PublisherUtility $utilityClass */
75
    $utilityClass = ucfirst($moduleDirName) . 'Utility';
76
    if (!class_exists($utilityClass)) {
77
        xoops_load('utility', $moduleDirName);
78
    }
79
80
    //delete .html entries from the tpl table
81
    $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
82
    $xoopsDB->queryF($sql);
83
    $sql = 'DELETE FROM ' . $xoopsDB->prefix('newblocks') . " WHERE `dirname` = '" . $module->getVar('dirname', 'n') . "' AND `template` LIKE '%.html%'";
84
    $xoopsDB->queryF($sql);
85
86
    if ($previousVersion <= 105) {
87
88
        //change TEXT fields to NULL
89
        $sql = '    ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getVar('dirname', 'n') . '_categories') . ' MODIFY `description` TEXT NULL';
90
        $xoopsDB->queryF($sql);
91
        $sql = '    ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getVar('dirname', 'n') . '_categories') . ' MODIFY `header` TEXT NULL';
92
        $xoopsDB->queryF($sql);
93
        $sql = '    ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getVar('dirname', 'n') . '_categories') . ' MODIFY `meta_keywords` TEXT NULL';
94
        $xoopsDB->queryF($sql);
95
        $sql = '    ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getVar('dirname', 'n') . '_categories') . ' MODIFY `meta_description` TEXT NULL';
96
        $xoopsDB->queryF($sql);
97
        /** @var PublisherUtility $utilityClass */
98
        $utilityClass = ucfirst($moduleDirName) . 'Utility';
99
        if (!class_exists($utilityClass)) {
100
            xoops_load('utility', $moduleDirName);
101
        }
102
103
        //delete old HTML templates
104
        if (count($configurator->templateFolders) > 0) {
105
            foreach ($configurator->templateFolders as $folder) {
106
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
107
                if (is_dir($templateFolder)) {
108
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
109
                    foreach ($templateList as $k => $v) {
110
                        $fileInfo = new SplFileInfo($templateFolder . $v);
111
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
112
                            if (file_exists($templateFolder . $v)) {
113
                                unlink($templateFolder . $v);
114
                            }
115
                        }
116
                    }
117
                }
118
            }
119
        }
120
121
        //  ---  DELETE OLD FILES ---------------
122
        if (count($configurator->oldFiles) > 0) {
123
            //    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...
124
            foreach (array_keys($configurator->oldFiles) as $i) {
125
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
126
                if (is_file($tempFile)) {
127
                    unlink($tempFile);
128
                }
129
            }
130
        }
131
132
        //  ---  DELETE OLD FOLDERS ---------------
133
        xoops_load('XoopsFile');
134
        if (count($configurator->oldFolders) > 0) {
135
            //    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...
136
            foreach (array_keys($configurator->oldFolders) as $i) {
137
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
138
                /** @var XoopsObjectHandler $folderHandler */
139
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
140
                $folderHandler->delete($tempFolder);
141
            }
142
        }
143
144
        //  ---  CREATE FOLDERS ---------------
145 View Code Duplication
        if (count($configurator->uploadFolders) > 0) {
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
            //    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...
147
            foreach (array_keys($configurator->uploadFolders) as $i) {
148
                $utilityClass::createFolder($configurator->uploadFolders[$i]);
149
            }
150
        }
151
152
        //  ---  COPY blank.png FILES ---------------
153 View Code Duplication
        if (count($configurator->blankFiles) > 0) {
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...
154
            $file = __DIR__ . '/../assets/images/blank.png';
155
            foreach (array_keys($configurator->blankFiles) as $i) {
156
                $dest = $configurator->blankFiles[$i] . '/blank.png';
157
                $utilityClass::copyFile($file, $dest);
158
            }
159
        }
160
161
        //delete .html entries from the tpl table
162
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
163
        $xoopsDB->queryF($sql);
164
    }
165
    /* @var  $gpermHandler XoopsGroupPermHandler */
166
    $gpermHandler = xoops_getHandler('groupperm');
167
168
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
169
}
170