Skip to content

Commit 728b03e

Browse files
committed
Add missing components
- Adds packet application layer - Adds packet transport layer - Adds packet direction - Adds product
1 parent 7981124 commit 728b03e

14 files changed

+554
-23
lines changed

libraries/CSRF.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace BNETDocs\Libraries;
44

5-
use \BNETDocs\Libraries\Common;
65
use \BNETDocs\Libraries\Cache;
6+
use \BNETDocs\Libraries\Common;
77

88
class CSRF {
99

libraries/EmailService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace BNETDocs\Libraries;
44

5-
use \BNETDocs\Libraries\Emails\User\Register as UserRegisterEmail;
65
use \BNETDocs\Libraries\EmailMessage;
6+
use \BNETDocs\Libraries\Emails\User\Register as UserRegisterEmail;
77

88
class EmailService {
99

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace BNETDocs\Libraries\Exceptions;
4+
5+
use \BNETDocs\Libraries\Exceptions\BNETDocsException;
6+
use \BNETDocs\Libraries\Logger;
7+
use \Exception;
8+
9+
class PacketApplicationLayerFoundException extends BNETDocsException {
10+
11+
public function __construct($query, Exception &$prev_ex = null) {
12+
parent::__construct("Packet application layer not found", 16, $prev_ex);
13+
Logger::logMetric("query", $query);
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace BNETDocs\Libraries\Exceptions;
4+
5+
use \BNETDocs\Libraries\Exceptions\BNETDocsException;
6+
use \BNETDocs\Libraries\Logger;
7+
use \Exception;
8+
9+
class PacketTransportLayerFoundException extends BNETDocsException {
10+
11+
public function __construct($query, Exception &$prev_ex = null) {
12+
parent::__construct("Packet direction is invalid", 18, $prev_ex);
13+
Logger::logMetric("query", $query);
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace BNETDocs\Libraries\Exceptions;
4+
5+
use \BNETDocs\Libraries\Exceptions\BNETDocsException;
6+
use \BNETDocs\Libraries\Logger;
7+
use \Exception;
8+
9+
class PacketTransportLayerFoundException extends BNETDocsException {
10+
11+
public function __construct($query, Exception &$prev_ex = null) {
12+
parent::__construct("Packet transport layer not found", 17, $prev_ex);
13+
Logger::logMetric("query", $query);
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace BNETDocs\Libraries\Exceptions;
4+
5+
use \BNETDocs\Libraries\Exceptions\BNETDocsException;
6+
use \BNETDocs\Libraries\Logger;
7+
use \Exception;
8+
9+
class ProductNotFoundException extends BNETDocsException {
10+
11+
public function __construct($query, Exception &$prev_ex = null) {
12+
parent::__construct("Product not found", 19, $prev_ex);
13+
Logger::logMetric("query", $query);
14+
}
15+
16+
}

libraries/Exceptions/Reference.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ Error Reference
33

44
All of the following errors are subclassed from the `BNETDocsException` class.
55

6-
| Error Code | Error Name | Error Message |
7-
| ---------- | ------------------------------- | ---------------------------------------------------- |
8-
| 1 | `ClassNotFoundException` | Required class `$className` not found |
9-
| 2 | `ControllerNotFoundException` | Unable to find a suitable controller given the path |
10-
| 3 | `IncorrectModelException` | Incorrect model provided to view |
11-
| 4 | `TemplateNotFoundException` | Unable to locate template required to load this view |
12-
| 5 | `DatabaseUnavailableException` | All configured databases are unavailable |
13-
| 6 | `QueryException` | `$message` |
14-
| 7 | `UserNotFoundException` | User not found |
15-
| 8 | `NewsPostNotFoundException` | News post not found |
16-
| 9 | `ServerNotFoundException` | Server not found |
17-
| 10 | `ServerTypeNotFoundException` | Server type not found |
18-
| 11 | `UserProfileNotFoundException` | User profile not found |
19-
| 12 | `NewsCategoryNotFoundException` | News category not found |
20-
| 13 | `PacketNotFoundException` | Packet not found |
21-
| 14 | `DocumentNotFoundException` | Document not found |
22-
| 15 | `RecaptchaException` | `$message` |
6+
| Error Code | Error Name | Error Message |
7+
| ---------- | ----------------------------------------- | ---------------------------------------------------- |
8+
| 1 | `ClassNotFoundException` | Required class `$className` not found |
9+
| 2 | `ControllerNotFoundException` | Unable to find a suitable controller given the path |
10+
| 3 | `IncorrectModelException` | Incorrect model provided to view |
11+
| 4 | `TemplateNotFoundException` | Unable to locate template required to load this view |
12+
| 5 | `DatabaseUnavailableException` | All configured databases are unavailable |
13+
| 6 | `QueryException` | `$message` |
14+
| 7 | `UserNotFoundException` | User not found |
15+
| 8 | `NewsPostNotFoundException` | News post not found |
16+
| 9 | `ServerNotFoundException` | Server not found |
17+
| 10 | `ServerTypeNotFoundException` | Server type not found |
18+
| 11 | `UserProfileNotFoundException` | User profile not found |
19+
| 12 | `NewsCategoryNotFoundException` | News category not found |
20+
| 13 | `PacketNotFoundException` | Packet not found |
21+
| 14 | `DocumentNotFoundException` | Document not found |
22+
| 15 | `RecaptchaException` | `$message` |
23+
| 16 | `PacketApplicationLayerNotFoundException` | Packet application layer not found |
24+
| 17 | `PacketTransportLayerNotFoundException` | Packet transport layer not found |
25+
| 18 | `PacketDirectionInvalidException` | Packet direction is invalid |
26+
| 19 | `ProductNotFoundException` | Product not found |

libraries/Packet.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
use \BNETDocs\Libraries\Common;
77
use \BNETDocs\Libraries\Database;
88
use \BNETDocs\Libraries\DatabaseDriver;
9+
use \BNETDocs\Libraries\Exceptions\PacketDirectionInvalidException;
910
use \BNETDocs\Libraries\Exceptions\PacketNotFoundException;
1011
use \BNETDocs\Libraries\Exceptions\QueryException;
1112
use \BNETDocs\Libraries\Markdown;
13+
use \BNETDocs\Libraries\PacketApplicationLayer;
14+
use \BNETDocs\Libraries\PacketTransportLayer;
1215
use \BNETDocs\Libraries\User;
1316
use \DateTime;
1417
use \DateTimeZone;
@@ -19,6 +22,10 @@
1922

2023
class Packet {
2124

25+
const DIRECTION_CLIENT_SERVER = 1;
26+
const DIRECTION_SERVER_CLIENT = 2;
27+
const DIRECTION_PEER_TO_PEER = 3;
28+
2229
const OPTION_MARKDOWN = 0x00000001;
2330
const OPTION_PUBLISHED = 0x00000002;
2431

@@ -147,6 +154,10 @@ public function getOptionsBitmask() {
147154
return $this->options_bitmask;
148155
}
149156

157+
public function getPacketApplicationLayer() {
158+
return new PacketApplicationLayer($this->packet_application_layer);
159+
}
160+
150161
public function getPacketApplicationLayerId() {
151162
return $this->packet_application_layer_id;
152163
}
@@ -155,6 +166,26 @@ public function getPacketDirectionId() {
155166
return $this->packet_direction_id;
156167
}
157168

169+
public function getPacketDirectionLabel() {
170+
switch ($this->packet_direction_id) {
171+
case self::DIRECTION_CLIENT_SERVER: return "Client to Server";
172+
case self::DIRECTION_SERVER_CLIENT: return "Server to Client";
173+
case self::DIRECTION_PEER_TO_PEER: return "Peer to Peer";
174+
default:
175+
throw new PacketDirectionInvalidException($this->packet_direction_id);
176+
}
177+
}
178+
179+
public function getPacketDirectionTag() {
180+
switch ($this->packet_direction_id) {
181+
case self::DIRECTION_CLIENT_SERVER: return "C>S";
182+
case self::DIRECTION_SERVER_CLIENT: return "S>C";
183+
case self::DIRECTION_PEER_TO_PEER: return "P2P";
184+
default:
185+
throw new PacketDirectionInvalidException($this->packet_direction_id);
186+
}
187+
}
188+
158189
public function getPacketFormat() {
159190
return $this->packet_format;
160191
}
@@ -180,6 +211,10 @@ public function getPacketRemarks($prepare) {
180211
}
181212
}
182213

214+
public function getPacketTransportLayer() {
215+
return new PacketTransportLayer($this->packet_transport_layer_id);
216+
}
217+
183218
public function getPacketTransportLayerId() {
184219
return $this->packet_transport_layer_id;
185220
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace BNETDocs\Libraries;
4+
5+
use \BNETDocs\Libraries\Cache;
6+
use \BNETDocs\Libraries\Common;
7+
use \BNETDocs\Libraries\Database;
8+
use \BNETDocs\Libraries\DatabaseDriver;
9+
use \BNETDocs\Libraries\Exceptions\PacketApplicationLayerNotFoundException;
10+
use \BNETDocs\Libraries\Exceptions\QueryException;
11+
use \InvalidArgumentException;
12+
use \PDO;
13+
use \PDOException;
14+
use \StdClass;
15+
16+
class PacketApplicationLayer {
17+
18+
protected $id;
19+
protected $label;
20+
protected $tag;
21+
22+
public function __construct($data) {
23+
if (is_numeric($data)) {
24+
$this->id = (int) $data;
25+
$this->label = null;
26+
$this->tag = null;
27+
$this->refresh();
28+
} else if ($data instanceof StdClass) {
29+
self::normalize($data);
30+
$this->id = $data->id;
31+
$this->label = $data->label;
32+
$this->tag = $data->tag;
33+
} else {
34+
throw new InvalidArgumentException("Cannot use data argument");
35+
}
36+
}
37+
38+
public static function getAllPacketApplicationLayers() {
39+
if (!isset(Common::$database)) {
40+
Common::$database = DatabaseDriver::getDatabaseObject();
41+
}
42+
try {
43+
$stmt = Common::$database->prepare("
44+
SELECT
45+
`id`,
46+
`label`,
47+
`tag`
48+
FROM `packet_application_layers`
49+
ORDER BY `id` ASC;
50+
");
51+
if (!$stmt->execute()) {
52+
throw new QueryException("Cannot refresh packet application layers");
53+
}
54+
$packetapplicationlayers = [];
55+
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
56+
$packetapplicationlayers[] = new self($row);
57+
Common::$cache->set(
58+
"bnetdocs-packetapplicationlayer-" . $row->id, serialize($row), 300
59+
);
60+
}
61+
$stmt->closeCursor();
62+
return $packetapplicationlayers;
63+
} catch (PDOException $e) {
64+
throw new QueryException("Cannot refresh packet application layers", $e);
65+
}
66+
return null;
67+
}
68+
69+
public function getId() {
70+
return $this->id;
71+
}
72+
73+
public function getLabel() {
74+
return $this->label;
75+
}
76+
77+
public function getTag() {
78+
return $this->tag;
79+
}
80+
81+
protected static function normalize(StdClass &$data) {
82+
$data->id = (int) $data->id;
83+
$data->label = (string) $data->label;
84+
$data->tag = (string) $data->tag;
85+
86+
return true;
87+
}
88+
89+
public function refresh() {
90+
$cache_key = "bnetdocs-packetapplicationlayer-" . $this->id;
91+
$cache_val = Common::$cache->get($cache_key);
92+
if ($cache_val !== false) {
93+
$cache_val = unserialize($cache_val);
94+
$this->label = $cache_val->label;
95+
$this->tag = $cache_val->tag;
96+
return true;
97+
}
98+
if (!isset(Common::$database)) {
99+
Common::$database = DatabaseDriver::getDatabaseObject();
100+
}
101+
try {
102+
$stmt = Common::$database->prepare("
103+
SELECT
104+
`id`,
105+
`label`,
106+
`tag`
107+
FROM `packet_application_layers`
108+
WHERE `id` = :id
109+
LIMIT 1;
110+
");
111+
$stmt->bindParam(":id", $this->id, PDO::PARAM_INT);
112+
if (!$stmt->execute()) {
113+
throw new QueryException("Cannot refresh packet application layer");
114+
} else if ($stmt->rowCount() == 0) {
115+
throw new PacketApplicationLayerNotFoundException($this->id);
116+
}
117+
$row = $stmt->fetch(PDO::FETCH_OBJ);
118+
$stmt->closeCursor();
119+
self::normalize($row);
120+
$this->label = $row->label;
121+
$this->tag = $row->tag;
122+
Common::$cache->set($cache_key, serialize($row), 300);
123+
return true;
124+
} catch (PDOException $e) {
125+
throw new QueryException("Cannot refresh packet application layer", $e);
126+
}
127+
return false;
128+
}
129+
130+
}

0 commit comments

Comments
 (0)