|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Controllers\Document; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\CSRF; |
| 6 | +use \BNETDocs\Libraries\Common; |
| 7 | +use \BNETDocs\Libraries\Controller; |
| 8 | +use \BNETDocs\Libraries\DatabaseDriver; |
| 9 | +use \BNETDocs\Libraries\Document; |
| 10 | +use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException; |
| 11 | +use \BNETDocs\Libraries\Logger; |
| 12 | +use \BNETDocs\Libraries\Router; |
| 13 | +use \BNETDocs\Libraries\User; |
| 14 | +use \BNETDocs\Libraries\UserSession; |
| 15 | +use \BNETDocs\Models\Document\Create as DocumentCreateModel; |
| 16 | +use \BNETDocs\Views\Document\CreateHtml as DocumentCreateHtmlView; |
| 17 | + |
| 18 | +class Create extends Controller { |
| 19 | + |
| 20 | + public function run(Router &$router) { |
| 21 | + switch ($router->getRequestPathExtension()) { |
| 22 | + case "htm": case "html": case "": |
| 23 | + $view = new DocumentCreateHtmlView(); |
| 24 | + break; |
| 25 | + default: |
| 26 | + throw new UnspecifiedViewException(); |
| 27 | + } |
| 28 | + $model = new DocumentCreateModel(); |
| 29 | + $model->csrf_id = mt_rand(); |
| 30 | + $model->csrf_token = CSRF::generate($model->csrf_id, 900); // 15 mins |
| 31 | + $model->error = null; |
| 32 | + $model->user_session = UserSession::load($router); |
| 33 | + $model->user = (isset($model->user_session) ? |
| 34 | + new User($model->user_session->user_id) : null); |
| 35 | + |
| 36 | + $model->acl_allowed = ($model->user && |
| 37 | + $model->user->getOptionsBitmask() & User::OPTION_ACL_DOCUMENT_CREATE |
| 38 | + ); |
| 39 | + |
| 40 | + if ($router->getRequestMethod() == "POST") { |
| 41 | + $this->handlePost($router, $model); |
| 42 | + } else if ($router->getRequestMethod() == "GET") { |
| 43 | + $model->markdown = true; |
| 44 | + } |
| 45 | + |
| 46 | + ob_start(); |
| 47 | + $view->render($model); |
| 48 | + $router->setResponseCode(($model->acl_allowed ? 200 : 403)); |
| 49 | + $router->setResponseTTL(0); |
| 50 | + $router->setResponseHeader("Content-Type", $view->getMimeType()); |
| 51 | + $router->setResponseContent(ob_get_contents()); |
| 52 | + ob_end_clean(); |
| 53 | + } |
| 54 | + |
| 55 | + protected function handlePost(Router &$router, DocumentCreateModel &$model) { |
| 56 | + if (!$model->acl_allowed) { |
| 57 | + $model->error = "ACL_NOT_SET"; |
| 58 | + return; |
| 59 | + } |
| 60 | + if (!isset(Common::$database)) { |
| 61 | + Common::$database = DatabaseDriver::getDatabaseObject(); |
| 62 | + } |
| 63 | + $data = $router->getRequestBodyArray(); |
| 64 | + $csrf_id = (isset($data["csrf_id" ]) ? $data["csrf_id" ] : null); |
| 65 | + $csrf_token = (isset($data["csrf_token"]) ? $data["csrf_token"] : null); |
| 66 | + $csrf_valid = CSRF::validate($csrf_id, $csrf_token); |
| 67 | + $title = (isset($data["title" ]) ? $data["title" ] : null); |
| 68 | + $markdown = (isset($data["markdown" ]) ? $data["markdown" ] : null); |
| 69 | + $content = (isset($data["content" ]) ? $data["content" ] : null); |
| 70 | + $publish = (isset($data["publish" ]) ? $data["publish" ] : null); |
| 71 | + $save = (isset($data["save" ]) ? $data["save" ] : null); |
| 72 | + |
| 73 | + $model->title = $title; |
| 74 | + $model->markdown = $markdown; |
| 75 | + $model->content = $content; |
| 76 | + |
| 77 | + if (!$csrf_valid) { |
| 78 | + $model->error = "INVALID_CSRF"; |
| 79 | + return; |
| 80 | + } |
| 81 | + CSRF::invalidate($csrf_id); |
| 82 | + |
| 83 | + if (empty($title)) { |
| 84 | + $model->error = "EMPTY_TITLE"; |
| 85 | + } else if (empty($content)) { |
| 86 | + $model->error = "EMPTY_CONTENT"; |
| 87 | + } |
| 88 | + |
| 89 | + $options_bitmask = 0; |
| 90 | + if ($markdown) $options_bitmask |= Document::OPTION_MARKDOWN; |
| 91 | + if ($publish ) $options_bitmask |= Document::OPTION_PUBLISHED; |
| 92 | + |
| 93 | + $user_id = $model->user_session->user_id; |
| 94 | + |
| 95 | + try { |
| 96 | + |
| 97 | + $success = Document::create( |
| 98 | + $user_id, $options_bitmask, $title, $content |
| 99 | + ); |
| 100 | + |
| 101 | + } catch (QueryException $e) { |
| 102 | + |
| 103 | + // SQL error occurred. We can show a friendly message to the user while |
| 104 | + // also notifying this problem to staff. |
| 105 | + Logger::logException($e); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + if (!$success) { |
| 110 | + $model->error = "INTERNAL_ERROR"; |
| 111 | + } else { |
| 112 | + $model->error = false; |
| 113 | + } |
| 114 | + |
| 115 | + Logger::logEvent( |
| 116 | + "document_created", |
| 117 | + $user_id, |
| 118 | + getenv("REMOTE_ADDR"), |
| 119 | + json_encode([ |
| 120 | + "error" => $model->error, |
| 121 | + "options_bitmask" => $options_bitmask, |
| 122 | + "title" => $title, |
| 123 | + "content" => $content, |
| 124 | + ]) |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments