-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIndex.php
More file actions
121 lines (98 loc) · 3.57 KB
/
Copy pathIndex.php
File metadata and controls
121 lines (98 loc) · 3.57 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
<?php
namespace BNETDocs\Controllers\Server;
use \BNETDocs\Libraries\Server\Server as ServerLib;
class Index extends \BNETDocs\Controllers\Base
{
/**
* Constructs a Controller, typically to initialize properties.
*/
public function __construct()
{
$this->model = new \BNETDocs\Models\Server\Index();
}
/**
* Invoked by the Router class to handle the request.
*
* @param array|null $args The optional route arguments and any captured URI arguments.
* @return boolean Whether the Router should invoke the configured View.
*/
public function invoke(?array $args): bool
{
$q = \BNETDocs\Libraries\Core\Router::query();
$this->model->server_types = [];
$this->model->servers = ServerLib::getAllServers();
$this->model->status_bitmasks = [
[
'bit' => ServerLib::STATUS_ONLINE,
'description' => 'Server is online if set, offline if not set',
'label' => 'Online'
],
[
'bit' => ServerLib::STATUS_DISABLED,
'description' => 'Server is not automatically checked if set',
'label' => 'Disabled'
]
];
// collect filter queries
$status = $q['status'] ?? null;
$type_id = $q['type_id'] ?? null;
$user_id = $q['user_id'] ?? null;
// filter by status bitmask
if (!is_null($status) && is_numeric($status))
foreach ($this->model->servers as $it => $server)
if (!($server->getStatusBitmask() & $status)) unset($this->model->servers[$it]);
// filter by type_id
if (!is_null($type_id) && is_numeric($type_id))
// type_id is a single integer
foreach ($this->model->servers as $it => $server)
if ($server->getTypeId() != $type_id) unset($this->model->servers[$it]);
if (is_string($type_id) && preg_match('/^(?:\d+,?)+$/', $type_id))
// type_id is a string like "1,2,3"
$type_id = explode(',', $type_id);
if ($type_id && is_array($type_id)) {
foreach ($this->model->servers as $it => $server) {
$found = false;
foreach ($type_id as $_type) {
if ($server->getTypeId() == $_type) {
$found = true;
break;
}
}
if (!$found) unset($this->model->servers[$it]);
}
}
// filter by user_id
if (!is_null($user_id) && is_numeric($user_id))
// user_id is a single integer
foreach ($this->model->servers as $it => $server)
if ($server->getUserId() != $user_id) unset($this->model->servers[$it]);
if (is_string($user_id) && preg_match('/^(?:\d+,?)+$/', $user_id))
// user_id is a string like "1,2,3"
$user_id = explode(',', $user_id);
if ($user_id && is_array($user_id)) {
foreach ($this->model->servers as $it => $server) {
$found = false;
foreach ($user_id as $_user) {
if ($server->getUserId() == $_user) {
$found = true;
break;
}
}
if (!$found) unset($this->model->servers[$it]);
}
}
// reindex servers after removal of indices using unset()
$this->model->servers = array_values($this->model->servers);
// collect types and filter
$server_types = [];
foreach ($this->model->servers as $server)
$server_types[] = $server->getTypeId();
sort($server_types);
$server_types = \array_unique($server_types);
$this->model->server_types = [];
foreach ($server_types as $id)
$this->model->server_types[] = new \BNETDocs\Libraries\Server\Type($id);
$this->model->_responseCode = \BNETDocs\Libraries\Core\HttpCode::HTTP_OK;
return true;
}
}