| 1 | <?php |
| 2 | |
| 3 | namespace wcf\page; |
| 4 | |
| 5 | use wcf\data\page\PageCache; |
| 6 | use wcf\data\user\online\UserOnline; |
| 7 | use wcf\data\user\online\UsersOnlineList; |
| 8 | use wcf\system\page\handler\IOnlineLocationPageHandler; |
| 9 | use wcf\system\page\PageLocationManager; |
| 10 | use wcf\system\request\LinkHandler; |
| 11 | use wcf\system\WCF; |
| 12 | use wcf\util\HeaderUtil; |
| 13 | |
| 14 | /** |
| 15 | * Shows page which lists all users who are online. |
| 16 | * |
| 17 | * @author Marcel Werk |
| 18 | * @copyright 2001-2019 WoltLab GmbH |
| 19 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 20 | * |
| 21 | * @property UsersOnlineList $objectList |
| 22 | */ |
| 23 | class UsersOnlineListPage extends SortablePage |
| 24 | { |
| 25 | /** |
| 26 | * @inheritDoc |
| 27 | */ |
| 28 | public $neededPermissions = ['user.profile.canViewUsersOnlineList']; |
| 29 | |
| 30 | /** |
| 31 | * @inheritDoc |
| 32 | */ |
| 33 | public $itemsPerPage = 100; |
| 34 | |
| 35 | /** |
| 36 | * @inheritDoc |
| 37 | */ |
| 38 | public $defaultSortField = USERS_ONLINE_DEFAULT_SORT_FIELD; |
| 39 | |
| 40 | /** |
| 41 | * @inheritDoc |
| 42 | */ |
| 43 | public $defaultSortOrder = USERS_ONLINE_DEFAULT_SORT_ORDER; |
| 44 | |
| 45 | /** |
| 46 | * @inheritDoc |
| 47 | */ |
| 48 | public $validSortFields = ['username', 'lastActivityTime', 'requestURI']; |
| 49 | |
| 50 | /** |
| 51 | * @inheritDoc |
| 52 | */ |
| 53 | public $objectListClassName = UsersOnlineList::class; |
| 54 | |
| 55 | /** |
| 56 | * page locations |
| 57 | * @var array |
| 58 | */ |
| 59 | public $locations = []; |
| 60 | |
| 61 | /** |
| 62 | * @inheritDoc |
| 63 | */ |
| 64 | public function readParameters() |
| 65 | { |
| 66 | parent::readParameters(); |
| 67 | |
| 68 | if (WCF::getSession()->getPermission('admin.user.canViewIpAddress')) { |
| 69 | $this->validSortFields[] = 'ipAddress'; |
| 70 | $this->validSortFields[] = 'userAgent'; |
| 71 | } |
| 72 | |
| 73 | if (!empty($_POST)) { |
| 74 | HeaderUtil::redirect(LinkHandler::getInstance()->getLink( |
| 75 | 'UsersOnlineList', |
| 76 | [], |
| 77 | 'sortField=' . $this->sortField . '&sortOrder=' . $this->sortOrder |
| 78 | )); |
| 79 | |
| 80 | exit; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @inheritDoc |
| 86 | */ |
| 87 | protected function initObjectList() |
| 88 | { |
| 89 | parent::initObjectList(); |
| 90 | $this->objectList->readStats(); |
| 91 | $this->objectList->checkRecord(); |
| 92 | |
| 93 | if (!USERS_ONLINE_SHOW_ROBOTS) { |
| 94 | $this->objectList->getConditionBuilder()->add('session.spiderIdentifier IS NULL'); |
| 95 | } |
| 96 | if (!USERS_ONLINE_SHOW_GUESTS) { |
| 97 | if (USERS_ONLINE_SHOW_ROBOTS) { |
| 98 | $this->objectList->getConditionBuilder()->add('(session.userID IS NOT NULL OR session.spiderIdentifier IS NOT NULL)'); |
| 99 | } else { |
| 100 | $this->objectList->getConditionBuilder()->add('session.userID IS NOT NULL'); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | $this->objectList->sqlSelects .= ", CASE WHEN session.userID IS NULL THEN 1 ELSE 0 END AS userIsGuest"; |
| 105 | $this->objectList->sqlSelects .= ", CASE WHEN session.spiderIdentifier IS NOT NULL THEN 1 ELSE 0 END AS userIsRobot"; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @inheritDoc |
| 110 | */ |
| 111 | public function readData() |
| 112 | { |
| 113 | parent::readData(); |
| 114 | |
| 115 | // add breadcrumbs |
| 116 | if (MODULE_MEMBERS_LIST) { |
| 117 | PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.MembersList'); |
| 118 | } |
| 119 | |
| 120 | // cache all necessary data for showing locations |
| 121 | foreach ($this->objectList as $userOnline) { |
| 122 | $page = PageCache::getInstance()->getPage($userOnline->pageID); |
| 123 | if ($page !== null && $page->getHandler() !== null && $page->getHandler() instanceof IOnlineLocationPageHandler) { |
| 124 | /** @noinspection PhpUndefinedMethodInspection */ |
| 125 | $page->getHandler()->prepareOnlineLocation($page, $userOnline); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // set locations |
| 130 | /** @var UserOnline $userOnline */ |
| 131 | foreach ($this->objectList as $userOnline) { |
| 132 | $userOnline->setLocation(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @inheritDoc |
| 138 | */ |
| 139 | protected function readObjects() |
| 140 | { |
| 141 | if ($this->sqlOrderBy) { |
| 142 | $this->sqlOrderBy = ($this->sortField == 'lastActivityTime' ? 'session.' : '') . $this->sqlOrderBy; |
| 143 | } |
| 144 | |
| 145 | $originalSqlOrderBy = $this->sqlOrderBy; |
| 146 | // sort in this order: users -> guests -> robots |
| 147 | $this->sqlOrderBy = "userIsGuest ASC, userIsRobot DESC, " . $this->sqlOrderBy; |
| 148 | |
| 149 | parent::readObjects(); |
| 150 | |
| 151 | // restore original order |
| 152 | $this->sqlOrderBy = $originalSqlOrderBy; |
| 153 | } |
| 154 | } |