onupdate.php ➔ xoops_module_update_apcal()   F
last analyzed

Complexity

Conditions 26
Paths 9216

Size

Total Lines 198

Duplication

Lines 21
Ratio 10.61 %

Importance

Changes 0
Metric Value
cc 26
nc 9216
nop 1
dl 21
loc 198
rs 0
c 0
b 0
f 0

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
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 http://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team,
18
 * @author       GIJ=CHECKMATE (PEAK Corp. http://www.peak.ne.jp/)
19
 * @author       Antiques Promotion (http://www.antiquespromotion.ca)
20
 * @return bool
21
 */
22
23
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
24
    || !$GLOBALS['xoopsUser']->IsAdmin()
25
) {
26
    exit('Restricted access' . PHP_EOL);
27
}
28
29
/**
30
 * @param string $tablename
31
 *
32
 * @return bool
33
 */
34
function tableExists($tablename)
35
{
36
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
37
38
    return $GLOBALS['xoopsDB']->getRowsNum($result) > 0;
39
}
40
41
/**
42
 *
43
 * Prepares system prior to attempting to install module
44
 * @param XoopsModule $module {@link XoopsModule}
45
 *
46
 * @return bool true if ready to install, false if not
47
 */
48
function xoops_module_pre_update_apcal(XoopsModule $module)
49
{
50
    $moduleDirName = basename(dirname(__DIR__));
51
    $classUtility  = ucfirst($moduleDirName) . 'Utility';
52
    if (!class_exists($classUtility)) {
53
        xoops_load('utility', $moduleDirName);
54
    }
55
    //check for minimum XOOPS version
56
    if (!$classUtility::checkVerXoops($module)) {
57
        return false;
58
    }
59
60
    // check for minimum PHP version
61
    if (!$classUtility::checkVerPhp($module)) {
62
        return false;
63
    }
64
65
    return true;
66
}
67
68
function xoops_module_update_apcal(XoopsModule $module)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
69
{
70
    //    global $xoopsDB;
71
    $moduleDirName = basename(dirname(__DIR__));
72
    $capsDirName   = strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
$capsDirName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73
74 View Code Duplication
    if (!$GLOBALS['xoopsDB']->queryF("SELECT shortsummary FROM {$GLOBALS['xoopsDB']->prefix('apcal_event')}")) {
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...
75
        if ($GLOBALS['xoopsDB']->queryF("ALTER TABLE {$GLOBALS['xoopsDB']->prefix('apcal_event')} ADD shortsummary VARCHAR(255) AFTER groupid")) {
76
        }
77
    }
78
    $result = $GLOBALS['xoopsDB']->queryF("SELECT id, summary FROM {$GLOBALS['xoopsDB']->prefix('apcal_event')}");
79
    while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) {
80
        $shortsummary = makeShort($row['summary']);
81
        $GLOBALS['xoopsDB']->queryF("UPDATE {$GLOBALS['xoopsDB']->prefix('apcal_event')} SET shortsummary='{$shortsummary}' WHERE id={$row['id']}");
82
    }
83
84 View Code Duplication
    if (!$GLOBALS['xoopsDB']->queryF("SELECT cat_shorttitle FROM {$GLOBALS['xoopsDB']->prefix('apcal_cat')}")) {
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...
85
        if ($GLOBALS['xoopsDB']->queryF("ALTER TABLE {$GLOBALS['xoopsDB']->prefix('apcal_cat')} ADD cat_shorttitle VARCHAR(255) AFTER enabled")) {
86
        }
87
    }
88
    $result = $GLOBALS['xoopsDB']->queryF("SELECT cid, cat_title FROM {$GLOBALS['xoopsDB']->prefix('apcal_cat')}");
89
    while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) {
90
        $cat_shorttitle = makeShort($row['cat_title']);
91
        $GLOBALS['xoopsDB']->queryF("UPDATE {$GLOBALS['xoopsDB']->prefix('apcal_cat')} SET cat_shorttitle='{$cat_shorttitle}' WHERE cid={$row['cid']}");
92
    }
93
94
    if (!$GLOBALS['xoopsDB']->queryF("SELECT email,url,mainCategory, otherHours FROM {$GLOBALS['xoopsDB']->prefix('apcal_event')}")) {
95
        $sql = "ALTER TABLE {$GLOBALS['xoopsDB']->prefix('apcal_event')} ";
96
        $sql .= 'ADD url VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT \'\' AFTER location,';
97
        $sql .= 'ADD email VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT \'\' AFTER url,';
98
        $sql .= 'ADD mainCategory SMALLINT( 5 ) UNSIGNED ZEROFILL NOT NULL DEFAULT \'00000\' AFTER dtstamp,';
99
        $sql .= 'ADD otherHours VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT \'\' AFTER end';
100
        $GLOBALS['xoopsDB']->queryF($sql);
101
    }
102
103
    if (!$GLOBALS['xoopsDB']->queryF("SELECT color,canbemain FROM {$GLOBALS['xoopsDB']->prefix('apcal_cat')}")) {
104
        $sql = "ALTER TABLE {$GLOBALS['xoopsDB']->prefix('apcal_cat')} ";
105
        $sql .= 'ADD color VARCHAR( 7 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT \'#5555AA\' AFTER cat_desc,';
106
        $sql .= 'ADD canbemain TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT \'0\' AFTER autocreated';
107
        $GLOBALS['xoopsDB']->queryF($sql);
108
    }
109
110
    $sql = "CREATE TABLE IF NOT EXISTS {$GLOBALS['xoopsDB']->prefix('apcal_pictures')} ";
111
    $sql .= '(id int(10) unsigned NOT NULL AUTO_INCREMENT,';
112
    $sql .= 'event_id int(10) unsigned zerofill NOT NULL,';
113
    $sql .= 'picture varchar(255) NOT NULL,';
114
    $sql .= 'main_pic tinyint(1) unsigned NOT NULL DEFAULT \'0\',';
115
    $sql .= 'PRIMARY KEY (id)) ';
116
    $sql .= 'ENGINE=MyISAM DEFAULT CHARSET=utf8';
117
    $GLOBALS['xoopsDB']->queryF($sql);
118
119
    $sql = "CREATE TABLE IF NOT EXISTS {$GLOBALS['xoopsDB']->prefix('apcal_ro_events')} (
120
            roe_id int(10) unsigned NOT NULL AUTO_INCREMENT,
121
            roe_eventid mediumint(8) unsigned zerofill NOT NULL DEFAULT '00000000',
122
            roe_number int(10) NOT NULL DEFAULT '0',
123
            roe_datelimit int(10) NOT NULL DEFAULT '0',
124
            roe_needconfirm INT(10) NOT NULL DEFAULT '0',
125
            roe_waitinglist INT(10) NOT NULL DEFAULT '0',
126
            roe_submitter int(10) NOT NULL DEFAULT '0',
127
            roe_date_created int(10) NOT NULL DEFAULT '0',
128
            PRIMARY KEY (roe_id),
129
            KEY event (roe_eventid))
130
            ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
131
    $GLOBALS['xoopsDB']->queryF($sql);
132
133
    $sql = "CREATE TABLE IF NOT EXISTS {$GLOBALS['xoopsDB']->prefix('apcal_ro_members')} (
134
            rom_id int(10) unsigned NOT NULL AUTO_INCREMENT,
135
            rom_eventid mediumint(8) unsigned zerofill NOT NULL DEFAULT '00000000',
136
            rom_firstname varchar(200) DEFAULT NULL,
137
            rom_lastname varchar(200) DEFAULT NULL,
138
            rom_email varchar(200) DEFAULT NULL,
139
            rom_extrainfo1 varchar(200) DEFAULT NULL,
140
            rom_extrainfo2 varchar(200) DEFAULT NULL,
141
            rom_extrainfo3 varchar(200) DEFAULT NULL,
142
            rom_extrainfo4 varchar(200) DEFAULT NULL,
143
            rom_extrainfo5 varchar(200) DEFAULT NULL,
144
            rom_poster_ip  varchar(200) DEFAULT NULL,
145
            rom_status int(10) NOT NULL DEFAULT '0',
146
            rom_submitter int(10) NOT NULL DEFAULT '0',
147
            rom_date_created int(10) NOT NULL DEFAULT '0',
148
            PRIMARY KEY (rom_id),
149
            UNIQUE KEY UNQ_EMAIL (rom_eventid, rom_email),
150
            KEY event (rom_eventid))
151
            ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
152
    $GLOBALS['xoopsDB']->queryF($sql);
153
154
    $sql = "CREATE TABLE IF NOT EXISTS {$GLOBALS['xoopsDB']->prefix('apcal_ro_notify')} (
155
            ron_id int(10) unsigned NOT NULL AUTO_INCREMENT,
156
            ron_eventid mediumint(8) unsigned zerofill NOT NULL DEFAULT '00000000',
157
            ron_email varchar(200) DEFAULT NULL,
158
            ron_submitter int(10) DEFAULT NULL,
159
            ron_date_created int(11) NOT NULL DEFAULT '0',
160
            PRIMARY KEY (ron_id),
161
            KEY event (ron_eventid))
162
            ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
163
    $GLOBALS['xoopsDB']->queryF($sql);
164
165
    $GLOBALS['xoopsDB']->queryF("UPDATE {$GLOBALS['xoopsDB']->prefix('apcal_event')} SET start_date=NULL,end_date=NULL");
166
    $GLOBALS['xoopsDB']->queryF("UPDATE {$GLOBALS['xoopsDB']->prefix('apcal_event')} t, (SELECT id, shortsummary FROM {$GLOBALS['xoopsDB']->prefix('apcal_event')} x WHERE x.rrule_pid>0 GROUP BY x.shortsummary ORDER BY start) AS e SET t.rrule_pid=e.id WHERE t.shortsummary=e.shortsummary;");
167
168
    //    fix problem from removed poster_ip
169
    $sql = "ALTER TABLE {$GLOBALS['xoopsDB']->prefix('apcal_ro_members')} ADD `rom_status` INT(1) NOT NULL DEFAULT '0' AFTER `rom_extrainfo5`;";
170
    $GLOBALS['xoopsDB']->queryF($sql);
171
    //    fix problem from removed poster_ip
172
    $sql = "ALTER TABLE {$GLOBALS['xoopsDB']->prefix('apcal_ro_members')} ADD `rom_poster_ip` VARCHAR(200) NULL DEFAULT '' AFTER `rom_extrainfo5`;";
173
    $GLOBALS['xoopsDB']->queryF($sql);
174
175
    //    fix problem from removed roe_waitinglist
176
    $sql = "ALTER TABLE {$GLOBALS['xoopsDB']->prefix('apcal_ro_events')} ADD `roe_waitinglist` INT(10) NOT NULL DEFAULT '0' AFTER `roe_datelimit`;";
177
    $GLOBALS['xoopsDB']->queryF($sql);
178
179
    //    fix problem from removed roe_waitinglist
180
    $sql = "ALTER TABLE {$GLOBALS['xoopsDB']->prefix('apcal_ro_events')} ADD `roe_needconfirm` INT(10) NOT NULL DEFAULT '0' AFTER `roe_datelimit`;";
181
    $GLOBALS['xoopsDB']->queryF($sql);
182
183
    //    if (!is_dir(XOOPS_UPLOAD_PATH . '/apcal/')) {
184
    //        mkdir(XOOPS_UPLOAD_PATH . '/apcal/', 0755);
185
    //    }
186
    //    if (!is_dir(XOOPS_UPLOAD_PATH . '/apcal/thumbs/')) {
187
    //        mkdir(XOOPS_UPLOAD_PATH . '/apcal/thumbs/', 0755);
188
    //    }
189
190
    require_once __DIR__ . '/config.php';
191
    $configurator = new ModuleConfigurator();
192
    $classUtility = ucfirst($moduleDirName) . 'Utility';
193
    if (!class_exists($classUtility)) {
194
        xoops_load('utility', $moduleDirName);
195
    }
196
197
    //delete old HTML templates
198
    if (count($configurator->templateFolders) > 0) {
199
        foreach ($configurator->templateFolders as $folder) {
200
            $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
201
            if (is_dir($templateFolder)) {
202
                $templateList = array_diff(scandir($templateFolder), array('..', '.'));
203
                foreach ($templateList as $k => $v) {
204
                    $fileInfo = new SplFileInfo($templateFolder . $v);
205
                    if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
206
                        if (file_exists($templateFolder . $v)) {
207
                            unlink($templateFolder . $v);
208
                        }
209
                    }
210
                }
211
            }
212
        }
213
    }
214
215
    //  ---  DELETE OLD FILES ---------------
216
    if (count($configurator->oldFiles) > 0) {
217
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
218
        foreach (array_keys($configurator->oldFiles) as $i) {
219
            $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
220
            if (is_file($tempFile)) {
221
                unlink($tempFile);
222
            }
223
        }
224
    }
225
226
    //  ---  DELETE OLD FOLDERS ---------------
227
    xoops_load('XoopsFile');
228
    if (count($configurator->oldFolders) > 0) {
229
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
230
        foreach (array_keys($configurator->oldFolders) as $i) {
231
            $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
232
            /* @var $folderHandler XoopsObjectHandler */
233
            $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
234
            $folderHandler->delete($tempFolder);
235
        }
236
    }
237
238
    //  ---  CREATE FOLDERS ---------------
239 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...
240
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
241
        foreach (array_keys($configurator->uploadFolders) as $i) {
242
            $classUtility::createFolder($configurator->uploadFolders[$i]);
243
        }
244
    }
245
246
    //  ---  COPY blank.png FILES ---------------
247 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...
248
        $file = __DIR__ . '/../assets/images/blank.png';
249
        foreach (array_keys($configurator->blankFiles) as $i) {
250
            $dest = $configurator->blankFiles[$i] . '/blank.png';
251
            $classUtility::copyFile($file, $dest);
252
        }
253
    }
254
255
    //delete .html entries from the tpl table
256
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
257
    $GLOBALS['xoopsDB']->queryF($sql);
258
259
    /** @var XoopsGroupPermHandler $gpermHandler */
260
    $gpermHandler = xoops_getHandler('groupperm');
261
262
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
263
264
    return true;
0 ignored issues
show
Unused Code introduced by
return true; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
265
}
266
267
/**
268
 * @param $str
269
 * @return mixed
270
 */
271 View Code Duplication
function makeShort($str)
0 ignored issues
show
Best Practice introduced by
The function makeShort() has been defined more than once; this definition is ignored, only the first definition in include/oninstall.php (L224-298) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Duplication introduced by
This function seems to be duplicated in 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...
272
{
273
    $replacements = array(
274
        'Š' => 'S',
275
        'š' => 's',
276
        'Ž' => 'Z',
277
        'ž' => 'z',
278
        'À' => 'A',
279
        'Á' => 'A',
280
        'Â' => 'A',
281
        'Ã' => 'A',
282
        'Ä' => 'A',
283
        'Å' => 'A',
284
        'Æ' => 'A',
285
        'Ç' => 'C',
286
        'È' => 'E',
287
        'É' => 'E',
288
        'Ê' => 'E',
289
        'Ë' => 'E',
290
        'Ì' => 'I',
291
        'Í' => 'I',
292
        'Î' => 'I',
293
        'Ï' => 'I',
294
        'Ñ' => 'N',
295
        'Ò' => 'O',
296
        'Ó' => 'O',
297
        'Ô' => 'O',
298
        'Õ' => 'O',
299
        'Ö' => 'O',
300
        'Ø' => 'O',
301
        'Ù' => 'U',
302
        'Ú' => 'U',
303
        'Û' => 'U',
304
        'Ü' => 'U',
305
        'Ý' => 'Y',
306
        'Þ' => 'B',
307
        'ß' => 'ss',
308
        'à' => 'a',
309
        'á' => 'a',
310
        'â' => 'a',
311
        'ã' => 'a',
312
        'ä' => 'a',
313
        'å' => 'a',
314
        'æ' => 'a',
315
        'ç' => 'c',
316
        'è' => 'e',
317
        'é' => 'e',
318
        'ê' => 'e',
319
        'ë' => 'e',
320
        'ì' => 'i',
321
        'í' => 'i',
322
        'î' => 'i',
323
        'ï' => 'i',
324
        'ð' => 'o',
325
        'ñ' => 'n',
326
        'ò' => 'o',
327
        'ó' => 'o',
328
        'ô' => 'o',
329
        'õ' => 'o',
330
        'ö' => 'o',
331
        'ø' => 'o',
332
        'ù' => 'u',
333
        'ú' => 'u',
334
        'û' => 'u',
335
        'ý' => 'y',
336
        'ý' => 'y',
337
        'þ' => 'b',
338
        'ÿ' => 'y'
339
    );
340
341
    $str = strip_tags($str);
342
    $str = strtr($str, $replacements);
343
344
    return str_replace(array(' ', '-', '/', "\\", "'", '"', "\r", "\n", '&', '?', '!', '%', ',', '.'), '', $str);
345
}
346