Completed
Push — master ( f454c2...819fc9 )
by Michael
19s queued 10s
created

onupdate.php ➔ xoops_module_update_xnewsletter_130()   F

Complexity

Conditions 11
Paths 384

Size

Total Lines 122

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 384
nop 0
dl 0
loc 122
rs 3.3466
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
 * ****************************************************************************
4
 *  - A Project by Developers TEAM For Xoops - ( https://xoops.org )
5
 * ****************************************************************************
6
 *  XNEWSLETTER - MODULE FOR XOOPS
7
 *  Copyright (c) 2007 - 2012
8
 *  Goffy ( wedega.com )
9
 *
10
 *  You may not change or alter any portion of this comment or credits
11
 *  of supporting developers from this source code or any supporting
12
 *  source code which is considered copyrighted (c) material of the
13
 *  original comment or credit authors.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *  ---------------------------------------------------------------------------
20
 * @copyright  Goffy ( wedega.com )
21
 * @license    GPL 2.0
22
 * @package    xnewsletter
23
 * @author     Goffy ( [email protected] )
24
 *
25
 *  Version : 1 Mon 2012/11/05 14:31:32 :  Exp $
26
 * ****************************************************************************
27
 *
28
 * @param      $xoopsModule
29
 * @param null $oldversion
30
 *
31
 * @return bool
32
 */
33
34
xoops_loadLanguage('admin', 'xnewsletter');
35
36
/**
37
 * @param \XoopsObject $xoopsModule
38
 * @param null         $oldversion
39
 * @return bool
40
 */
41
function xoops_module_update_xnewsletter(\XoopsObject $xoopsModule, $oldversion = null)
0 ignored issues
show
Unused Code introduced by
The parameter $xoopsModule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
{
43
    if (100 == $oldversion) {
44
        xoops_module_update_xnewsletter_101();
45
    }
46
    if ($oldversion < 103) {
47
        xoops_module_update_xnewsletter_103();
48
    }
49
    if ($oldversion < 104) {
50
        xoops_module_update_xnewsletter_104();
51
    }
52
    if ($oldversion < 130) {
53
        xoops_module_update_xnewsletter_130();
54
    }
55
56
    return true;
57
}
58
59
/**
60
 * @return bool
61
 */
62
function xoops_module_update_xnewsletter_130()
63
{
64
    // change module dirname to lowercase
65
    $path    = dirname(__DIR__);
66
    $dirname = basename(dirname(__DIR__));
67
    rename($path, mb_strtolower($dirname));
68
    // update module dirname field in database to lowercase
69
    global $xoopsDB;
70
    $sql    = "UPDATE `{$xoopsDB->prefix('modules')}` SET `dirname` = '" . mb_strtolower($dirname) . "'";
71
    $sql    .= " WHERE LOWER(`dirname`) = '" . mb_strtolower($dirname) . "';";
72
    $result = $xoopsDB->queryF($sql);
73
    if (!$result) {
74
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": UPDATE 'modules' SET `dirname` = '" . mb_strtolower($dirname) . "'";
75
    }
76
77
    // reverse 'mod_' prefix on tables
78
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_accounts');
79
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_attachment');
80
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_bmh');
81
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_cat');
82
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_catsubscr');
83
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_import');
84
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_letter');
85
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_mailinglist');
86
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_protocol');
87
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_subscr');
88
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_task');
89
    $sql = sprintf('DROP TABLE IF EXISTS `' . $xoopsDB->prefix('xnewsletter_template') . '`');
0 ignored issues
show
Unused Code introduced by
$sql 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...
90
91
    // create 'xnewsletter_template' table
92
    global $xoopsDB;
93
    $sql    = sprintf('DROP TABLE IF EXISTS `' . $xoopsDB->prefix('xnewsletter_template') . '`');
94
    $result = $xoopsDB->queryF($sql);
95
    if (!$result) {
96
        echo '<br>' . _AM_XNEWSLETTER_UPGRADEFAILED . ": 'DROP TABLE 'xnewsletter_template'";
97
    }
98
99
    $sql    = '
100
        CREATE TABLE `' . $xoopsDB->prefix('xnewsletter_template') . "` (
101
        `template_id` INT (8)   NOT NULL  AUTO_INCREMENT,
102
        `template_title` VARCHAR (100)   NOT NULL DEFAULT '',
103
        `template_description` TEXT   NOT NULL DEFAULT '',
104
        `template_content` TEXT   NOT NULL DEFAULT '',
105
        `template_submitter` INT (8)   NOT NULL DEFAULT '0',
106
        `template_created` INT (8)   NOT NULL DEFAULT '0',
107
        PRIMARY KEY (`template_id`)
108
        ) ENGINE=MyISAM;";
109
    $result = $xoopsDB->queryF($sql);
110
    if (!$result) {
111
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": CREATE TABLE 'xnewsletter_template'";
112
    }
113
114
    // add fields to 'xnewsletter_cat' table
115
    global $xoopsDB;
116
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_cat') . '`';
117
    $sql    .= " ADD COLUMN `dohtml` tinyint(1) NOT NULL default '0',";
118
    $sql    .= " ADD COLUMN `dosmiley` tinyint(1) NOT NULL default '1',";
119
    $sql    .= " ADD COLUMN `doxcode` tinyint(1) NOT NULL default '1',";
120
    $sql    .= " ADD COLUMN `doimage` tinyint(1) NOT NULL default '1',";
121
    $sql    .= " ADD COLUMN `dobr` tinyint(1) NOT NULL default '1';";
122
    $result = $xoopsDB->queryF($sql);
123
    if (!$result) {
124
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_cat' ADD";
125
    }
126
127
    // add fields to 'xnewsletter_letter' table
128
    global $xoopsDB;
129
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_letter') . '`';
130
    $sql    .= " ADD COLUMN `letter_sender` int(8) NOT NULL default '0',";
131
    $sql    .= " ADD COLUMN `letter_sent` int(10) NOT NULL default '0';";
132
    $result = $xoopsDB->queryF($sql);
133
    if (!$result) {
134
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_letter' ADD";
135
    }
136
137
    // add fields to 'xnewsletter_attachment' table
138
    global $xoopsDB;
139
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_attachment') . '`';
140
    $sql    .= " ADD COLUMN `attachment_size` int(8) NOT NULL default '0',";
141
    $sql    .= " ADD COLUMN `attachment_mode` int(8) NOT NULL default '0';";
142
    $result = $xoopsDB->queryF($sql);
143
    if (!$result) {
144
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_attachment' ADD";
145
    }
146
    
147
    // update fields 'xnewsletter_attachment' table
148
    global $xoopsDB;
149
    $sql    = 'UPDATE `' . $xoopsDB->prefix('xnewsletter_letter') . '`';
150
    $sql    .= " SET `letter_sent` = '1';";
151
    $result = $xoopsDB->queryF($sql);
152
    if (!$result) {
153
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_attachment' ADD";
154
    }
155
156
    // delete old html template files
157
    $templateDirectory = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/templates/';
158
    $template_list     = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
159
    foreach ($template_list as $k => $v) {
160
        $fileinfo = new \SplFileInfo($templateDirectory . $v);
161
        if ('html' === $fileinfo->getExtension() && 'index.html' !== $fileinfo->getFilename()) {
162
            @unlink($templateDirectory . $v);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
163
        }
164
    }
165
    // Load class XoopsFile
166
    xoops_load('xoopsfile');
167
168
    //delete /images directory
169
    $imagesDirectory = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/images/';
170
    $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
171
    $folderHandler->delete($imagesDirectory);
172
173
    //delete /templates/style.css file
174
    $cssFile       = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/templates/style.css';
175
    $folderHandler = XoopsFile::getHandler('file', $cssFile);
176
    $folderHandler->delete($cssFile);
177
178
    //delete .html entries from the tpl table
179
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE LOWER(`tpl_module`) = '" . mb_strtolower($dirname) . "' AND `tpl_file` LIKE '%.html%'";
180
    $GLOBALS['xoopsDB']->queryF($sql);
181
182
    return true;
183
}
184
185
/**
186
 * @return bool
187
 */
188
function xoops_module_update_xnewsletter_104()
189
{
190
    global $xoopsDB;
191
192
    $sql    = sprintf('DROP TABLE IF EXISTS `' . $xoopsDB->prefix('mod_xnewsletter_task') . '`');
193
    $result = $xoopsDB->queryF($sql);
194
    if (!$result) {
195
        echo '<br>' . _AM_XNEWSLETTER_UPGRADEFAILED . ": 'DROP TABLE 'mod_xnewsletter_task'";
196
    }
197
198
    $sql    = sprintf('CREATE TABLE `' . $xoopsDB->prefix('mod_xnewsletter_task') . "` (
199
        `task_id` INT(8) NOT NULL AUTO_INCREMENT,
200
        `task_letter_id` INT(8) NOT NULL DEFAULT '0',
201
        `task_subscr_id` INT(8) NOT NULL DEFAULT '0',
202
        `task_starttime` INT(8) NOT NULL DEFAULT '0',
203
        `task_submitter` INT(8) NOT NULL DEFAULT '0',
204
        `task_created` INT(8) NOT NULL DEFAULT '0',
205
        PRIMARY KEY (`task_id`),
206
        KEY `idx_task_starttime` (`task_starttime`)
207
        ) ENGINE=MyISAM;");
208
    $result = $xoopsDB->queryF($sql);
209
    if (!$result) {
210
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": CREATE TABLE 'mod_xnewsletter_task'";
211
    }
212
213
    unlink(XOOPS_ROOT_PATH . '/modules/xnewsletter/include/sendletter.php');
214
215
    return true;
216
}
217
218
/**
219
 * @return bool
220
 */
221
function xoops_module_update_xnewsletter_103()
222
{
223
    global $xoopsDB;
224
225
    $sql    = sprintf('DROP TABLE IF EXISTS `' . $xoopsDB->prefix('mod_xnewsletter_import') . '`');
226
    $result = $xoopsDB->queryF($sql);
227
    if (!$result) {
228
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": 'DROP TABLE 'mod_xnewsletter_import'";
229
    }
230
231
    $sql    = sprintf('CREATE TABLE `' . $xoopsDB->prefix('mod_xnewsletter_import') . "` (
232
            `import_id` INT (8)   NOT NULL  AUTO_INCREMENT,
233
            `import_email` VARCHAR (100)   NOT NULL DEFAULT ' ',
234
            `import_firstname` VARCHAR (100)   NULL DEFAULT ' ',
235
            `import_lastname` VARCHAR (100)   NULL DEFAULT ' ',
236
            `import_sex` VARCHAR (100)   NULL DEFAULT ' ',
237
            `import_cat_id` INT (8)   NOT NULL DEFAULT '0',
238
            `import_subscr_id` INT (8)   NOT NULL DEFAULT '0',
239
            `import_catsubscr_id` INT (8)   NOT NULL DEFAULT '0',
240
            `import_status` TINYINT (1)   NOT NULL DEFAULT '0',
241
            PRIMARY KEY (`import_id`),
242
            KEY `idx_email` (`import_email`),
243
            KEY `idx_subscr_id` (`import_subscr_id`),
244
            KEY `idx_import_status` (`import_status`)
245
            ) ENGINE=MyISAM;");
246
    $result = $xoopsDB->queryF($sql);
247
    if (!$result) {
248
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": CREATE TABLE 'mod_xnewsletter_import'";
249
    }
250
251
    $sql    = sprintf('ALTER TABLE `' . $xoopsDB->prefix('mod_xnewsletter_subscr') . '` ADD INDEX `idx_subscr_email` ( `subscr_email` )');
252
    $result = $xoopsDB->queryF($sql);
253
    if (!$result) {
254
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ': ADD INDEX `idx_subscr_email`';
255
    }
256
257
    $sql    = sprintf('ALTER TABLE `' . $xoopsDB->prefix('mod_xnewsletter_catsubscr') . '` ADD UNIQUE `idx_subscription` ( `catsubscr_catid` , `catsubscr_subscrid` )');
258
    $result = $xoopsDB->queryF($sql);
259
    if (!$result) {
260
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ': ADD INDEX `idx_subscription`';
261
    }
262
263
    return true;
264
}
265
266
/**
267
 * @return bool
268
 */
269
function xoops_module_update_xnewsletter_101()
270
{
271
    global $xoopsDB;
272
273
    //rename tables to new xoops naming scheme
274
    xoops_module_update_xnewsletter_rename_table('xnewsletter_accounts');
275
    xoops_module_update_xnewsletter_rename_table('xnewsletter_cat');
276
    xoops_module_update_xnewsletter_rename_table('xnewsletter_subscr');
277
    xoops_module_update_xnewsletter_rename_table('xnewsletter_catsubscr');
278
    xoops_module_update_xnewsletter_rename_table('xnewsletter_letter');
279
    xoops_module_update_xnewsletter_rename_table('xnewsletter_protocol');
280
    xoops_module_update_xnewsletter_rename_table('xnewsletter_attachment');
281
    xoops_module_update_xnewsletter_rename_table('xnewsletter_mailinglist');
282
    xoops_module_update_xnewsletter_rename_table('xnewsletter_bmh');
283
284
    return true;
285
}
286
287
/**
288
 * @param string $tablename
289
 *
290
 * @return bool
291
 */
292 View Code Duplication
function xoops_module_update_xnewsletter_rename_table($tablename)
0 ignored issues
show
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...
293
{
294
    global $xoopsDB;
295
296
    if (tableExists($xoopsDB->prefix($tablename))) {
297
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix($tablename) . ' RENAME ' . $xoopsDB->prefix('mod_' . $tablename));
298
        $result = $xoopsDB->queryF($sql);
299
        if (!$result) {
300
            echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": RENAME table '" . $tablename . "'";
301
            //            ++$errors;
302
        }
303
    }
304
305
    return true;
306
}
307
308
/**
309
 * @param string $tablename
310
 *
311
 * @return bool
312
 */
313 View Code Duplication
function xoops_module_update_xnewsletter_rename_mod_table($tablename)
0 ignored issues
show
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...
314
{
315
    global $xoopsDB;
316
317
    if (tableExists($xoopsDB->prefix('mod_' . $tablename))) {
318
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('mod_' . $tablename) . ' RENAME ' . $xoopsDB->prefix($tablename));
319
        $result = $xoopsDB->queryF($sql);
320
        if (!$result) {
321
            echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": RENAME table '" . $tablename . "'";
322
            //            ++$errors;
323
        }
324
    }
325
326
    return true;
327
}
328
329
/**
330
 * @param $tablename
331
 *
332
 * @return bool
333
 */
334
function tableExists($tablename)
335
{
336
    global $xoopsDB;
337
    $result = $xoopsDB->queryF("SHOW TABLES LIKE '{$tablename}'");
338
339
    return ($xoopsDB->getRowsNum($result) > 0);
340
}
341