From 265b85d489986d805cc9f5791df1478e374202a1 Mon Sep 17 00:00:00 2001 From: Floorb <132411956+Neetpone@users.noreply.github.com> Date: Wed, 5 Jul 2023 03:22:09 -0400 Subject: [PATCH] chore: move paste actions to paste_action.php to require admin authentication --- includes/Models/AdminLog.php | 8 +++-- includes/common.php | 12 ++++--- includes/functions.php | 13 ++++++++ public/admin/common.php | 17 ++++------ public/admin/dashboard.php | 2 ++ public/admin/index.php | 20 ++++++------ public/admin/paste_action.php | 60 +++++++++++++++++++++++++++++++++++ public/paste.php | 37 --------------------- theme/bulma/view.php | 3 +- 9 files changed, 106 insertions(+), 66 deletions(-) create mode 100644 public/admin/paste_action.php diff --git a/includes/Models/AdminLog.php b/includes/Models/AdminLog.php index f50f797..750d08a 100644 --- a/includes/Models/AdminLog.php +++ b/includes/Models/AdminLog.php @@ -7,15 +7,19 @@ class AdminLog extends Model { public const ACTION_LOGIN = 0; public const ACTION_FAIL_LOGIN = 1; public const ACTION_EDIT_CONFIG = 2; + public const ACTION_HIDE_PASTE = 3; + public const ACTION_BLANK_PASTE = 4; public const ACTION_NAMES = [ 'Login', 'Failed Login', - 'Edit Config' + 'Edit Config', + 'Hide Paste', + 'Blank Paste' ]; protected $table = 'admin_logs'; - protected $fillable = ['user_id', 'action', 'ip', 'time']; + protected $fillable = ['user_id', 'action', 'ip', 'time', 'message']; public $timestamps = false; diff --git a/includes/common.php b/includes/common.php index 2e16c0f..e738d98 100644 --- a/includes/common.php +++ b/includes/common.php @@ -33,12 +33,16 @@ function urlForPage($page = '') : string { return (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . '/' . $page; } -function urlForPaste(Paste $paste) : string { - if (PP_MOD_REWRITE) { - return "/{$paste->id}"; +function urlForPaste(int | Paste $paste) : string { + if (!is_int($paste)) { + $paste = $paste->id; } - return "/paste.php?id={$paste->id}"; + if (PP_MOD_REWRITE) { + return "/{$paste}"; + } + + return "/paste.php?id={$paste}"; } function urlForReport(Paste $paste) : string { diff --git a/includes/functions.php b/includes/functions.php index c698bab..4fc8fe6 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,6 +1,8 @@ $admin->id, + 'action' => $action, + 'ip' => $_SERVER['REMOTE_ADDR'], + 'message' => $message + ]); + + $log->save(); } \ No newline at end of file diff --git a/public/admin/common.php b/public/admin/common.php index 94f0dfe..9762837 100644 --- a/public/admin/common.php +++ b/public/admin/common.php @@ -5,25 +5,20 @@ if (!defined('IN_PONEPASTE')) { require_once('../../includes/common.php'); -use PonePaste\Models\AdminLog; use PonePaste\Models\User; -function updateAdminHistory(User $admin, int $action) : void { - $log = new AdminLog([ - 'user_id' => $admin->id, - 'action' => $action, - 'ip' => $_SERVER['REMOTE_ADDR'] - ]); - - $log->save(); -} - if ($current_user === null || $current_user->role < User::ROLE_MODERATOR) { header('Location: ..'); die(); } if (!isset($_SESSION['admin_login'])) { + // this is a hack, paste_id is set when POSTing to admin/paste_action.php, which we can only arrive at from a paste page + if (isset($_POST['paste_id'])) { + flashError('You must authenticate to perform that action.'); + $_SESSION['redirect_back'] = urlForPaste($_POST['paste_id']); + } + header('Location: .'); exit(); } diff --git a/public/admin/dashboard.php b/public/admin/dashboard.php index 15a0fc4..c86e2b5 100644 --- a/public/admin/dashboard.php +++ b/public/admin/dashboard.php @@ -231,6 +231,7 @@ $is_admin = $current_user->role >= User::ROLE_ADMIN;