Skip to content

Commit deb1170

Browse files
committed
Refactoring packet editing a bit
1 parent 6769e85 commit deb1170

File tree

4 files changed

+158
-81
lines changed

4 files changed

+158
-81
lines changed

src/controllers/Packet/Edit.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use \DateTime;
2222
use \DateTimeZone;
2323
use \InvalidArgumentException;
24+
use \OutOfBoundsException;
2425

2526
class Edit extends Controller {
2627
public function &run(Router &$router, View &$view, array &$args) {
@@ -58,7 +59,7 @@ public function &run(Router &$router, View &$view, array &$args) {
5859
);
5960

6061
$model->deprecated = $model->packet->isDeprecated();
61-
$model->id = $model->packet->getPacketId();
62+
$model->id = $model->packet->getPacketId(true);
6263
$model->name = $model->packet->getPacketName();
6364
$model->format = $model->packet->getPacketFormat();
6465
$model->remarks = $model->packet->getPacketRemarks(false);
@@ -142,6 +143,12 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
142143
// updated separately.
143144
$model->packet->setUsedBy($used_by);
144145

146+
} catch (OutOfBoundsException $e) {
147+
148+
// Some value was outside of a boundary
149+
Logger::logException($e);
150+
$success = false;
151+
145152
} catch (QueryException $e) {
146153

147154
// SQL error occurred. We can show a friendly message to the user while

src/libraries/Packet.php

+67-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use \DateTimeZone;
1414
use \InvalidArgumentException;
1515
use \JsonSerializable;
16+
use \OutOfBoundsException;
1617
use \PDO;
1718
use \PDOException;
1819
use \Parsedown;
@@ -27,6 +28,12 @@ class Packet implements JsonSerializable {
2728
const DIRECTION_SERVER_CLIENT = 2;
2829
const DIRECTION_PEER_TO_PEER = 3;
2930

31+
const MAX_EDITED_COUNT = 0xFFFFFFFFFFFFFFFF;
32+
const MAX_PACKET_FORMAT = 0xFFFF;
33+
const MAX_PACKET_ID = 0xFF;
34+
const MAX_PACKET_NAME = 191;
35+
const MAX_PACKET_REMARKS = 0xFFFF;
36+
3037
const OPTION_MARKDOWN = 0x00000001;
3138
const OPTION_PUBLISHED = 0x00000002;
3239
const OPTION_DEPRECATED = 0x00000004;
@@ -663,7 +670,13 @@ public function setDeprecated( $value ) {
663670
}
664671
}
665672

666-
public function setEditedCount( $value ) {
673+
public function setEditedCount(int $value)
674+
{
675+
if ($value < 0 || $value > self::MAX_EDITED_COUNT)
676+
{
677+
throw new OutOfBoundsException();
678+
}
679+
667680
$this->edited_count = $value;
668681
}
669682

@@ -688,19 +701,65 @@ public function setMarkdown( $value ) {
688701
}
689702
}
690703

691-
public function setPacketFormat( $value ) {
704+
public function setPacketFormat(string $value)
705+
{
706+
if (strlen($value) > self::MAX_PACKET_FORMAT)
707+
{
708+
throw new OutOfBoundsException();
709+
}
710+
692711
$this->packet_format = $value;
693712
}
694713

695-
public function setPacketId( $value ) {
696-
$this->packet_id = $value;
714+
/**
715+
* Sets the packet/message id
716+
*
717+
* @param mixed $value The packet id. Supports hexadecimal and octal input.
718+
*/
719+
public function setPacketId($value)
720+
{
721+
if (is_string($value) && strlen($value) >= 2
722+
&& substr($value, 0, 2) == '0x')
723+
{
724+
$this->packet_id = hexdec(substr($value, 2));
725+
}
726+
else if (is_string($value) && strlen($value) >= 1
727+
&& substr($value, 0, 1) == '0')
728+
{
729+
$this->packet_id = octdec(substr($value, 1));
730+
}
731+
else if (is_numeric($value))
732+
{
733+
$this->packet_id = (int) $value;
734+
}
735+
else
736+
{
737+
throw new InvalidArgumentException();
738+
}
739+
740+
if ($this->packet_id < 0 || $this->packet_id > self::MAX_PACKET_ID)
741+
{
742+
throw new OutOfBoundsException();
743+
}
697744
}
698745

699-
public function setPacketName( $value ) {
746+
public function setPacketName(string $value)
747+
{
748+
if (strlen($value) > self::MAX_PACKET_NAME)
749+
{
750+
throw new OutOfBoundsException();
751+
}
752+
700753
$this->packet_name = $value;
701754
}
702755

703-
public function setPacketRemarks( $value ) {
756+
public function setPacketRemarks(string $value)
757+
{
758+
if (strlen($value) > self::MAX_PACKET_REMARKS)
759+
{
760+
throw new OutOfBoundsException();
761+
}
762+
704763
$this->packet_remarks = $value;
705764
}
706765

@@ -768,6 +827,7 @@ public function update() {
768827
`packet_application_layer_id` = :app_layer_id,
769828
`packet_direction_id` = :direction,
770829
`packet_format` = :format,
830+
`packet_id` = :pid,
771831
`packet_name` = :name,
772832
`packet_remarks` = :remarks,
773833
`packet_transport_layer_id` = :transport_layer_id,
@@ -796,6 +856,7 @@ public function update() {
796856
$stmt->bindParam( ':id', $this->id, PDO::PARAM_INT );
797857
$stmt->bindParam( ':name', $this->packet_name, PDO::PARAM_STR );
798858
$stmt->bindParam( ':options', $this->options_bitmask, PDO::PARAM_INT );
859+
$stmt->bindParam( ':pid', $this->packet_id, PDO::PARAM_INT );
799860
$stmt->bindParam( ':remarks', $this->packet_remarks, PDO::PARAM_STR );
800861

801862
$stmt->bindParam(

src/templates/Packet/Edit.phtml

+21-74
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\Packet;
3-
use \BNETDocs\Libraries\Comment;
4-
use \CarlBennett\MVC\Libraries\Common;
53
use \CarlBennett\MVC\Libraries\Pair;
64
$title = 'Edit Packet';
75
$description = 'This form allows an individual to edit a packet.';
@@ -26,75 +24,24 @@ $packet_name = filter_var($this->getContext()->name, FILTER_SANITIZE_FULL_SPECIA
2624
$packet_remarks = filter_var($this->getContext()->remarks, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
2725
$packet_used_by = $this->getContext()->used_by;
2826
$products = $this->getContext()->products;
29-
require('./header.inc.phtml'); ?>
30-
<div class="container">
31-
<? if ($error !== false) { ?>
32-
<h1>Edit Packet</h1>
33-
<? if (!empty($message)) { ?>
34-
<div class="alert alert-danger"><p class="mb-0"><?=$message?></p></div>
35-
<? } ?>
36-
<? if ($error != 'NOT_FOUND') { ?>
37-
<form method="POST" action="?id=<?=$packet_id?>">
38-
<div class="row">
39-
<div class="col-md-3">
40-
<div class="form-group">
41-
<label class="font-weight-bold" for="id">Id:</label>
42-
<input class="bg-dark border border-primary form-control text-light" type="text" name="id" id="id" placeholder="Enter the message id here" tabindex="1" required autofocus="autofocus" value="<?=$id?>"/>
43-
</div>
44-
</div><div class="col-md-6">
45-
<div class="form-group">
46-
<label class="font-weight-bold" for="name">Name:</label>
47-
<input class="bg-dark border border-primary form-control text-light" type="text" name="name" id="name" placeholder="Enter the message name here" tabindex="2" required value="<?=$packet_name?>"/>
48-
</div>
49-
</div><div class="col-md-3">
50-
<div class="form-group">
51-
<label class="font-weight-bold">Options:</label>
52-
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="deprecated" name="deprecated" value="1"<?=($this->getContext()->deprecated?' checked="checked"':'')?> tabindex="6"/><label class="custom-control-label text-danger" for="deprecated">Deprecated</label></div>
53-
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="research" name="research" value="1"<?=($this->getContext()->research?' checked="checked"':'')?> tabindex="7"/> <label class="custom-control-label text-warning" for="research">In Research</label></div>
54-
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="published" name="published" value="1"<?=($this->getContext()->published?' checked="checked"':'')?> tabindex="8"/> <label class="custom-control-label text-success" for="published">Published</label></div>
55-
</div>
56-
</div>
57-
</div>
58-
<div class="form-group">
59-
<label class="font-weight-bold" for="format">Format:</label>
60-
<textarea class="bg-dark border border-primary form-control language-objectivec text-light text-monospace" style="height:120px;" name="format" id="format" placeholder="Enter the message format here" tabindex="3" required><?=$packet_format?></textarea>
61-
</div>
62-
<div class="form-group">
63-
<label class="font-weight-bold">Used by:</label>
64-
<table class="table table-borderless table-sm">
65-
<thead></thead><tbody>
66-
<? $product_ubound = count($products);
67-
for ($product_i = 0; $product_i < $product_ubound; ++$product_i)
68-
{
69-
if ($product_i % 2 === 0) echo '<tr>';
70-
$p = $products[$product_i];
71-
$p_id = $p->getBnetProductId();
72-
$p_label = $p->getLabel();
73-
printf('<td><div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="used_by_%s" name="used_by[]" value="%s"%s/><label class="custom-control-label" for="used_by_%s">%s</label></div></td>', $p_id, $p_id, (in_array($p, $packet_used_by) ? ' checked="checked"' : ''), $p_id, $p_label);
74-
if ($product_i % 2 === 1) echo '</tr>';
75-
} ?>
76-
</tbody>
77-
</table>
78-
</div>
79-
<div class="form-group">
80-
<label class="font-weight-bold" for="remarks">Remarks:</label>
81-
<span class="float-right">
82-
<div class="custom-control custom-switch">
83-
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="5"
84-
title="Use markdown or use raw HTML" value="1"<?=($this->getContext()->markdown?' checked="checked"':'')?>/>
85-
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
86-
</div>
87-
</span>
88-
<textarea class="bg-dark border border-primary form-control text-light" style="height:200px;" name="remarks" id="remarks" placeholder="Enter the message remarks here" tabindex="4" required><?=$packet_remarks?></textarea>
89-
</div>
90-
<div class="form-group text-center">
91-
<input class="btn btn-success" type="submit" value="Save" tabindex="9"/>
92-
</div>
93-
</form>
94-
<? $comment_parent_type = Comment::PARENT_TYPE_PACKET; $comment_parent_id = $packet_id; require('./Comment/Section.inc.phtml'); } ?>
95-
<? } else { ?>
96-
<h1 class="text-success">Edit Packet</h1>
97-
<div class="alert alert-success"><p class="mb-0">Your packet has been edited.</p></div>
98-
<? } ?>
99-
</div>
100-
<? require('./footer.inc.phtml'); ?>
27+
require('./header.inc.phtml');
28+
echo '<div class="container">' . PHP_EOL;
29+
if ($error !== false)
30+
{
31+
printf('<h1>%s</h1>%s', $title, PHP_EOL);
32+
if (!empty($message))
33+
{
34+
printf('<div class="alert alert-danger"><p class="mb-0">%s</p></div>%s', $message, PHP_EOL);
35+
}
36+
if ($error != 'NOT_FOUND')
37+
{
38+
require('./Packet/Form.inc.phtml');
39+
}
40+
}
41+
else
42+
{
43+
printf('<h1 class="text-success">%s</h1>%s', $title, PHP_EOL);
44+
printf('<div class="alert alert-success"><p class="mb-0">The packet has been edited successfully!</p></div>%s', PHP_EOL);
45+
}
46+
echo '</div>' . PHP_EOL;
47+
require('./footer.inc.phtml');

src/templates/Packet/Form.inc.phtml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
2+
namespace BNETDocs\Templates\Packet;
3+
use \BNETDocs\Libraries\Comment;
4+
use \CarlBennett\MVC\Libraries\Common; ?>
5+
<form method="POST" action="?id=<?=$packet_id?>">
6+
<div class="row">
7+
<div class="col-md-3">
8+
<div class="form-group">
9+
<label class="font-weight-bold" for="id">Id:</label>
10+
<input class="bg-dark border border-primary form-control text-light" type="text" name="id" id="id" placeholder="Enter the message id here" tabindex="1" required autofocus="autofocus" value="<?=$id?>"/>
11+
</div>
12+
</div><div class="col-md-6">
13+
<div class="form-group">
14+
<label class="font-weight-bold" for="name">Name:</label>
15+
<input class="bg-dark border border-primary form-control text-light" type="text" name="name" id="name" placeholder="Enter the message name here" tabindex="2" required value="<?=$packet_name?>"/>
16+
</div>
17+
</div><div class="col-md-3">
18+
<div class="form-group">
19+
<label class="font-weight-bold">Options:</label>
20+
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="deprecated" name="deprecated" value="1"<?=($this->getContext()->deprecated?' checked="checked"':'')?> tabindex="6"/><label class="custom-control-label text-danger" for="deprecated">Deprecated</label></div>
21+
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="research" name="research" value="1"<?=($this->getContext()->research?' checked="checked"':'')?> tabindex="7"/> <label class="custom-control-label text-warning" for="research">In Research</label></div>
22+
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="published" name="published" value="1"<?=($this->getContext()->published?' checked="checked"':'')?> tabindex="8"/> <label class="custom-control-label text-success" for="published">Published</label></div>
23+
</div>
24+
</div>
25+
</div>
26+
<div class="form-group">
27+
<label class="font-weight-bold" for="format">Format:</label>
28+
<textarea class="bg-dark border border-primary form-control language-objectivec text-light text-monospace" style="height:120px;" name="format" id="format" placeholder="Enter the message format here" tabindex="3" required><?=$packet_format?></textarea>
29+
</div>
30+
<div class="form-group">
31+
<label class="font-weight-bold">Used by:</label>
32+
<table class="table table-borderless table-sm">
33+
<thead></thead><tbody>
34+
<? $product_ubound = count($products);
35+
for ($product_i = 0; $product_i < $product_ubound; ++$product_i)
36+
{
37+
if ($product_i % 2 === 0) echo '<tr>';
38+
$p = $products[$product_i];
39+
$p_id = $p->getBnetProductId();
40+
$p_label = $p->getLabel();
41+
printf('<td><div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="used_by_%s" name="used_by[]" value="%s"%s/><label class="custom-control-label" for="used_by_%s">%s</label></div></td>', $p_id, $p_id, (in_array($p, $packet_used_by) ? ' checked="checked"' : ''), $p_id, $p_label);
42+
if ($product_i % 2 === 1) echo '</tr>';
43+
} ?>
44+
</tbody>
45+
</table>
46+
</div>
47+
<div class="form-group">
48+
<label class="font-weight-bold" for="remarks">Remarks:</label>
49+
<span class="float-right">
50+
<div class="custom-control custom-switch">
51+
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="5"
52+
title="Use markdown or use raw HTML" value="1"<?=($this->getContext()->markdown?' checked="checked"':'')?>/>
53+
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
54+
</div>
55+
</span>
56+
<textarea class="bg-dark border border-primary form-control text-light" style="height:200px;" name="remarks" id="remarks" placeholder="Enter the message remarks here" tabindex="4" required><?=$packet_remarks?></textarea>
57+
</div>
58+
<div class="form-group text-center">
59+
<input class="btn btn-success" type="submit" value="Save" tabindex="9"/>
60+
</div>
61+
</form>
62+
<? $comment_parent_type = Comment::PARENT_TYPE_PACKET; $comment_parent_id = $packet_id; require('./Comment/Section.inc.phtml'); ?>

0 commit comments

Comments
 (0)