Completed
Push — master ( 05d2ac...b00d44 )
by Richard
34s queued 30s
created

xoops_module_update_profile()   D

Complexity

Conditions 16
Paths 64

Size

Total Lines 132
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 80
nc 64
nop 2
dl 0
loc 132
rs 4.8896
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
 * Extended User Profile
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             profile
15
 * @since               2.3.0
16
 * @author              Taiwen Jiang <[email protected]>
17
 */
18
19
$path = dirname(dirname(dirname(__DIR__)));
20
require_once $path . '/include' . '/cp_header.php';
21
22
/**
23
 * @param      $module
24
 * @param null $oldversion
25
 * @return bool
26
 */
27
/**
28
 * @param      $module
29
 * @param null $oldversion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $oldversion is correct as it would always require null to be passed?
Loading history...
30
 * @return bool
31
 */
32
function xoops_module_update_profile(XoopsModule $module, $oldversion = null)
33
{
34
    if ($oldversion < '1.6.2') {
35
        $GLOBALS['xoopsDB']->queryF('UPDATE `' . $GLOBALS['xoopsDB']->prefix('profile_field') . ' SET field_valuetype=2 WHERE field_name=umode');
36
    }
37
38
    if ($oldversion < '1.0.0') {
39
40
        // Drop old category table
41
        $sql = 'DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('profile_category');
42
        $GLOBALS['xoopsDB']->queryF($sql);
43
44
        // Drop old field-category link table
45
        $sql = 'DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('profile_fieldcategory');
46
        $GLOBALS['xoopsDB']->queryF($sql);
47
48
        // Create new tables for new profile module
49
        $GLOBALS['xoopsDB']->queryFromFile(XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname', 'n') . '/sql/mysql.sql');
50
51
        include_once __DIR__ . '/install.php';
52
        xoops_module_install_profile($module);
53
        /* @var XoopsGroupPermHandler $goupperm_handler */
54
        $goupperm_handler = xoops_getHandler('groupperm');
55
56
        $field_handler = xoops_getModuleHandler('field', $module->getVar('dirname', 'n'));
57
        $skip_fields   = $field_handler->getUserVars();
0 ignored issues
show
Bug introduced by
The method getUserVars() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

57
        /** @scrutinizer ignore-call */ 
58
        $skip_fields   = $field_handler->getUserVars();
Loading history...
58
        $skip_fields[] = 'newemail';
59
        $skip_fields[] = 'pm_link';
60
        $sql           = 'SELECT * FROM `' . $GLOBALS['xoopsDB']->prefix('user_profile_field') . "` WHERE `field_name` NOT IN ('" . implode("', '", $skip_fields) . "')";
61
        $result        = $GLOBALS['xoopsDB']->query($sql);
62
        $fields        = array();
63
        while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
64
            $fields[] = $myrow['field_name'];
65
            $object   = $field_handler->create();
66
            $object->setVars($myrow, true);
67
            $object->setVar('cat_id', 1);
68
            if (!empty($myrow['field_register'])) {
69
                $object->setVar('step_id', 2);
70
            }
71
            if (!empty($myrow['field_options'])) {
72
                $object->setVar('field_options', unserialize($myrow['field_options']));
73
            }
74
            $field_handler->insert($object, true);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObjectHandler::insert() has too many arguments starting with true. ( Ignorable by Annotation )

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

74
            $field_handler->/** @scrutinizer ignore-call */ 
75
                            insert($object, true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
75
76
            $gperm_itemid = $object->getVar('field_id');
77
            $sql          = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('group_permission') . ' SET gperm_itemid = ' . $gperm_itemid . '   WHERE gperm_itemid = ' . $myrow['fieldid'] . '       AND gperm_modid = ' . $module->getVar('mid') . "       AND gperm_name IN ('profile_edit', 'profile_search')";
78
            $GLOBALS['xoopsDB']->queryF($sql);
79
80
            $groups_visible = $goupperm_handler->getGroupIds('profile_visible', $myrow['fieldid'], $module->getVar('mid'));
81
            $groups_show    = $goupperm_handler->getGroupIds('profile_show', $myrow['fieldid'], $module->getVar('mid'));
82
            foreach ($groups_visible as $ugid) {
83
                foreach ($groups_show as $pgid) {
84
                    $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('profile_visibility') . ' (field_id, user_group, profile_group) ' . ' VALUES ' . " ({$gperm_itemid}, {$ugid}, {$pgid})";
85
                    $GLOBALS['xoopsDB']->queryF($sql);
86
                }
87
            }
88
89
            //profile_install_setPermissions($object->getVar('field_id'), $module->getVar('mid'), $canedit, $visible);
90
            unset($object);
91
        }
92
93
        // Copy data from profile table
94
        foreach ($fields as $field) {
95
            $GLOBALS['xoopsDB']->queryF('UPDATE `' . $GLOBALS['xoopsDB']->prefix('profile_profile') . '` u, `' . $GLOBALS['xoopsDB']->prefix('user_profile') . "` p SET u.{$field} = p.{$field} WHERE u.profile_id=p.profileid");
96
        }
97
98
        // Drop old profile table
99
        $sql = 'DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('user_profile');
100
        $GLOBALS['xoopsDB']->queryF($sql);
101
102
        // Drop old field module
103
        $sql = 'DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('user_profile_field');
104
        $GLOBALS['xoopsDB']->queryF($sql);
105
106
        // Remove not used items
107
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('group_permission') . '   WHERE `gperm_modid` = ' . $module->getVar('mid') . " AND `gperm_name` IN ('profile_show', 'profile_visible')";
108
        $GLOBALS['xoopsDB']->queryF($sql);
109
    }
110
111
    if ($oldversion < '1.6.2') {
112
        $GLOBALS['xoopsDB']->queryF('UPDATE `' . $GLOBALS['xoopsDB']->prefix('profile_field') . "` SET `field_valuetype`=1 WHERE `field_name`='umode'");
113
    }
114
115
    if ($oldversion < '1.8.6') {
116
        // delete old html template files
117
        $templateDirectory = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname', 'n') . '/templates/';
118
        $template_list     = array_diff(scandir($templateDirectory), array('..', '.'));
119
        foreach ($template_list as $k => $v) {
120
            $fileinfo = new SplFileInfo($templateDirectory . $v);
121
            if ($fileinfo->getExtension() === 'html' && $fileinfo->getFilename() !== 'index.html') {
122
                @unlink($templateDirectory . $v);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

122
                /** @scrutinizer ignore-unhandled */ @unlink($templateDirectory . $v);

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...
123
            }
124
        }
125
126
        xoops_load('xoopsfile');
127
        //delete /images directory
128
        $imagesDirectory = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname', 'n') . '/images/';
129
        $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
130
        $folderHandler->delete($imagesDirectory);
131
        //delete /templates/style.css file
132
        $cssFile       = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname', 'n') . '/templates/style.css';
133
        $folderHandler = XoopsFile::getHandler('file', $cssFile);
134
        $folderHandler->delete($cssFile);
0 ignored issues
show
Unused Code introduced by
The call to XoopsFileHandler::delete() has too many arguments starting with $cssFile. ( Ignorable by Annotation )

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

134
        $folderHandler->/** @scrutinizer ignore-call */ 
135
                        delete($cssFile);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
135
        //delete .html entries from the tpl table
136
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
137
        $GLOBALS['xoopsDB']->queryF($sql);
138
    }
139
140
    if ($oldversion < '1.8.8') {
141
        // update user_sig field to use dhtml editor
142
        $tables = new Xmf\Database\Tables();
143
        $tables->useTable('profile_field');
144
        $criteria = new Criteria('field_name', 'user_sig', '=');
145
        $tables->update('profile_field', array('field_type' => 'dhtml'), $criteria);
146
        $tables->executeQueue(true);
147
    }
148
149
    if ($oldversion < '1.9.2') {
150
        // decrease field_name field's size from 200 to 64
151
        $sql          = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('profile_field') . " CHANGE `field_name` `field_name` VARCHAR(64) NOT NULL DEFAULT ''";
152
        $GLOBALS['xoopsDB']->queryF($sql);
153
154
    }
155
156
    $profile_handler = xoops_getModuleHandler('profile', $module->getVar('dirname', 'n'));
157
    $profile_handler->cleanOrphan($GLOBALS['xoopsDB']->prefix('users'), 'uid', 'profile_id');
0 ignored issues
show
Bug introduced by
The method cleanOrphan() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

157
    $profile_handler->/** @scrutinizer ignore-call */ 
158
                      cleanOrphan($GLOBALS['xoopsDB']->prefix('users'), 'uid', 'profile_id');
Loading history...
158
    $field_handler = xoops_getModuleHandler('field', $module->getVar('dirname', 'n'));
159
    $user_fields   = $field_handler->getUserVars();
160
    $criteria      = new Criteria('field_name', "('" . implode("', '", $user_fields) . "')", 'IN');
161
    $field_handler->updateAll('field_config', 0, $criteria);
0 ignored issues
show
Bug introduced by
The method updateAll() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

161
    $field_handler->/** @scrutinizer ignore-call */ 
162
                    updateAll('field_config', 0, $criteria);
Loading history...
162
163
    return true;
164
}
165