|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Controllers\Packet; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\CSRF; |
| 6 | +use \BNETDocs\Libraries\Exceptions\PacketNotFoundException; |
| 7 | +use \BNETDocs\Libraries\Logger; |
| 8 | +use \BNETDocs\Libraries\Packet; |
| 9 | +use \BNETDocs\Libraries\User; |
| 10 | +use \BNETDocs\Models\Packet\Edit as PacketEditModel; |
| 11 | +use \CarlBennett\MVC\Libraries\Common; |
| 12 | +use \CarlBennett\MVC\Libraries\Controller; |
| 13 | +use \CarlBennett\MVC\Libraries\DatabaseDriver; |
| 14 | +use \CarlBennett\MVC\Libraries\Router; |
| 15 | +use \CarlBennett\MVC\Libraries\View; |
| 16 | +use \DateTime; |
| 17 | +use \DateTimeZone; |
| 18 | +use \InvalidArgumentException; |
| 19 | + |
| 20 | +class Edit extends Controller { |
| 21 | + |
| 22 | + public function &run(Router &$router, View &$view, array &$args) { |
| 23 | + |
| 24 | + $data = $router->getRequestQueryArray(); |
| 25 | + $model = new PacketEditModel(); |
| 26 | + $model->content = null; |
| 27 | + $model->csrf_id = mt_rand(); |
| 28 | + $model->csrf_token = CSRF::generate($model->csrf_id, 900); // 15 mins |
| 29 | + $model->packet = null; |
| 30 | + $model->packet_id = (isset($data["id"]) ? $data["id"] : null); |
| 31 | + $model->error = null; |
| 32 | + $model->markdown = null; |
| 33 | + $model->published = null; |
| 34 | + $model->title = null; |
| 35 | + $model->user = ( |
| 36 | + isset($_SESSION['user_id']) ? new User($_SESSION['user_id']) : null |
| 37 | + ); |
| 38 | + |
| 39 | + $model->acl_allowed = ($model->user && $model->user->getAcl( |
| 40 | + User::OPTION_ACL_PACKET_MODIFY |
| 41 | + )); |
| 42 | + |
| 43 | + try { $model->packet = new Packet($model->packet_id); } |
| 44 | + catch (PacketNotFoundException $e) { $model->packet = null; } |
| 45 | + catch (InvalidArgumentException $e) { $model->packet = null; } |
| 46 | + |
| 47 | + if ($model->packet === null) { |
| 48 | + $model->error = "NOT_FOUND"; |
| 49 | + } else { |
| 50 | + $flags = $model->packet->getOptionsBitmask(); |
| 51 | + |
| 52 | + $model->content = $model->packet->getContent(false); |
| 53 | + $model->markdown = ($flags & Packet::OPTION_MARKDOWN); |
| 54 | + $model->published = ($flags & Packet::OPTION_PUBLISHED); |
| 55 | + $model->title = $model->packet->getTitle(); |
| 56 | + |
| 57 | + if ($router->getRequestMethod() == "POST") { |
| 58 | + $this->handlePost($router, $model); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + $view->render($model); |
| 63 | + |
| 64 | + $model->_responseCode = ($model->acl_allowed ? 200 : 403); |
| 65 | + $model->_responseHeaders["Content-Type"] = $view->getMimeType(); |
| 66 | + $model->_responseTTL = 0; |
| 67 | + |
| 68 | + return $model; |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + protected function handlePost(Router &$router, PacketEditModel &$model) { |
| 73 | + if (!$model->acl_allowed) { |
| 74 | + $model->error = "ACL_NOT_SET"; |
| 75 | + return; |
| 76 | + } |
| 77 | + if (!isset(Common::$database)) { |
| 78 | + Common::$database = DatabaseDriver::getDatabaseObject(); |
| 79 | + } |
| 80 | + |
| 81 | + $data = $router->getRequestBodyArray(); |
| 82 | + $csrf_id = (isset($data["csrf_id" ]) ? $data["csrf_id" ] : null); |
| 83 | + $csrf_token = (isset($data["csrf_token"]) ? $data["csrf_token"] : null); |
| 84 | + $csrf_valid = CSRF::validate($csrf_id, $csrf_token); |
| 85 | + $category = (isset($data["category" ]) ? $data["category" ] : null); |
| 86 | + $title = (isset($data["title" ]) ? $data["title" ] : null); |
| 87 | + $markdown = (isset($data["markdown" ]) ? $data["markdown" ] : null); |
| 88 | + $content = (isset($data["content" ]) ? $data["content" ] : null); |
| 89 | + $publish = (isset($data["publish" ]) ? $data["publish" ] : null); |
| 90 | + $save = (isset($data["save" ]) ? $data["save" ] : null); |
| 91 | + |
| 92 | + $model->category = $category; |
| 93 | + $model->title = $title; |
| 94 | + $model->markdown = $markdown; |
| 95 | + $model->content = $content; |
| 96 | + |
| 97 | + if (!$csrf_valid) { |
| 98 | + $model->error = "INVALID_CSRF"; |
| 99 | + return; |
| 100 | + } |
| 101 | + CSRF::invalidate($csrf_id); |
| 102 | + |
| 103 | + if (empty($title)) { |
| 104 | + $model->error = "EMPTY_TITLE"; |
| 105 | + } else if (empty($content)) { |
| 106 | + $model->error = "EMPTY_CONTENT"; |
| 107 | + } |
| 108 | + |
| 109 | + $user_id = $model->user->getId(); |
| 110 | + |
| 111 | + try { |
| 112 | + |
| 113 | + $model->packet->setTitle($model->title); |
| 114 | + $model->packet->setMarkdown($model->markdown); |
| 115 | + $model->packet->setContent($model->content); |
| 116 | + $model->packet->setPublished($publish); |
| 117 | + |
| 118 | + $model->packet->setEditedCount( |
| 119 | + $model->packet->getEditedCount() + 1 |
| 120 | + ); |
| 121 | + $model->packet->setEditedDateTime( |
| 122 | + new DateTime("now", new DateTimeZone("UTC")) |
| 123 | + ); |
| 124 | + |
| 125 | + $success = $model->packet->save(); |
| 126 | + |
| 127 | + } catch (QueryException $e) { |
| 128 | + |
| 129 | + // SQL error occurred. We can show a friendly message to the user while |
| 130 | + // also notifying this problem to staff. |
| 131 | + Logger::logException($e); |
| 132 | + |
| 133 | + $success = false; |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + if (!$success) { |
| 138 | + $model->error = "INTERNAL_ERROR"; |
| 139 | + } else { |
| 140 | + $model->error = false; |
| 141 | + } |
| 142 | + |
| 143 | + Logger::logEvent( |
| 144 | + "packet_edited", |
| 145 | + $user_id, |
| 146 | + getenv("REMOTE_ADDR"), |
| 147 | + json_encode([ |
| 148 | + "error" => $model->error, |
| 149 | + "packet_id" => $model->packet_id, |
| 150 | + "options_bitmask" => $model->packet->getOptionsBitmask(), |
| 151 | + "title" => $model->packet->getTitle(), |
| 152 | + "content" => $model->packet->getContent(false), |
| 153 | + ]) |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments