Completed
Pull Request — master (#29)
by Goffy
01:40
created

onupdate.php ➔ xoops_module_update_xnewsletter()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 32
nop 2
dl 0
loc 20
rs 8.9777
c 0
b 0
f 0
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
    if ($oldversion < 141) {
56
        xoops_module_update_xnewsletter_141();
57
    }
58
59
    return true;
60
}
61
62
/**
63
 * @return bool
64
 */
65
function xoops_module_update_xnewsletter_141()
66
{
67
    global $xoopsDB;
68
    
69
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_letter') . '`';
70
    $sql    .= " ADD COLUMN `letter_templateid` int(8) NOT NULL default '1' AFTER `letter_content`;";
71
    $result = $xoopsDB->queryF($sql);
72
    if (!$result) {
73
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_letter' ADD 'letter_templateid'";
74
    }
75
76
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_template') . '`';
77
    $sql    .= " ADD UNIQUE `template_title` (`template_title`);";
78
    $result = $xoopsDB->queryF($sql);
79
    if (!$result) {
80
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_template' ADD 'template_type'";
81
    }
82
83
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_template') . '`';
84
    $sql    .= " ADD COLUMN `template_type` tinyint(1) NOT NULL default '1';";
85
    $result = $xoopsDB->queryF($sql);
86
    if (!$result) {
87
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_template' ADD 'template_type'";
88
    }
89
    
90
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_template') . '`';
91
    $sql    .= " ADD COLUMN `template_online` tinyint(1) NOT NULL default '1';";
92
    $result = $xoopsDB->queryF($sql);
93
    if (!$result) {
94
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_template' ADD 'template_online'";
95
    }
96
    
97
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_protocol') . "` CHANGE `protocol_status_str_id` `protocol_status_str_id` TEXT NULL DEFAULT '';";
98
    $result = $xoopsDB->queryF($sql);
99
    if (!$result) {
100
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_protocol' CHANGE `protocol_status_str_id`";
101
    }
102
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_protocol') . "` CHANGE `protocol_status_vars` `protocol_status_vars` TEXT NULL DEFAULT '';";
103
    $result = $xoopsDB->queryF($sql);
104
    if (!$result) {
105
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_protocol' CHANGE `protocol_status_str_vars`";
106
    }
107
108
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_mailinglist') . '`';
109
    $sql    .= " ADD COLUMN `mailinglist_system` VARCHAR(100) NOT NULL  DEFAULT '',";
110
    $sql    .= " ADD COLUMN `mailinglist_target` VARCHAR(200) NOT NULL  DEFAULT '',";
111
    $sql    .= " ADD COLUMN `mailinglist_pwd` VARCHAR(100) NOT NULL  DEFAULT '',";
112
    $sql    .= " ADD COLUMN `mailinglist_notifyowner` tinyint(1) NOT NULL default '0';";
113
    $result = $xoopsDB->queryF($sql);
114
    if (!$result) {
115
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_mailinglist' ADD";
116
    }
117
118
    return true;
119
}
120
121
/**
122
 * @return bool
123
 */
124
function xoops_module_update_xnewsletter_130()
125
{
126
    // change module dirname to lowercase
127
    $path    = dirname(__DIR__);
128
    $dirname = basename(dirname(__DIR__));
129
    rename($path, mb_strtolower($dirname));
130
    // update module dirname field in database to lowercase
131
    global $xoopsDB;
132
    $sql    = "UPDATE `{$xoopsDB->prefix('modules')}` SET `dirname` = '" . mb_strtolower($dirname) . "'";
133
    $sql    .= " WHERE LOWER(`dirname`) = '" . mb_strtolower($dirname) . "';";
134
    $result = $xoopsDB->queryF($sql);
135
    if (!$result) {
136
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": UPDATE 'modules' SET `dirname` = '" . mb_strtolower($dirname) . "'";
137
    }
138
139
    // reverse 'mod_' prefix on tables
140
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_accounts');
141
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_attachment');
142
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_bmh');
143
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_cat');
144
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_catsubscr');
145
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_import');
146
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_letter');
147
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_mailinglist');
148
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_protocol');
149
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_subscr');
150
    xoops_module_update_xnewsletter_rename_mod_table('xnewsletter_task');
151
    $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...
152
153
    // create 'xnewsletter_template' table
154
    global $xoopsDB;
155
    $sql    = sprintf('DROP TABLE IF EXISTS `' . $xoopsDB->prefix('xnewsletter_template') . '`');
156
    $result = $xoopsDB->queryF($sql);
157
    if (!$result) {
158
        echo '<br>' . _AM_XNEWSLETTER_UPGRADEFAILED . ": 'DROP TABLE 'xnewsletter_template'";
159
    }
160
161
    $sql    = '
162
        CREATE TABLE `' . $xoopsDB->prefix('xnewsletter_template') . "` (
163
        `template_id` INT (8)   NOT NULL  AUTO_INCREMENT,
164
        `template_title` VARCHAR (100)   NOT NULL DEFAULT '',
165
        `template_description` TEXT   NOT NULL DEFAULT '',
166
        `template_content` TEXT   NOT NULL DEFAULT '',
167
        `template_submitter` INT (8)   NOT NULL DEFAULT '0',
168
        `template_created` INT (8)   NOT NULL DEFAULT '0',
169
        PRIMARY KEY (`template_id`)
170
        ) ENGINE=MyISAM;";
171
    $result = $xoopsDB->queryF($sql);
172
    if (!$result) {
173
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": CREATE TABLE 'xnewsletter_template'";
174
    }
175
176
    // add fields to 'xnewsletter_cat' table
177
    global $xoopsDB;
178
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_cat') . '`';
179
    $sql    .= " ADD COLUMN `dohtml` tinyint(1) NOT NULL default '0',";
180
    $sql    .= " ADD COLUMN `dosmiley` tinyint(1) NOT NULL default '1',";
181
    $sql    .= " ADD COLUMN `doxcode` tinyint(1) NOT NULL default '1',";
182
    $sql    .= " ADD COLUMN `doimage` tinyint(1) NOT NULL default '1',";
183
    $sql    .= " ADD COLUMN `dobr` tinyint(1) NOT NULL default '1';";
184
    $result = $xoopsDB->queryF($sql);
185
    if (!$result) {
186
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_cat' ADD";
187
    }
188
189
    // add fields to 'xnewsletter_letter' table
190
    global $xoopsDB;
191
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_letter') . '`';
192
    $sql    .= " ADD COLUMN `letter_sender` int(8) NOT NULL default '0',";
193
    $sql    .= " ADD COLUMN `letter_sent` int(10) NOT NULL default '0';";
194
    $result = $xoopsDB->queryF($sql);
195
    if (!$result) {
196
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_letter' ADD";
197
    }
198
199
    // add fields to 'xnewsletter_attachment' table
200
    global $xoopsDB;
201
    $sql    = 'ALTER TABLE `' . $xoopsDB->prefix('xnewsletter_attachment') . '`';
202
    $sql    .= " ADD COLUMN `attachment_size` int(8) NOT NULL default '0',";
203
    $sql    .= " ADD COLUMN `attachment_mode` int(8) NOT NULL default '0';";
204
    $result = $xoopsDB->queryF($sql);
205
    if (!$result) {
206
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_attachment' ADD";
207
    }
208
    
209
    // update fields 'xnewsletter_attachment' table
210
    global $xoopsDB;
211
    $sql    = 'UPDATE `' . $xoopsDB->prefix('xnewsletter_letter') . '`';
212
    $sql    .= " SET `letter_sent` = '1';";
213
    $result = $xoopsDB->queryF($sql);
214
    if (!$result) {
215
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": ALTER TABLE 'xnewsletter_attachment' ADD";
216
    }
217
218
    // delete old html template files
219
    $templateDirectory = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/templates/';
220
    $template_list     = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
221
    foreach ($template_list as $k => $v) {
222
        $fileinfo = new \SplFileInfo($templateDirectory . $v);
223
        if ('html' === $fileinfo->getExtension() && 'index.html' !== $fileinfo->getFilename()) {
224
            if (!@unlink($templateDirectory . $v)) {
225
                $e = error_get_last();
226
                trigger_error($e['message']);
227
            }
228
        }
229
    }
230
    // Load class XoopsFile
231
    xoops_load('xoopsfile');
232
233
    //delete /images directory
234
    $imagesDirectory = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/images/';
235
    /** @var \XoopsFolderHandler $folderHandler */
236
    $folderHandler = XoopsFile::getHandler('folder', $imagesDirectory);
237
    $folderHandler->delete($imagesDirectory);
238
239
    //delete /templates/style.css file
240
    $cssFile       = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/templates/style.css';
241
    /** @var \XoopsFileHandler $fileHandler */
242
    $fileHandler = XoopsFile::getHandler('file', $cssFile);
243
    $fileHandler->delete($cssFile);
244
245
    //delete .html entries from the tpl table
246
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE LOWER(`tpl_module`) = '" . mb_strtolower($dirname) . "' AND `tpl_file` LIKE '%.html%'";
247
    $GLOBALS['xoopsDB']->queryF($sql);
248
249
    return true;
250
}
251
252
/**
253
 * @return bool
254
 */
255
function xoops_module_update_xnewsletter_104()
256
{
257
    global $xoopsDB;
258
259
    $sql    = sprintf('DROP TABLE IF EXISTS `' . $xoopsDB->prefix('mod_xnewsletter_task') . '`');
260
    $result = $xoopsDB->queryF($sql);
261
    if (!$result) {
262
        echo '<br>' . _AM_XNEWSLETTER_UPGRADEFAILED . ": 'DROP TABLE 'mod_xnewsletter_task'";
263
    }
264
265
    $sql    = sprintf('CREATE TABLE `' . $xoopsDB->prefix('mod_xnewsletter_task') . "` (
266
        `task_id` INT(8) NOT NULL AUTO_INCREMENT,
267
        `task_letter_id` INT(8) NOT NULL DEFAULT '0',
268
        `task_subscr_id` INT(8) NOT NULL DEFAULT '0',
269
        `task_starttime` INT(8) NOT NULL DEFAULT '0',
270
        `task_submitter` INT(8) NOT NULL DEFAULT '0',
271
        `task_created` INT(8) NOT NULL DEFAULT '0',
272
        PRIMARY KEY (`task_id`),
273
        KEY `idx_task_starttime` (`task_starttime`)
274
        ) ENGINE=MyISAM;");
275
    $result = $xoopsDB->queryF($sql);
276
    if (!$result) {
277
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": CREATE TABLE 'mod_xnewsletter_task'";
278
    }
279
280
    if (!@unlink(XOOPS_ROOT_PATH . '/modules/xnewsletter/include/sendletter.php')) {
281
        $e = error_get_last();
282
        trigger_error($e['message']);
283
    }
284
285
    return true;
286
}
287
288
/**
289
 * @return bool
290
 */
291
function xoops_module_update_xnewsletter_103()
292
{
293
    global $xoopsDB;
294
295
    $sql    = sprintf('DROP TABLE IF EXISTS `' . $xoopsDB->prefix('mod_xnewsletter_import') . '`');
296
    $result = $xoopsDB->queryF($sql);
297
    if (!$result) {
298
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": 'DROP TABLE 'mod_xnewsletter_import'";
299
    }
300
301
    $sql    = sprintf('CREATE TABLE `' . $xoopsDB->prefix('mod_xnewsletter_import') . "` (
302
            `import_id` INT (8)   NOT NULL  AUTO_INCREMENT,
303
            `import_email` VARCHAR (100)   NOT NULL DEFAULT ' ',
304
            `import_firstname` VARCHAR (100)   NULL DEFAULT ' ',
305
            `import_lastname` VARCHAR (100)   NULL DEFAULT ' ',
306
            `import_sex` VARCHAR (100)   NULL DEFAULT ' ',
307
            `import_cat_id` INT (8)   NOT NULL DEFAULT '0',
308
            `import_subscr_id` INT (8)   NOT NULL DEFAULT '0',
309
            `import_catsubscr_id` INT (8)   NOT NULL DEFAULT '0',
310
            `import_status` TINYINT (1)   NOT NULL DEFAULT '0',
311
            PRIMARY KEY (`import_id`),
312
            KEY `idx_email` (`import_email`),
313
            KEY `idx_subscr_id` (`import_subscr_id`),
314
            KEY `idx_import_status` (`import_status`)
315
            ) ENGINE=MyISAM;");
316
    $result = $xoopsDB->queryF($sql);
317
    if (!$result) {
318
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": CREATE TABLE 'mod_xnewsletter_import'";
319
    }
320
321
    $sql    = sprintf('ALTER TABLE `' . $xoopsDB->prefix('mod_xnewsletter_subscr') . '` ADD INDEX `idx_subscr_email` ( `subscr_email` )');
322
    $result = $xoopsDB->queryF($sql);
323
    if (!$result) {
324
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ': ADD INDEX `idx_subscr_email`';
325
    }
326
327
    $sql    = sprintf('ALTER TABLE `' . $xoopsDB->prefix('mod_xnewsletter_catsubscr') . '` ADD UNIQUE `idx_subscription` ( `catsubscr_catid` , `catsubscr_subscrid` )');
328
    $result = $xoopsDB->queryF($sql);
329
    if (!$result) {
330
        echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ': ADD INDEX `idx_subscription`';
331
    }
332
333
    return true;
334
}
335
336
/**
337
 * @return bool
338
 */
339
function xoops_module_update_xnewsletter_101()
340
{
341
    global $xoopsDB;
342
343
    //rename tables to new xoops naming scheme
344
    xoops_module_update_xnewsletter_rename_table('xnewsletter_accounts');
345
    xoops_module_update_xnewsletter_rename_table('xnewsletter_cat');
346
    xoops_module_update_xnewsletter_rename_table('xnewsletter_subscr');
347
    xoops_module_update_xnewsletter_rename_table('xnewsletter_catsubscr');
348
    xoops_module_update_xnewsletter_rename_table('xnewsletter_letter');
349
    xoops_module_update_xnewsletter_rename_table('xnewsletter_protocol');
350
    xoops_module_update_xnewsletter_rename_table('xnewsletter_attachment');
351
    xoops_module_update_xnewsletter_rename_table('xnewsletter_mailinglist');
352
    xoops_module_update_xnewsletter_rename_table('xnewsletter_bmh');
353
354
    return true;
355
}
356
357
/**
358
 * @param string $tablename
359
 *
360
 * @return bool
361
 */
362 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...
363
{
364
    global $xoopsDB;
365
366
    if (tableExists($xoopsDB->prefix($tablename))) {
367
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix($tablename) . ' RENAME ' . $xoopsDB->prefix('mod_' . $tablename));
368
        $result = $xoopsDB->queryF($sql);
369
        if (!$result) {
370
            echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": RENAME table '" . $tablename . "'";
371
            //            ++$errors;
372
        }
373
    }
374
375
    return true;
376
}
377
378
/**
379
 * @param string $tablename
380
 *
381
 * @return bool
382
 */
383 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...
384
{
385
    global $xoopsDB;
386
387
    if (tableExists($xoopsDB->prefix('mod_' . $tablename))) {
388
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('mod_' . $tablename) . ' RENAME ' . $xoopsDB->prefix($tablename));
389
        $result = $xoopsDB->queryF($sql);
390
        if (!$result) {
391
            echo '<br>' . _MI_XNEWSLETTER_UPGRADEFAILED . ": RENAME table '" . $tablename . "'";
392
            //            ++$errors;
393
        }
394
    }
395
396
    return true;
397
}
398
399
/**
400
 * @param $tablename
401
 *
402
 * @return bool
403
 */
404
function tableExists($tablename)
405
{
406
    global $xoopsDB;
407
    $result = $xoopsDB->queryF("SHOW TABLES LIKE '{$tablename}'");
408
409
    return ($xoopsDB->getRowsNum($result) > 0);
410
}
411