newbbIsModuleAdministrators()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 28
nc 3
nop 1
dl 0
loc 44
rs 9.1608
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * Newbb module
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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
 * @since           4.0
16
 * @author          Taiwen Jiang <[email protected]>
17
 */
18
19
use XoopsModules\Newbb\{
20
    Forum,
21
    Helper
22
};
23
24
/** @var Helper $helper */
25
/**
26
 * Function to a list of user names associated with their user IDs
27
 * @param mixed[]|int $uid
28
 * @param bool  $usereal
29
 * @param bool $linked
30
 * @return array
31
 */
32
function newbbGetUnameFromIds($uid, bool $usereal = false, bool $linked = false): array
33
{
34
    xoops_load('xoopsuserutility');
35
    $ids = \XoopsUserUtility::getUnameFromIds($uid, $usereal, $linked);
36
37
    return $ids;
38
}
39
40
/**
41
 * @param int  $uid
42
 * @param int  $usereal
43
 * @param bool $linked
44
 * @return string
45
 */
46
function newbbGetUnameFromId(int $uid, int $usereal = 0, bool $linked = false): string
47
{
48
    xoops_load('xoopsuserutility');
49
50
    return \XoopsUserUtility::getUnameFromId($uid, $usereal, $linked);
51
}
52
53
/**
54
 * Function to check if a user is an administrator of the module
55
 *
56
 * @param int|string|array|\XoopsUser $user
57
 * @param int                         $mid
58
 * @return bool
59
 */
60
function newbbIsAdministrator($user = -1, int $mid = 0): bool
61
{
62
    global $xoopsModule;
63
64
    if (is_numeric($user) && -1 === $user) {
65
        $user = $GLOBALS['xoopsUser'];
66
    }
67
    if (!is_object($user) && (int)$user < 1) {
68
        return false;
69
    }
70
    $uid = is_object($user) ? $user->getVar('uid') : (int)$user;
71
72
    if (!$mid) {
73
        if (is_object($xoopsModule) && 'newbb' === $xoopsModule->getVar('dirname', 'n')) {
74
            $mid = $xoopsModule->getVar('mid', 'n');
75
        } else {
76
            /** @var \XoopsModuleHandler $moduleHandler */
77
            $moduleHandler = xoops_getHandler('module');
78
            $newbb_module  = $moduleHandler->getByDirname('newbb');
79
            $mid           = $newbb_module->getVar('mid', 'n');
80
            unset($newbb_module);
81
        }
82
    }
83
84
    if (is_object($xoopsModule) && is_object($GLOBALS['xoopsUser']) && $mid == $xoopsModule->getVar('mid', 'n')
85
        && $uid == $GLOBALS['xoopsUser']->getVar('uid', 'n')) {
86
        return $GLOBALS['xoopsUserIsAdmin'];
87
    }
88
89
    /** @var \XoopsMemberHandler $memberHandler */
90
    $memberHandler = xoops_getHandler('member');
91
    $groups        = $memberHandler->getGroupsByUser($uid);
92
93
    /** @var \XoopsGroupPermHandler $grouppermHandler */
94
    $grouppermHandler = xoops_getHandler('groupperm');
95
96
    return $grouppermHandler->checkRight('module_admin', $mid, $groups);
97
}
98
99
/**
100
 * Function to check if a user is a moderator of a forum
101
 *
102
 * @param mixed $forum
103
 * @param int|array |string|\XoopsUser $user
104
 * @return bool
105
 */
106
function newbbIsModerator(&$forum, $user = -1): bool
107
{
108
    if (!is_object($forum)) {
109
        $forum_id = (int)$forum;
110
        if (0 === $forum_id) {
111
            return false;
112
        }
113
        $forumHandler = Helper::getInstance()->getHandler('Forum');
114
        $forum        = $forumHandler->get($forum_id);
115
    }
116
117
    if (is_numeric($user) && -1 == $user) {
118
        $user = $GLOBALS['xoopsUser'];
119
    }
120
    if (!is_object($user) && (int)$user < 1) {
121
        return false;
122
    }
123
    $uid = is_object($user) ? $user->getVar('uid', 'n') : (int)$user;
124
125
    return in_array($uid, $forum->getVar('forum_moderator'), true);
0 ignored issues
show
Bug introduced by
It seems like $forum->getVar('forum_moderator') can also be of type boolean and null and string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

125
    return in_array($uid, /** @scrutinizer ignore-type */ $forum->getVar('forum_moderator'), true);
Loading history...
126
}
127
128
/**
129
 * Function to check if a user has moderation permission over a forum
130
 *
131
 * @param Forum|int $forum
132
 * @return bool
133
 */
134
function newbbIsAdmin($forum = 0): bool
135
{
136
    global $xoopsModule;
137
    static $_cachedModerators;
138
139
    if (empty($forum)) {
140
        return $GLOBALS['xoopsUserIsAdmin'];
141
    }
142
143
    if (!is_object($GLOBALS['xoopsUser'])) {
144
        return false;
145
    }
146
147
    if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) {
148
        return true;
149
    }
150
151
    $cache_id = is_object($forum) ? $forum->getVar('forum_id', 'n') : (int)$forum;
152
    if (!isset($_cachedModerators[$cache_id])) {
153
        if (!is_object($forum)) {
154
            $forumHandler = Helper::getInstance()->getHandler('Forum');
155
            $forum        = $forumHandler->get((int)$forum);
156
        }
157
        $_cachedModerators[$cache_id] = $forum->getVar('forum_moderator');
158
    }
159
160
    return in_array($GLOBALS['xoopsUser']->getVar('uid'), $_cachedModerators[$cache_id], true);
161
}
162
163
/* use hardcoded DB query to save queries */
164
/**
165
 * @param array $uid
166
 *
167
 * @return array
168
 *
169
 * @psalm-return list<mixed>
170
 */
171
function newbbIsModuleAdministrators(array $uid = []): array
172
{
173
    global $xoopsModule;
174
    $module_administrators = [];
175
176
    //    $xoopsMembershipHandler = xoops_getHandler('membership');
177
    //    $xoopsMembershipTable   = $xoopsMembershipHandler->table;
178
179
    /** @var \XoopsMembershipHandler $xoopsMembershipHandler */
180
    $xoopsMembershipHandler = xoops_getHandler('membership');
181
    $xoopsMembershipTable   = $xoopsMembershipHandler->table;
182
    /** @var \XoopsGroupPermHandler $grouppermHandler */
183
    $grouppermHandler = xoops_getHandler('groupperm');
184
    $xoopsGroupPermTable   = $grouppermHandler->table;
185
186
    if (!$uid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uid of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
187
        return $module_administrators;
188
    }
189
    $mid = $xoopsModule->getVar('mid');
190
191
    $sql = 'SELECT COUNT(l.groupid) AS count, l.uid FROM '
192
           . $xoopsMembershipTable
193
           . ' AS l'
194
           . ' LEFT JOIN '
195
           . $xoopsGroupPermTable
196
           . ' AS p ON p.gperm_groupid=l.groupid'
197
           . ' WHERE l.uid IN ('
198
           . implode(', ', array_map('\intval', $uid))
199
           . ')'
200
           . "    AND p.gperm_modid = '1' AND p.gperm_name = 'module_admin' AND p.gperm_itemid = '"
201
           . (int)$mid
202
           . "'"
203
           . ' GROUP BY l.uid';
204
205
    $result = $GLOBALS['xoopsDB']->query($sql);
206
    if ($GLOBALS['xoopsDB']->isResultSet($result)) {
207
        while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
208
            if (!empty($myrow['count'])) {
209
                $module_administrators[] = $myrow['uid'];
210
            }
211
        }
212
    }
213
214
    return $module_administrators;
215
}
216
217
/* use hardcoded DB query to save queries */
218
/**
219
 * @param array $uid
220
 * @param int   $mid
221
 * @return array
222
 */
223
function newbbIsForumModerators(array $uid = [], int $mid = 0): array
0 ignored issues
show
Unused Code introduced by
The parameter $mid 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

223
function newbbIsForumModerators(array $uid = [], /** @scrutinizer ignore-unused */ int $mid = 0): array

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...
224
{
225
    $forum_moderators = [];
226
227
    if ($uid === []) {
228
        return $forum_moderators;
229
    }
230
231
    $sql    = 'SELECT forum_moderator FROM ' . $GLOBALS['xoopsDB']->prefix('newbb_forums');
232
    $result = $GLOBALS['xoopsDB']->query($sql);
233
    if ($GLOBALS['xoopsDB']->isResultSet($result)) {
234
        while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
235
            if (empty($myrow['forum_moderator'])) {
236
                continue;
237
            }
238
            $forum_moderators = array_merge($forum_moderators, (array)unserialize($myrow['forum_moderator']));
239
        }
240
    }
241
242
    return array_unique($forum_moderators??[]);
243
}
244
//ENDIF;
245