|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Controllers\Comment; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\CSRF; |
| 6 | +use \BNETDocs\Libraries\Comment; |
| 7 | +use \BNETDocs\Libraries\Common; |
| 8 | +use \BNETDocs\Libraries\Controller; |
| 9 | +use \BNETDocs\Libraries\Exceptions\CommentNotFoundException; |
| 10 | +use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException; |
| 11 | +use \BNETDocs\Libraries\Logger; |
| 12 | +use \BNETDocs\Libraries\Router; |
| 13 | +use \BNETDocs\Libraries\UserSession; |
| 14 | +use \BNETDocs\Models\Comment\Delete as CommentDeleteModel; |
| 15 | +use \BNETDocs\Views\Comment\DeleteHtml as CommentDeleteHtmlView; |
| 16 | +use \InvalidArgumentException; |
| 17 | +use \UnexpectedValueException; |
| 18 | + |
| 19 | +class Delete extends Controller { |
| 20 | + |
| 21 | + public function run(Router &$router) { |
| 22 | + switch ($router->getRequestPathExtension()) { |
| 23 | + case "htm": case "html": case "": |
| 24 | + $view = new CommentDeleteHtmlView(); |
| 25 | + break; |
| 26 | + default: |
| 27 | + throw new UnspecifiedViewException(); |
| 28 | + } |
| 29 | + |
| 30 | + $data = $router->getRequestQueryArray(); |
| 31 | + $model = new CommentDeleteModel(); |
| 32 | + $model->comment = null; |
| 33 | + $model->csrf_id = mt_rand(); |
| 34 | + $model->csrf_token = CSRF::generate($model->csrf_id); |
| 35 | + $model->error = null; |
| 36 | + $model->id = (isset($data["id"]) ? $data["id"] : null); |
| 37 | + $model->parent_id = null; |
| 38 | + $model->parent_type = null; |
| 39 | + $model->title = null; |
| 40 | + $model->user_session = UserSession::load($router); |
| 41 | + |
| 42 | + try { $model->comment = new Comment($model->id); } |
| 43 | + catch (CommentNotFoundException $e) { $model->comment = null; } |
| 44 | + catch (InvalidArgumentException $e) { $model->comment = null; } |
| 45 | + |
| 46 | + if ($model->comment === null) { |
| 47 | + $model->error = "NOT_FOUND"; |
| 48 | + } else { |
| 49 | + $model->content = $model->comment->getContent(true); |
| 50 | + $model->parent_type = $model->comment->getParentType(); |
| 51 | + $model->parent_id = $model->comment->getParentId(); |
| 52 | + |
| 53 | + if ($router->getRequestMethod() == "POST") { |
| 54 | + $this->tryDelete($router, $model); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + ob_start(); |
| 59 | + $view->render($model); |
| 60 | + $router->setResponseCode(200); |
| 61 | + $router->setResponseTTL(0); |
| 62 | + $router->setResponseHeader("Content-Type", $view->getMimeType()); |
| 63 | + $router->setResponseContent(ob_get_contents()); |
| 64 | + ob_end_clean(); |
| 65 | + } |
| 66 | + |
| 67 | + protected function tryDelete(Router &$router, CommentDeleteModel &$model) { |
| 68 | + if (!isset($model->user_session)) { |
| 69 | + $model->error = "NOT_LOGGED_IN"; |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + $data = $router->getRequestBodyArray(); |
| 74 | + $csrf_id = (isset($data["csrf_id" ]) ? $data["csrf_id" ] : null); |
| 75 | + $csrf_token = (isset($data["csrf_token"]) ? $data["csrf_token"] : null); |
| 76 | + $csrf_valid = CSRF::validate($csrf_id, $csrf_token); |
| 77 | + |
| 78 | + if (!$csrf_valid) { |
| 79 | + $model->error = "INVALID_CSRF"; |
| 80 | + return; |
| 81 | + } |
| 82 | + CSRF::invalidate($csrf_id); |
| 83 | + |
| 84 | + $model->error = false; |
| 85 | + |
| 86 | + $id = (int) $model->id; |
| 87 | + $parent_type = (int) $model->parent_type; |
| 88 | + $parent_id = (int) $model->parent_id; |
| 89 | + $user_id = $model->user_session->user_id; |
| 90 | + |
| 91 | + $log_key = ""; |
| 92 | + switch ($parent_type) { |
| 93 | + case Comment::PARENT_TYPE_DOCUMENT: $log_key = "_document"; break; |
| 94 | + case Comment::PARENT_TYPE_COMMENT: $log_key = "_comment"; break; |
| 95 | + case Comment::PARENT_TYPE_NEWS_POST: $log_key = "_news"; break; |
| 96 | + case Comment::PARENT_TYPE_PACKET: $log_key = "_packet"; break; |
| 97 | + case Comment::PARENT_TYPE_SERVER: $log_key = "_server"; break; |
| 98 | + case Comment::PARENT_TYPE_USER: $log_key = "_user"; break; |
| 99 | + default: throw new UnexpectedValueException( |
| 100 | + "Parent type: " . $parent_type |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + |
| 106 | + $success = Comment::delete($id, $parent_type, $parent_id); |
| 107 | + |
| 108 | + } catch (QueryException $e) { |
| 109 | + |
| 110 | + // SQL error occurred. We can show a friendly message to the user while |
| 111 | + // also notifying this problem to staff. |
| 112 | + Logger::logException($e); |
| 113 | + |
| 114 | + $success = false; |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + if (!$success) { |
| 119 | + $model->error = "INTERNAL_ERROR"; |
| 120 | + } else { |
| 121 | + $model->error = false; |
| 122 | + } |
| 123 | + |
| 124 | + Logger::logEvent( |
| 125 | + "comment_deleted" . $log_key, |
| 126 | + $user_id, |
| 127 | + getenv("REMOTE_ADDR"), |
| 128 | + json_encode([ |
| 129 | + "error" => $model->error, |
| 130 | + "comment_id" => $id, |
| 131 | + "parent_type" => $parent_type, |
| 132 | + "parent_id" => $parent_id |
| 133 | + ]) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments