-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-region.php
More file actions
254 lines (221 loc) · 6.72 KB
/
Copy pathclass-region.php
File metadata and controls
254 lines (221 loc) · 6.72 KB
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
<?php
/**
* OpenSimulator Region Class - Framework Agnostic
*
* Core region functionality without framework dependencies
*/
class OpenSim_Region {
protected $uuid;
protected $item;
protected $data;
protected $db;
protected $server;
// Region properties
protected $name;
protected $owner_name;
protected $owner_uuid;
protected $serverURI;
protected $sizeX;
protected $sizeY;
protected $flags;
protected $last_seen;
protected $presence;
// Connection status
protected $console_connected = false;
protected $db_connected = false;
public function __construct($args = null, $database_connection = null) {
$this->db = $database_connection;
if (!OpenSim::empty($args)) {
$this->fetch_region_data($args);
}
$this->init_server_connection();
}
/**
* Initialize server connection if serverURI is available
*/
protected function init_server_connection() {
if (!empty($this->item->serverURI)) {
// This will be implemented by framework-specific classes
// as server connection classes may be framework-dependent
}
}
/**
* Fetch region data from database
*/
public function fetch_region_data($args) {
if (OpenSim::empty($args)) {
return;
}
if (is_object($args)) {
$this->uuid = $args->uuid;
$this->item = $args;
} elseif (is_string($args) && is_uuid($args)) {
$this->uuid = $args;
if ($this->db) {
$query = $this->get_main_query() . ' WHERE uuid = ?';
$this->item = $this->db->get_row($query, [$this->uuid]);
}
}
if ($this->item) {
$this->populate_properties();
}
}
/**
* Get the main query for fetching region data
*/
protected function get_main_query() {
return "SELECT regions.*,
CONCAT(UserAccounts.FirstName, ' ', UserAccounts.LastName) AS owner_name,
sizeX * sizeY AS size,
(SELECT COUNT(*) FROM Presence WHERE Presence.RegionID = regions.uuid) AS presence
FROM regions
LEFT JOIN UserAccounts ON regions.owner_uuid = UserAccounts.PrincipalID";
}
/**
* Populate object properties from item data
*/
protected function populate_properties() {
if (!$this->item) return;
$this->name = $this->item->regionName ?? null;
$this->owner_name = $this->item->owner_name ?? null;
$this->owner_uuid = $this->item->owner_uuid ?? null;
$this->serverURI = $this->item->serverURI ?? null;
$this->sizeX = $this->item->sizeX ?? 256;
$this->sizeY = $this->item->sizeY ?? 256;
$this->flags = $this->item->flags ?? 0;
$this->last_seen = $this->item->last_seen ?? 0;
$this->presence = $this->item->presence ?? 0;
}
/**
* Get region name
*/
public function get_name() {
return $this->name;
}
/**
* Get region UUID
*/
public function get_uuid() {
return $this->uuid;
}
/**
* Get region item data
*/
public function get_item() {
return $this->item;
}
/**
* Get region size as formatted string
*/
public function get_size_formatted() {
if (empty($this->sizeX) || empty($this->sizeY)) {
return null;
}
return $this->sizeX . '×' . $this->sizeY;
}
/**
* Get region flags array
*/
public function get_flags() {
return $this->match_flags($this->flags);
}
/**
* Match bitwise flags to labels
*/
public static function match_flags($bitwise, $flag_definitions = null) {
if ($flag_definitions === null) {
// Default OpenSimulator flags
$flag_definitions = [
1 => 'Default Region',
1024 => 'Default HG Region',
2 => 'Fallback Region',
256 => 'Authenticate',
512 => 'Hyperlink',
32 => 'Locked Out',
8 => 'No Direct Login',
64 => 'No Move',
16 => 'Persistent',
128 => 'Reservation',
];
}
$matches = [];
foreach ($flag_definitions as $flag => $label) {
if ($bitwise & $flag) {
$matches[$flag] = $label;
}
}
return $matches;
}
/**
* Check if region is online by testing server connection
*/
public function is_online() {
if (empty($this->serverURI)) {
return false;
}
// Simple HTTP check - can be overridden by framework-specific implementations
$url = rtrim($this->serverURI, '/') . '/';
$context = stream_context_create([
'http' => [
'timeout' => 5,
'method' => 'GET'
]
]);
$result = @file_get_contents($url, false, $context);
return $result !== false;
}
/**
* Get formatted server URI (hostname:port)
*/
public function get_server_uri_formatted($use_dns = true) {
if (empty($this->serverURI)) {
return null;
}
$parts = parse_url($this->serverURI);
$hostname = $parts['host'];
// Use DNS if hostname is an IP address
if ($use_dns && filter_var($hostname, FILTER_VALIDATE_IP)) {
$resolved = gethostbyaddr($parts['host']);
if ($resolved !== $parts['host']) {
$dot_count = substr_count($resolved, '.');
if ($dot_count > 0 && $dot_count <= 4) {
$hostname = $resolved;
}
}
}
return $hostname . ':' . ($parts['port'] ?? '80');
}
/**
* Get region teleport URI format
*/
public function get_tp_uri($gateway = null) {
if (!$this->name || !$gateway) {
return null;
}
// Strip protocol from gateway
$gateway = preg_replace('/^https?:\/\//', '', $gateway);
$gateway = rtrim($gateway, '/') . '/';
return $gateway . $this->name;
}
/**
* Format last seen timestamp
*/
public function get_last_seen_formatted() {
if ($this->last_seen === 0) {
return 'Never';
}
return date('Y-m-d H:i:s', $this->last_seen);
}
/**
* Get owner name
*/
public function get_owner_name() {
return $this->owner_name;
}
/**
* Get presence count
*/
public function get_presence() {
return $this->presence ?? 0;
}
}