Completed
Push — master ( a9decc...a21b67 )
by Michael
02:51
created

include/onupdate.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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

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    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package      extcal
16
 * @since
17
 * @author       XOOPS Development Team,
18
 *
19
 */
20
21
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
22
    || !$GLOBALS['xoopsUser']->IsAdmin()
23
) {
24
    exit('Restricted access' . PHP_EOL);
25
}
26
27
/**
28
 * @param string $tablename
29
 *
30
 * @return bool
31
 */
32
function tableExists($tablename)
33
{
34
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
35
36
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0) ? true : false;
37
}
38
39
/**
40
 *
41
 * Prepares system prior to attempting to install module
42
 * @param XoopsModule $module {@link XoopsModule}
43
 *
44
 * @return bool true if ready to install, false if not
45
 */
46
function xoops_module_pre_update_extcal(XoopsModule $module)
47
{
48
    $moduleDirName = basename(dirname(__DIR__));
49
    $classUtility  = ucfirst($moduleDirName) . 'Utility';
50
    if (!class_exists($classUtility)) {
51
        xoops_load('utility', $moduleDirName);
52
    }
53
    //check for minimum XOOPS version
54
    if (!$classUtility::checkVerXoops($module)) {
55
        return false;
56
    }
57
58
    // check for minimum PHP version
59
    if (!$classUtility::checkVerPhp($module)) {
60
        return false;
61
    }
62
63
    return true;
64
}
65
66
/**
67
 *
68
 * Performs tasks required during update of the module
69
 * @param XoopsModule $module {@link XoopsModule}
70
 * @param null        $previousVersion
71
 *
72
 * @return bool true if update successful, false if not
73
 */
74
75
function xoops_module_update_extcal(XoopsModule $module, $previousVersion = null)
76
{
77
    //    global $xoopsDB;
78
79
    $moduleDirName = basename(dirname(__DIR__));
80
81
    $newVersion = $xoopsModule->getVar('version') * 100;
82
    if ($newVersion == $previousVersion) {
83
        return true;
84
    }
85
86
    $fld = XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname') . '/versions/';
87
    $cls = 'extcal_%1$s';
88
89
    $version = array(
90
        '2_04' => 204,
91
        '2_15' => 215,
92
        '2_21' => 221,
93
        '2_28' => 228,
94
        '2_29' => 229,
95
        '2_33' => 233,
96
        '2_34' => 234,
97
        '2_35' => 235,
98
        '2_37' => 237,
99
    );
100
101
//    while (list($key, $val) = each($version)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
102 View Code Duplication
    foreach ($version as $key => $val) {
0 ignored issues
show
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...
103
        if ($previousVersion < $val) {
104
            $name = sprintf($cls, $key);
105
            $f    = $fld . $name . '.php';
106
            //ext_echo ("<hr>{$f}<hr>");
107
            if (is_readable($f)) {
108
                echo "mise à jour version : {$key} = {$val}<br>";
109
                require_once $f;
110
                $cl = new $name($xoopsModule, array('previousVersion' => $previousVersion));
111
            }
112
        }
113
    }
114
115
    if ($previousVersion < 240) {
116
        $configurator = include __DIR__ . '/config.php';
117
        $classUtility = ucfirst($moduleDirName) . 'Utility';
118
        if (!class_exists($classUtility)) {
119
            xoops_load('utility', $moduleDirName);
120
        }
121
122
        //delete old HTML templates
123
        if (count($configurator['templateFolders']) > 0) {
124
            foreach ($configurator['templateFolders'] as $folder) {
125
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
126
                if (is_dir($templateFolder)) {
127
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), array('..', '.'));
128
                    foreach ($templateList as $k => $v) {
129
                        $fileInfo = new SplFileInfo($templateFolder . $v);
130
                        if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
131
                            if (file_exists($templateFolder . $v)) {
132
                                unlink($templateFolder . $v);
133
                            }
134
                        }
135
                    }
136
                }
137
            }
138
        }
139
140
        //  ---  COPY blank.png FILES ---------------
141 View Code Duplication
        if (count($configurator['copyFiles']) > 0) {
142
            $file = __DIR__ . '/../assets/images/blank.png';
143
            foreach (array_keys($configurator['copyFiles']) as $i) {
144
                $dest = $configurator['copyFiles'][$i] . '/blank.png';
145
                $classUtility::copyFile($file, $dest);
146
            }
147
        }
148
149
        //  ---  DELETE OLD FILES ---------------
150
        if (count($configurator['oldFiles']) > 0) {
151
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
152
            foreach (array_keys($configurator['oldFiles']) as $i) {
153
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator['oldFiles'][$i]);
154
                if (is_file($tempFile)) {
155
                    unlink($tempFile);
156
                }
157
            }
158
        }
159
160
        //---------------------
161
162
        //delete .html entries from the tpl table
163
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
0 ignored issues
show
The variable $xoopsDB 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...
164
        $xoopsDB->queryF($sql);
165
166
        // Load class XoopsFile ====================
167
        xoops_load('XoopsFile');
168
169
        //delete /images directory ============
170
        $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
171
        $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
172
        $folderHandler->delete($imagesDirectory);
173
    }
174
175
    $gpermHandler = xoops_getHandler('groupperm');
176
177
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
178
}
179