/
UserListPage.class.php
executable file
·402 lines (346 loc) · 11.6 KB
/
UserListPage.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
namespace wcf\acp\page;
use wcf\data\user\group\UserGroup;
use wcf\data\user\option\ViewableUserOption;
use wcf\data\user\User;
use wcf\data\user\UserProfile;
use wcf\page\SortablePage;
use wcf\system\cache\builder\UserOptionCacheBuilder;
use wcf\system\clipboard\ClipboardHandler;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\event\EventHandler;
use wcf\system\exception\IllegalLinkException;
use wcf\system\option\user\IUserOptionOutput;
use wcf\system\option\IOptionHandler;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\DateUtil;
use wcf\util\StringUtil;
/**
* Shows the result of a user search.
*
* @author Marcel Werk
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @package WoltLabSuite\Core\Acp\Page
*/
class UserListPage extends SortablePage {
/**
* list of displayed column names
* @var string[]
*/
public $columnHeads = [];
/**
* list of selected columns
* @var string[]
*/
public $columns = ['registrationDate', 'lastActivityTime'];
/**
* applies special CSS classes for selected columns
* @var array
*/
public $columnStyling = [
'registrationDate' => 'columnDate',
'lastActivityTime' => 'columnDate',
'profileHits' => 'columnDigits',
'activityPoints' => 'columnDigits',
'likesReceived' => 'columnDigits'
];
/**
* list of column values
* @var string[][]
*/
public $columnValues = [];
/**
* @inheritDoc
*/
public $defaultSortField = 'username';
/**
* @inheritDoc
*/
public $itemsPerPage = 50;
/**
* list of marked user ids
* @var integer[]
*/
public $markedUsers = [];
/**
* @inheritDoc
*/
public $neededPermissions = ['admin.user.canSearchUser'];
/**
* IOptionHandler object
* @var IOptionHandler
*/
protected $optionHandler = null;
/**
* list of available user option names
* @var array
*/
public $options = [];
/**
* id of a user search
* @var integer
*/
public $searchID = 0;
/**
* list of user ids
* @var integer[]
*/
public $userIDs = [];
/**
* list of users
* @var UserProfile[]
*/
public $users = [];
/**
* page url
* @var string
*/
public $url = '';
/**
* condition builder for user filtering
* @var PreparedStatementConditionBuilder
*/
public $conditions = null;
/**
* @inheritDoc
*/
public $validSortFields = ['userID', 'registrationDate', 'username', 'lastActivityTime', 'profileHits', 'activityPoints', 'likesReceived'];
/**
* @inheritDoc
*/
public function readParameters() {
parent::readParameters();
$this->conditions = new PreparedStatementConditionBuilder();
if (!empty($_REQUEST['id'])) {
$this->searchID = intval($_REQUEST['id']);
if ($this->searchID) $this->readSearchResult();
if (empty($this->userIDs)) {
throw new IllegalLinkException();
}
$this->conditions->add("user_table.userID IN (?)", [$this->userIDs]);
}
// get user options
$this->readUserOptions();
}
/**
* @inheritDoc
*/
public function validateSortField() {
// add options to valid sort fields
$this->validSortFields = array_merge($this->validSortFields, array_keys($this->options));
// avoid leaking mail adresses by sorting
if (WCF::getSession()->getPermission('admin.user.canEditMailAddress')) {
$this->validSortFields[] = 'email';
}
parent::validateSortField();
}
/**
* @inheritDoc
*/
public function readData() {
parent::readData();
// add email column for authorized users
if (WCF::getSession()->getPermission('admin.user.canEditMailAddress')) {
array_unshift($this->columns, 'email');
}
// get marked users
$this->markedUsers = WCF::getSession()->getVar('markedUsers');
if ($this->markedUsers == null || !is_array($this->markedUsers)) $this->markedUsers = [];
// get columns heads
$this->readColumnsHeads();
// get users
$this->readUsers();
// build page url
$this->url = LinkHandler::getInstance()->getLink('UserList', [], 'searchID='.$this->searchID.'&action='.rawurlencode($this->action).'&pageNo='.$this->pageNo.'&sortField='.$this->sortField.'&sortOrder='.$this->sortOrder);
}
/**
* @inheritDoc
*/
public function assignVariables() {
parent::assignVariables();
WCF::getTPL()->assign([
'users' => $this->users,
'searchID' => $this->searchID,
'hasMarkedItems' => ClipboardHandler::getInstance()->hasMarkedItems(ClipboardHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.user')),
'url' => $this->url,
'columnHeads' => $this->columnHeads,
'columnValues' => $this->columnValues,
'columnStyling' => $this->columnStyling
]);
}
/**
* @inheritDoc
*/
public function show() {
$this->activeMenuItem = 'wcf.acp.menu.link.user.'.($this->searchID ? 'search' : 'list');
parent::show();
}
/**
* @inheritDoc
*/
public function countItems() {
// call countItems event
EventHandler::getInstance()->fireAction($this, 'countItems');
$sql = "SELECT COUNT(*)
FROM wcf".WCF_N."_user user_table
".$this->conditions;
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute($this->conditions->getParameters());
return $statement->fetchSingleColumn();
}
/**
* Fetches the list of results.
*/
protected function readUsers() {
// get user ids
$sql = "SELECT user_table.userID
FROM wcf".WCF_N."_user user_table
".(isset($this->options[$this->sortField]) ? "LEFT JOIN wcf".WCF_N."_user_option_value user_option_value ON (user_option_value.userID = user_table.userID)" : '')."
".$this->conditions."
ORDER BY ".(($this->sortField != 'email' && isset($this->options[$this->sortField])) ? 'user_option_value.userOption'.$this->options[$this->sortField]->optionID : $this->sortField)." ".$this->sortOrder;
$statement = WCF::getDB()->prepareStatement($sql, $this->itemsPerPage, ($this->pageNo - 1) * $this->itemsPerPage);
$statement->execute($this->conditions->getParameters());
$userIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
// get user data
if (!empty($userIDs)) {
// get group ids
$conditions = new PreparedStatementConditionBuilder();
$conditions->add("user_table.userID IN (?)", [$userIDs]);
$sql = "SELECT userID, groupID
FROM wcf".WCF_N."_user_to_group user_table
".$conditions;
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute($conditions->getParameters());
$userToGroups = $statement->fetchMap('userID', 'groupID', false);
$sql = "SELECT user_avatar.*, option_value.*, user_table.*
FROM wcf".WCF_N."_user user_table
LEFT JOIN wcf".WCF_N."_user_option_value option_value
ON (option_value.userID = user_table.userID)
LEFT JOIN wcf".WCF_N."_user_avatar user_avatar
ON (user_avatar.avatarID = user_table.avatarID)
".$conditions."
ORDER BY ".(($this->sortField != 'email' && isset($this->options[$this->sortField])) ? 'option_value.userOption'.$this->options[$this->sortField]->optionID : 'user_table.'.$this->sortField)." ".$this->sortOrder;
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute($conditions->getParameters());
while ($row = $statement->fetchArray()) {
$groupIDs = (isset($userToGroups[$row['userID']]) ? $userToGroups[$row['userID']] : []);
$row['groupIDs'] = implode(',', $groupIDs);
$accessible = (!empty($groupIDs) ? UserGroup::isAccessibleGroup($groupIDs) : true);
$row['accessible'] = $accessible;
$row['deletable'] = ($accessible && WCF::getSession()->getPermission('admin.user.canDeleteUser') && $row['userID'] != WCF::getUser()->userID) ? 1 : 0;
$row['editable'] = ($accessible && WCF::getSession()->getPermission('admin.user.canEditUser')) ? 1 : 0;
$row['bannable'] = ($accessible && WCF::getSession()->getPermission('admin.user.canBanUser') && $row['userID'] != WCF::getUser()->userID) ? 1 : 0;
$row['canBeEnabled'] = ($accessible && WCF::getSession()->getPermission('admin.user.canEnableUser') && $row['userID'] != WCF::getUser()->userID) ? 1 : 0;
$row['isMarked'] = intval(in_array($row['userID'], $this->markedUsers));
$this->users[] = new UserProfile(new User(null, $row));
}
// get special columns
foreach ($this->users as $key => $user) {
foreach ($this->columns as $column) {
switch ($column) {
case 'email':
$this->columnValues[$user->userID][$column] = '<a href="mailto:'.StringUtil::encodeHTML($user->email).'">'.StringUtil::encodeHTML($user->email).'</a>';
break;
case 'registrationDate':
$this->columnValues[$user->userID][$column] = DateUtil::format(DateUtil::getDateTimeByTimestamp($user->{$column}), DateUtil::DATE_FORMAT);
break;
case 'lastActivityTime':
if ($user->{$column}) {
$this->columnValues[$user->userID][$column] = str_replace('%time%', DateUtil::format(DateUtil::getDateTimeByTimestamp($user->{$column}), DateUtil::TIME_FORMAT), str_replace('%date%', DateUtil::format(DateUtil::getDateTimeByTimestamp($user->{$column}), DateUtil::DATE_FORMAT), WCF::getLanguage()->get('wcf.date.dateTimeFormat')));
}
break;
case 'profileHits':
case 'activityPoints':
case 'likesReceived':
$this->columnValues[$user->userID][$column] = StringUtil::formatInteger($user->{$column});
break;
default:
if (isset($this->options[$column])) {
if ($this->options[$column]->outputClass) {
$this->options[$column]->setOptionValue($user->getDecoratedObject());
/** @var IUserOptionOutput $outputObj */
$outputObj = $this->options[$column]->getOutputObject();
$this->columnValues[$user->userID][$column] = $outputObj->getOutput($user->getDecoratedObject(), $this->options[$column]->getDecoratedObject(), $user->{$column});
}
else {
$this->columnValues[$user->userID][$column] = StringUtil::encodeHTML($user->{$column});
}
}
break;
}
}
}
}
}
/**
* Fetches the result of the search with the given search id.
*/
protected function readSearchResult() {
// get user search from database
$sql = "SELECT searchData
FROM wcf".WCF_N."_search
WHERE searchID = ?
AND userID = ?
AND searchType = ?";
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute([
$this->searchID,
WCF::getUser()->userID,
'users'
]);
$search = $statement->fetchArray();
if (!isset($search['searchData'])) {
throw new IllegalLinkException();
}
$data = unserialize($search['searchData']);
$this->userIDs = $data['matches'];
$this->itemsPerPage = $data['itemsPerPage'];
$this->columns = $data['columns'];
unset($data);
}
/**
* Fetches the user options from cache.
*/
protected function readUserOptions() {
$this->options = UserOptionCacheBuilder::getInstance()->getData([], 'options');
foreach ($this->options as &$option) {
$option = new ViewableUserOption($option);
}
unset($option);
}
/**
* Reads the column heads.
*/
protected function readColumnsHeads() {
foreach ($this->columns as $column) {
if ($column == 'likesReceived') {
$this->columnHeads[$column] = 'wcf.like.likesReceived';
continue;
}
if ($column == 'activityPoints') {
$this->columnHeads[$column] = 'wcf.user.activityPoint';
continue;
}
if (isset($this->options[$column]) && $column != 'email') {
$this->columnHeads[$column] = 'wcf.user.option.'.$column;
}
else {
$this->columnHeads[$column] = 'wcf.user.'.$column;
}
}
}
/**
* @inheritDoc
*/
protected function initObjectList() {
// does nothing
}
/**
* @inheritDoc
*/
protected function readObjects() {
// does nothing
}
}