3 namespace wcf\data\user\online
;
5 use wcf\data\page\PageCache
;
6 use wcf\data\spider\Spider
;
7 use wcf\data\user\UserProfile
;
8 use wcf\system\cache\builder\SpiderCacheBuilder
;
9 use wcf\system\event\EventHandler
;
10 use wcf\system\page\handler\IOnlineLocationPageHandler
;
12 use wcf\util\StringUtil
;
13 use wcf\util\UserAgent
;
14 use wcf\util\UserUtil
;
17 * Represents a user who is online.
20 * @copyright 2001-2019 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package WoltLabSuite\Core\Data\User\Online
24 * @property-read int|null $pageID id of the last visited page
25 * @property-read int|null $pageObjectID id of the object the last visited page belongs to
26 * @property-read int|null $parentPageObjectID id of the parent of the object the last visited page belongs to
27 * @property-read string|null $userOnlineMarking HTML code used to print the formatted name of a user group member
29 class UserOnline
extends UserProfile
32 * location of the user
35 protected $location = '';
44 * Returns the formatted username.
48 public function getFormattedUsername()
50 $username = StringUtil
::encodeHTML($this->username
);
52 if ($this->userOnlineMarking
&& $this->userOnlineMarking
!= '%s') {
53 $username = \
str_replace('%s', $username, $this->userOnlineMarking
);
56 if ($this->canViewOnlineStatus
== UserProfile
::ACCESS_NOBODY
) {
57 $username .= WCF
::getLanguage()->get('wcf.user.usersOnline.invisible');
64 * Sets the location of the user. If no location is given, the method tries to
65 * automatically determine the location.
67 * @param string|null $location
68 * @return bool `true` if the location has been successfully set, otherwise `false`
70 public function setLocation($location = null)
72 if ($location === null) {
74 $page = PageCache
::getInstance()->getPage($this->pageID
);
76 if ($page->getHandler() !== null && $page->getHandler() instanceof IOnlineLocationPageHandler
) {
77 // refer to page handler
78 /** @noinspection PhpUndefinedMethodInspection */
79 $this->location
= $page->getHandler()->getOnlineLocation($page, $this);
82 } elseif ($page->isVisible() && $page->isAccessible()) {
83 $title = $page->getTitle();
85 if ($page->pageType
!= 'system') {
86 $this->location
= '<a href="' . StringUtil
::encodeHTML($page->getLink()) . '">' . StringUtil
::encodeHTML($title) . '</a>';
88 $this->location
= StringUtil
::encodeHTML($title);
92 return $this->location
!= '';
102 $this->location
= $location;
108 * Returns the location of the user.
112 public function getLocation()
114 return $this->location
;
118 * Returns the ip address.
122 public function getFormattedIPAddress()
124 if ($address = UserUtil
::convertIPv6To4($this->ipAddress
)) {
128 return $this->ipAddress
;
132 * Tries to retrieve browser name and version.
136 public function getBrowser()
138 $parameters = ['browser' => '', 'userAgent' => $this->userAgent
];
139 EventHandler
::getInstance()->fireAction($this, 'getBrowser', $parameters);
140 if (!empty($parameters['browser'])) {
141 return $parameters['browser'];
144 $userAgent = new UserAgent($this->userAgent
);
145 if ($userAgent->getBrowser() === null) {
146 return $this->userAgent
;
149 $browserVersion = $userAgent->getBrowserVersion();
151 return $userAgent->getBrowser() . ($browserVersion ?
' ' . $browserVersion : '');
155 * Returns the spider object
159 public function getSpider()
161 if (!$this->spiderID
) {
165 if ($this->spider
=== null) {
166 $spiderList = SpiderCacheBuilder
::getInstance()->getData();
167 $this->spider
= $spiderList[$this->spiderID
];
170 return $this->spider
;