|
| 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 | +} |
0 commit comments