Passed
Push — master ( dcfdf8...4f44e2 )
by Richard
05:14 queued 13s
created

form_user()   D

Complexity

Conditions 12
Paths 224

Size

Total Lines 171
Code Lines 139

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 139
nc 224
nop 2
dl 0
loc 171
rs 4.6666
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    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
use Xmf\Request;
20
// Check users rights
21
if (!is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid())) {
22
    exit(_NOPERM);
23
}
24
25
//  Check is active
26
if (!xoops_getModuleOption('active_users', 'system')) {
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

26
if (!/** @scrutinizer ignore-deprecated */ xoops_getModuleOption('active_users', 'system')) {
Loading history...
27
    redirect_header('admin.php', 2, _AM_SYSTEM_NOTACTIVE);
28
}
29
30
/*********************************************************/
31
/* Users Functions                                       */
32
/*********************************************************/
33
include_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
34
include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
35
36
//Display form add or edit
37
//true = add and false = edit
38
/**
39
 * @param        $add_or_edit
40
 * @param string $user
41
 */
42
function form_user($add_or_edit, $user = '')
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

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

42
function form_user($add_or_edit, /** @scrutinizer ignore-unused */ $user = '')

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

Loading history...
43
{
44
    global $xoopsConfig, $xoopsUser;
45
    $uid = Request::getInt('uid', 0);
46
47
    //RMV-NOTIFY
48
    include_once XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/notification.php';
49
    include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
50
51
    if ($add_or_edit === true) {
52
        /** @var XoopsConfigHandler $config_handler */
53
        $config_handler  = xoops_getHandler('config');
54
        $xoopsConfigUser = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
55
        //Add user
56
        $uid_value        = '';
57
        $uname_value      = '';
58
        $name_value       = '';
59
        $email_value      = '';
60
        $email_cbox_value = 0;
61
        $url_value        = '';
62
        $timezone_value   = $xoopsConfig['default_TZ'];
63
        $icq_value        = '';
64
        $aim_value        = '';
65
        $yim_value        = '';
66
        $msnm_value       = '';
67
        $location_value   = '';
68
        $occ_value        = '';
69
        $interest_value   = '';
70
        $sig_value        = '';
71
        $sig_cbox_value   = 0;
72
        $umode_value      = $xoopsConfig['com_mode'];
73
        $uorder_value     = $xoopsConfig['com_order'];
74
        // RMV-NOTIFY
75
        $notify_method_value = (isset($xoopsConfigUser['default_notification']) ? $xoopsConfigUser['default_notification'] : XOOPS_NOTIFICATION_METHOD_PM);
76
        $notify_mode_value   = XOOPS_NOTIFICATION_MODE_SENDALWAYS;
77
        $bio_value           = '';
78
        $rank_value          = 0;
79
        $mailok_value        = 0;
80
        $form_title          = _AM_SYSTEM_USERS_ADDUSER;
81
        $form_isedit         = false;
82
        $groups              = array(XOOPS_GROUP_USERS);
83
    } else {
84
        //Edit user
85
        /** @var XoopsMemberHandler $member_handler */
86
        $member_handler = xoops_getHandler('member');
87
        $user           = $member_handler->getUser($uid);
88
        if (is_object($user)) {
89
            $uid_value        = $uid;
90
            $uname_value      = $user->getVar('uname', 'E');
91
            $name_value       = $user->getVar('name', 'E');
92
            $email_value      = $user->getVar('email', 'E');
93
            $email_cbox_value = $user->getVar('user_viewemail') ? 1 : 0;
94
            $url_value        = $user->getVar('url', 'E');
95
            $temp             = $user->getVar('theme');
0 ignored issues
show
Unused Code introduced by
The assignment to $temp is dead and can be removed.
Loading history...
96
            $timezone_value   = $user->getVar('timezone_offset');
97
            $icq_value        = $user->getVar('user_icq', 'E');
98
            $aim_value        = $user->getVar('user_aim', 'E');
99
            $yim_value        = $user->getVar('user_yim', 'E');
100
            $msnm_value       = $user->getVar('user_msnm', 'E');
101
            $location_value   = $user->getVar('user_from', 'E');
102
            $occ_value        = $user->getVar('user_occ', 'E');
103
            $interest_value   = $user->getVar('user_intrest', 'E');
104
            $sig_value        = $user->getVar('user_sig', 'E');
105
            $sig_cbox_value   = ($user->getVar('attachsig') == 1) ? 1 : 0;
106
            $umode_value      = $user->getVar('umode');
107
            $uorder_value     = $user->getVar('uorder');
108
            // RMV-NOTIFY
109
            $notify_method_value = $user->getVar('notify_method');
110
            $notify_mode_value   = $user->getVar('notify_mode');
111
            $bio_value           = $user->getVar('bio', 'E');
112
            $rank_value          = $user->rank(false);
0 ignored issues
show
Unused Code introduced by
The call to XoopsUser::rank() has too many arguments starting with false. ( Ignorable by Annotation )

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

112
            /** @scrutinizer ignore-call */ 
113
            $rank_value          = $user->rank(false);

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...
113
            $mailok_value        = $user->getVar('user_mailok', 'E');
114
            $form_title          = _AM_SYSTEM_USERS_UPDATEUSER . ': ' . $user->getVar('uname');
115
            $form_isedit         = true;
116
            $groups              = array_values($user->getGroups());
117
        }
118
    }
119
120
    //Affichage du formulaire
121
    $form = new XoopsThemeForm($form_title, 'form_user', 'admin.php', 'post', true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $form_title does not seem to be defined for all execution paths leading up to this point.
Loading history...
122
123
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_NICKNAME, 'username', 25, 25, $uname_value), true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uname_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
124
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_NAME, 'name', 30, 60, $name_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
125
    $email_tray = new XoopsFormElementTray(_AM_SYSTEM_USERS_EMAIL, '<br>');
126
    $email_text = new XoopsFormText('', 'email', 30, 60, $email_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
127
    $email_tray->addElement($email_text, true);
128
    $email_cbox = new XoopsFormCheckBox('', 'user_viewemail', $email_cbox_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email_cbox_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
129
    $email_cbox->addOption(1, _AM_SYSTEM_USERS_AOUTVTEAD);
130
    $email_tray->addElement($email_cbox);
131
    $form->addElement($email_tray, true);
132
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_URL, 'url', 30, 100, $url_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
133
    $form->addElement(new XoopsFormSelectTimezone(_AM_SYSTEM_USERS_TIMEZONE, 'timezone_offset', $timezone_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $timezone_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
134
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_ICQ, 'user_icq', 15, 15, $icq_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $icq_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
135
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_AIM, 'user_aim', 18, 18, $aim_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $aim_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
136
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_YIM, 'user_yim', 25, 25, $yim_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $yim_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
137
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_MSNM, 'user_msnm', 30, 100, $msnm_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $msnm_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
138
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_LOCATION, 'user_from', 30, 100, $location_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $location_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
139
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_OCCUPATION, 'user_occ', 30, 100, $occ_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $occ_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
140
    $form->addElement(new XoopsFormText(_AM_SYSTEM_USERS_INTEREST, 'user_intrest', 30, 150, $interest_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $interest_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
141
    $sig_tray  = new XoopsFormElementTray(_AM_SYSTEM_USERS_SIGNATURE, '<br>');
142
    $sig_tarea = new XoopsFormTextArea('', 'user_sig', $sig_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sig_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
143
    $sig_tray->addElement($sig_tarea);
144
    $sig_cbox = new XoopsFormCheckBox('', 'attachsig', $sig_cbox_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sig_cbox_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
145
    $sig_cbox->addOption(1, _AM_SYSTEM_USERS_SHOWSIG);
146
    $sig_tray->addElement($sig_cbox);
147
    $form->addElement($sig_tray);
148
    $umode_select = new XoopsFormSelect(_AM_SYSTEM_USERS_CDISPLAYMODE, 'umode', $umode_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $umode_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
149
    $umode_select->addOptionArray(array('nest' => _NESTED, 'flat' => _FLAT, 'thread' => _THREADED));
150
    $form->addElement($umode_select);
151
    $uorder_select = new XoopsFormSelect(_AM_SYSTEM_USERS_CSORTORDER, 'uorder', $uorder_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uorder_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
152
    $uorder_select->addOptionArray(array('0' => _OLDESTFIRST, '1' => _NEWESTFIRST));
153
    $form->addElement($uorder_select);
154
    // RMV-NOTIFY
155
    $notify_method_select = new XoopsFormSelect(_NOT_NOTIFYMETHOD, 'notify_method', $notify_method_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $notify_method_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
156
    $notify_method_select->addOptionArray(array(
157
                                              XOOPS_NOTIFICATION_METHOD_DISABLE => _NOT_METHOD_DISABLE,
158
                                              XOOPS_NOTIFICATION_METHOD_PM      => _NOT_METHOD_PM,
159
                                              XOOPS_NOTIFICATION_METHOD_EMAIL   => _NOT_METHOD_EMAIL));
160
    $form->addElement($notify_method_select);
161
    $notify_mode_select = new XoopsFormSelect(_NOT_NOTIFYMODE, 'notify_mode', $notify_mode_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $notify_mode_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
162
    $notify_mode_select->addOptionArray(array(
163
                                            XOOPS_NOTIFICATION_MODE_SENDALWAYS         => _NOT_MODE_SENDALWAYS,
164
                                            XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE => _NOT_MODE_SENDONCE,
165
                                            XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT   => _NOT_MODE_SENDONCEPERLOGIN));
166
    $form->addElement($notify_mode_select);
167
    $form->addElement(new XoopsFormTextArea(_AM_SYSTEM_USERS_EXTRAINFO, 'bio', $bio_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $bio_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
168
    $rank_select = new XoopsFormSelect(_AM_SYSTEM_USERS_RANK, 'rank', $rank_value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rank_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
169
    $ranklist    = XoopsLists::getUserRankList();
170
    if (count($ranklist) > 0) {
171
        $rank_select->addOption(0, '--------------');
172
        $rank_select->addOptionArray($ranklist);
173
    } else {
174
        $rank_select->addOption(0, _AM_SYSTEM_USERS_NSRID);
175
    }
176
    $form->addElement($rank_select);
177
    // adding a new user requires password fields
178
    if (!$form_isedit) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $form_isedit does not seem to be defined for all execution paths leading up to this point.
Loading history...
179
        $form->addElement(new XoopsFormPassword(_AM_SYSTEM_USERS_PASSWORD, 'password', 10, 32), true);
180
        $form->addElement(new XoopsFormPassword(_AM_SYSTEM_USERS_RETYPEPD, 'pass2', 10, 32), true);
181
    } else {
182
        $form->addElement(new XoopsFormPassword(_AM_SYSTEM_USERS_PASSWORD, 'password', 10, 32));
183
        $form->addElement(new XoopsFormPassword(_AM_SYSTEM_USERS_RETYPEPD, 'pass2', 10, 32));
184
    }
185
    $form->addElement(new XoopsFormRadioYN(_AM_SYSTEM_USERS_ACCEPT_EMAIL, 'user_mailok', $mailok_value));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mailok_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
186
187
    //Groups administration addition XOOPS 2.0.9: Mith
188
    /** @var  XoopsGroupPermHandler $gperm_handler */
189
    $gperm_handler = xoops_getHandler('groupperm');
190
    //If user has admin rights on groups
191
    if ($gperm_handler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $xoopsUser->getGroups(), 1)) {
192
        //add group selection
193
        $group_select[] = new XoopsFormSelectGroup(_AM_SYSTEM_USERS_GROUPS, 'groups', false, $groups, 5, true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$group_select was never initialized. Although not strictly required by PHP, it is generally a good practice to add $group_select = array(); before regardless.
Loading history...
Comprehensibility Best Practice introduced by
The variable $groups does not seem to be defined for all execution paths leading up to this point.
Loading history...
194
    } else {
195
        //add each user groups
196
        foreach ($groups as $key => $group) {
197
            $group_select[] = new XoopsFormHidden('groups[' . $key . ']', $group);
198
        }
199
    }
200
    foreach ($group_select as $group) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $group_select does not seem to be defined for all execution paths leading up to this point.
Loading history...
201
        $form->addElement($group);
202
        unset($group);
203
    }
204
205
    $form->addElement(new XoopsFormHidden('fct', 'users'));
206
    $form->addElement(new XoopsFormHidden('op', 'users_save'));
207
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
208
209
    if (!empty($uid_value)) {
210
        $form->addElement(new XoopsFormHidden('uid', $uid_value));
211
    }
212
    $form->display();
213
}
214
215
/**
216
 * @param $uid
217
 * @param $type
218
 */
219
function synchronize($uid, $type)
220
{
221
    global $xoopsDB;
222
223
    include_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
224
    include_once XOOPS_ROOT_PATH . '/kernel/module.php';
225
226
    $tables = array();
227
    // Count comments (approved only: com_status == XOOPS_COMMENT_ACTIVE)
228
    $tables[] = array('table_name' => 'xoopscomments', 'uid_column' => 'com_uid', 'criteria' => new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
229
    // Count Content posts
230
    if (XoopsModule::getByDirname('fmcontent')) {
231
        $tables[] = array('table_name' => 'fmcontent_content', 'uid_column' => 'content_uid');
232
    }
233
    // Count forum posts
234
    if (XoopsModule::getByDirname('newbb')) {
235
        // Added support for NewBB 5.0 new table naming convention
236
        $tableTest = new \Xmf\Database\Tables();
237
        if($tableTest->useTable('newbb_posts')) {
238
            $tables[] = array('table_name' => 'newbb_posts', 'uid_column' => 'uid');
239
        } else {
240
            $tables[] = array('table_name' => 'bb_posts', 'uid_column' => 'uid');
241
        }
242
    }
243
244
    switch ($type) {
245
        case 'user':
246
            $total_posts = 0;
247
            foreach ($tables as $table) {
248
                $criteria = new CriteriaCompo();
249
                $criteria->add(new Criteria($table['uid_column'], $uid));
250
                if (!empty($table['criteria'])) {
251
                    $criteria->add($table['criteria']);
252
                }
253
                $sql = 'SELECT COUNT(*) AS total FROM ' . $xoopsDB->prefix($table['table_name']) . ' ' . $criteria->renderWhere();
254
                $result = $xoopsDB->query($sql);
255
                if ($xoopsDB->isResultSet($result)) {
256
                    if ($row = $xoopsDB->fetchArray($result)) {
257
                        $total_posts += $row['total'];
258
                    }
259
                }
260
            }
261
            $sql = 'UPDATE ' . $xoopsDB->prefix('users') . " SET posts = '" . $total_posts . "' WHERE uid = '" . $uid . "'";
262
            $result = $xoopsDB->queryF($sql);
263
            if (!$xoopsDB->isResultSet($result)) {
264
                redirect_header('admin.php?fct=users', 1, _AM_SYSTEM_USERS_CNUUSER);
265
            }
266
            break;
267
268
        case 'all users':
269
            $sql = 'SELECT uid FROM ' . $xoopsDB->prefix('users') . '';
270
            $result = $xoopsDB->query($sql);
271
            if (!$xoopsDB->isResultSet($result)) {
272
                redirect_header('admin.php?fct=users', 1, sprintf(_AM_SYSTEM_USERS_CNGUSERID, $uid));
273
            }
274
275
            while (false !== ($data = $xoopsDB->fetchArray($result))) {
276
                synchronize($data['uid'], 'user');
277
            }
278
            break;
279
    }
280
281
    // exit();
282
}
283