|
| 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\Document; |
| 9 | +use \BNETDocs\Libraries\Exceptions\DocumentNotFoundException; |
| 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\Document\Delete as DocumentDeleteModel; |
| 15 | +use \BNETDocs\Views\Document\DeleteHtml as DocumentDeleteHtmlView; |
| 16 | +use \InvalidArgumentException; |
| 17 | + |
| 18 | +class Delete extends Controller { |
| 19 | + |
| 20 | + public function run(Router &$router) { |
| 21 | + switch ($router->getRequestPathExtension()) { |
| 22 | + case "htm": case "html": case "": |
| 23 | + $view = new DocumentDeleteHtmlView(); |
| 24 | + break; |
| 25 | + default: |
| 26 | + throw new UnspecifiedViewException(); |
| 27 | + } |
| 28 | + |
| 29 | + $data = $router->getRequestQueryArray(); |
| 30 | + $model = new DocumentDeleteModel(); |
| 31 | + $model->csrf_id = mt_rand(); |
| 32 | + $model->csrf_token = CSRF::generate($model->csrf_id); |
| 33 | + $model->document = null; |
| 34 | + $model->error = null; |
| 35 | + $model->id = (isset($data["id"]) ? $data["id"] : null); |
| 36 | + $model->title = null; |
| 37 | + $model->user_session = UserSession::load($router); |
| 38 | + |
| 39 | + try { $model->document = new Document($model->id); } |
| 40 | + catch (DocumentNotFoundException $e) { $model->document = null; } |
| 41 | + catch (InvalidArgumentException $e) { $model->document = null; } |
| 42 | + |
| 43 | + if ($model->document === null) { |
| 44 | + $model->error = "NOT_FOUND"; |
| 45 | + } else { |
| 46 | + $model->title = $model->document->getTitle(); |
| 47 | + |
| 48 | + if ($router->getRequestMethod() == "POST") { |
| 49 | + $this->tryDelete($router, $model); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + ob_start(); |
| 54 | + $view->render($model); |
| 55 | + $router->setResponseCode(200); |
| 56 | + $router->setResponseTTL(0); |
| 57 | + $router->setResponseHeader("Content-Type", $view->getMimeType()); |
| 58 | + $router->setResponseContent(ob_get_contents()); |
| 59 | + ob_end_clean(); |
| 60 | + } |
| 61 | + |
| 62 | + protected function tryDelete(Router &$router, DocumentDeleteModel &$model) { |
| 63 | + if (!isset($model->user_session)) { |
| 64 | + $model->error = "NOT_LOGGED_IN"; |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $data = $router->getRequestBodyArray(); |
| 69 | + $csrf_id = (isset($data["csrf_id" ]) ? $data["csrf_id" ] : null); |
| 70 | + $csrf_token = (isset($data["csrf_token"]) ? $data["csrf_token"] : null); |
| 71 | + $csrf_valid = CSRF::validate($csrf_id, $csrf_token); |
| 72 | + |
| 73 | + if (!$csrf_valid) { |
| 74 | + $model->error = "INVALID_CSRF"; |
| 75 | + return; |
| 76 | + } |
| 77 | + CSRF::invalidate($csrf_id); |
| 78 | + |
| 79 | + $model->error = false; |
| 80 | + |
| 81 | + $id = (int) $model->id; |
| 82 | + $user_id = $model->user_session->user_id; |
| 83 | + |
| 84 | + try { |
| 85 | + |
| 86 | + $success = Document::delete($id); |
| 87 | + |
| 88 | + } catch (QueryException $e) { |
| 89 | + |
| 90 | + // SQL error occurred. We can show a friendly message to the user while |
| 91 | + // also notifying this problem to staff. |
| 92 | + Logger::logException($e); |
| 93 | + |
| 94 | + $success = false; |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + if (!$success) { |
| 99 | + $model->error = "INTERNAL_ERROR"; |
| 100 | + } else { |
| 101 | + $model->error = false; |
| 102 | + } |
| 103 | + |
| 104 | + Logger::logEvent( |
| 105 | + "document_deleted", |
| 106 | + $user_id, |
| 107 | + getenv("REMOTE_ADDR"), |
| 108 | + json_encode([ |
| 109 | + "error" => $model->error, |
| 110 | + "document_id" => $id, |
| 111 | + ]) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments