8ddb434c247243fd3cbbb123c91265da79584510
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / online / UserOnline.class.php
1 <?php
2
3 namespace wcf\data\user\online;
4
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;
11 use wcf\system\WCF;
12 use wcf\util\StringUtil;
13 use wcf\util\UserAgent;
14 use wcf\util\UserUtil;
15
16 /**
17 * Represents a user who is online.
18 *
19 * @author Marcel Werk
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
23 *
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
28 */
29 class UserOnline extends UserProfile
30 {
31 /**
32 * location of the user
33 * @var string
34 */
35 protected $location = '';
36
37 /**
38 * spider object
39 * @var Spider
40 */
41 protected $spider;
42
43 /**
44 * Returns the formatted username.
45 *
46 * @return string
47 */
48 public function getFormattedUsername()
49 {
50 $username = StringUtil::encodeHTML($this->username);
51
52 if ($this->userOnlineMarking && $this->userOnlineMarking != '%s') {
53 $username = \str_replace('%s', $username, $this->userOnlineMarking);
54 }
55
56 if ($this->canViewOnlineStatus == UserProfile::ACCESS_NOBODY) {
57 $username .= WCF::getLanguage()->get('wcf.user.usersOnline.invisible');
58 }
59
60 return $username;
61 }
62
63 /**
64 * Sets the location of the user. If no location is given, the method tries to
65 * automatically determine the location.
66 *
67 * @param string|null $location
68 * @return bool `true` if the location has been successfully set, otherwise `false`
69 */
70 public function setLocation($location = null)
71 {
72 if ($location === null) {
73 if ($this->pageID) {
74 $page = PageCache::getInstance()->getPage($this->pageID);
75 if ($page !== null) {
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);
80
81 return true;
82 } elseif ($page->isVisible() && $page->isAccessible()) {
83 $title = $page->getTitle();
84 if (!empty($title)) {
85 if ($page->pageType != 'system') {
86 $this->location = '<a href="' . StringUtil::encodeHTML($page->getLink()) . '">' . StringUtil::encodeHTML($title) . '</a>';
87 } else {
88 $this->location = StringUtil::encodeHTML($title);
89 }
90 }
91
92 return $this->location != '';
93 }
94 }
95 }
96
97 $this->location = '';
98
99 return false;
100 }
101
102 $this->location = $location;
103
104 return true;
105 }
106
107 /**
108 * Returns the location of the user.
109 *
110 * @return string
111 */
112 public function getLocation()
113 {
114 return $this->location;
115 }
116
117 /**
118 * Returns the ip address.
119 *
120 * @return string
121 */
122 public function getFormattedIPAddress()
123 {
124 if ($address = UserUtil::convertIPv6To4($this->ipAddress)) {
125 return $address;
126 }
127
128 return $this->ipAddress;
129 }
130
131 /**
132 * Tries to retrieve browser name and version.
133 *
134 * @return string
135 */
136 public function getBrowser()
137 {
138 $parameters = ['browser' => '', 'userAgent' => $this->userAgent];
139 EventHandler::getInstance()->fireAction($this, 'getBrowser', $parameters);
140 if (!empty($parameters['browser'])) {
141 return $parameters['browser'];
142 }
143
144 $userAgent = new UserAgent($this->userAgent);
145 if ($userAgent->getBrowser() === null) {
146 return $this->userAgent;
147 }
148
149 $browserVersion = $userAgent->getBrowserVersion();
150
151 return $userAgent->getBrowser() . ($browserVersion ? ' ' . $browserVersion : '');
152 }
153
154 /**
155 * Returns the spider object
156 *
157 * @return Spider
158 */
159 public function getSpider()
160 {
161 if (!$this->spiderID) {
162 return;
163 }
164
165 if ($this->spider === null) {
166 $spiderList = SpiderCacheBuilder::getInstance()->getData();
167 $this->spider = $spiderList[$this->spiderID];
168 }
169
170 return $this->spider;
171 }
172 }