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'); |
|
|
|
|
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) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
global $xoopsModule; |
|
|
|
|
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')) { |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
global $xoopsModule; |
|
|
|
|
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 = []) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
global $xoopsModule; |
|
|
|
|
167
|
|
|
$module_administrators = []; |
168
|
|
|
|
169
|
|
|
// $xoopsMembershipHandler = xoops_getHandler('membership'); |
|
|
|
|
170
|
|
|
// $xoopsMembershipTable = $xoopsMembershipHandler->table; |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.