Skip to content

Commit 58fd403

Browse files
committed
Merge branch 'develop' into phoenix
2 parents b3ce5d6 + aebf334 commit 58fd403

File tree

7 files changed

+270
-5
lines changed

7 files changed

+270
-5
lines changed

src/controllers/Packet/Delete.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace BNETDocs\Controllers\Packet;
4+
5+
use \BNETDocs\Libraries\Authentication;
6+
use \BNETDocs\Libraries\CSRF;
7+
use \BNETDocs\Libraries\Packet;
8+
use \BNETDocs\Libraries\EventTypes;
9+
use \BNETDocs\Libraries\Exceptions\PacketNotFoundException;
10+
use \BNETDocs\Libraries\Logger;
11+
use \BNETDocs\Libraries\User;
12+
use \BNETDocs\Models\Packet\Delete as PacketDeleteModel;
13+
14+
use \CarlBennett\MVC\Libraries\Common;
15+
use \CarlBennett\MVC\Libraries\Controller;
16+
use \CarlBennett\MVC\Libraries\Router;
17+
use \CarlBennett\MVC\Libraries\View;
18+
19+
use \InvalidArgumentException;
20+
21+
class Delete extends Controller {
22+
23+
public function &run(Router &$router, View &$view, array &$args) {
24+
25+
$data = $router->getRequestQueryArray();
26+
$model = new PacketDeleteModel();
27+
$model->csrf_id = mt_rand();
28+
$model->csrf_token = CSRF::generate($model->csrf_id);
29+
$model->error = null;
30+
$model->id = (isset($data["id"]) ? $data["id"] : null);
31+
$model->packet = null;
32+
$model->title = null;
33+
$model->user = Authentication::$user;
34+
35+
$model->acl_allowed = ($model->user && $model->user->getAcl(
36+
User::OPTION_ACL_PACKET_DELETE
37+
));
38+
39+
try { $model->packet = new Packet($model->id); }
40+
catch (PacketNotFoundException $e) { $model->packet = null; }
41+
catch (InvalidArgumentException $e) { $model->packet = null; }
42+
43+
if ($model->packet === null) {
44+
$model->error = "NOT_FOUND";
45+
} else {
46+
$model->title = $model->packet->getPacketDirectionTag() .
47+
' ' . $model->packet->getPacketName();
48+
49+
if ($router->getRequestMethod() == "POST") {
50+
$this->tryDelete($router, $model);
51+
}
52+
}
53+
54+
$view->render($model);
55+
56+
$model->_responseCode = ($model->acl_allowed ? 200 : 403);
57+
$model->_responseHeaders["Content-Type"] = $view->getMimeType();
58+
$model->_responseTTL = 0;
59+
60+
return $model;
61+
62+
}
63+
64+
protected function tryDelete(Router &$router, PacketDeleteModel &$model) {
65+
if (!isset($model->user)) {
66+
$model->error = "NOT_LOGGED_IN";
67+
return;
68+
}
69+
70+
$data = $router->getRequestBodyArray();
71+
$csrf_id = (isset($data["csrf_id" ]) ? $data["csrf_id" ] : null);
72+
$csrf_token = (isset($data["csrf_token"]) ? $data["csrf_token"] : null);
73+
$csrf_valid = CSRF::validate($csrf_id, $csrf_token);
74+
75+
if (!$csrf_valid) {
76+
$model->error = "INVALID_CSRF";
77+
return;
78+
}
79+
CSRF::invalidate($csrf_id);
80+
81+
if (!$model->acl_allowed) {
82+
$model->error = "ACL_NOT_SET";
83+
return;
84+
}
85+
86+
$model->error = false;
87+
88+
$id = (int) $model->id;
89+
$user_id = $model->user->getId();
90+
91+
try {
92+
93+
$success = Packet::delete($id);
94+
95+
} catch (QueryException $e) {
96+
97+
// SQL error occurred. We can show a friendly message to the user while
98+
// also notifying this problem to staff.
99+
Logger::logException($e);
100+
101+
$success = false;
102+
103+
}
104+
105+
if (!$success) {
106+
$model->error = "INTERNAL_ERROR";
107+
} else {
108+
$model->error = false;
109+
}
110+
111+
Logger::logEvent(
112+
EventTypes::PACKET_DELETED,
113+
$user_id,
114+
getenv("REMOTE_ADDR"),
115+
json_encode([
116+
"error" => $model->error,
117+
"packet_id" => $id,
118+
])
119+
);
120+
}
121+
122+
}

src/libraries/NewsPost.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,22 @@ public function save() {
408408
LIMIT 1;
409409
");
410410
$stmt->bindParam(":category_id", $this->category_id, PDO::PARAM_INT);
411-
$stmt->bindParam(":content", $this->content, PDO::PARAM_INT);
412-
$stmt->bindParam(":created_dt", $this->created_datetime, PDO::PARAM_INT);
411+
$stmt->bindParam(":content", $this->content, PDO::PARAM_STR);
412+
$stmt->bindParam(":created_dt", $this->created_datetime, PDO::PARAM_STR);
413413
$stmt->bindParam(":edited_count", $this->edited_count, PDO::PARAM_INT);
414-
$stmt->bindParam(":edited_dt", $this->edited_datetime, PDO::PARAM_INT);
414+
if (is_null($this->edited_datetime)) {
415+
$stmt->bindParam(":edited_dt", null, PDO::PARAM_NULL);
416+
} else {
417+
$stmt->bindParam(":edited_dt", $this->edited_datetime, PDO::PARAM_STR);
418+
}
415419
$stmt->bindParam(":id", $this->id, PDO::PARAM_INT);
416420
$stmt->bindParam(":options", $this->options_bitmask, PDO::PARAM_INT);
417-
$stmt->bindParam(":title", $this->title, PDO::PARAM_INT);
418-
$stmt->bindParam(":user_id", $this->user_id, PDO::PARAM_INT);
421+
$stmt->bindParam(":title", $this->title, PDO::PARAM_STR);
422+
if (is_null($this->user_id)) {
423+
$stmt->bindParam(":user_id", null, PDO::PARAM_NULL);
424+
} else {
425+
$stmt->bindParam(":user_id", $this->user_id, PDO::PARAM_INT);
426+
}
419427
if (!$stmt->execute()) {
420428
throw new QueryException("Cannot save news post");
421429
}

src/libraries/Packet.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,29 @@ public function __construct( $data ) {
8989
}
9090
}
9191

92+
public static function delete($id) {
93+
if (!isset(Common::$database)) {
94+
Common::$database = DatabaseDriver::getDatabaseObject();
95+
}
96+
$successful = false;
97+
try {
98+
$stmt = Common::$database->prepare("
99+
DELETE FROM `packets` WHERE `id` = :id LIMIT 1;
100+
");
101+
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
102+
$successful = $stmt->execute();
103+
$stmt->closeCursor();
104+
if ($successful) {
105+
Common::$cache->delete("bnetdocs-packet-" . (int) $id);
106+
Common::$cache->delete("bnetdocs-packets");
107+
}
108+
} catch (PDOException $e) {
109+
throw new QueryException("Cannot delete packet");
110+
} finally {
111+
return $successful;
112+
}
113+
}
114+
92115
public static function &getAllPackets(
93116
$order = null, $limit = null, $index = null
94117
) {

src/main.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ function main() {
165165
//$router->addRoute( // URL: /packet/create
166166
// "#^/packet/create/?$#", "Packet\\Create", "Packet\\CreateHtml"
167167
//);
168+
$router->addRoute( // URL: /packet/delete
169+
"#^/packet/delete/?$#", "Packet\\Delete", "Packet\\DeleteHtml"
170+
);
168171
$router->addRoute( // URL: /packet/edit
169172
"#^/packet/edit/?$#", "Packet\\Edit", "Packet\\EditHtml"
170173
);

src/models/Packet/Delete.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace BNETDocs\Models\Packet;
4+
5+
use \CarlBennett\MVC\Libraries\Model;
6+
7+
class Delete extends Model {
8+
9+
public $acl_allowed;
10+
public $csrf_id;
11+
public $csrf_token;
12+
public $error;
13+
public $id;
14+
public $packet;
15+
public $title;
16+
public $user;
17+
18+
}

src/templates/Packet/Delete.phtml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace BNETDocs\Templates\Packet;
4+
5+
use \CarlBennett\MVC\Libraries\Pair;
6+
7+
$title = "Delete Packet";
8+
$description = "This form allows an individual to delete a packet.";
9+
10+
$this->opengraph->attach(new Pair("url", "/packet/delete"));
11+
$this->opengraph->attach(new Pair("type", "article"));
12+
13+
switch ($this->getContext()->error) {
14+
case "ACL_NOT_SET":
15+
$message = "You do not have the privilege to delete packets.";
16+
break;
17+
case "NOT_FOUND":
18+
$message = "Cannot find packet by that id.";
19+
break;
20+
case "NOT_LOGGED_IN":
21+
$message = "You must be logged in to delete packets.";
22+
break;
23+
case "INVALID_CSRF":
24+
$message = "The Cross-Site Request Forgery token was invalid. Either the "
25+
. "delete packet form expired, or this may have been a malicious "
26+
. "attempt to delete a packet.";
27+
break;
28+
case "INTERNAL_ERROR":
29+
$message = "An internal error occurred while processing your request. "
30+
. "Our staff has been notified of the issue. Try again later.";
31+
break;
32+
default:
33+
$message = $this->getContext()->error;
34+
}
35+
36+
$this->additional_css[] = "/a/forms.css";
37+
require("./header.inc.phtml");
38+
?>
39+
<article>
40+
<?php if (is_null($this->getContext()->error)) { ?>
41+
<header>Delete Packet</header>
42+
<form method="POST" action="?id=<?php echo
43+
htmlspecialchars($this->getContext()->id, ENT_HTML5, "UTF-8"); ?>">
44+
<input type="hidden" name="csrf_id" value="<?php echo $this->getContext()->csrf_id; ?>"/>
45+
<input type="hidden" name="csrf_token" value="<?php echo $this->getContext()->csrf_token; ?>"/>
46+
<section>
47+
<p>Are you sure you wish to delete this packet?</p>
48+
<p><input type="text" readonly="readonly" value="<?php echo filter_var($this->getContext()->title, FILTER_SANITIZE_STRING); ?>" tabindex="1"/></p>
49+
<p><input type="submit" value="Delete Packet" tabindex="2" autofocus="autofocus"/></p>
50+
</section>
51+
</form>
52+
<?php } else if ($this->getContext()->error === false) { ?>
53+
<header class="green">Packet Deleted</header>
54+
<section class="green">
55+
<p>You have successfully deleted the packet!</p>
56+
<p>Use the navigation to the left to move to another page.</p>
57+
</section>
58+
<?php } else { ?>
59+
<header class="red">Delete Packet</header>
60+
<section class="red">
61+
<p>An error occurred while attempting to delete the packet.</p>
62+
<p><?php echo $message; ?></p>
63+
<p>Use the navigation to the left to move to another page.</p>
64+
</section>
65+
<?php } ?>
66+
</article>
67+
<?php require("./footer.inc.phtml"); ?>

src/views/Packet/DeleteHtml.php

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

0 commit comments

Comments
 (0)