Skip to content

Commit 259add3

Browse files
committed
Add the server details page
1 parent 2041d15 commit 259add3

File tree

8 files changed

+205
-8
lines changed

8 files changed

+205
-8
lines changed

src/controllers/Server/View.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace BNETDocs\Controllers\Server;
4+
5+
use \BNETDocs\Libraries\Controller;
6+
use \BNETDocs\Libraries\Exceptions\ServerNotFoundException;
7+
use \BNETDocs\Libraries\Exceptions\ServerTypeNotFoundException;
8+
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
9+
use \BNETDocs\Libraries\Packet;
10+
use \BNETDocs\Libraries\Router;
11+
use \BNETDocs\Libraries\Server;
12+
use \BNETDocs\Libraries\ServerType;
13+
use \BNETDocs\Models\Server\View as ServerViewModel;
14+
use \BNETDocs\Views\Server\ViewHtml as ServerViewHtmlView;
15+
use \CarlBennett\MVC\Libraries\Common;
16+
use \DateTime;
17+
use \DateTimeZone;
18+
19+
class View extends Controller {
20+
21+
protected $server_id;
22+
23+
public function __construct($server_id) {
24+
parent::__construct();
25+
$this->server_id = $server_id;
26+
}
27+
28+
public function run(Router &$router) {
29+
switch ($router->getRequestPathExtension()) {
30+
case "htm": case "html": case "":
31+
$view = new ServerViewHtmlView();
32+
break;
33+
default:
34+
throw new UnspecifiedViewException();
35+
}
36+
$model = new ServerViewModel();
37+
38+
$model->server_id = $this->server_id;
39+
40+
try {
41+
$model->server = new Server($this->server_id);
42+
$model->server_type = new ServerType($model->server->getTypeId());
43+
} catch (ServerNotFoundException $e) {
44+
$model->server = null;
45+
} catch (ServerTypeNotFoundException $e) {
46+
$model->server_type = null;
47+
}
48+
49+
ob_start();
50+
$view->render($model);
51+
$router->setResponseCode(($model->server ? 200 : 404));
52+
$router->setResponseTTL(0);
53+
$router->setResponseHeader("Content-Type", $view->getMimeType());
54+
$router->setResponseContent(ob_get_contents());
55+
ob_end_clean();
56+
}
57+
58+
}

src/libraries/Router.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use \BNETDocs\Controllers\Packet\View as PacketViewController;
2828
use \BNETDocs\Controllers\PageNotFound as PageNotFoundController;
2929
use \BNETDocs\Controllers\Redirect as RedirectController;
30+
use \BNETDocs\Controllers\Server\View as ServerViewController;
3031
use \BNETDocs\Controllers\Servers as ServersController;
3132
use \BNETDocs\Controllers\Status as StatusController;
3233
use \BNETDocs\Controllers\User\ChangePassword as UserChangePasswordController;
@@ -404,9 +405,18 @@ public function route(Pair &$redirect = null) {
404405
throw new ControllerNotFoundException($path . "/" . $subpath);
405406
}
406407
break;
407-
case "servers": case "servers.htm": case "servers.html":
408-
case "servers.json":
409-
$controller = new ServersController();
408+
case "server": case "server.htm": case "server.html":
409+
case "server.json": case "servers": case "servers.htm":
410+
case "servers.html": case "servers.json":
411+
if (is_numeric($subpath)) {
412+
$controller = new ServerViewController($subpath);
413+
} else if (empty($subpath)) {
414+
$controller = new ServersController();
415+
} else {
416+
throw new ControllerNotFoundException(
417+
$path . "/" . $subpath
418+
);
419+
}
410420
break;
411421
case "status": case "status.json": case "status.txt":
412422
$controller = new StatusController();

src/libraries/Server.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ public function getCreatedDateTime() {
128128
}
129129
}
130130

131+
public function getName() {
132+
return (empty($this->label) ?
133+
$this->address . ":" . $this->port :
134+
$this->label
135+
);
136+
}
137+
131138
public function getId() {
132139
return $this->id;
133140
}

src/models/Server/View.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace BNETDocs\Models\Server;
4+
5+
use \BNETDocs\Libraries\Model;
6+
7+
class View extends Model {
8+
9+
public $server;
10+
public $server_id;
11+
public $server_type;
12+
13+
public function __construct() {
14+
parent::__construct();
15+
$this->server = null;
16+
$this->server_id = null;
17+
$this->server_type = null;
18+
}
19+
20+
}

src/static/a/servers.css

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@
2020
display: block;
2121
}
2222
}
23+
table.serverview tr th:first-child {
24+
width: 100px;
25+
}
2326
table.servers th.status {
2427
width: 50px;
2528
}
26-
table.servers td.address {
29+
table.servers td.address,
30+
table.serverview td.address {
2731
font-family: source code pro, courier new, monospace;
2832
font-size: smaller;
2933
}
30-
table.servers td.offline {
34+
table.servers td.offline,
35+
table.serverview span.offline {
3136
color: #d74747;
3237
}
33-
table.servers td.online {
38+
table.servers td.online,
39+
table.serverview span.online {
3440
color: #47d747;
3541
}
36-
table.servers td.disabled {
42+
table.servers td.disabled,
43+
table.serverview span.disabled {
3744
color: #d74747;
3845
}

src/templates/Server/View.phtml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace BNETDocs\Templates\Server;
4+
5+
use \BNETDocs\Libraries\Server;
6+
use \CarlBennett\MVC\Libraries\Common;
7+
use \CarlBennett\MVC\Libraries\Pair;
8+
9+
$object_id = $this->getContext()->server_id;
10+
$object = $this->getContext()->server;
11+
$object_status = null;
12+
$object_type = $this->getContext()->server_type;
13+
14+
$object_user = ($object ? $object->getUser() : null);
15+
$object_user_avatar = ($object_user ? $object_user->getAvatarURI(22) : null);
16+
$object_user_url = ($object_user ? $object_user->getURI() : null);
17+
18+
$title = ($object ? $object->getName() : "Server Not Found");
19+
$description = ($object ? "The BNETDocs Status Page for " . $object->getName()
20+
: "The requested server does not exist or could not be found.");
21+
22+
$this->opengraph->attach(new Pair("type", "article"));
23+
24+
$url = Common::relativeUrlToAbsolute("/server/" . urlencode($object_id));
25+
26+
if ($object) {
27+
28+
$url = $object->getURI();
29+
30+
$object_status_bitmask = $object->getStatusBitmask();
31+
if ($object_status_bitmask & Server::STATUS_ONLINE) {
32+
$object_status = "<span class=\"online\">Online</span>";
33+
} else {
34+
$object_status = "<span class=\"offline\">Offline</span>";
35+
}
36+
if ($object_status_bitmask & Server::STATUS_DISABLED) {
37+
$object_status .= ", <span class=\"disabled\">Disabled</span>";
38+
}
39+
40+
}
41+
42+
$this->opengraph->attach(new Pair("url", $url));
43+
44+
$this->additional_css[] = "/a/servers.css";
45+
require("./header.inc.phtml");
46+
?>
47+
<article>
48+
<?php if ($object) { ?>
49+
<a href="https://plus.google.com/share?url=<?php echo urlencode($url); ?>" rel="external" data-popup="1"><img class="header-button float-right" src="<?php echo Common::relativeUrlToAbsolute("/a/social-gplus-24px.png"); ?>"/></a>
50+
<a href="https://twitter.com/share?text=<?php echo urlencode($title); ?>&amp;url=<?php echo urlencode($url); ?>" rel="external" data-popup="1"><img class="header-button float-right" src="<?php echo Common::relativeUrlToAbsolute("/a/social-twitter-24px.png"); ?>"/></a>
51+
<a href="https://facebook.com/sharer/sharer.php?u=<?php echo urlencode($url); ?>" rel="external" data-popup="1"><img class="header-button float-right" src="<?php echo Common::relativeUrlToAbsolute("/a/social-facebook-24px.png"); ?>"/></a>
52+
<header>Server Status</header>
53+
<section>
54+
<?php require("./NYI.inc.phtml"); ?>
55+
</section>
56+
<section>
57+
<table class="serverview"><tbody>
58+
<tr><th>Label</th><td><?php echo filter_var($object->getLabel(), FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></td></tr>
59+
<tr><th>Address</th><td class="address"><?php echo filter_var($object->getAddress() . ":" . $object->getPort(), FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></td></tr>
60+
<tr><th>Owner</th><td><?php if ($object_user) { ?><a href="<?php echo $object_user_url; ?>"><img class="avatar" src="<?php echo $object_user_avatar; ?>"/> <?php echo filter_var($object_user->getName(), FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></a><?php } else { echo "Anonymous"; } ?></td></tr>
61+
<tr><th>Type</th><td><?php echo filter_var($object_type->getLabel(), FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></td></tr>
62+
<tr><th>Current Status:</th><td><?php echo $object_status; ?></td></tr>
63+
</tbody></table>
64+
</section>
65+
<?php } else { ?>
66+
<header class="red"><?php echo filter_var($title, FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></header>
67+
<section class="red"><?php echo filter_var($description, FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></section>
68+
<?php } ?>
69+
</article>
70+
<?php require("./footer.inc.phtml"); ?>

src/templates/Servers.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ require("./header.inc.phtml");
5353
?>
5454
<tr>
5555
<td class="status center <?php echo $status_subclass; ?>"><?php echo $status; ?></td>
56-
<td class="label"><?php echo ($server->getLabel() != $server->getAddress() && !is_null($server->getLabel()) ? $server->getLabel() : "&nbsp;"); ?></td>
56+
<td class="label"><?php echo ($server->getLabel() != $server->getAddress() && !empty($server->getLabel()) ? $server->getLabel() : "&nbsp;"); ?></td>
5757
<td class="address" onclick="bnetdocs.fSelectText(this);"><?php echo $server->getAddress(); ?>:<?php echo $server->getPort(); ?></td>
5858
<td class="details center"><a href="<?php echo $server->getURI(); ?>">View Details</a></td>
5959
</tr>

src/views/Server/ViewHtml.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace BNETDocs\Views\Server;
4+
5+
use \BNETDocs\Libraries\Exceptions\IncorrectModelException;
6+
use \BNETDocs\Libraries\Model;
7+
use \BNETDocs\Libraries\Template;
8+
use \BNETDocs\Libraries\View;
9+
use \BNETDocs\Models\Server\View as ServerViewModel;
10+
use \CarlBennett\MVC\Libraries\Common;
11+
12+
class ViewHtml extends View {
13+
14+
public function getMimeType() {
15+
return "text/html;charset=utf-8";
16+
}
17+
18+
public function render(Model &$model) {
19+
if (!$model instanceof ServerViewModel) {
20+
throw new IncorrectModelException();
21+
}
22+
(new Template($model, "Server/View"))->render();
23+
}
24+
25+
}

0 commit comments

Comments
 (0)