Skip to content

Commit e3e0015

Browse files
committed
Merge branch 'develop' into phoenix
2 parents 0434b7a + e6e27ea commit e3e0015

File tree

18 files changed

+426
-37
lines changed

18 files changed

+426
-37
lines changed

src/controllers/Comment/Edit.php

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
namespace BNETDocs\Controllers\Comment;
4+
5+
use \BNETDocs\Libraries\Authentication;
6+
use \BNETDocs\Libraries\CSRF;
7+
use \BNETDocs\Libraries\Comment;
8+
use \BNETDocs\Libraries\EventTypes;
9+
use \BNETDocs\Libraries\Exceptions\CommentNotFoundException;
10+
use \BNETDocs\Libraries\Logger;
11+
use \BNETDocs\Libraries\User;
12+
13+
use \BNETDocs\Models\Comment\Edit as CommentEditModel;
14+
15+
use \CarlBennett\MVC\Libraries\Common;
16+
use \CarlBennett\MVC\Libraries\Controller;
17+
use \CarlBennett\MVC\Libraries\Exceptions\QueryException;
18+
use \CarlBennett\MVC\Libraries\Router;
19+
use \CarlBennett\MVC\Libraries\View;
20+
21+
use \DateTime;
22+
use \DateTimeZone;
23+
use \InvalidArgumentException;
24+
use \UnexpectedValueException;
25+
26+
class Edit extends Controller {
27+
public function &run( Router &$router, View &$view, array &$args ) {
28+
29+
$query_data = $router->getRequestQueryArray();
30+
$post_data = $router->getRequestBodyArray();
31+
32+
$model = new CommentEditModel();
33+
34+
$model->csrf_id = mt_rand();
35+
$model->csrf_token = CSRF::generate( $model->csrf_id );
36+
$model->user = Authentication::$user;
37+
38+
$model->id = (
39+
isset( $query_data[ 'id' ]) ? $query_data[ 'id' ] : null
40+
);
41+
$model->content = (
42+
isset( $post_data[ 'content' ]) ? $post_data[ 'content' ] : null
43+
);
44+
45+
try { $model->comment = new Comment( $model->id ); }
46+
catch ( CommentNotFoundException $e ) { $model->comment = null; }
47+
catch ( InvalidArgumentException $e ) { $model->comment = null; }
48+
49+
$model->acl_allowed = ( $model->user && (
50+
$model->user->getAcl( User::OPTION_ACL_COMMENT_MODIFY ) ||
51+
$model->user->getId() == $model->comment->getUserId()
52+
));
53+
54+
if ( is_null( $model->comment )) {
55+
$model->error = 'NOT_FOUND';
56+
} else {
57+
if ( is_null( $model->content )) {
58+
$model->content = $model->comment->getContent( false );
59+
}
60+
61+
$model->parent_type = $model->comment->getParentType();
62+
$model->parent_id = $model->comment->getParentId();
63+
64+
switch ( $model->parent_type ) {
65+
case Comment::PARENT_TYPE_DOCUMENT:
66+
$model->return_url = '/document/' . $model->parent_id; break;
67+
case Comment::PARENT_TYPE_COMMENT:
68+
$model->return_url = '/comment/' . $model->parent_id; break;
69+
case Comment::PARENT_TYPE_NEWS_POST:
70+
$model->return_url = '/news/' . $model->parent_id; break;
71+
case Comment::PARENT_TYPE_PACKET:
72+
$model->return_url = '/packet/' . $model->parent_id; break;
73+
case Comment::PARENT_TYPE_SERVER:
74+
$model->return_url = '/server/' . $model->parent_id; break;
75+
case Comment::PARENT_TYPE_USER:
76+
$model->return_url = '/user/' . $model->parent_id; break;
77+
default: throw new UnexpectedValueException(
78+
'Parent type: ' . $model->parent_type
79+
);
80+
}
81+
$model->return_url = Common::relativeUrlToAbsolute( $model->return_url );
82+
83+
if ( $router->getRequestMethod() == 'POST' ) {
84+
$this->tryModify( $router, $model );
85+
}
86+
}
87+
88+
$view->render( $model );
89+
90+
$model->_responseCode = ( $model->acl_allowed ? 200 : 403 );
91+
$model->_responseHeaders[ 'Content-Type' ] = $view->getMimeType();
92+
$model->_responseTTL = 0;
93+
94+
return $model;
95+
}
96+
97+
protected function tryModify( Router &$router, CommentEditModel &$model ) {
98+
if ( !isset( $model->user )) {
99+
$model->error = 'NOT_LOGGED_IN';
100+
return;
101+
}
102+
if ( !$model->acl_allowed ) {
103+
$model->error = 'ACL_NOT_SET';
104+
return;
105+
}
106+
107+
$post_data = $router->getRequestBodyArray();
108+
109+
$csrf_id = (
110+
isset( $post_data[ 'csrf_id' ]) ? $post_data[ 'csrf_id' ] : null
111+
);
112+
$csrf_token = (
113+
isset( $post_data[ 'csrf_token' ]) ? $post_data[ 'csrf_token' ] : null
114+
);
115+
$csrf_valid = CSRF::validate( $csrf_id, $csrf_token );
116+
117+
if ( !$csrf_valid ) {
118+
$model->error = 'INVALID_CSRF';
119+
return;
120+
}
121+
122+
CSRF::invalidate( $csrf_id );
123+
124+
$model->error = false;
125+
126+
$id = (int) $model->id;
127+
$parent_type = (int) $model->parent_type;
128+
$parent_id = (int) $model->parent_id;
129+
$user_id = $model->user->getId();
130+
131+
$log_key = null;
132+
switch ( $parent_type ) {
133+
case Comment::PARENT_TYPE_DOCUMENT:
134+
$log_key = EventTypes::COMMENT_EDITED_DOCUMENT; break;
135+
case Comment::PARENT_TYPE_COMMENT:
136+
$log_key = EventTypes::COMMENT_EDITED_COMMENT; break;
137+
case Comment::PARENT_TYPE_NEWS_POST:
138+
$log_key = EventTypes::COMMENT_EDITED_NEWS; break;
139+
case Comment::PARENT_TYPE_PACKET:
140+
$log_key = EventTypes::COMMENT_EDITED_PACKET; break;
141+
case Comment::PARENT_TYPE_SERVER:
142+
$log_key = EventTypes::COMMENT_EDITED_SERVER; break;
143+
case Comment::PARENT_TYPE_USER:
144+
$log_key = EventTypes::COMMENT_EDITED_USER; break;
145+
default: throw new UnexpectedValueException(
146+
'Parent type: ' . $parent_type
147+
);
148+
}
149+
150+
try {
151+
152+
$model->comment->setContent( $model->content );
153+
$model->comment->setEditedCount( $model->comment->getEditedCount() + 1 );
154+
$model->comment->setEditedDateTime(
155+
new DateTime( 'now', new DateTimeZone( 'Etc/UTC' ))
156+
);
157+
158+
$success = $model->comment->save();
159+
160+
} catch ( QueryException $e ) {
161+
162+
// SQL error occurred. We can show a friendly message to the user while
163+
// also notifying this problem to staff.
164+
Logger::logException( $e );
165+
166+
$success = false;
167+
168+
}
169+
170+
if ( !$success ) {
171+
$model->error = 'INTERNAL_ERROR';
172+
} else {
173+
$model->error = false;
174+
}
175+
176+
Logger::logEvent(
177+
$log_key,
178+
$user_id,
179+
getenv( 'REMOTE_ADDR' ),
180+
json_encode([
181+
'error' => $model->error,
182+
'comment_id' => $id,
183+
'content' => $model->content,
184+
'parent_type' => $parent_type,
185+
'parent_id' => $parent_id
186+
])
187+
);
188+
}
189+
}

src/controllers/Document/Edit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected function handlePost(Router &$router, DocumentEditModel &$model) {
121121
$model->document->getEditedCount() + 1
122122
);
123123
$model->document->setEditedDateTime(
124-
new DateTime("now", new DateTimeZone("Etc/UTC"))
124+
new DateTime( 'now', new DateTimeZone( 'Etc/UTC' ))
125125
);
126126

127127
$success = $model->document->save();

src/controllers/Document/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function &run(Router &$router, View &$view, array &$args) {
4545

4646
// Objectify for JSON
4747
if ($view instanceof DocumentIndexJSONView) {
48-
$model->timestamp = new DateTime("now", new DateTimeZone("Etc/UTC"));
48+
$model->timestamp = new DateTime( 'now', new DateTimeZone( 'Etc/UTC' ));
4949
$documents = [];
5050
foreach ($model->documents as $document) {
5151
$documents[] = [

src/controllers/News/Edit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ protected function handlePost(Router &$router, NewsEditModel &$model) {
139139
$model->news_post->getEditedCount() + 1
140140
);
141141
$model->news_post->setEditedDateTime(
142-
new DateTime("now", new DateTimeZone("Etc/UTC"))
142+
new DateTime( 'now', new DateTimeZone( 'Etc/UTC' ))
143143
);
144144

145145
$success = $model->news_post->save();

src/controllers/Packet/Edit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
133133
$model->packet->getEditedCount() + 1
134134
);
135135
$model->packet->setEditedDateTime(
136-
new DateTime("now", new DateTimeZone("Etc/UTC"))
136+
new DateTime( 'now', new DateTimeZone( 'Etc/UTC' ))
137137
);
138138

139139
$success = $model->packet->update();

src/controllers/User/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function getUserInfo(UserViewModel &$model) {
9595
// How long have they been a member?
9696
$model->user_est = Common::intervalToString(
9797
$model->user->getCreatedDateTime()->diff(
98-
new DateTime("now", new DateTimeZone("Etc/UTC"))
98+
new DateTime( 'now', new DateTimeZone( 'Etc/UTC' ))
9999
)
100100
);
101101
$user_est_comma = strpos($model->user_est, ",");

src/libraries/Comment.php

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
class Comment implements JsonSerializable {
2121

22+
const CACHE_TTL = 300;
23+
2224
const PARENT_TYPE_DOCUMENT = 0;
2325
const PARENT_TYPE_COMMENT = 1;
2426
const PARENT_TYPE_NEWS_POST = 2;
@@ -160,11 +162,11 @@ public static function getAll($parent_type, $parent_id) {
160162
$ids[] = (int) $row->id;
161163
$objects[] = new self($row);
162164
Common::$cache->set(
163-
"bnetdocs-comment-" . $row->id, serialize($row), 300
165+
"bnetdocs-comment-" . $row->id, serialize($row), self::CACHE_TTL
164166
);
165167
}
166168
$stmt->closeCursor();
167-
Common::$cache->set($ck, implode(",", $ids), 300);
169+
Common::$cache->set($ck, implode(",", $ids), self::CACHE_TTL);
168170
return $objects;
169171
} catch (PDOException $e) {
170172
throw new QueryException("Cannot refresh comment", $e);
@@ -184,7 +186,7 @@ public function getCreatedDateTime() {
184186
if (is_null($this->created_datetime)) {
185187
return $this->created_datetime;
186188
} else {
187-
$tz = new DateTimeZone("UTC");
189+
$tz = new DateTimeZone( 'Etc/UTC' );
188190
$dt = new DateTime($this->created_datetime);
189191
$dt->setTimezone($tz);
190192
return $dt;
@@ -199,7 +201,7 @@ public function getEditedDateTime() {
199201
if (is_null($this->edited_datetime)) {
200202
return $this->edited_datetime;
201203
} else {
202-
$tz = new DateTimeZone("UTC");
204+
$tz = new DateTimeZone( 'Etc/UTC' );
203205
$dt = new DateTime($this->edited_datetime);
204206
$dt->setTimezone($tz);
205207
return $dt;
@@ -314,12 +316,82 @@ public function refresh() {
314316
$this->parent_id = $row->parent_id;
315317
$this->parent_type = $row->parent_type;
316318
$this->user_id = $row->user_id;
317-
Common::$cache->set($ck, serialize($row), 300);
319+
Common::$cache->set($ck, serialize($row), self::CACHE_TTL);
318320
return true;
319321
} catch (PDOException $e) {
320322
throw new QueryException("Cannot refresh comment", $e);
321323
}
322324
return false;
323325
}
324326

327+
public function save() {
328+
if (!isset(Common::$database)) {
329+
Common::$database = DatabaseDriver::getDatabaseObject();
330+
}
331+
try {
332+
$stmt = Common::$database->prepare('
333+
UPDATE
334+
`comments`
335+
SET
336+
`content` = :content,
337+
`created_datetime` = :created_dt,
338+
`edited_count` = :edited_count,
339+
`edited_datetime` = :edited_dt,
340+
`parent_id` = :parent_id,
341+
`parent_type` = :parent_type,
342+
`user_id` = :user_id
343+
WHERE
344+
`id` = :id
345+
LIMIT 1;
346+
');
347+
$stmt->bindParam(':content', $this->content, PDO::PARAM_STR);
348+
$stmt->bindParam(':created_dt', $this->created_datetime, PDO::PARAM_STR);
349+
$stmt->bindParam(':edited_count', $this->edited_count, PDO::PARAM_INT);
350+
$stmt->bindParam(':edited_dt', $this->edited_datetime, PDO::PARAM_STR);
351+
$stmt->bindParam(':id', $this->id, PDO::PARAM_INT);
352+
$stmt->bindParam(':parent_id', $this->parent_id, PDO::PARAM_INT);
353+
$stmt->bindParam(':parent_type', $this->parent_type, PDO::PARAM_INT);
354+
$stmt->bindParam(':user_id', $this->user_id, PDO::PARAM_INT);
355+
if (!$stmt->execute()) {
356+
throw new QueryException( 'Cannot save comment' );
357+
}
358+
$stmt->closeCursor();
359+
360+
$object = new StdClass();
361+
$object->content = $this->content;
362+
$object->created_datetime = $this->created_datetime;
363+
$object->edited_count = $this->edited_count;
364+
$object->edited_datetime = $this->edited_datetime;
365+
$object->id = $this->id;
366+
$object->parent_id = $this->parent_id;
367+
$object->parent_type = $this->parent_type;
368+
$object->user_id = $this->user_id;
369+
370+
Common::$cache->set(
371+
'bnetdocs-comment-' . $this->id, serialize( $object ), self::CACHE_TTL
372+
);
373+
374+
Common::$cache->delete(
375+
'bnetdocs-comment-' . $this->parent_type . '-' . $this->parent_id
376+
);
377+
378+
return true;
379+
} catch ( PDOException $e ) {
380+
throw new QueryException( 'Cannot save comment', $e );
381+
}
382+
return false;
383+
}
384+
385+
public function setContent( $value ) {
386+
$this->content = $value;
387+
}
388+
389+
public function setEditedCount( $value ) {
390+
$this->edited_count = $value;
391+
}
392+
393+
public function setEditedDateTime( \DateTime $value ) {
394+
$this->edited_datetime = $value->format( 'Y-m-d H:i:s' );
395+
}
396+
325397
}

src/libraries/Document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function getCreatedDateTime() {
176176
if (is_null($this->created_datetime)) {
177177
return $this->created_datetime;
178178
} else {
179-
$tz = new DateTimeZone("UTC");
179+
$tz = new DateTimeZone( 'Etc/UTC' );
180180
$dt = new DateTime($this->created_datetime);
181181
$dt->setTimezone($tz);
182182
return $dt;
@@ -229,7 +229,7 @@ public function getEditedDateTime() {
229229
if (is_null($this->edited_datetime)) {
230230
return $this->edited_datetime;
231231
} else {
232-
$tz = new DateTimeZone("UTC");
232+
$tz = new DateTimeZone( 'Etc/UTC' );
233233
$dt = new DateTime($this->edited_datetime);
234234
$dt->setTimezone($tz);
235235
return $dt;

src/libraries/Event.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function getEventDateTime() {
152152
if ( is_null( $this->event_datetime ) ) {
153153
return $this->event_datetime;
154154
} else {
155-
$tz = new DateTimeZone( 'UTC' );
155+
$tz = new DateTimeZone( 'Etc/UTC' );
156156
$dt = new DateTime( $this->event_datetime );
157157
$dt->setTimezone( $tz );
158158
return $dt;

0 commit comments

Comments
 (0)