Completed
Push — master ( 4e8684...61995d )
by Michael
08:37
created

functions.user.php ➔ newbbIsAdmin()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 9
nop 1
dl 0
loc 28
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 * Newbb module
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       XOOPS Project (http://xoops.org)
13
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package         newbb
15
 * @since           4.0
16
 * @author          Taiwen Jiang <[email protected]>
17
 */
18
19
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
20
21
/**
22
 * Function to a list of user names associated with their user IDs
23
 * @param        $uid
24
 * @param  int   $usereal
25
 * @param  bool  $linked
26
 * @return array
27
 */
28
function newbbGetUnameFromIds($uid, $usereal = 0, $linked = false)
29
{
30
    xoops_load('xoopsuserutility');
31
    $ids = XoopsUserUtility::getUnameFromIds($uid, $usereal, $linked);
32
33
    return $ids;
34
}
35
36
/**
37
 * @param         $uid
38
 * @param  int    $usereal
39
 * @param  bool   $linked
40
 * @return string
41
 */
42
function newbbGetUnameFromId($uid, $usereal = 0, $linked = false)
43
{
44
    xoops_load('xoopsuserutility');
45
46
    return XoopsUserUtility::getUnameFromId($uid, $usereal, $linked);
47
}
48
49
/**
50
 * Function to check if a user is an administrator of the module
51
 *
52
 * @param  int $user
53
 * @param  int $mid
54
 * @return bool
55
 */
56
function newbbIsAdministrator($user = -1, $mid = 0)
0 ignored issues
show
Coding Style introduced by
newbbIsAdministrator uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
57
{
58
    global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
59
60
    if (is_numeric($user) && $user == -1) {
61
        $user = $GLOBALS['xoopsUser'];
62
    }
63
    if (!is_object($user) && (int)$user < 1) {
64
        return false;
65
    }
66
    $uid = is_object($user) ? $user->getVar('uid') : (int)$user;
67
68
    if (!$mid) {
69 View Code Duplication
        if (is_object($xoopsModule) && 'newbb' === $xoopsModule->getVar('dirname', 'n')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70
            $mid = $xoopsModule->getVar('mid', 'n');
71
        } else {
72
            /** @var \XoopsModuleHandler $moduleHandler */
73
            $moduleHandler = xoops_getHandler('module');
74
            $newbb_module  = $moduleHandler->getByDirname('newbb');
75
            $mid           = $newbb_module->getVar('mid', 'n');
76
            unset($newbb_module);
77
        }
78
    }
79
80
    if (is_object($xoopsModule) && is_object($GLOBALS['xoopsUser']) && $mid == $xoopsModule->getVar('mid', 'n')
81
        && $uid == $GLOBALS['xoopsUser']->getVar('uid', 'n')) {
82
        return $GLOBALS['xoopsUserIsAdmin'];
83
    }
84
85
    /** @var \XoopsMemberHandler $memberHandler */
86
    $memberHandler = xoops_getHandler('member');
87
    $groups        = $memberHandler->getGroupsByUser($uid);
88
89
    /** @var \XoopsGroupPermHandler $modulepermHandler */
90
    $modulepermHandler = xoops_getHandler('groupperm');
91
92
    return $modulepermHandler->checkRight('module_admin', $mid, $groups);
93
}
94
95
/**
96
 * Function to check if a user is a moderator of a forum
97
 *
98
 * @param       $forum
99
 * @param  int  $user
100
 * @return bool
101
 */
102
function newbbIsModerator(&$forum, $user = -1)
0 ignored issues
show
Coding Style introduced by
newbbIsModerator uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
103
{
104
    if (!is_object($forum)) {
105
        $forum_id = (int)$forum;
106
        if ($forum_id == 0) {
107
            return false;
108
        }
109
        $forumHandler = xoops_getModuleHandler('forum', 'newbb');
110
        $forum        = $forumHandler->get($forum_id);
111
    }
112
113
    if (is_numeric($user) && $user == -1) {
114
        $user = $GLOBALS['xoopsUser'];
115
    }
116
    if (!is_object($user) && (int)$user < 1) {
117
        return false;
118
    }
119
    $uid = is_object($user) ? $user->getVar('uid', 'n') : (int)$user;
120
121
    return in_array($uid, $forum->getVar('forum_moderator'), true);
122
}
123
124
/**
125
 * Function to check if a user has moderation permission over a forum
126
 *
127
 * @param  NewbbForum|int $forum
128
 * @return bool
129
 */
130
function newbbIsAdmin($forum = 0)
0 ignored issues
show
Coding Style introduced by
newbbIsAdmin uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
131
{
132
    global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
133
    static $_cachedModerators;
134
135
    if (empty($forum)) {
136
        return $GLOBALS['xoopsUserIsAdmin'];
137
    }
138
139
    if (!is_object($GLOBALS['xoopsUser'])) {
140
        return false;
141
    }
142
143
    if ($GLOBALS['xoopsUserIsAdmin'] && $xoopsModule->getVar('dirname') === 'newbb') {
144
        return true;
145
    }
146
147
    $cache_id = is_object($forum) ? $forum->getVar('forum_id', 'n') : (int)$forum;
148
    if (!isset($_cachedModerators[$cache_id])) {
149
        if (!is_object($forum)) {
150
            $forumHandler = xoops_getModuleHandler('forum', 'newbb');
151
            $forum        = $forumHandler->get((int)$forum);
152
        }
153
        $_cachedModerators[$cache_id] = $forum->getVar('forum_moderator');
154
    }
155
156
    return in_array($GLOBALS['xoopsUser']->getVar('uid'), $_cachedModerators[$cache_id]);
157
}
158
159
/* use hardcoded DB query to save queries */
160
/**
161
 * @param  array $uid
162
 * @return array
163
 */
164
function newbbIsModuleAdministrators(array $uid = [])
0 ignored issues
show
Coding Style introduced by
newbbIsModuleAdministrators uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
165
{
166
    global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
167
    $module_administrators = [];
168
169
    //    $xoopsMembershipHandler = xoops_getHandler('membership');
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
170
    //    $xoopsMembershipTable   = $xoopsMembershipHandler->table;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
171
172
    /** @var \XoopsMembershipHandler $xoopsMembershipHandler */
173
    $xoopsMembershipHandler = xoops_getHandler('membership');
174
    $xoopsMembershipTable   = $xoopsMembershipHandler->table;
175
    /** @var \XoopsGroupPermHandler $xoopsGroupPermHandler */
176
    $xoopsGroupPermHandler = xoops_getHandler('groupperm');
177
    $xoopsGroupPermTable   = $xoopsGroupPermHandler->table;
178
179
    if (!(bool)$uid) {
180
        return $module_administrators;
181
    }
182
    $mid = $xoopsModule->getVar('mid');
183
184
    $sql = 'SELECT COUNT(l.groupid) AS count, l.uid FROM '
185
           . $xoopsMembershipTable
186
           . ' AS l'
187
           . ' LEFT JOIN '
188
           . $xoopsGroupPermTable
189
           . ' AS p ON p.gperm_groupid=l.groupid'
190
           . ' WHERE l.uid IN ('
191
           . implode(', ', array_map('intval', $uid))
192
           . ')'
193
           . "    AND p.gperm_modid = '1' AND p.gperm_name = 'module_admin' AND p.gperm_itemid = '"
194
           . (int)$mid
195
           . "'"
196
           . ' GROUP BY l.uid';
197
198
    if ($result = $GLOBALS['xoopsDB']->query($sql)) {
199
        while ($myrow = $GLOBALS['xoopsDB']->fetchArray($result)) {
200
            if (!empty($myrow['count'])) {
201
                $module_administrators[] = $myrow['uid'];
202
            }
203
        }
204
    }
205
206
    return $module_administrators;
207
}
208
209
/* use hardcoded DB query to save queries */
210
/**
211
 * @param  array $uid
212
 * @param  int   $mid
213
 * @return array
214
 */
215
function newbbIsForumModerators(array $uid = [], $mid = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $mid 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...
Coding Style introduced by
newbbIsForumModerators uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
216
{
217
    $forum_moderators = [];
218
219
    if (!(bool)$uid) {
220
        return $forum_moderators;
221
    }
222
223
    $sql = 'SELECT forum_moderator FROM ' . $GLOBALS['xoopsDB']->prefix('newbb_forums');
224
    if ($result = $GLOBALS['xoopsDB']->query($sql)) {
225
        while ($myrow = $GLOBALS['xoopsDB']->fetchArray($result)) {
226
            if (empty($myrow['forum_moderator'])) {
227
                continue;
228
            }
229
            $forum_moderators = array_merge($forum_moderators, unserialize($myrow['forum_moderator']));
230
        }
231
    }
232
233
    return array_unique($forum_moderators);
234
}
235
//ENDIF;
236