Completed
Pull Request — master (#27)
by Michael
01:42
created

onupdate.php ➔ xoops_module_update_xnewsletter_130()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 113

Duplication

Lines 0
Ratio 0 %

Importance

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