Passed
Push — master ( 82b95d...638320 )
by Michael
04:10 queued 11s
created

tableExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
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    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link https://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
use XoopsModules\Extcal;
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
 * @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
 * Prepares system prior to attempting to install module
41
 * @param \XoopsModule $module {@link XoopsModule}
42
 *
43
 * @return bool true if ready to install, false if not
44
 */
45
function xoops_module_pre_update_extcal(\XoopsModule $module)
46
{
47
    /** @var Extcal\Helper $helper */
48
    /** @var Extcal\Utility $utility */
49
    $helper  = Extcal\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
50
    $utility = new Extcal\Utility();
51
52
    $xoopsSuccess = $utility::checkVerXoops($module);
0 ignored issues
show
Unused Code introduced by
The assignment to $xoopsSuccess is dead and can be removed.
Loading history...
53
    $phpSuccess   = $utility::checkVerPhp($module);
0 ignored issues
show
Unused Code introduced by
The assignment to $phpSuccess is dead and can be removed.
Loading history...
54
55
    //mb    return $xoopsSuccess && $phpSuccess;
56
57
    //    XoopsLoad::load('migrate', 'extcal');
58
    /** @var \XoopsModules\Extcal\Common\Configurator $configurator */
59
    $configurator = new \XoopsModules\Extcal\Common\Configurator();
60
61
    //create upload folders
62
    $uploadFolders = $configurator->uploadFolders;
63
    foreach ($uploadFolders as $value) {
64
        $utility::prepareFolder($value);
65
    }
66
67
    $migrator = new \XoopsModules\Extcal\Common\Migrate($configurator);
68
    $migrator->synchronizeSchema();
69
70
    return true;
71
}
72
73
/**
74
 * Performs tasks required during update of the module
75
 * @param \XoopsModule $module {@link XoopsModule}
76
 * @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...
77
 *
78
 * @return bool true if update successful, false if not
79
 */
80
function xoops_module_update_extcal(\XoopsModule $module, $previousVersion = null)
81
{
82
    //    global $xoopsDB;
83
    $moduleDirName = basename(dirname(__DIR__));
84
85
    $newVersion = $module->getVar('version') * 100;
86
    if ($newVersion == $previousVersion) {
87
        return true;
88
    }
89
90
    $fld = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/versions/';
91
    $cls = 'Extcal_%1$s';
92
93
    $version = [
94
        '2_04' => 204,
95
        '2_15' => 215,
96
        '2_21' => 221,
97
        '2_28' => 228,
98
        '2_29' => 229,
99
        '2_33' => 233,
100
        '2_34' => 234,
101
        '2_35' => 235,
102
        '2_37' => 237,
103
    ];
104
105
    //    while (list($key, $val) = each($version)) {
106
    foreach ($version as $key => $val) {
107
        if ($previousVersion < $val) {
108
            $name = sprintf($cls, $key);
109
            $f    = $fld . $name . '.php';
110
            //ext_echo ("<hr>{$f}<hr>");
111
            if (is_readable($f)) {
112
                echo "update version: {$key} = {$val}<br>";
113
                require_once $f;
114
                $cl = new $name($module, ['previousVersion' => $previousVersion]);
0 ignored issues
show
Unused Code introduced by
The assignment to $cl is dead and can be removed.
Loading history...
115
            }
116
        }
117
    }
118
119
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
120
121
    /** @var Extcal\Helper $helper */ 
122
    /** @var Extcal\Utility $utility */
123
    /** @var Extcal\Common\Configurator $configurator */
124
    $helper       = Extcal\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
125
    $utility      = new Extcal\Utility();
126
    $configurator = new Extcal\Common\Configurator();
127
128
    $migrator = new \XoopsModules\Extcal\Common\Migrate($configurator);
129
    $migrator->synchronizeSchema();
130
131
    if ($previousVersion < 241) {
132
        //delete old HTML templates
133
        if (count($configurator->templateFolders) > 0) {
134
            foreach ($configurator->templateFolders as $folder) {
135
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
136
                if (is_dir($templateFolder)) {
137
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
0 ignored issues
show
Bug introduced by
It seems like scandir($templateFolder, SCANDIR_SORT_NONE) can also be of type false; however, parameter $array1 of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

137
                    $templateList = array_diff(/** @scrutinizer ignore-type */ scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
Loading history...
138
                    foreach ($templateList as $k => $v) {
139
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
140
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
141
                            if (file_exists($templateFolder . $v)) {
142
                                unlink($templateFolder . $v);
143
                            }
144
                        }
145
                    }
146
                }
147
            }
148
        }
149
150
        //  ---  COPY blank.png FILES ---------------
151
        if (count($configurator->copyBlankFiles) > 0) {
152
            $file = dirname(__DIR__) . '/assets/images/blank.png';
153
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
154
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
155
                $utility::copyFile($file, $dest);
156
            }
157
        }
158
159
        //  ---  DELETE OLD FILES ---------------
160
        if (count($configurator->oldFiles) > 0) {
161
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
162
            foreach (array_keys($configurator->oldFiles) as $i) {
163
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
164
                if (is_file($tempFile)) {
165
                    unlink($tempFile);
166
                }
167
            }
168
        }
169
170
        //---------------------
171
172
        //delete .html entries from the tpl table
173
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
174
        $GLOBALS['xoopsDB']->queryF($sql);
175
176
        // Load class XoopsFile ====================
177
        xoops_load('XoopsFile');
178
179
        //delete /images directory ============
180
        $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
181
        $folderHandler   = \XoopsFile::getHandler('folder', $imagesDirectory);
182
        $folderHandler->delete($imagesDirectory);
183
    }
184
    /** @var \XoopsGroupPermHandler $grouppermHandler */
185
    $grouppermHandler = xoops_getHandler('groupperm');
186
187
    return $grouppermHandler->deleteByModule($module->getVar('mid'), 'item_read');
188
}
189